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

From: Joshua Crofts

Date: Wed Nov 25 2026 - 09:38:42 EST


On 25-Nov-26 13:54, Rafael J. Wysocki (Intel) 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.

Ack, this patch is messy...

Also Sashiko still isn't happy with the way I calculate these metrics.

--
Kind regards,
Joshua Crofts