176
strbuf_release(&buf);
177
}
178
179
+static uint64_t perf_start_times[10];
180
+static int perf_indent;
181
+
182
+uint64_t trace_performance_enter(void)
183
+{
184
+ uint64_t now;
185
+
186
+ if (!trace_want(&trace_perf_key))
187
+ return 0;
188
+
189
+ now = getnanotime();
190
+ perf_start_times[perf_indent] = now;
191
+ if (perf_indent + 1 < ARRAY_SIZE(perf_start_times))
192
+ perf_indent++;
193
+ else
194
+ BUG("Too deep indentation");
195
+ return now;
196
+}
197
+
198
static void trace_performance_vprintf_fl(const char *file, int line,
199
uint64_t nanos, const char *format,
200
va_list ap)
201
{
202
+ static const char space[] = " ";
203
struct strbuf buf = STRBUF_INIT;
204
205
if (!prepare_trace_line(file, line, &trace_perf_key, &buf))
208
strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000);
209
210
if (format && *format) {
191
- strbuf_addstr(&buf, ": ");
211
+ if (perf_indent >= strlen(space))
212
+ BUG("Too deep indentation");
213
+
214
+ strbuf_addf(&buf, ":%.*s ", perf_indent, space);
215
strbuf_vaddf(&buf, format, ap);
216
}
217
267
va_end(ap);
268
}
269
270
+void trace_performance_leave(const char *format, ...)
271
+{
272
+ va_list ap;
273
+ uint64_t since;
274
+
275
+ if (perf_indent)
276
+ perf_indent--;
277
+
278
+ if (!format) /* Allow callers to leave without tracing anything */
279
+ return;
280
+
281
+ since = perf_start_times[perf_indent];
282
+ va_start(ap, format);
283
+ trace_performance_vprintf_fl(NULL, 0, getnanotime() - since,
284
+ format, ap);
285
+ va_end(ap);
286
+}
287
+
288
#else
289
290
void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
314
va_end(ap);
315
}
316
317
+void trace_performance_leave_fl(const char *file, int line,
318
+ uint64_t nanos, const char *format, ...)
319
+{
320
+ va_list ap;
321
+ uint64_t since;
322
+
323
+ if (perf_indent)
324
+ perf_indent--;
325
+
326
+ if (!format) /* Allow callers to leave without tracing anything */
327
+ return;
328
+
329
+ since = perf_start_times[perf_indent];
330
+ va_start(ap, format);
331
+ trace_performance_vprintf_fl(file, line, nanos - since, format, ap);
332
+ va_end(ap);
333
+}
334
+
335
#endif /* HAVE_VARIADIC_MACROS */
336
337
470
}
471
}
472
414
-static uint64_t command_start_time;
473
static struct strbuf command_line = STRBUF_INIT;
474
475
static void print_command_performance_atexit(void)
476
{
419
- trace_performance_since(command_start_time, "git command:%s",
420
- command_line.buf);
477
+ trace_performance_leave("git command:%s", command_line.buf);
478
}
479
480
void trace_command_performance(const char **argv)
482
if (!trace_want(&trace_perf_key))
483
return;
484
428
- if (!command_start_time)
485
+ if (!command_line.len)
486
atexit(print_command_performance_atexit);
487
488
strbuf_reset(&command_line);
489
sq_quote_argv_pretty(&command_line, argv);
433
- command_start_time = getnanotime();
490
+ trace_performance_enter();
491
}