?? posix-cpu-timers.c
字號:
t = next_thread(t); } while (t != p); break; }}static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now){ /* * That's all for this thread or process. * We leave our residual in expires to be reported. */ put_task_struct(timer->it.cpu.task); timer->it.cpu.task = NULL; timer->it.cpu.expires = cpu_time_sub(timer->it_clock, timer->it.cpu.expires, now);}/* * Insert the timer on the appropriate list before any timers that * expire later. This must be called with the tasklist_lock held * for reading, and interrupts disabled. */static void arm_timer(struct k_itimer *timer, union cpu_time_count now){ struct task_struct *p = timer->it.cpu.task; struct list_head *head, *listpos; struct cpu_timer_list *const nt = &timer->it.cpu; struct cpu_timer_list *next; unsigned long i; head = (CPUCLOCK_PERTHREAD(timer->it_clock) ? p->cpu_timers : p->signal->cpu_timers); head += CPUCLOCK_WHICH(timer->it_clock); BUG_ON(!irqs_disabled()); spin_lock(&p->sighand->siglock); listpos = head; if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) { list_for_each_entry(next, head, entry) { if (next->expires.sched > nt->expires.sched) break; listpos = &next->entry; } } else { list_for_each_entry(next, head, entry) { if (cputime_gt(next->expires.cpu, nt->expires.cpu)) break; listpos = &next->entry; } } list_add(&nt->entry, listpos); if (listpos == head) { /* * We are the new earliest-expiring timer. * If we are a thread timer, there can always * be a process timer telling us to stop earlier. */ if (CPUCLOCK_PERTHREAD(timer->it_clock)) { switch (CPUCLOCK_WHICH(timer->it_clock)) { default: BUG(); case CPUCLOCK_PROF: if (cputime_eq(p->it_prof_expires, cputime_zero) || cputime_gt(p->it_prof_expires, nt->expires.cpu)) p->it_prof_expires = nt->expires.cpu; break; case CPUCLOCK_VIRT: if (cputime_eq(p->it_virt_expires, cputime_zero) || cputime_gt(p->it_virt_expires, nt->expires.cpu)) p->it_virt_expires = nt->expires.cpu; break; case CPUCLOCK_SCHED: if (p->it_sched_expires == 0 || p->it_sched_expires > nt->expires.sched) p->it_sched_expires = nt->expires.sched; break; } } else { /* * For a process timer, we must balance * all the live threads' expirations. */ switch (CPUCLOCK_WHICH(timer->it_clock)) { default: BUG(); case CPUCLOCK_VIRT: if (!cputime_eq(p->signal->it_virt_expires, cputime_zero) && cputime_lt(p->signal->it_virt_expires, timer->it.cpu.expires.cpu)) break; goto rebalance; case CPUCLOCK_PROF: if (!cputime_eq(p->signal->it_prof_expires, cputime_zero) && cputime_lt(p->signal->it_prof_expires, timer->it.cpu.expires.cpu)) break; i = p->signal->rlim[RLIMIT_CPU].rlim_cur; if (i != RLIM_INFINITY && i <= cputime_to_secs(timer->it.cpu.expires.cpu)) break; goto rebalance; case CPUCLOCK_SCHED: rebalance: process_timer_rebalance( timer->it.cpu.task, CPUCLOCK_WHICH(timer->it_clock), timer->it.cpu.expires, now); break; } } } spin_unlock(&p->sighand->siglock);}/* * The timer is locked, fire it and arrange for its reload. */static void cpu_timer_fire(struct k_itimer *timer){ if (unlikely(timer->sigq == NULL)) { /* * This a special case for clock_nanosleep, * not a normal timer from sys_timer_create. */ wake_up_process(timer->it_process); timer->it.cpu.expires.sched = 0; } else if (timer->it.cpu.incr.sched == 0) { /* * One-shot timer. Clear it as soon as it's fired. */ posix_timer_event(timer, 0); timer->it.cpu.expires.sched = 0; } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { /* * The signal did not get queued because the signal * was ignored, so we won't get any callback to * reload the timer. But we need to keep it * ticking in case the signal is deliverable next time. */ posix_cpu_timer_schedule(timer); }}/* * Guts of sys_timer_settime for CPU timers. * This is called with the timer locked and interrupts disabled. * If we return TIMER_RETRY, it's necessary to release the timer's lock * and try again. (This happens when the timer is in the middle of firing.) */int posix_cpu_timer_set(struct k_itimer *timer, int flags, struct itimerspec *new, struct itimerspec *old){ struct task_struct *p = timer->it.cpu.task; union cpu_time_count old_expires, new_expires, val; int ret; if (unlikely(p == NULL)) { /* * Timer refers to a dead task's clock. */ return -ESRCH; } new_expires = timespec_to_sample(timer->it_clock, &new->it_value); read_lock(&tasklist_lock); /* * We need the tasklist_lock to protect against reaping that * clears p->signal. If p has just been reaped, we can no * longer get any information about it at all. */ if (unlikely(p->signal == NULL)) { read_unlock(&tasklist_lock); put_task_struct(p); timer->it.cpu.task = NULL; return -ESRCH; } /* * Disarm any old timer after extracting its expiry time. */ BUG_ON(!irqs_disabled()); ret = 0; spin_lock(&p->sighand->siglock); old_expires = timer->it.cpu.expires; if (unlikely(timer->it.cpu.firing)) { timer->it.cpu.firing = -1; ret = TIMER_RETRY; } else list_del_init(&timer->it.cpu.entry); spin_unlock(&p->sighand->siglock); /* * We need to sample the current value to convert the new * value from to relative and absolute, and to convert the * old value from absolute to relative. To set a process * timer, we need a sample to balance the thread expiry * times (in arm_timer). With an absolute time, we must * check if it's already passed. In short, we need a sample. */ if (CPUCLOCK_PERTHREAD(timer->it_clock)) { cpu_clock_sample(timer->it_clock, p, &val); } else { cpu_clock_sample_group(timer->it_clock, p, &val); } if (old) { if (old_expires.sched == 0) { old->it_value.tv_sec = 0; old->it_value.tv_nsec = 0; } else { /* * Update the timer in case it has * overrun already. If it has, * we'll report it as having overrun * and with the next reloaded timer * already ticking, though we are * swallowing that pending * notification here to install the * new setting. */ bump_cpu_timer(timer, val); if (cpu_time_before(timer->it_clock, val, timer->it.cpu.expires)) { old_expires = cpu_time_sub( timer->it_clock, timer->it.cpu.expires, val); sample_to_timespec(timer->it_clock, old_expires, &old->it_value); } else { old->it_value.tv_nsec = 1; old->it_value.tv_sec = 0; } } } if (unlikely(ret)) { /* * We are colliding with the timer actually firing. * Punt after filling in the timer's old value, and * disable this firing since we are already reporting * it as an overrun (thanks to bump_cpu_timer above). */ read_unlock(&tasklist_lock); goto out; } if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) { cpu_time_add(timer->it_clock, &new_expires, val); } /* * Install the new expiry time (or zero). * For a timer with no notification action, we don't actually * arm the timer (we'll just fake it for timer_gettime). */ timer->it.cpu.expires = new_expires; if (new_expires.sched != 0 && (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE && cpu_time_before(timer->it_clock, val, new_expires)) { arm_timer(timer, val); } read_unlock(&tasklist_lock); /* * Install the new reload setting, and * set up the signal and overrun bookkeeping. */ timer->it.cpu.incr = timespec_to_sample(timer->it_clock, &new->it_interval); /* * This acts as a modification timestamp for the timer, * so any automatic reload attempt will punt on seeing * that we have reset the timer manually. */ timer->it_requeue_pending = (timer->it_requeue_pending + 2) & ~REQUEUE_PENDING; timer->it_overrun_last = 0; timer->it_overrun = -1; if (new_expires.sched != 0 && (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE && !cpu_time_before(timer->it_clock, val, new_expires)) { /* * The designated time already passed, so we notify * immediately, even if the thread never runs to * accumulate more time on this clock. */ cpu_timer_fire(timer); } ret = 0; out: if (old) { sample_to_timespec(timer->it_clock, timer->it.cpu.incr, &old->it_interval); } return ret;}void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp){ union cpu_time_count now; struct task_struct *p = timer->it.cpu.task; int clear_dead; /* * Easy part: convert the reload time. */ sample_to_timespec(timer->it_clock, timer->it.cpu.incr, &itp->it_interval); if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all. */ itp->it_value.tv_sec = itp->it_value.tv_nsec = 0; return; } if (unlikely(p == NULL)) { /* * This task already died and the timer will never fire. * In this case, expires is actually the dead value. */ dead: sample_to_timespec(timer->it_clock, timer->it.cpu.expires, &itp->it_value); return; } /* * Sample the clock to take the difference with the expiry time. */ if (CPUCLOCK_PERTHREAD(timer->it_clock)) { cpu_clock_sample(timer->it_clock, p, &now); clear_dead = p->exit_state; } else { read_lock(&tasklist_lock); if (unlikely(p->signal == NULL)) { /* * The process has been reaped. * We can't even collect a sample any more. * Call the timer disarmed, nothing else to do. */ put_task_struct(p); timer->it.cpu.task = NULL; timer->it.cpu.expires.sched = 0; read_unlock(&tasklist_lock); goto dead; } else { cpu_clock_sample_group(timer->it_clock, p, &now); clear_dead = (unlikely(p->exit_state) && thread_group_empty(p)); } read_unlock(&tasklist_lock); } if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { if (timer->it.cpu.incr.sched == 0 && cpu_time_before(timer->it_clock, timer->it.cpu.expires, now)) { /* * Do-nothing timer expired and has no reload, * so it's as if it was never set. */ timer->it.cpu.expires.sched = 0; itp->it_value.tv_sec = itp->it_value.tv_nsec = 0; return; } /* * Account for any expirations and reloads that should * have happened. */ bump_cpu_timer(timer, now); } if (unlikely(clear_dead)) { /* * We've noticed that the thread is dead, but * not yet reaped. Take this opportunity to * drop our task ref. */ clear_dead_task(timer, now); goto dead; } if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) { sample_to_timespec(timer->it_clock, cpu_time_sub(timer->it_clock, timer->it.cpu.expires, now), &itp->it_value); } else { /* * The timer should have expired already, but the firing * hasn't taken place yet. Say it's just about to expire. */ itp->it_value.tv_nsec = 1; itp->it_value.tv_sec = 0; }}/* * Check for any per-thread CPU timers that have fired and move them off * the tsk->cpu_timers[N] list onto the firing list. Here we update the * tsk->it_*_expires values to reflect the remaining thread CPU timers. */static void check_thread_timers(struct task_struct *tsk, struct list_head *firing){ int maxfire; struct list_head *timers = tsk->cpu_timers; maxfire = 20; tsk->it_prof_expires = cputime_zero; while (!list_empty(timers)) { struct cpu_timer_list *t = list_first_entry(timers, struct cpu_timer_list, entry); if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) { tsk->it_prof_expires = t->expires.cpu; break; } t->firing = 1; list_move_tail(&t->entry, firing); } ++timers; maxfire = 20; tsk->it_virt_expires = cputime_zero; while (!list_empty(timers)) { struct cpu_timer_list *t = list_first_entry(timers, struct cpu_timer_list, entry); if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) { tsk->it_virt_expires = t->expires.cpu; break; } t->firing = 1; list_move_tail(&t->entry, firing); } ++timers; maxfire = 20; tsk->it_sched_expires = 0; while (!list_empty(timers)) { struct cpu_timer_list *t = list_first_entry(timers, struct cpu_timer_list, entry); if (!--maxfire || tsk->sched_time < t->expires.sched) { tsk->it_sched_expires = t->expires.sched; break; } t->firing = 1; list_move_tail(&t->entry, firing); }}/* * Check for any per-thread CPU timers that have fired and move them * off the tsk->*_timers list onto the firing list. Per-thread timers * have already been taken off. */static void check_process_timers(struct task_struct *tsk, struct list_head *firing){ int maxfire; struct signal_struct *const sig = tsk->signal; cputime_t utime, stime, ptime, virt_expires, prof_expires; unsigned long long sched_time, sched_expires; struct task_struct *t; struct list_head *timers = sig->cpu_timers; /* * Don't sample the current process CPU clocks if there are no timers. */ if (list_empty(&timers[CPUCLOCK_PROF]) && cputime_eq(sig->it_prof_expires, cputime_zero) && sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY && list_empty(&timers[CPUCLOCK_VIRT]) && cputime_eq(sig->it_virt_expires, cputime_zero) && list_empty(&timers[CPUCLOCK_SCHED])) return; /* * Collect the current process totals. */ utime = sig->utime; stime = sig->stime; sched_time = sig->sched_time; t = tsk; do { utime = cputime_add(utime, t->utime); stime = cputime_add(stime, t->stime); sched_time += t->sched_time; t = next_thread(t); } while (t != tsk); ptime = cputime_add(utime, stime); maxfire = 20; prof_expires = cputime_zero; while (!list_empty(timers)) { struct cpu_timer_list *t = list_first_entry(timers, struct cpu_timer_list, entry); if (!--maxfire || cputime_lt(ptime, t->expires.cpu)) { prof_expires = t->expires.cpu; break; } t->firing = 1; list_move_tail(&t->entry, firing); } ++timers; maxfire = 20; virt_expires = cputime_zero; while (!list_empty(timers)) { struct cpu_timer_list *t = list_first_entry(timers, struct cpu_timer_list, entry); if (!--maxfire || cputime_lt(utime, t->expires.cpu)) { virt_expires = t->expires.cpu; break; } t->firing = 1; list_move_tail(&t->entry, firing); } ++timers; maxfire = 20; sched_expires = 0;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -