Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip

* 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
itimers: Add tracepoints for itimer
hrtimer: Add tracepoint for hrtimers
timers: Add tracepoints for timer_list timers
cputime: Optimize jiffies_to_cputime(1)
itimers: Simplify arm_timer() code a bit
itimers: Fix periodic tics precision
itimers: Merge ITIMER_VIRT and ITIMER_PROF

Trivial header file include conflicts in kernel/fork.c

+620 -172
+1
arch/ia64/include/asm/cputime.h
··· 30 30 typedef u64 cputime64_t; 31 31 32 32 #define cputime_zero ((cputime_t)0) 33 + #define cputime_one_jiffy jiffies_to_cputime(1) 33 34 #define cputime_max ((~((cputime_t)0) >> 1) - 1) 34 35 #define cputime_add(__a, __b) ((__a) + (__b)) 35 36 #define cputime_sub(__a, __b) ((__a) - (__b))
+13
arch/powerpc/include/asm/cputime.h
··· 18 18 19 19 #ifndef CONFIG_VIRT_CPU_ACCOUNTING 20 20 #include <asm-generic/cputime.h> 21 + #ifdef __KERNEL__ 22 + static inline void setup_cputime_one_jiffy(void) { } 23 + #endif 21 24 #else 22 25 23 26 #include <linux/types.h> ··· 50 47 #define cputime_to_cputime64(__ct) (__ct) 51 48 52 49 #ifdef __KERNEL__ 50 + 51 + /* 52 + * One jiffy in timebase units computed during initialization 53 + */ 54 + extern cputime_t cputime_one_jiffy; 53 55 54 56 /* 55 57 * Convert cputime <-> jiffies ··· 95 87 if (sec) 96 88 ct += (cputime_t) sec * tb_ticks_per_sec; 97 89 return ct; 90 + } 91 + 92 + static inline void setup_cputime_one_jiffy(void) 93 + { 94 + cputime_one_jiffy = jiffies_to_cputime(1); 98 95 } 99 96 100 97 static inline cputime64_t jiffies64_to_cputime64(const u64 jif)
+4
arch/powerpc/kernel/time.c
··· 193 193 DEFINE_PER_CPU(unsigned long, cputime_last_delta); 194 194 DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta); 195 195 196 + cputime_t cputime_one_jiffy; 197 + 196 198 static void calc_cputime_factors(void) 197 199 { 198 200 struct div_result res; ··· 503 501 tb_to_xs = divres.result_low; 504 502 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; 505 503 vdso_data->tb_to_xs = tb_to_xs; 504 + setup_cputime_one_jiffy(); 506 505 } 507 506 else { 508 507 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n" ··· 963 960 tb_ticks_per_usec = ppc_tb_freq / 1000000; 964 961 tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000); 965 962 calc_cputime_factors(); 963 + setup_cputime_one_jiffy(); 966 964 967 965 /* 968 966 * Calculate the length of each tick in ns. It will not be
+1
arch/s390/include/asm/cputime.h
··· 42 42 #endif /* __s390x__ */ 43 43 44 44 #define cputime_zero (0ULL) 45 + #define cputime_one_jiffy jiffies_to_cputime(1) 45 46 #define cputime_max ((~0UL >> 1) - 1) 46 47 #define cputime_add(__a, __b) ((__a) + (__b)) 47 48 #define cputime_sub(__a, __b) ((__a) - (__b))
+1
include/asm-generic/cputime.h
··· 7 7 typedef unsigned long cputime_t; 8 8 9 9 #define cputime_zero (0UL) 10 + #define cputime_one_jiffy jiffies_to_cputime(1) 10 11 #define cputime_max ((~0UL >> 1) - 1) 11 12 #define cputime_add(__a, __b) ((__a) + (__b)) 12 13 #define cputime_sub(__a, __b) ((__a) - (__b))
+13 -3
include/linux/sched.h
··· 493 493 unsigned long ac_minflt, ac_majflt; 494 494 }; 495 495 496 + struct cpu_itimer { 497 + cputime_t expires; 498 + cputime_t incr; 499 + u32 error; 500 + u32 incr_error; 501 + }; 502 + 496 503 /** 497 504 * struct task_cputime - collected CPU time counts 498 505 * @utime: time spent in user mode, in &cputime_t units ··· 594 587 struct pid *leader_pid; 595 588 ktime_t it_real_incr; 596 589 597 - /* ITIMER_PROF and ITIMER_VIRTUAL timers for the process */ 598 - cputime_t it_prof_expires, it_virt_expires; 599 - cputime_t it_prof_incr, it_virt_incr; 590 + /* 591 + * ITIMER_PROF and ITIMER_VIRTUAL timers for the process, we use 592 + * CPUCLOCK_PROF and CPUCLOCK_VIRT for indexing array as these 593 + * values are defined to 0 and 1 respectively 594 + */ 595 + struct cpu_itimer it[2]; 600 596 601 597 /* 602 598 * Thread group totals for process CPU timers.
+342
include/trace/events/timer.h
··· 1 + #undef TRACE_SYSTEM 2 + #define TRACE_SYSTEM timer 3 + 4 + #if !defined(_TRACE_TIMER_H) || defined(TRACE_HEADER_MULTI_READ) 5 + #define _TRACE_TIMER_H 6 + 7 + #include <linux/tracepoint.h> 8 + #include <linux/hrtimer.h> 9 + #include <linux/timer.h> 10 + 11 + /** 12 + * timer_init - called when the timer is initialized 13 + * @timer: pointer to struct timer_list 14 + */ 15 + TRACE_EVENT(timer_init, 16 + 17 + TP_PROTO(struct timer_list *timer), 18 + 19 + TP_ARGS(timer), 20 + 21 + TP_STRUCT__entry( 22 + __field( void *, timer ) 23 + ), 24 + 25 + TP_fast_assign( 26 + __entry->timer = timer; 27 + ), 28 + 29 + TP_printk("timer %p", __entry->timer) 30 + ); 31 + 32 + /** 33 + * timer_start - called when the timer is started 34 + * @timer: pointer to struct timer_list 35 + * @expires: the timers expiry time 36 + */ 37 + TRACE_EVENT(timer_start, 38 + 39 + TP_PROTO(struct timer_list *timer, unsigned long expires), 40 + 41 + TP_ARGS(timer, expires), 42 + 43 + TP_STRUCT__entry( 44 + __field( void *, timer ) 45 + __field( void *, function ) 46 + __field( unsigned long, expires ) 47 + __field( unsigned long, now ) 48 + ), 49 + 50 + TP_fast_assign( 51 + __entry->timer = timer; 52 + __entry->function = timer->function; 53 + __entry->expires = expires; 54 + __entry->now = jiffies; 55 + ), 56 + 57 + TP_printk("timer %p: func %pf, expires %lu, timeout %ld", 58 + __entry->timer, __entry->function, __entry->expires, 59 + (long)__entry->expires - __entry->now) 60 + ); 61 + 62 + /** 63 + * timer_expire_entry - called immediately before the timer callback 64 + * @timer: pointer to struct timer_list 65 + * 66 + * Allows to determine the timer latency. 67 + */ 68 + TRACE_EVENT(timer_expire_entry, 69 + 70 + TP_PROTO(struct timer_list *timer), 71 + 72 + TP_ARGS(timer), 73 + 74 + TP_STRUCT__entry( 75 + __field( void *, timer ) 76 + __field( unsigned long, now ) 77 + ), 78 + 79 + TP_fast_assign( 80 + __entry->timer = timer; 81 + __entry->now = jiffies; 82 + ), 83 + 84 + TP_printk("timer %p: now %lu", __entry->timer, __entry->now) 85 + ); 86 + 87 + /** 88 + * timer_expire_exit - called immediately after the timer callback returns 89 + * @timer: pointer to struct timer_list 90 + * 91 + * When used in combination with the timer_expire_entry tracepoint we can 92 + * determine the runtime of the timer callback function. 93 + * 94 + * NOTE: Do NOT derefernce timer in TP_fast_assign. The pointer might 95 + * be invalid. We solely track the pointer. 96 + */ 97 + TRACE_EVENT(timer_expire_exit, 98 + 99 + TP_PROTO(struct timer_list *timer), 100 + 101 + TP_ARGS(timer), 102 + 103 + TP_STRUCT__entry( 104 + __field(void *, timer ) 105 + ), 106 + 107 + TP_fast_assign( 108 + __entry->timer = timer; 109 + ), 110 + 111 + TP_printk("timer %p", __entry->timer) 112 + ); 113 + 114 + /** 115 + * timer_cancel - called when the timer is canceled 116 + * @timer: pointer to struct timer_list 117 + */ 118 + TRACE_EVENT(timer_cancel, 119 + 120 + TP_PROTO(struct timer_list *timer), 121 + 122 + TP_ARGS(timer), 123 + 124 + TP_STRUCT__entry( 125 + __field( void *, timer ) 126 + ), 127 + 128 + TP_fast_assign( 129 + __entry->timer = timer; 130 + ), 131 + 132 + TP_printk("timer %p", __entry->timer) 133 + ); 134 + 135 + /** 136 + * hrtimer_init - called when the hrtimer is initialized 137 + * @timer: pointer to struct hrtimer 138 + * @clockid: the hrtimers clock 139 + * @mode: the hrtimers mode 140 + */ 141 + TRACE_EVENT(hrtimer_init, 142 + 143 + TP_PROTO(struct hrtimer *timer, clockid_t clockid, 144 + enum hrtimer_mode mode), 145 + 146 + TP_ARGS(timer, clockid, mode), 147 + 148 + TP_STRUCT__entry( 149 + __field( void *, timer ) 150 + __field( clockid_t, clockid ) 151 + __field( enum hrtimer_mode, mode ) 152 + ), 153 + 154 + TP_fast_assign( 155 + __entry->timer = timer; 156 + __entry->clockid = clockid; 157 + __entry->mode = mode; 158 + ), 159 + 160 + TP_printk("hrtimer %p, clockid %s, mode %s", __entry->timer, 161 + __entry->clockid == CLOCK_REALTIME ? 162 + "CLOCK_REALTIME" : "CLOCK_MONOTONIC", 163 + __entry->mode == HRTIMER_MODE_ABS ? 164 + "HRTIMER_MODE_ABS" : "HRTIMER_MODE_REL") 165 + ); 166 + 167 + /** 168 + * hrtimer_start - called when the hrtimer is started 169 + * @timer: pointer to struct hrtimer 170 + */ 171 + TRACE_EVENT(hrtimer_start, 172 + 173 + TP_PROTO(struct hrtimer *timer), 174 + 175 + TP_ARGS(timer), 176 + 177 + TP_STRUCT__entry( 178 + __field( void *, timer ) 179 + __field( void *, function ) 180 + __field( s64, expires ) 181 + __field( s64, softexpires ) 182 + ), 183 + 184 + TP_fast_assign( 185 + __entry->timer = timer; 186 + __entry->function = timer->function; 187 + __entry->expires = hrtimer_get_expires(timer).tv64; 188 + __entry->softexpires = hrtimer_get_softexpires(timer).tv64; 189 + ), 190 + 191 + TP_printk("hrtimer %p, func %pf, expires %llu, softexpires %llu", 192 + __entry->timer, __entry->function, 193 + (unsigned long long)ktime_to_ns((ktime_t) { 194 + .tv64 = __entry->expires }), 195 + (unsigned long long)ktime_to_ns((ktime_t) { 196 + .tv64 = __entry->softexpires })) 197 + ); 198 + 199 + /** 200 + * htimmer_expire_entry - called immediately before the hrtimer callback 201 + * @timer: pointer to struct hrtimer 202 + * @now: pointer to variable which contains current time of the 203 + * timers base. 204 + * 205 + * Allows to determine the timer latency. 206 + */ 207 + TRACE_EVENT(hrtimer_expire_entry, 208 + 209 + TP_PROTO(struct hrtimer *timer, ktime_t *now), 210 + 211 + TP_ARGS(timer, now), 212 + 213 + TP_STRUCT__entry( 214 + __field( void *, timer ) 215 + __field( s64, now ) 216 + ), 217 + 218 + TP_fast_assign( 219 + __entry->timer = timer; 220 + __entry->now = now->tv64; 221 + ), 222 + 223 + TP_printk("hrtimer %p, now %llu", __entry->timer, 224 + (unsigned long long)ktime_to_ns((ktime_t) { 225 + .tv64 = __entry->now })) 226 + ); 227 + 228 + /** 229 + * hrtimer_expire_exit - called immediately after the hrtimer callback returns 230 + * @timer: pointer to struct hrtimer 231 + * 232 + * When used in combination with the hrtimer_expire_entry tracepoint we can 233 + * determine the runtime of the callback function. 234 + */ 235 + TRACE_EVENT(hrtimer_expire_exit, 236 + 237 + TP_PROTO(struct hrtimer *timer), 238 + 239 + TP_ARGS(timer), 240 + 241 + TP_STRUCT__entry( 242 + __field( void *, timer ) 243 + ), 244 + 245 + TP_fast_assign( 246 + __entry->timer = timer; 247 + ), 248 + 249 + TP_printk("hrtimer %p", __entry->timer) 250 + ); 251 + 252 + /** 253 + * hrtimer_cancel - called when the hrtimer is canceled 254 + * @timer: pointer to struct hrtimer 255 + */ 256 + TRACE_EVENT(hrtimer_cancel, 257 + 258 + TP_PROTO(struct hrtimer *timer), 259 + 260 + TP_ARGS(timer), 261 + 262 + TP_STRUCT__entry( 263 + __field( void *, timer ) 264 + ), 265 + 266 + TP_fast_assign( 267 + __entry->timer = timer; 268 + ), 269 + 270 + TP_printk("hrtimer %p", __entry->timer) 271 + ); 272 + 273 + /** 274 + * itimer_state - called when itimer is started or canceled 275 + * @which: name of the interval timer 276 + * @value: the itimers value, itimer is canceled if value->it_value is 277 + * zero, otherwise it is started 278 + * @expires: the itimers expiry time 279 + */ 280 + TRACE_EVENT(itimer_state, 281 + 282 + TP_PROTO(int which, const struct itimerval *const value, 283 + cputime_t expires), 284 + 285 + TP_ARGS(which, value, expires), 286 + 287 + TP_STRUCT__entry( 288 + __field( int, which ) 289 + __field( cputime_t, expires ) 290 + __field( long, value_sec ) 291 + __field( long, value_usec ) 292 + __field( long, interval_sec ) 293 + __field( long, interval_usec ) 294 + ), 295 + 296 + TP_fast_assign( 297 + __entry->which = which; 298 + __entry->expires = expires; 299 + __entry->value_sec = value->it_value.tv_sec; 300 + __entry->value_usec = value->it_value.tv_usec; 301 + __entry->interval_sec = value->it_interval.tv_sec; 302 + __entry->interval_usec = value->it_interval.tv_usec; 303 + ), 304 + 305 + TP_printk("which %d, expires %lu, it_value %lu.%lu, it_interval %lu.%lu", 306 + __entry->which, __entry->expires, 307 + __entry->value_sec, __entry->value_usec, 308 + __entry->interval_sec, __entry->interval_usec) 309 + ); 310 + 311 + /** 312 + * itimer_expire - called when itimer expires 313 + * @which: type of the interval timer 314 + * @pid: pid of the process which owns the timer 315 + * @now: current time, used to calculate the latency of itimer 316 + */ 317 + TRACE_EVENT(itimer_expire, 318 + 319 + TP_PROTO(int which, struct pid *pid, cputime_t now), 320 + 321 + TP_ARGS(which, pid, now), 322 + 323 + TP_STRUCT__entry( 324 + __field( int , which ) 325 + __field( pid_t, pid ) 326 + __field( cputime_t, now ) 327 + ), 328 + 329 + TP_fast_assign( 330 + __entry->which = which; 331 + __entry->now = now; 332 + __entry->pid = pid_nr(pid); 333 + ), 334 + 335 + TP_printk("which %d, pid %d, now %lu", __entry->which, 336 + (int) __entry->pid, __entry->now) 337 + ); 338 + 339 + #endif /* _TRACE_TIMER_H */ 340 + 341 + /* This part must be outside protection */ 342 + #include <trace/define_trace.h>
+5 -4
kernel/fork.c
··· 63 63 #include <linux/fs_struct.h> 64 64 #include <linux/magic.h> 65 65 #include <linux/perf_event.h> 66 + #include <linux/posix-timers.h> 66 67 67 68 #include <asm/pgtable.h> 68 69 #include <asm/pgalloc.h> ··· 806 805 thread_group_cputime_init(sig); 807 806 808 807 /* Expiration times and increments. */ 809 - sig->it_virt_expires = cputime_zero; 810 - sig->it_virt_incr = cputime_zero; 811 - sig->it_prof_expires = cputime_zero; 812 - sig->it_prof_incr = cputime_zero; 808 + sig->it[CPUCLOCK_PROF].expires = cputime_zero; 809 + sig->it[CPUCLOCK_PROF].incr = cputime_zero; 810 + sig->it[CPUCLOCK_VIRT].expires = cputime_zero; 811 + sig->it[CPUCLOCK_VIRT].incr = cputime_zero; 813 812 814 813 /* Cached expiration times. */ 815 814 sig->cputime_expires.prof_exp = cputime_zero;
+32 -8
kernel/hrtimer.c
··· 48 48 49 49 #include <asm/uaccess.h> 50 50 51 + #include <trace/events/timer.h> 52 + 51 53 /* 52 54 * The timer bases: 53 55 * ··· 444 442 static inline void debug_hrtimer_deactivate(struct hrtimer *timer) { } 445 443 #endif 446 444 445 + static inline void 446 + debug_init(struct hrtimer *timer, clockid_t clockid, 447 + enum hrtimer_mode mode) 448 + { 449 + debug_hrtimer_init(timer); 450 + trace_hrtimer_init(timer, clockid, mode); 451 + } 452 + 453 + static inline void debug_activate(struct hrtimer *timer) 454 + { 455 + debug_hrtimer_activate(timer); 456 + trace_hrtimer_start(timer); 457 + } 458 + 459 + static inline void debug_deactivate(struct hrtimer *timer) 460 + { 461 + debug_hrtimer_deactivate(timer); 462 + trace_hrtimer_cancel(timer); 463 + } 464 + 447 465 /* High resolution timer related functions */ 448 466 #ifdef CONFIG_HIGH_RES_TIMERS 449 467 ··· 820 798 struct hrtimer *entry; 821 799 int leftmost = 1; 822 800 823 - debug_hrtimer_activate(timer); 801 + debug_activate(timer); 824 802 825 803 /* 826 804 * Find the right place in the rbtree: ··· 906 884 * reprogramming happens in the interrupt handler. This is a 907 885 * rare case and less expensive than a smp call. 908 886 */ 909 - debug_hrtimer_deactivate(timer); 887 + debug_deactivate(timer); 910 888 timer_stats_hrtimer_clear_start_info(timer); 911 889 reprogram = base->cpu_base == &__get_cpu_var(hrtimer_bases); 912 890 __remove_hrtimer(timer, base, HRTIMER_STATE_INACTIVE, ··· 1139 1117 void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, 1140 1118 enum hrtimer_mode mode) 1141 1119 { 1142 - debug_hrtimer_init(timer); 1120 + debug_init(timer, clock_id, mode); 1143 1121 __hrtimer_init(timer, clock_id, mode); 1144 1122 } 1145 1123 EXPORT_SYMBOL_GPL(hrtimer_init); ··· 1163 1141 } 1164 1142 EXPORT_SYMBOL_GPL(hrtimer_get_res); 1165 1143 1166 - static void __run_hrtimer(struct hrtimer *timer) 1144 + static void __run_hrtimer(struct hrtimer *timer, ktime_t *now) 1167 1145 { 1168 1146 struct hrtimer_clock_base *base = timer->base; 1169 1147 struct hrtimer_cpu_base *cpu_base = base->cpu_base; ··· 1172 1150 1173 1151 WARN_ON(!irqs_disabled()); 1174 1152 1175 - debug_hrtimer_deactivate(timer); 1153 + debug_deactivate(timer); 1176 1154 __remove_hrtimer(timer, base, HRTIMER_STATE_CALLBACK, 0); 1177 1155 timer_stats_account_hrtimer(timer); 1178 1156 fn = timer->function; ··· 1183 1161 * the timer base. 1184 1162 */ 1185 1163 spin_unlock(&cpu_base->lock); 1164 + trace_hrtimer_expire_entry(timer, now); 1186 1165 restart = fn(timer); 1166 + trace_hrtimer_expire_exit(timer); 1187 1167 spin_lock(&cpu_base->lock); 1188 1168 1189 1169 /* ··· 1296 1272 break; 1297 1273 } 1298 1274 1299 - __run_hrtimer(timer); 1275 + __run_hrtimer(timer, &basenow); 1300 1276 } 1301 1277 base++; 1302 1278 } ··· 1418 1394 hrtimer_get_expires_tv64(timer)) 1419 1395 break; 1420 1396 1421 - __run_hrtimer(timer); 1397 + __run_hrtimer(timer, &base->softirq_time); 1422 1398 } 1423 1399 spin_unlock(&cpu_base->lock); 1424 1400 } ··· 1595 1571 while ((node = rb_first(&old_base->active))) { 1596 1572 timer = rb_entry(node, struct hrtimer, node); 1597 1573 BUG_ON(hrtimer_callback_running(timer)); 1598 - debug_hrtimer_deactivate(timer); 1574 + debug_deactivate(timer); 1599 1575 1600 1576 /* 1601 1577 * Mark it as STATE_MIGRATE not INACTIVE otherwise the
+91 -78
kernel/itimer.c
··· 12 12 #include <linux/time.h> 13 13 #include <linux/posix-timers.h> 14 14 #include <linux/hrtimer.h> 15 + #include <trace/events/timer.h> 15 16 16 17 #include <asm/uaccess.h> 17 18 ··· 42 41 return ktime_to_timeval(rem); 43 42 } 44 43 44 + static void get_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, 45 + struct itimerval *const value) 46 + { 47 + cputime_t cval, cinterval; 48 + struct cpu_itimer *it = &tsk->signal->it[clock_id]; 49 + 50 + spin_lock_irq(&tsk->sighand->siglock); 51 + 52 + cval = it->expires; 53 + cinterval = it->incr; 54 + if (!cputime_eq(cval, cputime_zero)) { 55 + struct task_cputime cputime; 56 + cputime_t t; 57 + 58 + thread_group_cputimer(tsk, &cputime); 59 + if (clock_id == CPUCLOCK_PROF) 60 + t = cputime_add(cputime.utime, cputime.stime); 61 + else 62 + /* CPUCLOCK_VIRT */ 63 + t = cputime.utime; 64 + 65 + if (cputime_le(cval, t)) 66 + /* about to fire */ 67 + cval = cputime_one_jiffy; 68 + else 69 + cval = cputime_sub(cval, t); 70 + } 71 + 72 + spin_unlock_irq(&tsk->sighand->siglock); 73 + 74 + cputime_to_timeval(cval, &value->it_value); 75 + cputime_to_timeval(cinterval, &value->it_interval); 76 + } 77 + 45 78 int do_getitimer(int which, struct itimerval *value) 46 79 { 47 80 struct task_struct *tsk = current; 48 - cputime_t cinterval, cval; 49 81 50 82 switch (which) { 51 83 case ITIMER_REAL: ··· 89 55 spin_unlock_irq(&tsk->sighand->siglock); 90 56 break; 91 57 case ITIMER_VIRTUAL: 92 - spin_lock_irq(&tsk->sighand->siglock); 93 - cval = tsk->signal->it_virt_expires; 94 - cinterval = tsk->signal->it_virt_incr; 95 - if (!cputime_eq(cval, cputime_zero)) { 96 - struct task_cputime cputime; 97 - cputime_t utime; 98 - 99 - thread_group_cputimer(tsk, &cputime); 100 - utime = cputime.utime; 101 - if (cputime_le(cval, utime)) { /* about to fire */ 102 - cval = jiffies_to_cputime(1); 103 - } else { 104 - cval = cputime_sub(cval, utime); 105 - } 106 - } 107 - spin_unlock_irq(&tsk->sighand->siglock); 108 - cputime_to_timeval(cval, &value->it_value); 109 - cputime_to_timeval(cinterval, &value->it_interval); 58 + get_cpu_itimer(tsk, CPUCLOCK_VIRT, value); 110 59 break; 111 60 case ITIMER_PROF: 112 - spin_lock_irq(&tsk->sighand->siglock); 113 - cval = tsk->signal->it_prof_expires; 114 - cinterval = tsk->signal->it_prof_incr; 115 - if (!cputime_eq(cval, cputime_zero)) { 116 - struct task_cputime times; 117 - cputime_t ptime; 118 - 119 - thread_group_cputimer(tsk, &times); 120 - ptime = cputime_add(times.utime, times.stime); 121 - if (cputime_le(cval, ptime)) { /* about to fire */ 122 - cval = jiffies_to_cputime(1); 123 - } else { 124 - cval = cputime_sub(cval, ptime); 125 - } 126 - } 127 - spin_unlock_irq(&tsk->sighand->siglock); 128 - cputime_to_timeval(cval, &value->it_value); 129 - cputime_to_timeval(cinterval, &value->it_interval); 61 + get_cpu_itimer(tsk, CPUCLOCK_PROF, value); 130 62 break; 131 63 default: 132 64 return(-EINVAL); ··· 123 123 struct signal_struct *sig = 124 124 container_of(timer, struct signal_struct, real_timer); 125 125 126 + trace_itimer_expire(ITIMER_REAL, sig->leader_pid, 0); 126 127 kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid); 127 128 128 129 return HRTIMER_NORESTART; 130 + } 131 + 132 + static inline u32 cputime_sub_ns(cputime_t ct, s64 real_ns) 133 + { 134 + struct timespec ts; 135 + s64 cpu_ns; 136 + 137 + cputime_to_timespec(ct, &ts); 138 + cpu_ns = timespec_to_ns(&ts); 139 + 140 + return (cpu_ns <= real_ns) ? 0 : cpu_ns - real_ns; 141 + } 142 + 143 + static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, 144 + const struct itimerval *const value, 145 + struct itimerval *const ovalue) 146 + { 147 + cputime_t cval, nval, cinterval, ninterval; 148 + s64 ns_ninterval, ns_nval; 149 + struct cpu_itimer *it = &tsk->signal->it[clock_id]; 150 + 151 + nval = timeval_to_cputime(&value->it_value); 152 + ns_nval = timeval_to_ns(&value->it_value); 153 + ninterval = timeval_to_cputime(&value->it_interval); 154 + ns_ninterval = timeval_to_ns(&value->it_interval); 155 + 156 + it->incr_error = cputime_sub_ns(ninterval, ns_ninterval); 157 + it->error = cputime_sub_ns(nval, ns_nval); 158 + 159 + spin_lock_irq(&tsk->sighand->siglock); 160 + 161 + cval = it->expires; 162 + cinterval = it->incr; 163 + if (!cputime_eq(cval, cputime_zero) || 164 + !cputime_eq(nval, cputime_zero)) { 165 + if (cputime_gt(nval, cputime_zero)) 166 + nval = cputime_add(nval, cputime_one_jiffy); 167 + set_process_cpu_timer(tsk, clock_id, &nval, &cval); 168 + } 169 + it->expires = nval; 170 + it->incr = ninterval; 171 + trace_itimer_state(clock_id == CPUCLOCK_VIRT ? 172 + ITIMER_VIRTUAL : ITIMER_PROF, value, nval); 173 + 174 + spin_unlock_irq(&tsk->sighand->siglock); 175 + 176 + if (ovalue) { 177 + cputime_to_timeval(cval, &ovalue->it_value); 178 + cputime_to_timeval(cinterval, &ovalue->it_interval); 179 + } 129 180 } 130 181 131 182 /* ··· 190 139 struct task_struct *tsk = current; 191 140 struct hrtimer *timer; 192 141 ktime_t expires; 193 - cputime_t cval, cinterval, nval, ninterval; 194 142 195 143 /* 196 144 * Validate the timevals in value. ··· 221 171 } else 222 172 tsk->signal->it_real_incr.tv64 = 0; 223 173 174 + trace_itimer_state(ITIMER_REAL, value, 0); 224 175 spin_unlock_irq(&tsk->sighand->siglock); 225 176 break; 226 177 case ITIMER_VIRTUAL: 227 - nval = timeval_to_cputime(&value->it_value); 228 - ninterval = timeval_to_cputime(&value->it_interval); 229 - spin_lock_irq(&tsk->sighand->siglock); 230 - cval = tsk->signal->it_virt_expires; 231 - cinterval = tsk->signal->it_virt_incr; 232 - if (!cputime_eq(cval, cputime_zero) || 233 - !cputime_eq(nval, cputime_zero)) { 234 - if (cputime_gt(nval, cputime_zero)) 235 - nval = cputime_add(nval, 236 - jiffies_to_cputime(1)); 237 - set_process_cpu_timer(tsk, CPUCLOCK_VIRT, 238 - &nval, &cval); 239 - } 240 - tsk->signal->it_virt_expires = nval; 241 - tsk->signal->it_virt_incr = ninterval; 242 - spin_unlock_irq(&tsk->sighand->siglock); 243 - if (ovalue) { 244 - cputime_to_timeval(cval, &ovalue->it_value); 245 - cputime_to_timeval(cinterval, &ovalue->it_interval); 246 - } 178 + set_cpu_itimer(tsk, CPUCLOCK_VIRT, value, ovalue); 247 179 break; 248 180 case ITIMER_PROF: 249 - nval = timeval_to_cputime(&value->it_value); 250 - ninterval = timeval_to_cputime(&value->it_interval); 251 - spin_lock_irq(&tsk->sighand->siglock); 252 - cval = tsk->signal->it_prof_expires; 253 - cinterval = tsk->signal->it_prof_incr; 254 - if (!cputime_eq(cval, cputime_zero) || 255 - !cputime_eq(nval, cputime_zero)) { 256 - if (cputime_gt(nval, cputime_zero)) 257 - nval = cputime_add(nval, 258 - jiffies_to_cputime(1)); 259 - set_process_cpu_timer(tsk, CPUCLOCK_PROF, 260 - &nval, &cval); 261 - } 262 - tsk->signal->it_prof_expires = nval; 263 - tsk->signal->it_prof_incr = ninterval; 264 - spin_unlock_irq(&tsk->sighand->siglock); 265 - if (ovalue) { 266 - cputime_to_timeval(cval, &ovalue->it_value); 267 - cputime_to_timeval(cinterval, &ovalue->it_interval); 268 - } 181 + set_cpu_itimer(tsk, CPUCLOCK_PROF, value, ovalue); 269 182 break; 270 183 default: 271 184 return -EINVAL;
+85 -70
kernel/posix-cpu-timers.c
··· 8 8 #include <linux/math64.h> 9 9 #include <asm/uaccess.h> 10 10 #include <linux/kernel_stat.h> 11 + #include <trace/events/timer.h> 11 12 12 13 /* 13 14 * Called after updating RLIMIT_CPU to set timer expiration if necessary. 14 15 */ 15 16 void update_rlimit_cpu(unsigned long rlim_new) 16 17 { 17 - cputime_t cputime; 18 + cputime_t cputime = secs_to_cputime(rlim_new); 19 + struct signal_struct *const sig = current->signal; 18 20 19 - cputime = secs_to_cputime(rlim_new); 20 - if (cputime_eq(current->signal->it_prof_expires, cputime_zero) || 21 - cputime_gt(current->signal->it_prof_expires, cputime)) { 21 + if (cputime_eq(sig->it[CPUCLOCK_PROF].expires, cputime_zero) || 22 + cputime_gt(sig->it[CPUCLOCK_PROF].expires, cputime)) { 22 23 spin_lock_irq(&current->sighand->siglock); 23 24 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL); 24 25 spin_unlock_irq(&current->sighand->siglock); ··· 543 542 now); 544 543 } 545 544 545 + static inline int expires_gt(cputime_t expires, cputime_t new_exp) 546 + { 547 + return cputime_eq(expires, cputime_zero) || 548 + cputime_gt(expires, new_exp); 549 + } 550 + 551 + static inline int expires_le(cputime_t expires, cputime_t new_exp) 552 + { 553 + return !cputime_eq(expires, cputime_zero) && 554 + cputime_le(expires, new_exp); 555 + } 546 556 /* 547 557 * Insert the timer on the appropriate list before any timers that 548 558 * expire later. This must be called with the tasklist_lock held ··· 598 586 */ 599 587 600 588 if (CPUCLOCK_PERTHREAD(timer->it_clock)) { 589 + union cpu_time_count *exp = &nt->expires; 590 + 601 591 switch (CPUCLOCK_WHICH(timer->it_clock)) { 602 592 default: 603 593 BUG(); 604 594 case CPUCLOCK_PROF: 605 - if (cputime_eq(p->cputime_expires.prof_exp, 606 - cputime_zero) || 607 - cputime_gt(p->cputime_expires.prof_exp, 608 - nt->expires.cpu)) 609 - p->cputime_expires.prof_exp = 610 - nt->expires.cpu; 595 + if (expires_gt(p->cputime_expires.prof_exp, 596 + exp->cpu)) 597 + p->cputime_expires.prof_exp = exp->cpu; 611 598 break; 612 599 case CPUCLOCK_VIRT: 613 - if (cputime_eq(p->cputime_expires.virt_exp, 614 - cputime_zero) || 615 - cputime_gt(p->cputime_expires.virt_exp, 616 - nt->expires.cpu)) 617 - p->cputime_expires.virt_exp = 618 - nt->expires.cpu; 600 + if (expires_gt(p->cputime_expires.virt_exp, 601 + exp->cpu)) 602 + p->cputime_expires.virt_exp = exp->cpu; 619 603 break; 620 604 case CPUCLOCK_SCHED: 621 605 if (p->cputime_expires.sched_exp == 0 || 622 - p->cputime_expires.sched_exp > 623 - nt->expires.sched) 606 + p->cputime_expires.sched_exp > exp->sched) 624 607 p->cputime_expires.sched_exp = 625 - nt->expires.sched; 608 + exp->sched; 626 609 break; 627 610 } 628 611 } else { 612 + struct signal_struct *const sig = p->signal; 613 + union cpu_time_count *exp = &timer->it.cpu.expires; 614 + 629 615 /* 630 616 * For a process timer, set the cached expiration time. 631 617 */ ··· 631 621 default: 632 622 BUG(); 633 623 case CPUCLOCK_VIRT: 634 - if (!cputime_eq(p->signal->it_virt_expires, 635 - cputime_zero) && 636 - cputime_lt(p->signal->it_virt_expires, 637 - timer->it.cpu.expires.cpu)) 624 + if (expires_le(sig->it[CPUCLOCK_VIRT].expires, 625 + exp->cpu)) 638 626 break; 639 - p->signal->cputime_expires.virt_exp = 640 - timer->it.cpu.expires.cpu; 627 + sig->cputime_expires.virt_exp = exp->cpu; 641 628 break; 642 629 case CPUCLOCK_PROF: 643 - if (!cputime_eq(p->signal->it_prof_expires, 644 - cputime_zero) && 645 - cputime_lt(p->signal->it_prof_expires, 646 - timer->it.cpu.expires.cpu)) 630 + if (expires_le(sig->it[CPUCLOCK_PROF].expires, 631 + exp->cpu)) 647 632 break; 648 - i = p->signal->rlim[RLIMIT_CPU].rlim_cur; 633 + i = sig->rlim[RLIMIT_CPU].rlim_cur; 649 634 if (i != RLIM_INFINITY && 650 - i <= cputime_to_secs(timer->it.cpu.expires.cpu)) 635 + i <= cputime_to_secs(exp->cpu)) 651 636 break; 652 - p->signal->cputime_expires.prof_exp = 653 - timer->it.cpu.expires.cpu; 637 + sig->cputime_expires.prof_exp = exp->cpu; 654 638 break; 655 639 case CPUCLOCK_SCHED: 656 - p->signal->cputime_expires.sched_exp = 657 - timer->it.cpu.expires.sched; 640 + sig->cputime_expires.sched_exp = exp->sched; 658 641 break; 659 642 } 660 643 } ··· 1074 1071 spin_unlock_irqrestore(&cputimer->lock, flags); 1075 1072 } 1076 1073 1074 + static u32 onecputick; 1075 + 1076 + static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it, 1077 + cputime_t *expires, cputime_t cur_time, int signo) 1078 + { 1079 + if (cputime_eq(it->expires, cputime_zero)) 1080 + return; 1081 + 1082 + if (cputime_ge(cur_time, it->expires)) { 1083 + if (!cputime_eq(it->incr, cputime_zero)) { 1084 + it->expires = cputime_add(it->expires, it->incr); 1085 + it->error += it->incr_error; 1086 + if (it->error >= onecputick) { 1087 + it->expires = cputime_sub(it->expires, 1088 + cputime_one_jiffy); 1089 + it->error -= onecputick; 1090 + } 1091 + } else { 1092 + it->expires = cputime_zero; 1093 + } 1094 + 1095 + trace_itimer_expire(signo == SIGPROF ? 1096 + ITIMER_PROF : ITIMER_VIRTUAL, 1097 + tsk->signal->leader_pid, cur_time); 1098 + __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); 1099 + } 1100 + 1101 + if (!cputime_eq(it->expires, cputime_zero) && 1102 + (cputime_eq(*expires, cputime_zero) || 1103 + cputime_lt(it->expires, *expires))) { 1104 + *expires = it->expires; 1105 + } 1106 + } 1107 + 1077 1108 /* 1078 1109 * Check for any per-thread CPU timers that have fired and move them 1079 1110 * off the tsk->*_timers list onto the firing list. Per-thread timers ··· 1127 1090 * Don't sample the current process CPU clocks if there are no timers. 1128 1091 */ 1129 1092 if (list_empty(&timers[CPUCLOCK_PROF]) && 1130 - cputime_eq(sig->it_prof_expires, cputime_zero) && 1093 + cputime_eq(sig->it[CPUCLOCK_PROF].expires, cputime_zero) && 1131 1094 sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY && 1132 1095 list_empty(&timers[CPUCLOCK_VIRT]) && 1133 - cputime_eq(sig->it_virt_expires, cputime_zero) && 1096 + cputime_eq(sig->it[CPUCLOCK_VIRT].expires, cputime_zero) && 1134 1097 list_empty(&timers[CPUCLOCK_SCHED])) { 1135 1098 stop_process_timers(tsk); 1136 1099 return; ··· 1190 1153 /* 1191 1154 * Check for the special case process timers. 1192 1155 */ 1193 - if (!cputime_eq(sig->it_prof_expires, cputime_zero)) { 1194 - if (cputime_ge(ptime, sig->it_prof_expires)) { 1195 - /* ITIMER_PROF fires and reloads. */ 1196 - sig->it_prof_expires = sig->it_prof_incr; 1197 - if (!cputime_eq(sig->it_prof_expires, cputime_zero)) { 1198 - sig->it_prof_expires = cputime_add( 1199 - sig->it_prof_expires, ptime); 1200 - } 1201 - __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk); 1202 - } 1203 - if (!cputime_eq(sig->it_prof_expires, cputime_zero) && 1204 - (cputime_eq(prof_expires, cputime_zero) || 1205 - cputime_lt(sig->it_prof_expires, prof_expires))) { 1206 - prof_expires = sig->it_prof_expires; 1207 - } 1208 - } 1209 - if (!cputime_eq(sig->it_virt_expires, cputime_zero)) { 1210 - if (cputime_ge(utime, sig->it_virt_expires)) { 1211 - /* ITIMER_VIRTUAL fires and reloads. */ 1212 - sig->it_virt_expires = sig->it_virt_incr; 1213 - if (!cputime_eq(sig->it_virt_expires, cputime_zero)) { 1214 - sig->it_virt_expires = cputime_add( 1215 - sig->it_virt_expires, utime); 1216 - } 1217 - __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk); 1218 - } 1219 - if (!cputime_eq(sig->it_virt_expires, cputime_zero) && 1220 - (cputime_eq(virt_expires, cputime_zero) || 1221 - cputime_lt(sig->it_virt_expires, virt_expires))) { 1222 - virt_expires = sig->it_virt_expires; 1223 - } 1224 - } 1156 + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, 1157 + SIGPROF); 1158 + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, 1159 + SIGVTALRM); 1160 + 1225 1161 if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) { 1226 1162 unsigned long psecs = cputime_to_secs(ptime); 1227 1163 cputime_t x; ··· 1467 1457 if (!cputime_eq(*oldval, cputime_zero)) { 1468 1458 if (cputime_le(*oldval, now.cpu)) { 1469 1459 /* Just about to fire. */ 1470 - *oldval = jiffies_to_cputime(1); 1460 + *oldval = cputime_one_jiffy; 1471 1461 } else { 1472 1462 *oldval = cputime_sub(*oldval, now.cpu); 1473 1463 } ··· 1713 1703 .nsleep = thread_cpu_nsleep, 1714 1704 .nsleep_restart = thread_cpu_nsleep_restart, 1715 1705 }; 1706 + struct timespec ts; 1716 1707 1717 1708 register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process); 1718 1709 register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread); 1710 + 1711 + cputime_to_timespec(cputime_one_jiffy, &ts); 1712 + onecputick = ts.tv_nsec; 1713 + WARN_ON(ts.tv_sec != 0); 1719 1714 1720 1715 return 0; 1721 1716 }
+4 -5
kernel/sched.c
··· 5092 5092 */ 5093 5093 void account_process_tick(struct task_struct *p, int user_tick) 5094 5094 { 5095 - cputime_t one_jiffy = jiffies_to_cputime(1); 5096 - cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy); 5095 + cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); 5097 5096 struct rq *rq = this_rq(); 5098 5097 5099 5098 if (user_tick) 5100 - account_user_time(p, one_jiffy, one_jiffy_scaled); 5099 + account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); 5101 5100 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) 5102 - account_system_time(p, HARDIRQ_OFFSET, one_jiffy, 5101 + account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy, 5103 5102 one_jiffy_scaled); 5104 5103 else 5105 - account_idle_time(one_jiffy); 5104 + account_idle_time(cputime_one_jiffy); 5106 5105 } 5107 5106 5108 5107 /*
+28 -4
kernel/timer.c
··· 46 46 #include <asm/timex.h> 47 47 #include <asm/io.h> 48 48 49 + #define CREATE_TRACE_POINTS 50 + #include <trace/events/timer.h> 51 + 49 52 u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; 50 53 51 54 EXPORT_SYMBOL(jiffies_64); ··· 524 521 static inline void debug_timer_deactivate(struct timer_list *timer) { } 525 522 #endif 526 523 524 + static inline void debug_init(struct timer_list *timer) 525 + { 526 + debug_timer_init(timer); 527 + trace_timer_init(timer); 528 + } 529 + 530 + static inline void 531 + debug_activate(struct timer_list *timer, unsigned long expires) 532 + { 533 + debug_timer_activate(timer); 534 + trace_timer_start(timer, expires); 535 + } 536 + 537 + static inline void debug_deactivate(struct timer_list *timer) 538 + { 539 + debug_timer_deactivate(timer); 540 + trace_timer_cancel(timer); 541 + } 542 + 527 543 static void __init_timer(struct timer_list *timer, 528 544 const char *name, 529 545 struct lock_class_key *key) ··· 571 549 const char *name, 572 550 struct lock_class_key *key) 573 551 { 574 - debug_timer_init(timer); 552 + debug_init(timer); 575 553 __init_timer(timer, name, key); 576 554 } 577 555 EXPORT_SYMBOL(init_timer_key); ··· 590 568 { 591 569 struct list_head *entry = &timer->entry; 592 570 593 - debug_timer_deactivate(timer); 571 + debug_deactivate(timer); 594 572 595 573 __list_del(entry->prev, entry->next); 596 574 if (clear_pending) ··· 654 632 goto out_unlock; 655 633 } 656 634 657 - debug_timer_activate(timer); 635 + debug_activate(timer, expires); 658 636 659 637 new_base = __get_cpu_var(tvec_bases); 660 638 ··· 809 787 BUG_ON(timer_pending(timer) || !timer->function); 810 788 spin_lock_irqsave(&base->lock, flags); 811 789 timer_set_base(timer, base); 812 - debug_timer_activate(timer); 790 + debug_activate(timer, timer->expires); 813 791 if (time_before(timer->expires, base->next_timer) && 814 792 !tbase_get_deferrable(timer->base)) 815 793 base->next_timer = timer->expires; ··· 1022 1000 */ 1023 1001 lock_map_acquire(&lockdep_map); 1024 1002 1003 + trace_timer_expire_entry(timer); 1025 1004 fn(data); 1005 + trace_timer_expire_exit(timer); 1026 1006 1027 1007 lock_map_release(&lockdep_map); 1028 1008