get_cpu_idle_time_us() and get_cpu_iowait_time_us() mostly share
the same code. Lets consolidate both implementations.

Signed-off-by: Frederic Weisbecker <[email protected]>
Cc: Fernando Luis Vazquez Cao <[email protected]>
Cc: Tetsuo Handa <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Arjan van de Ven <[email protected]>
---
 kernel/time/tick-sched.c |   76 ++++++++++++++++++++--------------------------
 1 files changed, 33 insertions(+), 43 deletions(-)

diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index f7fc27b..017bec2 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -440,24 +440,10 @@ static ktime_t tick_nohz_start_idle(int cpu, struct 
tick_sched *ts)
        return now;
 }
 
-/**
- * get_cpu_idle_time_us - get the total idle time of a cpu
- * @cpu: CPU number to query
- * @last_update_time: variable to store update time in. Do not update
- * counters if NULL.
- *
- * Return the cummulative idle time (since boot) for a given
- * CPU, in microseconds.
- *
- * This time is measured via accounting rather than sampling,
- * and is as accurate as ktime_get() is.
- *
- * This function returns -1 if NOHZ is not enabled.
- */
-u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
+static u64 get_cpu_sleep_time_us(int cpu, bool io, u64 *last_update_time)
 {
        struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
-       ktime_t now, idle;
+       ktime_t now, sleep;
        unsigned int seq;
 
        if (!tick_nohz_enabled)
@@ -469,16 +455,41 @@ u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
 
        do {
                seq = read_seqcount_begin(&ts->sleeptime_seq);
-               if (ts->idle_active && !nr_iowait_cpu(cpu)) {
+               if (io)
+                       sleep = ts->iowait_sleeptime;
+               else
+                       sleep = ts->idle_sleeptime;
+
+               if (ts->idle_active)
+                       continue;
+
+               if ((io && nr_iowait_cpu(cpu)) || (!io && !nr_iowait_cpu(cpu))) 
{
                        ktime_t delta = ktime_sub(now, ts->idle_entrytime);
-                       idle = ktime_add(ts->idle_sleeptime, delta);
-               } else {
-                       idle = ts->idle_sleeptime;
+                       sleep = ktime_add(sleep, delta);
                }
        } while (read_seqcount_retry(&ts->sleeptime_seq, seq));
 
-       return ktime_to_us(idle);
+       return ktime_to_us(sleep);
+
+}
 
+/**
+ * get_cpu_idle_time_us - get the total idle time of a cpu
+ * @cpu: CPU number to query
+ * @last_update_time: variable to store update time in. Do not update
+ * counters if NULL.
+ *
+ * Return the cummulative idle time (since boot) for a given
+ * CPU, in microseconds.
+ *
+ * This time is measured via accounting rather than sampling,
+ * and is as accurate as ktime_get() is.
+ *
+ * This function returns -1 if NOHZ is not enabled.
+ */
+u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
+{
+       return get_cpu_sleep_time_us(cpu, false, last_update_time);
 }
 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
 
@@ -498,28 +509,7 @@ EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  */
 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
 {
-       struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
-       ktime_t now, iowait;
-       unsigned int seq;
-
-       if (!tick_nohz_enabled)
-               return -1;
-
-       now = ktime_get();
-       if (last_update_time)
-               *last_update_time = ktime_to_us(now);
-
-       do {
-               seq = read_seqcount_begin(&ts->sleeptime_seq);
-               if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
-                       ktime_t delta = ktime_sub(now, ts->idle_entrytime);
-                       iowait = ktime_add(ts->iowait_sleeptime, delta);
-               } else {
-                       iowait = ts->iowait_sleeptime;
-               }
-       } while (read_seqcount_retry(&ts->sleeptime_seq, seq));
-
-       return ktime_to_us(iowait);
+       return get_cpu_sleep_time_us(cpu, true, last_update_time);
 }
 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
 
-- 
1.7.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to