Skip to content

Commit 08881b9

Browse files
jeffhostetlergitster
authored andcommitted
trace2: make SIDs more unique
Update SID component construction to use the current UTC datetime and a portion of the SHA1 of the hostname. Use an simplified date/time format to make it easier to use the SID component as a logfile filename. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bad229a commit 08881b9

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

Documentation/technical/api-trace2.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ git version 2.20.1.155.g426c96fcdb
7878
+
7979
------------
8080
$ cat ~/log.event
81-
{"event":"version","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16T17:28:42.620713Z","file":"common-main.c","line":38,"evt":"1","exe":"2.20.1.155.g426c96fcdb"}
82-
{"event":"start","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16T17:28:42.621027Z","file":"common-main.c","line":39,"t_abs":0.001173,"argv":["git","version"]}
83-
{"event":"cmd_name","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16T17:28:42.621122Z","file":"git.c","line":432,"name":"version","hierarchy":"version"}
84-
{"event":"exit","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16T17:28:42.621236Z","file":"git.c","line":662,"t_abs":0.001227,"code":0}
85-
{"event":"atexit","sid":"1547659722619736-11614","thread":"main","time":"2019-01-16T17:28:42.621268Z","file":"trace2/tr2_tgt_event.c","line":163,"t_abs":0.001265,"code":0}
81+
{"event":"version","sid":"sid":"20190408T191610.507018Z-H9b68c35f-P028492","thread":"main","time":"2019-01-16T17:28:42.620713Z","file":"common-main.c","line":38,"evt":"1","exe":"2.20.1.155.g426c96fcdb"}
82+
{"event":"start","sid":"20190408T191610.507018Z-H9b68c35f-P028492","thread":"main","time":"2019-01-16T17:28:42.621027Z","file":"common-main.c","line":39,"t_abs":0.001173,"argv":["git","version"]}
83+
{"event":"cmd_name","sid":"20190408T191610.507018Z-H9b68c35f-P028492","thread":"main","time":"2019-01-16T17:28:42.621122Z","file":"git.c","line":432,"name":"version","hierarchy":"version"}
84+
{"event":"exit","sid":"20190408T191610.507018Z-H9b68c35f-P028492","thread":"main","time":"2019-01-16T17:28:42.621236Z","file":"git.c","line":662,"t_abs":0.001227,"code":0}
85+
{"event":"atexit","sid":"20190408T191610.507018Z-H9b68c35f-P028492","thread":"main","time":"2019-01-16T17:28:42.621268Z","file":"trace2/tr2_tgt_event.c","line":163,"t_abs":0.001265,"code":0}
8686
------------
8787

8888
== Enabling a Target
@@ -540,11 +540,11 @@ The following key/value pairs are common to all events:
540540
------------
541541
{
542542
"event":"version",
543-
"sid":"1547659722619736-11614",
543+
"sid":"20190408T191827.272759Z-H9b68c35f-P011764",
544544
"thread":"main",
545-
"time":"2019-01-16T17:28:42.620713Z",
545+
"time":"2019-04-08T19:18:27.282761Z",
546546
"file":"common-main.c",
547-
"line":38,
547+
"line":42,
548548
...
549549
}
550550
------------

trace2/tr2_sid.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
11
#include "cache.h"
2+
#include "trace2/tr2_tbuf.h"
23
#include "trace2/tr2_sid.h"
34

45
#define TR2_ENVVAR_PARENT_SID "GIT_TR2_PARENT_SID"
56

67
static struct strbuf tr2sid_buf = STRBUF_INIT;
78
static int tr2sid_nr_git_parents;
89

10+
/*
11+
* Compute the final component of the SID representing the current process.
12+
* This should uniquely identify the process and be a valid filename (to
13+
* allow writing trace2 data to per-process files). It should also be fixed
14+
* length for possible use as a database key.
15+
*
16+
* "<yyyymmdd>T<hhmmss>.<fraction>Z-<host>-<process>"
17+
*
18+
* where <host> is a 9 character string:
19+
* "H<first_8_chars_of_sha1_of_hostname>"
20+
* "Localhost" when no hostname.
21+
*
22+
* where <process> is a 9 character string containing the least signifcant
23+
* 32 bits in the process-id.
24+
* "P<pid>"
25+
* (This is an abribrary choice. On most systems pid_t is a 32 bit value,
26+
* so limit doesn't matter. On larger systems, a truncated value is fine
27+
* for our purposes here.)
28+
*/
29+
static void tr2_sid_append_my_sid_component(void)
30+
{
31+
const struct git_hash_algo *algo = &hash_algos[GIT_HASH_SHA1];
32+
struct tr2_tbuf tb_now;
33+
git_hash_ctx ctx;
34+
pid_t pid = getpid();
35+
unsigned char hash[GIT_MAX_RAWSZ + 1];
36+
char hex[GIT_MAX_HEXSZ + 1];
37+
char hostname[HOST_NAME_MAX + 1];
38+
39+
tr2_tbuf_utc_datetime(&tb_now);
40+
strbuf_addstr(&tr2sid_buf, tb_now.buf);
41+
42+
strbuf_addch(&tr2sid_buf, '-');
43+
if (xgethostname(hostname, sizeof(hostname)))
44+
strbuf_add(&tr2sid_buf, "Localhost", 9);
45+
else {
46+
algo->init_fn(&ctx);
47+
algo->update_fn(&ctx, hostname, strlen(hostname));
48+
algo->final_fn(hash, &ctx);
49+
hash_to_hex_algop_r(hex, hash, algo);
50+
strbuf_addch(&tr2sid_buf, 'H');
51+
strbuf_add(&tr2sid_buf, hex, 8);
52+
}
53+
54+
strbuf_addf(&tr2sid_buf, "-P%08"PRIx32, (uint32_t)pid);
55+
}
56+
957
/*
1058
* Compute a "unique" session id (SID) for the current process. This allows
1159
* all events from this process to have a single label (much like a PID).
@@ -20,7 +68,6 @@ static int tr2sid_nr_git_parents;
2068
*/
2169
static void tr2_sid_compute(void)
2270
{
23-
uint64_t us_now;
2471
const char *parent_sid;
2572

2673
if (tr2sid_buf.len)
@@ -38,9 +85,7 @@ static void tr2_sid_compute(void)
3885
tr2sid_nr_git_parents++;
3986
}
4087

41-
us_now = getnanotime() / 1000;
42-
strbuf_addf(&tr2sid_buf, "%" PRIuMAX "-%" PRIdMAX, (uintmax_t)us_now,
43-
(intmax_t)getpid());
88+
tr2_sid_append_my_sid_component();
4489

4590
setenv(TR2_ENVVAR_PARENT_SID, tr2sid_buf.buf, 1);
4691
}

trace2/tr2_tbuf.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ void tr2_tbuf_utc_datetime_extended(struct tr2_tbuf *tb)
3030
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
3131
(long)tv.tv_usec);
3232
}
33+
34+
void tr2_tbuf_utc_datetime(struct tr2_tbuf *tb)
35+
{
36+
struct timeval tv;
37+
struct tm tm;
38+
time_t secs;
39+
40+
gettimeofday(&tv, NULL);
41+
secs = tv.tv_sec;
42+
gmtime_r(&secs, &tm);
43+
44+
xsnprintf(tb->buf, sizeof(tb->buf), "%4d%02d%02dT%02d%02d%02d.%06ldZ",
45+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
46+
tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
47+
}

trace2/tr2_tbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ void tr2_tbuf_local_time(struct tr2_tbuf *tb);
1919
* Fill buffer with formatted UTC datatime string.
2020
*/
2121
void tr2_tbuf_utc_datetime_extended(struct tr2_tbuf *tb);
22+
void tr2_tbuf_utc_datetime(struct tr2_tbuf *tb);
2223

2324
#endif /* TR2_TBUF_H */

0 commit comments

Comments
 (0)