Fix periodic itimers precision -v4
cover Fix periodic itimers precision -v4
Hello,
I'm resending patches for fixing itimers periodic ticks precision, for reference see: http://lkml.org/lkml/2009/4/2/230
Changelog:
v3 -> v4
- Rebase for 2.6.31
v2 -> v3 (resend 5):
- Use CPUCLOCK_PROF CPUCLOCK_VIRT defines in kernel/fork.c
- Using inline function cputime_sub_ns() with sanity check against negative results instead of macro.
- Add patch which optimize jiffies_to_cputime(1) for PowerPC with CONFIG_VIRT_CPU_ACCOUNTING
v1 -> v2 (resend 4):
- Add comment about CPUCLOCK_PROF and CPUCLOCK_VIRT are 0 and 1 respectively and can be used for indexing itimers array.
- Remove not needed BUG().
- Coding style fixes.
- Better algorithm (idea form Thomas) to calculate next signal time for periodic itimers, we use now precomputed nanosecond value of cputime 1 tick.
- Add patch with simplify arm_timer() function.
v0 -> v1 (resend 3):
- Fix bug: using utime instead of sum stime + utime when setup PROF itimer.
- Fix bug: using value->it_interval instead of ovalue->it_interavl.
- No new definitions IT_PROF and IT_VIRT only CPUCLOCK_PROF and CPUCLOCK_VIRT are used.
Cheers
Stanislaw
PATCH 1/4 itimers: merge ITIMER_VIRT and ITIMER_PROF
Both cpu itimers have same data flow in the few places, this patch make unification of code related with VIRT and PROF itimers.
--- include/linux/sched.h | 14 ++++- kernel/fork.c | 9 ++- kernel/itimer.c | 146 +++++++++++++++++++++------------------------ kernel/posix-cpu-timers.c | 98 ++++++++++++++---------------- 4 files changed, 130 insertions(+), 137 deletions(-)
include/linux/sched.h +11 -3
@@ -470,6 +470,11 @@ struct pacct_struct {unsigned long ac_minflt, ac_majflt; }; +struct cpu_itimer { +cputime_t expires; +cputime_t incr; +}; +/** * struct task_cputime - collected CPU time counts * @utime: time spent in user mode, in &cputime_t units
@@ -564,9 +569,12 @@ struct signal_struct {struct pid *leader_pid;ktime_t it_real_incr; -/* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */ -cputime_t it_prof_expires, it_virt_expires; -cputime_t it_prof_incr, it_virt_incr; +/* +* ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use +* CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these +* values are defined to 0 and 1 respectively +*/ +struct cpu_itimer it[2 ];/* * Thread group totals for process CPU timers.
kernel/fork.c +5 -4
@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/posix-timers.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -789,10 +790,10 @@ static void posix_cpu_timers_init_group(struct signal_struct *sig)thread_group_cputime_init (sig);/* Expiration times and increments. */ - sig->it_virt_expires = cputime_zero; - sig->it_virt_incr = cputime_zero; - sig->it_prof_expires = cputime_zero; - sig->it_prof_incr = cputime_zero; + sig->it[CPUCLOCK_PROF ].expires = cputime_zero; + sig->it[CPUCLOCK_PROF ].incr = cputime_zero; + sig->it[CPUCLOCK_VIRT ].expires = cputime_zero; + sig->it[CPUCLOCK_VIRT ].incr = cputime_zero;/* Cached expiration times. */ sig->cputime_expires.prof_exp = cputime_zero;
kernel/itimer.c +68 -78
183 lines not on this page · raw message
kernel/posix-cpu-timers.c +46 -52
147 lines not on this page · raw message
Signature · 1 line
1.6.2.5
PATCH 2/4 itimers: fix periodic tics precision
Measure ITIMER_PROF and ITIMER_VIRT timers interval error between real ticks and requested by user. Take it into account when scheduling next tick.
This patch introduce possibility where time between two consecutive tics is smaller then requested interval, it preserve however dependency that n tick is generated not earlier than n*interval time - counting from the beginning of periodic signal generation.
--- include/linux/sched.h | 2 ++ kernel/itimer.c | 24 +++++++++++++++++++++--- kernel/posix-cpu-timers.c | 20 +++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-)
include/linux/sched.h +2 -0
@@ -473,6 +473,8 @@ struct pacct_struct {struct cpu_itimer {cputime_t expires;cputime_t incr; +u32 error; +u32 incr_error; };/**
kernel/itimer.c +21 -3
@@ -42,7 +42,7 @@ static struct timeval itimer_get_remtime(struct hrtimer *timer) }static void get_cpu_itimer (struct task_struct *tsk,unsigned int clock_id, -struct itimerval *value) +struct itimerval *const value) {cputime_t cval, cinterval;struct cpu_itimer *it = &tsk->signal->it[clock_id];
@@ -127,14 +127,32 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)return HRTIMER_NORESTART ; } +static inline u32 cputime_sub_ns (cputime_t ct,s64 real_ns) +{ +struct timespec ts; +s64 cpu_ns; + +cputime_to_timespec (ct, &ts); + cpu_ns =timespec_to_ns (&ts); + +return (cpu_ns <= real_ns) ?0 : cpu_ns - real_ns; +} +static void set_cpu_itimer (struct task_struct *tsk,unsigned int clock_id, -struct itimerval *value,struct itimerval *ovalue) +const struct itimerval *const value, +struct itimerval *const ovalue) { -cputime_t cval, cinterval, nval, ninterval; +cputime_t cval, nval, cinterval, ninterval; +s64 ns_ninterval, ns_nval;struct cpu_itimer *it = &tsk->signal->it[clock_id]; nval =timeval_to_cputime (&value->it_value); + ns_nval =timeval_to_ns (&value->it_value); ninterval =timeval_to_cputime (&value->it_interval); + ns_ninterval =timeval_to_ns (&value->it_interval); + + it->incr_error =cputime_sub_ns (ninterval, ns_ninterval); + it->error =cputime_sub_ns (nval, ns_nval);spin_lock_irq (&tsk->sighand->siglock);
kernel/posix-cpu-timers.c +17 -3
@@ -1070,6 +1070,8 @@ static void stop_process_timers(struct task_struct *tsk)spin_unlock_irqrestore (&cputimer->lock, flags); } +static u32 onecputick; +static void check_cpu_itimer (struct task_struct *tsk,struct cpu_itimer *it,cputime_t *expires,cputime_t cur_time,int signo) {
@@ -1077,9 +1079,16 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,return ;if (cputime_ge (cur_time, it->expires)) { - it->expires = it->incr; -if (!cputime_eq (it->expires, cputime_zero)) - it->expires =cputime_add (it->expires, cur_time); +if (!cputime_eq (it->incr, cputime_zero)) { + it->expires =cputime_add (it->expires, it->incr); + it->error += it->incr_error; +if (it->error >= onecputick) { + it->expires =cputime_sub (it->expires, +jiffies_to_cputime (1 )); + it->error -= onecputick; + } + }else + it->expires = cputime_zero;__group_send_sig_info (signo,SEND_SIG_PRIV , tsk); }
@@ -1696,10 +1705,15 @@ static __init int init_posix_cpu_timers(void) .nsleep = thread_cpu_nsleep, .nsleep_restart = thread_cpu_nsleep_restart, }; +struct timespec ts;register_posix_clock (CLOCK_PROCESS_CPUTIME_ID , &process);register_posix_clock (CLOCK_THREAD_CPUTIME_ID , &thread); +cputime_to_timespec (jiffies_to_cputime (1 ), &ts); + onecputick = ts.tv_nsec; +WARN_ON (ts.tv_sec !=0 ); +return 0 ; }__initcall (init_posix_cpu_timers);
Signature · 1 line
1.6.2.5
PATCH 3/4 itimers: simplify arm_timer() code a bit
Don't update values in expiration cache when new ones are equal. Add expire_le() and expire_gt() helpers to simplify the code.
--- kernel/posix-cpu-timers.c | 44 +++++++++++++++++++++++--------------------- 1 files changed, 23 insertions(+), 21 deletions(-)
kernel/posix-cpu-timers.c +23 -21
@@ -541,6 +541,17 @@ static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now) now); } +static inline int expires_gt (cputime_t expires,cputime_t new_exp) +{ +return cputime_eq (expires, cputime_zero) || +cputime_gt (expires, new_exp); +} + +static inline int expires_le (cputime_t expires,cputime_t new_exp) +{ +return !cputime_eq (expires, cputime_zero) && +cputime_le (expires, new_exp); +}/* * Insert the timer on the appropriate list before any timers that * expire later. This must be called with the tasklist_lock held
@@ -585,31 +596,26 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)*/ if (CPUCLOCK_PERTHREAD (timer->it_clock)) { +union cpu_time_count *exp = &nt->expires; +switch (CPUCLOCK_WHICH (timer->it_clock)) {default :BUG ();case CPUCLOCK_PROF : -if (cputime_eq (p->cputime_expires.prof_exp, - cputime_zero) || -cputime_gt (p->cputime_expires.prof_exp, - nt->expires.cpu)) - p->cputime_expires.prof_exp = - nt->expires.cpu; +if (expires_gt (p->cputime_expires.prof_exp, + exp->cpu)) + p->cputime_expires.prof_exp = exp->cpu;break ;case CPUCLOCK_VIRT : -if (cputime_eq (p->cputime_expires.virt_exp, - cputime_zero) || -cputime_gt (p->cputime_expires.virt_exp, - nt->expires.cpu)) - p->cputime_expires.virt_exp = - nt->expires.cpu; +if (expires_gt (p->cputime_expires.virt_exp, + exp->cpu)) + p->cputime_expires.virt_exp = exp->cpu;break ;case CPUCLOCK_SCHED :if (p->cputime_expires.sched_exp ==0 || - p->cputime_expires.sched_exp > - nt->expires.sched) + p->cputime_expires.sched_exp > exp->sched) p->cputime_expires.sched_exp = - nt->expires.sched; + exp->sched;break ; } }else {
@@ -623,17 +629,13 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)default :BUG ();case CPUCLOCK_VIRT : -if (!cputime_eq (sig->it[CPUCLOCK_VIRT ].expires, - cputime_zero) && -cputime_lt (sig->it[CPUCLOCK_VIRT ].expires, +if (expires_le (sig->it[CPUCLOCK_VIRT ].expires, exp->cpu))break ; sig->cputime_expires.virt_exp = exp->cpu;break ;case CPUCLOCK_PROF : -if (!cputime_eq (sig->it[CPUCLOCK_PROF ].expires, - cputime_zero) && -cputime_lt (sig->it[CPUCLOCK_PROF ].expires, +if (expires_le (sig->it[CPUCLOCK_PROF ].expires, exp->cpu))break ; i = sig->rlim[RLIMIT_CPU ].rlim_cur;
Signature · 1 line
1.6.2.5
PATCH 4/4 cputime: optimize jiffies_to_cputime(1)
For powerpc with CONFIG_VIRT_CPU_ACCOUNTING jiffies_to_cputime(1) is not compile time constant and run time calculations are quite expensive. To optimize we use precomputed value. For all other architectures is is preprocessor definition.
--- arch/ia64/include/asm/cputime.h | 1 + arch/powerpc/include/asm/cputime.h | 13 +++++++++++++ arch/powerpc/kernel/time.c | 4 ++++ arch/s390/include/asm/cputime.h | 1 + include/asm-generic/cputime.h | 1 + kernel/itimer.c | 4 ++-- kernel/posix-cpu-timers.c | 6 +++--- kernel/sched.c | 9 ++++----- 8 files changed, 29 insertions(+), 10 deletions(-)
arch/ia64/include/asm/cputime.h +1 -0
@@ -30,6 +30,7 @@ typedef u64 cputime_t;typedef u64 cputime64_t ;#define cputime_zero ((cputime_t)0) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~((cputime_t)0) >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
arch/powerpc/include/asm/cputime.h +13 -0
@@ -18,6 +18,9 @@#ifndef CONFIG_VIRT_CPU_ACCOUNTING #include <asm-generic/cputime.h> +#ifdef __KERNEL__ +static inline void setup_cputime_one_jiffy (void ) { } +#endif #else #include <linux/types.h>
@@ -49,6 +52,11 @@ typedef u64 cputime64_t;#ifdef __KERNEL__/* +* One jiffy in timebase units computed during initialization +*/ +extern cputime_t cputime_one_jiffy; + +/* * Convert cputime <-> jiffies */ extern u64 __cputime_jiffies_factor;
@@ -89,6 +97,11 @@ static inline cputime_t jiffies_to_cputime(const unsigned long jif)return ct; } +static inline void setup_cputime_one_jiffy (void ) +{ + cputime_one_jiffy =jiffies_to_cputime (1 ); +} +static inline cputime64_t jiffies64_to_cputime64 (const u64 jif) {cputime_t ct;
arch/powerpc/kernel/time.c +4 -0
@@ -193,6 +193,8 @@ EXPORT_SYMBOL(__cputime_clockt_factor); DEFINE_PER_CPU(unsigned long, cputime_last_delta); DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta); +cputime_t cputime_one_jiffy; +static void calc_cputime_factors (void ) {struct div_result res;
@@ -500,6 +502,7 @@ static int __init iSeries_tb_recal(void) tb_to_xs = divres.result_low; vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; vdso_data->tb_to_xs = tb_to_xs; +setup_cputime_one_jiffy (); }else { printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
@@ -945,6 +948,7 @@ void __init time_init(void) tb_ticks_per_usec = ppc_tb_freq /1000000 ; tb_to_us =mulhwu_scale_factor (ppc_tb_freq,1000000 );calc_cputime_factors (); +setup_cputime_one_jiffy ();/* * Calculate the length of each tick in ns. It will not be
arch/s390/include/asm/cputime.h +1 -0
@@ -42,6 +42,7 @@ __div(unsigned long long n, unsigned int base)#endif /* __s390x__ */ #define cputime_zero (0ULL) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~0UL >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
include/asm-generic/cputime.h +1 -0
@@ -7,6 +7,7 @@typedef unsigned long cputime_t ;#define cputime_zero (0UL) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~0UL >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
kernel/itimer.c +2 -2
@@ -64,7 +64,7 @@ static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,if (cputime_le (cval, t))/* about to fire */ - cval =jiffies_to_cputime (1 ); + cval = cputime_one_jiffy;else cval =cputime_sub (cval, t); }
@@ -161,7 +161,7 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,if (!cputime_eq (cval, cputime_zero) || !cputime_eq (nval, cputime_zero)) {if (cputime_gt (nval, cputime_zero)) - nval =cputime_add (nval,jiffies_to_cputime (1 )); + nval =cputime_add (nval, cputime_one_jiffy);set_process_cpu_timer (tsk, clock_id, &nval, &cval); } it->expires = nval;
kernel/posix-cpu-timers.c +3 -3
@@ -1086,7 +1086,7 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, it->error += it->incr_error;if (it->error >= onecputick) { it->expires =cputime_sub (it->expires, -jiffies_to_cputime (1 )); + cputime_one_jiffy); it->error -= onecputick; } }else
@@ -1461,7 +1461,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,if (!cputime_eq (*oldval, cputime_zero)) {if (cputime_le (*oldval, now.cpu)) {/* Just about to fire. */ - *oldval =jiffies_to_cputime (1 ); + *oldval = cputime_one_jiffy; }else { *oldval =cputime_sub (*oldval, now.cpu); }
@@ -1712,7 +1712,7 @@ static __init int init_posix_cpu_timers(void)register_posix_clock (CLOCK_PROCESS_CPUTIME_ID , &process);register_posix_clock (CLOCK_THREAD_CPUTIME_ID , &thread); -cputime_to_timespec (jiffies_to_cputime (1 ), &ts); +cputime_to_timespec (cputime_one_jiffy, &ts); onecputick = ts.tv_nsec;WARN_ON (ts.tv_sec !=0 );
kernel/sched.c +4 -5
@@ -5031,17 +5031,16 @@ void account_idle_time(cputime_t cputime)*/ void account_process_tick (struct task_struct *p,int user_tick) { -cputime_t one_jiffy =jiffies_to_cputime (1 ); -cputime_t one_jiffy_scaled =cputime_to_scaled (one_jiffy); +cputime_t one_jiffy_scaled =cputime_to_scaled (cputime_one_jiffy);struct rq *rq =this_rq ();if (user_tick) -account_user_time (p, one_jiffy, one_jiffy_scaled); +account_user_time (p, cputime_one_jiffy, one_jiffy_scaled);else if ((p != rq->idle) || (irq_count () !=HARDIRQ_OFFSET )) -account_system_time (p,HARDIRQ_OFFSET , one_jiffy, +account_system_time (p,HARDIRQ_OFFSET , cputime_one_jiffy, one_jiffy_scaled);else -account_idle_time (one_jiffy); +account_idle_time (cputime_one_jiffy); }/*
Signature · 1 line
1.6.2.5
[tip:timers/posixtimers] cputime: Optimize jiffies_to_cputime(1)
Commit-ID: a42548a18866e87092db93b771e6c5b060d78401 Gitweb: http://git.kernel.org/tip/a42548a18866e87092db93b771e6c5b060d78401 Author: Stanislaw Gruszka <[email protected]> AuthorDate: Wed, 29 Jul 2009 12:15:29 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 3 Aug 2009 14:48:36 +0200
cputime: Optimize jiffies_to_cputime(1)
For powerpc with CONFIG_VIRT_CPU_ACCOUNTING jiffies_to_cputime(1) is not compile time constant and run time calculations are quite expensive. To optimize we use precomputed value. For all other architectures is is preprocessor definition.
LKML-Reference: <[email protected]>
--- arch/ia64/include/asm/cputime.h | 1 + arch/powerpc/include/asm/cputime.h | 13 +++++++++++++ arch/powerpc/kernel/time.c | 4 ++++ arch/s390/include/asm/cputime.h | 1 + include/asm-generic/cputime.h | 1 + kernel/itimer.c | 4 ++-- kernel/posix-cpu-timers.c | 6 +++--- kernel/sched.c | 9 ++++----- 8 files changed, 29 insertions(+), 10 deletions(-)
arch/ia64/include/asm/cputime.h +1 -0
@@ -30,6 +30,7 @@ typedef u64 cputime_t;typedef u64 cputime64_t ;#define cputime_zero ((cputime_t)0) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~((cputime_t)0) >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
arch/powerpc/include/asm/cputime.h +13 -0
@@ -18,6 +18,9 @@#ifndef CONFIG_VIRT_CPU_ACCOUNTING #include <asm-generic/cputime.h> +#ifdef __KERNEL__ +static inline void setup_cputime_one_jiffy (void ) { } +#endif #else #include <linux/types.h>
@@ -49,6 +52,11 @@ typedef u64 cputime64_t;#ifdef __KERNEL__/* +* One jiffy in timebase units computed during initialization +*/ +extern cputime_t cputime_one_jiffy; + +/* * Convert cputime <-> jiffies */ extern u64 __cputime_jiffies_factor;
@@ -89,6 +97,11 @@ static inline cputime_t jiffies_to_cputime(const unsigned long jif)return ct; } +static inline void setup_cputime_one_jiffy (void ) +{ + cputime_one_jiffy =jiffies_to_cputime (1 ); +} +static inline cputime64_t jiffies64_to_cputime64 (const u64 jif) {cputime_t ct;
arch/powerpc/kernel/time.c +4 -0
@@ -193,6 +193,8 @@ EXPORT_SYMBOL(__cputime_clockt_factor); DEFINE_PER_CPU(unsigned long, cputime_last_delta); DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta); +cputime_t cputime_one_jiffy; +static void calc_cputime_factors (void ) {struct div_result res;
@@ -500,6 +502,7 @@ static int __init iSeries_tb_recal(void) tb_to_xs = divres.result_low; vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; vdso_data->tb_to_xs = tb_to_xs; +setup_cputime_one_jiffy (); }else { printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
@@ -945,6 +948,7 @@ void __init time_init(void) tb_ticks_per_usec = ppc_tb_freq /1000000 ; tb_to_us =mulhwu_scale_factor (ppc_tb_freq,1000000 );calc_cputime_factors (); +setup_cputime_one_jiffy ();/* * Calculate the length of each tick in ns. It will not be
arch/s390/include/asm/cputime.h +1 -0
@@ -42,6 +42,7 @@ __div(unsigned long long n, unsigned int base)#endif /* __s390x__ */ #define cputime_zero (0ULL) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~0UL >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
include/asm-generic/cputime.h +1 -0
@@ -7,6 +7,7 @@typedef unsigned long cputime_t ;#define cputime_zero (0UL) +#define cputime_one_jiffy jiffies_to_cputime(1)#define cputime_max ((~0UL >> 1) - 1)#define cputime_add (__a, __b) ((__a) + (__b))#define cputime_sub (__a, __b) ((__a) - (__b))
kernel/itimer.c +2 -2
@@ -64,7 +64,7 @@ static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,if (cputime_le (cval, t))/* about to fire */ - cval =jiffies_to_cputime (1 ); + cval = cputime_one_jiffy;else cval =cputime_sub (cval, t); }
@@ -161,7 +161,7 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,if (!cputime_eq (cval, cputime_zero) || !cputime_eq (nval, cputime_zero)) {if (cputime_gt (nval, cputime_zero)) - nval =cputime_add (nval,jiffies_to_cputime (1 )); + nval =cputime_add (nval, cputime_one_jiffy);set_process_cpu_timer (tsk, clock_id, &nval, &cval); } it->expires = nval;
kernel/posix-cpu-timers.c +3 -3
@@ -1086,7 +1086,7 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, it->error += it->incr_error;if (it->error >= onecputick) { it->expires =cputime_sub (it->expires, -jiffies_to_cputime (1 )); + cputime_one_jiffy); it->error -= onecputick; } }else
@@ -1461,7 +1461,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,if (!cputime_eq (*oldval, cputime_zero)) {if (cputime_le (*oldval, now.cpu)) {/* Just about to fire. */ - *oldval =jiffies_to_cputime (1 ); + *oldval = cputime_one_jiffy; }else { *oldval =cputime_sub (*oldval, now.cpu); }
@@ -1712,7 +1712,7 @@ static __init int init_posix_cpu_timers(void)register_posix_clock (CLOCK_PROCESS_CPUTIME_ID , &process);register_posix_clock (CLOCK_THREAD_CPUTIME_ID , &thread); -cputime_to_timespec (jiffies_to_cputime (1 ), &ts); +cputime_to_timespec (cputime_one_jiffy, &ts); onecputick = ts.tv_nsec;WARN_ON (ts.tv_sec !=0 );
kernel/sched.c +4 -5
@@ -5031,17 +5031,16 @@ void account_idle_time(cputime_t cputime)*/ void account_process_tick (struct task_struct *p,int user_tick) { -cputime_t one_jiffy =jiffies_to_cputime (1 ); -cputime_t one_jiffy_scaled =cputime_to_scaled (one_jiffy); +cputime_t one_jiffy_scaled =cputime_to_scaled (cputime_one_jiffy);struct rq *rq =this_rq ();if (user_tick) -account_user_time (p, one_jiffy, one_jiffy_scaled); +account_user_time (p, cputime_one_jiffy, one_jiffy_scaled);else if ((p != rq->idle) || (irq_count () !=HARDIRQ_OFFSET )) -account_system_time (p,HARDIRQ_OFFSET , one_jiffy, +account_system_time (p,HARDIRQ_OFFSET , cputime_one_jiffy, one_jiffy_scaled);else -account_idle_time (one_jiffy); +account_idle_time (cputime_one_jiffy); }/*
[tip:timers/posixtimers] itimers: Simplify arm_timer() code a bit
In reply to Stanislaw GruszkaCommit-ID: d1e3b6d195770bd422e3229b88edfc154b6a27dd Gitweb: http://git.kernel.org/tip/d1e3b6d195770bd422e3229b88edfc154b6a27dd Author: Stanislaw Gruszka <[email protected]> AuthorDate: Wed, 29 Jul 2009 12:15:28 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 3 Aug 2009 14:48:36 +0200
itimers: Simplify arm_timer() code a bit
Don't update values in expiration cache when new ones are equal. Add expire_le() and expire_gt() helpers to simplify the code.
LKML-Reference: <[email protected]>
--- kernel/posix-cpu-timers.c | 44 +++++++++++++++++++++++--------------------- 1 files changed, 23 insertions(+), 21 deletions(-)
kernel/posix-cpu-timers.c +23 -21
@@ -541,6 +541,17 @@ static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now) now); } +static inline int expires_gt (cputime_t expires,cputime_t new_exp) +{ +return cputime_eq (expires, cputime_zero) || +cputime_gt (expires, new_exp); +} + +static inline int expires_le (cputime_t expires,cputime_t new_exp) +{ +return !cputime_eq (expires, cputime_zero) && +cputime_le (expires, new_exp); +}/* * Insert the timer on the appropriate list before any timers that * expire later. This must be called with the tasklist_lock held
@@ -585,31 +596,26 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)*/ if (CPUCLOCK_PERTHREAD (timer->it_clock)) { +union cpu_time_count *exp = &nt->expires; +switch (CPUCLOCK_WHICH (timer->it_clock)) {default :BUG ();case CPUCLOCK_PROF : -if (cputime_eq (p->cputime_expires.prof_exp, - cputime_zero) || -cputime_gt (p->cputime_expires.prof_exp, - nt->expires.cpu)) - p->cputime_expires.prof_exp = - nt->expires.cpu; +if (expires_gt (p->cputime_expires.prof_exp, + exp->cpu)) + p->cputime_expires.prof_exp = exp->cpu;break ;case CPUCLOCK_VIRT : -if (cputime_eq (p->cputime_expires.virt_exp, - cputime_zero) || -cputime_gt (p->cputime_expires.virt_exp, - nt->expires.cpu)) - p->cputime_expires.virt_exp = - nt->expires.cpu; +if (expires_gt (p->cputime_expires.virt_exp, + exp->cpu)) + p->cputime_expires.virt_exp = exp->cpu;break ;case CPUCLOCK_SCHED :if (p->cputime_expires.sched_exp ==0 || - p->cputime_expires.sched_exp > - nt->expires.sched) + p->cputime_expires.sched_exp > exp->sched) p->cputime_expires.sched_exp = - nt->expires.sched; + exp->sched;break ; } }else {
@@ -623,17 +629,13 @@ static void arm_timer(struct k_itimer *timer, union cpu_time_count now)default :BUG ();case CPUCLOCK_VIRT : -if (!cputime_eq (sig->it[CPUCLOCK_VIRT ].expires, - cputime_zero) && -cputime_lt (sig->it[CPUCLOCK_VIRT ].expires, +if (expires_le (sig->it[CPUCLOCK_VIRT ].expires, exp->cpu))break ; sig->cputime_expires.virt_exp = exp->cpu;break ;case CPUCLOCK_PROF : -if (!cputime_eq (sig->it[CPUCLOCK_PROF ].expires, - cputime_zero) && -cputime_lt (sig->it[CPUCLOCK_PROF ].expires, +if (expires_le (sig->it[CPUCLOCK_PROF ].expires, exp->cpu))break ; i = sig->rlim[RLIMIT_CPU ].rlim_cur;
[tip:timers/posixtimers] itimers: Fix periodic tics precision
In reply to Stanislaw GruszkaCommit-ID: 8356b5f9c424e5831715abbce747197c30d1fd71 Gitweb: http://git.kernel.org/tip/8356b5f9c424e5831715abbce747197c30d1fd71 Author: Stanislaw Gruszka <[email protected]> AuthorDate: Wed, 29 Jul 2009 12:15:27 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 3 Aug 2009 14:48:35 +0200
itimers: Fix periodic tics precision
Measure ITIMER_PROF and ITIMER_VIRT timers interval error between real ticks and requested by user. Take it into account when scheduling next tick.
This patch introduce possibility where time between two consecutive tics is smaller then requested interval, it preserve however dependency that n tick is generated not earlier than n*interval time - counting from the beginning of periodic signal generation.
LKML-Reference: <[email protected]>
--- include/linux/sched.h | 2 ++ kernel/itimer.c | 24 +++++++++++++++++++++--- kernel/posix-cpu-timers.c | 20 +++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-)
include/linux/sched.h +2 -0
@@ -473,6 +473,8 @@ struct pacct_struct {struct cpu_itimer {cputime_t expires;cputime_t incr; +u32 error; +u32 incr_error; };/**
kernel/itimer.c +21 -3
@@ -42,7 +42,7 @@ static struct timeval itimer_get_remtime(struct hrtimer *timer) }static void get_cpu_itimer (struct task_struct *tsk,unsigned int clock_id, -struct itimerval *value) +struct itimerval *const value) {cputime_t cval, cinterval;struct cpu_itimer *it = &tsk->signal->it[clock_id];
@@ -127,14 +127,32 @@ enum hrtimer_restart it_real_fn(struct hrtimer *timer)return HRTIMER_NORESTART ; } +static inline u32 cputime_sub_ns (cputime_t ct,s64 real_ns) +{ +struct timespec ts; +s64 cpu_ns; + +cputime_to_timespec (ct, &ts); + cpu_ns =timespec_to_ns (&ts); + +return (cpu_ns <= real_ns) ?0 : cpu_ns - real_ns; +} +static void set_cpu_itimer (struct task_struct *tsk,unsigned int clock_id, -struct itimerval *value,struct itimerval *ovalue) +const struct itimerval *const value, +struct itimerval *const ovalue) { -cputime_t cval, cinterval, nval, ninterval; +cputime_t cval, nval, cinterval, ninterval; +s64 ns_ninterval, ns_nval;struct cpu_itimer *it = &tsk->signal->it[clock_id]; nval =timeval_to_cputime (&value->it_value); + ns_nval =timeval_to_ns (&value->it_value); ninterval =timeval_to_cputime (&value->it_interval); + ns_ninterval =timeval_to_ns (&value->it_interval); + + it->incr_error =cputime_sub_ns (ninterval, ns_ninterval); + it->error =cputime_sub_ns (nval, ns_nval);spin_lock_irq (&tsk->sighand->siglock);
kernel/posix-cpu-timers.c +17 -3
@@ -1070,6 +1070,8 @@ static void stop_process_timers(struct task_struct *tsk)spin_unlock_irqrestore (&cputimer->lock, flags); } +static u32 onecputick; +static void check_cpu_itimer (struct task_struct *tsk,struct cpu_itimer *it,cputime_t *expires,cputime_t cur_time,int signo) {
@@ -1077,9 +1079,16 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,return ;if (cputime_ge (cur_time, it->expires)) { - it->expires = it->incr; -if (!cputime_eq (it->expires, cputime_zero)) - it->expires =cputime_add (it->expires, cur_time); +if (!cputime_eq (it->incr, cputime_zero)) { + it->expires =cputime_add (it->expires, it->incr); + it->error += it->incr_error; +if (it->error >= onecputick) { + it->expires =cputime_sub (it->expires, +jiffies_to_cputime (1 )); + it->error -= onecputick; + } + }else + it->expires = cputime_zero;__group_send_sig_info (signo,SEND_SIG_PRIV , tsk); }
@@ -1696,10 +1705,15 @@ static __init int init_posix_cpu_timers(void) .nsleep = thread_cpu_nsleep, .nsleep_restart = thread_cpu_nsleep_restart, }; +struct timespec ts;register_posix_clock (CLOCK_PROCESS_CPUTIME_ID , &process);register_posix_clock (CLOCK_THREAD_CPUTIME_ID , &thread); +cputime_to_timespec (jiffies_to_cputime (1 ), &ts); + onecputick = ts.tv_nsec; +WARN_ON (ts.tv_sec !=0 ); +return 0 ; }__initcall (init_posix_cpu_timers);
[tip:timers/posixtimers] itimers: Merge ITIMER_VIRT and ITIMER_PROF
In reply to Stanislaw GruszkaCommit-ID: 42c4ab41a176ee784c0f28c0b29025a8fc34f05a Gitweb: http://git.kernel.org/tip/42c4ab41a176ee784c0f28c0b29025a8fc34f05a Author: Stanislaw Gruszka <[email protected]> AuthorDate: Wed, 29 Jul 2009 12:15:26 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Mon, 3 Aug 2009 14:48:35 +0200
itimers: Merge ITIMER_VIRT and ITIMER_PROF
Both cpu itimers have same data flow in the few places, this patch make unification of code related with VIRT and PROF itimers.
LKML-Reference: <[email protected]>
--- include/linux/sched.h | 14 ++++- kernel/fork.c | 9 ++- kernel/itimer.c | 146 +++++++++++++++++++++------------------------ kernel/posix-cpu-timers.c | 98 ++++++++++++++---------------- 4 files changed, 130 insertions(+), 137 deletions(-)
include/linux/sched.h +11 -3
@@ -470,6 +470,11 @@ struct pacct_struct {unsigned long ac_minflt, ac_majflt; }; +struct cpu_itimer { +cputime_t expires; +cputime_t incr; +}; +/** * struct task_cputime - collected CPU time counts * @utime: time spent in user mode, in &cputime_t units
@@ -564,9 +569,12 @@ struct signal_struct {struct pid *leader_pid;ktime_t it_real_incr; -/* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */ -cputime_t it_prof_expires, it_virt_expires; -cputime_t it_prof_incr, it_virt_incr; +/* +* ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use +* CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these +* values are defined to 0 and 1 respectively +*/ +struct cpu_itimer it[2 ];/* * Thread group totals for process CPU timers.
kernel/fork.c +5 -4
@@ -62,6 +62,7 @@#include <linux/fs_struct.h> #include <linux/magic.h> #include <linux/perf_counter.h> +#include <linux/posix-timers.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -790,10 +791,10 @@ static void posix_cpu_timers_init_group(struct signal_struct *sig)thread_group_cputime_init (sig);/* Expiration times and increments. */ - sig->it_virt_expires = cputime_zero; - sig->it_virt_incr = cputime_zero; - sig->it_prof_expires = cputime_zero; - sig->it_prof_incr = cputime_zero; + sig->it[CPUCLOCK_PROF ].expires = cputime_zero; + sig->it[CPUCLOCK_PROF ].incr = cputime_zero; + sig->it[CPUCLOCK_VIRT ].expires = cputime_zero; + sig->it[CPUCLOCK_VIRT ].incr = cputime_zero;/* Cached expiration times. */ sig->cputime_expires.prof_exp = cputime_zero;
kernel/itimer.c +68 -78
183 lines not on this page · raw message
kernel/posix-cpu-timers.c +46 -52
147 lines not on this page · raw message