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