diff options
author | Alexander Motin <mav@FreeBSD.org> | 2010-09-13 07:25:35 +0000 |
---|---|---|
committer | Alexander Motin <mav@FreeBSD.org> | 2010-09-13 07:25:35 +0000 |
commit | a157e42516dcee534177e5e0dc59815c3334d647 (patch) | |
tree | b0666da99693a46e1663a3a882abfdee5e324830 /sys/kern/kern_clock.c | |
parent | 3c7f49dcf28dcb78fd219f420504e50518ccea48 (diff) | |
download | src-a157e42516dcee534177e5e0dc59815c3334d647.tar.gz src-a157e42516dcee534177e5e0dc59815c3334d647.zip |
Refactor timer management code with priority to one-shot operation mode.
The main goal of this is to generate timer interrupts only when there is
some work to do. When CPU is busy interrupts are generating at full rate
of hz + stathz to fullfill scheduler and timekeeping requirements. But
when CPU is idle, only minimum set of interrupts (down to 8 interrupts per
second per CPU now), needed to handle scheduled callouts is executed.
This allows significantly increase idle CPU sleep time, increasing effect
of static power-saving technologies. Also it should reduce host CPU load
on virtualized systems, when guest system is idle.
There is set of tunables, also available as writable sysctls, allowing to
control wanted event timer subsystem behavior:
kern.eventtimer.timer - allows to choose event timer hardware to use.
On x86 there is up to 4 different kinds of timers. Depending on whether
chosen timer is per-CPU, behavior of other options slightly differs.
kern.eventtimer.periodic - allows to choose periodic and one-shot
operation mode. In periodic mode, current timer hardware taken as the only
source of time for time events. This mode is quite alike to previous kernel
behavior. One-shot mode instead uses currently selected time counter
hardware to schedule all needed events one by one and program timer to
generate interrupt exactly in specified time. Default value depends of
chosen timer capabilities, but one-shot mode is preferred, until other is
forced by user or hardware.
kern.eventtimer.singlemul - in periodic mode specifies how much times
higher timer frequency should be, to not strictly alias hardclock() and
statclock() events. Default values are 2 and 4, but could be reduced to 1
if extra interrupts are unwanted.
kern.eventtimer.idletick - makes each CPU to receive every timer interrupt
independently of whether they busy or not. By default this options is
disabled. If chosen timer is per-CPU and runs in periodic mode, this option
has no effect - all interrupts are generating.
As soon as this patch modifies cpu_idle() on some platforms, I have also
refactored one on x86. Now it makes use of MONITOR/MWAIT instrunctions
(if supported) under high sleep/wakeup rate, as fast alternative to other
methods. It allows SMP scheduler to wake up sleeping CPUs much faster
without using IPI, significantly increasing performance on some highly
task-switching loads.
Tested by: many (on i386, amd64, sparc64 and powerc)
H/W donated by: Gheorghe Ardelean
Sponsored by: iXsystems, Inc.
Notes
Notes:
svn path=/head/; revision=212541
Diffstat (limited to 'sys/kern/kern_clock.c')
-rw-r--r-- | sys/kern/kern_clock.c | 140 |
1 files changed, 88 insertions, 52 deletions
diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index c283b6b1e6a7..ff5747e104c7 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -373,11 +373,9 @@ int profprocs; int ticks; int psratio; -int timer1hz; -int timer2hz; -static DPCPU_DEFINE(u_int, hard_cnt); -static DPCPU_DEFINE(u_int, stat_cnt); -static DPCPU_DEFINE(u_int, prof_cnt); +static DPCPU_DEFINE(int, pcputicks); /* Per-CPU version of ticks. */ +static struct mtx global_hardclock_mtx; +MTX_SYSINIT(global_hardclock_mtx, &global_hardclock_mtx, "ghc_mtx", MTX_SPIN); /* * Initialize clock frequencies and start both clocks running. @@ -408,52 +406,6 @@ initclocks(dummy) #endif } -void -timer1clock(int usermode, uintfptr_t pc) -{ - u_int *cnt; - - cnt = DPCPU_PTR(hard_cnt); - *cnt += hz; - if (*cnt >= timer1hz) { - *cnt -= timer1hz; - if (*cnt >= timer1hz) - *cnt = 0; - if (PCPU_GET(cpuid) == 0) - hardclock(usermode, pc); - else - hardclock_cpu(usermode); - } - if (timer2hz == 0) - timer2clock(usermode, pc); -} - -void -timer2clock(int usermode, uintfptr_t pc) -{ - u_int *cnt; - int t2hz = timer2hz ? timer2hz : timer1hz; - - cnt = DPCPU_PTR(stat_cnt); - *cnt += stathz; - if (*cnt >= t2hz) { - *cnt -= t2hz; - if (*cnt >= t2hz) - *cnt = 0; - statclock(usermode); - } - if (profprocs == 0) - return; - cnt = DPCPU_PTR(prof_cnt); - *cnt += profhz; - if (*cnt >= t2hz) { - *cnt -= t2hz; - if (*cnt >= t2hz) - *cnt = 0; - profclock(usermode, pc); - } -} - /* * Each time the real-time timer fires, this function is called on all CPUs. * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only @@ -486,7 +438,7 @@ hardclock_cpu(int usermode) PROC_SUNLOCK(p); } thread_lock(td); - sched_tick(); + sched_tick(1); td->td_flags |= flags; thread_unlock(td); @@ -507,6 +459,7 @@ hardclock(int usermode, uintfptr_t pc) atomic_add_int((volatile int *)&ticks, 1); hardclock_cpu(usermode); tc_ticktock(); + cpu_tick_calibration(); /* * If no separate statistics clock is available, run it from here. * @@ -525,6 +478,89 @@ hardclock(int usermode, uintfptr_t pc) #endif /* SW_WATCHDOG */ } +void +hardclock_anycpu(int cnt, int usermode) +{ + struct pstats *pstats; + struct thread *td = curthread; + struct proc *p = td->td_proc; + int *t = DPCPU_PTR(pcputicks); + int flags; + int global, newticks; + + /* + * Update per-CPU and possibly global ticks values. + */ + *t += cnt; + do { + global = ticks; + newticks = *t - global; + if (newticks <= 0) { + if (newticks < -1) + *t = global - 1; + newticks = 0; + break; + } + } while (!atomic_cmpset_int(&ticks, global, *t)); + + /* + * Run current process's virtual and profile time, as needed. + */ + pstats = p->p_stats; + flags = 0; + if (usermode && + timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) { + PROC_SLOCK(p); + if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], + tick * cnt) == 0) + flags |= TDF_ALRMPEND | TDF_ASTPENDING; + PROC_SUNLOCK(p); + } + if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) { + PROC_SLOCK(p); + if (itimerdecr(&pstats->p_timer[ITIMER_PROF], + tick * cnt) == 0) + flags |= TDF_PROFPEND | TDF_ASTPENDING; + PROC_SUNLOCK(p); + } + thread_lock(td); + sched_tick(cnt); + td->td_flags |= flags; + thread_unlock(td); + +#ifdef HWPMC_HOOKS + if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid))) + PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL); +#endif + callout_tick(); + /* We are in charge to handle this tick duty. */ + if (newticks > 0) { + mtx_lock_spin(&global_hardclock_mtx); + tc_ticktock(); +#ifdef DEVICE_POLLING + hardclock_device_poll(); /* This is very short and quick. */ +#endif /* DEVICE_POLLING */ +#ifdef SW_WATCHDOG + if (watchdog_enabled > 0) { + watchdog_ticks -= newticks; + if (watchdog_ticks <= 0) + watchdog_fire(); + } +#endif /* SW_WATCHDOG */ + mtx_unlock_spin(&global_hardclock_mtx); + } + if (curcpu == CPU_FIRST()) + cpu_tick_calibration(); +} + +void +hardclock_sync(int cpu) +{ + int *t = DPCPU_ID_PTR(cpu, pcputicks); + + *t = ticks; +} + /* * Compute number of ticks in the specified amount of time. */ |