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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/param.h> 310Sstevel@tonic-gate #include <sys/systm.h> 320Sstevel@tonic-gate #include <sys/user.h> 330Sstevel@tonic-gate #include <sys/proc.h> 340Sstevel@tonic-gate #include <sys/cpuvar.h> 350Sstevel@tonic-gate #include <sys/thread.h> 360Sstevel@tonic-gate #include <sys/debug.h> 370Sstevel@tonic-gate #include <sys/msacct.h> 380Sstevel@tonic-gate #include <sys/time.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate /* 410Sstevel@tonic-gate * Mega-theory block comment: 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * Microstate accounting uses finite states and the transitions between these 440Sstevel@tonic-gate * states to measure timing and accounting information. The state information 450Sstevel@tonic-gate * is presently tracked for threads (via microstate accounting) and cpus (via 460Sstevel@tonic-gate * cpu microstate accounting). In each case, these accounting mechanisms use 470Sstevel@tonic-gate * states and transitions to measure time spent in each state instead of 480Sstevel@tonic-gate * clock-based sampling methodologies. 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * For microstate accounting: 510Sstevel@tonic-gate * state transitions are accomplished by calling new_mstate() to switch between 520Sstevel@tonic-gate * states. Transitions from a sleeping state (LMS_SLEEP and LMS_STOPPED) occur 530Sstevel@tonic-gate * by calling restore_mstate() which restores a thread to its previously running 540Sstevel@tonic-gate * state. This code is primarialy executed by the dispatcher in disp() before 550Sstevel@tonic-gate * running a process that was put to sleep. If the thread was not in a sleeping 560Sstevel@tonic-gate * state, this call has little effect other than to update the count of time the 570Sstevel@tonic-gate * thread has spent waiting on run-queues in its lifetime. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * For cpu microstate accounting: 600Sstevel@tonic-gate * Cpu microstate accounting is similar to the microstate accounting for threads 610Sstevel@tonic-gate * but it tracks user, system, and idle time for cpus. Cpu microstate 620Sstevel@tonic-gate * accounting does not track interrupt times as there is a pre-existing 630Sstevel@tonic-gate * interrupt accounting mechanism for this purpose. Cpu microstate accounting 640Sstevel@tonic-gate * tracks time that user threads have spent active, idle, or in the system on a 650Sstevel@tonic-gate * given cpu. Cpu microstate accounting has fewer states which allows it to 660Sstevel@tonic-gate * have better defined transitions. The states transition in the following 670Sstevel@tonic-gate * order: 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * CMS_USER <-> CMS_SYSTEM <-> CMS_IDLE 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * In order to get to the idle state, the cpu microstate must first go through 720Sstevel@tonic-gate * the system state, and vice-versa for the user state from idle. The switching 730Sstevel@tonic-gate * of the microstates from user to system is done as part of the regular thread 740Sstevel@tonic-gate * microstate accounting code, except for the idle state which is switched by 750Sstevel@tonic-gate * the dispatcher before it runs the idle loop. 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * Cpu percentages: 780Sstevel@tonic-gate * Cpu percentages are now handled by and based upon microstate accounting 790Sstevel@tonic-gate * information (the same is true for load averages). The routines which handle 800Sstevel@tonic-gate * the growing/shrinking and exponentiation of cpu percentages have been moved 810Sstevel@tonic-gate * here as it now makes more sense for them to be generated from the microstate 820Sstevel@tonic-gate * code. Cpu percentages are generated similarly to the way they were before; 830Sstevel@tonic-gate * however, now they are based upon high-resolution timestamps and the 840Sstevel@tonic-gate * timestamps are modified at various state changes instead of during a clock() 850Sstevel@tonic-gate * interrupt. This allows us to generate more accurate cpu percentages which 860Sstevel@tonic-gate * are also in-sync with microstate data. 870Sstevel@tonic-gate */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * Initialize the microstate level and the 910Sstevel@tonic-gate * associated accounting information for an LWP. 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate void 940Sstevel@tonic-gate init_mstate( 950Sstevel@tonic-gate kthread_t *t, 960Sstevel@tonic-gate int init_state) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate struct mstate *ms; 990Sstevel@tonic-gate klwp_t *lwp; 1000Sstevel@tonic-gate hrtime_t curtime; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate ASSERT(init_state != LMS_WAIT_CPU); 1030Sstevel@tonic-gate ASSERT((unsigned)init_state < NMSTATES); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if ((lwp = ttolwp(t)) != NULL) { 1060Sstevel@tonic-gate ms = &lwp->lwp_mstate; 1070Sstevel@tonic-gate curtime = gethrtime_unscaled(); 1080Sstevel@tonic-gate ms->ms_prev = LMS_SYSTEM; 1090Sstevel@tonic-gate ms->ms_start = curtime; 1100Sstevel@tonic-gate ms->ms_term = 0; 1110Sstevel@tonic-gate ms->ms_state_start = curtime; 1120Sstevel@tonic-gate t->t_mstate = init_state; 1130Sstevel@tonic-gate t->t_waitrq = 0; 1140Sstevel@tonic-gate t->t_hrtime = curtime; 1150Sstevel@tonic-gate if ((t->t_proc_flag & TP_MSACCT) == 0) 1160Sstevel@tonic-gate t->t_proc_flag |= TP_MSACCT; 1170Sstevel@tonic-gate bzero((caddr_t)&ms->ms_acct[0], sizeof (ms->ms_acct)); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * Initialize the microstate level and associated accounting information 1230Sstevel@tonic-gate * for the specified cpu 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate void 1270Sstevel@tonic-gate init_cpu_mstate( 1280Sstevel@tonic-gate cpu_t *cpu, 1290Sstevel@tonic-gate int init_state) 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate ASSERT(init_state != CMS_DISABLED); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate cpu->cpu_mstate = init_state; 1340Sstevel@tonic-gate cpu->cpu_mstate_start = gethrtime_unscaled(); 1350Sstevel@tonic-gate cpu->cpu_waitrq = 0; 1360Sstevel@tonic-gate bzero((caddr_t)&cpu->cpu_acct[0], sizeof (cpu->cpu_acct)); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * sets cpu state to OFFLINE. We don't actually track this time, 1410Sstevel@tonic-gate * but it serves as a useful placeholder state for when we're not 1420Sstevel@tonic-gate * doing anything. 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate void 1460Sstevel@tonic-gate term_cpu_mstate(struct cpu *cpu) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate ASSERT(cpu->cpu_mstate != CMS_DISABLED); 1490Sstevel@tonic-gate cpu->cpu_mstate = CMS_DISABLED; 1500Sstevel@tonic-gate cpu->cpu_mstate_start = 0; 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate void 154*590Sesolom new_cpu_mstate(int cmstate, hrtime_t curtime) 1550Sstevel@tonic-gate { 156*590Sesolom cpu_t *cpu = CPU; 157*590Sesolom uint16_t gen; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate ASSERT(cpu->cpu_mstate != CMS_DISABLED); 1600Sstevel@tonic-gate ASSERT(cmstate < NCMSTATES); 1610Sstevel@tonic-gate ASSERT(cmstate != CMS_DISABLED); 162*590Sesolom 163*590Sesolom /* 164*590Sesolom * This function cannot be re-entrant on a given CPU. As such, 165*590Sesolom * we ASSERT and panic if we are called on behalf of an interrupt. 166*590Sesolom * The one exception is for an interrupt which has previously 167*590Sesolom * blocked. Such an interrupt is being scheduled by the dispatcher 168*590Sesolom * just like a normal thread, and as such cannot arrive here 169*590Sesolom * in a re-entrant manner. 170*590Sesolom */ 171*590Sesolom 172*590Sesolom ASSERT(!CPU_ON_INTR(cpu) && curthread->t_intr == NULL); 1730Sstevel@tonic-gate ASSERT(curthread->t_preempt > 0 || curthread == cpu->cpu_idle_thread); 1740Sstevel@tonic-gate 175*590Sesolom /* 176*590Sesolom * LOCKING, or lack thereof: 177*590Sesolom * 178*590Sesolom * Updates to CPU mstate can only be made by the CPU 179*590Sesolom * itself, and the above check to ignore interrupts 180*590Sesolom * should prevent recursion into this function on a given 181*590Sesolom * processor. i.e. no possible write contention. 182*590Sesolom * 183*590Sesolom * However, reads of CPU mstate can occur at any time 184*590Sesolom * from any CPU. Any locking added to this code path 185*590Sesolom * would seriously impact syscall performance. So, 186*590Sesolom * instead we have a best-effort protection for readers. 187*590Sesolom * The reader will want to account for any time between 188*590Sesolom * cpu_mstate_start and the present time. This requires 189*590Sesolom * some guarantees that the reader is getting coherent 190*590Sesolom * information. 191*590Sesolom * 192*590Sesolom * We use a generation counter, which is set to 0 before 193*590Sesolom * we start making changes, and is set to a new value 194*590Sesolom * after we're done. Someone reading the CPU mstate 195*590Sesolom * should check for the same non-zero value of this 196*590Sesolom * counter both before and after reading all state. The 197*590Sesolom * important point is that the reader is not a 198*590Sesolom * performance-critical path, but this function is. 199*590Sesolom */ 200*590Sesolom 201*590Sesolom gen = cpu->cpu_mstate_gen; 202*590Sesolom cpu->cpu_mstate_gen = 0; 203*590Sesolom 204*590Sesolom membar_producer(); 205*590Sesolom cpu->cpu_acct[cpu->cpu_mstate] += curtime - cpu->cpu_mstate_start; 206*590Sesolom cpu->cpu_mstate = cmstate; 207*590Sesolom cpu->cpu_mstate_start = curtime; 208*590Sesolom membar_producer(); 209*590Sesolom 210*590Sesolom cpu->cpu_mstate_gen = (++gen == 0) ? 1 : gen; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * Return an aggregation of microstate times in scaled nanoseconds (high-res 2150Sstevel@tonic-gate * time). This keeps in mind that p_acct is already scaled, and ms_acct is 2160Sstevel@tonic-gate * not. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate hrtime_t 2190Sstevel@tonic-gate mstate_aggr_state(proc_t *p, int a_state) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate struct mstate *ms; 2220Sstevel@tonic-gate kthread_t *t; 2230Sstevel@tonic-gate klwp_t *lwp; 2240Sstevel@tonic-gate hrtime_t aggr_time; 2250Sstevel@tonic-gate hrtime_t scaledtime; 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 2280Sstevel@tonic-gate ASSERT((unsigned)a_state < NMSTATES); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate aggr_time = p->p_acct[a_state]; 2310Sstevel@tonic-gate if (a_state == LMS_SYSTEM) 2320Sstevel@tonic-gate aggr_time += p->p_acct[LMS_TRAP]; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate t = p->p_tlist; 2350Sstevel@tonic-gate if (t == NULL) 2360Sstevel@tonic-gate return (aggr_time); 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate do { 2390Sstevel@tonic-gate if (t->t_proc_flag & TP_LWPEXIT) 2400Sstevel@tonic-gate continue; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate lwp = ttolwp(t); 2430Sstevel@tonic-gate ms = &lwp->lwp_mstate; 2440Sstevel@tonic-gate scaledtime = ms->ms_acct[a_state]; 2450Sstevel@tonic-gate scalehrtime(&scaledtime); 2460Sstevel@tonic-gate aggr_time += scaledtime; 2470Sstevel@tonic-gate if (a_state == LMS_SYSTEM) { 2480Sstevel@tonic-gate scaledtime = ms->ms_acct[LMS_TRAP]; 2490Sstevel@tonic-gate scalehrtime(&scaledtime); 2500Sstevel@tonic-gate aggr_time += scaledtime; 2510Sstevel@tonic-gate } 2520Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate return (aggr_time); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate void 2580Sstevel@tonic-gate syscall_mstate(int fromms, int toms) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate kthread_t *t = curthread; 2610Sstevel@tonic-gate struct mstate *ms; 2620Sstevel@tonic-gate hrtime_t *mstimep; 2630Sstevel@tonic-gate hrtime_t curtime; 2640Sstevel@tonic-gate klwp_t *lwp; 2650Sstevel@tonic-gate hrtime_t newtime; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL) 2680Sstevel@tonic-gate return; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate ASSERT(fromms < NMSTATES); 2710Sstevel@tonic-gate ASSERT(toms < NMSTATES); 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate ms = &lwp->lwp_mstate; 2740Sstevel@tonic-gate mstimep = &ms->ms_acct[fromms]; 2750Sstevel@tonic-gate curtime = gethrtime_unscaled(); 2760Sstevel@tonic-gate newtime = curtime - ms->ms_state_start; 2770Sstevel@tonic-gate while (newtime < 0) { 2780Sstevel@tonic-gate curtime = gethrtime_unscaled(); 2790Sstevel@tonic-gate newtime = curtime - ms->ms_state_start; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate *mstimep += newtime; 2820Sstevel@tonic-gate t->t_mstate = toms; 2830Sstevel@tonic-gate ms->ms_state_start = curtime; 2840Sstevel@tonic-gate ms->ms_prev = fromms; 285*590Sesolom kpreempt_disable(); /* don't change CPU while changing CPU's state */ 286*590Sesolom ASSERT(CPU == t->t_cpu); 287*590Sesolom if ((toms != LMS_USER) && (t->t_cpu->cpu_mstate != CMS_SYSTEM)) 288*590Sesolom new_cpu_mstate(CMS_SYSTEM, curtime); 289*590Sesolom else if ((toms == LMS_USER) && (t->t_cpu->cpu_mstate != CMS_USER)) 290*590Sesolom new_cpu_mstate(CMS_USER, curtime); 2910Sstevel@tonic-gate kpreempt_enable(); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * The following is for computing the percentage of cpu time used recently 2960Sstevel@tonic-gate * by an lwp. The function cpu_decay() is also called from /proc code. 2970Sstevel@tonic-gate * 2980Sstevel@tonic-gate * exp_x(x): 2990Sstevel@tonic-gate * Given x as a 64-bit non-negative scaled integer of arbitrary magnitude, 3000Sstevel@tonic-gate * Return exp(-x) as a 64-bit scaled integer in the range [0 .. 1]. 3010Sstevel@tonic-gate * 3020Sstevel@tonic-gate * Scaling for 64-bit scaled integer: 3030Sstevel@tonic-gate * The binary point is to the right of the high-order bit 3040Sstevel@tonic-gate * of the low-order 32-bit word. 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate #define LSHIFT 31 3080Sstevel@tonic-gate #define LSI_ONE ((uint32_t)1 << LSHIFT) /* 32-bit scaled integer 1 */ 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate #ifdef DEBUG 3110Sstevel@tonic-gate uint_t expx_cnt = 0; /* number of calls to exp_x() */ 3120Sstevel@tonic-gate uint_t expx_mul = 0; /* number of long multiplies in exp_x() */ 3130Sstevel@tonic-gate #endif 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate static uint64_t 3160Sstevel@tonic-gate exp_x(uint64_t x) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate int i; 3190Sstevel@tonic-gate uint64_t ull; 3200Sstevel@tonic-gate uint32_t ui; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate #ifdef DEBUG 3230Sstevel@tonic-gate expx_cnt++; 3240Sstevel@tonic-gate #endif 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * By the formula: 3270Sstevel@tonic-gate * exp(-x) = exp(-x/2) * exp(-x/2) 3280Sstevel@tonic-gate * we keep halving x until it becomes small enough for 3290Sstevel@tonic-gate * the following approximation to be accurate enough: 3300Sstevel@tonic-gate * exp(-x) = 1 - x 3310Sstevel@tonic-gate * We reduce x until it is less than 1/4 (the 2 in LSHIFT-2 below). 3320Sstevel@tonic-gate * Our final error will be smaller than 4% . 3330Sstevel@tonic-gate */ 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * Use a uint64_t for the initial shift calculation. 3370Sstevel@tonic-gate */ 3380Sstevel@tonic-gate ull = x >> (LSHIFT-2); 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate /* 3410Sstevel@tonic-gate * Short circuit: 3420Sstevel@tonic-gate * A number this large produces effectively 0 (actually .005). 3430Sstevel@tonic-gate * This way, we will never do more than 5 multiplies. 3440Sstevel@tonic-gate */ 3450Sstevel@tonic-gate if (ull >= (1 << 5)) 3460Sstevel@tonic-gate return (0); 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate ui = ull; /* OK. Now we can use a uint_t. */ 3490Sstevel@tonic-gate for (i = 0; ui != 0; i++) 3500Sstevel@tonic-gate ui >>= 1; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if (i != 0) { 3530Sstevel@tonic-gate #ifdef DEBUG 3540Sstevel@tonic-gate expx_mul += i; /* seldom happens */ 3550Sstevel@tonic-gate #endif 3560Sstevel@tonic-gate x >>= i; 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * Now we compute 1 - x and square it the number of times 3610Sstevel@tonic-gate * that we halved x above to produce the final result: 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate x = LSI_ONE - x; 3640Sstevel@tonic-gate while (i--) 3650Sstevel@tonic-gate x = (x * x) >> LSHIFT; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate return (x); 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Given the old percent cpu and a time delta in nanoseconds, 3720Sstevel@tonic-gate * return the new decayed percent cpu: pct * exp(-tau), 3730Sstevel@tonic-gate * where 'tau' is the time delta multiplied by a decay factor. 3740Sstevel@tonic-gate * We have chosen the decay factor (cpu_decay_factor in param.c) 3750Sstevel@tonic-gate * to make the decay over five seconds be approximately 20%. 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate * 'pct' is a 32-bit scaled integer <= 1 3780Sstevel@tonic-gate * The binary point is to the right of the high-order bit 3790Sstevel@tonic-gate * of the 32-bit word. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate static uint32_t 3820Sstevel@tonic-gate cpu_decay(uint32_t pct, hrtime_t nsec) 3830Sstevel@tonic-gate { 3840Sstevel@tonic-gate uint64_t delta = (uint64_t)nsec; 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate delta /= cpu_decay_factor; 3870Sstevel@tonic-gate return ((pct * exp_x(delta)) >> LSHIFT); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * Given the old percent cpu and a time delta in nanoseconds, 3920Sstevel@tonic-gate * return the new grown percent cpu: 1 - ( 1 - pct ) * exp(-tau) 3930Sstevel@tonic-gate */ 3940Sstevel@tonic-gate static uint32_t 3950Sstevel@tonic-gate cpu_grow(uint32_t pct, hrtime_t nsec) 3960Sstevel@tonic-gate { 3970Sstevel@tonic-gate return (LSI_ONE - cpu_decay(LSI_ONE - pct, nsec)); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate /* 4020Sstevel@tonic-gate * Defined to determine whether a lwp is still on a processor. 4030Sstevel@tonic-gate */ 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate #define T_ONPROC(kt) \ 4060Sstevel@tonic-gate ((kt)->t_mstate < LMS_SLEEP) 4070Sstevel@tonic-gate #define T_OFFPROC(kt) \ 4080Sstevel@tonic-gate ((kt)->t_mstate >= LMS_SLEEP) 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate uint_t 4110Sstevel@tonic-gate cpu_update_pct(kthread_t *t, hrtime_t newtime) 4120Sstevel@tonic-gate { 4130Sstevel@tonic-gate hrtime_t delta; 4140Sstevel@tonic-gate hrtime_t hrlb; 4150Sstevel@tonic-gate uint_t pctcpu; 4160Sstevel@tonic-gate uint_t npctcpu; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate /* 4190Sstevel@tonic-gate * This routine can get called at PIL > 0, this *has* to be 4200Sstevel@tonic-gate * done atomically. Holding locks here causes bad things to happen. 4210Sstevel@tonic-gate * (read: deadlock). 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate do { 4250Sstevel@tonic-gate if (T_ONPROC(t) && t->t_waitrq == 0) { 4260Sstevel@tonic-gate hrlb = t->t_hrtime; 4270Sstevel@tonic-gate delta = newtime - hrlb; 4280Sstevel@tonic-gate if (delta < 0) { 4290Sstevel@tonic-gate newtime = gethrtime_unscaled(); 4300Sstevel@tonic-gate delta = newtime - hrlb; 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate t->t_hrtime = newtime; 4330Sstevel@tonic-gate scalehrtime(&delta); 4340Sstevel@tonic-gate pctcpu = t->t_pctcpu; 4350Sstevel@tonic-gate npctcpu = cpu_grow(pctcpu, delta); 4360Sstevel@tonic-gate } else { 4370Sstevel@tonic-gate hrlb = t->t_hrtime; 4380Sstevel@tonic-gate delta = newtime - hrlb; 4390Sstevel@tonic-gate if (delta < 0) { 4400Sstevel@tonic-gate newtime = gethrtime_unscaled(); 4410Sstevel@tonic-gate delta = newtime - hrlb; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate t->t_hrtime = newtime; 4440Sstevel@tonic-gate scalehrtime(&delta); 4450Sstevel@tonic-gate pctcpu = t->t_pctcpu; 4460Sstevel@tonic-gate npctcpu = cpu_decay(pctcpu, delta); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate } while (cas32(&t->t_pctcpu, pctcpu, npctcpu) != pctcpu); 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate return (npctcpu); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* 4540Sstevel@tonic-gate * Change the microstate level for the LWP and update the 4550Sstevel@tonic-gate * associated accounting information. Return the previous 4560Sstevel@tonic-gate * LWP state. 4570Sstevel@tonic-gate */ 4580Sstevel@tonic-gate int 4590Sstevel@tonic-gate new_mstate(kthread_t *t, int new_state) 4600Sstevel@tonic-gate { 4610Sstevel@tonic-gate struct mstate *ms; 4620Sstevel@tonic-gate unsigned state; 4630Sstevel@tonic-gate hrtime_t *mstimep; 4640Sstevel@tonic-gate hrtime_t curtime; 4650Sstevel@tonic-gate hrtime_t newtime; 4660Sstevel@tonic-gate hrtime_t oldtime; 4670Sstevel@tonic-gate klwp_t *lwp; 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate ASSERT(new_state != LMS_WAIT_CPU); 4700Sstevel@tonic-gate ASSERT((unsigned)new_state < NMSTATES); 4710Sstevel@tonic-gate ASSERT(t == curthread || THREAD_LOCK_HELD(t)); 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL) 4740Sstevel@tonic-gate return (LMS_SYSTEM); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate curtime = gethrtime_unscaled(); 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* adjust cpu percentages before we go any further */ 4790Sstevel@tonic-gate (void) cpu_update_pct(t, curtime); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate ms = &lwp->lwp_mstate; 4820Sstevel@tonic-gate state = t->t_mstate; 4830Sstevel@tonic-gate do { 4840Sstevel@tonic-gate switch (state) { 4850Sstevel@tonic-gate case LMS_TFAULT: 4860Sstevel@tonic-gate case LMS_DFAULT: 4870Sstevel@tonic-gate case LMS_KFAULT: 4880Sstevel@tonic-gate case LMS_USER_LOCK: 4890Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SYSTEM]; 4900Sstevel@tonic-gate break; 4910Sstevel@tonic-gate default: 4920Sstevel@tonic-gate mstimep = &ms->ms_acct[state]; 4930Sstevel@tonic-gate break; 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate newtime = curtime - ms->ms_state_start; 4960Sstevel@tonic-gate if (newtime < 0) { 4970Sstevel@tonic-gate curtime = gethrtime_unscaled(); 4980Sstevel@tonic-gate oldtime = *mstimep - 1; /* force CAS to fail */ 4990Sstevel@tonic-gate continue; 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate oldtime = *mstimep; 5020Sstevel@tonic-gate newtime += oldtime; 5030Sstevel@tonic-gate t->t_mstate = new_state; 5040Sstevel@tonic-gate ms->ms_state_start = curtime; 5050Sstevel@tonic-gate } while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime); 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * Remember the previous running microstate. 5080Sstevel@tonic-gate */ 5090Sstevel@tonic-gate if (state != LMS_SLEEP && state != LMS_STOPPED) 5100Sstevel@tonic-gate ms->ms_prev = state; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* 5130Sstevel@tonic-gate * Switch CPU microstate if appropriate 5140Sstevel@tonic-gate */ 515*590Sesolom 5160Sstevel@tonic-gate kpreempt_disable(); /* MUST disable kpreempt before touching t->cpu */ 517*590Sesolom ASSERT(t->t_cpu == CPU); 518*590Sesolom if (!CPU_ON_INTR(t->t_cpu) && curthread->t_intr == NULL) { 519*590Sesolom if (new_state == LMS_USER && t->t_cpu->cpu_mstate != CMS_USER) 520*590Sesolom new_cpu_mstate(CMS_USER, curtime); 521*590Sesolom else if (new_state != LMS_USER && 522*590Sesolom t->t_cpu->cpu_mstate != CMS_SYSTEM) 523*590Sesolom new_cpu_mstate(CMS_SYSTEM, curtime); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate kpreempt_enable(); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate return (ms->ms_prev); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate static long waitrqis0 = 0; 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate /* 5330Sstevel@tonic-gate * Restore the LWP microstate to the previous runnable state. 5340Sstevel@tonic-gate * Called from disp() with the newly selected lwp. 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate void 5370Sstevel@tonic-gate restore_mstate(kthread_t *t) 5380Sstevel@tonic-gate { 5390Sstevel@tonic-gate struct mstate *ms; 5400Sstevel@tonic-gate hrtime_t *mstimep; 5410Sstevel@tonic-gate klwp_t *lwp; 5420Sstevel@tonic-gate hrtime_t curtime; 5430Sstevel@tonic-gate hrtime_t waitrq; 5440Sstevel@tonic-gate hrtime_t newtime; 5450Sstevel@tonic-gate hrtime_t oldtime; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL) 5480Sstevel@tonic-gate return; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate curtime = gethrtime_unscaled(); 5510Sstevel@tonic-gate (void) cpu_update_pct(t, curtime); 5520Sstevel@tonic-gate ms = &lwp->lwp_mstate; 5530Sstevel@tonic-gate ASSERT((unsigned)t->t_mstate < NMSTATES); 5540Sstevel@tonic-gate do { 5550Sstevel@tonic-gate switch (t->t_mstate) { 5560Sstevel@tonic-gate case LMS_SLEEP: 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * Update the timer for the current sleep state. 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate ASSERT((unsigned)ms->ms_prev < NMSTATES); 5610Sstevel@tonic-gate switch (ms->ms_prev) { 5620Sstevel@tonic-gate case LMS_TFAULT: 5630Sstevel@tonic-gate case LMS_DFAULT: 5640Sstevel@tonic-gate case LMS_KFAULT: 5650Sstevel@tonic-gate case LMS_USER_LOCK: 5660Sstevel@tonic-gate mstimep = &ms->ms_acct[ms->ms_prev]; 5670Sstevel@tonic-gate break; 5680Sstevel@tonic-gate default: 5690Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SLEEP]; 5700Sstevel@tonic-gate break; 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * Return to the previous run state. 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate t->t_mstate = ms->ms_prev; 5760Sstevel@tonic-gate break; 5770Sstevel@tonic-gate case LMS_STOPPED: 5780Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_STOPPED]; 5790Sstevel@tonic-gate /* 5800Sstevel@tonic-gate * Return to the previous run state. 5810Sstevel@tonic-gate */ 5820Sstevel@tonic-gate t->t_mstate = ms->ms_prev; 5830Sstevel@tonic-gate break; 5840Sstevel@tonic-gate case LMS_TFAULT: 5850Sstevel@tonic-gate case LMS_DFAULT: 5860Sstevel@tonic-gate case LMS_KFAULT: 5870Sstevel@tonic-gate case LMS_USER_LOCK: 5880Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SYSTEM]; 5890Sstevel@tonic-gate break; 5900Sstevel@tonic-gate default: 5910Sstevel@tonic-gate mstimep = &ms->ms_acct[t->t_mstate]; 5920Sstevel@tonic-gate break; 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate waitrq = t->t_waitrq; /* hopefully atomic */ 5950Sstevel@tonic-gate t->t_waitrq = 0; 5960Sstevel@tonic-gate if (waitrq == 0) { /* should only happen during boot */ 5970Sstevel@tonic-gate waitrq = curtime; 5980Sstevel@tonic-gate waitrqis0++; 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate newtime = waitrq - ms->ms_state_start; 6010Sstevel@tonic-gate if (newtime < 0) { 6020Sstevel@tonic-gate curtime = gethrtime_unscaled(); 6030Sstevel@tonic-gate oldtime = *mstimep - 1; /* force CAS to fail */ 6040Sstevel@tonic-gate continue; 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate oldtime = *mstimep; 6070Sstevel@tonic-gate newtime += oldtime; 6080Sstevel@tonic-gate } while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime); 6090Sstevel@tonic-gate /* 6100Sstevel@tonic-gate * Update the WAIT_CPU timer and per-cpu waitrq total. 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate ms->ms_acct[LMS_WAIT_CPU] += (curtime - waitrq); 613477Smishra CPU->cpu_waitrq += (curtime - waitrq); 6140Sstevel@tonic-gate ms->ms_state_start = curtime; 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * Copy lwp microstate accounting and resource usage information 6190Sstevel@tonic-gate * to the process. (lwp is terminating) 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate void 6220Sstevel@tonic-gate term_mstate(kthread_t *t) 6230Sstevel@tonic-gate { 6240Sstevel@tonic-gate struct mstate *ms; 6250Sstevel@tonic-gate proc_t *p = ttoproc(t); 6260Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 6270Sstevel@tonic-gate int i; 6280Sstevel@tonic-gate hrtime_t tmp; 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate ms = &lwp->lwp_mstate; 6330Sstevel@tonic-gate (void) new_mstate(t, LMS_STOPPED); 6340Sstevel@tonic-gate ms->ms_term = ms->ms_state_start; 6350Sstevel@tonic-gate tmp = ms->ms_term - ms->ms_start; 6360Sstevel@tonic-gate scalehrtime(&tmp); 6370Sstevel@tonic-gate p->p_mlreal += tmp; 6380Sstevel@tonic-gate for (i = 0; i < NMSTATES; i++) { 6390Sstevel@tonic-gate tmp = ms->ms_acct[i]; 6400Sstevel@tonic-gate scalehrtime(&tmp); 6410Sstevel@tonic-gate p->p_acct[i] += tmp; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate p->p_ru.minflt += lwp->lwp_ru.minflt; 6440Sstevel@tonic-gate p->p_ru.majflt += lwp->lwp_ru.majflt; 6450Sstevel@tonic-gate p->p_ru.nswap += lwp->lwp_ru.nswap; 6460Sstevel@tonic-gate p->p_ru.inblock += lwp->lwp_ru.inblock; 6470Sstevel@tonic-gate p->p_ru.oublock += lwp->lwp_ru.oublock; 6480Sstevel@tonic-gate p->p_ru.msgsnd += lwp->lwp_ru.msgsnd; 6490Sstevel@tonic-gate p->p_ru.msgrcv += lwp->lwp_ru.msgrcv; 6500Sstevel@tonic-gate p->p_ru.nsignals += lwp->lwp_ru.nsignals; 6510Sstevel@tonic-gate p->p_ru.nvcsw += lwp->lwp_ru.nvcsw; 6520Sstevel@tonic-gate p->p_ru.nivcsw += lwp->lwp_ru.nivcsw; 6530Sstevel@tonic-gate p->p_ru.sysc += lwp->lwp_ru.sysc; 6540Sstevel@tonic-gate p->p_ru.ioch += lwp->lwp_ru.ioch; 6550Sstevel@tonic-gate p->p_defunct++; 6560Sstevel@tonic-gate } 657