Skip to content

Commit 1974f47

Browse files
committed
Merge branch 'gk/tracing-optimization'
The tracing infrastructure has been optimized for cases where no tracing is requested. * gk/tracing-optimization: trace: improve performance while category is disabled trace: remove trace key normalization
2 parents 6f3a0b6 + 8eeb25c commit 1974f47

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

trace.c

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,14 @@
2424
#include "cache.h"
2525
#include "quote.h"
2626

27-
/*
28-
* "Normalize" a key argument by converting NULL to our trace_default,
29-
* and otherwise passing through the value. All caller-facing functions
30-
* should normalize their inputs in this way, though most get it
31-
* for free by calling get_trace_fd() (directly or indirectly).
32-
*/
33-
static void normalize_trace_key(struct trace_key **key)
34-
{
35-
static struct trace_key trace_default = { "GIT_TRACE" };
36-
if (!*key)
37-
*key = &trace_default;
38-
}
27+
struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
28+
struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
3929

4030
/* Get a trace file descriptor from "key" env variable. */
4131
static int get_trace_fd(struct trace_key *key)
4232
{
4333
const char *trace;
4434

45-
normalize_trace_key(&key);
46-
4735
/* don't open twice */
4836
if (key->initialized)
4937
return key->fd;
@@ -81,8 +69,6 @@ static int get_trace_fd(struct trace_key *key)
8169

8270
void trace_disable(struct trace_key *key)
8371
{
84-
normalize_trace_key(&key);
85-
8672
if (key->need_close)
8773
close(key->fd);
8874
key->fd = 0;
@@ -128,7 +114,6 @@ static int prepare_trace_line(const char *file, int line,
128114
static void trace_write(struct trace_key *key, const void *buf, unsigned len)
129115
{
130116
if (write_in_full(get_trace_fd(key), buf, len) < 0) {
131-
normalize_trace_key(&key);
132117
warning("unable to write trace for %s: %s",
133118
key->key, strerror(errno));
134119
trace_disable(key);
@@ -167,13 +152,13 @@ static void trace_argv_vprintf_fl(const char *file, int line,
167152
{
168153
struct strbuf buf = STRBUF_INIT;
169154

170-
if (!prepare_trace_line(file, line, NULL, &buf))
155+
if (!prepare_trace_line(file, line, &trace_default_key, &buf))
171156
return;
172157

173158
strbuf_vaddf(&buf, format, ap);
174159

175160
sq_quote_argv(&buf, argv, 0);
176-
print_trace_line(NULL, &buf);
161+
print_trace_line(&trace_default_key, &buf);
177162
}
178163

179164
void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
@@ -188,8 +173,6 @@ void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
188173
print_trace_line(key, &buf);
189174
}
190175

191-
static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
192-
193176
static void trace_performance_vprintf_fl(const char *file, int line,
194177
uint64_t nanos, const char *format,
195178
va_list ap)
@@ -215,7 +198,7 @@ void trace_printf(const char *format, ...)
215198
{
216199
va_list ap;
217200
va_start(ap, format);
218-
trace_vprintf_fl(NULL, 0, NULL, format, ap);
201+
trace_vprintf_fl(NULL, 0, &trace_default_key, format, ap);
219202
va_end(ap);
220203
}
221204

trace.h

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ struct trace_key {
1111
unsigned int need_close : 1;
1212
};
1313

14+
extern struct trace_key trace_default_key;
15+
1416
#define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
17+
extern struct trace_key trace_perf_key;
1518

1619
extern void trace_repo_setup(const char *prefix);
1720
extern int trace_want(struct trace_key *key);
@@ -77,24 +80,42 @@ extern void trace_performance_since(uint64_t start, const char *format, ...);
7780
* comma, but this is non-standard.
7881
*/
7982

80-
#define trace_printf(...) \
81-
trace_printf_key_fl(TRACE_CONTEXT, __LINE__, NULL, __VA_ARGS__)
82-
83-
#define trace_printf_key(key, ...) \
84-
trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, __VA_ARGS__)
85-
86-
#define trace_argv_printf(argv, ...) \
87-
trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, argv, __VA_ARGS__)
88-
89-
#define trace_strbuf(key, data) \
90-
trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data)
91-
92-
#define trace_performance(nanos, ...) \
93-
trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos, __VA_ARGS__)
94-
95-
#define trace_performance_since(start, ...) \
96-
trace_performance_fl(TRACE_CONTEXT, __LINE__, getnanotime() - (start), \
97-
__VA_ARGS__)
83+
#define trace_printf_key(key, ...) \
84+
do { \
85+
if (trace_pass_fl(key)) \
86+
trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
87+
__VA_ARGS__); \
88+
} while (0)
89+
90+
#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
91+
92+
#define trace_argv_printf(argv, ...) \
93+
do { \
94+
if (trace_pass_fl(&trace_default_key)) \
95+
trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
96+
argv, __VA_ARGS__); \
97+
} while (0)
98+
99+
#define trace_strbuf(key, data) \
100+
do { \
101+
if (trace_pass_fl(key)) \
102+
trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
103+
} while (0)
104+
105+
#define trace_performance(nanos, ...) \
106+
do { \
107+
if (trace_pass_fl(&trace_perf_key)) \
108+
trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
109+
__VA_ARGS__); \
110+
} while (0)
111+
112+
#define trace_performance_since(start, ...) \
113+
do { \
114+
if (trace_pass_fl(&trace_perf_key)) \
115+
trace_performance_fl(TRACE_CONTEXT, __LINE__, \
116+
getnanotime() - (start), \
117+
__VA_ARGS__); \
118+
} while (0)
98119

99120
/* backend functions, use non-*fl macros instead */
100121
__attribute__((format (printf, 4, 5)))
@@ -108,6 +129,10 @@ extern void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
108129
__attribute__((format (printf, 4, 5)))
109130
extern void trace_performance_fl(const char *file, int line,
110131
uint64_t nanos, const char *fmt, ...);
132+
static inline int trace_pass_fl(struct trace_key *key)
133+
{
134+
return key->fd || !key->initialized;
135+
}
111136

112137
#endif /* HAVE_VARIADIC_MACROS */
113138

0 commit comments

Comments
 (0)