From owner-p4-projects@FreeBSD.ORG Sun Mar 23 03:05:03 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id C8EA61065673; Sun, 23 Mar 2008 03:05:03 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6D9671065671 for ; Sun, 23 Mar 2008 03:05:03 +0000 (UTC) (envelope-from jb@freebsd.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 5E6BB8FC17 for ; Sun, 23 Mar 2008 03:05:03 +0000 (UTC) (envelope-from jb@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m2N353V1049379 for ; Sun, 23 Mar 2008 03:05:03 GMT (envelope-from jb@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id m2N353sG049377 for perforce@freebsd.org; Sun, 23 Mar 2008 03:05:03 GMT (envelope-from jb@freebsd.org) Date: Sun, 23 Mar 2008 03:05:03 GMT Message-Id: <200803230305.m2N353sG049377@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to jb@freebsd.org using -f From: John Birrell To: Perforce Change Reviews Cc: Subject: PERFORCE change 138320 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 23 Mar 2008 03:05:04 -0000 http://perforce.freebsd.org/chv.cgi?CH=138320 Change 138320 by jb@jb_freebsd1 on 2008/03/23 03:04:49 Get the skew between per-cpu tsc values. Ignore the possibility of drift like Solaris does. This makes the 'timestamp' DIF variable accurate across CPUs now so that timing function entry -> return makes sense. Affected files ... .. //depot/projects/dtrace/src/sys/cddl/dev/dtrace/amd64/dtrace_subr.c#10 edit .. //depot/projects/dtrace/src/sys/cddl/dev/dtrace/i386/dtrace_subr.c#13 edit Differences ... ==== //depot/projects/dtrace/src/sys/cddl/dev/dtrace/amd64/dtrace_subr.c#10 (text+ko) ==== @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -377,6 +378,72 @@ } #endif +static int64_t tgt_cpu_tsc; +static int64_t hst_cpu_tsc; +static int64_t tsc_skew[MAXCPU]; + +static void +dtrace_gethrtime_init_sync(void *arg) +{ +#ifdef CHECK_SYNC + /* + * Delay this function from returning on one + * of the CPUs to check that the synchronisation + * works. + */ + uintptr_t cpu = (uintptr_t) arg; + + if (cpu == curcpu) { + int i; + for (i = 0; i < 1000000000; i++) + tgt_cpu_tsc = rdtsc(); + tgt_cpu_tsc = 0; + } +#endif +} + +static void +dtrace_gethrtime_init_cpu(void *arg) +{ + uintptr_t cpu = (uintptr_t) arg; + + if (cpu == curcpu) + tgt_cpu_tsc = rdtsc(); + else + hst_cpu_tsc = rdtsc(); +} + +static void +dtrace_gethrtime_init(void *arg) +{ + cpumask_t map; + int i; + struct pcpu *cp; + + /* The current CPU is the reference one. */ + tsc_skew[curcpu] = 0; + + for (i = 0; i <= mp_maxid; i++) { + if (i == curcpu) + continue; + + if ((cp = pcpu_find(i)) == NULL) + continue; + + map = 0; + map |= (1 << curcpu); + map |= (1 << i); + + smp_rendezvous_cpus(map, dtrace_gethrtime_init_sync, + dtrace_gethrtime_init_cpu, + smp_no_rendevous_barrier, (void *)(uintptr_t) i); + + tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; + } +} + +SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL); + /* * DTrace needs a high resolution time function which can * be called from a probe context and guaranteed not to have @@ -387,7 +454,7 @@ uint64_t dtrace_gethrtime() { - return (rdtsc() * (uint64_t) 1000000000 / tsc_freq); + return ((rdtsc() + tsc_skew[curcpu]) * (int64_t) 1000000000 / tsc_freq); } uint64_t ==== //depot/projects/dtrace/src/sys/cddl/dev/dtrace/i386/dtrace_subr.c#13 (text+ko) ==== @@ -376,6 +376,72 @@ } #endif +static int64_t tgt_cpu_tsc; +static int64_t hst_cpu_tsc; +static int64_t tsc_skew[MAXCPU]; + +static void +dtrace_gethrtime_init_sync(void *arg) +{ +#ifdef CHECK_SYNC + /* + * Delay this function from returning on one + * of the CPUs to check that the synchronisation + * works. + */ + uintptr_t cpu = (uintptr_t) arg; + + if (cpu == curcpu) { + int i; + for (i = 0; i < 1000000000; i++) + tgt_cpu_tsc = rdtsc(); + tgt_cpu_tsc = 0; + } +#endif +} + +static void +dtrace_gethrtime_init_cpu(void *arg) +{ + uintptr_t cpu = (uintptr_t) arg; + + if (cpu == curcpu) + tgt_cpu_tsc = rdtsc(); + else + hst_cpu_tsc = rdtsc(); +} + +static void +dtrace_gethrtime_init(void *arg) +{ + cpumask_t map; + int i; + struct pcpu *cp; + + /* The current CPU is the reference one. */ + tsc_skew[curcpu] = 0; + + for (i = 0; i <= mp_maxid; i++) { + if (i == curcpu) + continue; + + if ((cp = pcpu_find(i)) == NULL) + continue; + + map = 0; + map |= (1 << curcpu); + map |= (1 << i); + + smp_rendezvous_cpus(map, dtrace_gethrtime_init_sync, + dtrace_gethrtime_init_cpu, + smp_no_rendevous_barrier, (void *)(uintptr_t) i); + + tsc_skew[i] = tgt_cpu_tsc - hst_cpu_tsc; + } +} + +SYSINIT(dtrace_gethrtime_init, SI_SUB_SMP, SI_ORDER_ANY, dtrace_gethrtime_init, NULL); + /* * DTrace needs a high resolution time function which can * be called from a probe context and guaranteed not to have @@ -386,7 +452,7 @@ uint64_t dtrace_gethrtime() { - return (rdtsc() * (uint64_t) 1000000000 / tsc_freq); + return ((rdtsc() + tsc_skew[curcpu]) * (int64_t) 1000000000 / tsc_freq); } uint64_t