10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52036Swentaoy * Common Development and Distribution License (the "License").
62036Swentaoy * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate /*
2512233Srafael.vanoni@oracle.com * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/tuneable.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/cpuvar.h>
350Sstevel@tonic-gate #include <sys/lgrp.h>
360Sstevel@tonic-gate #include <sys/user.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/callo.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/var.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/swap.h>
430Sstevel@tonic-gate #include <sys/vmsystm.h>
440Sstevel@tonic-gate #include <sys/class.h>
450Sstevel@tonic-gate #include <sys/time.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/vtrace.h>
480Sstevel@tonic-gate #include <sys/spl.h>
490Sstevel@tonic-gate #include <sys/atomic.h>
500Sstevel@tonic-gate #include <sys/dumphdr.h>
510Sstevel@tonic-gate #include <sys/archsystm.h>
520Sstevel@tonic-gate #include <sys/fs/swapnode.h>
530Sstevel@tonic-gate #include <sys/panic.h>
540Sstevel@tonic-gate #include <sys/disp.h>
550Sstevel@tonic-gate #include <sys/msacct.h>
560Sstevel@tonic-gate #include <sys/mem_cage.h>
570Sstevel@tonic-gate
580Sstevel@tonic-gate #include <vm/page.h>
590Sstevel@tonic-gate #include <vm/anon.h>
600Sstevel@tonic-gate #include <vm/rm.h>
610Sstevel@tonic-gate #include <sys/cyclic.h>
620Sstevel@tonic-gate #include <sys/cpupart.h>
630Sstevel@tonic-gate #include <sys/rctl.h>
640Sstevel@tonic-gate #include <sys/task.h>
650Sstevel@tonic-gate #include <sys/sdt.h>
665107Seota #include <sys/ddi_timer.h>
6710696SDavid.Hollister@Sun.COM #include <sys/random.h>
6810696SDavid.Hollister@Sun.COM #include <sys/modctl.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * for NTP support
720Sstevel@tonic-gate */
730Sstevel@tonic-gate #include <sys/timex.h>
740Sstevel@tonic-gate #include <sys/inttypes.h>
750Sstevel@tonic-gate
7611066Srafael.vanoni@sun.com #include <sys/sunddi.h>
7711066Srafael.vanoni@sun.com #include <sys/clock_impl.h>
7811066Srafael.vanoni@sun.com
790Sstevel@tonic-gate /*
803792Sakolb * clock() is called straight from the clock cyclic; see clock_init().
810Sstevel@tonic-gate *
820Sstevel@tonic-gate * Functions:
830Sstevel@tonic-gate * reprime clock
840Sstevel@tonic-gate * maintain date
850Sstevel@tonic-gate * jab the scheduler
860Sstevel@tonic-gate */
870Sstevel@tonic-gate
880Sstevel@tonic-gate extern kcondvar_t fsflush_cv;
890Sstevel@tonic-gate extern sysinfo_t sysinfo;
900Sstevel@tonic-gate extern vminfo_t vminfo;
910Sstevel@tonic-gate extern int idleswtch; /* flag set while idle in pswtch() */
9210696SDavid.Hollister@Sun.COM extern hrtime_t volatile devinfo_freeze;
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * high-precision avenrun values. These are needed to make the
960Sstevel@tonic-gate * regular avenrun values accurate.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate static uint64_t hp_avenrun[3];
990Sstevel@tonic-gate int avenrun[3]; /* FSCALED average run queue lengths */
1000Sstevel@tonic-gate time_t time; /* time in seconds since 1970 - for compatibility only */
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate static struct loadavg_s loadavg;
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * Phase/frequency-lock loop (PLL/FLL) definitions
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * The following variables are read and set by the ntp_adjtime() system
1070Sstevel@tonic-gate * call.
1080Sstevel@tonic-gate *
1090Sstevel@tonic-gate * time_state shows the state of the system clock, with values defined
1100Sstevel@tonic-gate * in the timex.h header file.
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * time_status shows the status of the system clock, with bits defined
1130Sstevel@tonic-gate * in the timex.h header file.
1140Sstevel@tonic-gate *
1150Sstevel@tonic-gate * time_offset is used by the PLL/FLL to adjust the system time in small
1160Sstevel@tonic-gate * increments.
1170Sstevel@tonic-gate *
1180Sstevel@tonic-gate * time_constant determines the bandwidth or "stiffness" of the PLL.
1190Sstevel@tonic-gate *
1200Sstevel@tonic-gate * time_tolerance determines maximum frequency error or tolerance of the
1210Sstevel@tonic-gate * CPU clock oscillator and is a property of the architecture; however,
1220Sstevel@tonic-gate * in principle it could change as result of the presence of external
1230Sstevel@tonic-gate * discipline signals, for instance.
1240Sstevel@tonic-gate *
1250Sstevel@tonic-gate * time_precision is usually equal to the kernel tick variable; however,
1260Sstevel@tonic-gate * in cases where a precision clock counter or external clock is
1270Sstevel@tonic-gate * available, the resolution can be much less than this and depend on
1280Sstevel@tonic-gate * whether the external clock is working or not.
1290Sstevel@tonic-gate *
1300Sstevel@tonic-gate * time_maxerror is initialized by a ntp_adjtime() call and increased by
1310Sstevel@tonic-gate * the kernel once each second to reflect the maximum error bound
1320Sstevel@tonic-gate * growth.
1330Sstevel@tonic-gate *
1340Sstevel@tonic-gate * time_esterror is set and read by the ntp_adjtime() call, but
1350Sstevel@tonic-gate * otherwise not used by the kernel.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate int32_t time_state = TIME_OK; /* clock state */
1380Sstevel@tonic-gate int32_t time_status = STA_UNSYNC; /* clock status bits */
1390Sstevel@tonic-gate int32_t time_offset = 0; /* time offset (us) */
1400Sstevel@tonic-gate int32_t time_constant = 0; /* pll time constant */
1410Sstevel@tonic-gate int32_t time_tolerance = MAXFREQ; /* frequency tolerance (scaled ppm) */
1420Sstevel@tonic-gate int32_t time_precision = 1; /* clock precision (us) */
1430Sstevel@tonic-gate int32_t time_maxerror = MAXPHASE; /* maximum error (us) */
1440Sstevel@tonic-gate int32_t time_esterror = MAXPHASE; /* estimated error (us) */
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * The following variables establish the state of the PLL/FLL and the
1480Sstevel@tonic-gate * residual time and frequency offset of the local clock. The scale
1490Sstevel@tonic-gate * factors are defined in the timex.h header file.
1500Sstevel@tonic-gate *
1510Sstevel@tonic-gate * time_phase and time_freq are the phase increment and the frequency
1520Sstevel@tonic-gate * increment, respectively, of the kernel time variable.
1530Sstevel@tonic-gate *
1540Sstevel@tonic-gate * time_freq is set via ntp_adjtime() from a value stored in a file when
1550Sstevel@tonic-gate * the synchronization daemon is first started. Its value is retrieved
1560Sstevel@tonic-gate * via ntp_adjtime() and written to the file about once per hour by the
1570Sstevel@tonic-gate * daemon.
1580Sstevel@tonic-gate *
1590Sstevel@tonic-gate * time_adj is the adjustment added to the value of tick at each timer
1600Sstevel@tonic-gate * interrupt and is recomputed from time_phase and time_freq at each
1610Sstevel@tonic-gate * seconds rollover.
1620Sstevel@tonic-gate *
1630Sstevel@tonic-gate * time_reftime is the second's portion of the system time at the last
1640Sstevel@tonic-gate * call to ntp_adjtime(). It is used to adjust the time_freq variable
1650Sstevel@tonic-gate * and to increase the time_maxerror as the time since last update
1660Sstevel@tonic-gate * increases.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate int32_t time_phase = 0; /* phase offset (scaled us) */
1690Sstevel@tonic-gate int32_t time_freq = 0; /* frequency offset (scaled ppm) */
1700Sstevel@tonic-gate int32_t time_adj = 0; /* tick adjust (scaled 1 / hz) */
1710Sstevel@tonic-gate int32_t time_reftime = 0; /* time at last adjustment (s) */
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate * The scale factors of the following variables are defined in the
1750Sstevel@tonic-gate * timex.h header file.
1760Sstevel@tonic-gate *
1770Sstevel@tonic-gate * pps_time contains the time at each calibration interval, as read by
1780Sstevel@tonic-gate * microtime(). pps_count counts the seconds of the calibration
1790Sstevel@tonic-gate * interval, the duration of which is nominally pps_shift in powers of
1800Sstevel@tonic-gate * two.
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * pps_offset is the time offset produced by the time median filter
1830Sstevel@tonic-gate * pps_tf[], while pps_jitter is the dispersion (jitter) measured by
1840Sstevel@tonic-gate * this filter.
1850Sstevel@tonic-gate *
1860Sstevel@tonic-gate * pps_freq is the frequency offset produced by the frequency median
1870Sstevel@tonic-gate * filter pps_ff[], while pps_stabil is the dispersion (wander) measured
1880Sstevel@tonic-gate * by this filter.
1890Sstevel@tonic-gate *
1900Sstevel@tonic-gate * pps_usec is latched from a high resolution counter or external clock
1910Sstevel@tonic-gate * at pps_time. Here we want the hardware counter contents only, not the
1920Sstevel@tonic-gate * contents plus the time_tv.usec as usual.
1930Sstevel@tonic-gate *
1940Sstevel@tonic-gate * pps_valid counts the number of seconds since the last PPS update. It
1950Sstevel@tonic-gate * is used as a watchdog timer to disable the PPS discipline should the
1960Sstevel@tonic-gate * PPS signal be lost.
1970Sstevel@tonic-gate *
1980Sstevel@tonic-gate * pps_glitch counts the number of seconds since the beginning of an
1990Sstevel@tonic-gate * offset burst more than tick/2 from current nominal offset. It is used
2000Sstevel@tonic-gate * mainly to suppress error bursts due to priority conflicts between the
2010Sstevel@tonic-gate * PPS interrupt and timer interrupt.
2020Sstevel@tonic-gate *
2030Sstevel@tonic-gate * pps_intcnt counts the calibration intervals for use in the interval-
2040Sstevel@tonic-gate * adaptation algorithm. It's just too complicated for words.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate struct timeval pps_time; /* kernel time at last interval */
2070Sstevel@tonic-gate int32_t pps_tf[] = {0, 0, 0}; /* pps time offset median filter (us) */
2080Sstevel@tonic-gate int32_t pps_offset = 0; /* pps time offset (us) */
2090Sstevel@tonic-gate int32_t pps_jitter = MAXTIME; /* time dispersion (jitter) (us) */
2100Sstevel@tonic-gate int32_t pps_ff[] = {0, 0, 0}; /* pps frequency offset median filter */
2110Sstevel@tonic-gate int32_t pps_freq = 0; /* frequency offset (scaled ppm) */
2120Sstevel@tonic-gate int32_t pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */
2130Sstevel@tonic-gate int32_t pps_usec = 0; /* microsec counter at last interval */
2140Sstevel@tonic-gate int32_t pps_valid = PPS_VALID; /* pps signal watchdog counter */
2150Sstevel@tonic-gate int32_t pps_glitch = 0; /* pps signal glitch counter */
2160Sstevel@tonic-gate int32_t pps_count = 0; /* calibration interval counter (s) */
2170Sstevel@tonic-gate int32_t pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */
2180Sstevel@tonic-gate int32_t pps_intcnt = 0; /* intervals at current duration */
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * PPS signal quality monitors
2220Sstevel@tonic-gate *
2230Sstevel@tonic-gate * pps_jitcnt counts the seconds that have been discarded because the
2240Sstevel@tonic-gate * jitter measured by the time median filter exceeds the limit MAXTIME
2250Sstevel@tonic-gate * (100 us).
2260Sstevel@tonic-gate *
2270Sstevel@tonic-gate * pps_calcnt counts the frequency calibration intervals, which are
2280Sstevel@tonic-gate * variable from 4 s to 256 s.
2290Sstevel@tonic-gate *
2300Sstevel@tonic-gate * pps_errcnt counts the calibration intervals which have been discarded
2310Sstevel@tonic-gate * because the wander exceeds the limit MAXFREQ (100 ppm) or where the
2320Sstevel@tonic-gate * calibration interval jitter exceeds two ticks.
2330Sstevel@tonic-gate *
2340Sstevel@tonic-gate * pps_stbcnt counts the calibration intervals that have been discarded
2350Sstevel@tonic-gate * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
2360Sstevel@tonic-gate */
2370Sstevel@tonic-gate int32_t pps_jitcnt = 0; /* jitter limit exceeded */
2380Sstevel@tonic-gate int32_t pps_calcnt = 0; /* calibration intervals */
2390Sstevel@tonic-gate int32_t pps_errcnt = 0; /* calibration errors */
2400Sstevel@tonic-gate int32_t pps_stbcnt = 0; /* stability limit exceeded */
2410Sstevel@tonic-gate
24211066Srafael.vanoni@sun.com kcondvar_t lbolt_cv;
2430Sstevel@tonic-gate
24411066Srafael.vanoni@sun.com /*
24511066Srafael.vanoni@sun.com * Hybrid lbolt implementation:
24611066Srafael.vanoni@sun.com *
24711066Srafael.vanoni@sun.com * The service historically provided by the lbolt and lbolt64 variables has
24811066Srafael.vanoni@sun.com * been replaced by the ddi_get_lbolt() and ddi_get_lbolt64() routines, and the
24911066Srafael.vanoni@sun.com * original symbols removed from the system. The once clock driven variables are
25011066Srafael.vanoni@sun.com * now implemented in an event driven fashion, backed by gethrtime() coarsed to
25111066Srafael.vanoni@sun.com * the appropriate clock resolution. The default event driven implementation is
25211066Srafael.vanoni@sun.com * complemented by a cyclic driven one, active only during periods of intense
25311066Srafael.vanoni@sun.com * activity around the DDI lbolt routines, when a lbolt specific cyclic is
25411066Srafael.vanoni@sun.com * reprogramed to fire at a clock tick interval to serve consumers of lbolt who
25511066Srafael.vanoni@sun.com * rely on the original low cost of consulting a memory position.
25611066Srafael.vanoni@sun.com *
25711066Srafael.vanoni@sun.com * The implementation uses the number of calls to these routines and the
25811066Srafael.vanoni@sun.com * frequency of these to determine when to transition from event to cyclic
25911066Srafael.vanoni@sun.com * driven and vice-versa. These values are kept on a per CPU basis for
26011066Srafael.vanoni@sun.com * scalability reasons and to prevent CPUs from constantly invalidating a single
26111066Srafael.vanoni@sun.com * cache line when modifying a global variable. The transition from event to
26211066Srafael.vanoni@sun.com * cyclic mode happens once the thresholds are crossed, and activity on any CPU
26311066Srafael.vanoni@sun.com * can cause such transition.
26411066Srafael.vanoni@sun.com *
26511066Srafael.vanoni@sun.com * The lbolt_hybrid function pointer is called by ddi_get_lbolt() and
26611066Srafael.vanoni@sun.com * ddi_get_lbolt64(), and will point to lbolt_event_driven() or
26711066Srafael.vanoni@sun.com * lbolt_cyclic_driven() according to the current mode. When the thresholds
26811066Srafael.vanoni@sun.com * are exceeded, lbolt_event_driven() will reprogram the lbolt cyclic to
26911066Srafael.vanoni@sun.com * fire at a nsec_per_tick interval and increment an internal variable at
27011066Srafael.vanoni@sun.com * each firing. lbolt_hybrid will then point to lbolt_cyclic_driven(), which
27111066Srafael.vanoni@sun.com * will simply return the value of such variable. lbolt_cyclic() will attempt
27211066Srafael.vanoni@sun.com * to shut itself off at each threshold interval (sampling period for calls
27311066Srafael.vanoni@sun.com * to the DDI lbolt routines), and return to the event driven mode, but will
27411066Srafael.vanoni@sun.com * be prevented from doing so if lbolt_cyclic_driven() is being heavily used.
27511066Srafael.vanoni@sun.com *
27611066Srafael.vanoni@sun.com * lbolt_bootstrap is used during boot to serve lbolt consumers who don't wait
27711066Srafael.vanoni@sun.com * for the cyclic subsystem to be intialized.
27811066Srafael.vanoni@sun.com *
27911066Srafael.vanoni@sun.com */
28011226Srafael.vanoni@sun.com int64_t lbolt_bootstrap(void);
28111066Srafael.vanoni@sun.com int64_t lbolt_event_driven(void);
28211066Srafael.vanoni@sun.com int64_t lbolt_cyclic_driven(void);
28311066Srafael.vanoni@sun.com int64_t (*lbolt_hybrid)(void) = lbolt_bootstrap;
28411066Srafael.vanoni@sun.com uint_t lbolt_ev_to_cyclic(caddr_t, caddr_t);
28511066Srafael.vanoni@sun.com
28611066Srafael.vanoni@sun.com /*
28711066Srafael.vanoni@sun.com * lbolt's cyclic, installed by clock_init().
28811066Srafael.vanoni@sun.com */
28911066Srafael.vanoni@sun.com static void lbolt_cyclic(void);
29011066Srafael.vanoni@sun.com
29111066Srafael.vanoni@sun.com /*
29211066Srafael.vanoni@sun.com * Tunable to keep lbolt in cyclic driven mode. This will prevent the system
29311066Srafael.vanoni@sun.com * from switching back to event driven, once it reaches cyclic mode.
29411066Srafael.vanoni@sun.com */
29511066Srafael.vanoni@sun.com static boolean_t lbolt_cyc_only = B_FALSE;
29611066Srafael.vanoni@sun.com
29711066Srafael.vanoni@sun.com /*
29811066Srafael.vanoni@sun.com * Cache aligned, per CPU structure with lbolt usage statistics.
29911066Srafael.vanoni@sun.com */
30011066Srafael.vanoni@sun.com static lbolt_cpu_t *lb_cpu;
30111066Srafael.vanoni@sun.com
30211066Srafael.vanoni@sun.com /*
30311066Srafael.vanoni@sun.com * Single, cache aligned, structure with all the information required by
30411066Srafael.vanoni@sun.com * the lbolt implementation.
30511066Srafael.vanoni@sun.com */
30611066Srafael.vanoni@sun.com lbolt_info_t *lb_info;
30711066Srafael.vanoni@sun.com
30811066Srafael.vanoni@sun.com
3090Sstevel@tonic-gate int one_sec = 1; /* turned on once every second */
3100Sstevel@tonic-gate static int fsflushcnt; /* counter for t_fsflushr */
3110Sstevel@tonic-gate int dosynctodr = 1; /* patchable; enable/disable sync to TOD chip */
3120Sstevel@tonic-gate int tod_needsync = 0; /* need to sync tod chip with software time */
3130Sstevel@tonic-gate static int tod_broken = 0; /* clock chip doesn't work */
3140Sstevel@tonic-gate time_t boot_time = 0; /* Boot time in seconds since 1970 */
3150Sstevel@tonic-gate cyclic_id_t clock_cyclic; /* clock()'s cyclic_id */
3160Sstevel@tonic-gate cyclic_id_t deadman_cyclic; /* deadman()'s cyclic_id */
3175265Seota cyclic_id_t ddi_timer_cyclic; /* cyclic_timer()'s cyclic_id */
3180Sstevel@tonic-gate
3195788Smv143129 extern void clock_tick_schedule(int);
3205788Smv143129
3210Sstevel@tonic-gate static int lgrp_ticks; /* counter to schedule lgrp load calcs */
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate * for tod fault detection
3250Sstevel@tonic-gate */
3260Sstevel@tonic-gate #define TOD_REF_FREQ ((longlong_t)(NANOSEC))
3270Sstevel@tonic-gate #define TOD_STALL_THRESHOLD (TOD_REF_FREQ * 3 / 2)
3280Sstevel@tonic-gate #define TOD_JUMP_THRESHOLD (TOD_REF_FREQ / 2)
3290Sstevel@tonic-gate #define TOD_FILTER_N 4
3300Sstevel@tonic-gate #define TOD_FILTER_SETTLE (4 * TOD_FILTER_N)
3310Sstevel@tonic-gate static int tod_faulted = TOD_NOFAULT;
33211752STrevor.Thompson@Sun.COM
33311752STrevor.Thompson@Sun.COM static int tod_status_flag = 0; /* used by tod_validate() */
33411752STrevor.Thompson@Sun.COM
33511752STrevor.Thompson@Sun.COM static hrtime_t prev_set_tick = 0; /* gethrtime() prior to tod_set() */
33611752STrevor.Thompson@Sun.COM static time_t prev_set_tod = 0; /* tv_sec value passed to tod_set() */
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate /* patchable via /etc/system */
3390Sstevel@tonic-gate int tod_validate_enable = 1;
3400Sstevel@tonic-gate
34110696SDavid.Hollister@Sun.COM /* Diagnose/Limit messages about delay(9F) called from interrupt context */
34210696SDavid.Hollister@Sun.COM int delay_from_interrupt_diagnose = 0;
34310696SDavid.Hollister@Sun.COM volatile uint32_t delay_from_interrupt_msg = 20;
34410696SDavid.Hollister@Sun.COM
3450Sstevel@tonic-gate /*
346950Ssethg * On non-SPARC systems, TOD validation must be deferred until gethrtime
347950Ssethg * returns non-zero values (after mach_clkinit's execution).
348950Ssethg * On SPARC systems, it must be deferred until after hrtime_base
349950Ssethg * and hres_last_tick are set (in the first invocation of hres_tick).
350950Ssethg * Since in both cases the prerequisites occur before the invocation of
351950Ssethg * tod_get() in clock(), the deferment is lifted there.
352950Ssethg */
353950Ssethg static boolean_t tod_validate_deferred = B_TRUE;
354950Ssethg
355950Ssethg /*
3560Sstevel@tonic-gate * tod_fault_table[] must be aligned with
3570Sstevel@tonic-gate * enum tod_fault_type in systm.h
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate static char *tod_fault_table[] = {
3600Sstevel@tonic-gate "Reversed", /* TOD_REVERSED */
3610Sstevel@tonic-gate "Stalled", /* TOD_STALLED */
3620Sstevel@tonic-gate "Jumped", /* TOD_JUMPED */
3635084Sjohnlev "Changed in Clock Rate", /* TOD_RATECHANGED */
3645084Sjohnlev "Is Read-Only" /* TOD_RDONLY */
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate * no strings needed for TOD_NOFAULT
3670Sstevel@tonic-gate */
3680Sstevel@tonic-gate };
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate * test hook for tod broken detection in tod_validate
3720Sstevel@tonic-gate */
3730Sstevel@tonic-gate int tod_unit_test = 0;
3740Sstevel@tonic-gate time_t tod_test_injector;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate #define CLOCK_ADJ_HIST_SIZE 4
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate static int adj_hist_entry;
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate int64_t clock_adj_hist[CLOCK_ADJ_HIST_SIZE];
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate static void calcloadavg(int, uint64_t *);
3830Sstevel@tonic-gate static int genloadavg(struct loadavg_s *);
3840Sstevel@tonic-gate static void loadavg_update();
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate void (*cmm_clock_callout)() = NULL;
3873792Sakolb void (*cpucaps_clock_callout)() = NULL;
3880Sstevel@tonic-gate
3895788Smv143129 extern clock_t clock_tick_proc_max;
3905788Smv143129
39111066Srafael.vanoni@sun.com static int64_t deadman_counter = 0;
39211066Srafael.vanoni@sun.com
3930Sstevel@tonic-gate static void
clock(void)3940Sstevel@tonic-gate clock(void)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate kthread_t *t;
3975788Smv143129 uint_t nrunnable;
3980Sstevel@tonic-gate uint_t w_io;
3990Sstevel@tonic-gate cpu_t *cp;
4000Sstevel@tonic-gate cpupart_t *cpupart;
4010Sstevel@tonic-gate extern void set_freemem();
4020Sstevel@tonic-gate void (*funcp)();
4030Sstevel@tonic-gate int32_t ltemp;
4040Sstevel@tonic-gate int64_t lltemp;
4050Sstevel@tonic-gate int s;
4060Sstevel@tonic-gate int do_lgrp_load;
4070Sstevel@tonic-gate int i;
40811066Srafael.vanoni@sun.com clock_t now = LBOLT_NO_ACCOUNT; /* current tick */
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate if (panicstr)
4110Sstevel@tonic-gate return;
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * Make sure that 'freemem' do not drift too far from the truth
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate set_freemem();
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Before the section which is repeated is executed, we do
4210Sstevel@tonic-gate * the time delta processing which occurs every clock tick
4220Sstevel@tonic-gate *
4230Sstevel@tonic-gate * There is additional processing which happens every time
4240Sstevel@tonic-gate * the nanosecond counter rolls over which is described
4250Sstevel@tonic-gate * below - see the section which begins with : if (one_sec)
4260Sstevel@tonic-gate *
4270Sstevel@tonic-gate * This section marks the beginning of the precision-kernel
4280Sstevel@tonic-gate * code fragment.
4290Sstevel@tonic-gate *
4300Sstevel@tonic-gate * First, compute the phase adjustment. If the low-order bits
4310Sstevel@tonic-gate * (time_phase) of the update overflow, bump the higher order
4320Sstevel@tonic-gate * bits (time_update).
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate time_phase += time_adj;
4350Sstevel@tonic-gate if (time_phase <= -FINEUSEC) {
4360Sstevel@tonic-gate ltemp = -time_phase / SCALE_PHASE;
4370Sstevel@tonic-gate time_phase += ltemp * SCALE_PHASE;
4380Sstevel@tonic-gate s = hr_clock_lock();
4390Sstevel@tonic-gate timedelta -= ltemp * (NANOSEC/MICROSEC);
4400Sstevel@tonic-gate hr_clock_unlock(s);
4410Sstevel@tonic-gate } else if (time_phase >= FINEUSEC) {
4420Sstevel@tonic-gate ltemp = time_phase / SCALE_PHASE;
4430Sstevel@tonic-gate time_phase -= ltemp * SCALE_PHASE;
4440Sstevel@tonic-gate s = hr_clock_lock();
4450Sstevel@tonic-gate timedelta += ltemp * (NANOSEC/MICROSEC);
4460Sstevel@tonic-gate hr_clock_unlock(s);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * End of precision-kernel code fragment which is processed
4510Sstevel@tonic-gate * every timer interrupt.
4520Sstevel@tonic-gate *
4530Sstevel@tonic-gate * Continue with the interrupt processing as scheduled.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * Count the number of runnable threads and the number waiting
4570Sstevel@tonic-gate * for some form of I/O to complete -- gets added to
4580Sstevel@tonic-gate * sysinfo.waiting. To know the state of the system, must add
4590Sstevel@tonic-gate * wait counts from all CPUs. Also add up the per-partition
4600Sstevel@tonic-gate * statistics.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate w_io = 0;
4630Sstevel@tonic-gate nrunnable = 0;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * keep track of when to update lgrp/part loads
4670Sstevel@tonic-gate */
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate do_lgrp_load = 0;
4700Sstevel@tonic-gate if (lgrp_ticks++ >= hz / 10) {
4710Sstevel@tonic-gate lgrp_ticks = 0;
4720Sstevel@tonic-gate do_lgrp_load = 1;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
47511066Srafael.vanoni@sun.com if (one_sec) {
4760Sstevel@tonic-gate loadavg_update();
47711066Srafael.vanoni@sun.com deadman_counter++;
47811066Srafael.vanoni@sun.com }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate * First count the threads waiting on kpreempt queues in each
4820Sstevel@tonic-gate * CPU partition.
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate cpupart = cp_list_head;
4860Sstevel@tonic-gate do {
4870Sstevel@tonic-gate uint_t cpupart_nrunnable = cpupart->cp_kp_queue.disp_nrunnable;
4880Sstevel@tonic-gate
4890Sstevel@tonic-gate cpupart->cp_updates++;
4900Sstevel@tonic-gate nrunnable += cpupart_nrunnable;
4910Sstevel@tonic-gate cpupart->cp_nrunnable_cum += cpupart_nrunnable;
4920Sstevel@tonic-gate if (one_sec) {
4930Sstevel@tonic-gate cpupart->cp_nrunning = 0;
4940Sstevel@tonic-gate cpupart->cp_nrunnable = cpupart_nrunnable;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate } while ((cpupart = cpupart->cp_next) != cp_list_head);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /* Now count the per-CPU statistics. */
5000Sstevel@tonic-gate cp = cpu_list;
5010Sstevel@tonic-gate do {
5020Sstevel@tonic-gate uint_t cpu_nrunnable = cp->cpu_disp->disp_nrunnable;
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate nrunnable += cpu_nrunnable;
5050Sstevel@tonic-gate cpupart = cp->cpu_part;
5060Sstevel@tonic-gate cpupart->cp_nrunnable_cum += cpu_nrunnable;
5073446Smrj if (one_sec) {
5080Sstevel@tonic-gate cpupart->cp_nrunnable += cpu_nrunnable;
5093446Smrj /*
5105788Smv143129 * Update user, system, and idle cpu times.
5115788Smv143129 */
5125788Smv143129 cpupart->cp_nrunning++;
5135788Smv143129 /*
5143446Smrj * w_io is used to update sysinfo.waiting during
5153446Smrj * one_second processing below. Only gather w_io
5163446Smrj * information when we walk the list of cpus if we're
5173446Smrj * going to perform one_second processing.
5183446Smrj */
5193446Smrj w_io += CPU_STATS(cp, sys.iowait);
5205076Smishra }
5213446Smrj
5225076Smishra if (one_sec && (cp->cpu_flags & CPU_EXISTS)) {
5235076Smishra int i, load, change;
5245076Smishra hrtime_t intracct, intrused;
5255076Smishra const hrtime_t maxnsec = 1000000000;
5265076Smishra const int precision = 100;
5275076Smishra
5285076Smishra /*
5295076Smishra * Estimate interrupt load on this cpu each second.
5305076Smishra * Computes cpu_intrload as %utilization (0-99).
5315076Smishra */
5325076Smishra
5335076Smishra /* add up interrupt time from all micro states */
5345076Smishra for (intracct = 0, i = 0; i < NCMSTATES; i++)
5355076Smishra intracct += cp->cpu_intracct[i];
5365076Smishra scalehrtime(&intracct);
5375076Smishra
5385076Smishra /* compute nsec used in the past second */
5395076Smishra intrused = intracct - cp->cpu_intrlast;
5405076Smishra cp->cpu_intrlast = intracct;
5415076Smishra
5425076Smishra /* limit the value for safety (and the first pass) */
5435076Smishra if (intrused >= maxnsec)
5445076Smishra intrused = maxnsec - 1;
5455076Smishra
5465076Smishra /* calculate %time in interrupt */
5475076Smishra load = (precision * intrused) / maxnsec;
5485076Smishra ASSERT(load >= 0 && load < precision);
5495076Smishra change = cp->cpu_intrload - load;
5505076Smishra
5515076Smishra /* jump to new max, or decay the old max */
5525076Smishra if (change < 0)
5535076Smishra cp->cpu_intrload = load;
5545076Smishra else if (change > 0)
5555076Smishra cp->cpu_intrload -= (change + 3) / 4;
5565076Smishra
5575076Smishra DTRACE_PROBE3(cpu_intrload,
5585076Smishra cpu_t *, cp,
5595076Smishra hrtime_t, intracct,
5605076Smishra hrtime_t, intrused);
5613446Smrj }
5625076Smishra
5630Sstevel@tonic-gate if (do_lgrp_load &&
5640Sstevel@tonic-gate (cp->cpu_flags & CPU_EXISTS)) {
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate * When updating the lgroup's load average,
5670Sstevel@tonic-gate * account for the thread running on the CPU.
5680Sstevel@tonic-gate * If the CPU is the current one, then we need
5690Sstevel@tonic-gate * to account for the underlying thread which
5700Sstevel@tonic-gate * got the clock interrupt not the thread that is
5710Sstevel@tonic-gate * handling the interrupt and caculating the load
5720Sstevel@tonic-gate * average
5730Sstevel@tonic-gate */
5740Sstevel@tonic-gate t = cp->cpu_thread;
5750Sstevel@tonic-gate if (CPU == cp)
5760Sstevel@tonic-gate t = t->t_intr;
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate * Account for the load average for this thread if
5800Sstevel@tonic-gate * it isn't the idle thread or it is on the interrupt
5810Sstevel@tonic-gate * stack and not the current CPU handling the clock
5820Sstevel@tonic-gate * interrupt
5830Sstevel@tonic-gate */
5840Sstevel@tonic-gate if ((t && t != cp->cpu_idle_thread) || (CPU != cp &&
5850Sstevel@tonic-gate CPU_ON_INTR(cp))) {
5860Sstevel@tonic-gate if (t->t_lpl == cp->cpu_lpl) {
5870Sstevel@tonic-gate /* local thread */
5880Sstevel@tonic-gate cpu_nrunnable++;
5890Sstevel@tonic-gate } else {
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate * This is a remote thread, charge it
5920Sstevel@tonic-gate * against its home lgroup. Note that
5930Sstevel@tonic-gate * we notice that a thread is remote
5940Sstevel@tonic-gate * only if it's currently executing.
5950Sstevel@tonic-gate * This is a reasonable approximation,
5960Sstevel@tonic-gate * since queued remote threads are rare.
5970Sstevel@tonic-gate * Note also that if we didn't charge
5980Sstevel@tonic-gate * it to its home lgroup, remote
5990Sstevel@tonic-gate * execution would often make a system
6000Sstevel@tonic-gate * appear balanced even though it was
6010Sstevel@tonic-gate * not, and thread placement/migration
6020Sstevel@tonic-gate * would often not be done correctly.
6030Sstevel@tonic-gate */
6040Sstevel@tonic-gate lgrp_loadavg(t->t_lpl,
6050Sstevel@tonic-gate LGRP_LOADAVG_IN_THREAD_MAX, 0);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate lgrp_loadavg(cp->cpu_lpl,
6090Sstevel@tonic-gate cpu_nrunnable * LGRP_LOADAVG_IN_THREAD_MAX, 1);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list);
6120Sstevel@tonic-gate
6135788Smv143129 clock_tick_schedule(one_sec);
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate /*
6160Sstevel@tonic-gate * Check for a callout that needs be called from the clock
6170Sstevel@tonic-gate * thread to support the membership protocol in a clustered
6180Sstevel@tonic-gate * system. Copy the function pointer so that we can reset
6190Sstevel@tonic-gate * this to NULL if needed.
6200Sstevel@tonic-gate */
6210Sstevel@tonic-gate if ((funcp = cmm_clock_callout) != NULL)
6220Sstevel@tonic-gate (*funcp)();
6230Sstevel@tonic-gate
6243792Sakolb if ((funcp = cpucaps_clock_callout) != NULL)
6253792Sakolb (*funcp)();
6263792Sakolb
6270Sstevel@tonic-gate /*
6280Sstevel@tonic-gate * Wakeup the cageout thread waiters once per second.
6290Sstevel@tonic-gate */
63012293SJames.McPherson@Sun.COM if (one_sec)
63112293SJames.McPherson@Sun.COM kcage_tick();
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate if (one_sec) {
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate int drift, absdrift;
6360Sstevel@tonic-gate timestruc_t tod;
6370Sstevel@tonic-gate int s;
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate * Beginning of precision-kernel code fragment executed
6410Sstevel@tonic-gate * every second.
6420Sstevel@tonic-gate *
6430Sstevel@tonic-gate * On rollover of the second the phase adjustment to be
6440Sstevel@tonic-gate * used for the next second is calculated. Also, the
6450Sstevel@tonic-gate * maximum error is increased by the tolerance. If the
6460Sstevel@tonic-gate * PPS frequency discipline code is present, the phase is
6470Sstevel@tonic-gate * increased to compensate for the CPU clock oscillator
6480Sstevel@tonic-gate * frequency error.
6490Sstevel@tonic-gate *
6500Sstevel@tonic-gate * On a 32-bit machine and given parameters in the timex.h
6510Sstevel@tonic-gate * header file, the maximum phase adjustment is +-512 ms
6520Sstevel@tonic-gate * and maximum frequency offset is (a tad less than)
6530Sstevel@tonic-gate * +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate time_maxerror += time_tolerance / SCALE_USEC;
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * Leap second processing. If in leap-insert state at
6590Sstevel@tonic-gate * the end of the day, the system clock is set back one
6600Sstevel@tonic-gate * second; if in leap-delete state, the system clock is
6610Sstevel@tonic-gate * set ahead one second. The microtime() routine or
6620Sstevel@tonic-gate * external clock driver will insure that reported time
6630Sstevel@tonic-gate * is always monotonic. The ugly divides should be
6640Sstevel@tonic-gate * replaced.
6650Sstevel@tonic-gate */
6660Sstevel@tonic-gate switch (time_state) {
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate case TIME_OK:
6690Sstevel@tonic-gate if (time_status & STA_INS)
6700Sstevel@tonic-gate time_state = TIME_INS;
6710Sstevel@tonic-gate else if (time_status & STA_DEL)
6720Sstevel@tonic-gate time_state = TIME_DEL;
6730Sstevel@tonic-gate break;
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate case TIME_INS:
6760Sstevel@tonic-gate if (hrestime.tv_sec % 86400 == 0) {
6770Sstevel@tonic-gate s = hr_clock_lock();
6780Sstevel@tonic-gate hrestime.tv_sec--;
6790Sstevel@tonic-gate hr_clock_unlock(s);
6800Sstevel@tonic-gate time_state = TIME_OOP;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate break;
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate case TIME_DEL:
6850Sstevel@tonic-gate if ((hrestime.tv_sec + 1) % 86400 == 0) {
6860Sstevel@tonic-gate s = hr_clock_lock();
6870Sstevel@tonic-gate hrestime.tv_sec++;
6880Sstevel@tonic-gate hr_clock_unlock(s);
6890Sstevel@tonic-gate time_state = TIME_WAIT;
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate break;
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate case TIME_OOP:
6940Sstevel@tonic-gate time_state = TIME_WAIT;
6950Sstevel@tonic-gate break;
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate case TIME_WAIT:
6980Sstevel@tonic-gate if (!(time_status & (STA_INS | STA_DEL)))
6990Sstevel@tonic-gate time_state = TIME_OK;
7000Sstevel@tonic-gate default:
7010Sstevel@tonic-gate break;
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate /*
7050Sstevel@tonic-gate * Compute the phase adjustment for the next second. In
7060Sstevel@tonic-gate * PLL mode, the offset is reduced by a fixed factor
7070Sstevel@tonic-gate * times the time constant. In FLL mode the offset is
7080Sstevel@tonic-gate * used directly. In either mode, the maximum phase
7090Sstevel@tonic-gate * adjustment for each second is clamped so as to spread
7100Sstevel@tonic-gate * the adjustment over not more than the number of
7110Sstevel@tonic-gate * seconds between updates.
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate if (time_offset == 0)
7140Sstevel@tonic-gate time_adj = 0;
7150Sstevel@tonic-gate else if (time_offset < 0) {
7160Sstevel@tonic-gate lltemp = -time_offset;
7170Sstevel@tonic-gate if (!(time_status & STA_FLL)) {
7180Sstevel@tonic-gate if ((1 << time_constant) >= SCALE_KG)
7190Sstevel@tonic-gate lltemp *= (1 << time_constant) /
7200Sstevel@tonic-gate SCALE_KG;
7210Sstevel@tonic-gate else
7220Sstevel@tonic-gate lltemp = (lltemp / SCALE_KG) >>
7230Sstevel@tonic-gate time_constant;
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7260Sstevel@tonic-gate lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7270Sstevel@tonic-gate time_offset += lltemp;
7280Sstevel@tonic-gate time_adj = -(lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7290Sstevel@tonic-gate } else {
7300Sstevel@tonic-gate lltemp = time_offset;
7310Sstevel@tonic-gate if (!(time_status & STA_FLL)) {
7320Sstevel@tonic-gate if ((1 << time_constant) >= SCALE_KG)
7330Sstevel@tonic-gate lltemp *= (1 << time_constant) /
7340Sstevel@tonic-gate SCALE_KG;
7350Sstevel@tonic-gate else
7360Sstevel@tonic-gate lltemp = (lltemp / SCALE_KG) >>
7370Sstevel@tonic-gate time_constant;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate if (lltemp > (MAXPHASE / MINSEC) * SCALE_UPDATE)
7400Sstevel@tonic-gate lltemp = (MAXPHASE / MINSEC) * SCALE_UPDATE;
7410Sstevel@tonic-gate time_offset -= lltemp;
7420Sstevel@tonic-gate time_adj = (lltemp * SCALE_PHASE) / hz / SCALE_UPDATE;
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate * Compute the frequency estimate and additional phase
7470Sstevel@tonic-gate * adjustment due to frequency error for the next
7480Sstevel@tonic-gate * second. When the PPS signal is engaged, gnaw on the
7490Sstevel@tonic-gate * watchdog counter and update the frequency computed by
7500Sstevel@tonic-gate * the pll and the PPS signal.
7510Sstevel@tonic-gate */
7520Sstevel@tonic-gate pps_valid++;
7530Sstevel@tonic-gate if (pps_valid == PPS_VALID) {
7540Sstevel@tonic-gate pps_jitter = MAXTIME;
7550Sstevel@tonic-gate pps_stabil = MAXFREQ;
7560Sstevel@tonic-gate time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
7570Sstevel@tonic-gate STA_PPSWANDER | STA_PPSERROR);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate lltemp = time_freq + pps_freq;
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate if (lltemp)
7620Sstevel@tonic-gate time_adj += (lltemp * SCALE_PHASE) / (SCALE_USEC * hz);
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate * End of precision kernel-code fragment
7660Sstevel@tonic-gate *
7670Sstevel@tonic-gate * The section below should be modified if we are planning
7680Sstevel@tonic-gate * to use NTP for synchronization.
7690Sstevel@tonic-gate *
7700Sstevel@tonic-gate * Note: the clock synchronization code now assumes
7710Sstevel@tonic-gate * the following:
7720Sstevel@tonic-gate * - if dosynctodr is 1, then compute the drift between
7730Sstevel@tonic-gate * the tod chip and software time and adjust one or
7740Sstevel@tonic-gate * the other depending on the circumstances
7750Sstevel@tonic-gate *
7760Sstevel@tonic-gate * - if dosynctodr is 0, then the tod chip is independent
7770Sstevel@tonic-gate * of the software clock and should not be adjusted,
7780Sstevel@tonic-gate * but allowed to free run. this allows NTP to sync.
7790Sstevel@tonic-gate * hrestime without any interference from the tod chip.
7800Sstevel@tonic-gate */
7810Sstevel@tonic-gate
782950Ssethg tod_validate_deferred = B_FALSE;
7830Sstevel@tonic-gate mutex_enter(&tod_lock);
7840Sstevel@tonic-gate tod = tod_get();
7850Sstevel@tonic-gate drift = tod.tv_sec - hrestime.tv_sec;
7860Sstevel@tonic-gate absdrift = (drift >= 0) ? drift : -drift;
7870Sstevel@tonic-gate if (tod_needsync || absdrift > 1) {
7880Sstevel@tonic-gate int s;
7890Sstevel@tonic-gate if (absdrift > 2) {
7900Sstevel@tonic-gate if (!tod_broken && tod_faulted == TOD_NOFAULT) {
7910Sstevel@tonic-gate s = hr_clock_lock();
7920Sstevel@tonic-gate hrestime = tod;
7930Sstevel@tonic-gate membar_enter(); /* hrestime visible */
7940Sstevel@tonic-gate timedelta = 0;
7954123Sdm120769 timechanged++;
7960Sstevel@tonic-gate tod_needsync = 0;
7970Sstevel@tonic-gate hr_clock_unlock(s);
7988048SMadhavan.Venkataraman@Sun.COM callout_hrestime();
7998048SMadhavan.Venkataraman@Sun.COM
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate } else {
8020Sstevel@tonic-gate if (tod_needsync || !dosynctodr) {
8030Sstevel@tonic-gate gethrestime(&tod);
8040Sstevel@tonic-gate tod_set(tod);
8050Sstevel@tonic-gate s = hr_clock_lock();
8060Sstevel@tonic-gate if (timedelta == 0)
8070Sstevel@tonic-gate tod_needsync = 0;
8080Sstevel@tonic-gate hr_clock_unlock(s);
8090Sstevel@tonic-gate } else {
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate * If the drift is 2 seconds on the
8120Sstevel@tonic-gate * money, then the TOD is adjusting
8130Sstevel@tonic-gate * the clock; record that.
8140Sstevel@tonic-gate */
8150Sstevel@tonic-gate clock_adj_hist[adj_hist_entry++ %
81611066Srafael.vanoni@sun.com CLOCK_ADJ_HIST_SIZE] = now;
8170Sstevel@tonic-gate s = hr_clock_lock();
8180Sstevel@tonic-gate timedelta = (int64_t)drift*NANOSEC;
8190Sstevel@tonic-gate hr_clock_unlock(s);
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate one_sec = 0;
8240Sstevel@tonic-gate time = gethrestime_sec(); /* for crusty old kmem readers */
8250Sstevel@tonic-gate mutex_exit(&tod_lock);
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate * Some drivers still depend on this... XXX
8290Sstevel@tonic-gate */
8300Sstevel@tonic-gate cv_broadcast(&lbolt_cv);
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate vminfo.freemem += freemem;
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate pgcnt_t maxswap, resv, free;
8350Sstevel@tonic-gate pgcnt_t avail =
8360Sstevel@tonic-gate MAX((spgcnt_t)(availrmem - swapfs_minfree), 0);
8370Sstevel@tonic-gate
8385076Smishra maxswap = k_anoninfo.ani_mem_resv +
8395076Smishra k_anoninfo.ani_max +avail;
840*12908SPavel.Tatashin@Sun.COM /* Update ani_free */
841*12908SPavel.Tatashin@Sun.COM set_anoninfo();
8420Sstevel@tonic-gate free = k_anoninfo.ani_free + avail;
8430Sstevel@tonic-gate resv = k_anoninfo.ani_phys_resv +
8440Sstevel@tonic-gate k_anoninfo.ani_mem_resv;
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate vminfo.swap_resv += resv;
8470Sstevel@tonic-gate /* number of reserved and allocated pages */
8480Sstevel@tonic-gate #ifdef DEBUG
8490Sstevel@tonic-gate if (maxswap < free)
8500Sstevel@tonic-gate cmn_err(CE_WARN, "clock: maxswap < free");
8510Sstevel@tonic-gate if (maxswap < resv)
8520Sstevel@tonic-gate cmn_err(CE_WARN, "clock: maxswap < resv");
8530Sstevel@tonic-gate #endif
8540Sstevel@tonic-gate vminfo.swap_alloc += maxswap - free;
8550Sstevel@tonic-gate vminfo.swap_avail += maxswap - resv;
8560Sstevel@tonic-gate vminfo.swap_free += free;
8570Sstevel@tonic-gate }
85811657SDonghai.Qiao@Sun.COM vminfo.updates++;
8590Sstevel@tonic-gate if (nrunnable) {
8600Sstevel@tonic-gate sysinfo.runque += nrunnable;
8610Sstevel@tonic-gate sysinfo.runocc++;
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate if (nswapped) {
8640Sstevel@tonic-gate sysinfo.swpque += nswapped;
8650Sstevel@tonic-gate sysinfo.swpocc++;
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate sysinfo.waiting += w_io;
86811657SDonghai.Qiao@Sun.COM sysinfo.updates++;
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate * Wake up fsflush to write out DELWRI
8720Sstevel@tonic-gate * buffers, dirty pages and other cached
8730Sstevel@tonic-gate * administrative data, e.g. inodes.
8740Sstevel@tonic-gate */
8750Sstevel@tonic-gate if (--fsflushcnt <= 0) {
8760Sstevel@tonic-gate fsflushcnt = tune.t_fsflushr;
8770Sstevel@tonic-gate cv_signal(&fsflush_cv);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate vmmeter();
8810Sstevel@tonic-gate calcloadavg(genloadavg(&loadavg), hp_avenrun);
8820Sstevel@tonic-gate for (i = 0; i < 3; i++)
8830Sstevel@tonic-gate /*
8840Sstevel@tonic-gate * At the moment avenrun[] can only hold 31
8850Sstevel@tonic-gate * bits of load average as it is a signed
8860Sstevel@tonic-gate * int in the API. We need to ensure that
8870Sstevel@tonic-gate * hp_avenrun[i] >> (16 - FSHIFT) will not be
8880Sstevel@tonic-gate * too large. If it is, we put the largest value
8890Sstevel@tonic-gate * that we can use into avenrun[i]. This is
8900Sstevel@tonic-gate * kludgey, but about all we can do until we
8910Sstevel@tonic-gate * avenrun[] is declared as an array of uint64[]
8920Sstevel@tonic-gate */
8930Sstevel@tonic-gate if (hp_avenrun[i] < ((uint64_t)1<<(31+16-FSHIFT)))
8940Sstevel@tonic-gate avenrun[i] = (int32_t)(hp_avenrun[i] >>
8950Sstevel@tonic-gate (16 - FSHIFT));
8960Sstevel@tonic-gate else
8970Sstevel@tonic-gate avenrun[i] = 0x7fffffff;
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate cpupart = cp_list_head;
9000Sstevel@tonic-gate do {
9010Sstevel@tonic-gate calcloadavg(genloadavg(&cpupart->cp_loadavg),
9020Sstevel@tonic-gate cpupart->cp_hp_avenrun);
9030Sstevel@tonic-gate } while ((cpupart = cpupart->cp_next) != cp_list_head);
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate /*
9060Sstevel@tonic-gate * Wake up the swapper thread if necessary.
9070Sstevel@tonic-gate */
9080Sstevel@tonic-gate if (runin ||
9090Sstevel@tonic-gate (runout && (avefree < desfree || wake_sched_sec))) {
9100Sstevel@tonic-gate t = &t0;
9110Sstevel@tonic-gate thread_lock(t);
9120Sstevel@tonic-gate if (t->t_state == TS_STOPPED) {
9130Sstevel@tonic-gate runin = runout = 0;
9140Sstevel@tonic-gate wake_sched_sec = 0;
9150Sstevel@tonic-gate t->t_whystop = 0;
9160Sstevel@tonic-gate t->t_whatstop = 0;
9170Sstevel@tonic-gate t->t_schedflag &= ~TS_ALLSTART;
9180Sstevel@tonic-gate THREAD_TRANSITION(t);
9190Sstevel@tonic-gate setfrontdq(t);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate thread_unlock(t);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate * Wake up the swapper if any high priority swapped-out threads
9270Sstevel@tonic-gate * became runable during the last tick.
9280Sstevel@tonic-gate */
9290Sstevel@tonic-gate if (wake_sched) {
9300Sstevel@tonic-gate t = &t0;
9310Sstevel@tonic-gate thread_lock(t);
9320Sstevel@tonic-gate if (t->t_state == TS_STOPPED) {
9330Sstevel@tonic-gate runin = runout = 0;
9340Sstevel@tonic-gate wake_sched = 0;
9350Sstevel@tonic-gate t->t_whystop = 0;
9360Sstevel@tonic-gate t->t_whatstop = 0;
9370Sstevel@tonic-gate t->t_schedflag &= ~TS_ALLSTART;
9380Sstevel@tonic-gate THREAD_TRANSITION(t);
9390Sstevel@tonic-gate setfrontdq(t);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate thread_unlock(t);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate void
clock_init(void)9460Sstevel@tonic-gate clock_init(void)
9470Sstevel@tonic-gate {
94811066Srafael.vanoni@sun.com cyc_handler_t clk_hdlr, timer_hdlr, lbolt_hdlr;
94911066Srafael.vanoni@sun.com cyc_time_t clk_when, lbolt_when;
95011066Srafael.vanoni@sun.com int i, sz;
95111066Srafael.vanoni@sun.com intptr_t buf;
9520Sstevel@tonic-gate
95311066Srafael.vanoni@sun.com /*
95411066Srafael.vanoni@sun.com * Setup handler and timer for the clock cyclic.
95511066Srafael.vanoni@sun.com */
95611066Srafael.vanoni@sun.com clk_hdlr.cyh_func = (cyc_func_t)clock;
95711066Srafael.vanoni@sun.com clk_hdlr.cyh_level = CY_LOCK_LEVEL;
95811066Srafael.vanoni@sun.com clk_hdlr.cyh_arg = NULL;
9590Sstevel@tonic-gate
96011066Srafael.vanoni@sun.com clk_when.cyt_when = 0;
96111066Srafael.vanoni@sun.com clk_when.cyt_interval = nsec_per_tick;
9625107Seota
9635107Seota /*
9645107Seota * cyclic_timer is dedicated to the ddi interface, which
9655107Seota * uses the same clock resolution as the system one.
9665107Seota */
96711066Srafael.vanoni@sun.com timer_hdlr.cyh_func = (cyc_func_t)cyclic_timer;
96811066Srafael.vanoni@sun.com timer_hdlr.cyh_level = CY_LOCK_LEVEL;
96911066Srafael.vanoni@sun.com timer_hdlr.cyh_arg = NULL;
97011066Srafael.vanoni@sun.com
97111066Srafael.vanoni@sun.com /*
97211226Srafael.vanoni@sun.com * The lbolt cyclic will be reprogramed to fire at a nsec_per_tick
97311226Srafael.vanoni@sun.com * interval to satisfy performance needs of the DDI lbolt consumers.
97411226Srafael.vanoni@sun.com * It is off by default.
97511066Srafael.vanoni@sun.com */
97611066Srafael.vanoni@sun.com lbolt_hdlr.cyh_func = (cyc_func_t)lbolt_cyclic;
97711066Srafael.vanoni@sun.com lbolt_hdlr.cyh_level = CY_LOCK_LEVEL;
97811066Srafael.vanoni@sun.com lbolt_hdlr.cyh_arg = NULL;
97911066Srafael.vanoni@sun.com
98011066Srafael.vanoni@sun.com lbolt_when.cyt_interval = nsec_per_tick;
98111066Srafael.vanoni@sun.com
98211066Srafael.vanoni@sun.com /*
98311066Srafael.vanoni@sun.com * Allocate cache line aligned space for the per CPU lbolt data and
98411099Srafael.vanoni@sun.com * lbolt info structures, and initialize them with their default
98511099Srafael.vanoni@sun.com * values. Note that these structures are also cache line sized.
98611066Srafael.vanoni@sun.com */
98711066Srafael.vanoni@sun.com sz = sizeof (lbolt_info_t) + CPU_CACHE_COHERENCE_SIZE;
98811066Srafael.vanoni@sun.com buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
98911066Srafael.vanoni@sun.com lb_info = (lbolt_info_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
99011066Srafael.vanoni@sun.com
99111066Srafael.vanoni@sun.com if (hz != HZ_DEFAULT)
99211066Srafael.vanoni@sun.com lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL *
99311066Srafael.vanoni@sun.com hz/HZ_DEFAULT;
99411066Srafael.vanoni@sun.com else
99511066Srafael.vanoni@sun.com lb_info->lbi_thresh_interval = LBOLT_THRESH_INTERVAL;
99611066Srafael.vanoni@sun.com
99711066Srafael.vanoni@sun.com lb_info->lbi_thresh_calls = LBOLT_THRESH_CALLS;
99811066Srafael.vanoni@sun.com
99911099Srafael.vanoni@sun.com sz = (sizeof (lbolt_cpu_t) * max_ncpus) + CPU_CACHE_COHERENCE_SIZE;
100011066Srafael.vanoni@sun.com buf = (intptr_t)kmem_zalloc(sz, KM_SLEEP);
100111066Srafael.vanoni@sun.com lb_cpu = (lbolt_cpu_t *)P2ROUNDUP(buf, CPU_CACHE_COHERENCE_SIZE);
100211066Srafael.vanoni@sun.com
100311066Srafael.vanoni@sun.com for (i = 0; i < max_ncpus; i++)
100411066Srafael.vanoni@sun.com lb_cpu[i].lbc_counter = lb_info->lbi_thresh_calls;
100511066Srafael.vanoni@sun.com
100611226Srafael.vanoni@sun.com /*
100711226Srafael.vanoni@sun.com * Install the softint used to switch between event and cyclic driven
100811226Srafael.vanoni@sun.com * lbolt. We use a soft interrupt to make sure the context of the
100911226Srafael.vanoni@sun.com * cyclic reprogram call is safe.
101011226Srafael.vanoni@sun.com */
101111066Srafael.vanoni@sun.com lbolt_softint_add();
101211066Srafael.vanoni@sun.com
101311226Srafael.vanoni@sun.com /*
101411226Srafael.vanoni@sun.com * Since the hybrid lbolt implementation is based on a hardware counter
101511226Srafael.vanoni@sun.com * that is reset at every hardware reboot and that we'd like to have
101611226Srafael.vanoni@sun.com * the lbolt value starting at zero after both a hardware and a fast
101711226Srafael.vanoni@sun.com * reboot, we calculate the number of clock ticks the system's been up
101811226Srafael.vanoni@sun.com * and store it in the lbi_debug_time field of the lbolt info structure.
101911226Srafael.vanoni@sun.com * The value of this field will be subtracted from lbolt before
102011226Srafael.vanoni@sun.com * returning it.
102111226Srafael.vanoni@sun.com */
102211226Srafael.vanoni@sun.com lb_info->lbi_internal = lb_info->lbi_debug_time =
102311226Srafael.vanoni@sun.com (gethrtime()/nsec_per_tick);
102411226Srafael.vanoni@sun.com
102511226Srafael.vanoni@sun.com /*
102611226Srafael.vanoni@sun.com * lbolt_hybrid points at lbolt_bootstrap until now. The LBOLT_* macros
102711226Srafael.vanoni@sun.com * and lbolt_debug_{enter,return} use this value as an indication that
102811226Srafael.vanoni@sun.com * the initializaion above hasn't been completed. Setting lbolt_hybrid
102911226Srafael.vanoni@sun.com * to either lbolt_{cyclic,event}_driven here signals those code paths
103011226Srafael.vanoni@sun.com * that the lbolt related structures can be used.
103111226Srafael.vanoni@sun.com */
103211195Srafael.vanoni@sun.com if (lbolt_cyc_only) {
103311195Srafael.vanoni@sun.com lbolt_when.cyt_when = 0;
103411195Srafael.vanoni@sun.com lbolt_hybrid = lbolt_cyclic_driven;
103511195Srafael.vanoni@sun.com } else {
103611195Srafael.vanoni@sun.com lbolt_when.cyt_when = CY_INFINITY;
103711195Srafael.vanoni@sun.com lbolt_hybrid = lbolt_event_driven;
103811195Srafael.vanoni@sun.com }
103911195Srafael.vanoni@sun.com
104011066Srafael.vanoni@sun.com /*
104111066Srafael.vanoni@sun.com * Grab cpu_lock and install all three cyclics.
104211066Srafael.vanoni@sun.com */
10435107Seota mutex_enter(&cpu_lock);
104411066Srafael.vanoni@sun.com
104511066Srafael.vanoni@sun.com clock_cyclic = cyclic_add(&clk_hdlr, &clk_when);
104611066Srafael.vanoni@sun.com ddi_timer_cyclic = cyclic_add(&timer_hdlr, &clk_when);
104711151Srafael.vanoni@sun.com lb_info->id.lbi_cyclic_id = cyclic_add(&lbolt_hdlr, &lbolt_when);
104811066Srafael.vanoni@sun.com
10495107Seota mutex_exit(&cpu_lock);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate /*
10530Sstevel@tonic-gate * Called before calcloadavg to get 10-sec moving loadavg together
10540Sstevel@tonic-gate */
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate static int
genloadavg(struct loadavg_s * avgs)10570Sstevel@tonic-gate genloadavg(struct loadavg_s *avgs)
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate int avg;
10600Sstevel@tonic-gate int spos; /* starting position */
10610Sstevel@tonic-gate int cpos; /* moving current position */
10620Sstevel@tonic-gate int i;
10630Sstevel@tonic-gate int slen;
10640Sstevel@tonic-gate hrtime_t hr_avg;
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate /* 10-second snapshot, calculate first positon */
10670Sstevel@tonic-gate if (avgs->lg_len == 0) {
10680Sstevel@tonic-gate return (0);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate slen = avgs->lg_len < S_MOVAVG_SZ ? avgs->lg_len : S_MOVAVG_SZ;
10710Sstevel@tonic-gate
10720Sstevel@tonic-gate spos = (avgs->lg_cur - 1) >= 0 ? avgs->lg_cur - 1 :
10730Sstevel@tonic-gate S_LOADAVG_SZ + (avgs->lg_cur - 1);
10740Sstevel@tonic-gate for (i = hr_avg = 0; i < slen; i++) {
10750Sstevel@tonic-gate cpos = (spos - i) >= 0 ? spos - i : S_LOADAVG_SZ + (spos - i);
10760Sstevel@tonic-gate hr_avg += avgs->lg_loads[cpos];
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate hr_avg = hr_avg / slen;
10800Sstevel@tonic-gate avg = hr_avg / (NANOSEC / LGRP_LOADAVG_IN_THREAD_MAX);
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate return (avg);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate * Run every second from clock () to update the loadavg count available to the
10870Sstevel@tonic-gate * system and cpu-partitions.
10880Sstevel@tonic-gate *
10890Sstevel@tonic-gate * This works by sampling the previous usr, sys, wait time elapsed,
10900Sstevel@tonic-gate * computing a delta, and adding that delta to the elapsed usr, sys,
10910Sstevel@tonic-gate * wait increase.
10920Sstevel@tonic-gate */
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate static void
loadavg_update()10950Sstevel@tonic-gate loadavg_update()
10960Sstevel@tonic-gate {
10970Sstevel@tonic-gate cpu_t *cp;
10980Sstevel@tonic-gate cpupart_t *cpupart;
10990Sstevel@tonic-gate hrtime_t cpu_total;
11000Sstevel@tonic-gate int prev;
11010Sstevel@tonic-gate
11020Sstevel@tonic-gate cp = cpu_list;
11030Sstevel@tonic-gate loadavg.lg_total = 0;
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate /*
11060Sstevel@tonic-gate * first pass totals up per-cpu statistics for system and cpu
11070Sstevel@tonic-gate * partitions
11080Sstevel@tonic-gate */
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate do {
11110Sstevel@tonic-gate struct loadavg_s *lavg;
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate lavg = &cp->cpu_loadavg;
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate cpu_total = cp->cpu_acct[CMS_USER] +
11160Sstevel@tonic-gate cp->cpu_acct[CMS_SYSTEM] + cp->cpu_waitrq;
11170Sstevel@tonic-gate /* compute delta against last total */
11180Sstevel@tonic-gate scalehrtime(&cpu_total);
11190Sstevel@tonic-gate prev = (lavg->lg_cur - 1) >= 0 ? lavg->lg_cur - 1 :
11200Sstevel@tonic-gate S_LOADAVG_SZ + (lavg->lg_cur - 1);
11210Sstevel@tonic-gate if (lavg->lg_loads[prev] <= 0) {
11220Sstevel@tonic-gate lavg->lg_loads[lavg->lg_cur] = cpu_total;
11230Sstevel@tonic-gate cpu_total = 0;
11240Sstevel@tonic-gate } else {
11250Sstevel@tonic-gate lavg->lg_loads[lavg->lg_cur] = cpu_total;
11260Sstevel@tonic-gate cpu_total = cpu_total - lavg->lg_loads[prev];
11270Sstevel@tonic-gate if (cpu_total < 0)
11280Sstevel@tonic-gate cpu_total = 0;
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11320Sstevel@tonic-gate lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11330Sstevel@tonic-gate lavg->lg_len + 1 : S_LOADAVG_SZ;
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate loadavg.lg_total += cpu_total;
11360Sstevel@tonic-gate cp->cpu_part->cp_loadavg.lg_total += cpu_total;
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list);
11390Sstevel@tonic-gate
11400Sstevel@tonic-gate loadavg.lg_loads[loadavg.lg_cur] = loadavg.lg_total;
11410Sstevel@tonic-gate loadavg.lg_cur = (loadavg.lg_cur + 1) % S_LOADAVG_SZ;
11420Sstevel@tonic-gate loadavg.lg_len = (loadavg.lg_len + 1) < S_LOADAVG_SZ ?
11430Sstevel@tonic-gate loadavg.lg_len + 1 : S_LOADAVG_SZ;
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate * Second pass updates counts
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate cpupart = cp_list_head;
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate do {
11500Sstevel@tonic-gate struct loadavg_s *lavg;
11510Sstevel@tonic-gate
11520Sstevel@tonic-gate lavg = &cpupart->cp_loadavg;
11530Sstevel@tonic-gate lavg->lg_loads[lavg->lg_cur] = lavg->lg_total;
11540Sstevel@tonic-gate lavg->lg_total = 0;
11550Sstevel@tonic-gate lavg->lg_cur = (lavg->lg_cur + 1) % S_LOADAVG_SZ;
11560Sstevel@tonic-gate lavg->lg_len = (lavg->lg_len + 1) < S_LOADAVG_SZ ?
11570Sstevel@tonic-gate lavg->lg_len + 1 : S_LOADAVG_SZ;
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate } while ((cpupart = cpupart->cp_next) != cp_list_head);
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate /*
11640Sstevel@tonic-gate * clock_update() - local clock update
11650Sstevel@tonic-gate *
11660Sstevel@tonic-gate * This routine is called by ntp_adjtime() to update the local clock
11670Sstevel@tonic-gate * phase and frequency. The implementation is of an
11680Sstevel@tonic-gate * adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
11690Sstevel@tonic-gate * routine computes new time and frequency offset estimates for each
11700Sstevel@tonic-gate * call. The PPS signal itself determines the new time offset,
11710Sstevel@tonic-gate * instead of the calling argument. Presumably, calls to
11720Sstevel@tonic-gate * ntp_adjtime() occur only when the caller believes the local clock
11730Sstevel@tonic-gate * is valid within some bound (+-128 ms with NTP). If the caller's
11740Sstevel@tonic-gate * time is far different than the PPS time, an argument will ensue,
11750Sstevel@tonic-gate * and it's not clear who will lose.
11760Sstevel@tonic-gate *
11770Sstevel@tonic-gate * For uncompensated quartz crystal oscillatores and nominal update
11780Sstevel@tonic-gate * intervals less than 1024 s, operation should be in phase-lock mode
11790Sstevel@tonic-gate * (STA_FLL = 0), where the loop is disciplined to phase. For update
11800Sstevel@tonic-gate * intervals greater than this, operation should be in frequency-lock
11810Sstevel@tonic-gate * mode (STA_FLL = 1), where the loop is disciplined to frequency.
11820Sstevel@tonic-gate *
11830Sstevel@tonic-gate * Note: mutex(&tod_lock) is in effect.
11840Sstevel@tonic-gate */
11850Sstevel@tonic-gate void
clock_update(int offset)11860Sstevel@tonic-gate clock_update(int offset)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate int ltemp, mtemp, s;
11890Sstevel@tonic-gate
11900Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME))
11930Sstevel@tonic-gate return;
11940Sstevel@tonic-gate ltemp = offset;
11950Sstevel@tonic-gate if ((time_status & STA_PPSTIME) && (time_status & STA_PPSSIGNAL))
11960Sstevel@tonic-gate ltemp = pps_offset;
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate * Scale the phase adjustment and clamp to the operating range.
12000Sstevel@tonic-gate */
12010Sstevel@tonic-gate if (ltemp > MAXPHASE)
12020Sstevel@tonic-gate time_offset = MAXPHASE * SCALE_UPDATE;
12030Sstevel@tonic-gate else if (ltemp < -MAXPHASE)
12040Sstevel@tonic-gate time_offset = -(MAXPHASE * SCALE_UPDATE);
12050Sstevel@tonic-gate else
12060Sstevel@tonic-gate time_offset = ltemp * SCALE_UPDATE;
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate /*
12090Sstevel@tonic-gate * Select whether the frequency is to be controlled and in which
12100Sstevel@tonic-gate * mode (PLL or FLL). Clamp to the operating range. Ugly
12110Sstevel@tonic-gate * multiply/divide should be replaced someday.
12120Sstevel@tonic-gate */
12130Sstevel@tonic-gate if (time_status & STA_FREQHOLD || time_reftime == 0)
12140Sstevel@tonic-gate time_reftime = hrestime.tv_sec;
12150Sstevel@tonic-gate
12160Sstevel@tonic-gate mtemp = hrestime.tv_sec - time_reftime;
12170Sstevel@tonic-gate time_reftime = hrestime.tv_sec;
12180Sstevel@tonic-gate
12190Sstevel@tonic-gate if (time_status & STA_FLL) {
12200Sstevel@tonic-gate if (mtemp >= MINSEC) {
12210Sstevel@tonic-gate ltemp = ((time_offset / mtemp) * (SCALE_USEC /
12220Sstevel@tonic-gate SCALE_UPDATE));
12230Sstevel@tonic-gate if (ltemp)
12240Sstevel@tonic-gate time_freq += ltemp / SCALE_KH;
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate } else {
12270Sstevel@tonic-gate if (mtemp < MAXSEC) {
12280Sstevel@tonic-gate ltemp *= mtemp;
12290Sstevel@tonic-gate if (ltemp)
12300Sstevel@tonic-gate time_freq += (int)(((int64_t)ltemp *
12310Sstevel@tonic-gate SCALE_USEC) / SCALE_KF)
12320Sstevel@tonic-gate / (1 << (time_constant * 2));
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate if (time_freq > time_tolerance)
12360Sstevel@tonic-gate time_freq = time_tolerance;
12370Sstevel@tonic-gate else if (time_freq < -time_tolerance)
12380Sstevel@tonic-gate time_freq = -time_tolerance;
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate s = hr_clock_lock();
12410Sstevel@tonic-gate tod_needsync = 1;
12420Sstevel@tonic-gate hr_clock_unlock(s);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate * ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
12470Sstevel@tonic-gate *
12480Sstevel@tonic-gate * This routine is called at each PPS interrupt in order to discipline
12490Sstevel@tonic-gate * the CPU clock oscillator to the PPS signal. It measures the PPS phase
12500Sstevel@tonic-gate * and leaves it in a handy spot for the clock() routine. It
12510Sstevel@tonic-gate * integrates successive PPS phase differences and calculates the
12520Sstevel@tonic-gate * frequency offset. This is used in clock() to discipline the CPU
12530Sstevel@tonic-gate * clock oscillator so that intrinsic frequency error is cancelled out.
12540Sstevel@tonic-gate * The code requires the caller to capture the time and hardware counter
12550Sstevel@tonic-gate * value at the on-time PPS signal transition.
12560Sstevel@tonic-gate *
12570Sstevel@tonic-gate * Note that, on some Unix systems, this routine runs at an interrupt
12580Sstevel@tonic-gate * priority level higher than the timer interrupt routine clock().
12590Sstevel@tonic-gate * Therefore, the variables used are distinct from the clock()
12600Sstevel@tonic-gate * variables, except for certain exceptions: The PPS frequency pps_freq
12610Sstevel@tonic-gate * and phase pps_offset variables are determined by this routine and
12620Sstevel@tonic-gate * updated atomically. The time_tolerance variable can be considered a
12630Sstevel@tonic-gate * constant, since it is infrequently changed, and then only when the
12640Sstevel@tonic-gate * PPS signal is disabled. The watchdog counter pps_valid is updated
12650Sstevel@tonic-gate * once per second by clock() and is atomically cleared in this
12660Sstevel@tonic-gate * routine.
12670Sstevel@tonic-gate *
12680Sstevel@tonic-gate * tvp is the time of the last tick; usec is a microsecond count since the
12690Sstevel@tonic-gate * last tick.
12700Sstevel@tonic-gate *
12710Sstevel@tonic-gate * Note: In Solaris systems, the tick value is actually given by
12720Sstevel@tonic-gate * usec_per_tick. This is called from the serial driver cdintr(),
12730Sstevel@tonic-gate * or equivalent, at a high PIL. Because the kernel keeps a
12740Sstevel@tonic-gate * highresolution time, the following code can accept either
12750Sstevel@tonic-gate * the traditional argument pair, or the current highres timestamp
12760Sstevel@tonic-gate * in tvp and zero in usec.
12770Sstevel@tonic-gate */
12780Sstevel@tonic-gate void
ddi_hardpps(struct timeval * tvp,int usec)12790Sstevel@tonic-gate ddi_hardpps(struct timeval *tvp, int usec)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate int u_usec, v_usec, bigtick;
12820Sstevel@tonic-gate time_t cal_sec;
12830Sstevel@tonic-gate int cal_usec;
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate /*
12860Sstevel@tonic-gate * An occasional glitch can be produced when the PPS interrupt
12870Sstevel@tonic-gate * occurs in the clock() routine before the time variable is
12880Sstevel@tonic-gate * updated. Here the offset is discarded when the difference
12890Sstevel@tonic-gate * between it and the last one is greater than tick/2, but not
12900Sstevel@tonic-gate * if the interval since the first discard exceeds 30 s.
12910Sstevel@tonic-gate */
12920Sstevel@tonic-gate time_status |= STA_PPSSIGNAL;
12930Sstevel@tonic-gate time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
12940Sstevel@tonic-gate pps_valid = 0;
12950Sstevel@tonic-gate u_usec = -tvp->tv_usec;
12960Sstevel@tonic-gate if (u_usec < -(MICROSEC/2))
12970Sstevel@tonic-gate u_usec += MICROSEC;
12980Sstevel@tonic-gate v_usec = pps_offset - u_usec;
12990Sstevel@tonic-gate if (v_usec < 0)
13000Sstevel@tonic-gate v_usec = -v_usec;
13010Sstevel@tonic-gate if (v_usec > (usec_per_tick >> 1)) {
13020Sstevel@tonic-gate if (pps_glitch > MAXGLITCH) {
13030Sstevel@tonic-gate pps_glitch = 0;
13040Sstevel@tonic-gate pps_tf[2] = u_usec;
13050Sstevel@tonic-gate pps_tf[1] = u_usec;
13060Sstevel@tonic-gate } else {
13070Sstevel@tonic-gate pps_glitch++;
13080Sstevel@tonic-gate u_usec = pps_offset;
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate } else
13110Sstevel@tonic-gate pps_glitch = 0;
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate /*
13140Sstevel@tonic-gate * A three-stage median filter is used to help deglitch the pps
13150Sstevel@tonic-gate * time. The median sample becomes the time offset estimate; the
13160Sstevel@tonic-gate * difference between the other two samples becomes the time
13170Sstevel@tonic-gate * dispersion (jitter) estimate.
13180Sstevel@tonic-gate */
13190Sstevel@tonic-gate pps_tf[2] = pps_tf[1];
13200Sstevel@tonic-gate pps_tf[1] = pps_tf[0];
13210Sstevel@tonic-gate pps_tf[0] = u_usec;
13220Sstevel@tonic-gate if (pps_tf[0] > pps_tf[1]) {
13230Sstevel@tonic-gate if (pps_tf[1] > pps_tf[2]) {
13240Sstevel@tonic-gate pps_offset = pps_tf[1]; /* 0 1 2 */
13250Sstevel@tonic-gate v_usec = pps_tf[0] - pps_tf[2];
13260Sstevel@tonic-gate } else if (pps_tf[2] > pps_tf[0]) {
13270Sstevel@tonic-gate pps_offset = pps_tf[0]; /* 2 0 1 */
13280Sstevel@tonic-gate v_usec = pps_tf[2] - pps_tf[1];
13290Sstevel@tonic-gate } else {
13300Sstevel@tonic-gate pps_offset = pps_tf[2]; /* 0 2 1 */
13310Sstevel@tonic-gate v_usec = pps_tf[0] - pps_tf[1];
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate } else {
13340Sstevel@tonic-gate if (pps_tf[1] < pps_tf[2]) {
13350Sstevel@tonic-gate pps_offset = pps_tf[1]; /* 2 1 0 */
13360Sstevel@tonic-gate v_usec = pps_tf[2] - pps_tf[0];
13370Sstevel@tonic-gate } else if (pps_tf[2] < pps_tf[0]) {
13380Sstevel@tonic-gate pps_offset = pps_tf[0]; /* 1 0 2 */
13390Sstevel@tonic-gate v_usec = pps_tf[1] - pps_tf[2];
13400Sstevel@tonic-gate } else {
13410Sstevel@tonic-gate pps_offset = pps_tf[2]; /* 1 2 0 */
13420Sstevel@tonic-gate v_usec = pps_tf[1] - pps_tf[0];
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate if (v_usec > MAXTIME)
13460Sstevel@tonic-gate pps_jitcnt++;
13470Sstevel@tonic-gate v_usec = (v_usec << PPS_AVG) - pps_jitter;
13480Sstevel@tonic-gate pps_jitter += v_usec / (1 << PPS_AVG);
13490Sstevel@tonic-gate if (pps_jitter > (MAXTIME >> 1))
13500Sstevel@tonic-gate time_status |= STA_PPSJITTER;
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate /*
13530Sstevel@tonic-gate * During the calibration interval adjust the starting time when
13540Sstevel@tonic-gate * the tick overflows. At the end of the interval compute the
13550Sstevel@tonic-gate * duration of the interval and the difference of the hardware
13560Sstevel@tonic-gate * counters at the beginning and end of the interval. This code
13570Sstevel@tonic-gate * is deliciously complicated by the fact valid differences may
13580Sstevel@tonic-gate * exceed the value of tick when using long calibration
13590Sstevel@tonic-gate * intervals and small ticks. Note that the counter can be
13600Sstevel@tonic-gate * greater than tick if caught at just the wrong instant, but
13610Sstevel@tonic-gate * the values returned and used here are correct.
13620Sstevel@tonic-gate */
13630Sstevel@tonic-gate bigtick = (int)usec_per_tick * SCALE_USEC;
13640Sstevel@tonic-gate pps_usec -= pps_freq;
13650Sstevel@tonic-gate if (pps_usec >= bigtick)
13660Sstevel@tonic-gate pps_usec -= bigtick;
13670Sstevel@tonic-gate if (pps_usec < 0)
13680Sstevel@tonic-gate pps_usec += bigtick;
13690Sstevel@tonic-gate pps_time.tv_sec++;
13700Sstevel@tonic-gate pps_count++;
13710Sstevel@tonic-gate if (pps_count < (1 << pps_shift))
13720Sstevel@tonic-gate return;
13730Sstevel@tonic-gate pps_count = 0;
13740Sstevel@tonic-gate pps_calcnt++;
13750Sstevel@tonic-gate u_usec = usec * SCALE_USEC;
13760Sstevel@tonic-gate v_usec = pps_usec - u_usec;
13770Sstevel@tonic-gate if (v_usec >= bigtick >> 1)
13780Sstevel@tonic-gate v_usec -= bigtick;
13790Sstevel@tonic-gate if (v_usec < -(bigtick >> 1))
13800Sstevel@tonic-gate v_usec += bigtick;
13810Sstevel@tonic-gate if (v_usec < 0)
13820Sstevel@tonic-gate v_usec = -(-v_usec >> pps_shift);
13830Sstevel@tonic-gate else
13840Sstevel@tonic-gate v_usec = v_usec >> pps_shift;
13850Sstevel@tonic-gate pps_usec = u_usec;
13860Sstevel@tonic-gate cal_sec = tvp->tv_sec;
13870Sstevel@tonic-gate cal_usec = tvp->tv_usec;
13880Sstevel@tonic-gate cal_sec -= pps_time.tv_sec;
13890Sstevel@tonic-gate cal_usec -= pps_time.tv_usec;
13900Sstevel@tonic-gate if (cal_usec < 0) {
13910Sstevel@tonic-gate cal_usec += MICROSEC;
13920Sstevel@tonic-gate cal_sec--;
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate pps_time = *tvp;
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate /*
13970Sstevel@tonic-gate * Check for lost interrupts, noise, excessive jitter and
13980Sstevel@tonic-gate * excessive frequency error. The number of timer ticks during
13990Sstevel@tonic-gate * the interval may vary +-1 tick. Add to this a margin of one
14000Sstevel@tonic-gate * tick for the PPS signal jitter and maximum frequency
14010Sstevel@tonic-gate * deviation. If the limits are exceeded, the calibration
14020Sstevel@tonic-gate * interval is reset to the minimum and we start over.
14030Sstevel@tonic-gate */
14040Sstevel@tonic-gate u_usec = (int)usec_per_tick << 1;
14050Sstevel@tonic-gate if (!((cal_sec == -1 && cal_usec > (MICROSEC - u_usec)) ||
14060Sstevel@tonic-gate (cal_sec == 0 && cal_usec < u_usec)) ||
14070Sstevel@tonic-gate v_usec > time_tolerance || v_usec < -time_tolerance) {
14080Sstevel@tonic-gate pps_errcnt++;
14090Sstevel@tonic-gate pps_shift = PPS_SHIFT;
14100Sstevel@tonic-gate pps_intcnt = 0;
14110Sstevel@tonic-gate time_status |= STA_PPSERROR;
14120Sstevel@tonic-gate return;
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate * A three-stage median filter is used to help deglitch the pps
14170Sstevel@tonic-gate * frequency. The median sample becomes the frequency offset
14180Sstevel@tonic-gate * estimate; the difference between the other two samples
14190Sstevel@tonic-gate * becomes the frequency dispersion (stability) estimate.
14200Sstevel@tonic-gate */
14210Sstevel@tonic-gate pps_ff[2] = pps_ff[1];
14220Sstevel@tonic-gate pps_ff[1] = pps_ff[0];
14230Sstevel@tonic-gate pps_ff[0] = v_usec;
14240Sstevel@tonic-gate if (pps_ff[0] > pps_ff[1]) {
14250Sstevel@tonic-gate if (pps_ff[1] > pps_ff[2]) {
14260Sstevel@tonic-gate u_usec = pps_ff[1]; /* 0 1 2 */
14270Sstevel@tonic-gate v_usec = pps_ff[0] - pps_ff[2];
14280Sstevel@tonic-gate } else if (pps_ff[2] > pps_ff[0]) {
14290Sstevel@tonic-gate u_usec = pps_ff[0]; /* 2 0 1 */
14300Sstevel@tonic-gate v_usec = pps_ff[2] - pps_ff[1];
14310Sstevel@tonic-gate } else {
14320Sstevel@tonic-gate u_usec = pps_ff[2]; /* 0 2 1 */
14330Sstevel@tonic-gate v_usec = pps_ff[0] - pps_ff[1];
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate } else {
14360Sstevel@tonic-gate if (pps_ff[1] < pps_ff[2]) {
14370Sstevel@tonic-gate u_usec = pps_ff[1]; /* 2 1 0 */
14380Sstevel@tonic-gate v_usec = pps_ff[2] - pps_ff[0];
14390Sstevel@tonic-gate } else if (pps_ff[2] < pps_ff[0]) {
14400Sstevel@tonic-gate u_usec = pps_ff[0]; /* 1 0 2 */
14410Sstevel@tonic-gate v_usec = pps_ff[1] - pps_ff[2];
14420Sstevel@tonic-gate } else {
14430Sstevel@tonic-gate u_usec = pps_ff[2]; /* 1 2 0 */
14440Sstevel@tonic-gate v_usec = pps_ff[1] - pps_ff[0];
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate
14480Sstevel@tonic-gate /*
14490Sstevel@tonic-gate * Here the frequency dispersion (stability) is updated. If it
14500Sstevel@tonic-gate * is less than one-fourth the maximum (MAXFREQ), the frequency
14510Sstevel@tonic-gate * offset is updated as well, but clamped to the tolerance. It
14520Sstevel@tonic-gate * will be processed later by the clock() routine.
14530Sstevel@tonic-gate */
14540Sstevel@tonic-gate v_usec = (v_usec >> 1) - pps_stabil;
14550Sstevel@tonic-gate if (v_usec < 0)
14560Sstevel@tonic-gate pps_stabil -= -v_usec >> PPS_AVG;
14570Sstevel@tonic-gate else
14580Sstevel@tonic-gate pps_stabil += v_usec >> PPS_AVG;
14590Sstevel@tonic-gate if (pps_stabil > MAXFREQ >> 2) {
14600Sstevel@tonic-gate pps_stbcnt++;
14610Sstevel@tonic-gate time_status |= STA_PPSWANDER;
14620Sstevel@tonic-gate return;
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate if (time_status & STA_PPSFREQ) {
14650Sstevel@tonic-gate if (u_usec < 0) {
14660Sstevel@tonic-gate pps_freq -= -u_usec >> PPS_AVG;
14670Sstevel@tonic-gate if (pps_freq < -time_tolerance)
14680Sstevel@tonic-gate pps_freq = -time_tolerance;
14690Sstevel@tonic-gate u_usec = -u_usec;
14700Sstevel@tonic-gate } else {
14710Sstevel@tonic-gate pps_freq += u_usec >> PPS_AVG;
14720Sstevel@tonic-gate if (pps_freq > time_tolerance)
14730Sstevel@tonic-gate pps_freq = time_tolerance;
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate * Here the calibration interval is adjusted. If the maximum
14790Sstevel@tonic-gate * time difference is greater than tick / 4, reduce the interval
14800Sstevel@tonic-gate * by half. If this is not the case for four consecutive
14810Sstevel@tonic-gate * intervals, double the interval.
14820Sstevel@tonic-gate */
14830Sstevel@tonic-gate if (u_usec << pps_shift > bigtick >> 2) {
14840Sstevel@tonic-gate pps_intcnt = 0;
14850Sstevel@tonic-gate if (pps_shift > PPS_SHIFT)
14860Sstevel@tonic-gate pps_shift--;
14870Sstevel@tonic-gate } else if (pps_intcnt >= 4) {
14880Sstevel@tonic-gate pps_intcnt = 0;
14890Sstevel@tonic-gate if (pps_shift < PPS_SHIFTMAX)
14900Sstevel@tonic-gate pps_shift++;
14910Sstevel@tonic-gate } else
14920Sstevel@tonic-gate pps_intcnt++;
14930Sstevel@tonic-gate
14940Sstevel@tonic-gate /*
14950Sstevel@tonic-gate * If recovering from kmdb, then make sure the tod chip gets resynced.
14960Sstevel@tonic-gate * If we took an early exit above, then we don't yet have a stable
14970Sstevel@tonic-gate * calibration signal to lock onto, so don't mark the tod for sync
14980Sstevel@tonic-gate * until we get all the way here.
14990Sstevel@tonic-gate */
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate int s = hr_clock_lock();
15020Sstevel@tonic-gate
15030Sstevel@tonic-gate tod_needsync = 1;
15040Sstevel@tonic-gate hr_clock_unlock(s);
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate /*
15090Sstevel@tonic-gate * Handle clock tick processing for a thread.
15100Sstevel@tonic-gate * Check for timer action, enforce CPU rlimit, do profiling etc.
15110Sstevel@tonic-gate */
15120Sstevel@tonic-gate void
clock_tick(kthread_t * t,int pending)15135788Smv143129 clock_tick(kthread_t *t, int pending)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate struct proc *pp;
15160Sstevel@tonic-gate klwp_id_t lwp;
15170Sstevel@tonic-gate struct as *as;
15185788Smv143129 clock_t ticks;
15190Sstevel@tonic-gate int poke = 0; /* notify another CPU */
15200Sstevel@tonic-gate int user_mode;
15210Sstevel@tonic-gate size_t rss;
15225788Smv143129 int i, total_usec, usec;
15235788Smv143129 rctl_qty_t secs;
15245788Smv143129
15255788Smv143129 ASSERT(pending > 0);
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate /* Must be operating on a lwp/thread */
15280Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL) {
15290Sstevel@tonic-gate panic("clock_tick: no lwp");
15300Sstevel@tonic-gate /*NOTREACHED*/
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15335788Smv143129 for (i = 0; i < pending; i++) {
15345788Smv143129 CL_TICK(t); /* Class specific tick processing */
15355788Smv143129 DTRACE_SCHED1(tick, kthread_t *, t);
15365788Smv143129 }
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate pp = ttoproc(t);
15390Sstevel@tonic-gate
15400Sstevel@tonic-gate /* pp->p_lock makes sure that the thread does not exit */
15410Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pp->p_lock));
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate user_mode = (lwp->lwp_state == LWP_USER);
15440Sstevel@tonic-gate
15455788Smv143129 ticks = (pp->p_utime + pp->p_stime) % hz;
15460Sstevel@tonic-gate /*
15470Sstevel@tonic-gate * Update process times. Should use high res clock and state
15480Sstevel@tonic-gate * changes instead of statistical sampling method. XXX
15490Sstevel@tonic-gate */
15500Sstevel@tonic-gate if (user_mode) {
15515788Smv143129 pp->p_utime += pending;
15520Sstevel@tonic-gate } else {
15535788Smv143129 pp->p_stime += pending;
15540Sstevel@tonic-gate }
15555788Smv143129
15565788Smv143129 pp->p_ttime += pending;
15570Sstevel@tonic-gate as = pp->p_as;
15580Sstevel@tonic-gate
15590Sstevel@tonic-gate /*
15600Sstevel@tonic-gate * Update user profiling statistics. Get the pc from the
15610Sstevel@tonic-gate * lwp when the AST happens.
15620Sstevel@tonic-gate */
15630Sstevel@tonic-gate if (pp->p_prof.pr_scale) {
15645788Smv143129 atomic_add_32(&lwp->lwp_oweupc, (int32_t)pending);
15650Sstevel@tonic-gate if (user_mode) {
15660Sstevel@tonic-gate poke = 1;
15670Sstevel@tonic-gate aston(t);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate
15715788Smv143129 /*
15725788Smv143129 * If CPU was in user state, process lwp-virtual time
15735788Smv143129 * interval timer. The value passed to itimerdecr() has to be
15745788Smv143129 * in microseconds and has to be less than one second. Hence
15755788Smv143129 * this loop.
15765788Smv143129 */
15775788Smv143129 total_usec = usec_per_tick * pending;
15785788Smv143129 while (total_usec > 0) {
15795788Smv143129 usec = MIN(total_usec, (MICROSEC - 1));
15805788Smv143129 if (user_mode &&
15815788Smv143129 timerisset(&lwp->lwp_timer[ITIMER_VIRTUAL].it_value) &&
15825788Smv143129 itimerdecr(&lwp->lwp_timer[ITIMER_VIRTUAL], usec) == 0) {
15835788Smv143129 poke = 1;
15845788Smv143129 sigtoproc(pp, t, SIGVTALRM);
15855788Smv143129 }
15865788Smv143129 total_usec -= usec;
15875788Smv143129 }
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate /*
15905788Smv143129 * If CPU was in user state, process lwp-profile
15910Sstevel@tonic-gate * interval timer.
15920Sstevel@tonic-gate */
15935788Smv143129 total_usec = usec_per_tick * pending;
15945788Smv143129 while (total_usec > 0) {
15955788Smv143129 usec = MIN(total_usec, (MICROSEC - 1));
15965788Smv143129 if (timerisset(&lwp->lwp_timer[ITIMER_PROF].it_value) &&
15975788Smv143129 itimerdecr(&lwp->lwp_timer[ITIMER_PROF], usec) == 0) {
15985788Smv143129 poke = 1;
15995788Smv143129 sigtoproc(pp, t, SIGPROF);
16005788Smv143129 }
16015788Smv143129 total_usec -= usec;
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate * Enforce CPU resource controls:
16060Sstevel@tonic-gate * (a) process.max-cpu-time resource control
16075788Smv143129 *
16085788Smv143129 * Perform the check only if we have accumulated more a second.
16090Sstevel@tonic-gate */
16105788Smv143129 if ((ticks + pending) >= hz) {
16115788Smv143129 (void) rctl_test(rctlproc_legacy[RLIMIT_CPU], pp->p_rctls, pp,
16125788Smv143129 (pp->p_utime + pp->p_stime)/hz, RCA_UNSAFE_SIGINFO);
16135788Smv143129 }
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate /*
16160Sstevel@tonic-gate * (b) task.max-cpu-time resource control
16175788Smv143129 *
16185788Smv143129 * If we have accumulated enough ticks, increment the task CPU
16195788Smv143129 * time usage and test for the resource limit. This minimizes the
16205788Smv143129 * number of calls to the rct_test(). The task CPU time mutex
16215788Smv143129 * is highly contentious as many processes can be sharing a task.
16220Sstevel@tonic-gate */
16235788Smv143129 if (pp->p_ttime >= clock_tick_proc_max) {
16245788Smv143129 secs = task_cpu_time_incr(pp->p_task, pp->p_ttime);
16255788Smv143129 pp->p_ttime = 0;
16265788Smv143129 if (secs) {
16275788Smv143129 (void) rctl_test(rc_task_cpu_time, pp->p_task->tk_rctls,
16285788Smv143129 pp, secs, RCA_UNSAFE_SIGINFO);
16295788Smv143129 }
16305788Smv143129 }
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate /*
16330Sstevel@tonic-gate * Update memory usage for the currently running process.
16340Sstevel@tonic-gate */
16350Sstevel@tonic-gate rss = rm_asrss(as);
16360Sstevel@tonic-gate PTOU(pp)->u_mem += rss;
16370Sstevel@tonic-gate if (rss > PTOU(pp)->u_mem_max)
16380Sstevel@tonic-gate PTOU(pp)->u_mem_max = rss;
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate /*
16410Sstevel@tonic-gate * Notify the CPU the thread is running on.
16420Sstevel@tonic-gate */
16430Sstevel@tonic-gate if (poke && t->t_cpu != CPU)
16440Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate void
profil_tick(uintptr_t upc)16480Sstevel@tonic-gate profil_tick(uintptr_t upc)
16490Sstevel@tonic-gate {
16500Sstevel@tonic-gate int ticks;
16510Sstevel@tonic-gate proc_t *p = ttoproc(curthread);
16520Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
16530Sstevel@tonic-gate struct prof *pr = &p->p_prof;
16540Sstevel@tonic-gate
16550Sstevel@tonic-gate do {
16560Sstevel@tonic-gate ticks = lwp->lwp_oweupc;
16570Sstevel@tonic-gate } while (cas32(&lwp->lwp_oweupc, ticks, 0) != ticks);
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate mutex_enter(&p->p_pflock);
16600Sstevel@tonic-gate if (pr->pr_scale >= 2 && upc >= pr->pr_off) {
16610Sstevel@tonic-gate /*
16620Sstevel@tonic-gate * Old-style profiling
16630Sstevel@tonic-gate */
16640Sstevel@tonic-gate uint16_t *slot = pr->pr_base;
16650Sstevel@tonic-gate uint16_t old, new;
16660Sstevel@tonic-gate if (pr->pr_scale != 2) {
16670Sstevel@tonic-gate uintptr_t delta = upc - pr->pr_off;
16680Sstevel@tonic-gate uintptr_t byteoff = ((delta >> 16) * pr->pr_scale) +
16690Sstevel@tonic-gate (((delta & 0xffff) * pr->pr_scale) >> 16);
16700Sstevel@tonic-gate if (byteoff >= (uintptr_t)pr->pr_size) {
16710Sstevel@tonic-gate mutex_exit(&p->p_pflock);
16720Sstevel@tonic-gate return;
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate slot += byteoff / sizeof (uint16_t);
16750Sstevel@tonic-gate }
16760Sstevel@tonic-gate if (fuword16(slot, &old) < 0 ||
16770Sstevel@tonic-gate (new = old + ticks) > SHRT_MAX ||
16780Sstevel@tonic-gate suword16(slot, new) < 0) {
16790Sstevel@tonic-gate pr->pr_scale = 0;
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate } else if (pr->pr_scale == 1) {
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate * PC Sampling
16840Sstevel@tonic-gate */
16850Sstevel@tonic-gate model_t model = lwp_getdatamodel(lwp);
16860Sstevel@tonic-gate int result;
16870Sstevel@tonic-gate #ifdef __lint
16880Sstevel@tonic-gate model = model;
16890Sstevel@tonic-gate #endif
16900Sstevel@tonic-gate while (ticks-- > 0) {
16910Sstevel@tonic-gate if (pr->pr_samples == pr->pr_size) {
16920Sstevel@tonic-gate /* buffer full, turn off sampling */
16930Sstevel@tonic-gate pr->pr_scale = 0;
16940Sstevel@tonic-gate break;
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate switch (SIZEOF_PTR(model)) {
16970Sstevel@tonic-gate case sizeof (uint32_t):
16980Sstevel@tonic-gate result = suword32(pr->pr_base, (uint32_t)upc);
16990Sstevel@tonic-gate break;
17000Sstevel@tonic-gate #ifdef _LP64
17010Sstevel@tonic-gate case sizeof (uint64_t):
17020Sstevel@tonic-gate result = suword64(pr->pr_base, (uint64_t)upc);
17030Sstevel@tonic-gate break;
17040Sstevel@tonic-gate #endif
17050Sstevel@tonic-gate default:
17060Sstevel@tonic-gate cmn_err(CE_WARN, "profil_tick: unexpected "
17070Sstevel@tonic-gate "data model");
17080Sstevel@tonic-gate result = -1;
17090Sstevel@tonic-gate break;
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate if (result != 0) {
17120Sstevel@tonic-gate pr->pr_scale = 0;
17130Sstevel@tonic-gate break;
17140Sstevel@tonic-gate }
17150Sstevel@tonic-gate pr->pr_base = (caddr_t)pr->pr_base + SIZEOF_PTR(model);
17160Sstevel@tonic-gate pr->pr_samples++;
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate mutex_exit(&p->p_pflock);
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate static void
delay_wakeup(void * arg)17230Sstevel@tonic-gate delay_wakeup(void *arg)
17240Sstevel@tonic-gate {
172510696SDavid.Hollister@Sun.COM kthread_t *t = arg;
17260Sstevel@tonic-gate
17270Sstevel@tonic-gate mutex_enter(&t->t_delay_lock);
17280Sstevel@tonic-gate cv_signal(&t->t_delay_cv);
17290Sstevel@tonic-gate mutex_exit(&t->t_delay_lock);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate
173210696SDavid.Hollister@Sun.COM /*
173310696SDavid.Hollister@Sun.COM * The delay(9F) man page indicates that it can only be called from user or
173410696SDavid.Hollister@Sun.COM * kernel context - detect and diagnose bad calls. The following macro will
173510696SDavid.Hollister@Sun.COM * produce a limited number of messages identifying bad callers. This is done
173610696SDavid.Hollister@Sun.COM * in a macro so that caller() is meaningful. When a bad caller is identified,
173710696SDavid.Hollister@Sun.COM * switching to 'drv_usecwait(TICK_TO_USEC(ticks));' may be appropriate.
173810696SDavid.Hollister@Sun.COM */
173910696SDavid.Hollister@Sun.COM #define DELAY_CONTEXT_CHECK() { \
174010696SDavid.Hollister@Sun.COM uint32_t m; \
174110696SDavid.Hollister@Sun.COM char *f; \
174210696SDavid.Hollister@Sun.COM ulong_t off; \
174310696SDavid.Hollister@Sun.COM \
174410696SDavid.Hollister@Sun.COM m = delay_from_interrupt_msg; \
174510696SDavid.Hollister@Sun.COM if (delay_from_interrupt_diagnose && servicing_interrupt() && \
174610696SDavid.Hollister@Sun.COM !panicstr && !devinfo_freeze && \
174710696SDavid.Hollister@Sun.COM atomic_cas_32(&delay_from_interrupt_msg, m ? m : 1, m-1)) { \
174810696SDavid.Hollister@Sun.COM f = modgetsymname((uintptr_t)caller(), &off); \
174910696SDavid.Hollister@Sun.COM cmn_err(CE_WARN, "delay(9F) called from " \
175010696SDavid.Hollister@Sun.COM "interrupt context: %s`%s", \
175110696SDavid.Hollister@Sun.COM mod_containing_pc(caller()), f ? f : "..."); \
175210696SDavid.Hollister@Sun.COM } \
175310696SDavid.Hollister@Sun.COM }
175410696SDavid.Hollister@Sun.COM
175510696SDavid.Hollister@Sun.COM /*
175610696SDavid.Hollister@Sun.COM * delay_common: common delay code.
175710696SDavid.Hollister@Sun.COM */
175810696SDavid.Hollister@Sun.COM static void
delay_common(clock_t ticks)175910696SDavid.Hollister@Sun.COM delay_common(clock_t ticks)
176010696SDavid.Hollister@Sun.COM {
176110696SDavid.Hollister@Sun.COM kthread_t *t = curthread;
176210696SDavid.Hollister@Sun.COM clock_t deadline;
176310696SDavid.Hollister@Sun.COM clock_t timeleft;
176410696SDavid.Hollister@Sun.COM callout_id_t id;
176510696SDavid.Hollister@Sun.COM
176610696SDavid.Hollister@Sun.COM /* If timeouts aren't running all we can do is spin. */
176710696SDavid.Hollister@Sun.COM if (panicstr || devinfo_freeze) {
176810696SDavid.Hollister@Sun.COM /* Convert delay(9F) call into drv_usecwait(9F) call. */
176910696SDavid.Hollister@Sun.COM if (ticks > 0)
177010696SDavid.Hollister@Sun.COM drv_usecwait(TICK_TO_USEC(ticks));
177110696SDavid.Hollister@Sun.COM return;
177210696SDavid.Hollister@Sun.COM }
177310696SDavid.Hollister@Sun.COM
177411066Srafael.vanoni@sun.com deadline = ddi_get_lbolt() + ticks;
177511066Srafael.vanoni@sun.com while ((timeleft = deadline - ddi_get_lbolt()) > 0) {
177610696SDavid.Hollister@Sun.COM mutex_enter(&t->t_delay_lock);
177710696SDavid.Hollister@Sun.COM id = timeout_default(delay_wakeup, t, timeleft);
177810696SDavid.Hollister@Sun.COM cv_wait(&t->t_delay_cv, &t->t_delay_lock);
177910696SDavid.Hollister@Sun.COM mutex_exit(&t->t_delay_lock);
178010696SDavid.Hollister@Sun.COM (void) untimeout_default(id, 0);
178110696SDavid.Hollister@Sun.COM }
178210696SDavid.Hollister@Sun.COM }
178310696SDavid.Hollister@Sun.COM
178410696SDavid.Hollister@Sun.COM /*
178510696SDavid.Hollister@Sun.COM * Delay specified number of clock ticks.
178610696SDavid.Hollister@Sun.COM */
17870Sstevel@tonic-gate void
delay(clock_t ticks)17880Sstevel@tonic-gate delay(clock_t ticks)
17890Sstevel@tonic-gate {
179010696SDavid.Hollister@Sun.COM DELAY_CONTEXT_CHECK();
179110696SDavid.Hollister@Sun.COM
179210696SDavid.Hollister@Sun.COM delay_common(ticks);
179310696SDavid.Hollister@Sun.COM }
17940Sstevel@tonic-gate
179510696SDavid.Hollister@Sun.COM /*
179610696SDavid.Hollister@Sun.COM * Delay a random number of clock ticks between 1 and ticks.
179710696SDavid.Hollister@Sun.COM */
179810696SDavid.Hollister@Sun.COM void
delay_random(clock_t ticks)179910696SDavid.Hollister@Sun.COM delay_random(clock_t ticks)
180010696SDavid.Hollister@Sun.COM {
180110696SDavid.Hollister@Sun.COM int r;
18020Sstevel@tonic-gate
180310696SDavid.Hollister@Sun.COM DELAY_CONTEXT_CHECK();
180410696SDavid.Hollister@Sun.COM
180510696SDavid.Hollister@Sun.COM (void) random_get_pseudo_bytes((void *)&r, sizeof (r));
180610696SDavid.Hollister@Sun.COM if (ticks == 0)
180710696SDavid.Hollister@Sun.COM ticks = 1;
180810696SDavid.Hollister@Sun.COM ticks = (r % ticks) + 1;
180910696SDavid.Hollister@Sun.COM delay_common(ticks);
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate
18120Sstevel@tonic-gate /*
18130Sstevel@tonic-gate * Like delay, but interruptible by a signal.
18140Sstevel@tonic-gate */
18150Sstevel@tonic-gate int
delay_sig(clock_t ticks)18160Sstevel@tonic-gate delay_sig(clock_t ticks)
18170Sstevel@tonic-gate {
181810696SDavid.Hollister@Sun.COM kthread_t *t = curthread;
181910696SDavid.Hollister@Sun.COM clock_t deadline;
182010696SDavid.Hollister@Sun.COM clock_t rc;
18210Sstevel@tonic-gate
182210696SDavid.Hollister@Sun.COM /* If timeouts aren't running all we can do is spin. */
182310696SDavid.Hollister@Sun.COM if (panicstr || devinfo_freeze) {
182410696SDavid.Hollister@Sun.COM if (ticks > 0)
182510696SDavid.Hollister@Sun.COM drv_usecwait(TICK_TO_USEC(ticks));
182610696SDavid.Hollister@Sun.COM return (0);
182710696SDavid.Hollister@Sun.COM }
182810696SDavid.Hollister@Sun.COM
182911066Srafael.vanoni@sun.com deadline = ddi_get_lbolt() + ticks;
183010696SDavid.Hollister@Sun.COM mutex_enter(&t->t_delay_lock);
18310Sstevel@tonic-gate do {
183210696SDavid.Hollister@Sun.COM rc = cv_timedwait_sig(&t->t_delay_cv,
183310696SDavid.Hollister@Sun.COM &t->t_delay_lock, deadline);
183410696SDavid.Hollister@Sun.COM /* loop until past deadline or signaled */
18350Sstevel@tonic-gate } while (rc > 0);
183610696SDavid.Hollister@Sun.COM mutex_exit(&t->t_delay_lock);
18370Sstevel@tonic-gate if (rc == 0)
18380Sstevel@tonic-gate return (EINTR);
18390Sstevel@tonic-gate return (0);
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate
184210696SDavid.Hollister@Sun.COM
18430Sstevel@tonic-gate #define SECONDS_PER_DAY 86400
18440Sstevel@tonic-gate
18450Sstevel@tonic-gate /*
18460Sstevel@tonic-gate * Initialize the system time based on the TOD chip. approx is used as
18470Sstevel@tonic-gate * an approximation of time (e.g. from the filesystem) in the event that
18480Sstevel@tonic-gate * the TOD chip has been cleared or is unresponsive. An approx of -1
18490Sstevel@tonic-gate * means the filesystem doesn't keep time.
18500Sstevel@tonic-gate */
18510Sstevel@tonic-gate void
clkset(time_t approx)18520Sstevel@tonic-gate clkset(time_t approx)
18530Sstevel@tonic-gate {
18540Sstevel@tonic-gate timestruc_t ts;
18550Sstevel@tonic-gate int spl;
18560Sstevel@tonic-gate int set_clock = 0;
18570Sstevel@tonic-gate
18580Sstevel@tonic-gate mutex_enter(&tod_lock);
18590Sstevel@tonic-gate ts = tod_get();
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate if (ts.tv_sec > 365 * SECONDS_PER_DAY) {
18620Sstevel@tonic-gate /*
18630Sstevel@tonic-gate * If the TOD chip is reporting some time after 1971,
18640Sstevel@tonic-gate * then it probably didn't lose power or become otherwise
18650Sstevel@tonic-gate * cleared in the recent past; check to assure that
18660Sstevel@tonic-gate * the time coming from the filesystem isn't in the future
18670Sstevel@tonic-gate * according to the TOD chip.
18680Sstevel@tonic-gate */
18690Sstevel@tonic-gate if (approx != -1 && approx > ts.tv_sec) {
18700Sstevel@tonic-gate cmn_err(CE_WARN, "Last shutdown is later "
18710Sstevel@tonic-gate "than time on time-of-day chip; check date.");
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate } else {
18740Sstevel@tonic-gate /*
18759158SKrishnendu.Sadhukhan@Sun.COM * If the TOD chip isn't giving correct time, set it to the
18769158SKrishnendu.Sadhukhan@Sun.COM * greater of i) approx and ii) 1987. That way if approx
18779158SKrishnendu.Sadhukhan@Sun.COM * is negative or is earlier than 1987, we set the clock
18789158SKrishnendu.Sadhukhan@Sun.COM * back to a time when Oliver North, ALF and Dire Straits
18799158SKrishnendu.Sadhukhan@Sun.COM * were all on the collective brain: 1987.
18800Sstevel@tonic-gate */
18810Sstevel@tonic-gate timestruc_t tmp;
18829158SKrishnendu.Sadhukhan@Sun.COM time_t diagnose_date = (1987 - 1970) * 365 * SECONDS_PER_DAY;
18839158SKrishnendu.Sadhukhan@Sun.COM ts.tv_sec = (approx > diagnose_date ? approx : diagnose_date);
18840Sstevel@tonic-gate ts.tv_nsec = 0;
18850Sstevel@tonic-gate
18860Sstevel@tonic-gate /*
18870Sstevel@tonic-gate * Attempt to write the new time to the TOD chip. Set spl high
18880Sstevel@tonic-gate * to avoid getting preempted between the tod_set and tod_get.
18890Sstevel@tonic-gate */
18900Sstevel@tonic-gate spl = splhi();
18910Sstevel@tonic-gate tod_set(ts);
18920Sstevel@tonic-gate tmp = tod_get();
18930Sstevel@tonic-gate splx(spl);
18940Sstevel@tonic-gate
18950Sstevel@tonic-gate if (tmp.tv_sec != ts.tv_sec && tmp.tv_sec != ts.tv_sec + 1) {
18960Sstevel@tonic-gate tod_broken = 1;
18970Sstevel@tonic-gate dosynctodr = 0;
18989158SKrishnendu.Sadhukhan@Sun.COM cmn_err(CE_WARN, "Time-of-day chip unresponsive.");
18990Sstevel@tonic-gate } else {
19000Sstevel@tonic-gate cmn_err(CE_WARN, "Time-of-day chip had "
19010Sstevel@tonic-gate "incorrect date; check and reset.");
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate set_clock = 1;
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate
19060Sstevel@tonic-gate if (!boot_time) {
19070Sstevel@tonic-gate boot_time = ts.tv_sec;
19080Sstevel@tonic-gate set_clock = 1;
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate
19110Sstevel@tonic-gate if (set_clock)
19120Sstevel@tonic-gate set_hrestime(&ts);
19130Sstevel@tonic-gate
19140Sstevel@tonic-gate mutex_exit(&tod_lock);
19150Sstevel@tonic-gate }
19160Sstevel@tonic-gate
19174123Sdm120769 int timechanged; /* for testing if the system time has been reset */
19180Sstevel@tonic-gate
19190Sstevel@tonic-gate void
set_hrestime(timestruc_t * ts)19200Sstevel@tonic-gate set_hrestime(timestruc_t *ts)
19210Sstevel@tonic-gate {
19220Sstevel@tonic-gate int spl = hr_clock_lock();
19230Sstevel@tonic-gate hrestime = *ts;
19244123Sdm120769 membar_enter(); /* hrestime must be visible before timechanged++ */
19250Sstevel@tonic-gate timedelta = 0;
19264123Sdm120769 timechanged++;
19270Sstevel@tonic-gate hr_clock_unlock(spl);
19288048SMadhavan.Venkataraman@Sun.COM callout_hrestime();
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate static uint_t deadman_seconds;
19320Sstevel@tonic-gate static uint32_t deadman_panics;
19330Sstevel@tonic-gate static int deadman_enabled = 0;
19340Sstevel@tonic-gate static int deadman_panic_timers = 1;
19350Sstevel@tonic-gate
19360Sstevel@tonic-gate static void
deadman(void)19370Sstevel@tonic-gate deadman(void)
19380Sstevel@tonic-gate {
19390Sstevel@tonic-gate if (panicstr) {
19400Sstevel@tonic-gate /*
19410Sstevel@tonic-gate * During panic, other CPUs besides the panic
19420Sstevel@tonic-gate * master continue to handle cyclics and some other
19430Sstevel@tonic-gate * interrupts. The code below is intended to be
19440Sstevel@tonic-gate * single threaded, so any CPU other than the master
19450Sstevel@tonic-gate * must keep out.
19460Sstevel@tonic-gate */
19470Sstevel@tonic-gate if (CPU->cpu_id != panic_cpu.cpu_id)
19480Sstevel@tonic-gate return;
19490Sstevel@tonic-gate
19500Sstevel@tonic-gate if (!deadman_panic_timers)
19510Sstevel@tonic-gate return; /* allow all timers to be manually disabled */
19520Sstevel@tonic-gate
19530Sstevel@tonic-gate /*
19540Sstevel@tonic-gate * If we are generating a crash dump or syncing filesystems and
19550Sstevel@tonic-gate * the corresponding timer is set, decrement it and re-enter
19560Sstevel@tonic-gate * the panic code to abort it and advance to the next state.
19570Sstevel@tonic-gate * The panic states and triggers are explained in panic.c.
19580Sstevel@tonic-gate */
19590Sstevel@tonic-gate if (panic_dump) {
19600Sstevel@tonic-gate if (dump_timeleft && (--dump_timeleft == 0)) {
19610Sstevel@tonic-gate panic("panic dump timeout");
19620Sstevel@tonic-gate /*NOTREACHED*/
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate } else if (panic_sync) {
19650Sstevel@tonic-gate if (sync_timeleft && (--sync_timeleft == 0)) {
19660Sstevel@tonic-gate panic("panic sync timeout");
19670Sstevel@tonic-gate /*NOTREACHED*/
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate }
19700Sstevel@tonic-gate
19710Sstevel@tonic-gate return;
19720Sstevel@tonic-gate }
19730Sstevel@tonic-gate
197411066Srafael.vanoni@sun.com if (deadman_counter != CPU->cpu_deadman_counter) {
197511066Srafael.vanoni@sun.com CPU->cpu_deadman_counter = deadman_counter;
19760Sstevel@tonic-gate CPU->cpu_deadman_countdown = deadman_seconds;
19770Sstevel@tonic-gate return;
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate
19806054Svb160487 if (--CPU->cpu_deadman_countdown > 0)
19810Sstevel@tonic-gate return;
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate /*
19840Sstevel@tonic-gate * Regardless of whether or not we actually bring the system down,
19850Sstevel@tonic-gate * bump the deadman_panics variable.
19860Sstevel@tonic-gate *
19870Sstevel@tonic-gate * N.B. deadman_panics is incremented once for each CPU that
19880Sstevel@tonic-gate * passes through here. It's expected that all the CPUs will
19890Sstevel@tonic-gate * detect this condition within one second of each other, so
19900Sstevel@tonic-gate * when deadman_enabled is off, deadman_panics will
19910Sstevel@tonic-gate * typically be a multiple of the total number of CPUs in
19920Sstevel@tonic-gate * the system.
19930Sstevel@tonic-gate */
19940Sstevel@tonic-gate atomic_add_32(&deadman_panics, 1);
19950Sstevel@tonic-gate
19960Sstevel@tonic-gate if (!deadman_enabled) {
19970Sstevel@tonic-gate CPU->cpu_deadman_countdown = deadman_seconds;
19980Sstevel@tonic-gate return;
19990Sstevel@tonic-gate }
20000Sstevel@tonic-gate
20010Sstevel@tonic-gate /*
20020Sstevel@tonic-gate * If we're here, we want to bring the system down.
20030Sstevel@tonic-gate */
20040Sstevel@tonic-gate panic("deadman: timed out after %d seconds of clock "
20050Sstevel@tonic-gate "inactivity", deadman_seconds);
20060Sstevel@tonic-gate /*NOTREACHED*/
20070Sstevel@tonic-gate }
20080Sstevel@tonic-gate
20090Sstevel@tonic-gate /*ARGSUSED*/
20100Sstevel@tonic-gate static void
deadman_online(void * arg,cpu_t * cpu,cyc_handler_t * hdlr,cyc_time_t * when)20110Sstevel@tonic-gate deadman_online(void *arg, cpu_t *cpu, cyc_handler_t *hdlr, cyc_time_t *when)
20120Sstevel@tonic-gate {
201311066Srafael.vanoni@sun.com cpu->cpu_deadman_counter = 0;
20140Sstevel@tonic-gate cpu->cpu_deadman_countdown = deadman_seconds;
20150Sstevel@tonic-gate
20160Sstevel@tonic-gate hdlr->cyh_func = (cyc_func_t)deadman;
20170Sstevel@tonic-gate hdlr->cyh_level = CY_HIGH_LEVEL;
20180Sstevel@tonic-gate hdlr->cyh_arg = NULL;
20190Sstevel@tonic-gate
20200Sstevel@tonic-gate /*
20210Sstevel@tonic-gate * Stagger the CPUs so that they don't all run deadman() at
20220Sstevel@tonic-gate * the same time. Simplest reason to do this is to make it
20230Sstevel@tonic-gate * more likely that only one CPU will panic in case of a
20240Sstevel@tonic-gate * timeout. This is (strictly speaking) an aesthetic, not a
20250Sstevel@tonic-gate * technical consideration.
20260Sstevel@tonic-gate */
20270Sstevel@tonic-gate when->cyt_when = cpu->cpu_id * (NANOSEC / NCPU);
20280Sstevel@tonic-gate when->cyt_interval = NANOSEC;
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate
20310Sstevel@tonic-gate
20320Sstevel@tonic-gate void
deadman_init(void)20330Sstevel@tonic-gate deadman_init(void)
20340Sstevel@tonic-gate {
20350Sstevel@tonic-gate cyc_omni_handler_t hdlr;
20360Sstevel@tonic-gate
20370Sstevel@tonic-gate if (deadman_seconds == 0)
20380Sstevel@tonic-gate deadman_seconds = snoop_interval / MICROSEC;
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate if (snooping)
20410Sstevel@tonic-gate deadman_enabled = 1;
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate hdlr.cyo_online = deadman_online;
20440Sstevel@tonic-gate hdlr.cyo_offline = NULL;
20450Sstevel@tonic-gate hdlr.cyo_arg = NULL;
20460Sstevel@tonic-gate
20470Sstevel@tonic-gate mutex_enter(&cpu_lock);
20480Sstevel@tonic-gate deadman_cyclic = cyclic_add_omni(&hdlr);
20490Sstevel@tonic-gate mutex_exit(&cpu_lock);
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate
20520Sstevel@tonic-gate /*
20530Sstevel@tonic-gate * tod_fault() is for updating tod validate mechanism state:
20540Sstevel@tonic-gate * (1) TOD_NOFAULT: for resetting the state to 'normal'.
20550Sstevel@tonic-gate * currently used for debugging only
20560Sstevel@tonic-gate * (2) The following four cases detected by tod validate mechanism:
20570Sstevel@tonic-gate * TOD_REVERSED: current tod value is less than previous value.
20580Sstevel@tonic-gate * TOD_STALLED: current tod value hasn't advanced.
20590Sstevel@tonic-gate * TOD_JUMPED: current tod value advanced too far from previous value.
20600Sstevel@tonic-gate * TOD_RATECHANGED: the ratio between average tod delta and
20610Sstevel@tonic-gate * average tick delta has changed.
20625084Sjohnlev * (3) TOD_RDONLY: when the TOD clock is not writeable e.g. because it is
20635084Sjohnlev * a virtual TOD provided by a hypervisor.
20640Sstevel@tonic-gate */
20650Sstevel@tonic-gate enum tod_fault_type
tod_fault(enum tod_fault_type ftype,int off)20660Sstevel@tonic-gate tod_fault(enum tod_fault_type ftype, int off)
20670Sstevel@tonic-gate {
20680Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate if (tod_faulted != ftype) {
20710Sstevel@tonic-gate switch (ftype) {
20720Sstevel@tonic-gate case TOD_NOFAULT:
207378Sae112802 plat_tod_fault(TOD_NOFAULT);
20740Sstevel@tonic-gate cmn_err(CE_NOTE, "Restarted tracking "
20755076Smishra "Time of Day clock.");
20760Sstevel@tonic-gate tod_faulted = ftype;
20770Sstevel@tonic-gate break;
20780Sstevel@tonic-gate case TOD_REVERSED:
20790Sstevel@tonic-gate case TOD_JUMPED:
20800Sstevel@tonic-gate if (tod_faulted == TOD_NOFAULT) {
208178Sae112802 plat_tod_fault(ftype);
20820Sstevel@tonic-gate cmn_err(CE_WARN, "Time of Day clock error: "
20830Sstevel@tonic-gate "reason [%s by 0x%x]. -- "
20840Sstevel@tonic-gate " Stopped tracking Time Of Day clock.",
20850Sstevel@tonic-gate tod_fault_table[ftype], off);
20860Sstevel@tonic-gate tod_faulted = ftype;
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate break;
20890Sstevel@tonic-gate case TOD_STALLED:
20900Sstevel@tonic-gate case TOD_RATECHANGED:
20910Sstevel@tonic-gate if (tod_faulted == TOD_NOFAULT) {
209278Sae112802 plat_tod_fault(ftype);
20930Sstevel@tonic-gate cmn_err(CE_WARN, "Time of Day clock error: "
20940Sstevel@tonic-gate "reason [%s]. -- "
20950Sstevel@tonic-gate " Stopped tracking Time Of Day clock.",
20960Sstevel@tonic-gate tod_fault_table[ftype]);
20970Sstevel@tonic-gate tod_faulted = ftype;
20980Sstevel@tonic-gate }
20990Sstevel@tonic-gate break;
21005084Sjohnlev case TOD_RDONLY:
21015084Sjohnlev if (tod_faulted == TOD_NOFAULT) {
21025084Sjohnlev plat_tod_fault(ftype);
21035084Sjohnlev cmn_err(CE_NOTE, "!Time of Day clock is "
21045084Sjohnlev "Read-Only; set of Date/Time will not "
21055084Sjohnlev "persist across reboot.");
21065084Sjohnlev tod_faulted = ftype;
21075084Sjohnlev }
21085084Sjohnlev break;
21090Sstevel@tonic-gate default:
21100Sstevel@tonic-gate break;
21110Sstevel@tonic-gate }
21120Sstevel@tonic-gate }
21130Sstevel@tonic-gate return (tod_faulted);
21140Sstevel@tonic-gate }
21150Sstevel@tonic-gate
211611752STrevor.Thompson@Sun.COM /*
211711752STrevor.Thompson@Sun.COM * Two functions that allow tod_status_flag to be manipulated by functions
211811752STrevor.Thompson@Sun.COM * external to this file.
211911752STrevor.Thompson@Sun.COM */
212011752STrevor.Thompson@Sun.COM
21210Sstevel@tonic-gate void
tod_status_set(int tod_flag)212211752STrevor.Thompson@Sun.COM tod_status_set(int tod_flag)
21230Sstevel@tonic-gate {
212411752STrevor.Thompson@Sun.COM tod_status_flag |= tod_flag;
212511752STrevor.Thompson@Sun.COM }
212611752STrevor.Thompson@Sun.COM
212711752STrevor.Thompson@Sun.COM void
tod_status_clear(int tod_flag)212811752STrevor.Thompson@Sun.COM tod_status_clear(int tod_flag)
212911752STrevor.Thompson@Sun.COM {
213011752STrevor.Thompson@Sun.COM tod_status_flag &= ~tod_flag;
21310Sstevel@tonic-gate }
21320Sstevel@tonic-gate
213311752STrevor.Thompson@Sun.COM /*
213411752STrevor.Thompson@Sun.COM * Record a timestamp and the value passed to tod_set(). The next call to
213511752STrevor.Thompson@Sun.COM * tod_validate() can use these values, prev_set_tick and prev_set_tod,
213611752STrevor.Thompson@Sun.COM * when checking the timestruc_t returned by tod_get(). Ordinarily,
213711752STrevor.Thompson@Sun.COM * tod_validate() will use prev_tick and prev_tod for this task but these
213811752STrevor.Thompson@Sun.COM * become obsolete, and will be re-assigned with the prev_set_* values,
213911752STrevor.Thompson@Sun.COM * in the case when the TOD is re-written.
214011752STrevor.Thompson@Sun.COM */
214111752STrevor.Thompson@Sun.COM void
tod_set_prev(timestruc_t ts)214211752STrevor.Thompson@Sun.COM tod_set_prev(timestruc_t ts)
214311752STrevor.Thompson@Sun.COM {
214411752STrevor.Thompson@Sun.COM if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
214511752STrevor.Thompson@Sun.COM tod_validate_deferred) {
214611752STrevor.Thompson@Sun.COM return;
214711752STrevor.Thompson@Sun.COM }
214811752STrevor.Thompson@Sun.COM prev_set_tick = gethrtime();
214911752STrevor.Thompson@Sun.COM /*
215011752STrevor.Thompson@Sun.COM * A negative value will be set to zero in utc_to_tod() so we fake
215111752STrevor.Thompson@Sun.COM * a zero here in such a case. This would need to change if the
215211752STrevor.Thompson@Sun.COM * behavior of utc_to_tod() changes.
215311752STrevor.Thompson@Sun.COM */
215411752STrevor.Thompson@Sun.COM prev_set_tod = ts.tv_sec < 0 ? 0 : ts.tv_sec;
215511752STrevor.Thompson@Sun.COM }
21560Sstevel@tonic-gate
21570Sstevel@tonic-gate /*
21580Sstevel@tonic-gate * tod_validate() is used for checking values returned by tod_get().
21590Sstevel@tonic-gate * Four error cases can be detected by this routine:
21600Sstevel@tonic-gate * TOD_REVERSED: current tod value is less than previous.
21610Sstevel@tonic-gate * TOD_STALLED: current tod value hasn't advanced.
21620Sstevel@tonic-gate * TOD_JUMPED: current tod value advanced too far from previous value.
21630Sstevel@tonic-gate * TOD_RATECHANGED: the ratio between average tod delta and
21640Sstevel@tonic-gate * average tick delta has changed.
21650Sstevel@tonic-gate */
21660Sstevel@tonic-gate time_t
tod_validate(time_t tod)21670Sstevel@tonic-gate tod_validate(time_t tod)
21680Sstevel@tonic-gate {
21690Sstevel@tonic-gate time_t diff_tod;
21700Sstevel@tonic-gate hrtime_t diff_tick;
21710Sstevel@tonic-gate
21720Sstevel@tonic-gate long dtick;
21730Sstevel@tonic-gate int dtick_delta;
21740Sstevel@tonic-gate
21750Sstevel@tonic-gate int off = 0;
21760Sstevel@tonic-gate enum tod_fault_type tod_bad = TOD_NOFAULT;
21770Sstevel@tonic-gate
21780Sstevel@tonic-gate static int firsttime = 1;
21790Sstevel@tonic-gate
21800Sstevel@tonic-gate static time_t prev_tod = 0;
21810Sstevel@tonic-gate static hrtime_t prev_tick = 0;
21820Sstevel@tonic-gate static long dtick_avg = TOD_REF_FREQ;
21830Sstevel@tonic-gate
218411752STrevor.Thompson@Sun.COM int cpr_resume_done = 0;
218511752STrevor.Thompson@Sun.COM int dr_resume_done = 0;
218611752STrevor.Thompson@Sun.COM
21870Sstevel@tonic-gate hrtime_t tick = gethrtime();
21880Sstevel@tonic-gate
21890Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
21900Sstevel@tonic-gate
21910Sstevel@tonic-gate /*
21920Sstevel@tonic-gate * tod_validate_enable is patchable via /etc/system.
2193950Ssethg * If TOD is already faulted, or if TOD validation is deferred,
2194950Ssethg * there is nothing to do.
21950Sstevel@tonic-gate */
2196950Ssethg if ((tod_validate_enable == 0) || (tod_faulted != TOD_NOFAULT) ||
2197950Ssethg tod_validate_deferred) {
21980Sstevel@tonic-gate return (tod);
21990Sstevel@tonic-gate }
22000Sstevel@tonic-gate
22010Sstevel@tonic-gate /*
220211752STrevor.Thompson@Sun.COM * If this is the first time through, we just need to save the tod
220311752STrevor.Thompson@Sun.COM * we were called with and hrtime so we can use them next time to
220411752STrevor.Thompson@Sun.COM * validate tod_get().
22050Sstevel@tonic-gate */
22060Sstevel@tonic-gate if (firsttime) {
22070Sstevel@tonic-gate firsttime = 0;
22080Sstevel@tonic-gate prev_tod = tod;
22090Sstevel@tonic-gate prev_tick = tick;
22100Sstevel@tonic-gate return (tod);
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate
22130Sstevel@tonic-gate /*
221411752STrevor.Thompson@Sun.COM * Handle any flags that have been turned on by tod_status_set().
221511752STrevor.Thompson@Sun.COM * In the case where a tod_set() is done and then a subsequent
221611752STrevor.Thompson@Sun.COM * tod_get() fails (ie, both TOD_SET_DONE and TOD_GET_FAILED are
221711752STrevor.Thompson@Sun.COM * true), we treat the TOD_GET_FAILED with precedence by switching
221811752STrevor.Thompson@Sun.COM * off the flag, returning tod and leaving TOD_SET_DONE asserted
221911752STrevor.Thompson@Sun.COM * until such time as tod_get() completes successfully.
22200Sstevel@tonic-gate */
222111752STrevor.Thompson@Sun.COM if (tod_status_flag & TOD_GET_FAILED) {
222211752STrevor.Thompson@Sun.COM /*
222311752STrevor.Thompson@Sun.COM * tod_get() has encountered an issue, possibly transitory,
222411752STrevor.Thompson@Sun.COM * when reading TOD. We'll just return the incoming tod
222511752STrevor.Thompson@Sun.COM * value (which is actually hrestime.tv_sec in this case)
222611752STrevor.Thompson@Sun.COM * and when we get a genuine tod, following a successful
222711752STrevor.Thompson@Sun.COM * tod_get(), we can validate using prev_tod and prev_tick.
222811752STrevor.Thompson@Sun.COM */
222911752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_GET_FAILED;
223011752STrevor.Thompson@Sun.COM return (tod);
223111752STrevor.Thompson@Sun.COM } else if (tod_status_flag & TOD_SET_DONE) {
223211752STrevor.Thompson@Sun.COM /*
223311752STrevor.Thompson@Sun.COM * TOD has been modified. Just before the TOD was written,
223411752STrevor.Thompson@Sun.COM * tod_set_prev() saved tod and hrtime; we can now use
223511752STrevor.Thompson@Sun.COM * those values, prev_set_tod and prev_set_tick, to validate
223611752STrevor.Thompson@Sun.COM * the incoming tod that's just been read.
223711752STrevor.Thompson@Sun.COM */
223811752STrevor.Thompson@Sun.COM prev_tod = prev_set_tod;
223911752STrevor.Thompson@Sun.COM prev_tick = prev_set_tick;
22400Sstevel@tonic-gate dtick_avg = TOD_REF_FREQ;
224111752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_SET_DONE;
224211752STrevor.Thompson@Sun.COM /*
224311752STrevor.Thompson@Sun.COM * If a tod_set() preceded a cpr_suspend() without an
224411752STrevor.Thompson@Sun.COM * intervening tod_validate(), we need to ensure that a
224511752STrevor.Thompson@Sun.COM * TOD_JUMPED condition is ignored.
224611752STrevor.Thompson@Sun.COM * Note this isn't a concern in the case of DR as we've
224711752STrevor.Thompson@Sun.COM * just reassigned dtick_avg, above.
224811752STrevor.Thompson@Sun.COM */
224911752STrevor.Thompson@Sun.COM if (tod_status_flag & TOD_CPR_RESUME_DONE) {
225011752STrevor.Thompson@Sun.COM cpr_resume_done = 1;
225111752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_CPR_RESUME_DONE;
225211752STrevor.Thompson@Sun.COM }
225311752STrevor.Thompson@Sun.COM } else if (tod_status_flag & TOD_CPR_RESUME_DONE) {
225411752STrevor.Thompson@Sun.COM /*
225511752STrevor.Thompson@Sun.COM * The system's coming back from a checkpoint resume.
225611752STrevor.Thompson@Sun.COM */
225711752STrevor.Thompson@Sun.COM cpr_resume_done = 1;
225811752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_CPR_RESUME_DONE;
225911752STrevor.Thompson@Sun.COM /*
226011752STrevor.Thompson@Sun.COM * We need to handle the possibility of a CPR suspend
226111752STrevor.Thompson@Sun.COM * operation having been initiated whilst a DR event was
226211752STrevor.Thompson@Sun.COM * in-flight.
226311752STrevor.Thompson@Sun.COM */
226411752STrevor.Thompson@Sun.COM if (tod_status_flag & TOD_DR_RESUME_DONE) {
226511752STrevor.Thompson@Sun.COM dr_resume_done = 1;
226611752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_DR_RESUME_DONE;
226711752STrevor.Thompson@Sun.COM }
226811752STrevor.Thompson@Sun.COM } else if (tod_status_flag & TOD_DR_RESUME_DONE) {
226911752STrevor.Thompson@Sun.COM /*
227011752STrevor.Thompson@Sun.COM * A Dynamic Reconfiguration event has taken place.
227111752STrevor.Thompson@Sun.COM */
227211752STrevor.Thompson@Sun.COM dr_resume_done = 1;
227311752STrevor.Thompson@Sun.COM tod_status_flag &= ~TOD_DR_RESUME_DONE;
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate
22760Sstevel@tonic-gate /* test hook */
22770Sstevel@tonic-gate switch (tod_unit_test) {
22780Sstevel@tonic-gate case 1: /* for testing jumping tod */
22790Sstevel@tonic-gate tod += tod_test_injector;
22800Sstevel@tonic-gate tod_unit_test = 0;
22810Sstevel@tonic-gate break;
22820Sstevel@tonic-gate case 2: /* for testing stuck tod bit */
22830Sstevel@tonic-gate tod |= 1 << tod_test_injector;
22840Sstevel@tonic-gate tod_unit_test = 0;
22850Sstevel@tonic-gate break;
22860Sstevel@tonic-gate case 3: /* for testing stalled tod */
22870Sstevel@tonic-gate tod = prev_tod;
22880Sstevel@tonic-gate tod_unit_test = 0;
22890Sstevel@tonic-gate break;
22900Sstevel@tonic-gate case 4: /* reset tod fault status */
22910Sstevel@tonic-gate (void) tod_fault(TOD_NOFAULT, 0);
22920Sstevel@tonic-gate tod_unit_test = 0;
22930Sstevel@tonic-gate break;
22940Sstevel@tonic-gate default:
22950Sstevel@tonic-gate break;
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate
22980Sstevel@tonic-gate diff_tod = tod - prev_tod;
22990Sstevel@tonic-gate diff_tick = tick - prev_tick;
23000Sstevel@tonic-gate
23010Sstevel@tonic-gate ASSERT(diff_tick >= 0);
23020Sstevel@tonic-gate
23030Sstevel@tonic-gate if (diff_tod < 0) {
23040Sstevel@tonic-gate /* ERROR - tod reversed */
23050Sstevel@tonic-gate tod_bad = TOD_REVERSED;
23060Sstevel@tonic-gate off = (int)(prev_tod - tod);
23070Sstevel@tonic-gate } else if (diff_tod == 0) {
23080Sstevel@tonic-gate /* tod did not advance */
23090Sstevel@tonic-gate if (diff_tick > TOD_STALL_THRESHOLD) {
23100Sstevel@tonic-gate /* ERROR - tod stalled */
23110Sstevel@tonic-gate tod_bad = TOD_STALLED;
23120Sstevel@tonic-gate } else {
23130Sstevel@tonic-gate /*
23140Sstevel@tonic-gate * Make sure we don't update prev_tick
23150Sstevel@tonic-gate * so that diff_tick is calculated since
23160Sstevel@tonic-gate * the first diff_tod == 0
23170Sstevel@tonic-gate */
23180Sstevel@tonic-gate return (tod);
23190Sstevel@tonic-gate }
23200Sstevel@tonic-gate } else {
23210Sstevel@tonic-gate /* calculate dtick */
23220Sstevel@tonic-gate dtick = diff_tick / diff_tod;
23230Sstevel@tonic-gate
23240Sstevel@tonic-gate /* update dtick averages */
23250Sstevel@tonic-gate dtick_avg += ((dtick - dtick_avg) / TOD_FILTER_N);
23260Sstevel@tonic-gate
23270Sstevel@tonic-gate /*
23280Sstevel@tonic-gate * Calculate dtick_delta as
23290Sstevel@tonic-gate * variation from reference freq in quartiles
23300Sstevel@tonic-gate */
23310Sstevel@tonic-gate dtick_delta = (dtick_avg - TOD_REF_FREQ) /
23325076Smishra (TOD_REF_FREQ >> 2);
23330Sstevel@tonic-gate
23340Sstevel@tonic-gate /*
23350Sstevel@tonic-gate * Even with a perfectly functioning TOD device,
23360Sstevel@tonic-gate * when the number of elapsed seconds is low the
23370Sstevel@tonic-gate * algorithm can calculate a rate that is beyond
23380Sstevel@tonic-gate * tolerance, causing an error. The algorithm is
23390Sstevel@tonic-gate * inaccurate when elapsed time is low (less than
23400Sstevel@tonic-gate * 5 seconds).
23410Sstevel@tonic-gate */
23420Sstevel@tonic-gate if (diff_tod > 4) {
23430Sstevel@tonic-gate if (dtick < TOD_JUMP_THRESHOLD) {
234411752STrevor.Thompson@Sun.COM /*
234511752STrevor.Thompson@Sun.COM * If we've just done a CPR resume, we detect
234611752STrevor.Thompson@Sun.COM * a jump in the TOD but, actually, what's
234711752STrevor.Thompson@Sun.COM * happened is that the TOD has been increasing
234811752STrevor.Thompson@Sun.COM * whilst the system was suspended and the tick
234911752STrevor.Thompson@Sun.COM * count hasn't kept up. We consider the first
235011752STrevor.Thompson@Sun.COM * occurrence of this after a resume as normal
235111752STrevor.Thompson@Sun.COM * and ignore it; otherwise, in a non-resume
235211752STrevor.Thompson@Sun.COM * case, we regard it as a TOD problem.
235311752STrevor.Thompson@Sun.COM */
235411752STrevor.Thompson@Sun.COM if (!cpr_resume_done) {
235511752STrevor.Thompson@Sun.COM /* ERROR - tod jumped */
235611752STrevor.Thompson@Sun.COM tod_bad = TOD_JUMPED;
235711752STrevor.Thompson@Sun.COM off = (int)diff_tod;
235811752STrevor.Thompson@Sun.COM }
235911752STrevor.Thompson@Sun.COM }
236011752STrevor.Thompson@Sun.COM if (dtick_delta) {
236111752STrevor.Thompson@Sun.COM /*
236211752STrevor.Thompson@Sun.COM * If we've just done a DR resume, dtick_avg
236311752STrevor.Thompson@Sun.COM * can go a bit askew so we reset it and carry
236411752STrevor.Thompson@Sun.COM * on; otherwise, the TOD is in error.
236511752STrevor.Thompson@Sun.COM */
236611752STrevor.Thompson@Sun.COM if (dr_resume_done) {
236711752STrevor.Thompson@Sun.COM dtick_avg = TOD_REF_FREQ;
236811752STrevor.Thompson@Sun.COM } else {
236911752STrevor.Thompson@Sun.COM /* ERROR - change in clock rate */
237011752STrevor.Thompson@Sun.COM tod_bad = TOD_RATECHANGED;
237111752STrevor.Thompson@Sun.COM }
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate }
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate
23760Sstevel@tonic-gate if (tod_bad != TOD_NOFAULT) {
23770Sstevel@tonic-gate (void) tod_fault(tod_bad, off);
23780Sstevel@tonic-gate
23790Sstevel@tonic-gate /*
23800Sstevel@tonic-gate * Disable dosynctodr since we are going to fault
23810Sstevel@tonic-gate * the TOD chip anyway here
23820Sstevel@tonic-gate */
23830Sstevel@tonic-gate dosynctodr = 0;
23840Sstevel@tonic-gate
23850Sstevel@tonic-gate /*
23860Sstevel@tonic-gate * Set tod to the correct value from hrestime
23870Sstevel@tonic-gate */
23880Sstevel@tonic-gate tod = hrestime.tv_sec;
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate prev_tod = tod;
23920Sstevel@tonic-gate prev_tick = tick;
23930Sstevel@tonic-gate return (tod);
23940Sstevel@tonic-gate }
23950Sstevel@tonic-gate
23960Sstevel@tonic-gate static void
calcloadavg(int nrun,uint64_t * hp_ave)23970Sstevel@tonic-gate calcloadavg(int nrun, uint64_t *hp_ave)
23980Sstevel@tonic-gate {
23990Sstevel@tonic-gate static int64_t f[3] = { 135, 27, 9 };
24000Sstevel@tonic-gate uint_t i;
24010Sstevel@tonic-gate int64_t q, r;
24020Sstevel@tonic-gate
24030Sstevel@tonic-gate /*
24040Sstevel@tonic-gate * Compute load average over the last 1, 5, and 15 minutes
24050Sstevel@tonic-gate * (60, 300, and 900 seconds). The constants in f[3] are for
24060Sstevel@tonic-gate * exponential decay:
24070Sstevel@tonic-gate * (1 - exp(-1/60)) << 13 = 135,
24080Sstevel@tonic-gate * (1 - exp(-1/300)) << 13 = 27,
24090Sstevel@tonic-gate * (1 - exp(-1/900)) << 13 = 9.
24100Sstevel@tonic-gate */
24110Sstevel@tonic-gate
24120Sstevel@tonic-gate /*
24130Sstevel@tonic-gate * a little hoop-jumping to avoid integer overflow
24140Sstevel@tonic-gate */
24150Sstevel@tonic-gate for (i = 0; i < 3; i++) {
24160Sstevel@tonic-gate q = (hp_ave[i] >> 16) << 7;
24170Sstevel@tonic-gate r = (hp_ave[i] & 0xffff) << 7;
24180Sstevel@tonic-gate hp_ave[i] += ((nrun - q) * f[i] - ((r * f[i]) >> 16)) >> 4;
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate }
242111066Srafael.vanoni@sun.com
242211066Srafael.vanoni@sun.com /*
242311066Srafael.vanoni@sun.com * lbolt_hybrid() is used by ddi_get_lbolt() and ddi_get_lbolt64() to
242411066Srafael.vanoni@sun.com * calculate the value of lbolt according to the current mode. In the event
242511066Srafael.vanoni@sun.com * driven mode (the default), lbolt is calculated by dividing the current hires
242611066Srafael.vanoni@sun.com * time by the number of nanoseconds per clock tick. In the cyclic driven mode
242711066Srafael.vanoni@sun.com * an internal variable is incremented at each firing of the lbolt cyclic
242811066Srafael.vanoni@sun.com * and returned by lbolt_cyclic_driven().
242911066Srafael.vanoni@sun.com *
243011066Srafael.vanoni@sun.com * The system will transition from event to cyclic driven mode when the number
243111066Srafael.vanoni@sun.com * of calls to lbolt_event_driven() exceeds the (per CPU) threshold within a
243211066Srafael.vanoni@sun.com * window of time. It does so by reprograming lbolt_cyclic from CY_INFINITY to
243311066Srafael.vanoni@sun.com * nsec_per_tick. The lbolt cyclic will remain ON while at least one CPU is
243411066Srafael.vanoni@sun.com * causing enough activity to cross the thresholds.
243511066Srafael.vanoni@sun.com */
243611226Srafael.vanoni@sun.com int64_t
lbolt_bootstrap(void)243711066Srafael.vanoni@sun.com lbolt_bootstrap(void)
243811066Srafael.vanoni@sun.com {
243911066Srafael.vanoni@sun.com return (0);
244011066Srafael.vanoni@sun.com }
244111066Srafael.vanoni@sun.com
244211066Srafael.vanoni@sun.com /* ARGSUSED */
244311066Srafael.vanoni@sun.com uint_t
lbolt_ev_to_cyclic(caddr_t arg1,caddr_t arg2)244411066Srafael.vanoni@sun.com lbolt_ev_to_cyclic(caddr_t arg1, caddr_t arg2)
244511066Srafael.vanoni@sun.com {
244611066Srafael.vanoni@sun.com hrtime_t ts, exp;
244711066Srafael.vanoni@sun.com int ret;
244811066Srafael.vanoni@sun.com
244911066Srafael.vanoni@sun.com ASSERT(lbolt_hybrid != lbolt_cyclic_driven);
245011066Srafael.vanoni@sun.com
245111066Srafael.vanoni@sun.com kpreempt_disable();
245211066Srafael.vanoni@sun.com
245311066Srafael.vanoni@sun.com ts = gethrtime();
245411066Srafael.vanoni@sun.com lb_info->lbi_internal = (ts/nsec_per_tick);
245511066Srafael.vanoni@sun.com
245611066Srafael.vanoni@sun.com /*
245711066Srafael.vanoni@sun.com * Align the next expiration to a clock tick boundary.
245811066Srafael.vanoni@sun.com */
245911066Srafael.vanoni@sun.com exp = ts + nsec_per_tick - 1;
246011066Srafael.vanoni@sun.com exp = (exp/nsec_per_tick) * nsec_per_tick;
246111066Srafael.vanoni@sun.com
246211151Srafael.vanoni@sun.com ret = cyclic_reprogram(lb_info->id.lbi_cyclic_id, exp);
246311066Srafael.vanoni@sun.com ASSERT(ret);
246411066Srafael.vanoni@sun.com
246511066Srafael.vanoni@sun.com lbolt_hybrid = lbolt_cyclic_driven;
246611066Srafael.vanoni@sun.com lb_info->lbi_cyc_deactivate = B_FALSE;
246711066Srafael.vanoni@sun.com lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
246811066Srafael.vanoni@sun.com
246911066Srafael.vanoni@sun.com kpreempt_enable();
247011066Srafael.vanoni@sun.com
247111066Srafael.vanoni@sun.com ret = atomic_dec_32_nv(&lb_info->lbi_token);
247211066Srafael.vanoni@sun.com ASSERT(ret == 0);
247311066Srafael.vanoni@sun.com
247411066Srafael.vanoni@sun.com return (1);
247511066Srafael.vanoni@sun.com }
247611066Srafael.vanoni@sun.com
247711066Srafael.vanoni@sun.com int64_t
lbolt_event_driven(void)247811066Srafael.vanoni@sun.com lbolt_event_driven(void)
247911066Srafael.vanoni@sun.com {
248011066Srafael.vanoni@sun.com hrtime_t ts;
248111066Srafael.vanoni@sun.com int64_t lb;
248211066Srafael.vanoni@sun.com int ret, cpu = CPU->cpu_seqid;
248311066Srafael.vanoni@sun.com
248411066Srafael.vanoni@sun.com ts = gethrtime();
248511066Srafael.vanoni@sun.com ASSERT(ts > 0);
248611066Srafael.vanoni@sun.com
248711066Srafael.vanoni@sun.com ASSERT(nsec_per_tick > 0);
248811066Srafael.vanoni@sun.com lb = (ts/nsec_per_tick);
248911066Srafael.vanoni@sun.com
249011066Srafael.vanoni@sun.com /*
249111066Srafael.vanoni@sun.com * Switch to cyclic mode if the number of calls to this routine
249211066Srafael.vanoni@sun.com * has reached the threshold within the interval.
249311066Srafael.vanoni@sun.com */
249411066Srafael.vanoni@sun.com if ((lb - lb_cpu[cpu].lbc_cnt_start) < lb_info->lbi_thresh_interval) {
249511066Srafael.vanoni@sun.com
249611066Srafael.vanoni@sun.com if (--lb_cpu[cpu].lbc_counter == 0) {
249711066Srafael.vanoni@sun.com /*
249811066Srafael.vanoni@sun.com * Reached the threshold within the interval, reset
249911066Srafael.vanoni@sun.com * the usage statistics.
250011066Srafael.vanoni@sun.com */
250111066Srafael.vanoni@sun.com lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
250211066Srafael.vanoni@sun.com lb_cpu[cpu].lbc_cnt_start = lb;
250311066Srafael.vanoni@sun.com
250411066Srafael.vanoni@sun.com /*
250511066Srafael.vanoni@sun.com * Make sure only one thread reprograms the
250611066Srafael.vanoni@sun.com * lbolt cyclic and changes the mode.
250711066Srafael.vanoni@sun.com */
250811066Srafael.vanoni@sun.com if (panicstr == NULL &&
250911066Srafael.vanoni@sun.com atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
251011066Srafael.vanoni@sun.com
251111066Srafael.vanoni@sun.com if (lbolt_hybrid == lbolt_cyclic_driven) {
251211066Srafael.vanoni@sun.com ret = atomic_dec_32_nv(
251311066Srafael.vanoni@sun.com &lb_info->lbi_token);
251411066Srafael.vanoni@sun.com ASSERT(ret == 0);
251511496Srafael.vanoni@sun.com } else {
251611496Srafael.vanoni@sun.com lbolt_softint_post();
251711066Srafael.vanoni@sun.com }
251811066Srafael.vanoni@sun.com }
251911066Srafael.vanoni@sun.com }
252011066Srafael.vanoni@sun.com } else {
252111066Srafael.vanoni@sun.com /*
252211066Srafael.vanoni@sun.com * Exceeded the interval, reset the usage statistics.
252311066Srafael.vanoni@sun.com */
252411066Srafael.vanoni@sun.com lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
252511066Srafael.vanoni@sun.com lb_cpu[cpu].lbc_cnt_start = lb;
252611066Srafael.vanoni@sun.com }
252711066Srafael.vanoni@sun.com
252811066Srafael.vanoni@sun.com ASSERT(lb >= lb_info->lbi_debug_time);
252911066Srafael.vanoni@sun.com
253011066Srafael.vanoni@sun.com return (lb - lb_info->lbi_debug_time);
253111066Srafael.vanoni@sun.com }
253211066Srafael.vanoni@sun.com
253311066Srafael.vanoni@sun.com int64_t
lbolt_cyclic_driven(void)253411066Srafael.vanoni@sun.com lbolt_cyclic_driven(void)
253511066Srafael.vanoni@sun.com {
253611066Srafael.vanoni@sun.com int64_t lb = lb_info->lbi_internal;
253712233Srafael.vanoni@oracle.com int cpu;
253811066Srafael.vanoni@sun.com
253912233Srafael.vanoni@oracle.com /*
254012233Srafael.vanoni@oracle.com * If a CPU has already prevented the lbolt cyclic from deactivating
254112233Srafael.vanoni@oracle.com * itself, don't bother tracking the usage. Otherwise check if we're
254212233Srafael.vanoni@oracle.com * within the interval and how the per CPU counter is doing.
254312233Srafael.vanoni@oracle.com */
254412233Srafael.vanoni@oracle.com if (lb_info->lbi_cyc_deactivate) {
254512233Srafael.vanoni@oracle.com cpu = CPU->cpu_seqid;
254612233Srafael.vanoni@oracle.com if ((lb - lb_cpu[cpu].lbc_cnt_start) <
254712233Srafael.vanoni@oracle.com lb_info->lbi_thresh_interval) {
254811066Srafael.vanoni@sun.com
254912233Srafael.vanoni@oracle.com if (lb_cpu[cpu].lbc_counter == 0)
255012233Srafael.vanoni@oracle.com /*
255112233Srafael.vanoni@oracle.com * Reached the threshold within the interval,
255212233Srafael.vanoni@oracle.com * prevent the lbolt cyclic from turning itself
255312233Srafael.vanoni@oracle.com * off.
255412233Srafael.vanoni@oracle.com */
255512233Srafael.vanoni@oracle.com lb_info->lbi_cyc_deactivate = B_FALSE;
255612233Srafael.vanoni@oracle.com else
255712233Srafael.vanoni@oracle.com lb_cpu[cpu].lbc_counter--;
255812233Srafael.vanoni@oracle.com } else {
255911066Srafael.vanoni@sun.com /*
256012233Srafael.vanoni@oracle.com * Only reset the usage statistics when we have
256112233Srafael.vanoni@oracle.com * exceeded the interval.
256211066Srafael.vanoni@sun.com */
256312233Srafael.vanoni@oracle.com lb_cpu[cpu].lbc_counter = lb_info->lbi_thresh_calls;
256412233Srafael.vanoni@oracle.com lb_cpu[cpu].lbc_cnt_start = lb;
256512233Srafael.vanoni@oracle.com }
256611066Srafael.vanoni@sun.com }
256711066Srafael.vanoni@sun.com
256811066Srafael.vanoni@sun.com ASSERT(lb >= lb_info->lbi_debug_time);
256911066Srafael.vanoni@sun.com
257011066Srafael.vanoni@sun.com return (lb - lb_info->lbi_debug_time);
257111066Srafael.vanoni@sun.com }
257211066Srafael.vanoni@sun.com
257311066Srafael.vanoni@sun.com /*
257411226Srafael.vanoni@sun.com * The lbolt_cyclic() routine will fire at a nsec_per_tick interval to satisfy
257511066Srafael.vanoni@sun.com * performance needs of ddi_get_lbolt() and ddi_get_lbolt64() consumers.
257611066Srafael.vanoni@sun.com * It is inactive by default, and will be activated when switching from event
257711066Srafael.vanoni@sun.com * to cyclic driven lbolt. The cyclic will turn itself off unless signaled
257811066Srafael.vanoni@sun.com * by lbolt_cyclic_driven().
257911066Srafael.vanoni@sun.com */
258011066Srafael.vanoni@sun.com static void
lbolt_cyclic(void)258111066Srafael.vanoni@sun.com lbolt_cyclic(void)
258211066Srafael.vanoni@sun.com {
258311066Srafael.vanoni@sun.com int ret;
258411066Srafael.vanoni@sun.com
258511066Srafael.vanoni@sun.com lb_info->lbi_internal++;
258611066Srafael.vanoni@sun.com
258711066Srafael.vanoni@sun.com if (!lbolt_cyc_only) {
258811066Srafael.vanoni@sun.com
258911066Srafael.vanoni@sun.com if (lb_info->lbi_cyc_deactivate) {
259011066Srafael.vanoni@sun.com /*
259111066Srafael.vanoni@sun.com * Switching from cyclic to event driven mode.
259211066Srafael.vanoni@sun.com */
259312269Srafael.vanoni@oracle.com if (panicstr == NULL &&
259412269Srafael.vanoni@oracle.com atomic_cas_32(&lb_info->lbi_token, 0, 1) == 0) {
259511066Srafael.vanoni@sun.com
259611066Srafael.vanoni@sun.com if (lbolt_hybrid == lbolt_event_driven) {
259711066Srafael.vanoni@sun.com ret = atomic_dec_32_nv(
259811066Srafael.vanoni@sun.com &lb_info->lbi_token);
259911066Srafael.vanoni@sun.com ASSERT(ret == 0);
260011066Srafael.vanoni@sun.com return;
260111066Srafael.vanoni@sun.com }
260211066Srafael.vanoni@sun.com
260311066Srafael.vanoni@sun.com kpreempt_disable();
260411066Srafael.vanoni@sun.com
260511066Srafael.vanoni@sun.com lbolt_hybrid = lbolt_event_driven;
260611151Srafael.vanoni@sun.com ret = cyclic_reprogram(
260711151Srafael.vanoni@sun.com lb_info->id.lbi_cyclic_id,
260811066Srafael.vanoni@sun.com CY_INFINITY);
260911066Srafael.vanoni@sun.com ASSERT(ret);
261011066Srafael.vanoni@sun.com
261111066Srafael.vanoni@sun.com kpreempt_enable();
261211066Srafael.vanoni@sun.com
261311066Srafael.vanoni@sun.com ret = atomic_dec_32_nv(&lb_info->lbi_token);
261411066Srafael.vanoni@sun.com ASSERT(ret == 0);
261511066Srafael.vanoni@sun.com }
261611066Srafael.vanoni@sun.com }
261711066Srafael.vanoni@sun.com
261811066Srafael.vanoni@sun.com /*
261911066Srafael.vanoni@sun.com * The lbolt cyclic should not try to deactivate itself before
262011066Srafael.vanoni@sun.com * the sampling period has elapsed.
262111066Srafael.vanoni@sun.com */
262211066Srafael.vanoni@sun.com if (lb_info->lbi_internal - lb_info->lbi_cyc_deac_start >=
262311066Srafael.vanoni@sun.com lb_info->lbi_thresh_interval) {
262411066Srafael.vanoni@sun.com lb_info->lbi_cyc_deactivate = B_TRUE;
262511066Srafael.vanoni@sun.com lb_info->lbi_cyc_deac_start = lb_info->lbi_internal;
262611066Srafael.vanoni@sun.com }
262711066Srafael.vanoni@sun.com }
262811066Srafael.vanoni@sun.com }
262911066Srafael.vanoni@sun.com
263011066Srafael.vanoni@sun.com /*
263111066Srafael.vanoni@sun.com * Since the lbolt service was historically cyclic driven, it must be 'stopped'
263211066Srafael.vanoni@sun.com * when the system drops into the kernel debugger. lbolt_debug_entry() is
263311066Srafael.vanoni@sun.com * called by the KDI system claim callbacks to record a hires timestamp at
263411066Srafael.vanoni@sun.com * debug enter time. lbolt_debug_return() is called by the sistem release
263511066Srafael.vanoni@sun.com * callbacks to account for the time spent in the debugger. The value is then
263611066Srafael.vanoni@sun.com * accumulated in the lb_info structure and used by lbolt_event_driven() and
263711066Srafael.vanoni@sun.com * lbolt_cyclic_driven(), as well as the mdb_get_lbolt() routine.
263811066Srafael.vanoni@sun.com */
263911066Srafael.vanoni@sun.com void
lbolt_debug_entry(void)264011066Srafael.vanoni@sun.com lbolt_debug_entry(void)
264111066Srafael.vanoni@sun.com {
264211195Srafael.vanoni@sun.com if (lbolt_hybrid != lbolt_bootstrap) {
264311195Srafael.vanoni@sun.com ASSERT(lb_info != NULL);
264411195Srafael.vanoni@sun.com lb_info->lbi_debug_ts = gethrtime();
264511195Srafael.vanoni@sun.com }
264611066Srafael.vanoni@sun.com }
264711066Srafael.vanoni@sun.com
264811151Srafael.vanoni@sun.com /*
264911151Srafael.vanoni@sun.com * Calculate the time spent in the debugger and add it to the lbolt info
265011151Srafael.vanoni@sun.com * structure. We also update the internal lbolt value in case we were in
265111151Srafael.vanoni@sun.com * cyclic driven mode going in.
265211151Srafael.vanoni@sun.com */
265311066Srafael.vanoni@sun.com void
lbolt_debug_return(void)265411066Srafael.vanoni@sun.com lbolt_debug_return(void)
265511066Srafael.vanoni@sun.com {
265611151Srafael.vanoni@sun.com hrtime_t ts;
265711151Srafael.vanoni@sun.com
265811195Srafael.vanoni@sun.com if (lbolt_hybrid != lbolt_bootstrap) {
265911195Srafael.vanoni@sun.com ASSERT(lb_info != NULL);
266011195Srafael.vanoni@sun.com ASSERT(nsec_per_tick > 0);
266111195Srafael.vanoni@sun.com
266211151Srafael.vanoni@sun.com ts = gethrtime();
266311151Srafael.vanoni@sun.com lb_info->lbi_internal = (ts/nsec_per_tick);
266411066Srafael.vanoni@sun.com lb_info->lbi_debug_time +=
266511151Srafael.vanoni@sun.com ((ts - lb_info->lbi_debug_ts)/nsec_per_tick);
266611195Srafael.vanoni@sun.com
266711195Srafael.vanoni@sun.com lb_info->lbi_debug_ts = 0;
266811151Srafael.vanoni@sun.com }
266911066Srafael.vanoni@sun.com }
2670