📄 timer.c
字号:
/* * Another bit of PARANOID. Note that the retval will be * 0 since no piece of kernel is supposed to do a check * for a negative retval of schedule_timeout() (since it * should never happens anyway). You just have the printk() * that will tell you if something is gone wrong and where. */ if (timeout < 0) { printk(KERN_ERR "schedule_timeout: wrong timeout " "value %lx\n", timeout); dump_stack(); current->state = TASK_RUNNING; goto out; } } expire = timeout + jiffies; setup_timer(&timer, process_timeout, (unsigned long)current); __mod_timer(&timer, expire); schedule(); del_singleshot_timer_sync(&timer); timeout = expire - jiffies; out: return timeout < 0 ? 0 : timeout;}EXPORT_SYMBOL(schedule_timeout);/* * We can use __set_current_state() here because schedule_timeout() calls * schedule() unconditionally. */signed long __sched schedule_timeout_interruptible(signed long timeout){ __set_current_state(TASK_INTERRUPTIBLE); return schedule_timeout(timeout);}EXPORT_SYMBOL(schedule_timeout_interruptible);signed long __sched schedule_timeout_uninterruptible(signed long timeout){ __set_current_state(TASK_UNINTERRUPTIBLE); return schedule_timeout(timeout);}EXPORT_SYMBOL(schedule_timeout_uninterruptible);/* Thread ID - the internal kernel "pid" */asmlinkage long sys_gettid(void){ return current->pid;}/** * do_sysinfo - fill in sysinfo struct * @info: pointer to buffer to fill */ int do_sysinfo(struct sysinfo *info){ unsigned long mem_total, sav_total; unsigned int mem_unit, bitcount; unsigned long seq; memset(info, 0, sizeof(struct sysinfo)); do { struct timespec tp; seq = read_seqbegin(&xtime_lock); /* * This is annoying. The below is the same thing * posix_get_clock_monotonic() does, but it wants to * take the lock which we want to cover the loads stuff * too. */ getnstimeofday(&tp); tp.tv_sec += wall_to_monotonic.tv_sec; tp.tv_nsec += wall_to_monotonic.tv_nsec; if (tp.tv_nsec - NSEC_PER_SEC >= 0) { tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC; tp.tv_sec++; } info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); info->loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT); info->loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT); info->loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT); info->procs = nr_threads; } while (read_seqretry(&xtime_lock, seq)); si_meminfo(info); si_swapinfo(info); /* * If the sum of all the available memory (i.e. ram + swap) * is less than can be stored in a 32 bit unsigned long then * we can be binary compatible with 2.2.x kernels. If not, * well, in that case 2.2.x was broken anyways... * * -Erik Andersen <andersee@debian.org> */ mem_total = info->totalram + info->totalswap; if (mem_total < info->totalram || mem_total < info->totalswap) goto out; bitcount = 0; mem_unit = info->mem_unit; while (mem_unit > 1) { bitcount++; mem_unit >>= 1; sav_total = mem_total; mem_total <<= 1; if (mem_total < sav_total) goto out; } /* * If mem_total did not overflow, multiply all memory values by * info->mem_unit and set it to 1. This leaves things compatible * with 2.2.x, and also retains compatibility with earlier 2.4.x * kernels... */ info->mem_unit = 1; info->totalram <<= bitcount; info->freeram <<= bitcount; info->sharedram <<= bitcount; info->bufferram <<= bitcount; info->totalswap <<= bitcount; info->freeswap <<= bitcount; info->totalhigh <<= bitcount; info->freehigh <<= bitcount;out: return 0;}asmlinkage long sys_sysinfo(struct sysinfo __user *info){ struct sysinfo val; do_sysinfo(&val); if (copy_to_user(info, &val, sizeof(struct sysinfo))) return -EFAULT; return 0;}/* * lockdep: we want to track each per-CPU base as a separate lock-class, * but timer-bases are kmalloc()-ed, so we need to attach separate * keys to them: */static struct lock_class_key base_lock_keys[NR_CPUS];static int __devinit init_timers_cpu(int cpu){ int j; tvec_base_t *base; static char __devinitdata tvec_base_done[NR_CPUS]; if (!tvec_base_done[cpu]) { static char boot_done; if (boot_done) { /* * The APs use this path later in boot */ base = kmalloc_node(sizeof(*base), GFP_KERNEL, cpu_to_node(cpu)); if (!base) return -ENOMEM; /* Make sure that tvec_base is 2 byte aligned */ if (tbase_get_deferrable(base)) { WARN_ON(1); kfree(base); return -ENOMEM; } memset(base, 0, sizeof(*base)); per_cpu(tvec_bases, cpu) = base; } else { /* * This is for the boot CPU - we use compile-time * static initialisation because per-cpu memory isn't * ready yet and because the memory allocators are not * initialised either. */ boot_done = 1; base = &boot_tvec_bases; } tvec_base_done[cpu] = 1; } else { base = per_cpu(tvec_bases, cpu); } spin_lock_init(&base->lock); lockdep_set_class(&base->lock, base_lock_keys + cpu); for (j = 0; j < TVN_SIZE; j++) { INIT_LIST_HEAD(base->tv5.vec + j); INIT_LIST_HEAD(base->tv4.vec + j); INIT_LIST_HEAD(base->tv3.vec + j); INIT_LIST_HEAD(base->tv2.vec + j); } for (j = 0; j < TVR_SIZE; j++) INIT_LIST_HEAD(base->tv1.vec + j); base->timer_jiffies = jiffies; return 0;}#ifdef CONFIG_HOTPLUG_CPUstatic void migrate_timer_list(tvec_base_t *new_base, struct list_head *head){ struct timer_list *timer; while (!list_empty(head)) { timer = list_first_entry(head, struct timer_list, entry); detach_timer(timer, 0); timer_set_base(timer, new_base); internal_add_timer(new_base, timer); }}static void __devinit migrate_timers(int cpu){ tvec_base_t *old_base; tvec_base_t *new_base; int i; BUG_ON(cpu_online(cpu)); old_base = per_cpu(tvec_bases, cpu); new_base = get_cpu_var(tvec_bases); local_irq_disable(); double_spin_lock(&new_base->lock, &old_base->lock, smp_processor_id() < cpu); BUG_ON(old_base->running_timer); for (i = 0; i < TVR_SIZE; i++) migrate_timer_list(new_base, old_base->tv1.vec + i); for (i = 0; i < TVN_SIZE; i++) { migrate_timer_list(new_base, old_base->tv2.vec + i); migrate_timer_list(new_base, old_base->tv3.vec + i); migrate_timer_list(new_base, old_base->tv4.vec + i); migrate_timer_list(new_base, old_base->tv5.vec + i); } double_spin_unlock(&new_base->lock, &old_base->lock, smp_processor_id() < cpu); local_irq_enable(); put_cpu_var(tvec_bases);}#endif /* CONFIG_HOTPLUG_CPU */static int __cpuinit timer_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu){ long cpu = (long)hcpu; switch(action) { case CPU_UP_PREPARE: case CPU_UP_PREPARE_FROZEN: if (init_timers_cpu(cpu) < 0) return NOTIFY_BAD; break;#ifdef CONFIG_HOTPLUG_CPU case CPU_DEAD: case CPU_DEAD_FROZEN: migrate_timers(cpu); break;#endif default: break; } return NOTIFY_OK;}static struct notifier_block __cpuinitdata timers_nb = { .notifier_call = timer_cpu_notify,};void __init init_timers(void){ int err = timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE, (void *)(long)smp_processor_id()); init_timer_stats(); BUG_ON(err == NOTIFY_BAD); register_cpu_notifier(&timers_nb); open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);}#ifdef CONFIG_TIME_INTERPOLATIONstruct time_interpolator *time_interpolator __read_mostly;static struct time_interpolator *time_interpolator_list __read_mostly;static DEFINE_SPINLOCK(time_interpolator_lock);static inline cycles_t time_interpolator_get_cycles(unsigned int src){ unsigned long (*x)(void); switch (src) { case TIME_SOURCE_FUNCTION: x = time_interpolator->addr; return x(); case TIME_SOURCE_MMIO64 : return readq_relaxed((void __iomem *)time_interpolator->addr); case TIME_SOURCE_MMIO32 : return readl_relaxed((void __iomem *)time_interpolator->addr); default: return get_cycles(); }}static inline u64 time_interpolator_get_counter(int writelock){ unsigned int src = time_interpolator->source; if (time_interpolator->jitter) { cycles_t lcycle; cycles_t now; do { lcycle = time_interpolator->last_cycle; now = time_interpolator_get_cycles(src); if (lcycle && time_after(lcycle, now)) return lcycle; /* When holding the xtime write lock, there's no need * to add the overhead of the cmpxchg. Readers are * force to retry until the write lock is released. */ if (writelock) { time_interpolator->last_cycle = now; return now; } /* Keep track of the last timer value returned. The use of cmpxchg here * will cause contention in an SMP environment. */ } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle)); return now; } else return time_interpolator_get_cycles(src);}void time_interpolator_reset(void){ time_interpolator->offset = 0; time_interpolator->last_counter = time_interpolator_get_counter(1);}#define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)unsigned long time_interpolator_get_offset(void){ /* If we do not have a time interpolator set up then just return zero */ if (!time_interpolator) return 0; return time_interpolator->offset + GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);}#define INTERPOLATOR_ADJUST 65536#define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUSTvoid time_interpolator_update(long delta_nsec){ u64 counter; unsigned long offset; /* If there is no time interpolator set up then do nothing */ if (!time_interpolator) return; /* * The interpolator compensates for late ticks by accumulating the late * time in time_interpolator->offset. A tick earlier than expected will * lead to a reset of the offset and a corresponding jump of the clock * forward. Again this only works if the interpolator clock is running * slightly slower than the regular clock and the tuning logic insures * that. */ counter = time_interpolator_get_counter(1); offset = time_interpolator->offset + GET_TI_NSECS(counter, time_interpolator); if (delta_nsec < 0 || (unsigned long) delta_nsec < offset) time_interpolator->offset = offset - delta_nsec; else { time_interpolator->skips++; time_interpolator->ns_skipped += delta_nsec - offset; time_interpolator->offset = 0; } time_interpolator->last_counter = counter; /* Tuning logic for time interpolator invoked every minute or so. * Decrease interpolator clock speed if no skips occurred and an offset is carried. * Increase interpolator clock speed if we skip too much time. */ if (jiffies % INTERPOLATOR_ADJUST == 0) { if (time_interpolator->skips == 0 && time_interpolator->offset > tick_nsec) time_interpolator->nsec_per_cyc--; if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0) time_interpolator->nsec_per_cyc++; time_interpolator->skips = 0; time_interpolator->ns_skipped = 0; }}static inline intis_better_time_interpolator(struct time_interpolator *new){ if (!time_interpolator) return 1; return new->frequency > 2*time_interpolator->frequency || (unsigned long)new->drift < (unsigned long)time_interpolator->drift;}voidregister_time_interpolator(struct time_interpolator *ti){ unsigned long flags; /* Sanity check */ BUG_ON(ti->frequency == 0 || ti->mask == 0); ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency; spin_lock(&time_interpolator_lock); write_seqlock_irqsave(&xtime_lock, flags); if (is_better_time_interpolator(ti)) { time_interpolator = ti; time_interpolator_reset(); } write_sequnlock_irqrestore(&xtime_lock, flags); ti->next = time_interpolator_list; time_interpolator_list = ti; spin_unlock(&time_interpolator_lock);}voidunregister_time_interpolator(struct time_interpolator *ti){ struct time_interpolator *curr, **prev; unsigned long flags; spin_lock(&time_interpolator_lock); prev = &time_interpolator_list; for (curr = *prev; curr; curr = curr->next) { if (curr == ti) { *prev = curr->next; break; } prev = &curr->next; } write_seqlock_irqsave(&xtime_lock, flags); if (ti == time_interpolator) { /* we lost the best time-interpolator: */ time_interpolator = NULL; /* find the next-best interpolator */ for (curr = time_interpolator_list; curr; curr = curr->next) if (is_better_time_interpolator(curr)) time_interpolator = curr; time_interpolator_reset(); } write_sequnlock_irqrestore(&xtime_lock, flags); spin_unlock(&time_interpolator_lock);}#endif /* CONFIG_TIME_INTERPOLATION *//** * msleep - sleep safely even with waitqueue interruptions * @msecs: Time in milliseconds to sleep for */void msleep(unsigned int msecs){ unsigned long timeout = msecs_to_jiffies(msecs) + 1; while (timeout) timeout = schedule_timeout_uninterruptible(timeout);}EXPORT_SYMBOL(msleep);/** * msleep_interruptible - sleep waiting for signals * @msecs: Time in milliseconds to sleep for */unsigned long msleep_interruptible(unsigned int msecs){ unsigned long timeout = msecs_to_jiffies(msecs) + 1; while (timeout && !signal_pending(current)) timeout = schedule_timeout_interruptible(timeout); return jiffies_to_msecs(timeout);}EXPORT_SYMBOL(msleep_interruptible);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -