From owner-freebsd-current@FreeBSD.ORG Thu Jun 24 10:12:16 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E370D16A4CE for ; Thu, 24 Jun 2004 10:12:16 +0000 (GMT) Received: from mailout2.pacific.net.au (mailout2.pacific.net.au [61.8.0.85]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6A6BF43D5A for ; Thu, 24 Jun 2004 10:12:16 +0000 (GMT) (envelope-from bde@zeta.org.au) Received: from mailproxy1.pacific.net.au (mailproxy1.pacific.net.au [61.8.0.86])i5OACA5v008101 for ; Thu, 24 Jun 2004 20:12:10 +1000 Received: from gamplex.bde.org (katana.zip.com.au [61.8.7.246]) i5OAC8ao024243 for ; Thu, 24 Jun 2004 20:12:09 +1000 Date: Thu, 24 Jun 2004 20:12:07 +1000 (EST) From: Bruce Evans X-X-Sender: bde@gamplex.bde.org To: current@freebsd.org Message-ID: <20040624201018.L41701@gamplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: fix for calcru() on running non-current threads X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Jun 2004 10:12:17 -0000 Accessing switchtime for other CPUs turned out to be easy. Please test and review. (Tests should show no difference for the non-SMP case.) %%% Index: kern_resource.c =================================================================== RCS file: /home/ncvs/src/sys/kern/kern_resource.c,v retrieving revision 1.140 diff -u -2 -r1.140 kern_resource.c --- kern_resource.c 21 Jun 2004 17:46:27 -0000 1.140 +++ kern_resource.c 24 Jun 2004 07:31:05 -0000 @@ -48,4 +48,5 @@ #include #include +#include #include #include @@ -707,5 +708,5 @@ /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; - int problemcase; + int bt_valid; mtx_assert(&sched_lock, MA_OWNED); @@ -722,27 +723,20 @@ } rt = p->p_runtime; - problemcase = 0; + bt_valid = 0; FOREACH_THREAD_IN_PROC(p, td) { - /* - * Adjust for the current time slice. This is actually fairly - * important since the error here is on the order of a time - * quantum, which is much greater than the sampling error. - */ - if (td == curthread) { - binuptime(&bt); - bintime_sub(&bt, PCPU_PTR(switchtime)); - bintime_add(&rt, &bt); - } else if (TD_IS_RUNNING(td)) { + if (TD_IS_RUNNING(td)) { /* - * XXX: this case should add the difference between - * the current time and the switch time as above, - * but the switch time is inaccessible, so we can't - * do the adjustment and will end up with a wrong - * runtime. A previous call with a different - * curthread may have obtained a (right or wrong) - * runtime that is in advance of ours. Just set a - * flag to avoid warning about this known problem. + * Adjust for the current time slice. This is + * important since the adjustment is on the order + * of a time quantum, which is much greater than + * precision of binuptime(). */ - problemcase = 1; + if (!bt_valid) { + binuptime(&bt); + bt_valid = 1; + } + bintime_add(&rt, &bt); + bintime_sub(&rt, + &pcpu_find(td->td_oncpu)->pc_switchtime); } } @@ -751,8 +745,7 @@ ptu = p->p_uu + p->p_su + p->p_iu; if (tu < ptu) { - if (!problemcase) - printf( + printf( "calcru: runtime went backwards from %ju usec to %ju usec for pid %d (%s)\n", - (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm); + (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm); tu = ptu; } %%% Bruce