From: Junio C Hamano Date: Mon, 13 May 2019 14:50:31 +0000 (+0900) Subject: Merge branch 'jh/trace2-sid-fix' X-Git-Tag: v2.22.0-rc0~13 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/5b2d1c0c6eceb5fd6c9527bc2863179644dce840?ds=inline;hp=--cc Merge branch 'jh/trace2-sid-fix' Polishing of the new trace2 facility continues. The system-level configuration can specify site-wide trace2 settings, which can be overridden with per-user configuration and environment variables. * jh/trace2-sid-fix: trace2: fixup access problem on /etc/gitconfig in read_very_early_config trace2: update docs to describe system/global config settings trace2: make SIDs more unique trace2: clarify UTC datetime formatting trace2: report peak memory usage of the process trace2: use system/global config for default trace2 settings config: add read_very_early_config() trace2: find exec-dir before trace2 initialization trace2: add absolute elapsed time to start event trace2: refactor setting process starting time config: initialize opts structure in repo_read_config() --- 5b2d1c0c6eceb5fd6c9527bc2863179644dce840 diff --cc Documentation/technical/api-trace2.txt index d0948ba250,8b6a5e6d4b..9e585b8e79 --- a/Documentation/technical/api-trace2.txt +++ b/Documentation/technical/api-trace2.txt @@@ -75,53 -115,33 +115,38 @@@ $ export GIT_TR2_EVENT=~/log.even $ git version git version 2.20.1.155.g426c96fcdb ------------ - + - ------------ - $ cat ~/log.event - {"event":"version","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16 17:28:42.620713","file":"common-main.c","line":38,"evt":"1","exe":"2.20.1.155.g426c96fcdb"} - {"event":"start","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16 17:28:42.621027","file":"common-main.c","line":39,"argv":["git","version"]} - {"event":"cmd_name","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16 17:28:42.621122","file":"git.c","line":432,"name":"version","hierarchy":"version"} - {"event":"exit","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16 17:28:42.621236","file":"git.c","line":662,"t_abs":0.001227,"code":0} - {"event":"atexit","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16 17:28:42.621268","file":"trace2/tr2_tgt_event.c","line":163,"t_abs":0.001265,"code":0} - ------------ - - == Enabling a Target - A Trace2 Target is enabled when the corresponding environment variable - (`GIT_TR2`, `GIT_TR2_PERF`, or `GIT_TR2_EVENT`) is set. The following - values are recognized. + or - `0`:: - `false`:: - - Disables the target. - - `1`:: - `true`:: - - Enables the target and writes stream to `STDERR`. - - `[2-9]`:: + ------------ + $ git config --global trace2.eventTarget ~/log.event + $ git version + git version 2.20.1.155.g426c96fcdb + ------------ - Enables the target and writes to the already opened file descriptor. + yields - ``:: + ------------ + $ cat ~/log.event + {"event":"version","sid":"sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.620713Z","file":"common-main.c","line":38,"evt":"1","exe":"2.20.1.155.g426c96fcdb"} + {"event":"start","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621027Z","file":"common-main.c","line":39,"t_abs":0.001173,"argv":["git","version"]} + {"event":"cmd_name","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621122Z","file":"git.c","line":432,"name":"version","hierarchy":"version"} + {"event":"exit","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621236Z","file":"git.c","line":662,"t_abs":0.001227,"code":0} + {"event":"atexit","sid":"20190408T191610.507018Z-H9b68c35f-P000059a8","thread":"main","time":"2019-01-16T17:28:42.621268Z","file":"trace2/tr2_tgt_event.c","line":163,"t_abs":0.001265,"code":0} + ------------ - Enables the target, opens and writes to the file in append mode. + === Enabling a Target - If the target already exists and is a directory, the traces will be - written to files (one per process) underneath the given directory. They - will be named according to the last component of the SID (optionally - followed by a counter to avoid filename collisions). + To enable a target, set the corresponding environment variable or + system or global config value to one of the following: - `af_unix:[:]`:: + include::../trace2-target-values.txt[] - Enables the target, opens and writes to a Unix Domain Socket - (on platforms that support them). - + - Socket type can be either `stream` or `dgram`. If the socket type is - omitted, Git will try both. ++If the target already exists and is a directory, the traces will be ++written to files (one per process) underneath the given directory. They ++will be named according to the last component of the SID (optionally ++followed by a counter to avoid filename collisions). + == Trace2 API All public Trace2 functions and macros are defined in `trace2.h` and diff --cc trace2/tr2_dst.c index c3d82ca6a4,7d96f33420..5dda0ca1cd --- a/trace2/tr2_dst.c +++ b/trace2/tr2_dst.c @@@ -1,23 -1,14 +1,13 @@@ #include "cache.h" #include "trace2/tr2_dst.h" +#include "trace2/tr2_sid.h" - - /* - * If a Trace2 target cannot be opened for writing, we should issue a - * warning to stderr, but this is very annoying if the target is a pipe - * or socket and beyond the user's control -- especially since every - * git command (and sub-command) will print the message. So we silently - * eat these warnings and just discard the trace data. - * - * Enable the following environment variable to see these warnings. - */ - #define TR2_ENVVAR_DST_DEBUG "GIT_TR2_DST_DEBUG" + #include "trace2/tr2_sysenv.h" /* - * If a Trace2 target cannot be opened for writing, we should issue a - * warning to stderr, but this is very annoying if the target is a pipe - * or socket and beyond the user's control -- especially since every - * git command (and sub-command) will print the message. So we silently - * eat these warnings and just discard the trace data. + * How many attempts we will make at creating an automatically-named trace file. */ +#define MAX_AUTO_ATTEMPTS 10 + static int tr2_dst_want_warning(void) { static int tr2env_dst_debug = -1; @@@ -42,55 -33,6 +32,56 @@@ void tr2_dst_trace_disable(struct tr2_d dst->need_close = 0; } +static int tr2_dst_try_auto_path(struct tr2_dst *dst, const char *tgt_prefix) +{ + int fd; + const char *last_slash, *sid = tr2_sid_get(); + struct strbuf path = STRBUF_INIT; + size_t base_path_len; + unsigned attempt_count; + + last_slash = strrchr(sid, '/'); + if (last_slash) + sid = last_slash + 1; + + strbuf_addstr(&path, tgt_prefix); + if (!is_dir_sep(path.buf[path.len - 1])) + strbuf_addch(&path, '/'); + strbuf_addstr(&path, sid); + base_path_len = path.len; + + for (attempt_count = 0; attempt_count < MAX_AUTO_ATTEMPTS; attempt_count++) { + if (attempt_count > 0) { + strbuf_setlen(&path, base_path_len); + strbuf_addf(&path, ".%d", attempt_count); + } + + fd = open(path.buf, O_WRONLY | O_CREAT | O_EXCL, 0666); + if (fd != -1) + break; + } + + if (fd == -1) { + if (tr2_dst_want_warning()) + warning("trace2: could not open '%.*s' for '%s' tracing: %s", + (int) base_path_len, path.buf, - dst->env_var_name, strerror(errno)); ++ tr2_sysenv_display_name(dst->sysenv_var), ++ strerror(errno)); + + tr2_dst_trace_disable(dst); + strbuf_release(&path); + return 0; + } + + strbuf_release(&path); + + dst->fd = fd; + dst->need_close = 1; + dst->initialized = 1; + + return dst->fd; +} + static int tr2_dst_try_path(struct tr2_dst *dst, const char *tgt_value) { int fd = open(tgt_value, O_WRONLY | O_APPEND | O_CREAT, 0666);