xref: /titanic_52/usr/src/uts/common/os/msacct.c (revision f2bd46275366373a10ce31b170ae2bbc7b03cc0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*f2bd4627Sjohansen  * Common Development and Distribution License (the "License").
6*f2bd4627Sjohansen  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f2bd4627Sjohansen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/user.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
347c478bd9Sstevel@tonic-gate #include <sys/thread.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
377c478bd9Sstevel@tonic-gate #include <sys/time.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Mega-theory block comment:
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Microstate accounting uses finite states and the transitions between these
437c478bd9Sstevel@tonic-gate  * states to measure timing and accounting information.  The state information
447c478bd9Sstevel@tonic-gate  * is presently tracked for threads (via microstate accounting) and cpus (via
457c478bd9Sstevel@tonic-gate  * cpu microstate accounting).  In each case, these accounting mechanisms use
467c478bd9Sstevel@tonic-gate  * states and transitions to measure time spent in each state instead of
477c478bd9Sstevel@tonic-gate  * clock-based sampling methodologies.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * For microstate accounting:
507c478bd9Sstevel@tonic-gate  * state transitions are accomplished by calling new_mstate() to switch between
517c478bd9Sstevel@tonic-gate  * states.  Transitions from a sleeping state (LMS_SLEEP and LMS_STOPPED) occur
527c478bd9Sstevel@tonic-gate  * by calling restore_mstate() which restores a thread to its previously running
537c478bd9Sstevel@tonic-gate  * state.  This code is primarialy executed by the dispatcher in disp() before
547c478bd9Sstevel@tonic-gate  * running a process that was put to sleep.  If the thread was not in a sleeping
557c478bd9Sstevel@tonic-gate  * state, this call has little effect other than to update the count of time the
567c478bd9Sstevel@tonic-gate  * thread has spent waiting on run-queues in its lifetime.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * For cpu microstate accounting:
597c478bd9Sstevel@tonic-gate  * Cpu microstate accounting is similar to the microstate accounting for threads
607c478bd9Sstevel@tonic-gate  * but it tracks user, system, and idle time for cpus.  Cpu microstate
617c478bd9Sstevel@tonic-gate  * accounting does not track interrupt times as there is a pre-existing
627c478bd9Sstevel@tonic-gate  * interrupt accounting mechanism for this purpose.  Cpu microstate accounting
637c478bd9Sstevel@tonic-gate  * tracks time that user threads have spent active, idle, or in the system on a
647c478bd9Sstevel@tonic-gate  * given cpu.  Cpu microstate accounting has fewer states which allows it to
657c478bd9Sstevel@tonic-gate  * have better defined transitions.  The states transition in the following
667c478bd9Sstevel@tonic-gate  * order:
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *  CMS_USER <-> CMS_SYSTEM <-> CMS_IDLE
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * In order to get to the idle state, the cpu microstate must first go through
717c478bd9Sstevel@tonic-gate  * the system state, and vice-versa for the user state from idle.  The switching
727c478bd9Sstevel@tonic-gate  * of the microstates from user to system is done as part of the regular thread
737c478bd9Sstevel@tonic-gate  * microstate accounting code, except for the idle state which is switched by
747c478bd9Sstevel@tonic-gate  * the dispatcher before it runs the idle loop.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * Cpu percentages:
777c478bd9Sstevel@tonic-gate  * Cpu percentages are now handled by and based upon microstate accounting
787c478bd9Sstevel@tonic-gate  * information (the same is true for load averages).  The routines which handle
797c478bd9Sstevel@tonic-gate  * the growing/shrinking and exponentiation of cpu percentages have been moved
807c478bd9Sstevel@tonic-gate  * here as it now makes more sense for them to be generated from the microstate
817c478bd9Sstevel@tonic-gate  * code.  Cpu percentages are generated similarly to the way they were before;
827c478bd9Sstevel@tonic-gate  * however, now they are based upon high-resolution timestamps and the
837c478bd9Sstevel@tonic-gate  * timestamps are modified at various state changes instead of during a clock()
847c478bd9Sstevel@tonic-gate  * interrupt.  This allows us to generate more accurate cpu percentages which
857c478bd9Sstevel@tonic-gate  * are also in-sync with microstate data.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Initialize the microstate level and the
907c478bd9Sstevel@tonic-gate  * associated accounting information for an LWP.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate void
937c478bd9Sstevel@tonic-gate init_mstate(
947c478bd9Sstevel@tonic-gate 	kthread_t	*t,
957c478bd9Sstevel@tonic-gate 	int		init_state)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct mstate *ms;
987c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
997c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	ASSERT(init_state != LMS_WAIT_CPU);
1027c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)init_state < NMSTATES);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) != NULL) {
1057c478bd9Sstevel@tonic-gate 		ms = &lwp->lwp_mstate;
1067c478bd9Sstevel@tonic-gate 		curtime = gethrtime_unscaled();
1077c478bd9Sstevel@tonic-gate 		ms->ms_prev = LMS_SYSTEM;
1087c478bd9Sstevel@tonic-gate 		ms->ms_start = curtime;
1097c478bd9Sstevel@tonic-gate 		ms->ms_term = 0;
1107c478bd9Sstevel@tonic-gate 		ms->ms_state_start = curtime;
1117c478bd9Sstevel@tonic-gate 		t->t_mstate = init_state;
1127c478bd9Sstevel@tonic-gate 		t->t_waitrq = 0;
1137c478bd9Sstevel@tonic-gate 		t->t_hrtime = curtime;
1147c478bd9Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_MSACCT) == 0)
1157c478bd9Sstevel@tonic-gate 			t->t_proc_flag |= TP_MSACCT;
1167c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ms->ms_acct[0], sizeof (ms->ms_acct));
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * Initialize the microstate level and associated accounting information
1227c478bd9Sstevel@tonic-gate  * for the specified cpu
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate init_cpu_mstate(
1277c478bd9Sstevel@tonic-gate 	cpu_t *cpu,
1287c478bd9Sstevel@tonic-gate 	int init_state)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	ASSERT(init_state != CMS_DISABLED);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate = init_state;
1337c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate_start = gethrtime_unscaled();
1347c478bd9Sstevel@tonic-gate 	cpu->cpu_waitrq = 0;
1357c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&cpu->cpu_acct[0], sizeof (cpu->cpu_acct));
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * sets cpu state to OFFLINE.  We don't actually track this time,
1407c478bd9Sstevel@tonic-gate  * but it serves as a useful placeholder state for when we're not
1417c478bd9Sstevel@tonic-gate  * doing anything.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate void
1457c478bd9Sstevel@tonic-gate term_cpu_mstate(struct cpu *cpu)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_mstate != CMS_DISABLED);
1487c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate = CMS_DISABLED;
1497c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate_start = 0;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1529102d475Sesolom /* NEW_CPU_MSTATE comments inline in new_cpu_mstate below. */
1539102d475Sesolom 
1549102d475Sesolom #define	NEW_CPU_MSTATE(state)						\
1559102d475Sesolom 	gen = cpu->cpu_mstate_gen;					\
1569102d475Sesolom 	cpu->cpu_mstate_gen = 0;					\
1579102d475Sesolom 	/* Need membar_producer() here if stores not ordered / TSO */	\
1589102d475Sesolom 	cpu->cpu_acct[cpu->cpu_mstate] += curtime - cpu->cpu_mstate_start; \
1599102d475Sesolom 	cpu->cpu_mstate = state;					\
1609102d475Sesolom 	cpu->cpu_mstate_start = curtime;				\
1619102d475Sesolom 	/* Need membar_producer() here if stores not ordered / TSO */	\
1629102d475Sesolom 	cpu->cpu_mstate_gen = (++gen == 0) ? 1 : gen;
1639102d475Sesolom 
1647c478bd9Sstevel@tonic-gate void
165eda89462Sesolom new_cpu_mstate(int cmstate, hrtime_t curtime)
1667c478bd9Sstevel@tonic-gate {
167eda89462Sesolom 	cpu_t *cpu = CPU;
168eda89462Sesolom 	uint16_t gen;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_mstate != CMS_DISABLED);
1717c478bd9Sstevel@tonic-gate 	ASSERT(cmstate < NCMSTATES);
1727c478bd9Sstevel@tonic-gate 	ASSERT(cmstate != CMS_DISABLED);
173eda89462Sesolom 
174eda89462Sesolom 	/*
175eda89462Sesolom 	 * This function cannot be re-entrant on a given CPU. As such,
176eda89462Sesolom 	 * we ASSERT and panic if we are called on behalf of an interrupt.
177eda89462Sesolom 	 * The one exception is for an interrupt which has previously
178eda89462Sesolom 	 * blocked. Such an interrupt is being scheduled by the dispatcher
179eda89462Sesolom 	 * just like a normal thread, and as such cannot arrive here
180eda89462Sesolom 	 * in a re-entrant manner.
181eda89462Sesolom 	 */
182eda89462Sesolom 
183eda89462Sesolom 	ASSERT(!CPU_ON_INTR(cpu) && curthread->t_intr == NULL);
1847c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt > 0 || curthread == cpu->cpu_idle_thread);
1857c478bd9Sstevel@tonic-gate 
186eda89462Sesolom 	/*
187eda89462Sesolom 	 * LOCKING, or lack thereof:
188eda89462Sesolom 	 *
189eda89462Sesolom 	 * Updates to CPU mstate can only be made by the CPU
190eda89462Sesolom 	 * itself, and the above check to ignore interrupts
191eda89462Sesolom 	 * should prevent recursion into this function on a given
192eda89462Sesolom 	 * processor. i.e. no possible write contention.
193eda89462Sesolom 	 *
194eda89462Sesolom 	 * However, reads of CPU mstate can occur at any time
195eda89462Sesolom 	 * from any CPU. Any locking added to this code path
196eda89462Sesolom 	 * would seriously impact syscall performance. So,
197eda89462Sesolom 	 * instead we have a best-effort protection for readers.
198eda89462Sesolom 	 * The reader will want to account for any time between
199eda89462Sesolom 	 * cpu_mstate_start and the present time. This requires
200eda89462Sesolom 	 * some guarantees that the reader is getting coherent
201eda89462Sesolom 	 * information.
202eda89462Sesolom 	 *
203eda89462Sesolom 	 * We use a generation counter, which is set to 0 before
204eda89462Sesolom 	 * we start making changes, and is set to a new value
205eda89462Sesolom 	 * after we're done. Someone reading the CPU mstate
206eda89462Sesolom 	 * should check for the same non-zero value of this
207eda89462Sesolom 	 * counter both before and after reading all state. The
208eda89462Sesolom 	 * important point is that the reader is not a
209eda89462Sesolom 	 * performance-critical path, but this function is.
2109102d475Sesolom 	 *
2119102d475Sesolom 	 * The ordering of writes is critical. cpu_mstate_gen must
2129102d475Sesolom 	 * be visibly zero on all CPUs before we change cpu_mstate
2139102d475Sesolom 	 * and cpu_mstate_start. Additionally, cpu_mstate_gen must
2149102d475Sesolom 	 * not be restored to oldgen+1 until after all of the other
2159102d475Sesolom 	 * writes have become visible.
2169102d475Sesolom 	 *
2179102d475Sesolom 	 * Normally one puts membar_producer() calls to accomplish
2189102d475Sesolom 	 * this. Unfortunately this routine is extremely performance
2199102d475Sesolom 	 * critical (esp. in syscall_mstate below) and we cannot
2209102d475Sesolom 	 * afford the additional time, particularly on some x86
2219102d475Sesolom 	 * architectures with extremely slow sfence calls. On a
2229102d475Sesolom 	 * CPU which guarantees write ordering (including sparc, x86,
2239102d475Sesolom 	 * and amd64) this is not a problem. The compiler could still
2249102d475Sesolom 	 * reorder the writes, so we make the four cpu fields
2259102d475Sesolom 	 * volatile to prevent this.
2269102d475Sesolom 	 *
2279102d475Sesolom 	 * TSO warning: should we port to a non-TSO (or equivalent)
2289102d475Sesolom 	 * CPU, this will break.
2299102d475Sesolom 	 *
2309102d475Sesolom 	 * The reader stills needs the membar_consumer() calls because,
2319102d475Sesolom 	 * although the volatiles prevent the compiler from reordering
2329102d475Sesolom 	 * loads, the CPU can still do so.
233eda89462Sesolom 	 */
234eda89462Sesolom 
2359102d475Sesolom 	NEW_CPU_MSTATE(cmstate);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Return an aggregation of microstate times in scaled nanoseconds (high-res
2407c478bd9Sstevel@tonic-gate  * time).  This keeps in mind that p_acct is already scaled, and ms_acct is
2417c478bd9Sstevel@tonic-gate  * not.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate hrtime_t
2447c478bd9Sstevel@tonic-gate mstate_aggr_state(proc_t *p, int a_state)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	struct mstate *ms;
2477c478bd9Sstevel@tonic-gate 	kthread_t *t;
2487c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2497c478bd9Sstevel@tonic-gate 	hrtime_t aggr_time;
2507c478bd9Sstevel@tonic-gate 	hrtime_t scaledtime;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2537c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)a_state < NMSTATES);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	aggr_time = p->p_acct[a_state];
2567c478bd9Sstevel@tonic-gate 	if (a_state == LMS_SYSTEM)
2577c478bd9Sstevel@tonic-gate 		aggr_time += p->p_acct[LMS_TRAP];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
2607c478bd9Sstevel@tonic-gate 	if (t == NULL)
2617c478bd9Sstevel@tonic-gate 		return (aggr_time);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	do {
2647c478bd9Sstevel@tonic-gate 		if (t->t_proc_flag & TP_LWPEXIT)
2657c478bd9Sstevel@tonic-gate 			continue;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		lwp = ttolwp(t);
2687c478bd9Sstevel@tonic-gate 		ms = &lwp->lwp_mstate;
2697c478bd9Sstevel@tonic-gate 		scaledtime = ms->ms_acct[a_state];
2707c478bd9Sstevel@tonic-gate 		scalehrtime(&scaledtime);
2717c478bd9Sstevel@tonic-gate 		aggr_time += scaledtime;
2727c478bd9Sstevel@tonic-gate 		if (a_state == LMS_SYSTEM) {
2737c478bd9Sstevel@tonic-gate 			scaledtime = ms->ms_acct[LMS_TRAP];
2747c478bd9Sstevel@tonic-gate 			scalehrtime(&scaledtime);
2757c478bd9Sstevel@tonic-gate 			aggr_time += scaledtime;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (aggr_time);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2829102d475Sesolom 
2837c478bd9Sstevel@tonic-gate void
2847c478bd9Sstevel@tonic-gate syscall_mstate(int fromms, int toms)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
2877c478bd9Sstevel@tonic-gate 	struct mstate *ms;
2887c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
2897c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
2907c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
2917c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
2929102d475Sesolom 	cpu_t *cpu;
2939102d475Sesolom 	uint16_t gen;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	ASSERT(fromms < NMSTATES);
2997c478bd9Sstevel@tonic-gate 	ASSERT(toms < NMSTATES);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
3027c478bd9Sstevel@tonic-gate 	mstimep = &ms->ms_acct[fromms];
3037c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
3047c478bd9Sstevel@tonic-gate 	newtime = curtime - ms->ms_state_start;
3057c478bd9Sstevel@tonic-gate 	while (newtime < 0) {
3067c478bd9Sstevel@tonic-gate 		curtime = gethrtime_unscaled();
3077c478bd9Sstevel@tonic-gate 		newtime = curtime - ms->ms_state_start;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	*mstimep += newtime;
3107c478bd9Sstevel@tonic-gate 	t->t_mstate = toms;
3117c478bd9Sstevel@tonic-gate 	ms->ms_state_start = curtime;
3127c478bd9Sstevel@tonic-gate 	ms->ms_prev = fromms;
313eda89462Sesolom 	kpreempt_disable(); /* don't change CPU while changing CPU's state */
3149102d475Sesolom 	cpu = CPU;
3159102d475Sesolom 	ASSERT(cpu == t->t_cpu);
3169102d475Sesolom 	if ((toms != LMS_USER) && (cpu->cpu_mstate != CMS_SYSTEM)) {
3179102d475Sesolom 		NEW_CPU_MSTATE(CMS_SYSTEM);
3189102d475Sesolom 	} else if ((toms == LMS_USER) && (cpu->cpu_mstate != CMS_USER)) {
3199102d475Sesolom 		NEW_CPU_MSTATE(CMS_USER);
3209102d475Sesolom 	}
3217c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3249102d475Sesolom #undef NEW_CPU_MSTATE
3259102d475Sesolom 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * The following is for computing the percentage of cpu time used recently
3287c478bd9Sstevel@tonic-gate  * by an lwp.  The function cpu_decay() is also called from /proc code.
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * exp_x(x):
3317c478bd9Sstevel@tonic-gate  * Given x as a 64-bit non-negative scaled integer of arbitrary magnitude,
3327c478bd9Sstevel@tonic-gate  * Return exp(-x) as a 64-bit scaled integer in the range [0 .. 1].
3337c478bd9Sstevel@tonic-gate  *
3347c478bd9Sstevel@tonic-gate  * Scaling for 64-bit scaled integer:
3357c478bd9Sstevel@tonic-gate  * The binary point is to the right of the high-order bit
3367c478bd9Sstevel@tonic-gate  * of the low-order 32-bit word.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate #define	LSHIFT	31
3407c478bd9Sstevel@tonic-gate #define	LSI_ONE	((uint32_t)1 << LSHIFT)	/* 32-bit scaled integer 1 */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate #ifdef DEBUG
3437c478bd9Sstevel@tonic-gate uint_t expx_cnt = 0;	/* number of calls to exp_x() */
3447c478bd9Sstevel@tonic-gate uint_t expx_mul = 0;	/* number of long multiplies in exp_x() */
3457c478bd9Sstevel@tonic-gate #endif
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static uint64_t
3487c478bd9Sstevel@tonic-gate exp_x(uint64_t x)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	int i;
3517c478bd9Sstevel@tonic-gate 	uint64_t ull;
3527c478bd9Sstevel@tonic-gate 	uint32_t ui;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate #ifdef DEBUG
3557c478bd9Sstevel@tonic-gate 	expx_cnt++;
3567c478bd9Sstevel@tonic-gate #endif
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * By the formula:
3597c478bd9Sstevel@tonic-gate 	 *	exp(-x) = exp(-x/2) * exp(-x/2)
3607c478bd9Sstevel@tonic-gate 	 * we keep halving x until it becomes small enough for
3617c478bd9Sstevel@tonic-gate 	 * the following approximation to be accurate enough:
3627c478bd9Sstevel@tonic-gate 	 *	exp(-x) = 1 - x
3637c478bd9Sstevel@tonic-gate 	 * We reduce x until it is less than 1/4 (the 2 in LSHIFT-2 below).
3647c478bd9Sstevel@tonic-gate 	 * Our final error will be smaller than 4% .
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Use a uint64_t for the initial shift calculation.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	ull = x >> (LSHIFT-2);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * Short circuit:
3747c478bd9Sstevel@tonic-gate 	 * A number this large produces effectively 0 (actually .005).
3757c478bd9Sstevel@tonic-gate 	 * This way, we will never do more than 5 multiplies.
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 	if (ull >= (1 << 5))
3787c478bd9Sstevel@tonic-gate 		return (0);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	ui = ull;	/* OK.  Now we can use a uint_t. */
3817c478bd9Sstevel@tonic-gate 	for (i = 0; ui != 0; i++)
3827c478bd9Sstevel@tonic-gate 		ui >>= 1;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (i != 0) {
3857c478bd9Sstevel@tonic-gate #ifdef DEBUG
3867c478bd9Sstevel@tonic-gate 		expx_mul += i;	/* seldom happens */
3877c478bd9Sstevel@tonic-gate #endif
3887c478bd9Sstevel@tonic-gate 		x >>= i;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Now we compute 1 - x and square it the number of times
3937c478bd9Sstevel@tonic-gate 	 * that we halved x above to produce the final result:
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 	x = LSI_ONE - x;
3967c478bd9Sstevel@tonic-gate 	while (i--)
3977c478bd9Sstevel@tonic-gate 		x = (x * x) >> LSHIFT;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	return (x);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Given the old percent cpu and a time delta in nanoseconds,
4047c478bd9Sstevel@tonic-gate  * return the new decayed percent cpu:  pct * exp(-tau),
4057c478bd9Sstevel@tonic-gate  * where 'tau' is the time delta multiplied by a decay factor.
4067c478bd9Sstevel@tonic-gate  * We have chosen the decay factor (cpu_decay_factor in param.c)
4077c478bd9Sstevel@tonic-gate  * to make the decay over five seconds be approximately 20%.
4087c478bd9Sstevel@tonic-gate  *
4097c478bd9Sstevel@tonic-gate  * 'pct' is a 32-bit scaled integer <= 1
4107c478bd9Sstevel@tonic-gate  * The binary point is to the right of the high-order bit
4117c478bd9Sstevel@tonic-gate  * of the 32-bit word.
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate static uint32_t
4147c478bd9Sstevel@tonic-gate cpu_decay(uint32_t pct, hrtime_t nsec)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	uint64_t delta = (uint64_t)nsec;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	delta /= cpu_decay_factor;
4197c478bd9Sstevel@tonic-gate 	return ((pct * exp_x(delta)) >> LSHIFT);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate  * Given the old percent cpu and a time delta in nanoseconds,
4247c478bd9Sstevel@tonic-gate  * return the new grown percent cpu:  1 - ( 1 - pct ) * exp(-tau)
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate static uint32_t
4277c478bd9Sstevel@tonic-gate cpu_grow(uint32_t pct, hrtime_t nsec)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	return (LSI_ONE - cpu_decay(LSI_ONE - pct, nsec));
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Defined to determine whether a lwp is still on a processor.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate #define	T_ONPROC(kt)	\
4387c478bd9Sstevel@tonic-gate 	((kt)->t_mstate < LMS_SLEEP)
4397c478bd9Sstevel@tonic-gate #define	T_OFFPROC(kt)	\
4407c478bd9Sstevel@tonic-gate 	((kt)->t_mstate >= LMS_SLEEP)
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate uint_t
4437c478bd9Sstevel@tonic-gate cpu_update_pct(kthread_t *t, hrtime_t newtime)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	hrtime_t delta;
4467c478bd9Sstevel@tonic-gate 	hrtime_t hrlb;
4477c478bd9Sstevel@tonic-gate 	uint_t pctcpu;
4487c478bd9Sstevel@tonic-gate 	uint_t npctcpu;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * This routine can get called at PIL > 0, this *has* to be
4527c478bd9Sstevel@tonic-gate 	 * done atomically. Holding locks here causes bad things to happen.
4537c478bd9Sstevel@tonic-gate 	 * (read: deadlock).
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	do {
4577c478bd9Sstevel@tonic-gate 		if (T_ONPROC(t) && t->t_waitrq == 0) {
4587c478bd9Sstevel@tonic-gate 			hrlb = t->t_hrtime;
4597c478bd9Sstevel@tonic-gate 			delta = newtime - hrlb;
4607c478bd9Sstevel@tonic-gate 			if (delta < 0) {
4617c478bd9Sstevel@tonic-gate 				newtime = gethrtime_unscaled();
4627c478bd9Sstevel@tonic-gate 				delta = newtime - hrlb;
4637c478bd9Sstevel@tonic-gate 			}
4647c478bd9Sstevel@tonic-gate 			t->t_hrtime = newtime;
4657c478bd9Sstevel@tonic-gate 			scalehrtime(&delta);
4667c478bd9Sstevel@tonic-gate 			pctcpu = t->t_pctcpu;
4677c478bd9Sstevel@tonic-gate 			npctcpu = cpu_grow(pctcpu, delta);
4687c478bd9Sstevel@tonic-gate 		} else {
4697c478bd9Sstevel@tonic-gate 			hrlb = t->t_hrtime;
4707c478bd9Sstevel@tonic-gate 			delta = newtime - hrlb;
4717c478bd9Sstevel@tonic-gate 			if (delta < 0) {
4727c478bd9Sstevel@tonic-gate 				newtime = gethrtime_unscaled();
4737c478bd9Sstevel@tonic-gate 				delta = newtime - hrlb;
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			t->t_hrtime = newtime;
4767c478bd9Sstevel@tonic-gate 			scalehrtime(&delta);
4777c478bd9Sstevel@tonic-gate 			pctcpu = t->t_pctcpu;
4787c478bd9Sstevel@tonic-gate 			npctcpu = cpu_decay(pctcpu, delta);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 	} while (cas32(&t->t_pctcpu, pctcpu, npctcpu) != pctcpu);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	return (npctcpu);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  * Change the microstate level for the LWP and update the
4877c478bd9Sstevel@tonic-gate  * associated accounting information.  Return the previous
4887c478bd9Sstevel@tonic-gate  * LWP state.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate int
4917c478bd9Sstevel@tonic-gate new_mstate(kthread_t *t, int new_state)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	struct mstate *ms;
4947c478bd9Sstevel@tonic-gate 	unsigned state;
4957c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
4967c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
4977c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
4987c478bd9Sstevel@tonic-gate 	hrtime_t oldtime;
4997c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	ASSERT(new_state != LMS_WAIT_CPU);
5027c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)new_state < NMSTATES);
5037c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread || THREAD_LOCK_HELD(t));
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
5067c478bd9Sstevel@tonic-gate 		return (LMS_SYSTEM);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/* adjust cpu percentages before we go any further */
5117c478bd9Sstevel@tonic-gate 	(void) cpu_update_pct(t, curtime);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
5147c478bd9Sstevel@tonic-gate 	state = t->t_mstate;
5157c478bd9Sstevel@tonic-gate 	do {
5167c478bd9Sstevel@tonic-gate 		switch (state) {
5177c478bd9Sstevel@tonic-gate 		case LMS_TFAULT:
5187c478bd9Sstevel@tonic-gate 		case LMS_DFAULT:
5197c478bd9Sstevel@tonic-gate 		case LMS_KFAULT:
5207c478bd9Sstevel@tonic-gate 		case LMS_USER_LOCK:
5217c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_SYSTEM];
5227c478bd9Sstevel@tonic-gate 			break;
5237c478bd9Sstevel@tonic-gate 		default:
5247c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[state];
5257c478bd9Sstevel@tonic-gate 			break;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 		newtime = curtime - ms->ms_state_start;
5287c478bd9Sstevel@tonic-gate 		if (newtime < 0) {
5297c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
5307c478bd9Sstevel@tonic-gate 			oldtime = *mstimep - 1; /* force CAS to fail */
5317c478bd9Sstevel@tonic-gate 			continue;
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		oldtime = *mstimep;
5347c478bd9Sstevel@tonic-gate 		newtime += oldtime;
5357c478bd9Sstevel@tonic-gate 		t->t_mstate = new_state;
5367c478bd9Sstevel@tonic-gate 		ms->ms_state_start = curtime;
5377c478bd9Sstevel@tonic-gate 	} while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Remember the previous running microstate.
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	if (state != LMS_SLEEP && state != LMS_STOPPED)
5427c478bd9Sstevel@tonic-gate 		ms->ms_prev = state;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * Switch CPU microstate if appropriate
5467c478bd9Sstevel@tonic-gate 	 */
547eda89462Sesolom 
5487c478bd9Sstevel@tonic-gate 	kpreempt_disable(); /* MUST disable kpreempt before touching t->cpu */
549eda89462Sesolom 	ASSERT(t->t_cpu == CPU);
550eda89462Sesolom 	if (!CPU_ON_INTR(t->t_cpu) && curthread->t_intr == NULL) {
551eda89462Sesolom 		if (new_state == LMS_USER && t->t_cpu->cpu_mstate != CMS_USER)
552eda89462Sesolom 			new_cpu_mstate(CMS_USER, curtime);
553eda89462Sesolom 		else if (new_state != LMS_USER &&
554eda89462Sesolom 		    t->t_cpu->cpu_mstate != CMS_SYSTEM)
555eda89462Sesolom 			new_cpu_mstate(CMS_SYSTEM, curtime);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	kpreempt_enable();
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (ms->ms_prev);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * Restore the LWP microstate to the previous runnable state.
5647c478bd9Sstevel@tonic-gate  * Called from disp() with the newly selected lwp.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate void
5677c478bd9Sstevel@tonic-gate restore_mstate(kthread_t *t)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	struct mstate *ms;
5707c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
5717c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
5727c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
5737c478bd9Sstevel@tonic-gate 	hrtime_t waitrq;
5747c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
5757c478bd9Sstevel@tonic-gate 	hrtime_t oldtime;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
5817c478bd9Sstevel@tonic-gate 	(void) cpu_update_pct(t, curtime);
5827c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
5837c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)t->t_mstate < NMSTATES);
5847c478bd9Sstevel@tonic-gate 	do {
5857c478bd9Sstevel@tonic-gate 		switch (t->t_mstate) {
5867c478bd9Sstevel@tonic-gate 		case LMS_SLEEP:
5877c478bd9Sstevel@tonic-gate 			/*
5887c478bd9Sstevel@tonic-gate 			 * Update the timer for the current sleep state.
5897c478bd9Sstevel@tonic-gate 			 */
5907c478bd9Sstevel@tonic-gate 			ASSERT((unsigned)ms->ms_prev < NMSTATES);
5917c478bd9Sstevel@tonic-gate 			switch (ms->ms_prev) {
5927c478bd9Sstevel@tonic-gate 			case LMS_TFAULT:
5937c478bd9Sstevel@tonic-gate 			case LMS_DFAULT:
5947c478bd9Sstevel@tonic-gate 			case LMS_KFAULT:
5957c478bd9Sstevel@tonic-gate 			case LMS_USER_LOCK:
5967c478bd9Sstevel@tonic-gate 				mstimep = &ms->ms_acct[ms->ms_prev];
5977c478bd9Sstevel@tonic-gate 				break;
5987c478bd9Sstevel@tonic-gate 			default:
5997c478bd9Sstevel@tonic-gate 				mstimep = &ms->ms_acct[LMS_SLEEP];
6007c478bd9Sstevel@tonic-gate 				break;
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 			/*
6037c478bd9Sstevel@tonic-gate 			 * Return to the previous run state.
6047c478bd9Sstevel@tonic-gate 			 */
6057c478bd9Sstevel@tonic-gate 			t->t_mstate = ms->ms_prev;
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 		case LMS_STOPPED:
6087c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_STOPPED];
6097c478bd9Sstevel@tonic-gate 			/*
6107c478bd9Sstevel@tonic-gate 			 * Return to the previous run state.
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 			t->t_mstate = ms->ms_prev;
6137c478bd9Sstevel@tonic-gate 			break;
6147c478bd9Sstevel@tonic-gate 		case LMS_TFAULT:
6157c478bd9Sstevel@tonic-gate 		case LMS_DFAULT:
6167c478bd9Sstevel@tonic-gate 		case LMS_KFAULT:
6177c478bd9Sstevel@tonic-gate 		case LMS_USER_LOCK:
6187c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_SYSTEM];
6197c478bd9Sstevel@tonic-gate 			break;
6207c478bd9Sstevel@tonic-gate 		default:
6217c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[t->t_mstate];
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 		waitrq = t->t_waitrq;	/* hopefully atomic */
625*f2bd4627Sjohansen 		if (waitrq == 0) {
6267c478bd9Sstevel@tonic-gate 			waitrq = curtime;
6277c478bd9Sstevel@tonic-gate 		}
628*f2bd4627Sjohansen 		t->t_waitrq = 0;
6297c478bd9Sstevel@tonic-gate 		newtime = waitrq - ms->ms_state_start;
6307c478bd9Sstevel@tonic-gate 		if (newtime < 0) {
6317c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
6327c478bd9Sstevel@tonic-gate 			oldtime = *mstimep - 1; /* force CAS to fail */
6337c478bd9Sstevel@tonic-gate 			continue;
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 		oldtime = *mstimep;
6367c478bd9Sstevel@tonic-gate 		newtime += oldtime;
6377c478bd9Sstevel@tonic-gate 	} while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Update the WAIT_CPU timer and per-cpu waitrq total.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	ms->ms_acct[LMS_WAIT_CPU] += (curtime - waitrq);
642b3383343Smishra 	CPU->cpu_waitrq += (curtime - waitrq);
6437c478bd9Sstevel@tonic-gate 	ms->ms_state_start = curtime;
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * Copy lwp microstate accounting and resource usage information
6487c478bd9Sstevel@tonic-gate  * to the process.  (lwp is terminating)
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate void
6517c478bd9Sstevel@tonic-gate term_mstate(kthread_t *t)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	struct mstate *ms;
6547c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
6557c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
6567c478bd9Sstevel@tonic-gate 	int i;
6577c478bd9Sstevel@tonic-gate 	hrtime_t tmp;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
6627c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_STOPPED);
6637c478bd9Sstevel@tonic-gate 	ms->ms_term = ms->ms_state_start;
6647c478bd9Sstevel@tonic-gate 	tmp = ms->ms_term - ms->ms_start;
6657c478bd9Sstevel@tonic-gate 	scalehrtime(&tmp);
6667c478bd9Sstevel@tonic-gate 	p->p_mlreal += tmp;
6677c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMSTATES; i++) {
6687c478bd9Sstevel@tonic-gate 		tmp = ms->ms_acct[i];
6697c478bd9Sstevel@tonic-gate 		scalehrtime(&tmp);
6707c478bd9Sstevel@tonic-gate 		p->p_acct[i] += tmp;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	p->p_ru.minflt   += lwp->lwp_ru.minflt;
6737c478bd9Sstevel@tonic-gate 	p->p_ru.majflt   += lwp->lwp_ru.majflt;
6747c478bd9Sstevel@tonic-gate 	p->p_ru.nswap    += lwp->lwp_ru.nswap;
6757c478bd9Sstevel@tonic-gate 	p->p_ru.inblock  += lwp->lwp_ru.inblock;
6767c478bd9Sstevel@tonic-gate 	p->p_ru.oublock  += lwp->lwp_ru.oublock;
6777c478bd9Sstevel@tonic-gate 	p->p_ru.msgsnd   += lwp->lwp_ru.msgsnd;
6787c478bd9Sstevel@tonic-gate 	p->p_ru.msgrcv   += lwp->lwp_ru.msgrcv;
6797c478bd9Sstevel@tonic-gate 	p->p_ru.nsignals += lwp->lwp_ru.nsignals;
6807c478bd9Sstevel@tonic-gate 	p->p_ru.nvcsw    += lwp->lwp_ru.nvcsw;
6817c478bd9Sstevel@tonic-gate 	p->p_ru.nivcsw   += lwp->lwp_ru.nivcsw;
6827c478bd9Sstevel@tonic-gate 	p->p_ru.sysc	 += lwp->lwp_ru.sysc;
6837c478bd9Sstevel@tonic-gate 	p->p_ru.ioch	 += lwp->lwp_ru.ioch;
6847c478bd9Sstevel@tonic-gate 	p->p_defunct++;
6857c478bd9Sstevel@tonic-gate }
686