]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PM: Add sysfs files to represent time spent in hardware sleep state
authorMario Limonciello <mario.limonciello@amd.com>
Mon, 17 Apr 2023 15:27:05 +0000 (10:27 -0500)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 20 Apr 2023 17:06:12 +0000 (19:06 +0200)
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>
Documentation/ABI/testing/sysfs-power
include/linux/suspend.h
kernel/power/main.c

index f99d433ff3117ce16f57b101afe48202759a32c9..a3942b1036e2514bcf4c2824a5628355fde73850 100644 (file)
@@ -413,6 +413,35 @@ Description:
                The /sys/power/suspend_stats/last_failed_step file contains
                the last failed step in the suspend/resume path.
 
+What:          /sys/power/suspend_stats/last_hw_sleep
+Date:          June 2023
+Contact:       Mario Limonciello <mario.limonciello@amd.com>
+Description:
+               The /sys/power/suspend_stats/last_hw_sleep file
+               contains the duration of time spent in a hardware sleep
+               state in the most recent system suspend-resume cycle.
+               This number is measured in microseconds.
+
+What:          /sys/power/suspend_stats/total_hw_sleep
+Date:          June 2023
+Contact:       Mario Limonciello <mario.limonciello@amd.com>
+Description:
+               The /sys/power/suspend_stats/total_hw_sleep file
+               contains the aggregate of time spent in a hardware sleep
+               state since the kernel was booted. This number
+               is measured in microseconds.
+
+What:          /sys/power/suspend_stats/max_hw_sleep
+Date:          June 2023
+Contact:       Mario Limonciello <mario.limonciello@amd.com>
+Description:
+               The /sys/power/suspend_stats/max_hw_sleep file
+               contains the maximum amount of time that the hardware can
+               report for time spent in a hardware sleep state. When sleep
+               cycles are longer than this time, the values for
+               'total_hw_sleep' and 'last_hw_sleep' may not be accurate.
+               This number is measured in microseconds.
+
 What:          /sys/power/sync_on_suspend
 Date:          October 2019
 Contact:       Jonas Meurer <jonas@freesources.org>
index cfe19a02891855ff9fe15f1b78bcf0ecb2a30f80..d0d4598a7b3f6f71a433f12a8e6b5e15f263f9ff 100644 (file)
@@ -68,6 +68,9 @@ struct suspend_stats {
        int     last_failed_errno;
        int     errno[REC_FAILED_NUM];
        int     last_failed_step;
+       u64     last_hw_sleep;
+       u64     total_hw_sleep;
+       u64     max_hw_sleep;
        enum suspend_stat_step  failed_steps[REC_FAILED_NUM];
 };
 
@@ -489,6 +492,8 @@ void restore_processor_state(void);
 extern int register_pm_notifier(struct notifier_block *nb);
 extern int unregister_pm_notifier(struct notifier_block *nb);
 extern void ksys_sync_helper(void);
+extern void pm_report_hw_sleep_time(u64 t);
+extern void pm_report_max_hw_sleep(u64 t);
 
 #define pm_notifier(fn, pri) {                         \
        static struct notifier_block fn##_nb =                  \
@@ -526,6 +531,9 @@ static inline int unregister_pm_notifier(struct notifier_block *nb)
        return 0;
 }
 
+static inline void pm_report_hw_sleep_time(u64 t) {};
+static inline void pm_report_max_hw_sleep(u64 t) {};
+
 static inline void ksys_sync_helper(void) {}
 
 #define pm_notifier(fn, pri)   do { (void)(fn); } while (0)
index 31ec4a9b9d7045315ed60c26c8b385be74eed652..3113ec2f1db4d1d0a339690da76754cfb6b9b3d0 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright (c) 2003 Open Source Development Lab
  */
 
+#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/kobject.h>
 #include <linux/string.h>
@@ -83,6 +84,19 @@ int unregister_pm_notifier(struct notifier_block *nb)
 }
 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
 
+void pm_report_hw_sleep_time(u64 t)
+{
+       suspend_stats.last_hw_sleep = t;
+       suspend_stats.total_hw_sleep += t;
+}
+EXPORT_SYMBOL_GPL(pm_report_hw_sleep_time);
+
+void pm_report_max_hw_sleep(u64 t)
+{
+       suspend_stats.max_hw_sleep = t;
+}
+EXPORT_SYMBOL_GPL(pm_report_max_hw_sleep);
+
 int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down)
 {
        int ret;
@@ -314,24 +328,27 @@ static char *suspend_step_name(enum suspend_stat_step step)
        }
 }
 
-#define suspend_attr(_name)                                    \
+#define suspend_attr(_name, format_str)                                \
 static ssize_t _name##_show(struct kobject *kobj,              \
                struct kobj_attribute *attr, char *buf)         \
 {                                                              \
-       return sprintf(buf, "%d\n", suspend_stats._name);       \
+       return sprintf(buf, format_str, suspend_stats._name);   \
 }                                                              \
 static struct kobj_attribute _name = __ATTR_RO(_name)
 
-suspend_attr(success);
-suspend_attr(fail);
-suspend_attr(failed_freeze);
-suspend_attr(failed_prepare);
-suspend_attr(failed_suspend);
-suspend_attr(failed_suspend_late);
-suspend_attr(failed_suspend_noirq);
-suspend_attr(failed_resume);
-suspend_attr(failed_resume_early);
-suspend_attr(failed_resume_noirq);
+suspend_attr(success, "%d\n");
+suspend_attr(fail, "%d\n");
+suspend_attr(failed_freeze, "%d\n");
+suspend_attr(failed_prepare, "%d\n");
+suspend_attr(failed_suspend, "%d\n");
+suspend_attr(failed_suspend_late, "%d\n");
+suspend_attr(failed_suspend_noirq, "%d\n");
+suspend_attr(failed_resume, "%d\n");
+suspend_attr(failed_resume_early, "%d\n");
+suspend_attr(failed_resume_noirq, "%d\n");
+suspend_attr(last_hw_sleep, "%llu\n");
+suspend_attr(total_hw_sleep, "%llu\n");
+suspend_attr(max_hw_sleep, "%llu\n");
 
 static ssize_t last_failed_dev_show(struct kobject *kobj,
                struct kobj_attribute *attr, char *buf)
@@ -391,12 +408,30 @@ static struct attribute *suspend_attrs[] = {
        &last_failed_dev.attr,
        &last_failed_errno.attr,
        &last_failed_step.attr,
+       &last_hw_sleep.attr,
+       &total_hw_sleep.attr,
+       &max_hw_sleep.attr,
        NULL,
 };
 
+static umode_t suspend_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
+{
+       if (attr != &last_hw_sleep.attr &&
+           attr != &total_hw_sleep.attr &&
+           attr != &max_hw_sleep.attr)
+               return 0444;
+
+#ifdef CONFIG_ACPI
+       if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
+               return 0444;
+#endif
+       return 0;
+}
+
 static const struct attribute_group suspend_attr_group = {
        .name = "suspend_stats",
        .attrs = suspend_attrs,
+       .is_visible = suspend_attr_is_visible,
 };
 
 #ifdef CONFIG_DEBUG_FS