Re: [PATCH v3 1/2] PM: runtime: add additional suspend/resume metrics

From: Rafael J. Wysocki (Intel)

Date: Wed Nov 25 2026 - 07:58:59 EST


On Wed, Nov 25, 2026 at 1:54 PM Rafael J. Wysocki (Intel)
<rafael@xxxxxxxxxx> wrote:
>
> On Mon, Nov 23, 2026 at 10:07 AM Joshua Crofts <crofts@xxxxxxxxxx> wrote:
> >
> > The current implementation only logs the total accumulated time a device
> > spends in suspended or active states.
> >
> > Add tracking for:
> > - resume_count : number of successful transitions to active
> > - suspend_count: number of successful transitions to suspended
> > - last_suspended_time: duration of the most recent sleep cycle (ns)
> > - min_suspended_time / max_suspended_time: shortest and longest sleep
> > cycle durations to help detect power thrashing (ns)
>
> I would mention that they would be only reachable through a debugfs interface.
>
> > Signed-off-by: Joshua Crofts <crofts@xxxxxxxxxx>
> > ---
> > drivers/base/power/runtime.c | 93 +++++++++++++++++++++++++++++++++++++++++---
> > include/linux/pm.h | 16 +++++++-
> > 2 files changed, 103 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c
> > index 4e9c23059a40..17aee68366cf 100644
> > --- a/drivers/base/power/runtime.c
> > +++ b/drivers/base/power/runtime.c
> > @@ -5,6 +5,8 @@
> > * Copyright (c) 2009 Rafael J. Wysocki <rjw@xxxxxxx>, Novell Inc.
> > * Copyright (C) 2010 Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
> > */
> > +#include <linux/pm.h>
> > +#include <linux/timekeeping.h>
> > #include <linux/sched/mm.h>
> > #include <linux/ktime.h>
> > #include <linux/hrtimer.h>
> > @@ -103,11 +105,70 @@ static void update_pm_runtime_accounting(struct device *dev)
> > dev->power.active_time += delta;
> > }
> >
> > +#ifdef CONFIG_DEBUG_FS
> > +static void rpm_account_suspend(struct device *dev)
> > +{
> > + dev->power.suspend_time = dev->power.accounting_timestamp;
> > + dev->power.suspend_count++;
> > +}
> > +
> > +static void rpm_account_sleep_end(struct device *dev)
> > +{
> > + u64 duration;
> > +
> > + if (!dev->power.suspend_time)
> > + return;
> > +
> > + if (dev->power.accounting_timestamp <= dev->power.suspend_time)
> > + return;
> > +
> > + duration = dev->power.accounting_timestamp - dev->power.suspend_time;
> > + dev->power.last_suspended_time = duration;
> > +
> > + /* Update min/max extrema at the exact same moment as last_suspended_time */
> > + if (!dev->power.min_suspended_time || duration < dev->power.min_suspended_time)
> > + dev->power.min_suspended_time = duration;
> > +
> > + if (duration > dev->power.max_suspended_time)
> > + dev->power.max_suspended_time = duration;
> > +}
> > +
> > +static void rpm_account_resume_complete(struct device *dev)
> > +{
> > + dev->power.resume_count++;
> > + dev->power.suspend_time = 0;
> > +}
> > +#endif /* CONFIG_DEBUG_FS */
> > +
> > static void __update_runtime_status(struct device *dev, enum rpm_status status)
> > {
> > +#ifdef CONFIG_DEBUG_FS
> > + enum rpm_status old_status = dev->power.runtime_status;
> > +#endif /* CONFIG_DEBUG_FS */
> > +
> > update_pm_runtime_accounting(dev);
> > trace_rpm_status(dev, status);
> > dev->power.runtime_status = status;
> > +
> > +#ifdef CONFIG_DEBUG_FS
> > + if (dev->power.disable_depth > 0) {
> > + if (status == RPM_ACTIVE)
> > + dev->power.suspend_time = 0;
> > + return;
> > + }
> > +
> > + if ((old_status == RPM_SUSPENDING || old_status == RPM_ACTIVE) &&
> > + status == RPM_SUSPENDED)
> > + rpm_account_suspend(dev);
> > + else if (old_status == RPM_RESUMING && status == RPM_SUSPENDED)
> > + dev->power.suspend_time = dev->power.accounting_timestamp;
> > + else if (old_status == RPM_SUSPENDED && status != RPM_SUSPENDED)
> > + rpm_account_sleep_end(dev);
> > +
> > + if ((old_status == RPM_RESUMING || old_status == RPM_SUSPENDED) &&
> > + status == RPM_ACTIVE)
> > + rpm_account_resume_complete(dev);
> > +#endif /* CONFIG_DEBUG_FS */
>
> Please move the code under CONFIG_DEBUG_FS in this function to a
> separate function called by it and defined as an empty stub if
> CONFIG_DEBUG_FS is unset.
>
> > }
> >
> > static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
> > @@ -1580,6 +1641,7 @@ EXPORT_SYMBOL_GPL(__pm_runtime_disable);
> > void pm_runtime_enable(struct device *dev)
> > {
> > unsigned long flags;
> > + u64 now;
> >
> > spin_lock_irqsave(&dev->power.lock, flags);
> >
> > @@ -1596,12 +1658,24 @@ void pm_runtime_enable(struct device *dev)
> > dump_stack();
> > }
> > dev->power.last_status = RPM_INVALID;
> > - dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
> > + now = ktime_get_mono_fast_ns();
> >
> > - if (dev->power.runtime_status == RPM_SUSPENDED &&
> > - !dev->power.ignore_children &&
> > - atomic_read(&dev->power.child_count) > 0)
> > - dev_warn(dev, "Enabling runtime PM for inactive device with active children\n");
> > + if (dev->power.runtime_status == RPM_SUSPENDED) {
> > +#ifdef CONFIG_DEBUG_FS
> > + if (dev->power.suspend_time) {
> > + if (dev->power.accounting_timestamp
> > + && now > dev->power.accounting_timestamp)
> > + dev->power.suspend_time += now - dev->power.accounting_timestamp;
> > + } else {
> > + dev->power.suspend_time = now;
> > + }
> > +#endif /* CONFIG_DEBUG_FS */
>
> Same here.
>
> Plus you can use dev->power.accounting_timestamp as "now" in the new
> function after setting it above.
>
> > + if (!dev->power.ignore_children &&
> > + atomic_read(&dev->power.child_count) > 0)
> > + dev_warn(dev, "Enabling runtime PM for inactive device with active children\n");
> > + }
> > +
> > + dev->power.accounting_timestamp = now;
> >
> > out:
> > spin_unlock_irqrestore(&dev->power.lock, flags);
> > @@ -1870,6 +1944,15 @@ void pm_runtime_init(struct device *dev)
> > pm_suspend_ignore_children(dev, false);
> > dev->power.runtime_auto = true;
> >
> > +#ifdef CONFIG_DEBUG_FS
> > + dev->power.resume_count = 0;
> > + dev->power.suspend_count = 0;
> > + dev->power.suspend_time = 0;
> > + dev->power.last_suspended_time = 0;
> > + dev->power.min_suspended_time = 0;
> > + dev->power.max_suspended_time = 0;
> > +#endif /* CONFIG_DEBUG_FS */
>
> And here too.
>
> > +
> > dev->power.request_pending = false;
> > dev->power.request = RPM_REQ_NONE;
> > dev->power.deferred_resume = false;
> > diff --git a/include/linux/pm.h b/include/linux/pm.h
> > index ef3f1310e749..caa4ba4a65de 100644
> > --- a/include/linux/pm.h
> > +++ b/include/linux/pm.h
> > @@ -747,6 +747,12 @@ struct pm_subsys_data {
> > * state.
> > * @accounting_timestamp: Timestamp in nanoseconds of the last runtime PM state
> > * accounting update.
> > + * @resume_count: Number of transitions from suspended -> active
> > + * @suspend_count: Number of transitions from active -> suspended
> > + * @suspend_time: Timestamp when device entered RPM_SUSPENDED
> > + * @last_suspended_time: Residency of the last sleep period (ns)

It is better to say "suspension" instead of "sleep" in this comment
and the two comments below to avoid confusion.

> > + * @min_suspended_time: Shortest sleep duration
> > + * @max_suspended_time: Longest sleep duration
> > * @subsys_data: Subsystem-specific power management data.
> > * @set_latency_tolerance: Callback for setting latency tolerance.
> > * @qos: Per-device PM Quality of Service (QoS) constraints.
> > @@ -816,7 +822,15 @@ struct dev_pm_info {
> > u64 active_time;
> > u64 suspended_time;
> > u64 accounting_timestamp;
> > -#endif
> > +#ifdef CONFIG_DEBUG_FS
> > + u64 resume_count;
> > + u64 suspend_count;
> > + u64 suspend_time;
> > + u64 last_suspended_time;
> > + u64 min_suspended_time;
> > + u64 max_suspended_time;
> > +#endif /* CONFIG_DEBUG_FS */
> > +#endif /* CONFIG_PM */
> > struct pm_subsys_data *subsys_data; /* Owned by the subsystem. */
> > void (*set_latency_tolerance)(struct device *, s32);
> > struct dev_pm_qos *qos;
> >
> > --
> > 2.47.3
> >