trace2: create new combined trace facility
[git.git] / trace2 / tr2_sid.c
blob984524a43ce44536e1dc825995ee4e0aa80734fb
1 #include "cache.h"
2 #include "trace2/tr2_sid.h"
4 #define TR2_ENVVAR_PARENT_SID "GIT_TR2_PARENT_SID"
6 static struct strbuf tr2sid_buf = STRBUF_INIT;
7 static int tr2sid_nr_git_parents;
9 /*
10 * Compute a "unique" session id (SID) for the current process. This allows
11 * all events from this process to have a single label (much like a PID).
13 * Export this into our environment so that all child processes inherit it.
15 * If we were started by another git instance, use our parent's SID as a
16 * prefix. (This lets us track parent/child relationships even if there
17 * is an intermediate shell process.)
19 * Additionally, count the number of nested git processes.
21 static void tr2_sid_compute(void)
23 uint64_t us_now;
24 const char *parent_sid;
26 if (tr2sid_buf.len)
27 return;
29 parent_sid = getenv(TR2_ENVVAR_PARENT_SID);
30 if (parent_sid && *parent_sid) {
31 const char *p;
32 for (p = parent_sid; *p; p++)
33 if (*p == '/')
34 tr2sid_nr_git_parents++;
36 strbuf_addstr(&tr2sid_buf, parent_sid);
37 strbuf_addch(&tr2sid_buf, '/');
38 tr2sid_nr_git_parents++;
41 us_now = getnanotime() / 1000;
42 strbuf_addf(&tr2sid_buf, "%" PRIuMAX "-%" PRIdMAX, (uintmax_t)us_now,
43 (intmax_t)getpid());
45 setenv(TR2_ENVVAR_PARENT_SID, tr2sid_buf.buf, 1);
48 const char *tr2_sid_get(void)
50 if (!tr2sid_buf.len)
51 tr2_sid_compute();
53 return tr2sid_buf.buf;
56 int tr2_sid_depth(void)
58 if (!tr2sid_buf.len)
59 tr2_sid_compute();
61 return tr2sid_nr_git_parents;
64 void tr2_sid_release(void)
66 strbuf_release(&tr2sid_buf);