Deleted Added
full compact
kern_clock.c (209761) kern_clock.c (212541)
1/*-
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
35 */
36
37#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/kern_clock.c 209761 2010-07-07 12:00:11Z attilio $");
38__FBSDID("$FreeBSD: head/sys/kern/kern_clock.c 212541 2010-09-13 07:25:35Z mav $");
39
40#include "opt_kdb.h"
41#include "opt_device_polling.h"
42#include "opt_hwpmc_hooks.h"
43#include "opt_ntp.h"
44#include "opt_watchdog.h"
45
46#include <sys/param.h>

--- 321 unchanged lines hidden (view full) ---

368 */
369
370int stathz;
371int profhz;
372int profprocs;
373int ticks;
374int psratio;
375
39
40#include "opt_kdb.h"
41#include "opt_device_polling.h"
42#include "opt_hwpmc_hooks.h"
43#include "opt_ntp.h"
44#include "opt_watchdog.h"
45
46#include <sys/param.h>

--- 321 unchanged lines hidden (view full) ---

368 */
369
370int stathz;
371int profhz;
372int profprocs;
373int ticks;
374int psratio;
375
376int timer1hz;
377int timer2hz;
378static DPCPU_DEFINE(u_int, hard_cnt);
379static DPCPU_DEFINE(u_int, stat_cnt);
380static DPCPU_DEFINE(u_int, prof_cnt);
376static DPCPU_DEFINE(int, pcputicks); /* Per-CPU version of ticks. */
377static struct mtx global_hardclock_mtx;
378MTX_SYSINIT(global_hardclock_mtx, &global_hardclock_mtx, "ghc_mtx", MTX_SPIN);
381
382/*
383 * Initialize clock frequencies and start both clocks running.
384 */
385/* ARGSUSED*/
386static void
387initclocks(dummy)
388 void *dummy;

--- 14 unchanged lines hidden (view full) ---

403 if (profhz == 0)
404 profhz = i;
405 psratio = profhz / i;
406#ifdef SW_WATCHDOG
407 EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
408#endif
409}
410
379
380/*
381 * Initialize clock frequencies and start both clocks running.
382 */
383/* ARGSUSED*/
384static void
385initclocks(dummy)
386 void *dummy;

--- 14 unchanged lines hidden (view full) ---

401 if (profhz == 0)
402 profhz = i;
403 psratio = profhz / i;
404#ifdef SW_WATCHDOG
405 EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
406#endif
407}
408
411void
412timer1clock(int usermode, uintfptr_t pc)
413{
414 u_int *cnt;
415
416 cnt = DPCPU_PTR(hard_cnt);
417 *cnt += hz;
418 if (*cnt >= timer1hz) {
419 *cnt -= timer1hz;
420 if (*cnt >= timer1hz)
421 *cnt = 0;
422 if (PCPU_GET(cpuid) == 0)
423 hardclock(usermode, pc);
424 else
425 hardclock_cpu(usermode);
426 }
427 if (timer2hz == 0)
428 timer2clock(usermode, pc);
429}
430
431void
432timer2clock(int usermode, uintfptr_t pc)
433{
434 u_int *cnt;
435 int t2hz = timer2hz ? timer2hz : timer1hz;
436
437 cnt = DPCPU_PTR(stat_cnt);
438 *cnt += stathz;
439 if (*cnt >= t2hz) {
440 *cnt -= t2hz;
441 if (*cnt >= t2hz)
442 *cnt = 0;
443 statclock(usermode);
444 }
445 if (profprocs == 0)
446 return;
447 cnt = DPCPU_PTR(prof_cnt);
448 *cnt += profhz;
449 if (*cnt >= t2hz) {
450 *cnt -= t2hz;
451 if (*cnt >= t2hz)
452 *cnt = 0;
453 profclock(usermode, pc);
454 }
455}
456
457/*
458 * Each time the real-time timer fires, this function is called on all CPUs.
459 * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only
460 * the other CPUs in the system need to call this function.
461 */
462void
463hardclock_cpu(int usermode)
464{

--- 16 unchanged lines hidden (view full) ---

481 }
482 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
483 PROC_SLOCK(p);
484 if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
485 flags |= TDF_PROFPEND | TDF_ASTPENDING;
486 PROC_SUNLOCK(p);
487 }
488 thread_lock(td);
409/*
410 * Each time the real-time timer fires, this function is called on all CPUs.
411 * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only
412 * the other CPUs in the system need to call this function.
413 */
414void
415hardclock_cpu(int usermode)
416{

--- 16 unchanged lines hidden (view full) ---

433 }
434 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
435 PROC_SLOCK(p);
436 if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
437 flags |= TDF_PROFPEND | TDF_ASTPENDING;
438 PROC_SUNLOCK(p);
439 }
440 thread_lock(td);
489 sched_tick();
441 sched_tick(1);
490 td->td_flags |= flags;
491 thread_unlock(td);
492
493#ifdef HWPMC_HOOKS
494 if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
495 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
496#endif
497 callout_tick();

--- 4 unchanged lines hidden (view full) ---

502 */
503void
504hardclock(int usermode, uintfptr_t pc)
505{
506
507 atomic_add_int((volatile int *)&ticks, 1);
508 hardclock_cpu(usermode);
509 tc_ticktock();
442 td->td_flags |= flags;
443 thread_unlock(td);
444
445#ifdef HWPMC_HOOKS
446 if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
447 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
448#endif
449 callout_tick();

--- 4 unchanged lines hidden (view full) ---

454 */
455void
456hardclock(int usermode, uintfptr_t pc)
457{
458
459 atomic_add_int((volatile int *)&ticks, 1);
460 hardclock_cpu(usermode);
461 tc_ticktock();
462 cpu_tick_calibration();
510 /*
511 * If no separate statistics clock is available, run it from here.
512 *
513 * XXX: this only works for UP
514 */
515 if (stathz == 0) {
516 profclock(usermode, pc);
517 statclock(usermode);
518 }
519#ifdef DEVICE_POLLING
520 hardclock_device_poll(); /* this is very short and quick */
521#endif /* DEVICE_POLLING */
522#ifdef SW_WATCHDOG
523 if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
524 watchdog_fire();
525#endif /* SW_WATCHDOG */
526}
527
463 /*
464 * If no separate statistics clock is available, run it from here.
465 *
466 * XXX: this only works for UP
467 */
468 if (stathz == 0) {
469 profclock(usermode, pc);
470 statclock(usermode);
471 }
472#ifdef DEVICE_POLLING
473 hardclock_device_poll(); /* this is very short and quick */
474#endif /* DEVICE_POLLING */
475#ifdef SW_WATCHDOG
476 if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
477 watchdog_fire();
478#endif /* SW_WATCHDOG */
479}
480
481void
482hardclock_anycpu(int cnt, int usermode)
483{
484 struct pstats *pstats;
485 struct thread *td = curthread;
486 struct proc *p = td->td_proc;
487 int *t = DPCPU_PTR(pcputicks);
488 int flags;
489 int global, newticks;
490
491 /*
492 * Update per-CPU and possibly global ticks values.
493 */
494 *t += cnt;
495 do {
496 global = ticks;
497 newticks = *t - global;
498 if (newticks <= 0) {
499 if (newticks < -1)
500 *t = global - 1;
501 newticks = 0;
502 break;
503 }
504 } while (!atomic_cmpset_int(&ticks, global, *t));
505
506 /*
507 * Run current process's virtual and profile time, as needed.
508 */
509 pstats = p->p_stats;
510 flags = 0;
511 if (usermode &&
512 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
513 PROC_SLOCK(p);
514 if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL],
515 tick * cnt) == 0)
516 flags |= TDF_ALRMPEND | TDF_ASTPENDING;
517 PROC_SUNLOCK(p);
518 }
519 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
520 PROC_SLOCK(p);
521 if (itimerdecr(&pstats->p_timer[ITIMER_PROF],
522 tick * cnt) == 0)
523 flags |= TDF_PROFPEND | TDF_ASTPENDING;
524 PROC_SUNLOCK(p);
525 }
526 thread_lock(td);
527 sched_tick(cnt);
528 td->td_flags |= flags;
529 thread_unlock(td);
530
531#ifdef HWPMC_HOOKS
532 if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
533 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
534#endif
535 callout_tick();
536 /* We are in charge to handle this tick duty. */
537 if (newticks > 0) {
538 mtx_lock_spin(&global_hardclock_mtx);
539 tc_ticktock();
540#ifdef DEVICE_POLLING
541 hardclock_device_poll(); /* This is very short and quick. */
542#endif /* DEVICE_POLLING */
543#ifdef SW_WATCHDOG
544 if (watchdog_enabled > 0) {
545 watchdog_ticks -= newticks;
546 if (watchdog_ticks <= 0)
547 watchdog_fire();
548 }
549#endif /* SW_WATCHDOG */
550 mtx_unlock_spin(&global_hardclock_mtx);
551 }
552 if (curcpu == CPU_FIRST())
553 cpu_tick_calibration();
554}
555
556void
557hardclock_sync(int cpu)
558{
559 int *t = DPCPU_ID_PTR(cpu, pcputicks);
560
561 *t = ticks;
562}
563
528/*
529 * Compute number of ticks in the specified amount of time.
530 */
531int
532tvtohz(tv)
533 struct timeval *tv;
534{
535 register unsigned long ticks;

--- 285 unchanged lines hidden ---
564/*
565 * Compute number of ticks in the specified amount of time.
566 */
567int
568tvtohz(tv)
569 struct timeval *tv;
570{
571 register unsigned long ticks;

--- 285 unchanged lines hidden ---