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