diff options
Diffstat (limited to 'kernel/posix-cpu-timers.c')
-rw-r--r-- | kernel/posix-cpu-timers.c | 123 |
1 files changed, 115 insertions, 8 deletions
diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c index fa07da94d7be..e976e505648d 100644 --- a/kernel/posix-cpu-timers.c +++ b/kernel/posix-cpu-timers.c | |||
@@ -230,6 +230,71 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p, | |||
230 | return 0; | 230 | return 0; |
231 | } | 231 | } |
232 | 232 | ||
233 | void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times) | ||
234 | { | ||
235 | struct sighand_struct *sighand; | ||
236 | struct signal_struct *sig; | ||
237 | struct task_struct *t; | ||
238 | |||
239 | *times = INIT_CPUTIME; | ||
240 | |||
241 | rcu_read_lock(); | ||
242 | sighand = rcu_dereference(tsk->sighand); | ||
243 | if (!sighand) | ||
244 | goto out; | ||
245 | |||
246 | sig = tsk->signal; | ||
247 | |||
248 | t = tsk; | ||
249 | do { | ||
250 | times->utime = cputime_add(times->utime, t->utime); | ||
251 | times->stime = cputime_add(times->stime, t->stime); | ||
252 | times->sum_exec_runtime += t->se.sum_exec_runtime; | ||
253 | |||
254 | t = next_thread(t); | ||
255 | } while (t != tsk); | ||
256 | |||
257 | times->utime = cputime_add(times->utime, sig->utime); | ||
258 | times->stime = cputime_add(times->stime, sig->stime); | ||
259 | times->sum_exec_runtime += sig->sum_sched_runtime; | ||
260 | out: | ||
261 | rcu_read_unlock(); | ||
262 | } | ||
263 | |||
264 | static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b) | ||
265 | { | ||
266 | if (cputime_gt(b->utime, a->utime)) | ||
267 | a->utime = b->utime; | ||
268 | |||
269 | if (cputime_gt(b->stime, a->stime)) | ||
270 | a->stime = b->stime; | ||
271 | |||
272 | if (b->sum_exec_runtime > a->sum_exec_runtime) | ||
273 | a->sum_exec_runtime = b->sum_exec_runtime; | ||
274 | } | ||
275 | |||
276 | void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) | ||
277 | { | ||
278 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; | ||
279 | struct task_cputime sum; | ||
280 | unsigned long flags; | ||
281 | |||
282 | spin_lock_irqsave(&cputimer->lock, flags); | ||
283 | if (!cputimer->running) { | ||
284 | cputimer->running = 1; | ||
285 | /* | ||
286 | * The POSIX timer interface allows for absolute time expiry | ||
287 | * values through the TIMER_ABSTIME flag, therefore we have | ||
288 | * to synchronize the timer to the clock every time we start | ||
289 | * it. | ||
290 | */ | ||
291 | thread_group_cputime(tsk, &sum); | ||
292 | update_gt_cputime(&cputimer->cputime, &sum); | ||
293 | } | ||
294 | *times = cputimer->cputime; | ||
295 | spin_unlock_irqrestore(&cputimer->lock, flags); | ||
296 | } | ||
297 | |||
233 | /* | 298 | /* |
234 | * Sample a process (thread group) clock for the given group_leader task. | 299 | * Sample a process (thread group) clock for the given group_leader task. |
235 | * Must be called with tasklist_lock held for reading. | 300 | * Must be called with tasklist_lock held for reading. |
@@ -457,7 +522,7 @@ void posix_cpu_timers_exit_group(struct task_struct *tsk) | |||
457 | { | 522 | { |
458 | struct task_cputime cputime; | 523 | struct task_cputime cputime; |
459 | 524 | ||
460 | thread_group_cputime(tsk, &cputime); | 525 | thread_group_cputimer(tsk, &cputime); |
461 | cleanup_timers(tsk->signal->cpu_timers, | 526 | cleanup_timers(tsk->signal->cpu_timers, |
462 | cputime.utime, cputime.stime, cputime.sum_exec_runtime); | 527 | cputime.utime, cputime.stime, cputime.sum_exec_runtime); |
463 | } | 528 | } |
@@ -616,6 +681,33 @@ static void cpu_timer_fire(struct k_itimer *timer) | |||
616 | } | 681 | } |
617 | 682 | ||
618 | /* | 683 | /* |
684 | * Sample a process (thread group) timer for the given group_leader task. | ||
685 | * Must be called with tasklist_lock held for reading. | ||
686 | */ | ||
687 | static int cpu_timer_sample_group(const clockid_t which_clock, | ||
688 | struct task_struct *p, | ||
689 | union cpu_time_count *cpu) | ||
690 | { | ||
691 | struct task_cputime cputime; | ||
692 | |||
693 | thread_group_cputimer(p, &cputime); | ||
694 | switch (CPUCLOCK_WHICH(which_clock)) { | ||
695 | default: | ||
696 | return -EINVAL; | ||
697 | case CPUCLOCK_PROF: | ||
698 | cpu->cpu = cputime_add(cputime.utime, cputime.stime); | ||
699 | break; | ||
700 | case CPUCLOCK_VIRT: | ||
701 | cpu->cpu = cputime.utime; | ||
702 | break; | ||
703 | case CPUCLOCK_SCHED: | ||
704 | cpu->sched = cputime.sum_exec_runtime + task_delta_exec(p); | ||
705 | break; | ||
706 | } | ||
707 | return 0; | ||
708 | } | ||
709 | |||
710 | /* | ||
619 | * Guts of sys_timer_settime for CPU timers. | 711 | * Guts of sys_timer_settime for CPU timers. |
620 | * This is called with the timer locked and interrupts disabled. | 712 | * This is called with the timer locked and interrupts disabled. |
621 | * If we return TIMER_RETRY, it's necessary to release the timer's lock | 713 | * If we return TIMER_RETRY, it's necessary to release the timer's lock |
@@ -676,7 +768,7 @@ int posix_cpu_timer_set(struct k_itimer *timer, int flags, | |||
676 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { | 768 | if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
677 | cpu_clock_sample(timer->it_clock, p, &val); | 769 | cpu_clock_sample(timer->it_clock, p, &val); |
678 | } else { | 770 | } else { |
679 | cpu_clock_sample_group(timer->it_clock, p, &val); | 771 | cpu_timer_sample_group(timer->it_clock, p, &val); |
680 | } | 772 | } |
681 | 773 | ||
682 | if (old) { | 774 | if (old) { |
@@ -824,7 +916,7 @@ void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp) | |||
824 | read_unlock(&tasklist_lock); | 916 | read_unlock(&tasklist_lock); |
825 | goto dead; | 917 | goto dead; |
826 | } else { | 918 | } else { |
827 | cpu_clock_sample_group(timer->it_clock, p, &now); | 919 | cpu_timer_sample_group(timer->it_clock, p, &now); |
828 | clear_dead = (unlikely(p->exit_state) && | 920 | clear_dead = (unlikely(p->exit_state) && |
829 | thread_group_empty(p)); | 921 | thread_group_empty(p)); |
830 | } | 922 | } |
@@ -964,6 +1056,19 @@ static void check_thread_timers(struct task_struct *tsk, | |||
964 | } | 1056 | } |
965 | } | 1057 | } |
966 | 1058 | ||
1059 | static void stop_process_timers(struct task_struct *tsk) | ||
1060 | { | ||
1061 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; | ||
1062 | unsigned long flags; | ||
1063 | |||
1064 | if (!cputimer->running) | ||
1065 | return; | ||
1066 | |||
1067 | spin_lock_irqsave(&cputimer->lock, flags); | ||
1068 | cputimer->running = 0; | ||
1069 | spin_unlock_irqrestore(&cputimer->lock, flags); | ||
1070 | } | ||
1071 | |||
967 | /* | 1072 | /* |
968 | * Check for any per-thread CPU timers that have fired and move them | 1073 | * Check for any per-thread CPU timers that have fired and move them |
969 | * off the tsk->*_timers list onto the firing list. Per-thread timers | 1074 | * off the tsk->*_timers list onto the firing list. Per-thread timers |
@@ -987,13 +1092,15 @@ static void check_process_timers(struct task_struct *tsk, | |||
987 | sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY && | 1092 | sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY && |
988 | list_empty(&timers[CPUCLOCK_VIRT]) && | 1093 | list_empty(&timers[CPUCLOCK_VIRT]) && |
989 | cputime_eq(sig->it_virt_expires, cputime_zero) && | 1094 | cputime_eq(sig->it_virt_expires, cputime_zero) && |
990 | list_empty(&timers[CPUCLOCK_SCHED])) | 1095 | list_empty(&timers[CPUCLOCK_SCHED])) { |
1096 | stop_process_timers(tsk); | ||
991 | return; | 1097 | return; |
1098 | } | ||
992 | 1099 | ||
993 | /* | 1100 | /* |
994 | * Collect the current process totals. | 1101 | * Collect the current process totals. |
995 | */ | 1102 | */ |
996 | thread_group_cputime(tsk, &cputime); | 1103 | thread_group_cputimer(tsk, &cputime); |
997 | utime = cputime.utime; | 1104 | utime = cputime.utime; |
998 | ptime = cputime_add(utime, cputime.stime); | 1105 | ptime = cputime_add(utime, cputime.stime); |
999 | sum_sched_runtime = cputime.sum_exec_runtime; | 1106 | sum_sched_runtime = cputime.sum_exec_runtime; |
@@ -1164,7 +1271,7 @@ void posix_cpu_timer_schedule(struct k_itimer *timer) | |||
1164 | clear_dead_task(timer, now); | 1271 | clear_dead_task(timer, now); |
1165 | goto out_unlock; | 1272 | goto out_unlock; |
1166 | } | 1273 | } |
1167 | cpu_clock_sample_group(timer->it_clock, p, &now); | 1274 | cpu_timer_sample_group(timer->it_clock, p, &now); |
1168 | bump_cpu_timer(timer, now); | 1275 | bump_cpu_timer(timer, now); |
1169 | /* Leave the tasklist_lock locked for the call below. */ | 1276 | /* Leave the tasklist_lock locked for the call below. */ |
1170 | } | 1277 | } |
@@ -1259,7 +1366,7 @@ static inline int fastpath_timer_check(struct task_struct *tsk) | |||
1259 | if (!task_cputime_zero(&sig->cputime_expires)) { | 1366 | if (!task_cputime_zero(&sig->cputime_expires)) { |
1260 | struct task_cputime group_sample; | 1367 | struct task_cputime group_sample; |
1261 | 1368 | ||
1262 | thread_group_cputime(tsk, &group_sample); | 1369 | thread_group_cputimer(tsk, &group_sample); |
1263 | if (task_cputime_expired(&group_sample, &sig->cputime_expires)) | 1370 | if (task_cputime_expired(&group_sample, &sig->cputime_expires)) |
1264 | return 1; | 1371 | return 1; |
1265 | } | 1372 | } |
@@ -1341,7 +1448,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, | |||
1341 | struct list_head *head; | 1448 | struct list_head *head; |
1342 | 1449 | ||
1343 | BUG_ON(clock_idx == CPUCLOCK_SCHED); | 1450 | BUG_ON(clock_idx == CPUCLOCK_SCHED); |
1344 | cpu_clock_sample_group(clock_idx, tsk, &now); | 1451 | cpu_timer_sample_group(clock_idx, tsk, &now); |
1345 | 1452 | ||
1346 | if (oldval) { | 1453 | if (oldval) { |
1347 | if (!cputime_eq(*oldval, cputime_zero)) { | 1454 | if (!cputime_eq(*oldval, cputime_zero)) { |