* This function is analogical to the getpcpu() function in the ps(1) command. * They should both calculate in the same way so that the racct %cpu * calculations are consistent with the values showed by the ps(1) tool. * The calculations are more complex in the 4BSD scheduler because of the value * of the ccpu variable. In ULE it is defined to be zero which saves us some * work. */
| 324 | * work. |
| 325 | */ |
| 326 | static uint64_t |
| 327 | racct_getpcpu(struct proc *p, u_int pcpu) |
| 328 | { |
| 329 | u_int swtime; |
| 330 | #ifdef SCHED_4BSD |
| 331 | fixpt_t pctcpu, pctcpu_next; |
| 332 | #endif |
| 333 | #ifdef SMP |
| 334 | struct pcpu *pc; |
| 335 | int found; |
| 336 | #endif |
| 337 | fixpt_t p_pctcpu; |
| 338 | struct thread *td; |
| 339 | |
| 340 | ASSERT_RACCT_ENABLED(); |
| 341 | |
| 342 | /* |
| 343 | * If the process is swapped out, we count its %cpu usage as zero. |
| 344 | * This behaviour is consistent with the userland ps(1) tool. |
| 345 | */ |
| 346 | if ((p->p_flag & P_INMEM) == 0) |
| 347 | return (0); |
| 348 | swtime = (ticks - p->p_swtick) / hz; |
| 349 | |
| 350 | /* |
| 351 | * For short-lived processes, the sched_pctcpu() returns small |
| 352 | * values even for cpu intensive processes. Therefore we use |
| 353 | * our own estimate in this case. |
| 354 | */ |
| 355 | if (swtime < RACCT_PCPU_SECS) |
| 356 | return (pcpu); |
| 357 | |
| 358 | p_pctcpu = 0; |
| 359 | FOREACH_THREAD_IN_PROC(p, td) { |
| 360 | if (td == PCPU_GET(idlethread)) |
| 361 | continue; |
| 362 | #ifdef SMP |
| 363 | found = 0; |
| 364 | STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) { |
| 365 | if (td == pc->pc_idlethread) { |
| 366 | found = 1; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | if (found) |
| 371 | continue; |
| 372 | #endif |
| 373 | thread_lock(td); |
| 374 | #ifdef SCHED_4BSD |
| 375 | pctcpu = sched_pctcpu(td); |
| 376 | /* Count also the yet unfinished second. */ |
| 377 | pctcpu_next = (pctcpu * ccpu_exp[1]) >> FSHIFT; |
| 378 | pctcpu_next += sched_pctcpu_delta(td); |
| 379 | p_pctcpu += max(pctcpu, pctcpu_next); |
| 380 | #else |
| 381 | /* |
| 382 | * In ULE the %cpu statistics are updated on every |
| 383 | * sched_pctcpu() call. So special calculations to |
no test coverage detected