2 * GIT - The information manager from hell
4 * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
5 * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
6 * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
7 * Copyright (C) 2006 Mike McCormack
8 * Copyright (C) 2006 Christian Couder
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 #define USE_THE_REPOSITORY_VARIABLE
26 #include "git-compat-util.h"
28 #include "repository.h"
33 struct trace_key trace_default_key
= { "GIT_TRACE", 0, 0, 0 };
34 struct trace_key trace_perf_key
= TRACE_KEY_INIT(PERFORMANCE
);
35 struct trace_key trace_setup_key
= TRACE_KEY_INIT(SETUP
);
37 /* Get a trace file descriptor from "key" env variable. */
38 static int get_trace_fd(struct trace_key
*key
, const char *override_envvar
)
42 /* don't open twice */
46 trace
= override_envvar
? override_envvar
: getenv(key
->key
);
48 if (!trace
|| !strcmp(trace
, "") ||
49 !strcmp(trace
, "0") || !strcasecmp(trace
, "false"))
51 else if (!strcmp(trace
, "1") || !strcasecmp(trace
, "true"))
52 key
->fd
= STDERR_FILENO
;
53 else if (strlen(trace
) == 1 && isdigit(*trace
))
54 key
->fd
= atoi(trace
);
55 else if (is_absolute_path(trace
)) {
56 int fd
= open(trace
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
58 warning("could not open '%s' for tracing: %s",
59 trace
, strerror(errno
));
66 warning("unknown trace value for '%s': %s\n"
67 " If you want to trace into a file, then please set %s\n"
68 " to an absolute pathname (starting with /)",
69 key
->key
, trace
, key
->key
);
77 void trace_override_envvar(struct trace_key
*key
, const char *value
)
83 * Invoke get_trace_fd() to initialize key using the given value
84 * instead of the value of the environment variable.
86 get_trace_fd(key
, value
);
89 void trace_disable(struct trace_key
*key
)
98 static int prepare_trace_line(const char *file
, int line
,
99 struct trace_key
*key
, struct strbuf
*buf
)
101 static struct trace_key trace_bare
= TRACE_KEY_INIT(BARE
);
106 if (!trace_want(key
))
109 /* unit tests may want to disable additional trace output */
110 if (trace_want(&trace_bare
))
113 /* print current timestamp */
114 gettimeofday(&tv
, NULL
);
116 localtime_r(&secs
, &tm
);
117 strbuf_addf(buf
, "%02d:%02d:%02d.%06ld %s:%d", tm
.tm_hour
, tm
.tm_min
,
118 tm
.tm_sec
, (long) tv
.tv_usec
, file
, line
);
119 /* align trace output (column 40 catches most files names in git) */
120 while (buf
->len
< 40)
121 strbuf_addch(buf
, ' ');
126 static void trace_write(struct trace_key
*key
, const void *buf
, unsigned len
)
128 if (write_in_full(get_trace_fd(key
, NULL
), buf
, len
) < 0) {
129 warning("unable to write trace for %s: %s",
130 key
->key
, strerror(errno
));
135 void trace_verbatim(struct trace_key
*key
, const void *buf
, unsigned len
)
137 if (!trace_want(key
))
139 trace_write(key
, buf
, len
);
142 static void print_trace_line(struct trace_key
*key
, struct strbuf
*buf
)
144 strbuf_complete_line(buf
);
145 trace_write(key
, buf
->buf
, buf
->len
);
148 static void trace_vprintf_fl(const char *file
, int line
, struct trace_key
*key
,
149 const char *format
, va_list ap
)
151 struct strbuf buf
= STRBUF_INIT
;
153 if (!prepare_trace_line(file
, line
, key
, &buf
))
156 strbuf_vaddf(&buf
, format
, ap
);
157 print_trace_line(key
, &buf
);
158 strbuf_release(&buf
);
161 static void trace_argv_vprintf_fl(const char *file
, int line
,
162 const char **argv
, const char *format
,
165 struct strbuf buf
= STRBUF_INIT
;
167 if (!prepare_trace_line(file
, line
, &trace_default_key
, &buf
))
170 strbuf_vaddf(&buf
, format
, ap
);
172 sq_quote_argv_pretty(&buf
, argv
);
173 print_trace_line(&trace_default_key
, &buf
);
174 strbuf_release(&buf
);
177 void trace_strbuf_fl(const char *file
, int line
, struct trace_key
*key
,
178 const struct strbuf
*data
)
180 struct strbuf buf
= STRBUF_INIT
;
182 if (!prepare_trace_line(file
, line
, key
, &buf
))
185 strbuf_addbuf(&buf
, data
);
186 print_trace_line(key
, &buf
);
187 strbuf_release(&buf
);
190 static uint64_t perf_start_times
[10];
191 static int perf_indent
;
193 uint64_t trace_performance_enter(void)
197 if (!trace_want(&trace_perf_key
))
201 perf_start_times
[perf_indent
] = now
;
202 if (perf_indent
+ 1 < ARRAY_SIZE(perf_start_times
))
205 BUG("Too deep indentation");
209 static void trace_performance_vprintf_fl(const char *file
, int line
,
210 uint64_t nanos
, const char *format
,
213 static const char space
[] = " ";
214 struct strbuf buf
= STRBUF_INIT
;
216 if (!prepare_trace_line(file
, line
, &trace_perf_key
, &buf
))
219 strbuf_addf(&buf
, "performance: %.9f s", (double) nanos
/ 1000000000);
221 if (format
&& *format
) {
222 if (perf_indent
>= strlen(space
))
223 BUG("Too deep indentation");
225 strbuf_addf(&buf
, ":%.*s ", perf_indent
, space
);
226 strbuf_vaddf(&buf
, format
, ap
);
229 print_trace_line(&trace_perf_key
, &buf
);
230 strbuf_release(&buf
);
233 void trace_printf_key_fl(const char *file
, int line
, struct trace_key
*key
,
234 const char *format
, ...)
237 va_start(ap
, format
);
238 trace_vprintf_fl(file
, line
, key
, format
, ap
);
242 void trace_argv_printf_fl(const char *file
, int line
, const char **argv
,
243 const char *format
, ...)
246 va_start(ap
, format
);
247 trace_argv_vprintf_fl(file
, line
, argv
, format
, ap
);
251 void trace_performance_fl(const char *file
, int line
, uint64_t nanos
,
252 const char *format
, ...)
255 va_start(ap
, format
);
256 trace_performance_vprintf_fl(file
, line
, nanos
, format
, ap
);
260 void trace_performance_leave_fl(const char *file
, int line
,
261 uint64_t nanos
, const char *format
, ...)
269 if (!format
) /* Allow callers to leave without tracing anything */
272 since
= perf_start_times
[perf_indent
];
273 va_start(ap
, format
);
274 trace_performance_vprintf_fl(file
, line
, nanos
- since
, format
, ap
);
278 static const char *quote_crnl(const char *path
)
280 static struct strbuf new_path
= STRBUF_INIT
;
285 strbuf_reset(&new_path
);
289 case '\\': strbuf_addstr(&new_path
, "\\\\"); break;
290 case '\n': strbuf_addstr(&new_path
, "\\n"); break;
291 case '\r': strbuf_addstr(&new_path
, "\\r"); break;
293 strbuf_addch(&new_path
, *path
);
300 void trace_repo_setup(void)
302 const char *git_work_tree
, *prefix
= startup_info
->prefix
;
305 if (!trace_want(&trace_setup_key
))
310 if (!(git_work_tree
= repo_get_work_tree(the_repository
)))
311 git_work_tree
= "(null)";
313 if (!startup_info
->prefix
)
316 trace_printf_key(&trace_setup_key
, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(the_repository
)));
317 trace_printf_key(&trace_setup_key
, "setup: git_common_dir: %s\n", quote_crnl(repo_get_common_dir(the_repository
)));
318 trace_printf_key(&trace_setup_key
, "setup: worktree: %s\n", quote_crnl(git_work_tree
));
319 trace_printf_key(&trace_setup_key
, "setup: cwd: %s\n", quote_crnl(cwd
));
320 trace_printf_key(&trace_setup_key
, "setup: prefix: %s\n", quote_crnl(prefix
));
325 int trace_want(struct trace_key
*key
)
327 return !!get_trace_fd(key
, NULL
);
330 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_MONOTONIC)
332 static inline uint64_t highres_nanos(void)
335 if (clock_gettime(CLOCK_MONOTONIC
, &ts
))
337 return (uint64_t) ts
.tv_sec
* 1000000000 + ts
.tv_nsec
;
340 #elif defined (GIT_WINDOWS_NATIVE)
342 static inline uint64_t highres_nanos(void)
344 static uint64_t high_ns
, scaled_low_ns
;
349 if (!QueryPerformanceFrequency(&cnt
))
352 /* high_ns = number of ns per cnt.HighPart */
353 high_ns
= (1000000000LL << 32) / (uint64_t) cnt
.QuadPart
;
356 * Number of ns per cnt.LowPart is 10^9 / frequency (or
357 * high_ns >> 32). For maximum precision, we scale this factor
358 * so that it just fits within 32 bit (i.e. won't overflow if
359 * multiplied with cnt.LowPart).
361 scaled_low_ns
= high_ns
;
363 while (scaled_low_ns
>= 0x100000000LL
) {
369 /* if QPF worked on initialization, we expect QPC to work as well */
370 QueryPerformanceCounter(&cnt
);
372 return (high_ns
* cnt
.HighPart
) +
373 ((scaled_low_ns
* cnt
.LowPart
) >> scale
);
377 # define highres_nanos() 0
380 static inline uint64_t gettimeofday_nanos(void)
383 gettimeofday(&tv
, NULL
);
384 return (uint64_t) tv
.tv_sec
* 1000000000 + tv
.tv_usec
* 1000;
388 * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
389 * (i.e. favoring high precision over wall clock time accuracy).
391 uint64_t getnanotime(void)
393 static uint64_t offset
;
395 /* initialization succeeded, return offset + high res time */
396 return offset
+ highres_nanos();
397 } else if (offset
== 1) {
398 /* initialization failed, fall back to gettimeofday */
399 return gettimeofday_nanos();
401 /* initialize offset if high resolution timer works */
402 uint64_t now
= gettimeofday_nanos();
403 uint64_t highres
= highres_nanos();
405 offset
= now
- highres
;
412 static struct strbuf command_line
= STRBUF_INIT
;
414 static void print_command_performance_atexit(void)
416 trace_performance_leave("git command:%s", command_line
.buf
);
419 void trace_command_performance(const char **argv
)
421 if (!trace_want(&trace_perf_key
))
424 if (!command_line
.len
)
425 atexit(print_command_performance_atexit
);
427 strbuf_reset(&command_line
);
428 sq_quote_argv_pretty(&command_line
, argv
);
429 trace_performance_enter();