diff --git a/sys/alpha/alpha/trap.c b/sys/alpha/alpha/trap.c index 6c554f75c30e..e1e8f99b1b48 100644 --- a/sys/alpha/alpha/trap.c +++ b/sys/alpha/alpha/trap.c @@ -296,7 +296,7 @@ trap(a0, a1, a2, entry, framep) CTR5(KTR_TRAP, "%s trap: pid %d, (%lx, %lx, %lx)", user ? "user" : "kernel", p->p_pid, a0, a1, a2); if (user) { - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = framep; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -666,7 +666,7 @@ syscall(code, framep) cnt.v_syscall++; td->td_frame = framep; opc = framep->tf_regs[FRAME_PC] - 4; - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; if (td->td_ucred != p->p_ucred) cred_update_thread(td); if (p->p_flag & P_KSES) diff --git a/sys/amd64/amd64/trap.c b/sys/amd64/amd64/trap.c index 54c4af8ae911..2c219836b229 100644 --- a/sys/amd64/amd64/trap.c +++ b/sys/amd64/amd64/trap.c @@ -264,7 +264,7 @@ trap(frame) !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) { /* user trap */ - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = &frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -957,7 +957,7 @@ syscall(frame) KASSERT((td->td_kse != NULL), ("syscall: kse/thread UNLINKED")); KASSERT((td->td_kse->ke_thread == td), ("syscall:kse/thread mismatch")); - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = &frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/i386/i386/trap.c b/sys/i386/i386/trap.c index 54c4af8ae911..2c219836b229 100644 --- a/sys/i386/i386/trap.c +++ b/sys/i386/i386/trap.c @@ -264,7 +264,7 @@ trap(frame) !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) { /* user trap */ - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = &frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -957,7 +957,7 @@ syscall(frame) KASSERT((td->td_kse != NULL), ("syscall: kse/thread UNLINKED")); KASSERT((td->td_kse->ke_thread == td), ("syscall:kse/thread mismatch")); - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = &frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/ia64/ia64/trap.c b/sys/ia64/ia64/trap.c index d0e4aa616458..1ce7d4e5bf24 100644 --- a/sys/ia64/ia64/trap.c +++ b/sys/ia64/ia64/trap.c @@ -331,7 +331,7 @@ trap(int vector, int imm, struct trapframe *framep) user = ((framep->tf_cr_ipsr & IA64_PSR_CPL) == IA64_PSR_CPL_USER); if (user) { - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = framep; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -792,7 +792,7 @@ syscall(int code, u_int64_t *args, struct trapframe *framep) p = td->td_proc; td->td_frame = framep; - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -939,7 +939,7 @@ ia32_syscall(struct trapframe *framep) */ cnt.v_syscall++; - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = framep; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index 8cb6fcdc760b..df7ca5acc892 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -375,7 +376,7 @@ statclock(frame) */ if (p->p_flag & P_KSES) thread_add_ticks_intr(1, 1); - ke->ke_uticks++; + p->p_uticks++; if (ke->ke_ksegrp->kg_nice > NZERO) cp_time[CP_NICE]++; else @@ -394,12 +395,13 @@ statclock(frame) * in ``non-process'' (i.e., interrupt) work. */ if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) { - ke->ke_iticks++; + p->p_iticks++; cp_time[CP_INTR]++; } else { if (p->p_flag & P_KSES) thread_add_ticks_intr(0, 1); - ke->ke_sticks++; + td->td_sticks++; + p->p_sticks++; if (p != PCPU_GET(idlethread)->td_proc) cp_time[CP_SYS]++; else diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 9ad3732eb7f9..d0892e1e7c57 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -671,32 +671,23 @@ calcru(p, up, sp, ip) { /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; - u_int64_t uut = 0, sut = 0, iut = 0; - int s; struct timeval tv; struct bintime bt; - struct kse *ke; - struct ksegrp *kg; mtx_assert(&sched_lock, MA_OWNED); /* XXX: why spl-protect ? worst case is an off-by-one report */ - FOREACH_KSEGRP_IN_PROC(p, kg) { - /* we could accumulate per ksegrp and per process here*/ - FOREACH_KSE_IN_GROUP(kg, ke) { - s = splstatclock(); - ut = ke->ke_uticks; - st = ke->ke_sticks; - it = ke->ke_iticks; - splx(s); + ut = p->p_uticks; + st = p->p_sticks; + it = p->p_iticks; - tt = ut + st + it; - if (tt == 0) { - st = 1; - tt = 1; - } + tt = ut + st + it; + if (tt == 0) { + st = 1; + tt = 1; + } - if (ke == curthread->td_kse) { + if (curthread->td_proc == p) { /* * Adjust for the current time slice. This is actually fairly * important since the error here is on the order of a time @@ -705,64 +696,59 @@ calcru(p, up, sp, ip) * processors also being 'current'. */ - binuptime(&bt); - bintime_sub(&bt, PCPU_PTR(switchtime)); - bintime_add(&bt, &p->p_runtime); - } else { - bt = p->p_runtime; - } - bintime2timeval(&bt, &tv); - tu = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; - ptu = ke->ke_uu + ke->ke_su + ke->ke_iu; - if (tu < ptu || (int64_t)tu < 0) { - /* XXX no %qd in kernel. Truncate. */ - printf("calcru: negative time of %ld usec for pid %d (%s)\n", - (long)tu, p->p_pid, p->p_comm); - tu = ptu; - } + binuptime(&bt); + bintime_sub(&bt, PCPU_PTR(switchtime)); + bintime_add(&bt, &p->p_runtime); + } else { + bt = p->p_runtime; + } + bintime2timeval(&bt, &tv); + tu = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec; + ptu = p->p_uu + p->p_su + p->p_iu; + if (tu < ptu || (int64_t)tu < 0) { + /* XXX no %qd in kernel. Truncate. */ + printf("calcru: negative time of %ld usec for pid %d (%s)\n", + (long)tu, p->p_pid, p->p_comm); + tu = ptu; + } - /* Subdivide tu. */ - uu = (tu * ut) / tt; - su = (tu * st) / tt; - iu = tu - uu - su; + /* Subdivide tu. */ + uu = (tu * ut) / tt; + su = (tu * st) / tt; + iu = tu - uu - su; - /* Enforce monotonicity. */ - if (uu < ke->ke_uu || su < ke->ke_su || iu < ke->ke_iu) { - if (uu < ke->ke_uu) - uu = ke->ke_uu; - else if (uu + ke->ke_su + ke->ke_iu > tu) - uu = tu - ke->ke_su - ke->ke_iu; - if (st == 0) - su = ke->ke_su; - else { - su = ((tu - uu) * st) / (st + it); - if (su < ke->ke_su) - su = ke->ke_su; - else if (uu + su + ke->ke_iu > tu) - su = tu - uu - ke->ke_iu; - } - KASSERT(uu + su + ke->ke_iu <= tu, - ("calcru: monotonisation botch 1")); - iu = tu - uu - su; - KASSERT(iu >= ke->ke_iu, - ("calcru: monotonisation botch 2")); - } - ke->ke_uu = uu; - ke->ke_su = su; - ke->ke_iu = iu; - uut += uu; - sut += su; - iut += iu; - - } /* end kse loop */ - } /* end kseg loop */ - up->tv_sec = uut / 1000000; - up->tv_usec = uut % 1000000; - sp->tv_sec = sut / 1000000; - sp->tv_usec = sut % 1000000; + /* Enforce monotonicity. */ + if (uu < p->p_uu || su < p->p_su || iu < p->p_iu) { + if (uu < p->p_uu) + uu = p->p_uu; + else if (uu + p->p_su + p->p_iu > tu) + uu = tu - p->p_su - p->p_iu; + if (st == 0) + su = p->p_su; + else { + su = ((tu - uu) * st) / (st + it); + if (su < p->p_su) + su = p->p_su; + else if (uu + su + p->p_iu > tu) + su = tu - uu - p->p_iu; + } + KASSERT(uu + su + p->p_iu <= tu, + ("calcru: monotonisation botch 1")); + iu = tu - uu - su; + KASSERT(iu >= p->p_iu, + ("calcru: monotonisation botch 2")); + } + p->p_uu = uu; + p->p_su = su; + p->p_iu = iu; + + up->tv_sec = uu / 1000000; + up->tv_usec = uu % 1000000; + sp->tv_sec = su / 1000000; + sp->tv_usec = su % 1000000; if (ip != NULL) { - ip->tv_sec = iut / 1000000; - ip->tv_usec = iut % 1000000; + ip->tv_sec = iu / 1000000; + ip->tv_usec = iu % 1000000; } } diff --git a/sys/kern/subr_trap.c b/sys/kern/subr_trap.c index eec2ae6fda68..47e847224458 100644 --- a/sys/kern/subr_trap.c +++ b/sys/kern/subr_trap.c @@ -124,7 +124,7 @@ userret(td, frame, oticks) quad_t ticks; mtx_lock_spin(&sched_lock); - ticks = ke->ke_sticks - oticks; + ticks = td->td_sticks - oticks; mtx_unlock_spin(&sched_lock); addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio); } @@ -175,7 +175,7 @@ ast(struct trapframe *framep) */ mtx_lock_spin(&sched_lock); ke = td->td_kse; - sticks = ke->ke_sticks; + sticks = td->td_sticks; flags = ke->ke_flags; sflag = p->p_sflag; p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU); diff --git a/sys/powerpc/aim/trap.c b/sys/powerpc/aim/trap.c index 5469c2e3678a..4874093f3fb0 100644 --- a/sys/powerpc/aim/trap.c +++ b/sys/powerpc/aim/trap.c @@ -169,7 +169,7 @@ trap(struct trapframe *frame) trapname(type), user ? "user" : "kernel"); if (user) { - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/powerpc/powerpc/trap.c b/sys/powerpc/powerpc/trap.c index 5469c2e3678a..4874093f3fb0 100644 --- a/sys/powerpc/powerpc/trap.c +++ b/sys/powerpc/powerpc/trap.c @@ -169,7 +169,7 @@ trap(struct trapframe *frame) trapname(type), user ? "user" : "kernel"); if (user) { - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = frame; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/sparc64/sparc64/trap.c b/sys/sparc64/sparc64/trap.c index 7601695c68e0..c085e52161ae 100644 --- a/sys/sparc64/sparc64/trap.c +++ b/sys/sparc64/sparc64/trap.c @@ -243,7 +243,7 @@ trap(struct trapframe *tf) KASSERT(td->td_proc != NULL, ("trap: curproc NULL")); p = td->td_proc; - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = tf; if (td->td_ucred != p->p_ucred) cred_update_thread(td); @@ -495,7 +495,7 @@ syscall(struct trapframe *tf) reg = 0; regcnt = REG_MAXARGS; - sticks = td->td_kse->ke_sticks; + sticks = td->td_sticks; td->td_frame = tf; if (td->td_ucred != p->p_ucred) cred_update_thread(td); diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 4d4037915b2a..c4f0e13f8671 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -298,6 +298,7 @@ struct thread { struct ucred *td_ucred; /* (k) Reference to credentials. */ void (*td_switchin)(void); /* (k) Switchin special func. */ struct thread *td_standin; /* (?) Use this for an upcall */ + u_int64_t td_sticks; /* (j) Statclock hits in system mode. */ u_int td_usticks; /* (?) Statclock kernel hits, for UTS */ u_int td_critnest; /* (k) Critical section nest level. */ #define td_endzero td_base_pri @@ -442,12 +443,6 @@ struct kse { struct thread *ke_thread; /* Active associated thread. */ struct thread *ke_owner; /* Always points to the owner */ fixpt_t ke_pctcpu; /* (j) %cpu during p_swtime. */ - u_int64_t ke_uu; /* (j) Previous user time in usec. */ - u_int64_t ke_su; /* (j) Previous system time in usec. */ - u_int64_t ke_iu; /* (j) Previous intr time in usec. */ - u_int64_t ke_uticks; /* (j) Statclock hits in user mode. */ - u_int64_t ke_sticks; /* (j) Statclock hits in system mode. */ - u_int64_t ke_iticks; /* (j) Statclock hits in intr. */ u_int ke_uuticks; /* Statclock hits in user, for UTS */ u_int ke_usticks; /* Statclock hits in kernel, for UTS */ u_char ke_oncpu; /* (j) Which cpu we are on. */ @@ -569,6 +564,12 @@ struct proc { u_int p_swtime; /* (j) Time swapped in or out. */ struct itimerval p_realtimer; /* (h?/k?) Alarm timer. */ struct bintime p_runtime; /* (j) Real time. */ + u_int64_t p_uu; /* (j) Previous user time in usec. */ + u_int64_t p_su; /* (j) Previous system time in usec. */ + u_int64_t p_iu; /* (j) Previous intr time in usec. */ + u_int64_t p_uticks; /* (j) Statclock hits in user mode. */ + u_int64_t p_sticks; /* (j) Statclock hits in system mode. */ + u_int64_t p_iticks; /* (j) Statclock hits in intr. */ int p_profthreads; /* (c) Num threads in addupc_task */ int p_traceflag; /* (o) Kernel trace points. */ struct vnode *p_tracep; /* (c + o) Trace to vnode. */