Merge branch 'jh/trace2-sid-fix'
authorJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:31 +0000 (23:50 +0900)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 May 2019 14:50:31 +0000 (23:50 +0900)
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()

1  2 
Documentation/technical/api-trace2.txt
Makefile
config.c
t/t0210-trace2-normal.sh
trace2.c
trace2.h
trace2/tr2_dst.c
trace2/tr2_tgt_event.c
trace2/tr2_tgt_normal.c
trace2/tr2_tgt_perf.c
index d0948ba250933ed7e5c3f06bdf70cb7a1e6e5814,8b6a5e6d4b98564a337544a12230c5921343ad60..9e585b8e79fbc70434fb9964159d9ee18757f82c
@@@ -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
  
- `<absolute-pathname>`::
+ ------------
+ $ 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:[<socket_type>:]<absolute-pathname>`::
+ 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 Makefile
Simple merge
diff --cc config.c
Simple merge
Simple merge
diff --cc trace2.c
Simple merge
diff --cc trace2.h
Simple merge
index c3d82ca6a4bd852ed175c0800f31ce08bfb32ea3,7d96f33420b389a10a6256f6d95a46a76f1765f3..5dda0ca1cdb5d09a8cdfb27dd45d4436f3e8aa6f
@@@ -1,23 -1,14 +1,13 @@@
  #include "cache.h"
  #include "trace2/tr2_dst.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_sid.h"
+ #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;
  }
  
-                               dst->env_var_name, strerror(errno));
 +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,
++                              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);
Simple merge
Simple merge
Simple merge