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