Currently we only do cpu accounting to userspace based on what is actually happening precisely on each tick. The accuracy of that accounting gets progressively worse the lower HZ is. As we already keep accounting of nanosecond resolution we can accurately track user cpu, nice cpu and idle cpu if we move the accounting to update_cpu_clock with a nanosecond cpu_usage_stat entry. This increases overhead slightly but avoids the problem of tick aliasing errors making accounting unreliable. Signed-off-by: Con Kolivas Signed-off-by: Ingo Molnar --- include/linux/kernel_stat.h | 3 ++ include/linux/sched.h | 2 - kernel/sched.c | 64 +++++++++++++++++++++++++++++++++++++++++--- kernel/timer.c | 5 +-- 4 files changed, 66 insertions(+), 8 deletions(-) Index: linux-2.6.20.4-rsdl/include/linux/kernel_stat.h =================================================================== --- linux-2.6.20.4-rsdl.orig/include/linux/kernel_stat.h 2007-03-26 10:00:35.000000000 +1000 +++ linux-2.6.20.4-rsdl/include/linux/kernel_stat.h 2007-03-26 10:01:05.000000000 +1000 @@ -16,11 +16,14 @@ struct cpu_usage_stat { cputime64_t user; + cputime64_t user_ns; cputime64_t nice; + cputime64_t nice_ns; cputime64_t system; cputime64_t softirq; cputime64_t irq; cputime64_t idle; + cputime64_t idle_ns; cputime64_t iowait; cputime64_t steal; }; Index: linux-2.6.20.4-rsdl/include/linux/sched.h =================================================================== --- linux-2.6.20.4-rsdl.orig/include/linux/sched.h 2007-03-26 10:00:35.000000000 +1000 +++ linux-2.6.20.4-rsdl/include/linux/sched.h 2007-03-26 10:01:32.000000000 +1000 @@ -887,7 +887,7 @@ struct task_struct { int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */ unsigned long rt_priority; - cputime_t utime, stime; + cputime_t utime, utime_ns, stime; unsigned long nvcsw, nivcsw; /* context switch counts */ struct timespec start_time; /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ Index: linux-2.6.20.4-rsdl/kernel/sched.c =================================================================== --- linux-2.6.20.4-rsdl.orig/kernel/sched.c 2007-03-26 10:00:35.000000000 +1000 +++ linux-2.6.20.4-rsdl/kernel/sched.c 2007-03-26 10:01:05.000000000 +1000 @@ -77,6 +77,13 @@ #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) #define SCHED_PRIO(p) ((p)+MAX_RT_PRIO) +/* + * Some helpers for converting nanosecond timing to jiffy resolution + */ +#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) +#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) +#define JIFFY_NS JIFFIES_TO_NS(1) + #define TASK_PREEMPTS_CURR(p, curr) ((p)->prio < (curr)->prio) /* @@ -2993,8 +3000,59 @@ EXPORT_PER_CPU_SYMBOL(kstat); static inline void update_cpu_clock(struct task_struct *p, struct rq *rq, unsigned long long now) { - p->sched_time += now - p->last_ran; - p->last_ran = rq->most_recent_timestamp = now; + struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; + cputime64_t time_diff; + + /* Sanity check. It should never go backwards or ruin accounting */ + if (unlikely(now < p->last_ran)) + goto out_set; + /* All the userspace visible cpu accounting is done here */ + time_diff = now - p->last_ran; + p->sched_time += time_diff; + if (p != rq->idle) { + cputime_t utime_diff = time_diff; + + if (TASK_NICE(p) > 0) { + cpustat->nice_ns = cputime64_add(cpustat->nice_ns, + time_diff); + if (cpustat->nice_ns > JIFFY_NS) { + cpustat->nice_ns = + cputime64_sub(cpustat->nice_ns, + JIFFY_NS); + cpustat->nice = + cputime64_add(cpustat->nice, 1); + } + } else { + cpustat->user_ns = cputime64_add(cpustat->user_ns, + time_diff); + if (cpustat->user_ns > JIFFY_NS) { + cpustat->user_ns = + cputime64_sub(cpustat->user_ns, + JIFFY_NS); + cpustat ->user = + cputime64_add(cpustat->user, 1); + } + } + p->utime_ns = cputime_add(p->utime_ns, utime_diff); + if (p->utime_ns > JIFFY_NS) { + p->utime_ns = cputime_sub(p->utime_ns, JIFFY_NS); + p->utime = cputime_add(p->utime, + jiffies_to_cputime(1)); + } + } else { + cpustat->idle_ns = cputime64_add(cpustat->idle_ns, time_diff); + if (cpustat->idle_ns > JIFFY_NS) { + cpustat->idle_ns = cputime64_sub(cpustat->idle_ns, + JIFFY_NS); + cpustat->idle = cputime64_add(cpustat->idle, 1); + } + } +out_set: + /* + * We still need to set these values even if the clock appeared to + * go backwards in case _this_ is the correct timestamp. + */ + rq->most_recent_timestamp = p->last_ran = now; } /* @@ -3059,8 +3117,6 @@ void account_system_time(struct task_str cpustat->system = cputime64_add(cpustat->system, tmp); else if (atomic_read(&rq->nr_iowait) > 0) cpustat->iowait = cputime64_add(cpustat->iowait, tmp); - else - cpustat->idle = cputime64_add(cpustat->idle, tmp); /* Account for system time used */ acct_update_integrals(p); } Index: linux-2.6.20.4-rsdl/kernel/timer.c =================================================================== --- linux-2.6.20.4-rsdl.orig/kernel/timer.c 2007-03-26 10:00:35.000000000 +1000 +++ linux-2.6.20.4-rsdl/kernel/timer.c 2007-03-26 10:01:05.000000000 +1000 @@ -1106,10 +1106,9 @@ void update_process_times(int user_tick) int cpu = smp_processor_id(); /* Note: this timer irq context must be accounted for as well. */ - if (user_tick) - account_user_time(p, jiffies_to_cputime(1)); - else + if (!user_tick) account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1)); + /* User time is accounted for in update_cpu_clock in sched.c */ run_local_timers(); if (rcu_pending(cpu)) rcu_check_callbacks(cpu, user_tick);