Commit b23b112dfe8eceb39eaaea2d5e60d971c4371aa0

Russell Belfer 2014-04-29T11:29:49

Add payloads, bitmaps to trace API This is a proposed adjustment to the trace APIs. This makes the trace levels into a bitmask so that they can be selectively enabled and adds a callback-level payload, plus a message-level payload. This makes it easier for me to a GIT_TRACE_PERF callbacks that are simply bypassed if the PERF level is not set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
diff --git a/include/git2/trace.h b/include/git2/trace.h
index f9b4d6f..867b346 100644
--- a/include/git2/trace.h
+++ b/include/git2/trace.h
@@ -20,47 +20,64 @@
 GIT_BEGIN_DECL
 
 /**
- * Available tracing levels.  When tracing is set to a particular level,
- * callers will be provided tracing at the given level and all lower levels.
+ * Available tracing messages.  Each tracing level can be enabled
+ * independently or pass GIT_TRACE_ALL to enable all levels.
  */
 typedef enum {
 	/** No tracing will be performed. */
-	GIT_TRACE_NONE = 0,
+	GIT_TRACE_NONE = 0x0000u,
+
+	/** All tracing messages will be sent. */
+	GIT_TRACE_ALL = 0xFFFFu,
 
 	/** Severe errors that may impact the program's execution */
-	GIT_TRACE_FATAL = 1,
+	GIT_TRACE_FATAL = 0x0001u,
 
 	/** Errors that do not impact the program's execution */
-	GIT_TRACE_ERROR = 2,
+	GIT_TRACE_ERROR = 0x0002u,
+	GIT_TRACE_ERROR_AND_BELOW = 0x0003u,
 
 	/** Warnings that suggest abnormal data */
-	GIT_TRACE_WARN = 3,
+	GIT_TRACE_WARN = 0x0004u,
+	GIT_TRACE_WARN_AND_BELOW = 0x0007u,
 
 	/** Informational messages about program execution */
-	GIT_TRACE_INFO = 4,
+	GIT_TRACE_INFO = 0x0008u,
+	GIT_TRACE_INFO_AND_BELOW = 0x000Fu,
 
 	/** Detailed data that allows for debugging */
-	GIT_TRACE_DEBUG = 5,
+	GIT_TRACE_DEBUG = 0x0010u,
 
 	/** Exceptionally detailed debugging data */
-	GIT_TRACE_TRACE = 6
+	GIT_TRACE_TRACE = 0x0020u,
+
+	/** Performance tracking related traces */
+	GIT_TRACE_PERF = 0x0040u,
 } git_trace_level_t;
 
 /**
  * An instance for a tracing function
  */
-typedef void (*git_trace_callback)(git_trace_level_t level, const char *msg);
+typedef void (*git_trace_callback)(
+	git_trace_level_t level, /* just one bit will be sent */
+	void *cb_payload,
+	void *msg_payload,
+	const char *msg);
 
 /**
  * Sets the system tracing configuration to the specified level with the
  * specified callback.  When system events occur at a level equal to, or
  * lower than, the given level they will be reported to the given callback.
  *
- * @param level Level to set tracing to
- * @param cb Function to call with trace data
+ * @param level Bitmask of all enabled trace levels
+ * @param cb Function to call with trace messages
+ * @param cb_payload Payload to pass when callback is invoked
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_trace_set(git_trace_level_t level, git_trace_callback cb);
+GIT_EXTERN(int) git_trace_set(
+	git_trace_level_t level,
+	git_trace_callback cb,
+	void *cb_payload);
 
 /** @} */
 GIT_END_DECL
diff --git a/src/diff.c b/src/diff.c
index 8b7433c..5a6b127 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -555,7 +555,7 @@ int git_diff__oid_for_entry(
 	if (!entry.mode) {
 		struct stat st;
 
-		git_trace(GIT_TRACE_TRACE, "stat=1");
+		git_trace(GIT_TRACE_PERF, NULL, "stat");
 		if (p_stat(full_path.ptr, &st) < 0) {
 			error = git_path_set_error(errno, entry.path, "stat");
 			git_buf_free(&full_path);
@@ -570,7 +570,7 @@ int git_diff__oid_for_entry(
 	if (S_ISGITLINK(entry.mode)) {
 		git_submodule *sm;
 
-		git_trace(GIT_TRACE_TRACE, "submodule_lookup=1");
+		git_trace(GIT_TRACE_PERF, NULL, "submodule_lookup");
 		if (!git_submodule_lookup(&sm, diff->repo, entry.path)) {
 			const git_oid *sm_oid = git_submodule_wd_id(sm);
 			if (sm_oid)
@@ -583,7 +583,7 @@ int git_diff__oid_for_entry(
 			giterr_clear();
 		}
 	} else if (S_ISLNK(entry.mode)) {
-		git_trace(GIT_TRACE_TRACE, "oid_calculation=1");
+		git_trace(GIT_TRACE_PERF, NULL, "oid_calculation");
 		error = git_odb__hashlink(out, full_path.ptr);
 	} else if (!git__is_sizet(entry.file_size)) {
 		giterr_set(GITERR_OS, "File size overflow (for 32-bits) on '%s'",
@@ -596,7 +596,7 @@ int git_diff__oid_for_entry(
 		if (fd < 0)
 			error = fd;
 		else {
-			git_trace(GIT_TRACE_TRACE, "oid_calculation=1");
+			git_trace(GIT_TRACE_PERF, NULL, "oid_calculation");
 			error = git_odb__hashfd_filtered(
 				out, fd, (size_t)entry.file_size, GIT_OBJ_BLOB, fl);
 			p_close(fd);
@@ -655,7 +655,7 @@ static int maybe_modified_submodule(
 		ign == GIT_SUBMODULE_IGNORE_ALL)
 		return 0;
 
-	git_trace(GIT_TRACE_TRACE, "submodule_lookup=1");
+	git_trace(GIT_TRACE_PERF, NULL, "submodule_lookup");
 
 	if ((error = git_submodule_lookup(
 			&sub, diff->repo, info->nitem->path)) < 0) {
@@ -965,7 +965,7 @@ static int handle_unmatched_new_item(
 		delta_type = GIT_DELTA_ADDED;
 
 	else if (nitem->mode == GIT_FILEMODE_COMMIT) {
-		git_trace(GIT_TRACE_TRACE, "submodule_lookup=1");
+		git_trace(GIT_TRACE_PERF, NULL, "submodule_lookup");
 
 		/* ignore things that are not actual submodules */
 		if (git_submodule_lookup(NULL, info->repo, nitem->path) != 0) {
diff --git a/src/iterator.c b/src/iterator.c
index bebdeba..0d7e591 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1018,7 +1018,7 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
 		return GIT_ENOTFOUND;
 	}
 
-	git_trace(GIT_TRACE_TRACE, "stat=%ld", (long)ff->entries.length);
+	git_trace(GIT_TRACE_PERF, &ff->entries.length, "stat");
 
 	fs_iterator__seek_frame_start(fi, ff);
 
@@ -1310,7 +1310,7 @@ static int workdir_iterator__enter_dir(fs_iterator *fi)
 		if (!S_ISDIR(entry->st.st_mode) || !strcmp(GIT_DIR, entry->path))
 			continue;
 
-		git_trace(GIT_TRACE_TRACE, "submodule_lookup=1");
+		git_trace(GIT_TRACE_PERF, entry->path, "submodule_lookup");
 		if (git_submodule__is_submodule(fi->base.repo, entry->path)) {
 			entry->st.st_mode = GIT_FILEMODE_COMMIT;
 			entry->path_len--;
diff --git a/src/trace.c b/src/trace.c
index ee5039f..6ee2cf2 100644
--- a/src/trace.c
+++ b/src/trace.c
@@ -17,22 +17,25 @@ struct git_trace_data git_trace__data = {0};
 
 #endif
 
-int git_trace_set(git_trace_level_t level, git_trace_callback callback)
+int git_trace_set(
+	git_trace_level_t level, git_trace_callback cb, void *cb_payload)
 {
 #ifdef GIT_TRACE
-	assert(level == 0 || callback != NULL);
+	assert(level == 0 || cb != NULL);
 
 	git_trace__data.level = level;
-	git_trace__data.callback = callback;
+	git_trace__data.callback = cb;
+	git_trace__data.callback_payload = cb_payload;
 	GIT_MEMORY_BARRIER;
 
 	return 0;
 #else
 	GIT_UNUSED(level);
-	GIT_UNUSED(callback);
+	GIT_UNUSED(cb);
+	GIT_UNUSED(cb_payload);
 
-	giterr_set(GITERR_INVALID,
-		"This version of libgit2 was not built with tracing.");
+	giterr_set(
+		GITERR_INVALID, "This version of libgit2 was not built with tracing.");
 	return -1;
 #endif
 }
diff --git a/src/trace.h b/src/trace.h
index 4d4e3bf..b35e380 100644
--- a/src/trace.h
+++ b/src/trace.h
@@ -15,13 +15,16 @@
 struct git_trace_data {
 	git_trace_level_t level;
 	git_trace_callback callback;
+	void *callback_payload;
 };
 
 extern struct git_trace_data git_trace__data;
 
 GIT_INLINE(void) git_trace__write_fmt(
 	git_trace_level_t level,
-	const char *fmt, ...)
+	void *message_payload,
+	const char *fmt,
+	...)
 {
 	git_trace_callback callback = git_trace__data.callback;
 	git_buf message = GIT_BUF_INIT;
@@ -31,18 +34,18 @@ GIT_INLINE(void) git_trace__write_fmt(
 	git_buf_vprintf(&message, fmt, ap);
 	va_end(ap);
 
-	callback(level, git_buf_cstr(&message));
+	callback(
+		level, git_trace__data.callback_payload, message_payload,
+		git_buf_cstr(&message));
 
 	git_buf_free(&message);
 }
 
 #define git_trace_level()		(git_trace__data.level)
-#define git_trace(l, ...)		{ \
-									if (git_trace__data.level >= l && \
-										git_trace__data.callback != NULL) { \
-										git_trace__write_fmt(l, __VA_ARGS__); \
-									} \
-								}
+#define git_trace(l, p, ...)	do { \
+	if ((git_trace__data.level & (l)) != 0 && git_trace__data.callback) { \
+		git_trace__write_fmt((l), (p), __VA_ARGS__); \
+	} } while (0)
 
 #else
 
diff --git a/tests/diff/diff_helpers.c b/tests/diff/diff_helpers.c
index 279cb20..5de9834 100644
--- a/tests/diff/diff_helpers.c
+++ b/tests/diff/diff_helpers.c
@@ -229,3 +229,22 @@ void diff_print_raw(FILE *fp, git_diff *diff)
 		git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
 			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
 }
+
+void diff_perf_track_stats(
+	git_trace_level_t level,
+	void *cb_payload,
+	void *msg_payload,
+	const char *msg)
+{
+	diff_perf *data = cb_payload;
+
+	if (!(level & GIT_TRACE_PERF))
+		return;
+
+	if (!strcmp("stat", msg))
+		data->stat_calls += msg_payload ? *((size_t *)msg_payload) : 1;
+	else if (!strcmp("submodule_lookup", msg))
+		data->submodule_lookups++;
+	else if (!strcmp("oid_calculation", msg))
+		data->oid_calcs++;
+}
diff --git a/tests/diff/diff_helpers.h b/tests/diff/diff_helpers.h
index bf21f4b..3ed5387 100644
--- a/tests/diff/diff_helpers.h
+++ b/tests/diff/diff_helpers.h
@@ -62,3 +62,17 @@ extern int diff_foreach_via_iterator(
 
 extern void diff_print(FILE *fp, git_diff *diff);
 extern void diff_print_raw(FILE *fp, git_diff *diff);
+
+#include "git2/trace.h"
+
+typedef struct {
+	size_t stat_calls;
+	size_t oid_calcs;
+	size_t submodule_lookups;
+} diff_perf;
+
+extern void diff_perf_track_stats(
+	git_trace_level_t level,
+	void *cb_payload,
+	void *msg_payload,
+	const char *msg);
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c
index 0fd41d3..952c902 100644
--- a/tests/diff/workdir.c
+++ b/tests/diff/workdir.c
@@ -1,40 +1,19 @@
 #include "clar_libgit2.h"
 #include "diff_helpers.h"
 #include "repository.h"
-#include <git2/trace.h>
 
 static git_repository *g_repo = NULL;
 
 #ifdef GIT_TRACE
-static struct {
-	size_t stat_calls;
-	size_t oid_calcs;
-	size_t submodule_lookups;
-} g_diff_perf;
-
-static void add_stats(git_trace_level_t level, const char *msg)
-{
-	const char *assign = strchr(msg, '=');
-
-	GIT_UNUSED(level);
-
-	if (!assign)
-		return;
-
-	if (!strncmp("stat", msg, (assign - msg)))
-		g_diff_perf.stat_calls += atoi(assign + 1);
-	else if (!strncmp("submodule_lookup", msg, (assign - msg)))
-		g_diff_perf.submodule_lookups += atoi(assign + 1);
-	else if (!strncmp("oid_calculation", msg, (assign - msg)))
-		g_diff_perf.oid_calcs += atoi(assign + 1);
-}
+static diff_perf g_diff_perf;
 #endif
 
 void test_diff_workdir__initialize(void)
 {
 #ifdef GIT_TRACE
 	memset(&g_diff_perf, 0, sizeof(g_diff_perf));
-	cl_git_pass(git_trace_set(GIT_TRACE_TRACE, add_stats));
+	cl_git_pass(git_trace_set(
+		GIT_TRACE_PERF, diff_perf_track_stats, &g_diff_perf));
 #endif
 }
 
@@ -42,7 +21,7 @@ void test_diff_workdir__cleanup(void)
 {
 	cl_git_sandbox_cleanup();
 #ifdef GIT_TRACE
-	cl_git_pass(git_trace_set(0, NULL));
+	cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL, NULL));
 #endif
 }
 
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index c1d6be9..06864ad 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -5,38 +5,18 @@
 #include "posix.h"
 #include "util.h"
 #include "path.h"
-#include <git2/trace.h>
+#include "../diff/diff_helpers.h"
 
 #ifdef GIT_TRACE
-static struct {
-	size_t stat_calls;
-	size_t oid_calcs;
-	size_t submodule_lookups;
-} g_diff_perf;
-
-static void add_stats(git_trace_level_t level, const char *msg)
-{
-	const char *assign = strchr(msg, '=');
-
-	GIT_UNUSED(level);
-
-	if (!assign)
-		return;
-
-	if (!strncmp("stat", msg, (assign - msg)))
-		g_diff_perf.stat_calls += atoi(assign + 1);
-	else if (!strncmp("submodule_lookup", msg, (assign - msg)))
-		g_diff_perf.submodule_lookups += atoi(assign + 1);
-	else if (!strncmp("oid_calculation", msg, (assign - msg)))
-		g_diff_perf.oid_calcs += atoi(assign + 1);
-}
+static diff_perf g_diff_perf;
 #endif
 
 void test_status_worktree__initialize(void)
 {
 #ifdef GIT_TRACE
 	memset(&g_diff_perf, 0, sizeof(g_diff_perf));
-	cl_git_pass(git_trace_set(GIT_TRACE_TRACE, add_stats));
+	cl_git_pass(git_trace_set(
+		GIT_TRACE_PERF, diff_perf_track_stats, &g_diff_perf));
 #endif
 }
 
@@ -50,7 +30,7 @@ void test_status_worktree__cleanup(void)
 {
 	cl_git_sandbox_cleanup();
 #ifdef GIT_TRACE
-	cl_git_pass(git_trace_set(0, NULL));
+	cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL, NULL));
 #endif
 }
 
@@ -956,7 +936,5 @@ void test_status_worktree__update_stat_cache_0(void)
 	cl_assert_equal_sz(13 + 3, g_diff_perf.stat_calls);
 	cl_assert_equal_sz(0, g_diff_perf.oid_calcs);
 	cl_assert_equal_sz(1, g_diff_perf.submodule_lookups);
-
-	memset(&g_diff_perf, 0, sizeof(g_diff_perf));
 #endif
 }
diff --git a/tests/trace/trace.c b/tests/trace/trace.c
index 87b3253..3285393 100644
--- a/tests/trace/trace.c
+++ b/tests/trace/trace.c
@@ -3,44 +3,49 @@
 
 static int written = 0;
 
-static void trace_callback(git_trace_level_t level, const char *message)
+static void trace_callback(
+	git_trace_level_t level,
+	void *cb_payload,
+	void *msg_payload,
+	const char *msg)
 {
-	GIT_UNUSED(level);
+	GIT_UNUSED(level); GIT_UNUSED(msg_payload);
 
-	cl_assert(strcmp(message, "Hello world!") == 0);
+	cl_assert(strcmp(msg, "Hello world!") == 0);
 
-	written = 1;
+	if (cb_payload)
+		*((int *)cb_payload) = 1;
 }
 
 void test_trace_trace__initialize(void)
 {
-	git_trace_set(GIT_TRACE_INFO, trace_callback);
+	git_trace_set(GIT_TRACE_INFO_AND_BELOW, trace_callback, &written);
 	written = 0;
 }
 
 void test_trace_trace__cleanup(void)
 {
-	git_trace_set(GIT_TRACE_NONE, NULL);
+	git_trace_set(GIT_TRACE_NONE, NULL, NULL);
 }
 
 void test_trace_trace__sets(void)
 {
 #ifdef GIT_TRACE
-	cl_assert(git_trace_level() == GIT_TRACE_INFO);
+	cl_assert(git_trace_level() == GIT_TRACE_INFO_AND_BELOW);
 #endif
 }
 
 void test_trace_trace__can_reset(void)
 {
 #ifdef GIT_TRACE
-	cl_assert(git_trace_level() == GIT_TRACE_INFO);
-	cl_git_pass(git_trace_set(GIT_TRACE_ERROR, trace_callback));
+	cl_assert(git_trace_level() == GIT_TRACE_INFO_AND_BELOW);
+	cl_git_pass(git_trace_set(GIT_TRACE_ERROR, trace_callback, &written));
 
 	cl_assert(written == 0);
-	git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
+	git_trace(GIT_TRACE_INFO, NULL, "Hello %s!", "world");
 	cl_assert(written == 0);
 
-	git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
+	git_trace(GIT_TRACE_ERROR, NULL, "Hello %s!", "world");
 	cl_assert(written == 1);
 #endif
 }
@@ -48,13 +53,13 @@ void test_trace_trace__can_reset(void)
 void test_trace_trace__can_unset(void)
 {
 #ifdef GIT_TRACE
-	cl_assert(git_trace_level() == GIT_TRACE_INFO);
-	cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL));
+	cl_assert(git_trace_level() == GIT_TRACE_INFO_AND_BELOW);
+	cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL, NULL));
 
 	cl_assert(git_trace_level() == GIT_TRACE_NONE);
 
 	cl_assert(written == 0);
-	git_trace(GIT_TRACE_FATAL, "Hello %s!", "world");
+	git_trace(GIT_TRACE_FATAL, NULL, "Hello %s!", "world");
 	cl_assert(written == 0);
 #endif
 }
@@ -63,7 +68,7 @@ void test_trace_trace__skips_higher_level(void)
 {
 #ifdef GIT_TRACE
 	cl_assert(written == 0);
-	git_trace(GIT_TRACE_DEBUG, "Hello %s!", "world");
+	git_trace(GIT_TRACE_DEBUG, NULL, "Hello %s!", "world");
 	cl_assert(written == 0);
 #endif
 }
@@ -72,7 +77,7 @@ void test_trace_trace__writes(void)
 {
 #ifdef GIT_TRACE
 	cl_assert(written == 0);
-	git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
+	git_trace(GIT_TRACE_INFO, NULL, "Hello %s!", "world");
 	cl_assert(written == 1);
 #endif
 }
@@ -81,7 +86,7 @@ void test_trace_trace__writes_lower_level(void)
 {
 #ifdef GIT_TRACE
 	cl_assert(written == 0);
-	git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
+	git_trace(GIT_TRACE_ERROR, NULL, "Hello %s!", "world");
 	cl_assert(written == 1);
 #endif
 }