trace: add high resolution timer function to debug performance issues

Add a getnanotime() function that returns nanoseconds since 01/01/1970 as unsigned 64-bit integer (i.e. overflows in july 2554). This is easier to work with than e.g. struct timeval or struct timespec. Basing the timer on the epoch allows using the results with other time-related APIs. To simplify adaption to different platforms, split the implementation into a common getnanotime() and a platform-specific highres_nanos() function. The common getnanotime() function handles errors, falling back to gettimeofday() if highres_nanos() isn't implemented or doesn't work. getnanotime() is also responsible for normalizing to the epoch. The offset to the system clock is calculated only once on initialization, i.e. manually setting the system clock has no impact on the timer (except if the fallback gettimeofday() is in use). Git processes are typically short lived, so we don't need to handle clock drift. The highres_nanos() function returns monotonically increasing nanoseconds relative to some arbitrary point in time (e.g. system boot), or 0 on failure. Providing platform-specific implementations should be relatively easy, e.g. adapting to clock_gettime() as defined by the POSIX realtime extensions is seven lines of code. This version includes highres_nanos() implementations for: * Linux: using clock_gettime(CLOCK_MONOTONIC) * Windows: using QueryPerformanceCounter() Todo: * enable clock_gettime() on more platforms * add Mac OSX version, e.g. using mach_absolute_time + mach_timebase_info Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jul 12, 2014 at 02:05 UTC 148d6771bf5e00aa1d7fa2221507a3dfe4c1e37f
4 files changed +91
Makefile
+7
@@ -340,6 +340,8 @@ all::
340 #
341 # Define GMTIME_UNRELIABLE_ERRORS if your gmtime() function does not
342 # return NULL when it receives a bogus time_t.
343 +#
344 +# Define HAVE_CLOCK_GETTIME if your platform has clock_gettime in librt.
345
346 GIT-VERSION-FILE: FORCE
347 @$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1497,6 +1499,11 @@ ifdef GMTIME_UNRELIABLE_ERRORS
1499 BASIC_CFLAGS += -DGMTIME_UNRELIABLE_ERRORS
1500 endif
1501
1502 +ifdef HAVE_CLOCK_GETTIME
1503 + BASIC_CFLAGS += -DHAVE_CLOCK_GETTIME
1504 + EXTLIBS += -lrt
1505 +endif
1506 +
1507 ifeq ($(TCLTK_PATH),)
1508 NO_TCLTK = NoThanks
1509 endif
config.mak.uname
+1
@@ -34,6 +34,7 @@ ifeq ($(uname_S),Linux)
34 HAVE_PATHS_H = YesPlease
35 LIBC_CONTAINS_LIBINTL = YesPlease
36 HAVE_DEV_TTY = YesPlease
37 + HAVE_CLOCK_GETTIME = YesPlease
38 endif
39 ifeq ($(uname_S),GNU/kFreeBSD)
40 HAVE_ALLOCA_H = YesPlease
trace.c
+82
@@ -275,3 +275,85 @@ int trace_want(struct trace_key *key)
275 {
276 return !!get_trace_fd(key);
277 }
278 +
279 +#ifdef HAVE_CLOCK_GETTIME
280 +
281 +static inline uint64_t highres_nanos(void)
282 +{
283 + struct timespec ts;
284 + if (clock_gettime(CLOCK_MONOTONIC, &ts))
285 + return 0;
286 + return (uint64_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
287 +}
288 +
289 +#elif defined (GIT_WINDOWS_NATIVE)
290 +
291 +static inline uint64_t highres_nanos(void)
292 +{
293 + static uint64_t high_ns, scaled_low_ns;
294 + static int scale;
295 + LARGE_INTEGER cnt;
296 +
297 + if (!scale) {
298 + if (!QueryPerformanceFrequency(&cnt))
299 + return 0;
300 +
301 + /* high_ns = number of ns per cnt.HighPart */
302 + high_ns = (1000000000LL << 32) / (uint64_t) cnt.QuadPart;
303 +
304 + /*
305 + * Number of ns per cnt.LowPart is 10^9 / frequency (or
306 + * high_ns >> 32). For maximum precision, we scale this factor
307 + * so that it just fits within 32 bit (i.e. won't overflow if
308 + * multiplied with cnt.LowPart).
309 + */
310 + scaled_low_ns = high_ns;
311 + scale = 32;
312 + while (scaled_low_ns >= 0x100000000LL) {
313 + scaled_low_ns >>= 1;
314 + scale--;
315 + }
316 + }
317 +
318 + /* if QPF worked on initialization, we expect QPC to work as well */
319 + QueryPerformanceCounter(&cnt);
320 +
321 + return (high_ns * cnt.HighPart) +
322 + ((scaled_low_ns * cnt.LowPart) >> scale);
323 +}
324 +
325 +#else
326 +# define highres_nanos() 0
327 +#endif
328 +
329 +static inline uint64_t gettimeofday_nanos(void)
330 +{
331 + struct timeval tv;
332 + gettimeofday(&tv, NULL);
333 + return (uint64_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
334 +}
335 +
336 +/*
337 + * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
338 + * (i.e. favoring high precision over wall clock time accuracy).
339 + */
340 +inline uint64_t getnanotime(void)
341 +{
342 + static uint64_t offset;
343 + if (offset > 1) {
344 + /* initialization succeeded, return offset + high res time */
345 + return offset + highres_nanos();
346 + } else if (offset == 1) {
347 + /* initialization failed, fall back to gettimeofday */
348 + return gettimeofday_nanos();
349 + } else {
350 + /* initialize offset if high resolution timer works */
351 + uint64_t now = gettimeofday_nanos();
352 + uint64_t highres = highres_nanos();
353 + if (highres)
354 + offset = now - highres;
355 + else
356 + offset = 1;
357 + return now;
358 + }
359 +}
trace.h
+1
@@ -16,6 +16,7 @@ struct trace_key {
16 extern void trace_repo_setup(const char *prefix);
17 extern int trace_want(struct trace_key *key);
18 extern void trace_disable(struct trace_key *key);
19 +extern uint64_t getnanotime(void);
20
21 #ifndef HAVE_VARIADIC_MACROS
22