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 <blees@dcon.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jul 12, 2014 at 02:06 UTC 09b2c1c769a69d3ff03ee7913fa50fa05b4f5a46
2 files changed +65
trace.c
+47
@@ -169,6 +169,27 @@ void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
169 print_trace_line(key, &buf);
170 }
171
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 +
193 #ifndef HAVE_VARIADIC_MACROS
194
195 void trace_printf(const char *format, ...)
@@ -200,6 +221,23 @@ void trace_strbuf(const char *key, const struct strbuf *data)
221 trace_strbuf_fl(NULL, 0, key, data);
222 }
223
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 +
241 #else
242
243 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,
258 va_end(ap);
259 }
260
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 +
270 #endif /* HAVE_VARIADIC_MACROS */
271
272
trace.h
+18
@@ -31,6 +31,14 @@ extern void trace_argv_printf(const char **argv, const char *format, ...);
31
32 extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
33
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 +
42 #else
43
44 /*
@@ -79,6 +87,13 @@ extern void trace_strbuf(struct trace_key *key, const struct strbuf *data);
87 #define trace_strbuf(key, data) \
88 trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
89
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 +
97 /* backend functions, use non-*fl macros instead */
98 __attribute__((format (printf, 4, 5)))
99 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,
103 const char *format, ...);
104 extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
105 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, ...);
109
110 #endif /* HAVE_VARIADIC_MACROS */
111