| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 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 |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| Josh Soref | d05b08c | 2023-11-24 03:35:13 | [diff] [blame] | 21 | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 22 | */ |
| 23 | |
| Patrick Steinhardt | 41f43b8 | 2024-12-06 10:27:19 | [diff] [blame] | 24 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| Patrick Steinhardt | 246deea | 2024-09-12 11:29:24 | [diff] [blame] | 25 | |
| Elijah Newren | 5579f44 | 2023-04-11 07:41:48 | [diff] [blame] | 26 | #include "git-compat-util.h" |
| Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 | [diff] [blame] | 27 | #include "abspath.h" |
| Patrick Steinhardt | 246deea | 2024-09-12 11:29:24 | [diff] [blame] | 28 | #include "repository.h" |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 29 | #include "quote.h" |
| Elijah Newren | e38da48 | 2023-03-21 06:26:05 | [diff] [blame] | 30 | #include "setup.h" |
| Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 | [diff] [blame] | 31 | #include "trace.h" |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 32 | |
| Gennady Kupava | 406102a | 2017-11-26 20:11:18 | [diff] [blame] | 33 | struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 }; |
| Gennady Kupava | 8eeb25c | 2017-11-26 20:11:19 | [diff] [blame] | 34 | struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE); |
| Nguyễn Thái Ngọc Duy | cb50761 | 2018-03-30 18:34:59 | [diff] [blame] | 35 | struct trace_key trace_setup_key = TRACE_KEY_INIT(SETUP); |
| Jeff King | c81539b | 2016-08-03 22:56:57 | [diff] [blame] | 36 | |
| Jeff King | 0679660 | 2011-02-24 14:28:41 | [diff] [blame] | 37 | /* Get a trace file descriptor from "key" env variable. */ |
| Jonathan Tan | 7167a62 | 2020-05-11 17:43:10 | [diff] [blame] | 38 | static int get_trace_fd(struct trace_key *key, const char *override_envvar) |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 39 | { |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 40 | const char *trace; |
| 41 | |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 42 | /* don't open twice */ |
| 43 | if (key->initialized) |
| 44 | return key->fd; |
| 45 | |
| Jonathan Tan | 7167a62 | 2020-05-11 17:43:10 | [diff] [blame] | 46 | trace = override_envvar ? override_envvar : getenv(key->key); |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 47 | |
| Christian Couder | 6844fc8 | 2006-10-14 14:05:25 | [diff] [blame] | 48 | if (!trace || !strcmp(trace, "") || |
| 49 | !strcmp(trace, "0") || !strcasecmp(trace, "false")) |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 50 | key->fd = 0; |
| 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)) { |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 56 | int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666); |
| 57 | if (fd == -1) { |
| Jeff King | 6f25305 | 2016-08-05 07:58:38 | [diff] [blame] | 58 | warning("could not open '%s' for tracing: %s", |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 59 | trace, strerror(errno)); |
| Jeff King | 6f25305 | 2016-08-05 07:58:38 | [diff] [blame] | 60 | trace_disable(key); |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 61 | } else { |
| 62 | key->fd = fd; |
| 63 | key->need_close = 1; |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 64 | } |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 65 | } else { |
| Jeff King | b3a1c5d | 2016-08-03 23:00:23 | [diff] [blame] | 66 | warning("unknown trace value for '%s': %s\n" |
| 67 | " If you want to trace into a file, then please set %s\n" |
| Jeff King | 6f25305 | 2016-08-05 07:58:38 | [diff] [blame] | 68 | " to an absolute pathname (starting with /)", |
| Jeff King | b3a1c5d | 2016-08-03 23:00:23 | [diff] [blame] | 69 | key->key, trace, key->key); |
| Jeff King | 6f25305 | 2016-08-05 07:58:38 | [diff] [blame] | 70 | trace_disable(key); |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 71 | } |
| 72 | |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 73 | key->initialized = 1; |
| 74 | return key->fd; |
| 75 | } |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 76 | |
| Jonathan Tan | 7167a62 | 2020-05-11 17:43:10 | [diff] [blame] | 77 | void trace_override_envvar(struct trace_key *key, const char *value) |
| 78 | { |
| 79 | trace_disable(key); |
| 80 | key->initialized = 0; |
| 81 | |
| 82 | /* |
| 83 | * Invoke get_trace_fd() to initialize key using the given value |
| 84 | * instead of the value of the environment variable. |
| 85 | */ |
| 86 | get_trace_fd(key, value); |
| 87 | } |
| 88 | |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 89 | void trace_disable(struct trace_key *key) |
| 90 | { |
| 91 | if (key->need_close) |
| 92 | close(key->fd); |
| 93 | key->fd = 0; |
| 94 | key->initialized = 1; |
| 95 | key->need_close = 0; |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 96 | } |
| 97 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 98 | static int prepare_trace_line(const char *file, int line, |
| 99 | struct trace_key *key, struct strbuf *buf) |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 100 | { |
| Karsten Blees | 124647c | 2014-07-12 00:03:01 | [diff] [blame] | 101 | static struct trace_key trace_bare = TRACE_KEY_INIT(BARE); |
| Karsten Blees | b72be02 | 2014-07-12 00:03:50 | [diff] [blame] | 102 | struct timeval tv; |
| 103 | struct tm tm; |
| 104 | time_t secs; |
| Karsten Blees | 124647c | 2014-07-12 00:03:01 | [diff] [blame] | 105 | |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 106 | if (!trace_want(key)) |
| 107 | return 0; |
| 108 | |
| Karsten Blees | 124647c | 2014-07-12 00:03:01 | [diff] [blame] | 109 | /* unit tests may want to disable additional trace output */ |
| 110 | if (trace_want(&trace_bare)) |
| 111 | return 1; |
| 112 | |
| Karsten Blees | b72be02 | 2014-07-12 00:03:50 | [diff] [blame] | 113 | /* print current timestamp */ |
| 114 | gettimeofday(&tv, NULL); |
| 115 | secs = tv.tv_sec; |
| 116 | localtime_r(&secs, &tm); |
| Ævar Arnfjörð Bjarmason | 56a29d2 | 2022-02-21 16:05:27 | [diff] [blame] | 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); |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 119 | /* align trace output (column 40 catches most files names in git) */ |
| 120 | while (buf->len < 40) |
| 121 | strbuf_addch(buf, ' '); |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 122 | |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
| Jeff King | c0222e7 | 2016-08-03 22:58:00 | [diff] [blame] | 126 | static void trace_write(struct trace_key *key, const void *buf, unsigned len) |
| 127 | { |
| Jonathan Tan | 7167a62 | 2020-05-11 17:43:10 | [diff] [blame] | 128 | if (write_in_full(get_trace_fd(key, NULL), buf, len) < 0) { |
| Jeff King | 3b0c3ab | 2016-08-03 23:00:32 | [diff] [blame] | 129 | warning("unable to write trace for %s: %s", |
| 130 | key->key, strerror(errno)); |
| Jeff King | 46ac74b | 2016-08-03 23:01:04 | [diff] [blame] | 131 | trace_disable(key); |
| Jeff King | 3b0c3ab | 2016-08-03 23:00:32 | [diff] [blame] | 132 | } |
| Jeff King | c0222e7 | 2016-08-03 22:58:00 | [diff] [blame] | 133 | } |
| 134 | |
| Jeff King | 3235983 | 2015-06-16 17:23:20 | [diff] [blame] | 135 | void trace_verbatim(struct trace_key *key, const void *buf, unsigned len) |
| 136 | { |
| 137 | if (!trace_want(key)) |
| 138 | return; |
| Jeff King | c0222e7 | 2016-08-03 22:58:00 | [diff] [blame] | 139 | trace_write(key, buf, len); |
| Jeff King | 3235983 | 2015-06-16 17:23:20 | [diff] [blame] | 140 | } |
| 141 | |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 142 | static void print_trace_line(struct trace_key *key, struct strbuf *buf) |
| 143 | { |
| René Scharfe | a0d4923 | 2014-12-12 19:16:38 | [diff] [blame] | 144 | strbuf_complete_line(buf); |
| Jeff King | c0222e7 | 2016-08-03 22:58:00 | [diff] [blame] | 145 | trace_write(key, buf->buf, buf->len); |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 146 | } |
| 147 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 148 | static void trace_vprintf_fl(const char *file, int line, struct trace_key *key, |
| 149 | const char *format, va_list ap) |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 150 | { |
| Jeff King | ebeb609 | 2011-02-26 05:08:53 | [diff] [blame] | 151 | struct strbuf buf = STRBUF_INIT; |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 152 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 153 | if (!prepare_trace_line(file, line, key, &buf)) |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 154 | return; |
| 155 | |
| Karsten Blees | 4a3b0b2 | 2014-06-11 07:57:23 | [diff] [blame] | 156 | strbuf_vaddf(&buf, format, ap); |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 157 | print_trace_line(key, &buf); |
| Nguyễn Thái Ngọc Duy | 33011e7 | 2018-01-15 10:59:45 | [diff] [blame] | 158 | strbuf_release(&buf); |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 159 | } |
| 160 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 161 | static void trace_argv_vprintf_fl(const char *file, int line, |
| 162 | const char **argv, const char *format, |
| 163 | va_list ap) |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 164 | { |
| Jeff King | ebeb609 | 2011-02-26 05:08:53 | [diff] [blame] | 165 | struct strbuf buf = STRBUF_INIT; |
| Karsten Blees | c69dfd2 | 2014-07-12 00:02:18 | [diff] [blame] | 166 | |
| Gennady Kupava | 406102a | 2017-11-26 20:11:18 | [diff] [blame] | 167 | if (!prepare_trace_line(file, line, &trace_default_key, &buf)) |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 168 | return; |
| 169 | |
| Karsten Blees | 4a3b0b2 | 2014-06-11 07:57:23 | [diff] [blame] | 170 | strbuf_vaddf(&buf, format, ap); |
| Pierre Habouzit | 19247e5 | 2007-09-20 08:43:11 | [diff] [blame] | 171 | |
| Jeff King | 1fbdab2 | 2018-01-15 10:59:44 | [diff] [blame] | 172 | sq_quote_argv_pretty(&buf, argv); |
| Gennady Kupava | 406102a | 2017-11-26 20:11:18 | [diff] [blame] | 173 | print_trace_line(&trace_default_key, &buf); |
| Nguyễn Thái Ngọc Duy | 33011e7 | 2018-01-15 10:59:45 | [diff] [blame] | 174 | strbuf_release(&buf); |
| Christian Couder | 6ce4e61 | 2006-09-02 16:23:48 | [diff] [blame] | 175 | } |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 176 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 177 | void trace_strbuf_fl(const char *file, int line, struct trace_key *key, |
| 178 | const struct strbuf *data) |
| Karsten Blees | 66f66c5 | 2014-07-12 00:04:29 | [diff] [blame] | 179 | { |
| 180 | struct strbuf buf = STRBUF_INIT; |
| 181 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 182 | if (!prepare_trace_line(file, line, key, &buf)) |
| Karsten Blees | 66f66c5 | 2014-07-12 00:04:29 | [diff] [blame] | 183 | return; |
| 184 | |
| 185 | strbuf_addbuf(&buf, data); |
| 186 | print_trace_line(key, &buf); |
| Nguyễn Thái Ngọc Duy | 33011e7 | 2018-01-15 10:59:45 | [diff] [blame] | 187 | strbuf_release(&buf); |
| Karsten Blees | 66f66c5 | 2014-07-12 00:04:29 | [diff] [blame] | 188 | } |
| 189 | |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 190 | static uint64_t perf_start_times[10]; |
| 191 | static int perf_indent; |
| 192 | |
| 193 | uint64_t trace_performance_enter(void) |
| 194 | { |
| 195 | uint64_t now; |
| 196 | |
| 197 | if (!trace_want(&trace_perf_key)) |
| 198 | return 0; |
| 199 | |
| 200 | now = getnanotime(); |
| 201 | perf_start_times[perf_indent] = now; |
| 202 | if (perf_indent + 1 < ARRAY_SIZE(perf_start_times)) |
| 203 | perf_indent++; |
| 204 | else |
| 205 | BUG("Too deep indentation"); |
| 206 | return now; |
| 207 | } |
| 208 | |
| Karsten Blees | 09b2c1c | 2014-07-12 00:06:28 | [diff] [blame] | 209 | static void trace_performance_vprintf_fl(const char *file, int line, |
| 210 | uint64_t nanos, const char *format, |
| 211 | va_list ap) |
| 212 | { |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 213 | static const char space[] = " "; |
| Karsten Blees | 09b2c1c | 2014-07-12 00:06:28 | [diff] [blame] | 214 | struct strbuf buf = STRBUF_INIT; |
| 215 | |
| 216 | if (!prepare_trace_line(file, line, &trace_perf_key, &buf)) |
| 217 | return; |
| 218 | |
| 219 | strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000); |
| 220 | |
| 221 | if (format && *format) { |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 222 | if (perf_indent >= strlen(space)) |
| 223 | BUG("Too deep indentation"); |
| 224 | |
| 225 | strbuf_addf(&buf, ":%.*s ", perf_indent, space); |
| Karsten Blees | 09b2c1c | 2014-07-12 00:06:28 | [diff] [blame] | 226 | strbuf_vaddf(&buf, format, ap); |
| 227 | } |
| 228 | |
| 229 | print_trace_line(&trace_perf_key, &buf); |
| Nguyễn Thái Ngọc Duy | 33011e7 | 2018-01-15 10:59:45 | [diff] [blame] | 230 | strbuf_release(&buf); |
| Karsten Blees | 09b2c1c | 2014-07-12 00:06:28 | [diff] [blame] | 231 | } |
| 232 | |
| Karsten Blees | e05bed9 | 2014-07-12 00:05:03 | [diff] [blame] | 233 | void trace_printf_key_fl(const char *file, int line, struct trace_key *key, |
| 234 | const char *format, ...) |
| 235 | { |
| 236 | va_list ap; |
| 237 | va_start(ap, format); |
| 238 | trace_vprintf_fl(file, line, key, format, ap); |
| 239 | va_end(ap); |
| 240 | } |
| 241 | |
| 242 | void trace_argv_printf_fl(const char *file, int line, const char **argv, |
| 243 | const char *format, ...) |
| 244 | { |
| 245 | va_list ap; |
| 246 | va_start(ap, format); |
| 247 | trace_argv_vprintf_fl(file, line, argv, format, ap); |
| 248 | va_end(ap); |
| 249 | } |
| 250 | |
| Karsten Blees | 09b2c1c | 2014-07-12 00:06:28 | [diff] [blame] | 251 | void trace_performance_fl(const char *file, int line, uint64_t nanos, |
| 252 | const char *format, ...) |
| 253 | { |
| 254 | va_list ap; |
| 255 | va_start(ap, format); |
| 256 | trace_performance_vprintf_fl(file, line, nanos, format, ap); |
| 257 | va_end(ap); |
| 258 | } |
| 259 | |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 260 | void trace_performance_leave_fl(const char *file, int line, |
| 261 | uint64_t nanos, const char *format, ...) |
| 262 | { |
| 263 | va_list ap; |
| 264 | uint64_t since; |
| 265 | |
| 266 | if (perf_indent) |
| 267 | perf_indent--; |
| 268 | |
| 269 | if (!format) /* Allow callers to leave without tracing anything */ |
| 270 | return; |
| 271 | |
| 272 | since = perf_start_times[perf_indent]; |
| 273 | va_start(ap, format); |
| 274 | trace_performance_vprintf_fl(file, line, nanos - since, format, ap); |
| 275 | va_end(ap); |
| 276 | } |
| 277 | |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 278 | static const char *quote_crnl(const char *path) |
| 279 | { |
| Jeff King | 0bb443f | 2015-09-24 21:05:54 | [diff] [blame] | 280 | static struct strbuf new_path = STRBUF_INIT; |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 281 | |
| 282 | if (!path) |
| 283 | return NULL; |
| 284 | |
| Jeff King | 0bb443f | 2015-09-24 21:05:54 | [diff] [blame] | 285 | strbuf_reset(&new_path); |
| 286 | |
| 287 | while (*path) { |
| 288 | switch (*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; |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 292 | default: |
| Jeff King | 0bb443f | 2015-09-24 21:05:54 | [diff] [blame] | 293 | strbuf_addch(&new_path, *path); |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 294 | } |
| Jeff King | 0bb443f | 2015-09-24 21:05:54 | [diff] [blame] | 295 | path++; |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 296 | } |
| Jeff King | 0bb443f | 2015-09-24 21:05:54 | [diff] [blame] | 297 | return new_path.buf; |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 298 | } |
| 299 | |
| Patrick Steinhardt | bd0c0fb | 2024-12-17 06:43:50 | [diff] [blame] | 300 | void trace_repo_setup(struct repository *r) |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 301 | { |
| idriss fekir | 17ab64e | 2023-02-19 00:25:27 | [diff] [blame] | 302 | const char *git_work_tree, *prefix = startup_info->prefix; |
| René Scharfe | 56b9f6e | 2014-07-28 18:30:39 | [diff] [blame] | 303 | char *cwd; |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 304 | |
| Nguyễn Thái Ngọc Duy | cb50761 | 2018-03-30 18:34:59 | [diff] [blame] | 305 | if (!trace_want(&trace_setup_key)) |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 306 | return; |
| 307 | |
| René Scharfe | 56b9f6e | 2014-07-28 18:30:39 | [diff] [blame] | 308 | cwd = xgetcwd(); |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 309 | |
| Patrick Steinhardt | bd0c0fb | 2024-12-17 06:43:50 | [diff] [blame] | 310 | if (!(git_work_tree = repo_get_work_tree(r))) |
| Brandon Casey | e83c267 | 2011-01-06 00:30:01 | [diff] [blame] | 311 | git_work_tree = "(null)"; |
| 312 | |
| idriss fekir | 17ab64e | 2023-02-19 00:25:27 | [diff] [blame] | 313 | if (!startup_info->prefix) |
| Brandon Casey | e83c267 | 2011-01-06 00:30:01 | [diff] [blame] | 314 | prefix = "(null)"; |
| 315 | |
| Patrick Steinhardt | bd0c0fb | 2024-12-17 06:43:50 | [diff] [blame] | 316 | trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(r))); |
| 317 | trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(repo_get_common_dir(r))); |
| Nguyễn Thái Ngọc Duy | cb50761 | 2018-03-30 18:34:59 | [diff] [blame] | 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)); |
| René Scharfe | 56b9f6e | 2014-07-28 18:30:39 | [diff] [blame] | 321 | |
| 322 | free(cwd); |
| Nguyễn Thái Ngọc Duy | a9ca8a8 | 2010-11-26 15:31:57 | [diff] [blame] | 323 | } |
| Jeff King | 39bc5e4 | 2011-02-24 14:28:59 | [diff] [blame] | 324 | |
| Karsten Blees | 6aa3085 | 2014-07-12 00:00:06 | [diff] [blame] | 325 | int trace_want(struct trace_key *key) |
| Jeff King | 39bc5e4 | 2011-02-24 14:28:59 | [diff] [blame] | 326 | { |
| Jonathan Tan | 7167a62 | 2020-05-11 17:43:10 | [diff] [blame] | 327 | return !!get_trace_fd(key, NULL); |
| Jeff King | 39bc5e4 | 2011-02-24 14:28:59 | [diff] [blame] | 328 | } |
| Karsten Blees | 148d677 | 2014-07-12 00:05:42 | [diff] [blame] | 329 | |
| Reuben Hawkins | a6c3c63 | 2015-01-08 20:00:56 | [diff] [blame] | 330 | #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_MONOTONIC) |
| Karsten Blees | 148d677 | 2014-07-12 00:05:42 | [diff] [blame] | 331 | |
| 332 | static inline uint64_t highres_nanos(void) |
| 333 | { |
| 334 | struct timespec ts; |
| 335 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 336 | return 0; |
| 337 | return (uint64_t) ts.tv_sec * 1000000000 + ts.tv_nsec; |
| 338 | } |
| 339 | |
| 340 | #elif defined (GIT_WINDOWS_NATIVE) |
| 341 | |
| 342 | static inline uint64_t highres_nanos(void) |
| 343 | { |
| 344 | static uint64_t high_ns, scaled_low_ns; |
| 345 | static int scale; |
| 346 | LARGE_INTEGER cnt; |
| 347 | |
| 348 | if (!scale) { |
| 349 | if (!QueryPerformanceFrequency(&cnt)) |
| 350 | return 0; |
| 351 | |
| 352 | /* high_ns = number of ns per cnt.HighPart */ |
| 353 | high_ns = (1000000000LL << 32) / (uint64_t) cnt.QuadPart; |
| 354 | |
| 355 | /* |
| 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). |
| 360 | */ |
| 361 | scaled_low_ns = high_ns; |
| 362 | scale = 32; |
| 363 | while (scaled_low_ns >= 0x100000000LL) { |
| 364 | scaled_low_ns >>= 1; |
| 365 | scale--; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* if QPF worked on initialization, we expect QPC to work as well */ |
| 370 | QueryPerformanceCounter(&cnt); |
| 371 | |
| 372 | return (high_ns * cnt.HighPart) + |
| 373 | ((scaled_low_ns * cnt.LowPart) >> scale); |
| 374 | } |
| 375 | |
| 376 | #else |
| 377 | # define highres_nanos() 0 |
| 378 | #endif |
| 379 | |
| 380 | static inline uint64_t gettimeofday_nanos(void) |
| 381 | { |
| 382 | struct timeval tv; |
| 383 | gettimeofday(&tv, NULL); |
| 384 | return (uint64_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Returns nanoseconds since the epoch (01/01/1970), for performance tracing |
| 389 | * (i.e. favoring high precision over wall clock time accuracy). |
| 390 | */ |
| Ben Walton | 6433d56 | 2014-09-28 07:50:26 | [diff] [blame] | 391 | uint64_t getnanotime(void) |
| Karsten Blees | 148d677 | 2014-07-12 00:05:42 | [diff] [blame] | 392 | { |
| 393 | static uint64_t offset; |
| 394 | if (offset > 1) { |
| 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(); |
| 400 | } else { |
| 401 | /* initialize offset if high resolution timer works */ |
| 402 | uint64_t now = gettimeofday_nanos(); |
| 403 | uint64_t highres = highres_nanos(); |
| 404 | if (highres) |
| 405 | offset = now - highres; |
| 406 | else |
| 407 | offset = 1; |
| 408 | return now; |
| 409 | } |
| 410 | } |
| Karsten Blees | 578da03 | 2014-07-12 00:07:01 | [diff] [blame] | 411 | |
| Karsten Blees | 578da03 | 2014-07-12 00:07:01 | [diff] [blame] | 412 | static struct strbuf command_line = STRBUF_INIT; |
| 413 | |
| 414 | static void print_command_performance_atexit(void) |
| 415 | { |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 416 | trace_performance_leave("git command:%s", command_line.buf); |
| Karsten Blees | 578da03 | 2014-07-12 00:07:01 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void trace_command_performance(const char **argv) |
| 420 | { |
| 421 | if (!trace_want(&trace_perf_key)) |
| 422 | return; |
| 423 | |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 424 | if (!command_line.len) |
| Karsten Blees | 578da03 | 2014-07-12 00:07:01 | [diff] [blame] | 425 | atexit(print_command_performance_atexit); |
| 426 | |
| 427 | strbuf_reset(&command_line); |
| Jeff King | 1fbdab2 | 2018-01-15 10:59:44 | [diff] [blame] | 428 | sq_quote_argv_pretty(&command_line, argv); |
| Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 14:41:22 | [diff] [blame] | 429 | trace_performance_enter(); |
| Karsten Blees | 578da03 | 2014-07-12 00:07:01 | [diff] [blame] | 430 | } |