trace: add trace_performance facility to debug performance issues
[git] / trace.h
1 #ifndef TRACE_H
2 #define TRACE_H
3
4 #include "git-compat-util.h"
5 #include "strbuf.h"
6
7 struct trace_key {
8         const char * const key;
9         int fd;
10         unsigned int initialized : 1;
11         unsigned int  need_close : 1;
12 };
13
14 #define TRACE_KEY_INIT(name) { "GIT_TRACE_" #name, 0, 0, 0 }
15
16 extern void trace_repo_setup(const char *prefix);
17 extern int trace_want(struct trace_key *key);
18 extern void trace_disable(struct trace_key *key);
19 extern uint64_t getnanotime(void);
20
21 #ifndef HAVE_VARIADIC_MACROS
22
23 __attribute__((format (printf, 1, 2)))
24 extern void trace_printf(const char *format, ...);
25
26 __attribute__((format (printf, 2, 3)))
27 extern void trace_printf_key(struct trace_key *key, const char *format, ...);
28
29 __attribute__((format (printf, 2, 3)))
30 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 /*
45  * Macros to add file:line - see above for C-style declarations of how these
46  * should be used.
47  */
48
49 /*
50  * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
51  * default is __FILE__, as it is consistent with assert(), and static function
52  * names are not necessarily unique.
53  *
54  * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
55  * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
56  * the compiler as a string constant.
57  */
58 #ifndef TRACE_CONTEXT
59 # define TRACE_CONTEXT __FILE__
60 #endif
61
62 /*
63  * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
64  * parameter ('format' in this case). Otherwise, a call without variable
65  * arguments will have a surplus ','. E.g.:
66  *
67  *  #define foo(format, ...) bar(format, __VA_ARGS__)
68  *  foo("test");
69  *
70  * will expand to
71  *
72  *  bar("test",);
73  *
74  * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
75  * comma, but this is non-standard.
76  */
77
78 #define trace_printf(...) \
79         trace_printf_key_fl(TRACE_CONTEXT, __LINE__, NULL, __VA_ARGS__)
80
81 #define trace_printf_key(key, ...) \
82         trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, __VA_ARGS__)
83
84 #define trace_argv_printf(argv, ...) \
85         trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, argv, __VA_ARGS__)
86
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,
100                                 const char *format, ...);
101 __attribute__((format (printf, 4, 5)))
102 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
112 #endif /* TRACE_H */