check for CLOCK_MONOTONIC

Björn JACKE samba at j3e.de
Tue Jun 6 20:53:47 GMT 2006


Hi,

attached is a patch which checks the availabiliy of the different 
clocks on that System and if they really work on that hardware. Keep 
in mind that clock_gettime(3) mentions that even if a clock is known 
by the libc, it might be unavailable, which will result in 
clock_gettime() returning EINVAL.

James, that's you code, can you please have a look?

Cheers
Bjoern
-------------- next part --------------
Index: source/profile/profile.c
===================================================================
--- source/profile/profile.c	(Revision 16044)
+++ source/profile/profile.c	(Arbeitskopie)
@@ -100,28 +100,48 @@
   open the profiling shared memory area
   ******************************************************************/
 #ifdef WITH_PROFILE
+#ifdef HAVE_CLOCK_GETTIME
+static void init_clock_gettime(void)
+{
+	struct timespec ts;
+
+#ifdef CLOCK_MONOTONIC
+	if (this_is_smp() && (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)) {
+		DEBUG(10, ("Using CLOCK_MONOTONIC for profile_clock\n"));
+		__profile_clock = CLOCK_MONOTONIC;
+		return;
+	}
+#endif
+
+#ifdef CLOCK_PROCESS_CPUTIME_ID
+	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
+		DEBUG(10, ("Using CLOCK_PROCESS_CPUTIME_ID for profile_clock\n"));
+		__profile_clock = CLOCK_PROCESS_CPUTIME_ID;
+		return;
+	}
+#endif
+
+	/* CLOCK_REALTIME should be defined everywhere where we have
+	 * clock_gettime... */
+	if (clock_gettime(CLOCK_REALTIME, &ts) == 0) {
+		DEBUG(0, ("WARNING: Using (slow) CLOCK_REALTIME for profile_clock\n"));
+		__profile_clock = CLOCK_REALTIME;
+		return;
+	}
+
+	DEBUG(0, ("WARNING: Could not find a working version of clock_gettime for profiling\n"));
+	return;
+}
+#endif
+
 BOOL profile_setup(BOOL rdonly)
 {
 	struct shmid_ds shm_ds;
 
 	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;
-	}
+#ifdef HAVE_CLOCK_GETTIME
+	init_clock_gettime();
 #endif
 
  again:


More information about the samba-technical mailing list