Skip to content

Commit b52124a

Browse files
superm1rafaeljw
authored andcommitted
PM: Add sysfs files to represent time spent in hardware sleep state
Userspace can't easily discover how much of a sleep cycle was spent in a hardware sleep state without using kernel tracing and vendor specific sysfs or debugfs files. To make this information more discoverable, introduce 3 new sysfs files: 1) The time spent in a hw sleep state for last cycle. 2) The time spent in a hw sleep state since the kernel booted 3) The maximum time that the hardware can report for a sleep cycle. All of these files will be present only if the system supports s2idle. Reviewed-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 6a8f57a commit b52124a

3 files changed

Lines changed: 84 additions & 12 deletions

File tree

Documentation/ABI/testing/sysfs-power

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,35 @@ Description:
413413
The /sys/power/suspend_stats/last_failed_step file contains
414414
the last failed step in the suspend/resume path.
415415

416+
What: /sys/power/suspend_stats/last_hw_sleep
417+
Date: June 2023
418+
Contact: Mario Limonciello <mario.limonciello@amd.com>
419+
Description:
420+
The /sys/power/suspend_stats/last_hw_sleep file
421+
contains the duration of time spent in a hardware sleep
422+
state in the most recent system suspend-resume cycle.
423+
This number is measured in microseconds.
424+
425+
What: /sys/power/suspend_stats/total_hw_sleep
426+
Date: June 2023
427+
Contact: Mario Limonciello <mario.limonciello@amd.com>
428+
Description:
429+
The /sys/power/suspend_stats/total_hw_sleep file
430+
contains the aggregate of time spent in a hardware sleep
431+
state since the kernel was booted. This number
432+
is measured in microseconds.
433+
434+
What: /sys/power/suspend_stats/max_hw_sleep
435+
Date: June 2023
436+
Contact: Mario Limonciello <mario.limonciello@amd.com>
437+
Description:
438+
The /sys/power/suspend_stats/max_hw_sleep file
439+
contains the maximum amount of time that the hardware can
440+
report for time spent in a hardware sleep state. When sleep
441+
cycles are longer than this time, the values for
442+
'total_hw_sleep' and 'last_hw_sleep' may not be accurate.
443+
This number is measured in microseconds.
444+
416445
What: /sys/power/sync_on_suspend
417446
Date: October 2019
418447
Contact: Jonas Meurer <jonas@freesources.org>

include/linux/suspend.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ struct suspend_stats {
6868
int last_failed_errno;
6969
int errno[REC_FAILED_NUM];
7070
int last_failed_step;
71+
u64 last_hw_sleep;
72+
u64 total_hw_sleep;
73+
u64 max_hw_sleep;
7174
enum suspend_stat_step failed_steps[REC_FAILED_NUM];
7275
};
7376

@@ -489,6 +492,8 @@ void restore_processor_state(void);
489492
extern int register_pm_notifier(struct notifier_block *nb);
490493
extern int unregister_pm_notifier(struct notifier_block *nb);
491494
extern void ksys_sync_helper(void);
495+
extern void pm_report_hw_sleep_time(u64 t);
496+
extern void pm_report_max_hw_sleep(u64 t);
492497

493498
#define pm_notifier(fn, pri) { \
494499
static struct notifier_block fn##_nb = \
@@ -526,6 +531,9 @@ static inline int unregister_pm_notifier(struct notifier_block *nb)
526531
return 0;
527532
}
528533

534+
static inline void pm_report_hw_sleep_time(u64 t) {};
535+
static inline void pm_report_max_hw_sleep(u64 t) {};
536+
529537
static inline void ksys_sync_helper(void) {}
530538

531539
#define pm_notifier(fn, pri) do { (void)(fn); } while (0)

kernel/power/main.c

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright (c) 2003 Open Source Development Lab
77
*/
88

9+
#include <linux/acpi.h>
910
#include <linux/export.h>
1011
#include <linux/kobject.h>
1112
#include <linux/string.h>
@@ -83,6 +84,19 @@ int unregister_pm_notifier(struct notifier_block *nb)
8384
}
8485
EXPORT_SYMBOL_GPL(unregister_pm_notifier);
8586

87+
void pm_report_hw_sleep_time(u64 t)
88+
{
89+
suspend_stats.last_hw_sleep = t;
90+
suspend_stats.total_hw_sleep += t;
91+
}
92+
EXPORT_SYMBOL_GPL(pm_report_hw_sleep_time);
93+
94+
void pm_report_max_hw_sleep(u64 t)
95+
{
96+
suspend_stats.max_hw_sleep = t;
97+
}
98+
EXPORT_SYMBOL_GPL(pm_report_max_hw_sleep);
99+
86100
int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down)
87101
{
88102
int ret;
@@ -314,24 +328,27 @@ static char *suspend_step_name(enum suspend_stat_step step)
314328
}
315329
}
316330

317-
#define suspend_attr(_name) \
331+
#define suspend_attr(_name, format_str) \
318332
static ssize_t _name##_show(struct kobject *kobj, \
319333
struct kobj_attribute *attr, char *buf) \
320334
{ \
321-
return sprintf(buf, "%d\n", suspend_stats._name); \
335+
return sprintf(buf, format_str, suspend_stats._name); \
322336
} \
323337
static struct kobj_attribute _name = __ATTR_RO(_name)
324338

325-
suspend_attr(success);
326-
suspend_attr(fail);
327-
suspend_attr(failed_freeze);
328-
suspend_attr(failed_prepare);
329-
suspend_attr(failed_suspend);
330-
suspend_attr(failed_suspend_late);
331-
suspend_attr(failed_suspend_noirq);
332-
suspend_attr(failed_resume);
333-
suspend_attr(failed_resume_early);
334-
suspend_attr(failed_resume_noirq);
339+
suspend_attr(success, "%d\n");
340+
suspend_attr(fail, "%d\n");
341+
suspend_attr(failed_freeze, "%d\n");
342+
suspend_attr(failed_prepare, "%d\n");
343+
suspend_attr(failed_suspend, "%d\n");
344+
suspend_attr(failed_suspend_late, "%d\n");
345+
suspend_attr(failed_suspend_noirq, "%d\n");
346+
suspend_attr(failed_resume, "%d\n");
347+
suspend_attr(failed_resume_early, "%d\n");
348+
suspend_attr(failed_resume_noirq, "%d\n");
349+
suspend_attr(last_hw_sleep, "%llu\n");
350+
suspend_attr(total_hw_sleep, "%llu\n");
351+
suspend_attr(max_hw_sleep, "%llu\n");
335352

336353
static ssize_t last_failed_dev_show(struct kobject *kobj,
337354
struct kobj_attribute *attr, char *buf)
@@ -391,12 +408,30 @@ static struct attribute *suspend_attrs[] = {
391408
&last_failed_dev.attr,
392409
&last_failed_errno.attr,
393410
&last_failed_step.attr,
411+
&last_hw_sleep.attr,
412+
&total_hw_sleep.attr,
413+
&max_hw_sleep.attr,
394414
NULL,
395415
};
396416

417+
static umode_t suspend_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
418+
{
419+
if (attr != &last_hw_sleep.attr &&
420+
attr != &total_hw_sleep.attr &&
421+
attr != &max_hw_sleep.attr)
422+
return 0444;
423+
424+
#ifdef CONFIG_ACPI
425+
if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
426+
return 0444;
427+
#endif
428+
return 0;
429+
}
430+
397431
static const struct attribute_group suspend_attr_group = {
398432
.name = "suspend_stats",
399433
.attrs = suspend_attrs,
434+
.is_visible = suspend_attr_is_visible,
400435
};
401436

402437
#ifdef CONFIG_DEBUG_FS

0 commit comments

Comments
 (0)