r15508: Use clock_gettime for profiling timstamps if it is available. Use
authorJames Peach <jpeach@samba.org>
Mon, 8 May 2006 03:20:49 +0000 (03:20 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:16:55 +0000 (11:16 -0500)
the fastest clock available on uniprocessors.
(This used to be commit d44862928206b524f826bd7c2997ab5353c0b6a0)

source3/configure.in
source3/include/smbprofile.h
source3/lib/util.c
source3/profile/profile.c
source3/tdb/spinlock.c

index 40cf2d4a814721a018219dc22fe64021ed20ef67..ca06afc08a166e3c92f052ae90789c9810c2c4b2 100644 (file)
@@ -2019,6 +2019,8 @@ if test x"$samba_cv_HAVE_GETTIMEOFDAY_TZ" = x"yes"; then
     AC_DEFINE(HAVE_GETTIMEOFDAY_TZ,1,[Whether gettimeofday() is available])
 fi
 
+AC_LIBTESTFUNC(rt, clock_gettime)
+
 AC_CACHE_CHECK([for va_copy],samba_cv_HAVE_VA_COPY,[
 AC_TRY_LINK([#include <stdarg.h>
 va_list ap1,ap2;], [va_copy(ap1,ap2);],
index dd171ac13ebfea309db32ec92af0a979e11dad2a..fb5fa078c1c47b385e00e0d52536fa806fc0cf02 100644 (file)
@@ -419,6 +419,25 @@ extern BOOL do_profile_times;
 #define DEC_PROFILE_COUNT(x) profile_p->x--
 #define ADD_PROFILE_COUNT(x,y) profile_p->x += (y)
 
+#if defined(HAVE_CLOCK_GETTIME)
+
+extern clockid_t __profile_clock;
+
+static inline unsigned long long profile_timestamp(void)
+{
+       struct timespec ts;
+
+       /* FIXME: On a single-CPU system, or a system where we have bound
+        * daemon threads to single CPUs (eg. using cpusets or processor
+        * affinity), it might be preferable to use CLOCK_PROCESS_CPUTIME_ID.
+        */
+
+       clock_gettime(__profile_clock, &ts);
+       return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000); /* usec */
+}
+
+#else
+
 static inline unsigned long long profile_timestamp(void)
 {
        struct timeval tv;
@@ -426,6 +445,8 @@ static inline unsigned long long profile_timestamp(void)
        return (tv.tv_sec * 1000000) + tv.tv_usec;
 }
 
+#endif
+
 /* end of helper macros */
 
 #define DO_PROFILE_INC(x) \
index a9aebd0822a84cd6eb54c4e8824983eabe202b85..fd48cbfa8382ac0c5340b5ff12b263adc151b8d5 100644 (file)
@@ -2969,3 +2969,21 @@ BOOL procid_is_local(const struct process_id *pid)
 {
        return True;
 }
+
+int this_is_smp(void)
+{
+#if defined(HAVE_SYSCONF)
+
+#if defined(SYSCONF_SC_NPROC_ONLN)
+        return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;
+#elif defined(SYSCONF_SC_NPROCESSORS_ONLN)
+        return (sysconf(_SC_NPROCESSORS_ONLN) > 1) ? 1 : 0;
+#else
+       return 0;
+#endif
+
+#else
+       return 0;
+#endif
+}
+
index db8a643042b92488b0b3872776b6c6da1e72c0fd..bacf00eb01706538f45a3449bac1ddf6a758bd97 100644 (file)
@@ -28,6 +28,9 @@
 #ifdef WITH_PROFILE
 static int shm_id;
 static BOOL read_only;
+#if defined(HAVE_CLOCK_GETTIME)
+clockid_t __profile_clock;
+#endif
 #endif
 
 struct profile_header *profile_h;
@@ -103,6 +106,24 @@ BOOL profile_setup(BOOL rdonly)
 
        read_only = rdonly;
 
+#if defined(HAVE_CLOCK_GETTIME)
+       if (this_is_smp()) {
+               /* This is faster that gettimeofday, but not fast enough to
+                * leave it enabled in production.
+                */
+               __profile_clock = CLOCK_MONOTONIC;
+       } else {
+               /* CLOCK_PROCESS_CPUTIME_ID is sufficiently fast that the
+                * always profiling times is plausible. Unfortunately it is
+                * only accurate if we can guarantee we will not be scheduled
+                * onto a different CPU between samples. Until there is some
+                * way to set processor affinity, we can only use this on
+                * uniprocessors.
+                */
+               __profile_clock = CLOCK_PROCESS_CPUTIME_ID;
+       }
+#endif
+
  again:
        /* try to use an existing key */
        shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
index e6abb3a0bc8c43fe071df15e5317709fd0840bb9..e42a6901c71a166e40be6f3f86444ba9b7dece82 100644 (file)
@@ -266,23 +266,6 @@ static void yield_cpu(void)
 #endif
 }
 
-static int this_is_smp(void)
-{
-#if defined(HAVE_SYSCONF)
-
-#if defined(SYSCONF_SC_NPROC_ONLN)
-        return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;
-#elif defined(SYSCONF_SC_NPROCESSORS_ONLN)
-        return (sysconf(_SC_NPROCESSORS_ONLN) > 1) ? 1 : 0;
-#else
-       return 0;
-#endif
-
-#else
-       return 0;
-#endif
-}
-
 /*
  * GENERIC
  */