THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

490 lines
12KB

  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
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include "cache.h"
  24. #include "quote.h"
  25. struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
  26. struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
  27. struct trace_key trace_setup_key = TRACE_KEY_INIT(SETUP);
  28. /* Get a trace file descriptor from "key" env variable. */
  29. static int get_trace_fd(struct trace_key *key)
  30. {
  31. const char *trace;
  32. /* don't open twice */
  33. if (key->initialized)
  34. return key->fd;
  35. trace = getenv(key->key);
  36. if (!trace || !strcmp(trace, "") ||
  37. !strcmp(trace, "0") || !strcasecmp(trace, "false"))
  38. key->fd = 0;
  39. else if (!strcmp(trace, "1") || !strcasecmp(trace, "true"))
  40. key->fd = STDERR_FILENO;
  41. else if (strlen(trace) == 1 && isdigit(*trace))
  42. key->fd = atoi(trace);
  43. else if (is_absolute_path(trace)) {
  44. int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
  45. if (fd == -1) {
  46. warning("could not open '%s' for tracing: %s",
  47. trace, strerror(errno));
  48. trace_disable(key);
  49. } else {
  50. key->fd = fd;
  51. key->need_close = 1;
  52. }
  53. } else {
  54. warning("unknown trace value for '%s': %s\n"
  55. " If you want to trace into a file, then please set %s\n"
  56. " to an absolute pathname (starting with /)",
  57. key->key, trace, key->key);
  58. trace_disable(key);
  59. }
  60. key->initialized = 1;
  61. return key->fd;
  62. }
  63. void trace_disable(struct trace_key *key)
  64. {
  65. if (key->need_close)
  66. close(key->fd);
  67. key->fd = 0;
  68. key->initialized = 1;
  69. key->need_close = 0;
  70. }
  71. static int prepare_trace_line(const char *file, int line,
  72. struct trace_key *key, struct strbuf *buf)
  73. {
  74. static struct trace_key trace_bare = TRACE_KEY_INIT(BARE);
  75. struct timeval tv;
  76. struct tm tm;
  77. time_t secs;
  78. if (!trace_want(key))
  79. return 0;
  80. /* unit tests may want to disable additional trace output */
  81. if (trace_want(&trace_bare))
  82. return 1;
  83. /* print current timestamp */
  84. gettimeofday(&tv, NULL);
  85. secs = tv.tv_sec;
  86. localtime_r(&secs, &tm);
  87. strbuf_addf(buf, "%02d:%02d:%02d.%06ld ", tm.tm_hour, tm.tm_min,
  88. tm.tm_sec, (long) tv.tv_usec);
  89. #ifdef HAVE_VARIADIC_MACROS
  90. /* print file:line */
  91. strbuf_addf(buf, "%s:%d ", file, line);
  92. /* align trace output (column 40 catches most files names in git) */
  93. while (buf->len < 40)
  94. strbuf_addch(buf, ' ');
  95. #endif
  96. return 1;
  97. }
  98. static void trace_write(struct trace_key *key, const void *buf, unsigned len)
  99. {
  100. if (write_in_full(get_trace_fd(key), buf, len) < 0) {
  101. warning("unable to write trace for %s: %s",
  102. key->key, strerror(errno));
  103. trace_disable(key);
  104. }
  105. }
  106. void trace_verbatim(struct trace_key *key, const void *buf, unsigned len)
  107. {
  108. if (!trace_want(key))
  109. return;
  110. trace_write(key, buf, len);
  111. }
  112. static void print_trace_line(struct trace_key *key, struct strbuf *buf)
  113. {
  114. strbuf_complete_line(buf);
  115. trace_write(key, buf->buf, buf->len);
  116. }
  117. static void trace_vprintf_fl(const char *file, int line, struct trace_key *key,
  118. const char *format, va_list ap)
  119. {
  120. struct strbuf buf = STRBUF_INIT;
  121. if (!prepare_trace_line(file, line, key, &buf))
  122. return;
  123. strbuf_vaddf(&buf, format, ap);
  124. print_trace_line(key, &buf);
  125. strbuf_release(&buf);
  126. }
  127. static void trace_argv_vprintf_fl(const char *file, int line,
  128. const char **argv, const char *format,
  129. va_list ap)
  130. {
  131. struct strbuf buf = STRBUF_INIT;
  132. if (!prepare_trace_line(file, line, &trace_default_key, &buf))
  133. return;
  134. strbuf_vaddf(&buf, format, ap);
  135. sq_quote_argv_pretty(&buf, argv);
  136. print_trace_line(&trace_default_key, &buf);
  137. strbuf_release(&buf);
  138. }
  139. void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
  140. const struct strbuf *data)
  141. {
  142. struct strbuf buf = STRBUF_INIT;
  143. if (!prepare_trace_line(file, line, key, &buf))
  144. return;
  145. strbuf_addbuf(&buf, data);
  146. print_trace_line(key, &buf);
  147. strbuf_release(&buf);
  148. }
  149. static uint64_t perf_start_times[10];
  150. static int perf_indent;
  151. uint64_t trace_performance_enter(void)
  152. {
  153. uint64_t now;
  154. if (!trace_want(&trace_perf_key))
  155. return 0;
  156. now = getnanotime();
  157. perf_start_times[perf_indent] = now;
  158. if (perf_indent + 1 < ARRAY_SIZE(perf_start_times))
  159. perf_indent++;
  160. else
  161. BUG("Too deep indentation");
  162. return now;
  163. }
  164. static void trace_performance_vprintf_fl(const char *file, int line,
  165. uint64_t nanos, const char *format,
  166. va_list ap)
  167. {
  168. static const char space[] = " ";
  169. struct strbuf buf = STRBUF_INIT;
  170. if (!prepare_trace_line(file, line, &trace_perf_key, &buf))
  171. return;
  172. strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000);
  173. if (format && *format) {
  174. if (perf_indent >= strlen(space))
  175. BUG("Too deep indentation");
  176. strbuf_addf(&buf, ":%.*s ", perf_indent, space);
  177. strbuf_vaddf(&buf, format, ap);
  178. }
  179. print_trace_line(&trace_perf_key, &buf);
  180. strbuf_release(&buf);
  181. }
  182. #ifndef HAVE_VARIADIC_MACROS
  183. void trace_printf(const char *format, ...)
  184. {
  185. va_list ap;
  186. va_start(ap, format);
  187. trace_vprintf_fl(NULL, 0, &trace_default_key, format, ap);
  188. va_end(ap);
  189. }
  190. void trace_printf_key(struct trace_key *key, const char *format, ...)
  191. {
  192. va_list ap;
  193. va_start(ap, format);
  194. trace_vprintf_fl(NULL, 0, key, format, ap);
  195. va_end(ap);
  196. }
  197. void trace_argv_printf(const char **argv, const char *format, ...)
  198. {
  199. va_list ap;
  200. va_start(ap, format);
  201. trace_argv_vprintf_fl(NULL, 0, argv, format, ap);
  202. va_end(ap);
  203. }
  204. void trace_strbuf(struct trace_key *key, const struct strbuf *data)
  205. {
  206. trace_strbuf_fl(NULL, 0, key, data);
  207. }
  208. void trace_performance(uint64_t nanos, const char *format, ...)
  209. {
  210. va_list ap;
  211. va_start(ap, format);
  212. trace_performance_vprintf_fl(NULL, 0, nanos, format, ap);
  213. va_end(ap);
  214. }
  215. void trace_performance_since(uint64_t start, const char *format, ...)
  216. {
  217. va_list ap;
  218. va_start(ap, format);
  219. trace_performance_vprintf_fl(NULL, 0, getnanotime() - start,
  220. format, ap);
  221. va_end(ap);
  222. }
  223. void trace_performance_leave(const char *format, ...)
  224. {
  225. va_list ap;
  226. uint64_t since;
  227. if (perf_indent)
  228. perf_indent--;
  229. if (!format) /* Allow callers to leave without tracing anything */
  230. return;
  231. since = perf_start_times[perf_indent];
  232. va_start(ap, format);
  233. trace_performance_vprintf_fl(NULL, 0, getnanotime() - since,
  234. format, ap);
  235. va_end(ap);
  236. }
  237. #else
  238. void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
  239. const char *format, ...)
  240. {
  241. va_list ap;
  242. va_start(ap, format);
  243. trace_vprintf_fl(file, line, key, format, ap);
  244. va_end(ap);
  245. }
  246. void trace_argv_printf_fl(const char *file, int line, const char **argv,
  247. const char *format, ...)
  248. {
  249. va_list ap;
  250. va_start(ap, format);
  251. trace_argv_vprintf_fl(file, line, argv, format, ap);
  252. va_end(ap);
  253. }
  254. void trace_performance_fl(const char *file, int line, uint64_t nanos,
  255. const char *format, ...)
  256. {
  257. va_list ap;
  258. va_start(ap, format);
  259. trace_performance_vprintf_fl(file, line, nanos, format, ap);
  260. va_end(ap);
  261. }
  262. void trace_performance_leave_fl(const char *file, int line,
  263. uint64_t nanos, const char *format, ...)
  264. {
  265. va_list ap;
  266. uint64_t since;
  267. if (perf_indent)
  268. perf_indent--;
  269. if (!format) /* Allow callers to leave without tracing anything */
  270. return;
  271. since = perf_start_times[perf_indent];
  272. va_start(ap, format);
  273. trace_performance_vprintf_fl(file, line, nanos - since, format, ap);
  274. va_end(ap);
  275. }
  276. #endif /* HAVE_VARIADIC_MACROS */
  277. static const char *quote_crnl(const char *path)
  278. {
  279. static struct strbuf new_path = STRBUF_INIT;
  280. if (!path)
  281. return NULL;
  282. strbuf_reset(&new_path);
  283. while (*path) {
  284. switch (*path) {
  285. case '\\': strbuf_addstr(&new_path, "\\\\"); break;
  286. case '\n': strbuf_addstr(&new_path, "\\n"); break;
  287. case '\r': strbuf_addstr(&new_path, "\\r"); break;
  288. default:
  289. strbuf_addch(&new_path, *path);
  290. }
  291. path++;
  292. }
  293. return new_path.buf;
  294. }
  295. /* FIXME: move prefix to startup_info struct and get rid of this arg */
  296. void trace_repo_setup(const char *prefix)
  297. {
  298. const char *git_work_tree;
  299. char *cwd;
  300. if (!trace_want(&trace_setup_key))
  301. return;
  302. cwd = xgetcwd();
  303. if (!(git_work_tree = get_git_work_tree()))
  304. git_work_tree = "(null)";
  305. if (!prefix)
  306. prefix = "(null)";
  307. trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
  308. trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
  309. trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
  310. trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
  311. trace_printf_key(&trace_setup_key, "setup: prefix: %s\n", quote_crnl(prefix));
  312. free(cwd);
  313. }
  314. int trace_want(struct trace_key *key)
  315. {
  316. return !!get_trace_fd(key);
  317. }
  318. #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_CLOCK_MONOTONIC)
  319. static inline uint64_t highres_nanos(void)
  320. {
  321. struct timespec ts;
  322. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  323. return 0;
  324. return (uint64_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
  325. }
  326. #elif defined (GIT_WINDOWS_NATIVE)
  327. static inline uint64_t highres_nanos(void)
  328. {
  329. static uint64_t high_ns, scaled_low_ns;
  330. static int scale;
  331. LARGE_INTEGER cnt;
  332. if (!scale) {
  333. if (!QueryPerformanceFrequency(&cnt))
  334. return 0;
  335. /* high_ns = number of ns per cnt.HighPart */
  336. high_ns = (1000000000LL << 32) / (uint64_t) cnt.QuadPart;
  337. /*
  338. * Number of ns per cnt.LowPart is 10^9 / frequency (or
  339. * high_ns >> 32). For maximum precision, we scale this factor
  340. * so that it just fits within 32 bit (i.e. won't overflow if
  341. * multiplied with cnt.LowPart).
  342. */
  343. scaled_low_ns = high_ns;
  344. scale = 32;
  345. while (scaled_low_ns >= 0x100000000LL) {
  346. scaled_low_ns >>= 1;
  347. scale--;
  348. }
  349. }
  350. /* if QPF worked on initialization, we expect QPC to work as well */
  351. QueryPerformanceCounter(&cnt);
  352. return (high_ns * cnt.HighPart) +
  353. ((scaled_low_ns * cnt.LowPart) >> scale);
  354. }
  355. #else
  356. # define highres_nanos() 0
  357. #endif
  358. static inline uint64_t gettimeofday_nanos(void)
  359. {
  360. struct timeval tv;
  361. gettimeofday(&tv, NULL);
  362. return (uint64_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
  363. }
  364. /*
  365. * Returns nanoseconds since the epoch (01/01/1970), for performance tracing
  366. * (i.e. favoring high precision over wall clock time accuracy).
  367. */
  368. uint64_t getnanotime(void)
  369. {
  370. static uint64_t offset;
  371. if (offset > 1) {
  372. /* initialization succeeded, return offset + high res time */
  373. return offset + highres_nanos();
  374. } else if (offset == 1) {
  375. /* initialization failed, fall back to gettimeofday */
  376. return gettimeofday_nanos();
  377. } else {
  378. /* initialize offset if high resolution timer works */
  379. uint64_t now = gettimeofday_nanos();
  380. uint64_t highres = highres_nanos();
  381. if (highres)
  382. offset = now - highres;
  383. else
  384. offset = 1;
  385. return now;
  386. }
  387. }
  388. static struct strbuf command_line = STRBUF_INIT;
  389. static void print_command_performance_atexit(void)
  390. {
  391. trace_performance_leave("git command:%s", command_line.buf);
  392. }
  393. void trace_command_performance(const char **argv)
  394. {
  395. if (!trace_want(&trace_perf_key))
  396. return;
  397. if (!command_line.len)
  398. atexit(print_command_performance_atexit);
  399. strbuf_reset(&command_line);
  400. sq_quote_argv_pretty(&command_line, argv);
  401. trace_performance_enter();
  402. }