xref: /titanic_53/usr/src/uts/common/os/msacct.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Mega-theory block comment:
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * Microstate accounting uses finite states and the transitions between these
44*7c478bd9Sstevel@tonic-gate  * states to measure timing and accounting information.  The state information
45*7c478bd9Sstevel@tonic-gate  * is presently tracked for threads (via microstate accounting) and cpus (via
46*7c478bd9Sstevel@tonic-gate  * cpu microstate accounting).  In each case, these accounting mechanisms use
47*7c478bd9Sstevel@tonic-gate  * states and transitions to measure time spent in each state instead of
48*7c478bd9Sstevel@tonic-gate  * clock-based sampling methodologies.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * For microstate accounting:
51*7c478bd9Sstevel@tonic-gate  * state transitions are accomplished by calling new_mstate() to switch between
52*7c478bd9Sstevel@tonic-gate  * states.  Transitions from a sleeping state (LMS_SLEEP and LMS_STOPPED) occur
53*7c478bd9Sstevel@tonic-gate  * by calling restore_mstate() which restores a thread to its previously running
54*7c478bd9Sstevel@tonic-gate  * state.  This code is primarialy executed by the dispatcher in disp() before
55*7c478bd9Sstevel@tonic-gate  * running a process that was put to sleep.  If the thread was not in a sleeping
56*7c478bd9Sstevel@tonic-gate  * state, this call has little effect other than to update the count of time the
57*7c478bd9Sstevel@tonic-gate  * thread has spent waiting on run-queues in its lifetime.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * For cpu microstate accounting:
60*7c478bd9Sstevel@tonic-gate  * Cpu microstate accounting is similar to the microstate accounting for threads
61*7c478bd9Sstevel@tonic-gate  * but it tracks user, system, and idle time for cpus.  Cpu microstate
62*7c478bd9Sstevel@tonic-gate  * accounting does not track interrupt times as there is a pre-existing
63*7c478bd9Sstevel@tonic-gate  * interrupt accounting mechanism for this purpose.  Cpu microstate accounting
64*7c478bd9Sstevel@tonic-gate  * tracks time that user threads have spent active, idle, or in the system on a
65*7c478bd9Sstevel@tonic-gate  * given cpu.  Cpu microstate accounting has fewer states which allows it to
66*7c478bd9Sstevel@tonic-gate  * have better defined transitions.  The states transition in the following
67*7c478bd9Sstevel@tonic-gate  * order:
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *  CMS_USER <-> CMS_SYSTEM <-> CMS_IDLE
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  * In order to get to the idle state, the cpu microstate must first go through
72*7c478bd9Sstevel@tonic-gate  * the system state, and vice-versa for the user state from idle.  The switching
73*7c478bd9Sstevel@tonic-gate  * of the microstates from user to system is done as part of the regular thread
74*7c478bd9Sstevel@tonic-gate  * microstate accounting code, except for the idle state which is switched by
75*7c478bd9Sstevel@tonic-gate  * the dispatcher before it runs the idle loop.
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * Cpu percentages:
78*7c478bd9Sstevel@tonic-gate  * Cpu percentages are now handled by and based upon microstate accounting
79*7c478bd9Sstevel@tonic-gate  * information (the same is true for load averages).  The routines which handle
80*7c478bd9Sstevel@tonic-gate  * the growing/shrinking and exponentiation of cpu percentages have been moved
81*7c478bd9Sstevel@tonic-gate  * here as it now makes more sense for them to be generated from the microstate
82*7c478bd9Sstevel@tonic-gate  * code.  Cpu percentages are generated similarly to the way they were before;
83*7c478bd9Sstevel@tonic-gate  * however, now they are based upon high-resolution timestamps and the
84*7c478bd9Sstevel@tonic-gate  * timestamps are modified at various state changes instead of during a clock()
85*7c478bd9Sstevel@tonic-gate  * interrupt.  This allows us to generate more accurate cpu percentages which
86*7c478bd9Sstevel@tonic-gate  * are also in-sync with microstate data.
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * Initialize the microstate level and the
91*7c478bd9Sstevel@tonic-gate  * associated accounting information for an LWP.
92*7c478bd9Sstevel@tonic-gate  */
93*7c478bd9Sstevel@tonic-gate void
94*7c478bd9Sstevel@tonic-gate init_mstate(
95*7c478bd9Sstevel@tonic-gate 	kthread_t	*t,
96*7c478bd9Sstevel@tonic-gate 	int		init_state)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
99*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
100*7c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	ASSERT(init_state != LMS_WAIT_CPU);
103*7c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)init_state < NMSTATES);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) != NULL) {
106*7c478bd9Sstevel@tonic-gate 		ms = &lwp->lwp_mstate;
107*7c478bd9Sstevel@tonic-gate 		curtime = gethrtime_unscaled();
108*7c478bd9Sstevel@tonic-gate 		ms->ms_prev = LMS_SYSTEM;
109*7c478bd9Sstevel@tonic-gate 		ms->ms_start = curtime;
110*7c478bd9Sstevel@tonic-gate 		ms->ms_term = 0;
111*7c478bd9Sstevel@tonic-gate 		ms->ms_state_start = curtime;
112*7c478bd9Sstevel@tonic-gate 		t->t_mstate = init_state;
113*7c478bd9Sstevel@tonic-gate 		t->t_waitrq = 0;
114*7c478bd9Sstevel@tonic-gate 		t->t_hrtime = curtime;
115*7c478bd9Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_MSACCT) == 0)
116*7c478bd9Sstevel@tonic-gate 			t->t_proc_flag |= TP_MSACCT;
117*7c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ms->ms_acct[0], sizeof (ms->ms_acct));
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * Initialize the microstate level and associated accounting information
123*7c478bd9Sstevel@tonic-gate  * for the specified cpu
124*7c478bd9Sstevel@tonic-gate  */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate void
127*7c478bd9Sstevel@tonic-gate init_cpu_mstate(
128*7c478bd9Sstevel@tonic-gate 	cpu_t *cpu,
129*7c478bd9Sstevel@tonic-gate 	int init_state)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	ASSERT(init_state != CMS_DISABLED);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate = init_state;
134*7c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate_start = gethrtime_unscaled();
135*7c478bd9Sstevel@tonic-gate 	cpu->cpu_waitrq = 0;
136*7c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&cpu->cpu_acct[0], sizeof (cpu->cpu_acct));
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * sets cpu state to OFFLINE.  We don't actually track this time,
141*7c478bd9Sstevel@tonic-gate  * but it serves as a useful placeholder state for when we're not
142*7c478bd9Sstevel@tonic-gate  * doing anything.
143*7c478bd9Sstevel@tonic-gate  */
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate void
146*7c478bd9Sstevel@tonic-gate term_cpu_mstate(struct cpu *cpu)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_mstate != CMS_DISABLED);
149*7c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate = CMS_DISABLED;
150*7c478bd9Sstevel@tonic-gate 	cpu->cpu_mstate_start = 0;
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate void
154*7c478bd9Sstevel@tonic-gate new_cpu_mstate(cpu_t *cpu, int cmstate)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
157*7c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
158*7c478bd9Sstevel@tonic-gate 	hrtime_t oldtime;
159*7c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	ASSERT(cpu->cpu_mstate != CMS_DISABLED);
162*7c478bd9Sstevel@tonic-gate 	ASSERT(cmstate < NCMSTATES);
163*7c478bd9Sstevel@tonic-gate 	ASSERT(cmstate != CMS_DISABLED);
164*7c478bd9Sstevel@tonic-gate 	ASSERT(curthread->t_preempt > 0 || curthread == cpu->cpu_idle_thread);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
167*7c478bd9Sstevel@tonic-gate 	mstimep = &cpu->cpu_acct[cpu->cpu_mstate];
168*7c478bd9Sstevel@tonic-gate 	do {
169*7c478bd9Sstevel@tonic-gate 		newtime = curtime - cpu->cpu_mstate_start;
170*7c478bd9Sstevel@tonic-gate 		if (newtime < 0) {
171*7c478bd9Sstevel@tonic-gate 			/* force CAS to fail */
172*7c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
173*7c478bd9Sstevel@tonic-gate 			oldtime = *mstimep - 1;
174*7c478bd9Sstevel@tonic-gate 			continue;
175*7c478bd9Sstevel@tonic-gate 		}
176*7c478bd9Sstevel@tonic-gate 		oldtime = *mstimep;
177*7c478bd9Sstevel@tonic-gate 		newtime += oldtime;
178*7c478bd9Sstevel@tonic-gate 		cpu->cpu_mstate = cmstate;
179*7c478bd9Sstevel@tonic-gate 		cpu->cpu_mstate_start = curtime;
180*7c478bd9Sstevel@tonic-gate 	} while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate /*
184*7c478bd9Sstevel@tonic-gate  * Return an aggregation of microstate times in scaled nanoseconds (high-res
185*7c478bd9Sstevel@tonic-gate  * time).  This keeps in mind that p_acct is already scaled, and ms_acct is
186*7c478bd9Sstevel@tonic-gate  * not.
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate hrtime_t
189*7c478bd9Sstevel@tonic-gate mstate_aggr_state(proc_t *p, int a_state)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
192*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
193*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
194*7c478bd9Sstevel@tonic-gate 	hrtime_t aggr_time;
195*7c478bd9Sstevel@tonic-gate 	hrtime_t scaledtime;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
198*7c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)a_state < NMSTATES);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	aggr_time = p->p_acct[a_state];
201*7c478bd9Sstevel@tonic-gate 	if (a_state == LMS_SYSTEM)
202*7c478bd9Sstevel@tonic-gate 		aggr_time += p->p_acct[LMS_TRAP];
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
205*7c478bd9Sstevel@tonic-gate 	if (t == NULL)
206*7c478bd9Sstevel@tonic-gate 		return (aggr_time);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	do {
209*7c478bd9Sstevel@tonic-gate 		if (t->t_proc_flag & TP_LWPEXIT)
210*7c478bd9Sstevel@tonic-gate 			continue;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		lwp = ttolwp(t);
213*7c478bd9Sstevel@tonic-gate 		ms = &lwp->lwp_mstate;
214*7c478bd9Sstevel@tonic-gate 		scaledtime = ms->ms_acct[a_state];
215*7c478bd9Sstevel@tonic-gate 		scalehrtime(&scaledtime);
216*7c478bd9Sstevel@tonic-gate 		aggr_time += scaledtime;
217*7c478bd9Sstevel@tonic-gate 		if (a_state == LMS_SYSTEM) {
218*7c478bd9Sstevel@tonic-gate 			scaledtime = ms->ms_acct[LMS_TRAP];
219*7c478bd9Sstevel@tonic-gate 			scalehrtime(&scaledtime);
220*7c478bd9Sstevel@tonic-gate 			aggr_time += scaledtime;
221*7c478bd9Sstevel@tonic-gate 		}
222*7c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	return (aggr_time);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate void
228*7c478bd9Sstevel@tonic-gate syscall_mstate(int fromms, int toms)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
231*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
232*7c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
233*7c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
234*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
235*7c478bd9Sstevel@tonic-gate 	struct cpu *cpup;
236*7c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
239*7c478bd9Sstevel@tonic-gate 		return;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	ASSERT(fromms < NMSTATES);
242*7c478bd9Sstevel@tonic-gate 	ASSERT(toms < NMSTATES);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
245*7c478bd9Sstevel@tonic-gate 	mstimep = &ms->ms_acct[fromms];
246*7c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
247*7c478bd9Sstevel@tonic-gate 	newtime = curtime - ms->ms_state_start;
248*7c478bd9Sstevel@tonic-gate 	while (newtime < 0) {
249*7c478bd9Sstevel@tonic-gate 		curtime = gethrtime_unscaled();
250*7c478bd9Sstevel@tonic-gate 		newtime = curtime - ms->ms_state_start;
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 	*mstimep += newtime;
253*7c478bd9Sstevel@tonic-gate 	t->t_mstate = toms;
254*7c478bd9Sstevel@tonic-gate 	ms->ms_state_start = curtime;
255*7c478bd9Sstevel@tonic-gate 	ms->ms_prev = fromms;
256*7c478bd9Sstevel@tonic-gate 	/*
257*7c478bd9Sstevel@tonic-gate 	 * Here, you could call new_cpu_mstate() to switch the cpu
258*7c478bd9Sstevel@tonic-gate 	 * microstate.  However, in the interest of making things
259*7c478bd9Sstevel@tonic-gate 	 * as expeditious as possible, the relevant work has been inlined.
260*7c478bd9Sstevel@tonic-gate 	 */
261*7c478bd9Sstevel@tonic-gate 	kpreempt_disable(); /* MUST disable kpreempt before touching t->cpu */
262*7c478bd9Sstevel@tonic-gate 	cpup = t->t_cpu;
263*7c478bd9Sstevel@tonic-gate 	ASSERT(cpup->cpu_mstate != CMS_DISABLED);
264*7c478bd9Sstevel@tonic-gate 	if ((toms != LMS_USER) && (cpup->cpu_mstate != CMS_SYSTEM)) {
265*7c478bd9Sstevel@tonic-gate 		mstimep = &cpup->cpu_acct[CMS_USER];
266*7c478bd9Sstevel@tonic-gate 		newtime = curtime - cpup->cpu_mstate_start;
267*7c478bd9Sstevel@tonic-gate 		while (newtime < 0) {
268*7c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
269*7c478bd9Sstevel@tonic-gate 			newtime = curtime - cpup->cpu_mstate_start;
270*7c478bd9Sstevel@tonic-gate 		}
271*7c478bd9Sstevel@tonic-gate 		*mstimep += newtime;
272*7c478bd9Sstevel@tonic-gate 		cpup->cpu_mstate = CMS_SYSTEM;
273*7c478bd9Sstevel@tonic-gate 		cpup->cpu_mstate_start = curtime;
274*7c478bd9Sstevel@tonic-gate 	} else if ((toms == LMS_USER) && (cpup->cpu_mstate != CMS_USER)) {
275*7c478bd9Sstevel@tonic-gate 		mstimep = &cpup->cpu_acct[CMS_SYSTEM];
276*7c478bd9Sstevel@tonic-gate 		newtime = curtime - cpup->cpu_mstate_start;
277*7c478bd9Sstevel@tonic-gate 		while (newtime < 0) {
278*7c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
279*7c478bd9Sstevel@tonic-gate 			newtime = curtime - cpup->cpu_mstate_start;
280*7c478bd9Sstevel@tonic-gate 		}
281*7c478bd9Sstevel@tonic-gate 		*mstimep += newtime;
282*7c478bd9Sstevel@tonic-gate 		cpup->cpu_mstate = CMS_USER;
283*7c478bd9Sstevel@tonic-gate 		cpup->cpu_mstate_start = curtime;
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /*
289*7c478bd9Sstevel@tonic-gate  * The following is for computing the percentage of cpu time used recently
290*7c478bd9Sstevel@tonic-gate  * by an lwp.  The function cpu_decay() is also called from /proc code.
291*7c478bd9Sstevel@tonic-gate  *
292*7c478bd9Sstevel@tonic-gate  * exp_x(x):
293*7c478bd9Sstevel@tonic-gate  * Given x as a 64-bit non-negative scaled integer of arbitrary magnitude,
294*7c478bd9Sstevel@tonic-gate  * Return exp(-x) as a 64-bit scaled integer in the range [0 .. 1].
295*7c478bd9Sstevel@tonic-gate  *
296*7c478bd9Sstevel@tonic-gate  * Scaling for 64-bit scaled integer:
297*7c478bd9Sstevel@tonic-gate  * The binary point is to the right of the high-order bit
298*7c478bd9Sstevel@tonic-gate  * of the low-order 32-bit word.
299*7c478bd9Sstevel@tonic-gate  */
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate #define	LSHIFT	31
302*7c478bd9Sstevel@tonic-gate #define	LSI_ONE	((uint32_t)1 << LSHIFT)	/* 32-bit scaled integer 1 */
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
305*7c478bd9Sstevel@tonic-gate uint_t expx_cnt = 0;	/* number of calls to exp_x() */
306*7c478bd9Sstevel@tonic-gate uint_t expx_mul = 0;	/* number of long multiplies in exp_x() */
307*7c478bd9Sstevel@tonic-gate #endif
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate static uint64_t
310*7c478bd9Sstevel@tonic-gate exp_x(uint64_t x)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	int i;
313*7c478bd9Sstevel@tonic-gate 	uint64_t ull;
314*7c478bd9Sstevel@tonic-gate 	uint32_t ui;
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
317*7c478bd9Sstevel@tonic-gate 	expx_cnt++;
318*7c478bd9Sstevel@tonic-gate #endif
319*7c478bd9Sstevel@tonic-gate 	/*
320*7c478bd9Sstevel@tonic-gate 	 * By the formula:
321*7c478bd9Sstevel@tonic-gate 	 *	exp(-x) = exp(-x/2) * exp(-x/2)
322*7c478bd9Sstevel@tonic-gate 	 * we keep halving x until it becomes small enough for
323*7c478bd9Sstevel@tonic-gate 	 * the following approximation to be accurate enough:
324*7c478bd9Sstevel@tonic-gate 	 *	exp(-x) = 1 - x
325*7c478bd9Sstevel@tonic-gate 	 * We reduce x until it is less than 1/4 (the 2 in LSHIFT-2 below).
326*7c478bd9Sstevel@tonic-gate 	 * Our final error will be smaller than 4% .
327*7c478bd9Sstevel@tonic-gate 	 */
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	/*
330*7c478bd9Sstevel@tonic-gate 	 * Use a uint64_t for the initial shift calculation.
331*7c478bd9Sstevel@tonic-gate 	 */
332*7c478bd9Sstevel@tonic-gate 	ull = x >> (LSHIFT-2);
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	/*
335*7c478bd9Sstevel@tonic-gate 	 * Short circuit:
336*7c478bd9Sstevel@tonic-gate 	 * A number this large produces effectively 0 (actually .005).
337*7c478bd9Sstevel@tonic-gate 	 * This way, we will never do more than 5 multiplies.
338*7c478bd9Sstevel@tonic-gate 	 */
339*7c478bd9Sstevel@tonic-gate 	if (ull >= (1 << 5))
340*7c478bd9Sstevel@tonic-gate 		return (0);
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	ui = ull;	/* OK.  Now we can use a uint_t. */
343*7c478bd9Sstevel@tonic-gate 	for (i = 0; ui != 0; i++)
344*7c478bd9Sstevel@tonic-gate 		ui >>= 1;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if (i != 0) {
347*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
348*7c478bd9Sstevel@tonic-gate 		expx_mul += i;	/* seldom happens */
349*7c478bd9Sstevel@tonic-gate #endif
350*7c478bd9Sstevel@tonic-gate 		x >>= i;
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	/*
354*7c478bd9Sstevel@tonic-gate 	 * Now we compute 1 - x and square it the number of times
355*7c478bd9Sstevel@tonic-gate 	 * that we halved x above to produce the final result:
356*7c478bd9Sstevel@tonic-gate 	 */
357*7c478bd9Sstevel@tonic-gate 	x = LSI_ONE - x;
358*7c478bd9Sstevel@tonic-gate 	while (i--)
359*7c478bd9Sstevel@tonic-gate 		x = (x * x) >> LSHIFT;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	return (x);
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * Given the old percent cpu and a time delta in nanoseconds,
366*7c478bd9Sstevel@tonic-gate  * return the new decayed percent cpu:  pct * exp(-tau),
367*7c478bd9Sstevel@tonic-gate  * where 'tau' is the time delta multiplied by a decay factor.
368*7c478bd9Sstevel@tonic-gate  * We have chosen the decay factor (cpu_decay_factor in param.c)
369*7c478bd9Sstevel@tonic-gate  * to make the decay over five seconds be approximately 20%.
370*7c478bd9Sstevel@tonic-gate  *
371*7c478bd9Sstevel@tonic-gate  * 'pct' is a 32-bit scaled integer <= 1
372*7c478bd9Sstevel@tonic-gate  * The binary point is to the right of the high-order bit
373*7c478bd9Sstevel@tonic-gate  * of the 32-bit word.
374*7c478bd9Sstevel@tonic-gate  */
375*7c478bd9Sstevel@tonic-gate static uint32_t
376*7c478bd9Sstevel@tonic-gate cpu_decay(uint32_t pct, hrtime_t nsec)
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate 	uint64_t delta = (uint64_t)nsec;
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	delta /= cpu_decay_factor;
381*7c478bd9Sstevel@tonic-gate 	return ((pct * exp_x(delta)) >> LSHIFT);
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate /*
385*7c478bd9Sstevel@tonic-gate  * Given the old percent cpu and a time delta in nanoseconds,
386*7c478bd9Sstevel@tonic-gate  * return the new grown percent cpu:  1 - ( 1 - pct ) * exp(-tau)
387*7c478bd9Sstevel@tonic-gate  */
388*7c478bd9Sstevel@tonic-gate static uint32_t
389*7c478bd9Sstevel@tonic-gate cpu_grow(uint32_t pct, hrtime_t nsec)
390*7c478bd9Sstevel@tonic-gate {
391*7c478bd9Sstevel@tonic-gate 	return (LSI_ONE - cpu_decay(LSI_ONE - pct, nsec));
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /*
396*7c478bd9Sstevel@tonic-gate  * Defined to determine whether a lwp is still on a processor.
397*7c478bd9Sstevel@tonic-gate  */
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate #define	T_ONPROC(kt)	\
400*7c478bd9Sstevel@tonic-gate 	((kt)->t_mstate < LMS_SLEEP)
401*7c478bd9Sstevel@tonic-gate #define	T_OFFPROC(kt)	\
402*7c478bd9Sstevel@tonic-gate 	((kt)->t_mstate >= LMS_SLEEP)
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate uint_t
405*7c478bd9Sstevel@tonic-gate cpu_update_pct(kthread_t *t, hrtime_t newtime)
406*7c478bd9Sstevel@tonic-gate {
407*7c478bd9Sstevel@tonic-gate 	hrtime_t delta;
408*7c478bd9Sstevel@tonic-gate 	hrtime_t hrlb;
409*7c478bd9Sstevel@tonic-gate 	uint_t pctcpu;
410*7c478bd9Sstevel@tonic-gate 	uint_t npctcpu;
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	/*
413*7c478bd9Sstevel@tonic-gate 	 * This routine can get called at PIL > 0, this *has* to be
414*7c478bd9Sstevel@tonic-gate 	 * done atomically. Holding locks here causes bad things to happen.
415*7c478bd9Sstevel@tonic-gate 	 * (read: deadlock).
416*7c478bd9Sstevel@tonic-gate 	 */
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	do {
419*7c478bd9Sstevel@tonic-gate 		if (T_ONPROC(t) && t->t_waitrq == 0) {
420*7c478bd9Sstevel@tonic-gate 			hrlb = t->t_hrtime;
421*7c478bd9Sstevel@tonic-gate 			delta = newtime - hrlb;
422*7c478bd9Sstevel@tonic-gate 			if (delta < 0) {
423*7c478bd9Sstevel@tonic-gate 				newtime = gethrtime_unscaled();
424*7c478bd9Sstevel@tonic-gate 				delta = newtime - hrlb;
425*7c478bd9Sstevel@tonic-gate 			}
426*7c478bd9Sstevel@tonic-gate 			t->t_hrtime = newtime;
427*7c478bd9Sstevel@tonic-gate 			scalehrtime(&delta);
428*7c478bd9Sstevel@tonic-gate 			pctcpu = t->t_pctcpu;
429*7c478bd9Sstevel@tonic-gate 			npctcpu = cpu_grow(pctcpu, delta);
430*7c478bd9Sstevel@tonic-gate 		} else {
431*7c478bd9Sstevel@tonic-gate 			hrlb = t->t_hrtime;
432*7c478bd9Sstevel@tonic-gate 			delta = newtime - hrlb;
433*7c478bd9Sstevel@tonic-gate 			if (delta < 0) {
434*7c478bd9Sstevel@tonic-gate 				newtime = gethrtime_unscaled();
435*7c478bd9Sstevel@tonic-gate 				delta = newtime - hrlb;
436*7c478bd9Sstevel@tonic-gate 			}
437*7c478bd9Sstevel@tonic-gate 			t->t_hrtime = newtime;
438*7c478bd9Sstevel@tonic-gate 			scalehrtime(&delta);
439*7c478bd9Sstevel@tonic-gate 			pctcpu = t->t_pctcpu;
440*7c478bd9Sstevel@tonic-gate 			npctcpu = cpu_decay(pctcpu, delta);
441*7c478bd9Sstevel@tonic-gate 		}
442*7c478bd9Sstevel@tonic-gate 	} while (cas32(&t->t_pctcpu, pctcpu, npctcpu) != pctcpu);
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	return (npctcpu);
445*7c478bd9Sstevel@tonic-gate }
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate /*
448*7c478bd9Sstevel@tonic-gate  * Change the microstate level for the LWP and update the
449*7c478bd9Sstevel@tonic-gate  * associated accounting information.  Return the previous
450*7c478bd9Sstevel@tonic-gate  * LWP state.
451*7c478bd9Sstevel@tonic-gate  */
452*7c478bd9Sstevel@tonic-gate int
453*7c478bd9Sstevel@tonic-gate new_mstate(kthread_t *t, int new_state)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
456*7c478bd9Sstevel@tonic-gate 	unsigned state;
457*7c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
458*7c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
459*7c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
460*7c478bd9Sstevel@tonic-gate 	hrtime_t oldtime;
461*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	ASSERT(new_state != LMS_WAIT_CPU);
464*7c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)new_state < NMSTATES);
465*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread || THREAD_LOCK_HELD(t));
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
468*7c478bd9Sstevel@tonic-gate 		return (LMS_SYSTEM);
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
471*7c478bd9Sstevel@tonic-gate 
472*7c478bd9Sstevel@tonic-gate 	/* adjust cpu percentages before we go any further */
473*7c478bd9Sstevel@tonic-gate 	(void) cpu_update_pct(t, curtime);
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
476*7c478bd9Sstevel@tonic-gate 	state = t->t_mstate;
477*7c478bd9Sstevel@tonic-gate 	do {
478*7c478bd9Sstevel@tonic-gate 		switch (state) {
479*7c478bd9Sstevel@tonic-gate 		case LMS_TFAULT:
480*7c478bd9Sstevel@tonic-gate 		case LMS_DFAULT:
481*7c478bd9Sstevel@tonic-gate 		case LMS_KFAULT:
482*7c478bd9Sstevel@tonic-gate 		case LMS_USER_LOCK:
483*7c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_SYSTEM];
484*7c478bd9Sstevel@tonic-gate 			break;
485*7c478bd9Sstevel@tonic-gate 		default:
486*7c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[state];
487*7c478bd9Sstevel@tonic-gate 			break;
488*7c478bd9Sstevel@tonic-gate 		}
489*7c478bd9Sstevel@tonic-gate 		newtime = curtime - ms->ms_state_start;
490*7c478bd9Sstevel@tonic-gate 		if (newtime < 0) {
491*7c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
492*7c478bd9Sstevel@tonic-gate 			oldtime = *mstimep - 1; /* force CAS to fail */
493*7c478bd9Sstevel@tonic-gate 			continue;
494*7c478bd9Sstevel@tonic-gate 		}
495*7c478bd9Sstevel@tonic-gate 		oldtime = *mstimep;
496*7c478bd9Sstevel@tonic-gate 		newtime += oldtime;
497*7c478bd9Sstevel@tonic-gate 		t->t_mstate = new_state;
498*7c478bd9Sstevel@tonic-gate 		ms->ms_state_start = curtime;
499*7c478bd9Sstevel@tonic-gate 	} while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
500*7c478bd9Sstevel@tonic-gate 	/*
501*7c478bd9Sstevel@tonic-gate 	 * Remember the previous running microstate.
502*7c478bd9Sstevel@tonic-gate 	 */
503*7c478bd9Sstevel@tonic-gate 	if (state != LMS_SLEEP && state != LMS_STOPPED)
504*7c478bd9Sstevel@tonic-gate 		ms->ms_prev = state;
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	/*
507*7c478bd9Sstevel@tonic-gate 	 * Switch CPU microstate if appropriate
508*7c478bd9Sstevel@tonic-gate 	 */
509*7c478bd9Sstevel@tonic-gate 	kpreempt_disable(); /* MUST disable kpreempt before touching t->cpu */
510*7c478bd9Sstevel@tonic-gate 	if (new_state == LMS_USER && t->t_cpu->cpu_mstate != CMS_USER) {
511*7c478bd9Sstevel@tonic-gate 		new_cpu_mstate(t->t_cpu, CMS_USER);
512*7c478bd9Sstevel@tonic-gate 	} else if (new_state != LMS_USER &&
513*7c478bd9Sstevel@tonic-gate 	    t->t_cpu->cpu_mstate != CMS_SYSTEM) {
514*7c478bd9Sstevel@tonic-gate 		new_cpu_mstate(t->t_cpu, CMS_SYSTEM);
515*7c478bd9Sstevel@tonic-gate 	}
516*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	return (ms->ms_prev);
519*7c478bd9Sstevel@tonic-gate }
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate static long waitrqis0 = 0;
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate /*
524*7c478bd9Sstevel@tonic-gate  * Restore the LWP microstate to the previous runnable state.
525*7c478bd9Sstevel@tonic-gate  * Called from disp() with the newly selected lwp.
526*7c478bd9Sstevel@tonic-gate  */
527*7c478bd9Sstevel@tonic-gate void
528*7c478bd9Sstevel@tonic-gate restore_mstate(kthread_t *t)
529*7c478bd9Sstevel@tonic-gate {
530*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
531*7c478bd9Sstevel@tonic-gate 	hrtime_t *mstimep;
532*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
533*7c478bd9Sstevel@tonic-gate 	hrtime_t curtime;
534*7c478bd9Sstevel@tonic-gate 	hrtime_t waitrq;
535*7c478bd9Sstevel@tonic-gate 	hrtime_t newtime;
536*7c478bd9Sstevel@tonic-gate 	hrtime_t oldtime;
537*7c478bd9Sstevel@tonic-gate 	struct cpu *cpup;
538*7c478bd9Sstevel@tonic-gate 
539*7c478bd9Sstevel@tonic-gate 	if ((lwp = ttolwp(t)) == NULL)
540*7c478bd9Sstevel@tonic-gate 		return;
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	curtime = gethrtime_unscaled();
543*7c478bd9Sstevel@tonic-gate 	(void) cpu_update_pct(t, curtime);
544*7c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
545*7c478bd9Sstevel@tonic-gate 	ASSERT((unsigned)t->t_mstate < NMSTATES);
546*7c478bd9Sstevel@tonic-gate 	do {
547*7c478bd9Sstevel@tonic-gate 		switch (t->t_mstate) {
548*7c478bd9Sstevel@tonic-gate 		case LMS_SLEEP:
549*7c478bd9Sstevel@tonic-gate 			/*
550*7c478bd9Sstevel@tonic-gate 			 * Update the timer for the current sleep state.
551*7c478bd9Sstevel@tonic-gate 			 */
552*7c478bd9Sstevel@tonic-gate 			ASSERT((unsigned)ms->ms_prev < NMSTATES);
553*7c478bd9Sstevel@tonic-gate 			switch (ms->ms_prev) {
554*7c478bd9Sstevel@tonic-gate 			case LMS_TFAULT:
555*7c478bd9Sstevel@tonic-gate 			case LMS_DFAULT:
556*7c478bd9Sstevel@tonic-gate 			case LMS_KFAULT:
557*7c478bd9Sstevel@tonic-gate 			case LMS_USER_LOCK:
558*7c478bd9Sstevel@tonic-gate 				mstimep = &ms->ms_acct[ms->ms_prev];
559*7c478bd9Sstevel@tonic-gate 				break;
560*7c478bd9Sstevel@tonic-gate 			default:
561*7c478bd9Sstevel@tonic-gate 				mstimep = &ms->ms_acct[LMS_SLEEP];
562*7c478bd9Sstevel@tonic-gate 				break;
563*7c478bd9Sstevel@tonic-gate 			}
564*7c478bd9Sstevel@tonic-gate 			/*
565*7c478bd9Sstevel@tonic-gate 			 * Return to the previous run state.
566*7c478bd9Sstevel@tonic-gate 			 */
567*7c478bd9Sstevel@tonic-gate 			t->t_mstate = ms->ms_prev;
568*7c478bd9Sstevel@tonic-gate 			break;
569*7c478bd9Sstevel@tonic-gate 		case LMS_STOPPED:
570*7c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_STOPPED];
571*7c478bd9Sstevel@tonic-gate 			/*
572*7c478bd9Sstevel@tonic-gate 			 * Return to the previous run state.
573*7c478bd9Sstevel@tonic-gate 			 */
574*7c478bd9Sstevel@tonic-gate 			t->t_mstate = ms->ms_prev;
575*7c478bd9Sstevel@tonic-gate 			break;
576*7c478bd9Sstevel@tonic-gate 		case LMS_TFAULT:
577*7c478bd9Sstevel@tonic-gate 		case LMS_DFAULT:
578*7c478bd9Sstevel@tonic-gate 		case LMS_KFAULT:
579*7c478bd9Sstevel@tonic-gate 		case LMS_USER_LOCK:
580*7c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[LMS_SYSTEM];
581*7c478bd9Sstevel@tonic-gate 			break;
582*7c478bd9Sstevel@tonic-gate 		default:
583*7c478bd9Sstevel@tonic-gate 			mstimep = &ms->ms_acct[t->t_mstate];
584*7c478bd9Sstevel@tonic-gate 			break;
585*7c478bd9Sstevel@tonic-gate 		}
586*7c478bd9Sstevel@tonic-gate 		waitrq = t->t_waitrq;	/* hopefully atomic */
587*7c478bd9Sstevel@tonic-gate 		t->t_waitrq = 0;
588*7c478bd9Sstevel@tonic-gate 		if (waitrq == 0) {	/* should only happen during boot */
589*7c478bd9Sstevel@tonic-gate 			waitrq = curtime;
590*7c478bd9Sstevel@tonic-gate 			waitrqis0++;
591*7c478bd9Sstevel@tonic-gate 		}
592*7c478bd9Sstevel@tonic-gate 		newtime = waitrq - ms->ms_state_start;
593*7c478bd9Sstevel@tonic-gate 		if (newtime < 0) {
594*7c478bd9Sstevel@tonic-gate 			curtime = gethrtime_unscaled();
595*7c478bd9Sstevel@tonic-gate 			oldtime = *mstimep - 1; /* force CAS to fail */
596*7c478bd9Sstevel@tonic-gate 			continue;
597*7c478bd9Sstevel@tonic-gate 		}
598*7c478bd9Sstevel@tonic-gate 		oldtime = *mstimep;
599*7c478bd9Sstevel@tonic-gate 		newtime += oldtime;
600*7c478bd9Sstevel@tonic-gate 	} while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
601*7c478bd9Sstevel@tonic-gate 	/*
602*7c478bd9Sstevel@tonic-gate 	 * Update the WAIT_CPU timer and per-cpu waitrq total.
603*7c478bd9Sstevel@tonic-gate 	 */
604*7c478bd9Sstevel@tonic-gate 	cpup = t->t_disp_queue->disp_cpu;
605*7c478bd9Sstevel@tonic-gate 	if (cpup == NULL)
606*7c478bd9Sstevel@tonic-gate 		cpup = t->t_cpu;
607*7c478bd9Sstevel@tonic-gate 	ms->ms_acct[LMS_WAIT_CPU] += (curtime - waitrq);
608*7c478bd9Sstevel@tonic-gate 	cpup->cpu_waitrq += (curtime - waitrq);
609*7c478bd9Sstevel@tonic-gate 	ms->ms_state_start = curtime;
610*7c478bd9Sstevel@tonic-gate }
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate /*
613*7c478bd9Sstevel@tonic-gate  * Copy lwp microstate accounting and resource usage information
614*7c478bd9Sstevel@tonic-gate  * to the process.  (lwp is terminating)
615*7c478bd9Sstevel@tonic-gate  */
616*7c478bd9Sstevel@tonic-gate void
617*7c478bd9Sstevel@tonic-gate term_mstate(kthread_t *t)
618*7c478bd9Sstevel@tonic-gate {
619*7c478bd9Sstevel@tonic-gate 	struct mstate *ms;
620*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
621*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
622*7c478bd9Sstevel@tonic-gate 	int i;
623*7c478bd9Sstevel@tonic-gate 	hrtime_t tmp;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 	ms = &lwp->lwp_mstate;
628*7c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_STOPPED);
629*7c478bd9Sstevel@tonic-gate 	ms->ms_term = ms->ms_state_start;
630*7c478bd9Sstevel@tonic-gate 	tmp = ms->ms_term - ms->ms_start;
631*7c478bd9Sstevel@tonic-gate 	scalehrtime(&tmp);
632*7c478bd9Sstevel@tonic-gate 	p->p_mlreal += tmp;
633*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NMSTATES; i++) {
634*7c478bd9Sstevel@tonic-gate 		tmp = ms->ms_acct[i];
635*7c478bd9Sstevel@tonic-gate 		scalehrtime(&tmp);
636*7c478bd9Sstevel@tonic-gate 		p->p_acct[i] += tmp;
637*7c478bd9Sstevel@tonic-gate 	}
638*7c478bd9Sstevel@tonic-gate 	p->p_ru.minflt   += lwp->lwp_ru.minflt;
639*7c478bd9Sstevel@tonic-gate 	p->p_ru.majflt   += lwp->lwp_ru.majflt;
640*7c478bd9Sstevel@tonic-gate 	p->p_ru.nswap    += lwp->lwp_ru.nswap;
641*7c478bd9Sstevel@tonic-gate 	p->p_ru.inblock  += lwp->lwp_ru.inblock;
642*7c478bd9Sstevel@tonic-gate 	p->p_ru.oublock  += lwp->lwp_ru.oublock;
643*7c478bd9Sstevel@tonic-gate 	p->p_ru.msgsnd   += lwp->lwp_ru.msgsnd;
644*7c478bd9Sstevel@tonic-gate 	p->p_ru.msgrcv   += lwp->lwp_ru.msgrcv;
645*7c478bd9Sstevel@tonic-gate 	p->p_ru.nsignals += lwp->lwp_ru.nsignals;
646*7c478bd9Sstevel@tonic-gate 	p->p_ru.nvcsw    += lwp->lwp_ru.nvcsw;
647*7c478bd9Sstevel@tonic-gate 	p->p_ru.nivcsw   += lwp->lwp_ru.nivcsw;
648*7c478bd9Sstevel@tonic-gate 	p->p_ru.sysc	 += lwp->lwp_ru.sysc;
649*7c478bd9Sstevel@tonic-gate 	p->p_ru.ioch	 += lwp->lwp_ru.ioch;
650*7c478bd9Sstevel@tonic-gate 	p->p_defunct++;
651*7c478bd9Sstevel@tonic-gate }
652