Skip to content

Commit 148d677

Browse files
kbleesgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e05bed9 commit 148d677

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ all::
340340
#
341341
# Define GMTIME_UNRELIABLE_ERRORS if your gmtime() function does not
342342
# return NULL when it receives a bogus time_t.
343+
#
344+
# Define HAVE_CLOCK_GETTIME if your platform has clock_gettime in librt.
343345

344346
GIT-VERSION-FILE: FORCE
345347
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1497,6 +1499,11 @@ ifdef GMTIME_UNRELIABLE_ERRORS
14971499
BASIC_CFLAGS += -DGMTIME_UNRELIABLE_ERRORS
14981500
endif
14991501

1502+
ifdef HAVE_CLOCK_GETTIME
1503+
BASIC_CFLAGS += -DHAVE_CLOCK_GETTIME
1504+
EXTLIBS += -lrt
1505+
endif
1506+
15001507
ifeq ($(TCLTK_PATH),)
15011508
NO_TCLTK = NoThanks
15021509
endif

config.mak.uname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ifeq ($(uname_S),Linux)
3434
HAVE_PATHS_H = YesPlease
3535
LIBC_CONTAINS_LIBINTL = YesPlease
3636
HAVE_DEV_TTY = YesPlease
37+
HAVE_CLOCK_GETTIME = YesPlease
3738
endif
3839
ifeq ($(uname_S),GNU/kFreeBSD)
3940
HAVE_ALLOCA_H = YesPlease

trace.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,85 @@ int trace_want(struct trace_key *key)
275275
{
276276
return !!get_trace_fd(key);
277277
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct trace_key {
1616
extern void trace_repo_setup(const char *prefix);
1717
extern int trace_want(struct trace_key *key);
1818
extern void trace_disable(struct trace_key *key);
19+
extern uint64_t getnanotime(void);
1920

2021
#ifndef HAVE_VARIADIC_MACROS
2122

0 commit comments

Comments
 (0)