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