10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53426Sjohansen * Common Development and Distribution License (the "License").
63426Sjohansen * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11173SJonathan.Adams@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/user.h>
300Sstevel@tonic-gate #include <sys/proc.h>
310Sstevel@tonic-gate #include <sys/cpuvar.h>
320Sstevel@tonic-gate #include <sys/thread.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/msacct.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * Mega-theory block comment:
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * Microstate accounting uses finite states and the transitions between these
410Sstevel@tonic-gate * states to measure timing and accounting information. The state information
420Sstevel@tonic-gate * is presently tracked for threads (via microstate accounting) and cpus (via
430Sstevel@tonic-gate * cpu microstate accounting). In each case, these accounting mechanisms use
440Sstevel@tonic-gate * states and transitions to measure time spent in each state instead of
450Sstevel@tonic-gate * clock-based sampling methodologies.
460Sstevel@tonic-gate *
470Sstevel@tonic-gate * For microstate accounting:
480Sstevel@tonic-gate * state transitions are accomplished by calling new_mstate() to switch between
490Sstevel@tonic-gate * states. Transitions from a sleeping state (LMS_SLEEP and LMS_STOPPED) occur
500Sstevel@tonic-gate * by calling restore_mstate() which restores a thread to its previously running
510Sstevel@tonic-gate * state. This code is primarialy executed by the dispatcher in disp() before
520Sstevel@tonic-gate * running a process that was put to sleep. If the thread was not in a sleeping
530Sstevel@tonic-gate * state, this call has little effect other than to update the count of time the
540Sstevel@tonic-gate * thread has spent waiting on run-queues in its lifetime.
550Sstevel@tonic-gate *
560Sstevel@tonic-gate * For cpu microstate accounting:
570Sstevel@tonic-gate * Cpu microstate accounting is similar to the microstate accounting for threads
580Sstevel@tonic-gate * but it tracks user, system, and idle time for cpus. Cpu microstate
590Sstevel@tonic-gate * accounting does not track interrupt times as there is a pre-existing
600Sstevel@tonic-gate * interrupt accounting mechanism for this purpose. Cpu microstate accounting
610Sstevel@tonic-gate * tracks time that user threads have spent active, idle, or in the system on a
620Sstevel@tonic-gate * given cpu. Cpu microstate accounting has fewer states which allows it to
630Sstevel@tonic-gate * have better defined transitions. The states transition in the following
640Sstevel@tonic-gate * order:
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * CMS_USER <-> CMS_SYSTEM <-> CMS_IDLE
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * In order to get to the idle state, the cpu microstate must first go through
690Sstevel@tonic-gate * the system state, and vice-versa for the user state from idle. The switching
700Sstevel@tonic-gate * of the microstates from user to system is done as part of the regular thread
710Sstevel@tonic-gate * microstate accounting code, except for the idle state which is switched by
720Sstevel@tonic-gate * the dispatcher before it runs the idle loop.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * Cpu percentages:
750Sstevel@tonic-gate * Cpu percentages are now handled by and based upon microstate accounting
760Sstevel@tonic-gate * information (the same is true for load averages). The routines which handle
770Sstevel@tonic-gate * the growing/shrinking and exponentiation of cpu percentages have been moved
780Sstevel@tonic-gate * here as it now makes more sense for them to be generated from the microstate
790Sstevel@tonic-gate * code. Cpu percentages are generated similarly to the way they were before;
800Sstevel@tonic-gate * however, now they are based upon high-resolution timestamps and the
810Sstevel@tonic-gate * timestamps are modified at various state changes instead of during a clock()
820Sstevel@tonic-gate * interrupt. This allows us to generate more accurate cpu percentages which
830Sstevel@tonic-gate * are also in-sync with microstate data.
840Sstevel@tonic-gate */
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * Initialize the microstate level and the
880Sstevel@tonic-gate * associated accounting information for an LWP.
890Sstevel@tonic-gate */
900Sstevel@tonic-gate void
init_mstate(kthread_t * t,int init_state)910Sstevel@tonic-gate init_mstate(
920Sstevel@tonic-gate kthread_t *t,
930Sstevel@tonic-gate int init_state)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate struct mstate *ms;
960Sstevel@tonic-gate klwp_t *lwp;
970Sstevel@tonic-gate hrtime_t curtime;
980Sstevel@tonic-gate
990Sstevel@tonic-gate ASSERT(init_state != LMS_WAIT_CPU);
1000Sstevel@tonic-gate ASSERT((unsigned)init_state < NMSTATES);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if ((lwp = ttolwp(t)) != NULL) {
1030Sstevel@tonic-gate ms = &lwp->lwp_mstate;
1040Sstevel@tonic-gate curtime = gethrtime_unscaled();
1050Sstevel@tonic-gate ms->ms_prev = LMS_SYSTEM;
1060Sstevel@tonic-gate ms->ms_start = curtime;
1070Sstevel@tonic-gate ms->ms_term = 0;
1080Sstevel@tonic-gate ms->ms_state_start = curtime;
1090Sstevel@tonic-gate t->t_mstate = init_state;
1100Sstevel@tonic-gate t->t_waitrq = 0;
1110Sstevel@tonic-gate t->t_hrtime = curtime;
1120Sstevel@tonic-gate if ((t->t_proc_flag & TP_MSACCT) == 0)
1130Sstevel@tonic-gate t->t_proc_flag |= TP_MSACCT;
1140Sstevel@tonic-gate bzero((caddr_t)&ms->ms_acct[0], sizeof (ms->ms_acct));
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate * Initialize the microstate level and associated accounting information
1200Sstevel@tonic-gate * for the specified cpu
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate void
init_cpu_mstate(cpu_t * cpu,int init_state)1240Sstevel@tonic-gate init_cpu_mstate(
1250Sstevel@tonic-gate cpu_t *cpu,
1260Sstevel@tonic-gate int init_state)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate ASSERT(init_state != CMS_DISABLED);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate cpu->cpu_mstate = init_state;
1310Sstevel@tonic-gate cpu->cpu_mstate_start = gethrtime_unscaled();
1320Sstevel@tonic-gate cpu->cpu_waitrq = 0;
1330Sstevel@tonic-gate bzero((caddr_t)&cpu->cpu_acct[0], sizeof (cpu->cpu_acct));
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * sets cpu state to OFFLINE. We don't actually track this time,
1380Sstevel@tonic-gate * but it serves as a useful placeholder state for when we're not
1390Sstevel@tonic-gate * doing anything.
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate void
term_cpu_mstate(struct cpu * cpu)1430Sstevel@tonic-gate term_cpu_mstate(struct cpu *cpu)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate ASSERT(cpu->cpu_mstate != CMS_DISABLED);
1460Sstevel@tonic-gate cpu->cpu_mstate = CMS_DISABLED;
1470Sstevel@tonic-gate cpu->cpu_mstate_start = 0;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1501058Sesolom /* NEW_CPU_MSTATE comments inline in new_cpu_mstate below. */
1511058Sesolom
1521058Sesolom #define NEW_CPU_MSTATE(state) \
1531058Sesolom gen = cpu->cpu_mstate_gen; \
1541058Sesolom cpu->cpu_mstate_gen = 0; \
1551058Sesolom /* Need membar_producer() here if stores not ordered / TSO */ \
1561058Sesolom cpu->cpu_acct[cpu->cpu_mstate] += curtime - cpu->cpu_mstate_start; \
1571058Sesolom cpu->cpu_mstate = state; \
1581058Sesolom cpu->cpu_mstate_start = curtime; \
1591058Sesolom /* Need membar_producer() here if stores not ordered / TSO */ \
1601058Sesolom cpu->cpu_mstate_gen = (++gen == 0) ? 1 : gen;
1611058Sesolom
1620Sstevel@tonic-gate void
new_cpu_mstate(int cmstate,hrtime_t curtime)163590Sesolom new_cpu_mstate(int cmstate, hrtime_t curtime)
1640Sstevel@tonic-gate {
165590Sesolom cpu_t *cpu = CPU;
166590Sesolom uint16_t gen;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate ASSERT(cpu->cpu_mstate != CMS_DISABLED);
1690Sstevel@tonic-gate ASSERT(cmstate < NCMSTATES);
1700Sstevel@tonic-gate ASSERT(cmstate != CMS_DISABLED);
171590Sesolom
172590Sesolom /*
173590Sesolom * This function cannot be re-entrant on a given CPU. As such,
174590Sesolom * we ASSERT and panic if we are called on behalf of an interrupt.
175590Sesolom * The one exception is for an interrupt which has previously
176590Sesolom * blocked. Such an interrupt is being scheduled by the dispatcher
177590Sesolom * just like a normal thread, and as such cannot arrive here
178590Sesolom * in a re-entrant manner.
179590Sesolom */
180590Sesolom
181590Sesolom ASSERT(!CPU_ON_INTR(cpu) && curthread->t_intr == NULL);
1820Sstevel@tonic-gate ASSERT(curthread->t_preempt > 0 || curthread == cpu->cpu_idle_thread);
1830Sstevel@tonic-gate
184590Sesolom /*
185590Sesolom * LOCKING, or lack thereof:
186590Sesolom *
187590Sesolom * Updates to CPU mstate can only be made by the CPU
188590Sesolom * itself, and the above check to ignore interrupts
189590Sesolom * should prevent recursion into this function on a given
190590Sesolom * processor. i.e. no possible write contention.
191590Sesolom *
192590Sesolom * However, reads of CPU mstate can occur at any time
193590Sesolom * from any CPU. Any locking added to this code path
194590Sesolom * would seriously impact syscall performance. So,
195590Sesolom * instead we have a best-effort protection for readers.
196590Sesolom * The reader will want to account for any time between
197590Sesolom * cpu_mstate_start and the present time. This requires
198590Sesolom * some guarantees that the reader is getting coherent
199590Sesolom * information.
200590Sesolom *
201590Sesolom * We use a generation counter, which is set to 0 before
202590Sesolom * we start making changes, and is set to a new value
203590Sesolom * after we're done. Someone reading the CPU mstate
204590Sesolom * should check for the same non-zero value of this
205590Sesolom * counter both before and after reading all state. The
206590Sesolom * important point is that the reader is not a
207590Sesolom * performance-critical path, but this function is.
2081058Sesolom *
2091058Sesolom * The ordering of writes is critical. cpu_mstate_gen must
2101058Sesolom * be visibly zero on all CPUs before we change cpu_mstate
2111058Sesolom * and cpu_mstate_start. Additionally, cpu_mstate_gen must
2121058Sesolom * not be restored to oldgen+1 until after all of the other
2131058Sesolom * writes have become visible.
2141058Sesolom *
2151058Sesolom * Normally one puts membar_producer() calls to accomplish
2161058Sesolom * this. Unfortunately this routine is extremely performance
2171058Sesolom * critical (esp. in syscall_mstate below) and we cannot
2181058Sesolom * afford the additional time, particularly on some x86
2191058Sesolom * architectures with extremely slow sfence calls. On a
2201058Sesolom * CPU which guarantees write ordering (including sparc, x86,
2211058Sesolom * and amd64) this is not a problem. The compiler could still
2221058Sesolom * reorder the writes, so we make the four cpu fields
2231058Sesolom * volatile to prevent this.
2241058Sesolom *
2251058Sesolom * TSO warning: should we port to a non-TSO (or equivalent)
2261058Sesolom * CPU, this will break.
2271058Sesolom *
2281058Sesolom * The reader stills needs the membar_consumer() calls because,
2291058Sesolom * although the volatiles prevent the compiler from reordering
2301058Sesolom * loads, the CPU can still do so.
231590Sesolom */
232590Sesolom
2331058Sesolom NEW_CPU_MSTATE(cmstate);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /*
2373792Sakolb * Return an aggregation of user and system CPU time consumed by
2383792Sakolb * the specified thread in scaled nanoseconds.
2393792Sakolb */
2403792Sakolb hrtime_t
mstate_thread_onproc_time(kthread_t * t)2413792Sakolb mstate_thread_onproc_time(kthread_t *t)
2423792Sakolb {
2433792Sakolb hrtime_t aggr_time;
2443792Sakolb hrtime_t now;
245*11173SJonathan.Adams@Sun.COM hrtime_t waitrq;
2463792Sakolb hrtime_t state_start;
2473792Sakolb struct mstate *ms;
2483792Sakolb klwp_t *lwp;
2493792Sakolb int mstate;
2503792Sakolb
2513792Sakolb ASSERT(THREAD_LOCK_HELD(t));
2523792Sakolb
2533792Sakolb if ((lwp = ttolwp(t)) == NULL)
2543792Sakolb return (0);
2553792Sakolb
2563792Sakolb mstate = t->t_mstate;
257*11173SJonathan.Adams@Sun.COM waitrq = t->t_waitrq;
2583792Sakolb ms = &lwp->lwp_mstate;
2593792Sakolb state_start = ms->ms_state_start;
2603792Sakolb
2613792Sakolb aggr_time = ms->ms_acct[LMS_USER] +
2623792Sakolb ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
2633792Sakolb
2643792Sakolb now = gethrtime_unscaled();
2653792Sakolb
2663792Sakolb /*
2673792Sakolb * NOTE: gethrtime_unscaled on X86 taken on different CPUs is
2683792Sakolb * inconsistent, so it is possible that now < state_start.
2693792Sakolb */
270*11173SJonathan.Adams@Sun.COM if (mstate == LMS_USER || mstate == LMS_SYSTEM || mstate == LMS_TRAP) {
271*11173SJonathan.Adams@Sun.COM /* if waitrq is zero, count all of the time. */
272*11173SJonathan.Adams@Sun.COM if (waitrq == 0) {
273*11173SJonathan.Adams@Sun.COM waitrq = now;
274*11173SJonathan.Adams@Sun.COM }
275*11173SJonathan.Adams@Sun.COM
276*11173SJonathan.Adams@Sun.COM if (waitrq > state_start) {
277*11173SJonathan.Adams@Sun.COM aggr_time += waitrq - state_start;
278*11173SJonathan.Adams@Sun.COM }
2793792Sakolb }
2803792Sakolb
2813792Sakolb scalehrtime(&aggr_time);
2823792Sakolb return (aggr_time);
2833792Sakolb }
2843792Sakolb
2853792Sakolb /*
286*11173SJonathan.Adams@Sun.COM * Return the amount of onproc and runnable time this thread has experienced.
287*11173SJonathan.Adams@Sun.COM *
288*11173SJonathan.Adams@Sun.COM * Because the fields we read are not protected by locks when updated
289*11173SJonathan.Adams@Sun.COM * by the thread itself, this is an inherently racey interface. In
290*11173SJonathan.Adams@Sun.COM * particular, the ASSERT(THREAD_LOCK_HELD(t)) doesn't guarantee as much
291*11173SJonathan.Adams@Sun.COM * as it might appear to.
292*11173SJonathan.Adams@Sun.COM *
293*11173SJonathan.Adams@Sun.COM * The implication for users of this interface is that onproc and runnable
294*11173SJonathan.Adams@Sun.COM * are *NOT* monotonically increasing; they may temporarily be larger than
295*11173SJonathan.Adams@Sun.COM * they should be.
296*11173SJonathan.Adams@Sun.COM */
297*11173SJonathan.Adams@Sun.COM void
mstate_systhread_times(kthread_t * t,hrtime_t * onproc,hrtime_t * runnable)298*11173SJonathan.Adams@Sun.COM mstate_systhread_times(kthread_t *t, hrtime_t *onproc, hrtime_t *runnable)
299*11173SJonathan.Adams@Sun.COM {
300*11173SJonathan.Adams@Sun.COM struct mstate *const ms = &ttolwp(t)->lwp_mstate;
301*11173SJonathan.Adams@Sun.COM
302*11173SJonathan.Adams@Sun.COM int mstate;
303*11173SJonathan.Adams@Sun.COM hrtime_t now;
304*11173SJonathan.Adams@Sun.COM hrtime_t state_start;
305*11173SJonathan.Adams@Sun.COM hrtime_t waitrq;
306*11173SJonathan.Adams@Sun.COM hrtime_t aggr_onp;
307*11173SJonathan.Adams@Sun.COM hrtime_t aggr_run;
308*11173SJonathan.Adams@Sun.COM
309*11173SJonathan.Adams@Sun.COM ASSERT(THREAD_LOCK_HELD(t));
310*11173SJonathan.Adams@Sun.COM ASSERT(t->t_procp->p_flag & SSYS);
311*11173SJonathan.Adams@Sun.COM ASSERT(ttolwp(t) != NULL);
312*11173SJonathan.Adams@Sun.COM
313*11173SJonathan.Adams@Sun.COM /* shouldn't be any non-SYSTEM on-CPU time */
314*11173SJonathan.Adams@Sun.COM ASSERT(ms->ms_acct[LMS_USER] == 0);
315*11173SJonathan.Adams@Sun.COM ASSERT(ms->ms_acct[LMS_TRAP] == 0);
316*11173SJonathan.Adams@Sun.COM
317*11173SJonathan.Adams@Sun.COM mstate = t->t_mstate;
318*11173SJonathan.Adams@Sun.COM waitrq = t->t_waitrq;
319*11173SJonathan.Adams@Sun.COM state_start = ms->ms_state_start;
320*11173SJonathan.Adams@Sun.COM
321*11173SJonathan.Adams@Sun.COM aggr_onp = ms->ms_acct[LMS_SYSTEM];
322*11173SJonathan.Adams@Sun.COM aggr_run = ms->ms_acct[LMS_WAIT_CPU];
323*11173SJonathan.Adams@Sun.COM
324*11173SJonathan.Adams@Sun.COM now = gethrtime_unscaled();
325*11173SJonathan.Adams@Sun.COM
326*11173SJonathan.Adams@Sun.COM /* if waitrq == 0, then there is no time to account to TS_RUN */
327*11173SJonathan.Adams@Sun.COM if (waitrq == 0)
328*11173SJonathan.Adams@Sun.COM waitrq = now;
329*11173SJonathan.Adams@Sun.COM
330*11173SJonathan.Adams@Sun.COM /* If there is system time to accumulate, do so */
331*11173SJonathan.Adams@Sun.COM if (mstate == LMS_SYSTEM && state_start < waitrq)
332*11173SJonathan.Adams@Sun.COM aggr_onp += waitrq - state_start;
333*11173SJonathan.Adams@Sun.COM
334*11173SJonathan.Adams@Sun.COM if (waitrq < now)
335*11173SJonathan.Adams@Sun.COM aggr_run += now - waitrq;
336*11173SJonathan.Adams@Sun.COM
337*11173SJonathan.Adams@Sun.COM scalehrtime(&aggr_onp);
338*11173SJonathan.Adams@Sun.COM scalehrtime(&aggr_run);
339*11173SJonathan.Adams@Sun.COM
340*11173SJonathan.Adams@Sun.COM *onproc = aggr_onp;
341*11173SJonathan.Adams@Sun.COM *runnable = aggr_run;
342*11173SJonathan.Adams@Sun.COM }
343*11173SJonathan.Adams@Sun.COM
344*11173SJonathan.Adams@Sun.COM /*
3450Sstevel@tonic-gate * Return an aggregation of microstate times in scaled nanoseconds (high-res
3460Sstevel@tonic-gate * time). This keeps in mind that p_acct is already scaled, and ms_acct is
3470Sstevel@tonic-gate * not.
3480Sstevel@tonic-gate */
3490Sstevel@tonic-gate hrtime_t
mstate_aggr_state(proc_t * p,int a_state)3500Sstevel@tonic-gate mstate_aggr_state(proc_t *p, int a_state)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate struct mstate *ms;
3530Sstevel@tonic-gate kthread_t *t;
3540Sstevel@tonic-gate klwp_t *lwp;
3550Sstevel@tonic-gate hrtime_t aggr_time;
3560Sstevel@tonic-gate hrtime_t scaledtime;
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
3590Sstevel@tonic-gate ASSERT((unsigned)a_state < NMSTATES);
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate aggr_time = p->p_acct[a_state];
3620Sstevel@tonic-gate if (a_state == LMS_SYSTEM)
3630Sstevel@tonic-gate aggr_time += p->p_acct[LMS_TRAP];
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate t = p->p_tlist;
3660Sstevel@tonic-gate if (t == NULL)
3670Sstevel@tonic-gate return (aggr_time);
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate do {
3700Sstevel@tonic-gate if (t->t_proc_flag & TP_LWPEXIT)
3710Sstevel@tonic-gate continue;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate lwp = ttolwp(t);
3740Sstevel@tonic-gate ms = &lwp->lwp_mstate;
3750Sstevel@tonic-gate scaledtime = ms->ms_acct[a_state];
3760Sstevel@tonic-gate scalehrtime(&scaledtime);
3770Sstevel@tonic-gate aggr_time += scaledtime;
3780Sstevel@tonic-gate if (a_state == LMS_SYSTEM) {
3790Sstevel@tonic-gate scaledtime = ms->ms_acct[LMS_TRAP];
3800Sstevel@tonic-gate scalehrtime(&scaledtime);
3810Sstevel@tonic-gate aggr_time += scaledtime;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist);
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate return (aggr_time);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
3881058Sesolom
3890Sstevel@tonic-gate void
syscall_mstate(int fromms,int toms)3900Sstevel@tonic-gate syscall_mstate(int fromms, int toms)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate kthread_t *t = curthread;
3930Sstevel@tonic-gate struct mstate *ms;
3940Sstevel@tonic-gate hrtime_t *mstimep;
3950Sstevel@tonic-gate hrtime_t curtime;
3960Sstevel@tonic-gate klwp_t *lwp;
3970Sstevel@tonic-gate hrtime_t newtime;
3981058Sesolom cpu_t *cpu;
3991058Sesolom uint16_t gen;
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL)
4020Sstevel@tonic-gate return;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate ASSERT(fromms < NMSTATES);
4050Sstevel@tonic-gate ASSERT(toms < NMSTATES);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate ms = &lwp->lwp_mstate;
4080Sstevel@tonic-gate mstimep = &ms->ms_acct[fromms];
4090Sstevel@tonic-gate curtime = gethrtime_unscaled();
4100Sstevel@tonic-gate newtime = curtime - ms->ms_state_start;
4110Sstevel@tonic-gate while (newtime < 0) {
4120Sstevel@tonic-gate curtime = gethrtime_unscaled();
4130Sstevel@tonic-gate newtime = curtime - ms->ms_state_start;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate *mstimep += newtime;
4160Sstevel@tonic-gate t->t_mstate = toms;
4170Sstevel@tonic-gate ms->ms_state_start = curtime;
4180Sstevel@tonic-gate ms->ms_prev = fromms;
419590Sesolom kpreempt_disable(); /* don't change CPU while changing CPU's state */
4201058Sesolom cpu = CPU;
4211058Sesolom ASSERT(cpu == t->t_cpu);
4221058Sesolom if ((toms != LMS_USER) && (cpu->cpu_mstate != CMS_SYSTEM)) {
4231058Sesolom NEW_CPU_MSTATE(CMS_SYSTEM);
4241058Sesolom } else if ((toms == LMS_USER) && (cpu->cpu_mstate != CMS_USER)) {
4251058Sesolom NEW_CPU_MSTATE(CMS_USER);
4261058Sesolom }
4270Sstevel@tonic-gate kpreempt_enable();
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4301058Sesolom #undef NEW_CPU_MSTATE
4311058Sesolom
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * The following is for computing the percentage of cpu time used recently
4340Sstevel@tonic-gate * by an lwp. The function cpu_decay() is also called from /proc code.
4350Sstevel@tonic-gate *
4360Sstevel@tonic-gate * exp_x(x):
4370Sstevel@tonic-gate * Given x as a 64-bit non-negative scaled integer of arbitrary magnitude,
4380Sstevel@tonic-gate * Return exp(-x) as a 64-bit scaled integer in the range [0 .. 1].
4390Sstevel@tonic-gate *
4400Sstevel@tonic-gate * Scaling for 64-bit scaled integer:
4410Sstevel@tonic-gate * The binary point is to the right of the high-order bit
4420Sstevel@tonic-gate * of the low-order 32-bit word.
4430Sstevel@tonic-gate */
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate #define LSHIFT 31
4460Sstevel@tonic-gate #define LSI_ONE ((uint32_t)1 << LSHIFT) /* 32-bit scaled integer 1 */
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate #ifdef DEBUG
4490Sstevel@tonic-gate uint_t expx_cnt = 0; /* number of calls to exp_x() */
4500Sstevel@tonic-gate uint_t expx_mul = 0; /* number of long multiplies in exp_x() */
4510Sstevel@tonic-gate #endif
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate static uint64_t
exp_x(uint64_t x)4540Sstevel@tonic-gate exp_x(uint64_t x)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate int i;
4570Sstevel@tonic-gate uint64_t ull;
4580Sstevel@tonic-gate uint32_t ui;
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate #ifdef DEBUG
4610Sstevel@tonic-gate expx_cnt++;
4620Sstevel@tonic-gate #endif
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * By the formula:
4650Sstevel@tonic-gate * exp(-x) = exp(-x/2) * exp(-x/2)
4660Sstevel@tonic-gate * we keep halving x until it becomes small enough for
4670Sstevel@tonic-gate * the following approximation to be accurate enough:
4680Sstevel@tonic-gate * exp(-x) = 1 - x
4690Sstevel@tonic-gate * We reduce x until it is less than 1/4 (the 2 in LSHIFT-2 below).
4700Sstevel@tonic-gate * Our final error will be smaller than 4% .
4710Sstevel@tonic-gate */
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * Use a uint64_t for the initial shift calculation.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate ull = x >> (LSHIFT-2);
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate * Short circuit:
4800Sstevel@tonic-gate * A number this large produces effectively 0 (actually .005).
4810Sstevel@tonic-gate * This way, we will never do more than 5 multiplies.
4820Sstevel@tonic-gate */
4830Sstevel@tonic-gate if (ull >= (1 << 5))
4840Sstevel@tonic-gate return (0);
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate ui = ull; /* OK. Now we can use a uint_t. */
4870Sstevel@tonic-gate for (i = 0; ui != 0; i++)
4880Sstevel@tonic-gate ui >>= 1;
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate if (i != 0) {
4910Sstevel@tonic-gate #ifdef DEBUG
4920Sstevel@tonic-gate expx_mul += i; /* seldom happens */
4930Sstevel@tonic-gate #endif
4940Sstevel@tonic-gate x >>= i;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate * Now we compute 1 - x and square it the number of times
4990Sstevel@tonic-gate * that we halved x above to produce the final result:
5000Sstevel@tonic-gate */
5010Sstevel@tonic-gate x = LSI_ONE - x;
5020Sstevel@tonic-gate while (i--)
5030Sstevel@tonic-gate x = (x * x) >> LSHIFT;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate return (x);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * Given the old percent cpu and a time delta in nanoseconds,
5100Sstevel@tonic-gate * return the new decayed percent cpu: pct * exp(-tau),
5110Sstevel@tonic-gate * where 'tau' is the time delta multiplied by a decay factor.
5120Sstevel@tonic-gate * We have chosen the decay factor (cpu_decay_factor in param.c)
5130Sstevel@tonic-gate * to make the decay over five seconds be approximately 20%.
5140Sstevel@tonic-gate *
5150Sstevel@tonic-gate * 'pct' is a 32-bit scaled integer <= 1
5160Sstevel@tonic-gate * The binary point is to the right of the high-order bit
5170Sstevel@tonic-gate * of the 32-bit word.
5180Sstevel@tonic-gate */
5190Sstevel@tonic-gate static uint32_t
cpu_decay(uint32_t pct,hrtime_t nsec)5200Sstevel@tonic-gate cpu_decay(uint32_t pct, hrtime_t nsec)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate uint64_t delta = (uint64_t)nsec;
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate delta /= cpu_decay_factor;
5250Sstevel@tonic-gate return ((pct * exp_x(delta)) >> LSHIFT);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate * Given the old percent cpu and a time delta in nanoseconds,
5300Sstevel@tonic-gate * return the new grown percent cpu: 1 - ( 1 - pct ) * exp(-tau)
5310Sstevel@tonic-gate */
5320Sstevel@tonic-gate static uint32_t
cpu_grow(uint32_t pct,hrtime_t nsec)5330Sstevel@tonic-gate cpu_grow(uint32_t pct, hrtime_t nsec)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate return (LSI_ONE - cpu_decay(LSI_ONE - pct, nsec));
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate * Defined to determine whether a lwp is still on a processor.
5410Sstevel@tonic-gate */
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate #define T_ONPROC(kt) \
5440Sstevel@tonic-gate ((kt)->t_mstate < LMS_SLEEP)
5450Sstevel@tonic-gate #define T_OFFPROC(kt) \
5460Sstevel@tonic-gate ((kt)->t_mstate >= LMS_SLEEP)
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate uint_t
cpu_update_pct(kthread_t * t,hrtime_t newtime)5490Sstevel@tonic-gate cpu_update_pct(kthread_t *t, hrtime_t newtime)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate hrtime_t delta;
5520Sstevel@tonic-gate hrtime_t hrlb;
5530Sstevel@tonic-gate uint_t pctcpu;
5540Sstevel@tonic-gate uint_t npctcpu;
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate * This routine can get called at PIL > 0, this *has* to be
5580Sstevel@tonic-gate * done atomically. Holding locks here causes bad things to happen.
5590Sstevel@tonic-gate * (read: deadlock).
5600Sstevel@tonic-gate */
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate do {
5630Sstevel@tonic-gate if (T_ONPROC(t) && t->t_waitrq == 0) {
5640Sstevel@tonic-gate hrlb = t->t_hrtime;
5650Sstevel@tonic-gate delta = newtime - hrlb;
5660Sstevel@tonic-gate if (delta < 0) {
5670Sstevel@tonic-gate newtime = gethrtime_unscaled();
5680Sstevel@tonic-gate delta = newtime - hrlb;
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate t->t_hrtime = newtime;
5710Sstevel@tonic-gate scalehrtime(&delta);
5720Sstevel@tonic-gate pctcpu = t->t_pctcpu;
5730Sstevel@tonic-gate npctcpu = cpu_grow(pctcpu, delta);
5740Sstevel@tonic-gate } else {
5750Sstevel@tonic-gate hrlb = t->t_hrtime;
5760Sstevel@tonic-gate delta = newtime - hrlb;
5770Sstevel@tonic-gate if (delta < 0) {
5780Sstevel@tonic-gate newtime = gethrtime_unscaled();
5790Sstevel@tonic-gate delta = newtime - hrlb;
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate t->t_hrtime = newtime;
5820Sstevel@tonic-gate scalehrtime(&delta);
5830Sstevel@tonic-gate pctcpu = t->t_pctcpu;
5840Sstevel@tonic-gate npctcpu = cpu_decay(pctcpu, delta);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate } while (cas32(&t->t_pctcpu, pctcpu, npctcpu) != pctcpu);
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate return (npctcpu);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * Change the microstate level for the LWP and update the
5930Sstevel@tonic-gate * associated accounting information. Return the previous
5940Sstevel@tonic-gate * LWP state.
5950Sstevel@tonic-gate */
5960Sstevel@tonic-gate int
new_mstate(kthread_t * t,int new_state)5970Sstevel@tonic-gate new_mstate(kthread_t *t, int new_state)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate struct mstate *ms;
6000Sstevel@tonic-gate unsigned state;
6010Sstevel@tonic-gate hrtime_t *mstimep;
6020Sstevel@tonic-gate hrtime_t curtime;
6030Sstevel@tonic-gate hrtime_t newtime;
6040Sstevel@tonic-gate hrtime_t oldtime;
6050Sstevel@tonic-gate klwp_t *lwp;
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate ASSERT(new_state != LMS_WAIT_CPU);
6080Sstevel@tonic-gate ASSERT((unsigned)new_state < NMSTATES);
6090Sstevel@tonic-gate ASSERT(t == curthread || THREAD_LOCK_HELD(t));
6100Sstevel@tonic-gate
6114071Sjohansen /*
6124071Sjohansen * Don't do microstate processing for threads without a lwp (kernel
6134071Sjohansen * threads). Also, if we're an interrupt thread that is pinning another
6144071Sjohansen * thread, our t_mstate hasn't been initialized. We'd be modifying the
6154071Sjohansen * microstate of the underlying lwp which doesn't realize that it's
6164071Sjohansen * pinned. In this case, also don't change the microstate.
6174071Sjohansen */
6184071Sjohansen if (((lwp = ttolwp(t)) == NULL) || t->t_intr)
6190Sstevel@tonic-gate return (LMS_SYSTEM);
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate curtime = gethrtime_unscaled();
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate /* adjust cpu percentages before we go any further */
6240Sstevel@tonic-gate (void) cpu_update_pct(t, curtime);
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate ms = &lwp->lwp_mstate;
6270Sstevel@tonic-gate state = t->t_mstate;
6280Sstevel@tonic-gate do {
6290Sstevel@tonic-gate switch (state) {
6300Sstevel@tonic-gate case LMS_TFAULT:
6310Sstevel@tonic-gate case LMS_DFAULT:
6320Sstevel@tonic-gate case LMS_KFAULT:
6330Sstevel@tonic-gate case LMS_USER_LOCK:
6340Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SYSTEM];
6350Sstevel@tonic-gate break;
6360Sstevel@tonic-gate default:
6370Sstevel@tonic-gate mstimep = &ms->ms_acct[state];
6380Sstevel@tonic-gate break;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate newtime = curtime - ms->ms_state_start;
6410Sstevel@tonic-gate if (newtime < 0) {
6420Sstevel@tonic-gate curtime = gethrtime_unscaled();
6430Sstevel@tonic-gate oldtime = *mstimep - 1; /* force CAS to fail */
6440Sstevel@tonic-gate continue;
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate oldtime = *mstimep;
6470Sstevel@tonic-gate newtime += oldtime;
6480Sstevel@tonic-gate t->t_mstate = new_state;
6490Sstevel@tonic-gate ms->ms_state_start = curtime;
6500Sstevel@tonic-gate } while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * Remember the previous running microstate.
6530Sstevel@tonic-gate */
6540Sstevel@tonic-gate if (state != LMS_SLEEP && state != LMS_STOPPED)
6550Sstevel@tonic-gate ms->ms_prev = state;
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * Switch CPU microstate if appropriate
6590Sstevel@tonic-gate */
660590Sesolom
6610Sstevel@tonic-gate kpreempt_disable(); /* MUST disable kpreempt before touching t->cpu */
662590Sesolom ASSERT(t->t_cpu == CPU);
663590Sesolom if (!CPU_ON_INTR(t->t_cpu) && curthread->t_intr == NULL) {
664590Sesolom if (new_state == LMS_USER && t->t_cpu->cpu_mstate != CMS_USER)
665590Sesolom new_cpu_mstate(CMS_USER, curtime);
666590Sesolom else if (new_state != LMS_USER &&
667590Sesolom t->t_cpu->cpu_mstate != CMS_SYSTEM)
668590Sesolom new_cpu_mstate(CMS_SYSTEM, curtime);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate kpreempt_enable();
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate return (ms->ms_prev);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate * Restore the LWP microstate to the previous runnable state.
6770Sstevel@tonic-gate * Called from disp() with the newly selected lwp.
6780Sstevel@tonic-gate */
6790Sstevel@tonic-gate void
restore_mstate(kthread_t * t)6800Sstevel@tonic-gate restore_mstate(kthread_t *t)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate struct mstate *ms;
6830Sstevel@tonic-gate hrtime_t *mstimep;
6840Sstevel@tonic-gate klwp_t *lwp;
6850Sstevel@tonic-gate hrtime_t curtime;
6860Sstevel@tonic-gate hrtime_t waitrq;
6870Sstevel@tonic-gate hrtime_t newtime;
6880Sstevel@tonic-gate hrtime_t oldtime;
6890Sstevel@tonic-gate
6904071Sjohansen /*
6914071Sjohansen * Don't call restore mstate of threads without lwps. (Kernel threads)
6924071Sjohansen *
6934071Sjohansen * threads with t_intr set shouldn't be in the dispatcher, so assert
6944071Sjohansen * that nobody here has t_intr.
6954071Sjohansen */
6964071Sjohansen ASSERT(t->t_intr == NULL);
6974071Sjohansen
6980Sstevel@tonic-gate if ((lwp = ttolwp(t)) == NULL)
6990Sstevel@tonic-gate return;
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate curtime = gethrtime_unscaled();
7020Sstevel@tonic-gate (void) cpu_update_pct(t, curtime);
7030Sstevel@tonic-gate ms = &lwp->lwp_mstate;
7040Sstevel@tonic-gate ASSERT((unsigned)t->t_mstate < NMSTATES);
7050Sstevel@tonic-gate do {
7060Sstevel@tonic-gate switch (t->t_mstate) {
7070Sstevel@tonic-gate case LMS_SLEEP:
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate * Update the timer for the current sleep state.
7100Sstevel@tonic-gate */
7110Sstevel@tonic-gate ASSERT((unsigned)ms->ms_prev < NMSTATES);
7120Sstevel@tonic-gate switch (ms->ms_prev) {
7130Sstevel@tonic-gate case LMS_TFAULT:
7140Sstevel@tonic-gate case LMS_DFAULT:
7150Sstevel@tonic-gate case LMS_KFAULT:
7160Sstevel@tonic-gate case LMS_USER_LOCK:
7170Sstevel@tonic-gate mstimep = &ms->ms_acct[ms->ms_prev];
7180Sstevel@tonic-gate break;
7190Sstevel@tonic-gate default:
7200Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SLEEP];
7210Sstevel@tonic-gate break;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate * Return to the previous run state.
7250Sstevel@tonic-gate */
7260Sstevel@tonic-gate t->t_mstate = ms->ms_prev;
7270Sstevel@tonic-gate break;
7280Sstevel@tonic-gate case LMS_STOPPED:
7290Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_STOPPED];
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate * Return to the previous run state.
7320Sstevel@tonic-gate */
7330Sstevel@tonic-gate t->t_mstate = ms->ms_prev;
7340Sstevel@tonic-gate break;
7350Sstevel@tonic-gate case LMS_TFAULT:
7360Sstevel@tonic-gate case LMS_DFAULT:
7370Sstevel@tonic-gate case LMS_KFAULT:
7380Sstevel@tonic-gate case LMS_USER_LOCK:
7390Sstevel@tonic-gate mstimep = &ms->ms_acct[LMS_SYSTEM];
7400Sstevel@tonic-gate break;
7410Sstevel@tonic-gate default:
7420Sstevel@tonic-gate mstimep = &ms->ms_acct[t->t_mstate];
7430Sstevel@tonic-gate break;
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate waitrq = t->t_waitrq; /* hopefully atomic */
7463426Sjohansen if (waitrq == 0) {
7470Sstevel@tonic-gate waitrq = curtime;
7480Sstevel@tonic-gate }
7493426Sjohansen t->t_waitrq = 0;
7500Sstevel@tonic-gate newtime = waitrq - ms->ms_state_start;
7510Sstevel@tonic-gate if (newtime < 0) {
7520Sstevel@tonic-gate curtime = gethrtime_unscaled();
7530Sstevel@tonic-gate oldtime = *mstimep - 1; /* force CAS to fail */
7540Sstevel@tonic-gate continue;
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate oldtime = *mstimep;
7570Sstevel@tonic-gate newtime += oldtime;
7580Sstevel@tonic-gate } while (cas64((uint64_t *)mstimep, oldtime, newtime) != oldtime);
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate * Update the WAIT_CPU timer and per-cpu waitrq total.
7610Sstevel@tonic-gate */
7620Sstevel@tonic-gate ms->ms_acct[LMS_WAIT_CPU] += (curtime - waitrq);
763477Smishra CPU->cpu_waitrq += (curtime - waitrq);
7640Sstevel@tonic-gate ms->ms_state_start = curtime;
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate /*
7680Sstevel@tonic-gate * Copy lwp microstate accounting and resource usage information
7690Sstevel@tonic-gate * to the process. (lwp is terminating)
7700Sstevel@tonic-gate */
7710Sstevel@tonic-gate void
term_mstate(kthread_t * t)7720Sstevel@tonic-gate term_mstate(kthread_t *t)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate struct mstate *ms;
7750Sstevel@tonic-gate proc_t *p = ttoproc(t);
7760Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
7770Sstevel@tonic-gate int i;
7780Sstevel@tonic-gate hrtime_t tmp;
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate ms = &lwp->lwp_mstate;
7830Sstevel@tonic-gate (void) new_mstate(t, LMS_STOPPED);
7840Sstevel@tonic-gate ms->ms_term = ms->ms_state_start;
7850Sstevel@tonic-gate tmp = ms->ms_term - ms->ms_start;
7860Sstevel@tonic-gate scalehrtime(&tmp);
7870Sstevel@tonic-gate p->p_mlreal += tmp;
7880Sstevel@tonic-gate for (i = 0; i < NMSTATES; i++) {
7890Sstevel@tonic-gate tmp = ms->ms_acct[i];
7900Sstevel@tonic-gate scalehrtime(&tmp);
7910Sstevel@tonic-gate p->p_acct[i] += tmp;
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate p->p_ru.minflt += lwp->lwp_ru.minflt;
7940Sstevel@tonic-gate p->p_ru.majflt += lwp->lwp_ru.majflt;
7950Sstevel@tonic-gate p->p_ru.nswap += lwp->lwp_ru.nswap;
7960Sstevel@tonic-gate p->p_ru.inblock += lwp->lwp_ru.inblock;
7970Sstevel@tonic-gate p->p_ru.oublock += lwp->lwp_ru.oublock;
7980Sstevel@tonic-gate p->p_ru.msgsnd += lwp->lwp_ru.msgsnd;
7990Sstevel@tonic-gate p->p_ru.msgrcv += lwp->lwp_ru.msgrcv;
8000Sstevel@tonic-gate p->p_ru.nsignals += lwp->lwp_ru.nsignals;
8010Sstevel@tonic-gate p->p_ru.nvcsw += lwp->lwp_ru.nvcsw;
8020Sstevel@tonic-gate p->p_ru.nivcsw += lwp->lwp_ru.nivcsw;
8030Sstevel@tonic-gate p->p_ru.sysc += lwp->lwp_ru.sysc;
8040Sstevel@tonic-gate p->p_ru.ioch += lwp->lwp_ru.ioch;
8050Sstevel@tonic-gate p->p_defunct++;
8060Sstevel@tonic-gate }
807