power: wakeup_reason: fix suspend time reporting
authorAmit Pundir <amit.pundir@linaro.org>
Mon, 13 Apr 2015 21:08:20 +0000 (02:38 +0530)
committerJohn Stultz <john.stultz@linaro.org>
Tue, 16 Feb 2016 21:53:41 +0000 (13:53 -0800)
Suspend time reporting Change-Id: I2cb9a9408a5fd12166aaec11b935a0fd6a408c63
(Power: Report suspend times from last_suspend_time), is broken on 3.16+
kernels because get_xtime_and_monotonic_and_sleep_offset() hrtimer helper
routine is removed from kernel timekeeping.

The replacement helper routines ktime_get_update_offsets_{tick,now}()
are private to core kernel timekeeping so we can't use them, hence using
ktime_get() and ktime_get_boottime() instead and sampling the time twice.

Idea is to use Monotonic boottime offset to calculate total time spent
in last suspend state and CLOCK_MONOTONIC to calculate time spent in
last suspend-resume process.

Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
kernel/power/wakeup_reason.c

index 76b53400cb167aca141c22e501d0e4af4fc5efe3..21787ebb332fcfe5e2b45b15581a1a5aae1ca737 100644 (file)
@@ -36,10 +36,10 @@ static char abort_reason[MAX_SUSPEND_ABORT_LEN];
 static struct kobject *wakeup_reason;
 static DEFINE_SPINLOCK(resume_reason_lock);
 
-static struct timespec last_xtime; /* wall time before last suspend */
-static struct timespec curr_xtime; /* wall time after last suspend */
-static struct timespec last_stime; /* total_sleep_time before last suspend */
-static struct timespec curr_stime; /* total_sleep_time after last suspend */
+static ktime_t last_monotime; /* monotonic time before last suspend */
+static ktime_t curr_monotime; /* monotonic time after last suspend */
+static ktime_t last_stime; /* monotonic boottime offset before last suspend */
+static ktime_t curr_stime; /* monotonic boottime offset after last suspend */
 
 static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
                char *buf)
@@ -71,14 +71,22 @@ static ssize_t last_suspend_time_show(struct kobject *kobj,
        struct timespec total_time;
        struct timespec suspend_resume_time;
 
-       sleep_time = timespec_sub(curr_stime, last_stime);
-       total_time = timespec_sub(curr_xtime, last_xtime);
-       suspend_resume_time = timespec_sub(total_time, sleep_time);
+       /*
+        * total_time is calculated from monotonic bootoffsets because
+        * unlike CLOCK_MONOTONIC it include the time spent in suspend state.
+        */
+       total_time = ktime_to_timespec(ktime_sub(curr_stime, last_stime));
 
        /*
-        * suspend_resume_time is calculated from sleep_time. Userspace would
-        * always need both. Export them in pair here.
+        * suspend_resume_time is calculated as monotonic (CLOCK_MONOTONIC)
+        * time interval before entering suspend and post suspend.
         */
+       suspend_resume_time = ktime_to_timespec(ktime_sub(curr_monotime, last_monotime));
+
+       /* sleep_time = total_time - suspend_resume_time */
+       sleep_time = timespec_sub(total_time, suspend_resume_time);
+
+       /* Export suspend_resume_time and sleep_time in pair here. */
        return sprintf(buf, "%lu.%09lu %lu.%09lu\n",
                                suspend_resume_time.tv_sec, suspend_resume_time.tv_nsec,
                                sleep_time.tv_sec, sleep_time.tv_nsec);
@@ -160,21 +168,22 @@ void log_suspend_abort_reason(const char *fmt, ...)
 static int wakeup_reason_pm_event(struct notifier_block *notifier,
                unsigned long pm_event, void *unused)
 {
-       struct timespec xtom; /* wall_to_monotonic, ignored */
-
        switch (pm_event) {
        case PM_SUSPEND_PREPARE:
                spin_lock(&resume_reason_lock);
                irqcount = 0;
                suspend_abort = false;
                spin_unlock(&resume_reason_lock);
-
-               get_xtime_and_monotonic_and_sleep_offset(&last_xtime, &xtom,
-                       &last_stime);
+               /* monotonic time since boot */
+               last_monotime = ktime_get();
+               /* monotonic time since boot including the time spent in suspend */
+               last_stime = ktime_get_boottime();
                break;
        case PM_POST_SUSPEND:
-               get_xtime_and_monotonic_and_sleep_offset(&curr_xtime, &xtom,
-                       &curr_stime);
+               /* monotonic time since boot */
+               curr_monotime = ktime_get();
+               /* monotonic time since boot including the time spent in suspend */
+               curr_stime = ktime_get_boottime();
                break;
        default:
                break;