/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#include <sys/tuneable.h>
#include <sys/sysmacros.h>
#include <sys/archsystm.h>
#include <sys/mem_cage.h>
#include <sys/ddi_periodic.h>
/*
* for NTP support
*/
#include <sys/inttypes.h>
#include <sys/clock_impl.h>
/*
* clock() is called straight from the clock cyclic; see clock_init().
*
* Functions:
* reprime clock
* maintain date
* jab the scheduler
*/
extern kcondvar_t fsflush_cv;
extern int idleswtch; /* flag set while idle in pswtch() */
extern hrtime_t volatile devinfo_freeze;
/*
* high-precision avenrun values. These are needed to make the
* regular avenrun values accurate.
*/
/*
* Phase/frequency-lock loop (PLL/FLL) definitions
*
* The following variables are read and set by the ntp_adjtime() system
* call.
*
* time_state shows the state of the system clock, with values defined
* in the timex.h header file.
*
* time_status shows the status of the system clock, with bits defined
* in the timex.h header file.
*
* increments.
*
* time_constant determines the bandwidth or "stiffness" of the PLL.
*
* time_tolerance determines maximum frequency error or tolerance of the
* CPU clock oscillator and is a property of the architecture; however,
* in principle it could change as result of the presence of external
* discipline signals, for instance.
*
* time_precision is usually equal to the kernel tick variable; however,
* in cases where a precision clock counter or external clock is
* available, the resolution can be much less than this and depend on
* whether the external clock is working or not.
*
* time_maxerror is initialized by a ntp_adjtime() call and increased by
* the kernel once each second to reflect the maximum error bound
* growth.
*
* time_esterror is set and read by the ntp_adjtime() call, but
* otherwise not used by the kernel.
*/
/*
* residual time and frequency offset of the local clock. The scale
* factors are defined in the timex.h header file.
*
* time_phase and time_freq are the phase increment and the frequency
* increment, respectively, of the kernel time variable.
*
* time_freq is set via ntp_adjtime() from a value stored in a file when
* the synchronization daemon is first started. Its value is retrieved
* via ntp_adjtime() and written to the file about once per hour by the
* daemon.
*
* time_adj is the adjustment added to the value of tick at each timer
* interrupt and is recomputed from time_phase and time_freq at each
* seconds rollover.
*
* time_reftime is the second's portion of the system time at the last
* call to ntp_adjtime(). It is used to adjust the time_freq variable
* and to increase the time_maxerror as the time since last update
* increases.
*/
/*
* The scale factors of the following variables are defined in the
* timex.h header file.
*
* pps_time contains the time at each calibration interval, as read by
* microtime(). pps_count counts the seconds of the calibration
* interval, the duration of which is nominally pps_shift in powers of
* two.
*
* pps_offset is the time offset produced by the time median filter
* pps_tf[], while pps_jitter is the dispersion (jitter) measured by
* this filter.
*
* pps_freq is the frequency offset produced by the frequency median
* filter pps_ff[], while pps_stabil is the dispersion (wander) measured
* by this filter.
*
* pps_usec is latched from a high resolution counter or external clock
* at pps_time. Here we want the hardware counter contents only, not the
* contents plus the time_tv.usec as usual.
*
* pps_valid counts the number of seconds since the last PPS update. It
* is used as a watchdog timer to disable the PPS discipline should the
* PPS signal be lost.
*
* pps_glitch counts the number of seconds since the beginning of an
* offset burst more than tick/2 from current nominal offset. It is used
* mainly to suppress error bursts due to priority conflicts between the
* PPS interrupt and timer interrupt.
*
* pps_intcnt counts the calibration intervals for use in the interval-
* adaptation algorithm. It's just too complicated for words.
*/
/*
* PPS signal quality monitors
*
* pps_jitcnt counts the seconds that have been discarded because the
* jitter measured by the time median filter exceeds the limit MAXTIME
* (100 us).
*
* pps_calcnt counts the frequency calibration intervals, which are
* variable from 4 s to 256 s.
*
* pps_errcnt counts the calibration intervals which have been discarded
* because the wander exceeds the limit MAXFREQ (100 ppm) or where the
* calibration interval jitter exceeds two ticks.
*
* pps_stbcnt counts the calibration intervals that have been discarded
* because the frequency wander exceeds the limit MAXFREQ / 4 (25 us).
*/
/*
* Hybrid lbolt implementation:
*
* The service historically provided by the lbolt and lbolt64 variables has
* been replaced by the ddi_get_lbolt() and ddi_get_lbolt64() routines, and the
* original symbols removed from the system. The once clock driven variables are
* now implemented in an event driven fashion, backed by gethrtime() coarsed to
* the appropriate clock resolution. The default event driven implementation is
* complemented by a cyclic driven one, active only during periods of intense
* activity around the DDI lbolt routines, when a lbolt specific cyclic is
* reprogramed to fire at a clock tick interval to serve consumers of lbolt who
* rely on the original low cost of consulting a memory position.
*
* The implementation uses the number of calls to these routines and the
* frequency of these to determine when to transition from event to cyclic
* driven and vice-versa. These values are kept on a per CPU basis for
* scalability reasons and to prevent CPUs from constantly invalidating a single
* cache line when modifying a global variable. The transition from event to
* cyclic mode happens once the thresholds are crossed, and activity on any CPU
* can cause such transition.
*
* The lbolt_hybrid function pointer is called by ddi_get_lbolt() and
* ddi_get_lbolt64(), and will point to lbolt_event_driven() or
* lbolt_cyclic_driven() according to the current mode. When the thresholds
* are exceeded, lbolt_event_driven() will reprogram the lbolt cyclic to
* fire at a nsec_per_tick interval and increment an internal variable at
* each firing. lbolt_hybrid will then point to lbolt_cyclic_driven(), which
* will simply return the value of such variable. lbolt_cyclic() will attempt
* to shut itself off at each threshold interval (sampling period for calls
* to the DDI lbolt routines), and return to the event driven mode, but will
* be prevented from doing so if lbolt_cyclic_driven() is being heavily used.
*
* lbolt_bootstrap is used during boot to serve lbolt consumers who don't wait
* for the cyclic subsystem to be intialized.
*
*/
int64_t lbolt_bootstrap(void);
int64_t lbolt_event_driven(void);
int64_t lbolt_cyclic_driven(void);
/*
* lbolt's cyclic, installed by clock_init().
*/
static void lbolt_cyclic(void);
/*
* Tunable to keep lbolt in cyclic driven mode. This will prevent the system
* from switching back to event driven, once it reaches cyclic mode.
*/
/*
* Cache aligned, per CPU structure with lbolt usage statistics.
*/
/*
* Single, cache aligned, structure with all the information required by
* the lbolt implementation.
*/
extern void clock_tick_schedule(int);
/*
* for tod fault detection
*/
int delay_from_interrupt_diagnose = 0;
/*
* On non-SPARC systems, TOD validation must be deferred until gethrtime
* returns non-zero values (after mach_clkinit's execution).
* On SPARC systems, it must be deferred until after hrtime_base
* and hres_last_tick are set (in the first invocation of hres_tick).
* Since in both cases the prerequisites occur before the invocation of
* tod_get() in clock(), the deferment is lifted there.
*/
/*
* tod_fault_table[] must be aligned with
* enum tod_fault_type in systm.h
*/
static char *tod_fault_table[] = {
"Reversed", /* TOD_REVERSED */
"Stalled", /* TOD_STALLED */
"Jumped", /* TOD_JUMPED */
"Changed in Clock Rate", /* TOD_RATECHANGED */
"Is Read-Only" /* TOD_RDONLY */
/*
* no strings needed for TOD_NOFAULT
*/
};
/*
* test hook for tod broken detection in tod_validate
*/
int tod_unit_test = 0;
static int adj_hist_entry;
static void calcloadavg(int, uint64_t *);
static int genloadavg(struct loadavg_s *);
static void loadavg_update();
extern clock_t clock_tick_proc_max;
static void
clock(void)
{
kthread_t *t;
extern void set_freemem();
void (*funcp)();
int s;
int do_lgrp_load;
int i;
if (panicstr)
return;
/*
* Make sure that 'freemem' do not drift too far from the truth
*/
set_freemem();
/*
* Before the section which is repeated is executed, we do
* the time delta processing which occurs every clock tick
*
* There is additional processing which happens every time
* the nanosecond counter rolls over which is described
* below - see the section which begins with : if (one_sec)
*
* This section marks the beginning of the precision-kernel
* code fragment.
*
* First, compute the phase adjustment. If the low-order bits
* (time_phase) of the update overflow, bump the higher order
* bits (time_update).
*/
time_phase += time_adj;
if (time_phase <= -FINEUSEC) {
s = hr_clock_lock();
hr_clock_unlock(s);
} else if (time_phase >= FINEUSEC) {
s = hr_clock_lock();
hr_clock_unlock(s);
}
/*
* End of precision-kernel code fragment which is processed
* every timer interrupt.
*
* Continue with the interrupt processing as scheduled.
*/
/*
* Count the number of runnable threads and the number waiting
* for some form of I/O to complete -- gets added to
* sysinfo.waiting. To know the state of the system, must add
* wait counts from all CPUs. Also add up the per-partition
* statistics.
*/
w_io = 0;
nrunnable = 0;
/*
*/
do_lgrp_load = 0;
lgrp_ticks = 0;
do_lgrp_load = 1;
}
if (one_sec) {
}
/*
* First count the threads waiting on kpreempt queues in each
* CPU partition.
*/
do {
cpupart->cp_updates++;
if (one_sec) {
cpupart->cp_nrunning = 0;
}
/* Now count the per-CPU statistics. */
do {
if (one_sec) {
/*
* Update user, system, and idle cpu times.
*/
cpupart->cp_nrunning++;
/*
* w_io is used to update sysinfo.waiting during
* one_second processing below. Only gather w_io
* information when we walk the list of cpus if we're
* going to perform one_second processing.
*/
}
/*
* Estimate interrupt load on this cpu each second.
* Computes cpu_intrload as %utilization (0-99).
*/
/* add up interrupt time from all micro states */
/* compute nsec used in the past second */
/* limit the value for safety (and the first pass) */
/* calculate %time in interrupt */
/* jump to new max, or decay the old max */
if (change < 0)
else if (change > 0)
}
if (do_lgrp_load &&
/*
* When updating the lgroup's load average,
* account for the thread running on the CPU.
* If the CPU is the current one, then we need
* to account for the underlying thread which
* got the clock interrupt not the thread that is
* handling the interrupt and caculating the load
* average
*/
t = cp->cpu_thread;
t = t->t_intr;
/*
* Account for the load average for this thread if
* it isn't the idle thread or it is on the interrupt
* stack and not the current CPU handling the clock
* interrupt
*/
CPU_ON_INTR(cp))) {
/* local thread */
} else {
/*
* This is a remote thread, charge it
* against its home lgroup. Note that
* we notice that a thread is remote
* only if it's currently executing.
* This is a reasonable approximation,
* since queued remote threads are rare.
* Note also that if we didn't charge
* it to its home lgroup, remote
* execution would often make a system
* appear balanced even though it was
* would often not be done correctly.
*/
lgrp_loadavg(t->t_lpl,
}
}
}
/*
* Check for a callout that needs be called from the clock
* thread to support the membership protocol in a clustered
* system. Copy the function pointer so that we can reset
* this to NULL if needed.
*/
(*funcp)();
(*funcp)();
/*
* Wakeup the cageout thread waiters once per second.
*/
if (one_sec)
kcage_tick();
if (one_sec) {
int s;
/*
* Beginning of precision-kernel code fragment executed
* every second.
*
* On rollover of the second the phase adjustment to be
* used for the next second is calculated. Also, the
* maximum error is increased by the tolerance. If the
* PPS frequency discipline code is present, the phase is
* increased to compensate for the CPU clock oscillator
* frequency error.
*
* On a 32-bit machine and given parameters in the timex.h
* header file, the maximum phase adjustment is +-512 ms
* and maximum frequency offset is (a tad less than)
* +-512 ppm. On a 64-bit machine, you shouldn't need to ask.
*/
/*
* Leap second processing. If in leap-insert state at
* the end of the day, the system clock is set back one
* second; if in leap-delete state, the system clock is
* set ahead one second. The microtime() routine or
* external clock driver will insure that reported time
* is always monotonic. The ugly divides should be
* replaced.
*/
switch (time_state) {
case TIME_OK:
if (time_status & STA_INS)
else if (time_status & STA_DEL)
break;
case TIME_INS:
s = hr_clock_lock();
hr_clock_unlock(s);
}
break;
case TIME_DEL:
s = hr_clock_lock();
hr_clock_unlock(s);
}
break;
case TIME_OOP:
break;
case TIME_WAIT:
default:
break;
}
/*
* Compute the phase adjustment for the next second. In
* PLL mode, the offset is reduced by a fixed factor
* times the time constant. In FLL mode the offset is
* used directly. In either mode, the maximum phase
* adjustment for each second is clamped so as to spread
* the adjustment over not more than the number of
* seconds between updates.
*/
if (time_offset == 0)
time_adj = 0;
else if (time_offset < 0) {
lltemp = -time_offset;
if (!(time_status & STA_FLL)) {
else
}
time_offset += lltemp;
} else {
if (!(time_status & STA_FLL)) {
else
}
time_offset -= lltemp;
}
/*
* Compute the frequency estimate and additional phase
* adjustment due to frequency error for the next
* second. When the PPS signal is engaged, gnaw on the
* watchdog counter and update the frequency computed by
* the pll and the PPS signal.
*/
pps_valid++;
}
if (lltemp)
/*
* End of precision kernel-code fragment
*
* The section below should be modified if we are planning
* to use NTP for synchronization.
*
* Note: the clock synchronization code now assumes
* the following:
* - if dosynctodr is 1, then compute the drift between
* the tod chip and software time and adjust one or
* the other depending on the circumstances
*
* - if dosynctodr is 0, then the tod chip is independent
* of the software clock and should not be adjusted,
* but allowed to free run. this allows NTP to sync.
* hrestime without any interference from the tod chip.
*/
int s;
if (absdrift > 2) {
s = hr_clock_lock();
membar_enter(); /* hrestime visible */
timedelta = 0;
timechanged++;
tod_needsync = 0;
hr_clock_unlock(s);
}
} else {
if (tod_needsync || !dosynctodr) {
gethrestime(&tod);
s = hr_clock_lock();
if (timedelta == 0)
tod_needsync = 0;
hr_clock_unlock(s);
} else {
/*
* If the drift is 2 seconds on the
* money, then the TOD is adjusting
* the clock; record that.
*/
s = hr_clock_lock();
hr_clock_unlock(s);
}
}
}
one_sec = 0;
/*
* Some drivers still depend on this... XXX
*/
{
/* Update ani_free */
set_anoninfo();
/* number of reserved and allocated pages */
#ifdef DEBUG
#endif
}
if (nrunnable) {
}
if (nswapped) {
}
/*
* Wake up fsflush to write out DELWRI
* buffers, dirty pages and other cached
* administrative data, e.g. inodes.
*/
if (--fsflushcnt <= 0) {
}
vmmeter();
for (i = 0; i < 3; i++)
/*
* At the moment avenrun[] can only hold 31
* bits of load average as it is a signed
* int in the API. We need to ensure that
* hp_avenrun[i] >> (16 - FSHIFT) will not be
* too large. If it is, we put the largest value
* that we can use into avenrun[i]. This is
* kludgey, but about all we can do until we
* avenrun[] is declared as an array of uint64[]
*/
(16 - FSHIFT));
else
avenrun[i] = 0x7fffffff;
do {
/*
* Wake up the swapper thread if necessary.
*/
if (runin ||
t = &t0;
thread_lock(t);
if (t->t_state == TS_STOPPED) {
wake_sched_sec = 0;
t->t_whystop = 0;
t->t_whatstop = 0;
t->t_schedflag &= ~TS_ALLSTART;
setfrontdq(t);
}
thread_unlock(t);
}
}
/*
* Wake up the swapper if any high priority swapped-out threads
* became runable during the last tick.
*/
if (wake_sched) {
t = &t0;
thread_lock(t);
if (t->t_state == TS_STOPPED) {
wake_sched = 0;
t->t_whystop = 0;
t->t_whatstop = 0;
t->t_schedflag &= ~TS_ALLSTART;
setfrontdq(t);
}
thread_unlock(t);
}
}
void
clock_init(void)
{
int i, sz;
/*
* Setup handler and timer for the clock cyclic.
*/
/*
* The lbolt cyclic will be reprogramed to fire at a nsec_per_tick
* interval to satisfy performance needs of the DDI lbolt consumers.
* It is off by default.
*/
/*
* Allocate cache line aligned space for the per CPU lbolt data and
* lbolt info structures, and initialize them with their default
* values. Note that these structures are also cache line sized.
*/
if (hz != HZ_DEFAULT)
else
for (i = 0; i < max_ncpus; i++)
/*
* Install the softint used to switch between event and cyclic driven
* lbolt. We use a soft interrupt to make sure the context of the
* cyclic reprogram call is safe.
*/
/*
* Since the hybrid lbolt implementation is based on a hardware counter
* that is reset at every hardware reboot and that we'd like to have
* the lbolt value starting at zero after both a hardware and a fast
* reboot, we calculate the number of clock ticks the system's been up
* and store it in the lbi_debug_time field of the lbolt info structure.
* The value of this field will be subtracted from lbolt before
* returning it.
*/
(gethrtime()/nsec_per_tick);
/*
* lbolt_hybrid points at lbolt_bootstrap until now. The LBOLT_* macros
* and lbolt_debug_{enter,return} use this value as an indication that
* the initializaion above hasn't been completed. Setting lbolt_hybrid
* to either lbolt_{cyclic,event}_driven here signals those code paths
* that the lbolt related structures can be used.
*/
if (lbolt_cyc_only) {
lbolt_when.cyt_when = 0;
} else {
}
/*
* Grab cpu_lock and install all three cyclics.
*/
}
/*
* Called before calcloadavg to get 10-sec moving loadavg together
*/
static int
{
int avg;
int i;
int slen;
/* 10-second snapshot, calculate first positon */
return (0);
}
}
return (avg);
}
/*
* Run every second from clock () to update the loadavg count available to the
* system and cpu-partitions.
*
* This works by sampling the previous usr, sys, wait time elapsed,
* computing a delta, and adding that delta to the elapsed usr, sys,
* wait increase.
*/
static void
{
int prev;
/*
* first pass totals up per-cpu statistics for system and cpu
* partitions
*/
do {
/* compute delta against last total */
cpu_total = 0;
} else {
if (cpu_total < 0)
cpu_total = 0;
}
/*
* Second pass updates counts
*/
do {
/*
* Third pass totals up per-zone statistics.
*/
}
/*
* clock_update() - local clock update
*
* This routine is called by ntp_adjtime() to update the local clock
* phase and frequency. The implementation is of an
* adaptive-parameter, hybrid phase/frequency-lock loop (PLL/FLL). The
* routine computes new time and frequency offset estimates for each
* call. The PPS signal itself determines the new time offset,
* instead of the calling argument. Presumably, calls to
* ntp_adjtime() occur only when the caller believes the local clock
* is valid within some bound (+-128 ms with NTP). If the caller's
* time is far different than the PPS time, an argument will ensue,
* and it's not clear who will lose.
*
* For uncompensated quartz crystal oscillatores and nominal update
* intervals less than 1024 s, operation should be in phase-lock mode
* (STA_FLL = 0), where the loop is disciplined to phase. For update
* intervals greater than this, operation should be in frequency-lock
* mode (STA_FLL = 1), where the loop is disciplined to frequency.
*
* Note: mutex(&tod_lock) is in effect.
*/
void
{
return;
ltemp = pps_offset;
/*
* Scale the phase adjustment and clamp to the operating range.
*/
else
/*
* Select whether the frequency is to be controlled and in which
* mode (PLL or FLL). Clamp to the operating range. Ugly
*/
if (time_status & STA_FLL) {
SCALE_UPDATE));
if (ltemp)
}
} else {
if (ltemp)
SCALE_USEC) / SCALE_KF)
}
}
if (time_freq > time_tolerance)
else if (time_freq < -time_tolerance)
s = hr_clock_lock();
tod_needsync = 1;
hr_clock_unlock(s);
}
/*
* ddi_hardpps() - discipline CPU clock oscillator to external PPS signal
*
* This routine is called at each PPS interrupt in order to discipline
* the CPU clock oscillator to the PPS signal. It measures the PPS phase
* and leaves it in a handy spot for the clock() routine. It
* integrates successive PPS phase differences and calculates the
* frequency offset. This is used in clock() to discipline the CPU
* clock oscillator so that intrinsic frequency error is cancelled out.
* The code requires the caller to capture the time and hardware counter
* value at the on-time PPS signal transition.
*
* Note that, on some Unix systems, this routine runs at an interrupt
* priority level higher than the timer interrupt routine clock().
* Therefore, the variables used are distinct from the clock()
* variables, except for certain exceptions: The PPS frequency pps_freq
* and phase pps_offset variables are determined by this routine and
* updated atomically. The time_tolerance variable can be considered a
* constant, since it is infrequently changed, and then only when the
* PPS signal is disabled. The watchdog counter pps_valid is updated
* once per second by clock() and is atomically cleared in this
* routine.
*
* tvp is the time of the last tick; usec is a microsecond count since the
* last tick.
*
* Note: In Solaris systems, the tick value is actually given by
* usec_per_tick. This is called from the serial driver cdintr(),
* or equivalent, at a high PIL. Because the kernel keeps a
* highresolution time, the following code can accept either
* the traditional argument pair, or the current highres timestamp
* in tvp and zero in usec.
*/
void
{
int cal_usec;
/*
* An occasional glitch can be produced when the PPS interrupt
* occurs in the clock() routine before the time variable is
* updated. Here the offset is discarded when the difference
* between it and the last one is greater than tick/2, but not
* if the interval since the first discard exceeds 30 s.
*/
pps_valid = 0;
if (v_usec < 0)
if (pps_glitch > MAXGLITCH) {
pps_glitch = 0;
} else {
pps_glitch++;
u_usec = pps_offset;
}
} else
pps_glitch = 0;
/*
* A three-stage median filter is used to help deglitch the pps
* time. The median sample becomes the time offset estimate; the
* difference between the other two samples becomes the time
* dispersion (jitter) estimate.
*/
} else {
}
} else {
} else {
}
}
pps_jitcnt++;
/*
* During the calibration interval adjust the starting time when
* the tick overflows. At the end of the interval compute the
* duration of the interval and the difference of the hardware
* counters at the beginning and end of the interval. This code
* is deliciously complicated by the fact valid differences may
* exceed the value of tick when using long calibration
* intervals and small ticks. Note that the counter can be
* greater than tick if caught at just the wrong instant, but
* the values returned and used here are correct.
*/
if (pps_usec < 0)
pps_count++;
return;
pps_count = 0;
pps_calcnt++;
if (v_usec < 0)
else
if (cal_usec < 0) {
cal_sec--;
}
/*
* Check for lost interrupts, noise, excessive jitter and
* excessive frequency error. The number of timer ticks during
* the interval may vary +-1 tick. Add to this a margin of one
* tick for the PPS signal jitter and maximum frequency
* deviation. If the limits are exceeded, the calibration
* interval is reset to the minimum and we start over.
*/
pps_errcnt++;
pps_intcnt = 0;
return;
}
/*
* A three-stage median filter is used to help deglitch the pps
* frequency. The median sample becomes the frequency offset
* estimate; the difference between the other two samples
* becomes the frequency dispersion (stability) estimate.
*/
} else {
}
} else {
} else {
}
}
/*
* Here the frequency dispersion (stability) is updated. If it
* is less than one-fourth the maximum (MAXFREQ), the frequency
* offset is updated as well, but clamped to the tolerance. It
* will be processed later by the clock() routine.
*/
if (v_usec < 0)
else
pps_stbcnt++;
return;
}
if (time_status & STA_PPSFREQ) {
if (u_usec < 0) {
if (pps_freq < -time_tolerance)
} else {
if (pps_freq > time_tolerance)
}
}
/*
* Here the calibration interval is adjusted. If the maximum
* time difference is greater than tick / 4, reduce the interval
* by half. If this is not the case for four consecutive
* intervals, double the interval.
*/
pps_intcnt = 0;
pps_shift--;
} else if (pps_intcnt >= 4) {
pps_intcnt = 0;
if (pps_shift < PPS_SHIFTMAX)
pps_shift++;
} else
pps_intcnt++;
/*
* If recovering from kmdb, then make sure the tod chip gets resynced.
* If we took an early exit above, then we don't yet have a stable
* calibration signal to lock onto, so don't mark the tod for sync
* until we get all the way here.
*/
{
int s = hr_clock_lock();
tod_needsync = 1;
hr_clock_unlock(s);
}
}
/*
* Handle clock tick processing for a thread.
* Check for timer action, enforce CPU rlimit, do profiling etc.
*/
void
{
int user_mode;
panic("clock_tick: no lwp");
/*NOTREACHED*/
}
for (i = 0; i < pending; i++) {
CL_TICK(t); /* Class specific tick processing */
}
/* pp->p_lock makes sure that the thread does not exit */
/*
* Update process times. Should use high res clock and state
* changes instead of statistical sampling method. XXX
*/
if (user_mode) {
} else {
}
/*
* Update user profiling statistics. Get the pc from the
* lwp when the AST happens.
*/
if (user_mode) {
poke = 1;
aston(t);
}
}
/*
* If CPU was in user state, process lwp-virtual time
* interval timer. The value passed to itimerdecr() has to be
* in microseconds and has to be less than one second. Hence
* this loop.
*/
while (total_usec > 0) {
if (user_mode &&
poke = 1;
}
total_usec -= usec;
}
/*
* If CPU was in user state, process lwp-profile
* interval timer.
*/
while (total_usec > 0) {
poke = 1;
}
total_usec -= usec;
}
/*
* Enforce CPU resource controls:
* (a) process.max-cpu-time resource control
*
* Perform the check only if we have accumulated more a second.
*/
}
/*
* (b) task.max-cpu-time resource control
*
* If we have accumulated enough ticks, increment the task CPU
* time usage and test for the resource limit. This minimizes the
* number of calls to the rct_test(). The task CPU time mutex
* is highly contentious as many processes can be sharing a task.
*/
if (secs) {
}
}
/*
* Update memory usage for the currently running process.
*/
/*
* Notify the CPU the thread is running on.
*/
}
void
{
int ticks;
do {
mutex_enter(&p->p_pflock);
/*
* Old-style profiling
*/
mutex_exit(&p->p_pflock);
return;
}
}
}
/*
* PC Sampling
*/
int result;
#ifdef __lint
#endif
while (ticks-- > 0) {
/* buffer full, turn off sampling */
break;
}
switch (SIZEOF_PTR(model)) {
case sizeof (uint32_t):
break;
#ifdef _LP64
case sizeof (uint64_t):
break;
#endif
default:
"data model");
result = -1;
break;
}
if (result != 0) {
break;
}
pr->pr_samples++;
}
}
mutex_exit(&p->p_pflock);
}
static void
{
mutex_enter(&t->t_delay_lock);
cv_signal(&t->t_delay_cv);
mutex_exit(&t->t_delay_lock);
}
/*
* The delay(9F) man page indicates that it can only be called from user or
* kernel context - detect and diagnose bad calls. The following macro will
* produce a limited number of messages identifying bad callers. This is done
* in a macro so that caller() is meaningful. When a bad caller is identified,
* switching to 'drv_usecwait(TICK_TO_USEC(ticks));' may be appropriate.
*/
#define DELAY_CONTEXT_CHECK() { \
uint32_t m; \
char *f; \
\
m = delay_from_interrupt_msg; \
if (delay_from_interrupt_diagnose && servicing_interrupt() && \
!panicstr && !devinfo_freeze && \
"interrupt context: %s`%s", \
} \
}
/*
* delay_common: common delay code.
*/
static void
{
/* If timeouts aren't running all we can do is spin. */
if (panicstr || devinfo_freeze) {
/* Convert delay(9F) call into drv_usecwait(9F) call. */
if (ticks > 0)
return;
}
mutex_enter(&t->t_delay_lock);
mutex_exit(&t->t_delay_lock);
(void) untimeout_default(id, 0);
}
}
/*
* Delay specified number of clock ticks.
*/
void
{
}
/*
* Delay a random number of clock ticks between 1 and ticks.
*/
void
{
int r;
(void) random_get_pseudo_bytes((void *)&r, sizeof (r));
if (ticks == 0)
ticks = 1;
}
/*
* Like delay, but interruptible by a signal.
*/
int
{
/* If timeouts aren't running all we can do is spin. */
if (panicstr || devinfo_freeze) {
if (ticks > 0)
return (0);
}
mutex_enter(&t->t_delay_lock);
do {
&t->t_delay_lock, deadline);
/* loop until past deadline or signaled */
} while (rc > 0);
mutex_exit(&t->t_delay_lock);
if (rc == 0)
return (EINTR);
return (0);
}
/*
* Initialize the system time based on the TOD chip. approx is used as
* an approximation of time (e.g. from the filesystem) in the event that
* the TOD chip has been cleared or is unresponsive. An approx of -1
* means the filesystem doesn't keep time.
*/
void
{
int spl;
int set_clock = 0;
/*
* If the TOD chip is reporting some time after 1971,
* then it probably didn't lose power or become otherwise
* cleared in the recent past; check to assure that
* the time coming from the filesystem isn't in the future
* according to the TOD chip.
*/
"than time on time-of-day chip; check date.");
}
} else {
/*
* If the TOD chip isn't giving correct time, set it to the
* greater of i) approx and ii) 1987. That way if approx
* is negative or is earlier than 1987, we set the clock
* back to a time when Oliver North, ALF and Dire Straits
* were all on the collective brain: 1987.
*/
/*
* Attempt to write the new time to the TOD chip. Set spl high
* to avoid getting preempted between the tod_set and tod_get.
*/
tod_broken = 1;
dosynctodr = 0;
} else {
"incorrect date; check and reset.");
}
set_clock = 1;
}
if (!boot_time) {
set_clock = 1;
}
if (set_clock)
set_hrestime(&ts);
}
void
{
membar_enter(); /* hrestime must be visible before timechanged++ */
timedelta = 0;
timechanged++;
}
static int deadman_enabled = 0;
static void
deadman(void)
{
if (panicstr) {
/*
* During panic, other CPUs besides the panic
* master continue to handle cyclics and some other
* interrupts. The code below is intended to be
* single threaded, so any CPU other than the master
* must keep out.
*/
return;
if (!deadman_panic_timers)
return; /* allow all timers to be manually disabled */
/*
* If we are generating a crash dump or syncing filesystems and
* the corresponding timer is set, decrement it and re-enter
* the panic code to abort it and advance to the next state.
* The panic states and triggers are explained in panic.c.
*/
if (panic_dump) {
if (dump_timeleft && (--dump_timeleft == 0)) {
panic("panic dump timeout");
/*NOTREACHED*/
}
} else if (panic_sync) {
if (sync_timeleft && (--sync_timeleft == 0)) {
panic("panic sync timeout");
/*NOTREACHED*/
}
}
return;
}
return;
}
if (--CPU->cpu_deadman_countdown > 0)
return;
/*
* Regardless of whether or not we actually bring the system down,
* bump the deadman_panics variable.
*
* N.B. deadman_panics is incremented once for each CPU that
* passes through here. It's expected that all the CPUs will
* detect this condition within one second of each other, so
* when deadman_enabled is off, deadman_panics will
* typically be a multiple of the total number of CPUs in
* the system.
*/
if (!deadman_enabled) {
return;
}
/*
* If we're here, we want to bring the system down.
*/
panic("deadman: timed out after %d seconds of clock "
"inactivity", deadman_seconds);
/*NOTREACHED*/
}
/*ARGSUSED*/
static void
{
cpu->cpu_deadman_counter = 0;
/*
* Stagger the CPUs so that they don't all run deadman() at
* the same time. Simplest reason to do this is to make it
* more likely that only one CPU will panic in case of a
* timeout. This is (strictly speaking) an aesthetic, not a
* technical consideration.
*/
}
void
deadman_init(void)
{
if (deadman_seconds == 0)
if (snooping)
deadman_enabled = 1;
}
/*
* tod_fault() is for updating tod validate mechanism state:
* (1) TOD_NOFAULT: for resetting the state to 'normal'.
* currently used for debugging only
* (2) The following four cases detected by tod validate mechanism:
* TOD_REVERSED: current tod value is less than previous value.
* TOD_STALLED: current tod value hasn't advanced.
* TOD_JUMPED: current tod value advanced too far from previous value.
* TOD_RATECHANGED: the ratio between average tod delta and
* average tick delta has changed.
* (3) TOD_RDONLY: when the TOD clock is not writeable e.g. because it is
* a virtual TOD provided by a hypervisor.
*/
enum tod_fault_type
{
if (tod_faulted != ftype) {
switch (ftype) {
case TOD_NOFAULT:
"Time of Day clock.");
tod_faulted = ftype;
break;
case TOD_REVERSED:
case TOD_JUMPED:
if (tod_faulted == TOD_NOFAULT) {
"reason [%s by 0x%x]. -- "
" Stopped tracking Time Of Day clock.",
tod_faulted = ftype;
}
break;
case TOD_STALLED:
case TOD_RATECHANGED:
if (tod_faulted == TOD_NOFAULT) {
"reason [%s]. -- "
" Stopped tracking Time Of Day clock.",
tod_faulted = ftype;
}
break;
case TOD_RDONLY:
if (tod_faulted == TOD_NOFAULT) {
"persist across reboot.");
tod_faulted = ftype;
}
break;
default:
break;
}
}
return (tod_faulted);
}
/*
* Two functions that allow tod_status_flag to be manipulated by functions
* external to this file.
*/
void
{
}
void
{
tod_status_flag &= ~tod_flag;
}
/*
* Record a timestamp and the value passed to tod_set(). The next call to
* tod_validate() can use these values, prev_set_tick and prev_set_tod,
* when checking the timestruc_t returned by tod_get(). Ordinarily,
* tod_validate() will use prev_tick and prev_tod for this task but these
* become obsolete, and will be re-assigned with the prev_set_* values,
* in the case when the TOD is re-written.
*/
void
{
return;
}
prev_set_tick = gethrtime();
/*
* A negative value will be set to zero in utc_to_tod() so we fake
* a zero here in such a case. This would need to change if the
* behavior of utc_to_tod() changes.
*/
}
/*
* tod_validate() is used for checking values returned by tod_get().
* Four error cases can be detected by this routine:
* TOD_REVERSED: current tod value is less than previous.
* TOD_STALLED: current tod value hasn't advanced.
* TOD_JUMPED: current tod value advanced too far from previous value.
* TOD_RATECHANGED: the ratio between average tod delta and
* average tick delta has changed.
*/
{
long dtick;
int dtick_delta;
int off = 0;
int cpr_resume_done = 0;
int dr_resume_done = 0;
/*
* If TOD is already faulted, or if TOD validation is deferred,
* there is nothing to do.
*/
return (tod);
}
/*
* If this is the first time through, we just need to save the tod
* we were called with and hrtime so we can use them next time to
* validate tod_get().
*/
if (firsttime) {
firsttime = 0;
return (tod);
}
/*
* Handle any flags that have been turned on by tod_status_set().
* In the case where a tod_set() is done and then a subsequent
* tod_get() fails (ie, both TOD_SET_DONE and TOD_GET_FAILED are
* true), we treat the TOD_GET_FAILED with precedence by switching
* off the flag, returning tod and leaving TOD_SET_DONE asserted
* until such time as tod_get() completes successfully.
*/
if (tod_status_flag & TOD_GET_FAILED) {
/*
* tod_get() has encountered an issue, possibly transitory,
* when reading TOD. We'll just return the incoming tod
* value (which is actually hrestime.tv_sec in this case)
* and when we get a genuine tod, following a successful
* tod_get(), we can validate using prev_tod and prev_tick.
*/
return (tod);
} else if (tod_status_flag & TOD_SET_DONE) {
/*
* TOD has been modified. Just before the TOD was written,
* tod_set_prev() saved tod and hrtime; we can now use
* those values, prev_set_tod and prev_set_tick, to validate
* the incoming tod that's just been read.
*/
/*
* If a tod_set() preceded a cpr_suspend() without an
* intervening tod_validate(), we need to ensure that a
* TOD_JUMPED condition is ignored.
* Note this isn't a concern in the case of DR as we've
* just reassigned dtick_avg, above.
*/
if (tod_status_flag & TOD_CPR_RESUME_DONE) {
cpr_resume_done = 1;
}
} else if (tod_status_flag & TOD_CPR_RESUME_DONE) {
/*
* The system's coming back from a checkpoint resume.
*/
cpr_resume_done = 1;
/*
* We need to handle the possibility of a CPR suspend
* operation having been initiated whilst a DR event was
* in-flight.
*/
if (tod_status_flag & TOD_DR_RESUME_DONE) {
dr_resume_done = 1;
}
} else if (tod_status_flag & TOD_DR_RESUME_DONE) {
/*
* A Dynamic Reconfiguration event has taken place.
*/
dr_resume_done = 1;
}
/* test hook */
switch (tod_unit_test) {
case 1: /* for testing jumping tod */
tod += tod_test_injector;
tod_unit_test = 0;
break;
case 2: /* for testing stuck tod bit */
tod_unit_test = 0;
break;
case 3: /* for testing stalled tod */
tod_unit_test = 0;
break;
case 4: /* reset tod fault status */
(void) tod_fault(TOD_NOFAULT, 0);
tod_unit_test = 0;
break;
default:
break;
}
if (diff_tod < 0) {
/* ERROR - tod reversed */
} else if (diff_tod == 0) {
/* tod did not advance */
if (diff_tick > TOD_STALL_THRESHOLD) {
/* ERROR - tod stalled */
} else {
/*
* Make sure we don't update prev_tick
* so that diff_tick is calculated since
* the first diff_tod == 0
*/
return (tod);
}
} else {
/* calculate dtick */
/* update dtick averages */
/*
* Calculate dtick_delta as
* variation from reference freq in quartiles
*/
(TOD_REF_FREQ >> 2);
/*
* Even with a perfectly functioning TOD device,
* when the number of elapsed seconds is low the
* algorithm can calculate a rate that is beyond
* tolerance, causing an error. The algorithm is
* inaccurate when elapsed time is low (less than
* 5 seconds).
*/
if (diff_tod > 4) {
if (dtick < TOD_JUMP_THRESHOLD) {
/*
* If we've just done a CPR resume, we detect
* a jump in the TOD but, actually, what's
* happened is that the TOD has been increasing
* whilst the system was suspended and the tick
* count hasn't kept up. We consider the first
* occurrence of this after a resume as normal
* and ignore it; otherwise, in a non-resume
* case, we regard it as a TOD problem.
*/
if (!cpr_resume_done) {
/* ERROR - tod jumped */
}
}
if (dtick_delta) {
/*
* If we've just done a DR resume, dtick_avg
* can go a bit askew so we reset it and carry
* on; otherwise, the TOD is in error.
*/
if (dr_resume_done) {
} else {
/* ERROR - change in clock rate */
}
}
}
}
if (tod_bad != TOD_NOFAULT) {
/*
* Disable dosynctodr since we are going to fault
* the TOD chip anyway here
*/
dosynctodr = 0;
/*
* Set tod to the correct value from hrestime
*/
}
return (tod);
}
static void
{
uint_t i;
int64_t q, r;
/*
* Compute load average over the last 1, 5, and 15 minutes
* (60, 300, and 900 seconds). The constants in f[3] are for
* exponential decay:
* (1 - exp(-1/60)) << 13 = 135,
* (1 - exp(-1/300)) << 13 = 27,
* (1 - exp(-1/900)) << 13 = 9.
*/
/*
* a little hoop-jumping to avoid integer overflow
*/
for (i = 0; i < 3; i++) {
}
}
/*
* lbolt_hybrid() is used by ddi_get_lbolt() and ddi_get_lbolt64() to
* calculate the value of lbolt according to the current mode. In the event
* driven mode (the default), lbolt is calculated by dividing the current hires
* time by the number of nanoseconds per clock tick. In the cyclic driven mode
* an internal variable is incremented at each firing of the lbolt cyclic
* and returned by lbolt_cyclic_driven().
*
* The system will transition from event to cyclic driven mode when the number
* of calls to lbolt_event_driven() exceeds the (per CPU) threshold within a
* window of time. It does so by reprograming lbolt_cyclic from CY_INFINITY to
* nsec_per_tick. The lbolt cyclic will remain ON while at least one CPU is
* causing enough activity to cross the thresholds.
*/
lbolt_bootstrap(void)
{
return (0);
}
/* ARGSUSED */
{
int ret;
/*
* Align the next expiration to a clock tick boundary.
*/
return (1);
}
lbolt_event_driven(void)
{
ASSERT(nsec_per_tick > 0);
/*
* Switch to cyclic mode if the number of calls to this routine
* has reached the threshold within the interval.
*/
/*
* Reached the threshold within the interval, reset
* the usage statistics.
*/
/*
* Make sure only one thread reprograms the
* lbolt cyclic and changes the mode.
*/
if (lbolt_hybrid == lbolt_cyclic_driven) {
} else {
}
}
}
} else {
/*
* Exceeded the interval, reset the usage statistics.
*/
}
}
lbolt_cyclic_driven(void)
{
int cpu;
/*
* If a CPU has already prevented the lbolt cyclic from deactivating
* itself, don't bother tracking the usage. Otherwise check if we're
* within the interval and how the per CPU counter is doing.
*/
if (lb_info->lbi_cyc_deactivate) {
/*
* Reached the threshold within the interval,
* prevent the lbolt cyclic from turning itself
* off.
*/
else
} else {
/*
* Only reset the usage statistics when we have
* exceeded the interval.
*/
}
}
}
/*
* The lbolt_cyclic() routine will fire at a nsec_per_tick interval to satisfy
* performance needs of ddi_get_lbolt() and ddi_get_lbolt64() consumers.
* It is inactive by default, and will be activated when switching from event
* to cyclic driven lbolt. The cyclic will turn itself off unless signaled
* by lbolt_cyclic_driven().
*/
static void
lbolt_cyclic(void)
{
int ret;
lb_info->lbi_internal++;
if (!lbolt_cyc_only) {
if (lb_info->lbi_cyc_deactivate) {
/*
* Switching from cyclic to event driven mode.
*/
if (lbolt_hybrid == lbolt_event_driven) {
return;
}
}
}
/*
* The lbolt cyclic should not try to deactivate itself before
* the sampling period has elapsed.
*/
}
}
}
/*
* Since the lbolt service was historically cyclic driven, it must be 'stopped'
* when the system drops into the kernel debugger. lbolt_debug_entry() is
* called by the KDI system claim callbacks to record a hires timestamp at
* debug enter time. lbolt_debug_return() is called by the sistem release
* callbacks to account for the time spent in the debugger. The value is then
* accumulated in the lb_info structure and used by lbolt_event_driven() and
* lbolt_cyclic_driven(), as well as the mdb_get_lbolt() routine.
*/
void
lbolt_debug_entry(void)
{
if (lbolt_hybrid != lbolt_bootstrap) {
}
}
/*
* Calculate the time spent in the debugger and add it to the lbolt info
* structure. We also update the internal lbolt value in case we were in
* cyclic driven mode going in.
*/
void
lbolt_debug_return(void)
{
if (lbolt_hybrid != lbolt_bootstrap) {
ASSERT(nsec_per_tick > 0);
lb_info->lbi_debug_ts = 0;
}
}