Skip to content

Commit 09b2c1c

Browse files
kbleesgitster
authored andcommitted
trace: add trace_performance facility to debug performance issues
Add trace_performance and trace_performance_since macros that print a duration and an optional printf-formatted text to the file specified in environment variable GIT_TRACE_PERFORMANCE. These macros, in conjunction with getnanotime(), are intended to simplify performance measurements from within the application (i.e. profiling via manual instrumentation, rather than using an external profiling tool). Unless enabled via GIT_TRACE_PERFORMANCE, these macros have no noticeable impact on performance, so that test code for well known time killers may be shipped in release builds. Alternatively, a developer could provide an additional performance patch (not meant for master) that allows reviewers to reproduce performance tests more easily, e.g. on other platforms or using their own repositories. Usage examples: Simple use case (measure one code section): uint64_t start = getnanotime(); /* code section to measure */ trace_performance_since(start, "foobar"); Complex use case (measure repetitive code sections): uint64_t t = 0; for (;;) { /* ignore */ t -= getnanotime(); /* code section to measure */ t += getnanotime(); /* ignore */ } trace_performance(t, "frotz"); Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 148d677 commit 09b2c1c

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

trace.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
169169
print_trace_line(key, &buf);
170170
}
171171

172+
static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
173+
174+
static void trace_performance_vprintf_fl(const char *file, int line,
175+
uint64_t nanos, const char *format,
176+
va_list ap)
177+
{
178+
struct strbuf buf = STRBUF_INIT;
179+
180+
if (!prepare_trace_line(file, line, &trace_perf_key, &buf))
181+
return;
182+
183+
strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000);
184+
185+
if (format && *format) {
186+
strbuf_addstr(&buf, ": ");
187+
strbuf_vaddf(&buf, format, ap);
188+
}
189+
190+
print_trace_line(&trace_perf_key, &buf);
191+
}
192+
172193
#ifndef HAVE_VARIADIC_MACROS
173194

174195
void trace_printf(const char *format, ...)
@@ -200,6 +221,23 @@ void trace_strbuf(const char *key, const struct strbuf *data)
200221
trace_strbuf_fl(NULL, 0, key, data);
201222
}
202223

224+
void trace_performance(uint64_t nanos, const char *format, ...)
225+
{
226+
va_list ap;
227+
va_start(ap, format);
228+
trace_performance_vprintf_fl(NULL, 0, nanos, format, ap);
229+
va_end(ap);
230+
}
231+
232+
void trace_performance_since(uint64_t start, const char *format, ...)
233+
{
234+
va_list ap;
235+
va_start(ap, format);
236+
trace_performance_vprintf_fl(NULL, 0, getnanotime() - start,
237+
format, ap);
238+
va_end(ap);
239+
}
240+
203241
#else
204242

205243
void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
@@ -220,6 +258,15 @@ void trace_argv_printf_fl(const char *file, int line, const char **argv,
220258
va_end(ap);
221259
}
222260

261+
void trace_performance_fl(const char *file, int line, uint64_t nanos,
262+
const char *format, ...)
263+
{
264+
va_list ap;
265+
va_start(ap, format);
266+
trace_performance_vprintf_fl(file, line, nanos, format, ap);
267+
va_end(ap);
268+
}
269+
223270
#endif /* HAVE_VARIADIC_MACROS */
224271

225272

trace.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ extern void trace_argv_printf(const char **argv, const char *format, ...);
3131

3232
extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
3333

34+
/* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled. */
35+
__attribute__((format (printf, 2, 3)))
36+
extern void trace_performance(uint64_t nanos, const char *format, ...);
37+
38+
/* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled. */
39+
__attribute__((format (printf, 2, 3)))
40+
extern void trace_performance_since(uint64_t start, const char *format, ...);
41+
3442
#else
3543

3644
/*
@@ -79,6 +87,13 @@ extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
7987
#define trace_strbuf(key, data) \
8088
trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
8189

90+
#define trace_performance(nanos, ...) \
91+
trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos, __VA_ARGS__)
92+
93+
#define trace_performance_since(start, ...) \
94+
trace_performance_fl(TRACE_CONTEXT, __LINE__, getnanotime() - (start), \
95+
__VA_ARGS__)
96+
8297
/* backend functions, use non-*fl macros instead */
8398
__attribute__((format (printf, 4, 5)))
8499
extern void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
@@ -88,6 +103,9 @@ extern void trace_argv_printf_fl(const char *file, int line, const char **argv,
88103
const char *format, ...);
89104
extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
90105
const struct strbuf *data);
106+
__attribute__((format (printf, 4, 5)))
107+
extern void trace_performance_fl(const char *file, int line,
108+
uint64_t nanos, const char *fmt, ...);
91109

92110
#endif /* HAVE_VARIADIC_MACROS */
93111

0 commit comments

Comments
 (0)