2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 #include "opt_device_polling.h"
40 #include "opt_hwpmc_hooks.h"
42 #include "opt_watchdog.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/epoch.h>
48 #include <sys/eventhandler.h>
49 #include <sys/gtaskqueue.h>
51 #include <sys/kernel.h>
52 #include <sys/kthread.h>
55 #include <sys/mutex.h>
57 #include <sys/resource.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sched.h>
61 #include <sys/signalvar.h>
62 #include <sys/sleepqueue.h>
66 #include <vm/vm_map.h>
67 #include <sys/sysctl.h>
69 #include <sys/interrupt.h>
70 #include <sys/limits.h>
71 #include <sys/timetc.h>
74 #include <sys/pmckern.h>
75 PMC_SOFT_DEFINE( , , clock, hard);
76 PMC_SOFT_DEFINE( , , clock, stat);
77 PMC_SOFT_DEFINE_EX( , , clock, prof, \
78 cpu_startprofclock, cpu_stopprofclock);
82 extern void hardclock_device_poll(void);
83 #endif /* DEVICE_POLLING */
85 /* Spin-lock protecting profiling statistics. */
86 static struct mtx time_lock;
88 SDT_PROVIDER_DECLARE(sched);
89 SDT_PROBE_DEFINE2(sched, , , tick, "struct thread *", "struct proc *");
92 sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
95 long cp_time[CPUSTATES];
98 unsigned int cp_time32[CPUSTATES];
101 read_cpu_time(cp_time);
103 if (req->flags & SCTL_MASK32) {
105 return SYSCTL_OUT(req, 0, sizeof(cp_time32));
106 for (i = 0; i < CPUSTATES; i++)
107 cp_time32[i] = (unsigned int)cp_time[i];
108 error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
113 return SYSCTL_OUT(req, 0, sizeof(cp_time));
114 error = SYSCTL_OUT(req, cp_time, sizeof(cp_time));
119 SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
120 0,0, sysctl_kern_cp_time, "LU", "CPU time statistics");
122 static long empty[CPUSTATES];
125 sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS)
132 unsigned int cp_time32[CPUSTATES];
138 if (req->flags & SCTL_MASK32)
139 return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1));
142 return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1));
144 for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) {
145 if (!CPU_ABSENT(c)) {
147 cp_time = pcpu->pc_cp_time;
152 if (req->flags & SCTL_MASK32) {
153 for (i = 0; i < CPUSTATES; i++)
154 cp_time32[i] = (unsigned int)cp_time[i];
155 error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
158 error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES);
163 SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
164 0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics");
167 static const char *blessed[] = {
173 static int slptime_threshold = 1800;
174 static int blktime_threshold = 900;
175 static int sleepfreq = 3;
178 deadlres_td_on_lock(struct proc *p, struct thread *td, int blkticks)
182 sx_assert(&allproc_lock, SX_LOCKED);
183 PROC_LOCK_ASSERT(p, MA_OWNED);
184 THREAD_LOCK_ASSERT(td, MA_OWNED);
186 * The thread should be blocked on a turnstile, simply check
187 * if the turnstile channel is in good state.
189 MPASS(td->td_blocked != NULL);
191 tticks = ticks - td->td_blktick;
192 if (tticks > blkticks)
194 * Accordingly with provided thresholds, this thread is stuck
195 * for too long on a turnstile.
197 panic("%s: possible deadlock detected for %p (%s), "
198 "blocked for %d ticks\n", __func__,
199 td, sched_tdname(td), tticks);
203 deadlres_td_sleep_q(struct proc *p, struct thread *td, int slpticks)
206 int i, slptype, tticks;
208 sx_assert(&allproc_lock, SX_LOCKED);
209 PROC_LOCK_ASSERT(p, MA_OWNED);
210 THREAD_LOCK_ASSERT(td, MA_OWNED);
212 * Check if the thread is sleeping on a lock, otherwise skip the check.
213 * Drop the thread lock in order to avoid a LOR with the sleepqueue
216 wchan = td->td_wchan;
217 tticks = ticks - td->td_slptick;
218 slptype = sleepq_type(wchan);
219 if ((slptype == SLEEPQ_SX || slptype == SLEEPQ_LK) &&
222 * Accordingly with provided thresholds, this thread is stuck
223 * for too long on a sleepqueue.
224 * However, being on a sleepqueue, we might still check for the
227 for (i = 0; blessed[i] != NULL; i++)
228 if (!strcmp(blessed[i], td->td_wmesg))
231 panic("%s: possible deadlock detected for %p (%s), "
232 "blocked for %d ticks\n", __func__,
233 td, sched_tdname(td), tticks);
242 int blkticks, slpticks, tryl;
246 blkticks = blktime_threshold * hz;
247 slpticks = slptime_threshold * hz;
250 * Avoid to sleep on the sx_lock in order to avoid a
251 * possible priority inversion problem leading to
253 * If the lock can't be held after 100 tries, panic.
255 if (!sx_try_slock(&allproc_lock)) {
257 panic("%s: possible deadlock detected "
258 "on allproc_lock\n", __func__);
260 pause("allproc", sleepfreq * hz);
264 FOREACH_PROC_IN_SYSTEM(p) {
266 if (p->p_state == PRS_NEW) {
270 FOREACH_THREAD_IN_PROC(p, td) {
273 deadlres_td_on_lock(p, td,
275 else if (TD_IS_SLEEPING(td))
276 deadlres_td_sleep_q(p, td,
282 sx_sunlock(&allproc_lock);
284 /* Sleep for sleepfreq seconds. */
285 pause("-", sleepfreq * hz);
289 static struct kthread_desc deadlkres_kd = {
292 (struct thread **)NULL
295 SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd);
297 static SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
298 "Deadlock resolver");
299 SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RWTUN,
300 &slptime_threshold, 0,
301 "Number of seconds within is valid to sleep on a sleepqueue");
302 SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RWTUN,
303 &blktime_threshold, 0,
304 "Number of seconds within is valid to block on a turnstile");
305 SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RWTUN, &sleepfreq, 0,
306 "Number of seconds between any deadlock resolver thread run");
307 #endif /* DEADLKRES */
310 read_cpu_time(long *cp_time)
315 /* Sum up global cp_time[]. */
316 bzero(cp_time, sizeof(long) * CPUSTATES);
319 for (j = 0; j < CPUSTATES; j++)
320 cp_time[j] += pc->pc_cp_time[j];
324 #include <sys/watchdog.h>
326 static int watchdog_ticks;
327 static int watchdog_enabled;
328 static void watchdog_fire(void);
329 static void watchdog_config(void *, u_int, int *);
332 watchdog_attach(void)
334 EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
338 * Clock handling routines.
340 * This code is written to operate with two timers that run independently of
343 * The main timer, running hz times per second, is used to trigger interval
344 * timers, timeouts and rescheduling as needed.
346 * The second timer handles kernel and user profiling,
347 * and does resource use estimation. If the second timer is programmable,
348 * it is randomized to avoid aliasing between the two clocks. For example,
349 * the randomization prevents an adversary from always giving up the cpu
350 * just before its quantum expires. Otherwise, it would never accumulate
351 * cpu ticks. The mean frequency of the second timer is stathz.
353 * If no second timer exists, stathz will be zero; in this case we drive
354 * profiling and statistics off the main clock. This WILL NOT be accurate;
355 * do not do it unless absolutely necessary.
357 * The statistics clock may (or may not) be run at a higher rate while
358 * profiling. This profile clock runs at profhz. We require that profhz
359 * be an integral multiple of stathz.
361 * If the statistics clock is running fast, it must be divided by the ratio
362 * profhz/stathz for statistics. (For profiling, every tick counts.)
364 * Time-of-day is maintained using a "timecounter", which may or may
365 * not be related to the hardware generating the above mentioned
375 DPCPU_DEFINE_STATIC(int, pcputicks); /* Per-CPU version of ticks. */
376 #ifdef DEVICE_POLLING
377 static int devpoll_run = 0;
381 ast_oweupc(struct thread *td, int tda __unused)
383 if ((td->td_proc->p_flag & P_PROFIL) == 0)
385 addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
386 td->td_profil_ticks = 0;
387 td->td_pflags &= ~TDP_OWEUPC;
391 ast_alrm(struct thread *td, int tda __unused)
397 kern_psignal(p, SIGVTALRM);
402 ast_prof(struct thread *td, int tda __unused)
408 kern_psignal(p, SIGPROF);
413 * Initialize clock frequencies and start both clocks running.
416 initclocks(void *dummy __unused)
421 * Set divisors to 1 (normal case) and let the machine-specific
424 mtx_init(&time_lock, "time lock", NULL, MTX_DEF);
428 * Compute profhz/stathz, and fix profhz if needed.
430 i = stathz ? stathz : hz;
433 psratio = profhz / i;
435 ast_register(TDA_OWEUPC, ASTR_ASTF_REQUIRED, 0, ast_oweupc);
436 ast_register(TDA_ALRM, ASTR_ASTF_REQUIRED, 0, ast_alrm);
437 ast_register(TDA_PROF, ASTR_ASTF_REQUIRED, 0, ast_prof);
440 /* Enable hardclock watchdog now, even if a hardware watchdog exists. */
443 /* Volunteer to run a software watchdog. */
444 if (wdog_software_attach == NULL)
445 wdog_software_attach = watchdog_attach;
448 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
450 static __noinline void
451 hardclock_itimer(struct thread *td, struct pstats *pstats, int cnt, int usermode)
459 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
461 if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL],
463 ast |= TDAI(TDA_ALRM);
466 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
468 if (itimerdecr(&pstats->p_timer[ITIMER_PROF],
470 ast |= TDAI(TDA_PROF);
474 ast_sched_mask(td, ast);
478 hardclock(int cnt, int usermode)
480 struct pstats *pstats;
481 struct thread *td = curthread;
482 struct proc *p = td->td_proc;
483 int *t = DPCPU_PTR(pcputicks);
484 int global, i, newticks;
487 * Update per-CPU and possibly global ticks values.
492 newticks = *t - global;
499 } while (!atomic_fcmpset_int(&ticks, &global, *t));
502 * Run current process's virtual and profile time, as needed.
506 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) ||
507 timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)))
508 hardclock_itimer(td, pstats, cnt, usermode);
511 if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
512 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
513 if (td->td_intr_frame != NULL)
514 PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame);
516 /* We are in charge to handle this tick duty. */
518 tc_ticktock(newticks);
519 #ifdef DEVICE_POLLING
520 /* Dangerous and no need to call these things concurrently. */
521 if (atomic_cmpset_acq_int(&devpoll_run, 0, 1)) {
522 /* This is very short and quick. */
523 hardclock_device_poll();
524 atomic_store_rel_int(&devpoll_run, 0);
526 #endif /* DEVICE_POLLING */
527 if (watchdog_enabled > 0) {
528 i = atomic_fetchadd_int(&watchdog_ticks, -newticks);
529 if (i > 0 && i <= newticks)
532 intr_event_handle(clk_intr_event, NULL);
534 if (curcpu == CPU_FIRST())
535 cpu_tick_calibration();
536 if (__predict_false(DPCPU_GET(epoch_cb_count)))
537 GROUPTASK_ENQUEUE(DPCPU_PTR(epoch_cb_task));
541 hardclock_sync(int cpu)
544 KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu));
545 t = DPCPU_ID_PTR(cpu, pcputicks);
551 * Regular integer scaling formula without losing precision:
553 #define TIME_INT_SCALE(value, mul, div) \
554 (((value) / (div)) * (mul) + (((value) % (div)) * (mul)) / (div))
557 * Macro for converting seconds and microseconds into actual ticks,
558 * based on the given hz value:
560 #define TIME_TO_TICKS(sec, usec, hz) \
561 ((sec) * (hz) + TIME_INT_SCALE(usec, hz, 1 << 6) / (1000000 >> 6))
563 #define TIME_ASSERT_VALID_HZ(hz) \
564 _Static_assert(TIME_TO_TICKS(INT_MAX / (hz) - 1, 999999, hz) >= 0 && \
565 TIME_TO_TICKS(INT_MAX / (hz) - 1, 999999, hz) < INT_MAX, \
566 "tvtohz() can overflow the regular integer type")
569 * Compile time assert the maximum and minimum values to fit into a
570 * regular integer when computing TIME_TO_TICKS():
572 TIME_ASSERT_VALID_HZ(HZ_MAXIMUM);
573 TIME_ASSERT_VALID_HZ(HZ_MINIMUM);
576 * The formula is mostly linear, but test some more common values just
579 TIME_ASSERT_VALID_HZ(1024);
580 TIME_ASSERT_VALID_HZ(1000);
581 TIME_ASSERT_VALID_HZ(128);
582 TIME_ASSERT_VALID_HZ(100);
585 * Compute number of ticks representing the specified amount of time.
586 * If the specified time is negative, a value of 1 is returned. This
587 * function returns a value from 1 up to and including INT_MAX.
590 tvtohz(struct timeval *tv)
595 * The values passed here may come from user-space and these
596 * checks ensure "tv_usec" is within its allowed range:
599 /* check for tv_usec underflow */
600 if (__predict_false(tv->tv_usec < 0)) {
601 tv->tv_sec += tv->tv_usec / 1000000;
602 tv->tv_usec = tv->tv_usec % 1000000;
603 /* convert tv_usec to a positive value */
604 if (__predict_true(tv->tv_usec < 0)) {
605 tv->tv_usec += 1000000;
608 /* check for tv_usec overflow */
609 } else if (__predict_false(tv->tv_usec >= 1000000)) {
610 tv->tv_sec += tv->tv_usec / 1000000;
611 tv->tv_usec = tv->tv_usec % 1000000;
614 /* check for tv_sec underflow */
615 if (__predict_false(tv->tv_sec < 0))
617 /* check for tv_sec overflow (including room for the tv_usec part) */
618 else if (__predict_false(tv->tv_sec >= tick_seconds_max))
621 /* cast to "int" to avoid platform differences */
622 retval = TIME_TO_TICKS((int)tv->tv_sec, (int)tv->tv_usec, hz);
624 /* add one additional tick */
629 * Start profiling on a process.
631 * Kernel profiling passes proc0 which never exits and hence
632 * keeps the profile clock running constantly.
635 startprofclock(struct proc *p)
638 PROC_LOCK_ASSERT(p, MA_OWNED);
639 if (p->p_flag & P_STOPPROF)
641 if ((p->p_flag & P_PROFIL) == 0) {
642 p->p_flag |= P_PROFIL;
643 mtx_lock(&time_lock);
644 if (++profprocs == 1)
645 cpu_startprofclock();
646 mtx_unlock(&time_lock);
651 * Stop profiling on a process.
654 stopprofclock(struct proc *p)
657 PROC_LOCK_ASSERT(p, MA_OWNED);
658 if (p->p_flag & P_PROFIL) {
659 if (p->p_profthreads != 0) {
660 while (p->p_profthreads != 0) {
661 p->p_flag |= P_STOPPROF;
662 msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
666 if ((p->p_flag & P_PROFIL) == 0)
668 p->p_flag &= ~P_PROFIL;
669 mtx_lock(&time_lock);
670 if (--profprocs == 0)
672 mtx_unlock(&time_lock);
677 * Statistics clock. Updates rusage information and calls the scheduler
678 * to adjust priorities of the active thread.
680 * This should be called by all active processors.
683 statclock(int cnt, int usermode)
691 uint64_t runtime, new_switchtime;
696 cp_time = (long *)PCPU_PTR(cp_time);
699 * Charge the time as appropriate.
701 td->td_uticks += cnt;
702 if (p->p_nice > NZERO)
703 cp_time[CP_NICE] += cnt;
705 cp_time[CP_USER] += cnt;
708 * Came from kernel mode, so we were:
709 * - handling an interrupt,
710 * - doing syscall or trap work on behalf of the current
712 * - spinning in the idle loop.
713 * Whichever it is, charge the time as appropriate.
714 * Note that we charge interrupts to the current process,
715 * regardless of whether they are ``for'' that process,
716 * so that we know how much of its real time was spent
717 * in ``non-process'' (i.e., interrupt) work.
719 if ((td->td_pflags & TDP_ITHREAD) ||
720 td->td_intr_nesting_level >= 2) {
721 td->td_iticks += cnt;
722 cp_time[CP_INTR] += cnt;
724 td->td_pticks += cnt;
725 td->td_sticks += cnt;
726 if (!TD_IS_IDLETHREAD(td))
727 cp_time[CP_SYS] += cnt;
729 cp_time[CP_IDLE] += cnt;
733 /* Update resource usage integrals and maximums. */
734 MPASS(p->p_vmspace != NULL);
737 ru->ru_ixrss += pgtok(vm->vm_tsize) * cnt;
738 ru->ru_idrss += pgtok(vm->vm_dsize) * cnt;
739 ru->ru_isrss += pgtok(vm->vm_ssize) * cnt;
740 rss = pgtok(vmspace_resident_count(vm));
741 if (ru->ru_maxrss < rss)
743 KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock",
744 "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz);
745 SDT_PROBE2(sched, , , tick, td, td->td_proc);
746 thread_lock_flags(td, MTX_QUIET);
749 * Compute the amount of time during which the current
750 * thread was running, and add that to its total so far.
752 new_switchtime = cpu_ticks();
753 runtime = new_switchtime - PCPU_GET(switchtime);
754 td->td_runtime += runtime;
755 td->td_incruntime += runtime;
756 PCPU_SET(switchtime, new_switchtime);
758 sched_clock(td, cnt);
761 if (td->td_intr_frame != NULL)
762 PMC_SOFT_CALL_TF( , , clock, stat, td->td_intr_frame);
767 profclock(int cnt, int usermode, uintfptr_t pc)
774 * Came from user mode; CPU was in user state.
775 * If this process is being profiled, record the tick.
776 * if there is no related user location yet, don't
777 * bother trying to count it.
779 if (td->td_proc->p_flag & P_PROFIL)
780 addupc_intr(td, pc, cnt);
783 if (td->td_intr_frame != NULL)
784 PMC_SOFT_CALL_TF( , , clock, prof, td->td_intr_frame);
789 * Return information about system clocks.
792 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
794 struct clockinfo clkinfo;
796 * Construct clockinfo structure.
798 bzero(&clkinfo, sizeof(clkinfo));
801 clkinfo.profhz = profhz;
802 clkinfo.stathz = stathz ? stathz : hz;
803 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
806 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate,
807 CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE,
808 0, 0, sysctl_kern_clockrate, "S,clockinfo",
809 "Rate and period of various kernel clocks");
812 watchdog_config(void *unused __unused, u_int cmd, int *error)
816 u = cmd & WD_INTERVAL;
817 if (u >= WD_TO_1SEC) {
818 watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
819 watchdog_enabled = 1;
822 watchdog_enabled = 0;
827 * Handle a watchdog timeout by dropping to DDB or panicking.
833 #if defined(KDB) && !defined(KDB_UNATTENDED)
835 kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
837 panic("watchdog timeout");