The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_clock.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   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$");
   39 
   40 #include "opt_kdb.h"
   41 #include "opt_device_polling.h"
   42 #include "opt_hwpmc_hooks.h"
   43 #include "opt_kdtrace.h"
   44 #include "opt_ntp.h"
   45 #include "opt_watchdog.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/callout.h>
   50 #include <sys/kdb.h>
   51 #include <sys/kernel.h>
   52 #include <sys/kthread.h>
   53 #include <sys/ktr.h>
   54 #include <sys/lock.h>
   55 #include <sys/mutex.h>
   56 #include <sys/proc.h>
   57 #include <sys/resource.h>
   58 #include <sys/resourcevar.h>
   59 #include <sys/sched.h>
   60 #include <sys/sdt.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/sleepqueue.h>
   63 #include <sys/smp.h>
   64 #include <vm/vm.h>
   65 #include <vm/pmap.h>
   66 #include <vm/vm_map.h>
   67 #include <sys/sysctl.h>
   68 #include <sys/bus.h>
   69 #include <sys/interrupt.h>
   70 #include <sys/limits.h>
   71 #include <sys/timetc.h>
   72 
   73 #ifdef GPROF
   74 #include <sys/gmon.h>
   75 #endif
   76 
   77 #ifdef HWPMC_HOOKS
   78 #include <sys/pmckern.h>
   79 #endif
   80 
   81 #ifdef DEVICE_POLLING
   82 extern void hardclock_device_poll(void);
   83 #endif /* DEVICE_POLLING */
   84 
   85 static void initclocks(void *dummy);
   86 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
   87 
   88 /* Spin-lock protecting profiling statistics. */
   89 static struct mtx time_lock;
   90 
   91 SDT_PROVIDER_DECLARE(sched);
   92 SDT_PROBE_DEFINE2(sched, , , tick, tick, "struct thread *", "struct proc *");
   93 
   94 static int
   95 sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
   96 {
   97         int error;
   98         long cp_time[CPUSTATES];
   99 #ifdef SCTL_MASK32
  100         int i;
  101         unsigned int cp_time32[CPUSTATES];
  102 #endif
  103 
  104         read_cpu_time(cp_time);
  105 #ifdef SCTL_MASK32
  106         if (req->flags & SCTL_MASK32) {
  107                 if (!req->oldptr)
  108                         return SYSCTL_OUT(req, 0, sizeof(cp_time32));
  109                 for (i = 0; i < CPUSTATES; i++)
  110                         cp_time32[i] = (unsigned int)cp_time[i];
  111                 error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
  112         } else
  113 #endif
  114         {
  115                 if (!req->oldptr)
  116                         return SYSCTL_OUT(req, 0, sizeof(cp_time));
  117                 error = SYSCTL_OUT(req, cp_time, sizeof(cp_time));
  118         }
  119         return error;
  120 }
  121 
  122 SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
  123     0,0, sysctl_kern_cp_time, "LU", "CPU time statistics");
  124 
  125 static long empty[CPUSTATES];
  126 
  127 static int
  128 sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS)
  129 {
  130         struct pcpu *pcpu;
  131         int error;
  132         int c;
  133         long *cp_time;
  134 #ifdef SCTL_MASK32
  135         unsigned int cp_time32[CPUSTATES];
  136         int i;
  137 #endif
  138 
  139         if (!req->oldptr) {
  140 #ifdef SCTL_MASK32
  141                 if (req->flags & SCTL_MASK32)
  142                         return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1));
  143                 else
  144 #endif
  145                         return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1));
  146         }
  147         for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) {
  148                 if (!CPU_ABSENT(c)) {
  149                         pcpu = pcpu_find(c);
  150                         cp_time = pcpu->pc_cp_time;
  151                 } else {
  152                         cp_time = empty;
  153                 }
  154 #ifdef SCTL_MASK32
  155                 if (req->flags & SCTL_MASK32) {
  156                         for (i = 0; i < CPUSTATES; i++)
  157                                 cp_time32[i] = (unsigned int)cp_time[i];
  158                         error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
  159                 } else
  160 #endif
  161                         error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES);
  162         }
  163         return error;
  164 }
  165 
  166 SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
  167     0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics");
  168 
  169 #ifdef DEADLKRES
  170 static const char *blessed[] = {
  171         "getblk",
  172         "so_snd_sx",
  173         "so_rcv_sx",
  174         NULL
  175 };
  176 static int slptime_threshold = 1800;
  177 static int blktime_threshold = 900;
  178 static int sleepfreq = 3;
  179 
  180 static void
  181 deadlkres(void)
  182 {
  183         struct proc *p;
  184         struct thread *td;
  185         void *wchan;
  186         int blkticks, i, slpticks, slptype, tryl, tticks;
  187 
  188         tryl = 0;
  189         for (;;) {
  190                 blkticks = blktime_threshold * hz;
  191                 slpticks = slptime_threshold * hz;
  192 
  193                 /*
  194                  * Avoid to sleep on the sx_lock in order to avoid a possible
  195                  * priority inversion problem leading to starvation.
  196                  * If the lock can't be held after 100 tries, panic.
  197                  */
  198                 if (!sx_try_slock(&allproc_lock)) {
  199                         if (tryl > 100)
  200                 panic("%s: possible deadlock detected on allproc_lock\n",
  201                                     __func__);
  202                         tryl++;
  203                         pause("allproc", sleepfreq * hz);
  204                         continue;
  205                 }
  206                 tryl = 0;
  207                 FOREACH_PROC_IN_SYSTEM(p) {
  208                         PROC_LOCK(p);
  209                         if (p->p_state == PRS_NEW) {
  210                                 PROC_UNLOCK(p);
  211                                 continue;
  212                         }
  213                         FOREACH_THREAD_IN_PROC(p, td) {
  214 
  215                                 /*
  216                                  * Once a thread is found in "interesting"
  217                                  * state a possible ticks wrap-up needs to be
  218                                  * checked.
  219                                  */
  220                                 thread_lock(td);
  221                                 if (TD_ON_LOCK(td) && ticks < td->td_blktick) {
  222 
  223                                         /*
  224                                          * The thread should be blocked on a
  225                                          * turnstile, simply check if the
  226                                          * turnstile channel is in good state.
  227                                          */
  228                                         MPASS(td->td_blocked != NULL);
  229 
  230                                         tticks = ticks - td->td_blktick;
  231                                         thread_unlock(td);
  232                                         if (tticks > blkticks) {
  233 
  234                                                 /*
  235                                                  * Accordingly with provided
  236                                                  * thresholds, this thread is
  237                                                  * stuck for too long on a
  238                                                  * turnstile.
  239                                                  */
  240                                                 PROC_UNLOCK(p);
  241                                                 sx_sunlock(&allproc_lock);
  242         panic("%s: possible deadlock detected for %p, blocked for %d ticks\n",
  243                                                     __func__, td, tticks);
  244                                         }
  245                                 } else if (TD_IS_SLEEPING(td) &&
  246                                     TD_ON_SLEEPQ(td) &&
  247                                     ticks < td->td_blktick) {
  248 
  249                                         /*
  250                                          * Check if the thread is sleeping on a
  251                                          * lock, otherwise skip the check.
  252                                          * Drop the thread lock in order to
  253                                          * avoid a LOR with the sleepqueue
  254                                          * spinlock.
  255                                          */
  256                                         wchan = td->td_wchan;
  257                                         tticks = ticks - td->td_slptick;
  258                                         thread_unlock(td);
  259                                         slptype = sleepq_type(wchan);
  260                                         if ((slptype == SLEEPQ_SX ||
  261                                             slptype == SLEEPQ_LK) &&
  262                                             tticks > slpticks) {
  263 
  264                                                 /*
  265                                                  * Accordingly with provided
  266                                                  * thresholds, this thread is
  267                                                  * stuck for too long on a
  268                                                  * sleepqueue.
  269                                                  * However, being on a
  270                                                  * sleepqueue, we might still
  271                                                  * check for the blessed
  272                                                  * list.
  273                                                  */
  274                                                 tryl = 0;
  275                                                 for (i = 0; blessed[i] != NULL;
  276                                                     i++) {
  277                                                         if (!strcmp(blessed[i],
  278                                                             td->td_wmesg)) {
  279                                                                 tryl = 1;
  280                                                                 break;
  281                                                         }
  282                                                 }
  283                                                 if (tryl != 0) {
  284                                                         tryl = 0;
  285                                                         continue;
  286                                                 }
  287                                                 PROC_UNLOCK(p);
  288                                                 sx_sunlock(&allproc_lock);
  289         panic("%s: possible deadlock detected for %p, blocked for %d ticks\n",
  290                                                     __func__, td, tticks);
  291                                         }
  292                                 } else
  293                                         thread_unlock(td);
  294                         }
  295                         PROC_UNLOCK(p);
  296                 }
  297                 sx_sunlock(&allproc_lock);
  298 
  299                 /* Sleep for sleepfreq seconds. */
  300                 pause("-", sleepfreq * hz);
  301         }
  302 }
  303 
  304 static struct kthread_desc deadlkres_kd = {
  305         "deadlkres",
  306         deadlkres,
  307         (struct thread **)NULL
  308 };
  309 
  310 SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd);
  311 
  312 SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW, 0, "Deadlock resolver");
  313 SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RW,
  314     &slptime_threshold, 0,
  315     "Number of seconds within is valid to sleep on a sleepqueue");
  316 SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RW,
  317     &blktime_threshold, 0,
  318     "Number of seconds within is valid to block on a turnstile");
  319 SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RW, &sleepfreq, 0,
  320     "Number of seconds between any deadlock resolver thread run");
  321 #endif  /* DEADLKRES */
  322 
  323 void
  324 read_cpu_time(long *cp_time)
  325 {
  326         struct pcpu *pc;
  327         int i, j;
  328 
  329         /* Sum up global cp_time[]. */
  330         bzero(cp_time, sizeof(long) * CPUSTATES);
  331         CPU_FOREACH(i) {
  332                 pc = pcpu_find(i);
  333                 for (j = 0; j < CPUSTATES; j++)
  334                         cp_time[j] += pc->pc_cp_time[j];
  335         }
  336 }
  337 
  338 #ifdef SW_WATCHDOG
  339 #include <sys/watchdog.h>
  340 
  341 static int watchdog_ticks;
  342 static int watchdog_enabled;
  343 static void watchdog_fire(void);
  344 static void watchdog_config(void *, u_int, int *);
  345 #endif /* SW_WATCHDOG */
  346 
  347 /*
  348  * Clock handling routines.
  349  *
  350  * This code is written to operate with two timers that run independently of
  351  * each other.
  352  *
  353  * The main timer, running hz times per second, is used to trigger interval
  354  * timers, timeouts and rescheduling as needed.
  355  *
  356  * The second timer handles kernel and user profiling,
  357  * and does resource use estimation.  If the second timer is programmable,
  358  * it is randomized to avoid aliasing between the two clocks.  For example,
  359  * the randomization prevents an adversary from always giving up the cpu
  360  * just before its quantum expires.  Otherwise, it would never accumulate
  361  * cpu ticks.  The mean frequency of the second timer is stathz.
  362  *
  363  * If no second timer exists, stathz will be zero; in this case we drive
  364  * profiling and statistics off the main clock.  This WILL NOT be accurate;
  365  * do not do it unless absolutely necessary.
  366  *
  367  * The statistics clock may (or may not) be run at a higher rate while
  368  * profiling.  This profile clock runs at profhz.  We require that profhz
  369  * be an integral multiple of stathz.
  370  *
  371  * If the statistics clock is running fast, it must be divided by the ratio
  372  * profhz/stathz for statistics.  (For profiling, every tick counts.)
  373  *
  374  * Time-of-day is maintained using a "timecounter", which may or may
  375  * not be related to the hardware generating the above mentioned
  376  * interrupts.
  377  */
  378 
  379 int     stathz;
  380 int     profhz;
  381 int     profprocs;
  382 volatile int    ticks;
  383 int     psratio;
  384 
  385 /*
  386  * Initialize clock frequencies and start both clocks running.
  387  */
  388 /* ARGSUSED*/
  389 static void
  390 initclocks(dummy)
  391         void *dummy;
  392 {
  393         register int i;
  394 
  395         /*
  396          * Set divisors to 1 (normal case) and let the machine-specific
  397          * code do its bit.
  398          */
  399         mtx_init(&time_lock, "time lock", NULL, MTX_SPIN);
  400         cpu_initclocks();
  401 
  402         /*
  403          * Compute profhz/stathz, and fix profhz if needed.
  404          */
  405         i = stathz ? stathz : hz;
  406         if (profhz == 0)
  407                 profhz = i;
  408         psratio = profhz / i;
  409 #ifdef SW_WATCHDOG
  410         EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
  411 #endif
  412 }
  413 
  414 /*
  415  * Each time the real-time timer fires, this function is called on all CPUs.
  416  * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only
  417  * the other CPUs in the system need to call this function.
  418  */
  419 void
  420 hardclock_cpu(int usermode)
  421 {
  422         struct pstats *pstats;
  423         struct thread *td = curthread;
  424         struct proc *p = td->td_proc;
  425         int flags;
  426 
  427         /*
  428          * Run current process's virtual and profile time, as needed.
  429          */
  430         pstats = p->p_stats;
  431         flags = 0;
  432         if (usermode &&
  433             timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
  434                 PROC_SLOCK(p);
  435                 if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
  436                         flags |= TDF_ALRMPEND | TDF_ASTPENDING;
  437                 PROC_SUNLOCK(p);
  438         }
  439         if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
  440                 PROC_SLOCK(p);
  441                 if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
  442                         flags |= TDF_PROFPEND | TDF_ASTPENDING;
  443                 PROC_SUNLOCK(p);
  444         }
  445         thread_lock(td);
  446         sched_tick();
  447         td->td_flags |= flags;
  448         thread_unlock(td);
  449 
  450 #ifdef  HWPMC_HOOKS
  451         if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
  452                 PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
  453 #endif
  454         callout_tick();
  455 }
  456 
  457 /*
  458  * The real-time timer, interrupting hz times per second.
  459  */
  460 void
  461 hardclock(int usermode, uintfptr_t pc)
  462 {
  463 
  464         atomic_add_int(&ticks, 1);
  465         hardclock_cpu(usermode);
  466         tc_ticktock();
  467         /*
  468          * If no separate statistics clock is available, run it from here.
  469          *
  470          * XXX: this only works for UP
  471          */
  472         if (stathz == 0) {
  473                 profclock(usermode, pc);
  474                 statclock(usermode);
  475         }
  476 #ifdef DEVICE_POLLING
  477         hardclock_device_poll();        /* this is very short and quick */
  478 #endif /* DEVICE_POLLING */
  479 #ifdef SW_WATCHDOG
  480         if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
  481                 watchdog_fire();
  482 #endif /* SW_WATCHDOG */
  483 }
  484 
  485 /*
  486  * Compute number of ticks in the specified amount of time.
  487  */
  488 int
  489 tvtohz(tv)
  490         struct timeval *tv;
  491 {
  492         register unsigned long ticks;
  493         register long sec, usec;
  494 
  495         /*
  496          * If the number of usecs in the whole seconds part of the time
  497          * difference fits in a long, then the total number of usecs will
  498          * fit in an unsigned long.  Compute the total and convert it to
  499          * ticks, rounding up and adding 1 to allow for the current tick
  500          * to expire.  Rounding also depends on unsigned long arithmetic
  501          * to avoid overflow.
  502          *
  503          * Otherwise, if the number of ticks in the whole seconds part of
  504          * the time difference fits in a long, then convert the parts to
  505          * ticks separately and add, using similar rounding methods and
  506          * overflow avoidance.  This method would work in the previous
  507          * case but it is slightly slower and assumes that hz is integral.
  508          *
  509          * Otherwise, round the time difference down to the maximum
  510          * representable value.
  511          *
  512          * If ints have 32 bits, then the maximum value for any timeout in
  513          * 10ms ticks is 248 days.
  514          */
  515         sec = tv->tv_sec;
  516         usec = tv->tv_usec;
  517         if (usec < 0) {
  518                 sec--;
  519                 usec += 1000000;
  520         }
  521         if (sec < 0) {
  522 #ifdef DIAGNOSTIC
  523                 if (usec > 0) {
  524                         sec++;
  525                         usec -= 1000000;
  526                 }
  527                 printf("tvotohz: negative time difference %ld sec %ld usec\n",
  528                        sec, usec);
  529 #endif
  530                 ticks = 1;
  531         } else if (sec <= LONG_MAX / 1000000)
  532                 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
  533                         / tick + 1;
  534         else if (sec <= LONG_MAX / hz)
  535                 ticks = sec * hz
  536                         + ((unsigned long)usec + (tick - 1)) / tick + 1;
  537         else
  538                 ticks = LONG_MAX;
  539         if (ticks > INT_MAX)
  540                 ticks = INT_MAX;
  541         return ((int)ticks);
  542 }
  543 
  544 /*
  545  * Start profiling on a process.
  546  *
  547  * Kernel profiling passes proc0 which never exits and hence
  548  * keeps the profile clock running constantly.
  549  */
  550 void
  551 startprofclock(p)
  552         register struct proc *p;
  553 {
  554 
  555         PROC_LOCK_ASSERT(p, MA_OWNED);
  556         if (p->p_flag & P_STOPPROF)
  557                 return;
  558         if ((p->p_flag & P_PROFIL) == 0) {
  559                 p->p_flag |= P_PROFIL;
  560                 mtx_lock_spin(&time_lock);
  561                 if (++profprocs == 1)
  562                         cpu_startprofclock();
  563                 mtx_unlock_spin(&time_lock);
  564         }
  565 }
  566 
  567 /*
  568  * Stop profiling on a process.
  569  */
  570 void
  571 stopprofclock(p)
  572         register struct proc *p;
  573 {
  574 
  575         PROC_LOCK_ASSERT(p, MA_OWNED);
  576         if (p->p_flag & P_PROFIL) {
  577                 if (p->p_profthreads != 0) {
  578                         p->p_flag |= P_STOPPROF;
  579                         while (p->p_profthreads != 0)
  580                                 msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
  581                                     "stopprof", 0);
  582                         p->p_flag &= ~P_STOPPROF;
  583                 }
  584                 if ((p->p_flag & P_PROFIL) == 0)
  585                         return;
  586                 p->p_flag &= ~P_PROFIL;
  587                 mtx_lock_spin(&time_lock);
  588                 if (--profprocs == 0)
  589                         cpu_stopprofclock();
  590                 mtx_unlock_spin(&time_lock);
  591         }
  592 }
  593 
  594 /*
  595  * Statistics clock.  Updates rusage information and calls the scheduler
  596  * to adjust priorities of the active thread.
  597  *
  598  * This should be called by all active processors.
  599  */
  600 void
  601 statclock(int usermode)
  602 {
  603         struct rusage *ru;
  604         struct vmspace *vm;
  605         struct thread *td;
  606         struct proc *p;
  607         long rss;
  608         long *cp_time;
  609 
  610         td = curthread;
  611         p = td->td_proc;
  612 
  613         cp_time = (long *)PCPU_PTR(cp_time);
  614         if (usermode) {
  615                 /*
  616                  * Charge the time as appropriate.
  617                  */
  618                 td->td_uticks++;
  619                 if (p->p_nice > NZERO)
  620                         cp_time[CP_NICE]++;
  621                 else
  622                         cp_time[CP_USER]++;
  623         } else {
  624                 /*
  625                  * Came from kernel mode, so we were:
  626                  * - handling an interrupt,
  627                  * - doing syscall or trap work on behalf of the current
  628                  *   user process, or
  629                  * - spinning in the idle loop.
  630                  * Whichever it is, charge the time as appropriate.
  631                  * Note that we charge interrupts to the current process,
  632                  * regardless of whether they are ``for'' that process,
  633                  * so that we know how much of its real time was spent
  634                  * in ``non-process'' (i.e., interrupt) work.
  635                  */
  636                 if ((td->td_pflags & TDP_ITHREAD) ||
  637                     td->td_intr_nesting_level >= 2) {
  638                         td->td_iticks++;
  639                         cp_time[CP_INTR]++;
  640                 } else {
  641                         td->td_pticks++;
  642                         td->td_sticks++;
  643                         if (!TD_IS_IDLETHREAD(td))
  644                                 cp_time[CP_SYS]++;
  645                         else
  646                                 cp_time[CP_IDLE]++;
  647                 }
  648         }
  649 
  650         /* Update resource usage integrals and maximums. */
  651         MPASS(p->p_vmspace != NULL);
  652         vm = p->p_vmspace;
  653         ru = &td->td_ru;
  654         ru->ru_ixrss += pgtok(vm->vm_tsize);
  655         ru->ru_idrss += pgtok(vm->vm_dsize);
  656         ru->ru_isrss += pgtok(vm->vm_ssize);
  657         rss = pgtok(vmspace_resident_count(vm));
  658         if (ru->ru_maxrss < rss)
  659                 ru->ru_maxrss = rss;
  660         KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock",
  661             "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz);
  662         SDT_PROBE2(sched, , , tick, td, td->td_proc);
  663         thread_lock_flags(td, MTX_QUIET);
  664         sched_clock(td);
  665         thread_unlock(td);
  666 }
  667 
  668 void
  669 profclock(int usermode, uintfptr_t pc)
  670 {
  671         struct thread *td;
  672 #ifdef GPROF
  673         struct gmonparam *g;
  674         uintfptr_t i;
  675 #endif
  676 
  677         td = curthread;
  678         if (usermode) {
  679                 /*
  680                  * Came from user mode; CPU was in user state.
  681                  * If this process is being profiled, record the tick.
  682                  * if there is no related user location yet, don't
  683                  * bother trying to count it.
  684                  */
  685                 if (td->td_proc->p_flag & P_PROFIL)
  686                         addupc_intr(td, pc, 1);
  687         }
  688 #ifdef GPROF
  689         else {
  690                 /*
  691                  * Kernel statistics are just like addupc_intr, only easier.
  692                  */
  693                 g = &_gmonparam;
  694                 if (g->state == GMON_PROF_ON && pc >= g->lowpc) {
  695                         i = PC_TO_I(g, pc);
  696                         if (i < g->textsize) {
  697                                 KCOUNT(g, i)++;
  698                         }
  699                 }
  700         }
  701 #endif
  702 }
  703 
  704 /*
  705  * Return information about system clocks.
  706  */
  707 static int
  708 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
  709 {
  710         struct clockinfo clkinfo;
  711         /*
  712          * Construct clockinfo structure.
  713          */
  714         bzero(&clkinfo, sizeof(clkinfo));
  715         clkinfo.hz = hz;
  716         clkinfo.tick = tick;
  717         clkinfo.profhz = profhz;
  718         clkinfo.stathz = stathz ? stathz : hz;
  719         return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
  720 }
  721 
  722 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate,
  723         CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE,
  724         0, 0, sysctl_kern_clockrate, "S,clockinfo",
  725         "Rate and period of various kernel clocks");
  726 
  727 #ifdef SW_WATCHDOG
  728 
  729 static void
  730 watchdog_config(void *unused __unused, u_int cmd, int *error)
  731 {
  732         u_int u;
  733 
  734         u = cmd & WD_INTERVAL;
  735         if (u >= WD_TO_1SEC) {
  736                 watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
  737                 watchdog_enabled = 1;
  738                 *error = 0;
  739         } else {
  740                 watchdog_enabled = 0;
  741         }
  742 }
  743 
  744 /*
  745  * Handle a watchdog timeout by dumping interrupt information and
  746  * then either dropping to DDB or panicking.
  747  */
  748 static void
  749 watchdog_fire(void)
  750 {
  751         int nintr;
  752         u_int64_t inttotal;
  753         u_long *curintr;
  754         char *curname;
  755 
  756         curintr = intrcnt;
  757         curname = intrnames;
  758         inttotal = 0;
  759         nintr = eintrcnt - intrcnt;
  760 
  761         printf("interrupt                   total\n");
  762         while (--nintr >= 0) {
  763                 if (*curintr)
  764                         printf("%-12s %20lu\n", curname, *curintr);
  765                 curname += strlen(curname) + 1;
  766                 inttotal += *curintr++;
  767         }
  768         printf("Total        %20ju\n", (uintmax_t)inttotal);
  769 
  770 #if defined(KDB) && !defined(KDB_UNATTENDED)
  771         kdb_backtrace();
  772         kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
  773 #else
  774         panic("watchdog timeout");
  775 #endif
  776 }
  777 
  778 #endif /* SW_WATCHDOG */

Cache object: cda2272605ca0a682844ae71506a986c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.