Add a helper function to compare file contents
[git.git] / t / helper / test-trace2.c
blobf374c21ec32ecc049bace6821d5630e103b8937c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "strvec.h"
4 #include "run-command.h"
5 #include "exec-cmd.h"
6 #include "config.h"
8 typedef int(fn_unit_test)(int argc, const char **argv);
10 struct unit_test {
11 fn_unit_test *ut_fn;
12 const char *ut_name;
13 const char *ut_usage;
16 #define MyOk 0
17 #define MyError 1
19 static int get_i(int *p_value, const char *data)
21 char *endptr;
23 if (!data || !*data)
24 return MyError;
26 *p_value = strtol(data, &endptr, 10);
27 if (*endptr || errno == ERANGE)
28 return MyError;
30 return MyOk;
34 * Cause process to exit with the requested value via "return".
36 * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit()
37 * with our result.
39 * Test harness can confirm:
40 * [] the process-exit value.
41 * [] the "code" field in the "exit" trace2 event.
42 * [] the "code" field in the "atexit" trace2 event.
43 * [] the "name" field in the "cmd_name" trace2 event.
44 * [] "def_param" events for all of the "interesting" pre-defined
45 * config settings.
47 static int ut_001return(int argc, const char **argv)
49 int rc;
51 if (get_i(&rc, argv[0]))
52 die("expect <exit_code>");
54 return rc;
58 * Cause the process to exit with the requested value via "exit()".
60 * Test harness can confirm:
61 * [] the "code" field in the "exit" trace2 event.
62 * [] the "code" field in the "atexit" trace2 event.
63 * [] the "name" field in the "cmd_name" trace2 event.
64 * [] "def_param" events for all of the "interesting" pre-defined
65 * config settings.
67 static int ut_002exit(int argc, const char **argv)
69 int rc;
71 if (get_i(&rc, argv[0]))
72 die("expect <exit_code>");
74 exit(rc);
78 * Send an "error" event with each value in argv. Normally, git only issues
79 * a single "error" event immediately before issuing an "exit" event (such
80 * as in die() or BUG()), but multiple "error" events are allowed.
82 * Test harness can confirm:
83 * [] a trace2 "error" event for each value in argv.
84 * [] the "name" field in the "cmd_name" trace2 event.
85 * [] (optional) the file:line in the "exit" event refers to this function.
87 static int ut_003error(int argc, const char **argv)
89 int k;
91 if (!argv[0] || !*argv[0])
92 die("expect <error_message>");
94 for (k = 0; k < argc; k++)
95 error("%s", argv[k]);
97 return 0;
101 * Run a child process and wait for it to finish and exit with its return code.
102 * test-tool trace2 004child [<child-command-line>]
104 * For example:
105 * test-tool trace2 004child git version
106 * test-tool trace2 004child test-tool trace2 001return 0
107 * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child
108 * test-tool trace2 004child git -c alias.xyz=version xyz
110 * Test harness can confirm:
111 * [] the "name" field in the "cmd_name" trace2 event.
112 * [] that the outer process has a single component SID (or depth "d0" in
113 * the PERF stream).
114 * [] that "child_start" and "child_exit" events are generated for the child.
115 * [] if the child process is an instrumented executable:
116 * [] that "version", "start", ..., "exit", and "atexit" events are
117 * generated by the child process.
118 * [] that the child process events have a multiple component SID (or
119 * depth "dN+1" in the PERF stream).
120 * [] that the child exit code is propagated to the parent process "exit"
121 * and "atexit" events..
122 * [] (optional) that the "t_abs" field in the child process "atexit" event
123 * is less than the "t_rel" field in the "child_exit" event of the parent
124 * process.
125 * [] if the child process is like the alias example above,
126 * [] (optional) the child process attempts to run "git-xyx" as a dashed
127 * command.
128 * [] the child process emits an "alias" event with "xyz" => "version"
129 * [] the child process runs "git version" as a child process.
130 * [] the child process has a 3 component SID (or depth "d2" in the PERF
131 * stream).
133 static int ut_004child(int argc, const char **argv)
135 struct child_process cmd = CHILD_PROCESS_INIT;
136 int result;
139 * Allow empty <child_command_line> so we can do arbitrarily deep
140 * command nesting and let the last one be null.
142 if (!argc)
143 return 0;
145 strvec_pushv(&cmd.args, argv);
146 result = run_command(&cmd);
147 exit(result);
151 * Exec a git command. This may either create a child process (Windows)
152 * or replace the existing process.
153 * test-tool trace2 005exec <git_command_args>
155 * For example:
156 * test-tool trace2 005exec version
158 * Test harness can confirm (on Windows):
159 * [] the "name" field in the "cmd_name" trace2 event.
160 * [] that the outer process has a single component SID (or depth "d0" in
161 * the PERF stream).
162 * [] that "exec" and "exec_result" events are generated for the child
163 * process (since the Windows compatibility layer fakes an exec() with
164 * a CreateProcess(), WaitForSingleObject(), and exit()).
165 * [] that the child process has multiple component SID (or depth "dN+1"
166 * in the PERF stream).
168 * Test harness can confirm (on platforms with a real exec() function):
169 * [] TODO talk about process replacement and how it affects SID.
171 static int ut_005exec(int argc, const char **argv)
173 int result;
175 if (!argc)
176 return 0;
178 result = execv_git_cmd(argv);
179 return result;
182 static int ut_006data(int argc, const char **argv)
184 const char *usage_error =
185 "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]";
187 if (argc % 3 != 0)
188 die("%s", usage_error);
190 while (argc) {
191 if (!argv[0] || !*argv[0] || !argv[1] || !*argv[1] ||
192 !argv[2] || !*argv[2])
193 die("%s", usage_error);
195 trace2_data_string(argv[0], the_repository, argv[1], argv[2]);
196 argv += 3;
197 argc -= 3;
200 return 0;
203 static int ut_007BUG(int argc, const char **argv)
206 * Exercise BUG() to ensure that the message is printed to trace2.
208 BUG("the bug message");
211 static int ut_008bug(int argc, const char **argv)
213 bug("a bug message");
214 bug("another bug message");
215 BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required");
216 return 0;
219 static int ut_009bug_BUG(int argc, const char **argv)
221 bug("a bug message");
222 bug("another bug message");
223 /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */
224 return 0;
227 static int ut_010bug_BUG(int argc, const char **argv)
229 bug("a %s message", "bug");
230 BUG("a %s message", "BUG");
234 * Single-threaded timer test. Create several intervals using the
235 * TEST1 timer. The test script can verify that an aggregate Trace2
236 * "timer" event is emitted indicating that we started+stopped the
237 * timer the requested number of times.
239 static int ut_100timer(int argc, const char **argv)
241 const char *usage_error =
242 "expect <count> <ms_delay>";
244 int count = 0;
245 int delay = 0;
246 int k;
248 if (argc != 2)
249 die("%s", usage_error);
250 if (get_i(&count, argv[0]))
251 die("%s", usage_error);
252 if (get_i(&delay, argv[1]))
253 die("%s", usage_error);
255 for (k = 0; k < count; k++) {
256 trace2_timer_start(TRACE2_TIMER_ID_TEST1);
257 sleep_millisec(delay);
258 trace2_timer_stop(TRACE2_TIMER_ID_TEST1);
261 return 0;
264 struct ut_101_data {
265 int count;
266 int delay;
269 static void *ut_101timer_thread_proc(void *_ut_101_data)
271 struct ut_101_data *data = _ut_101_data;
272 int k;
274 trace2_thread_start("ut_101");
276 for (k = 0; k < data->count; k++) {
277 trace2_timer_start(TRACE2_TIMER_ID_TEST2);
278 sleep_millisec(data->delay);
279 trace2_timer_stop(TRACE2_TIMER_ID_TEST2);
282 trace2_thread_exit();
283 return NULL;
287 * Multi-threaded timer test. Create several threads that each create
288 * several intervals using the TEST2 timer. The test script can verify
289 * that an individual Trace2 "th_timer" events for each thread and an
290 * aggregate "timer" event are generated.
292 static int ut_101timer(int argc, const char **argv)
294 const char *usage_error =
295 "expect <count> <ms_delay> <threads>";
297 struct ut_101_data data = { 0, 0 };
298 int nr_threads = 0;
299 int k;
300 pthread_t *pids = NULL;
302 if (argc != 3)
303 die("%s", usage_error);
304 if (get_i(&data.count, argv[0]))
305 die("%s", usage_error);
306 if (get_i(&data.delay, argv[1]))
307 die("%s", usage_error);
308 if (get_i(&nr_threads, argv[2]))
309 die("%s", usage_error);
311 CALLOC_ARRAY(pids, nr_threads);
313 for (k = 0; k < nr_threads; k++) {
314 if (pthread_create(&pids[k], NULL, ut_101timer_thread_proc, &data))
315 die("failed to create thread[%d]", k);
318 for (k = 0; k < nr_threads; k++) {
319 if (pthread_join(pids[k], NULL))
320 die("failed to join thread[%d]", k);
323 free(pids);
325 return 0;
329 * Single-threaded counter test. Add several values to the TEST1 counter.
330 * The test script can verify that the final sum is reported in the "counter"
331 * event.
333 static int ut_200counter(int argc, const char **argv)
335 const char *usage_error =
336 "expect <v1> [<v2> [...]]";
337 int value;
338 int k;
340 if (argc < 1)
341 die("%s", usage_error);
343 for (k = 0; k < argc; k++) {
344 if (get_i(&value, argv[k]))
345 die("invalid value[%s] -- %s",
346 argv[k], usage_error);
347 trace2_counter_add(TRACE2_COUNTER_ID_TEST1, value);
350 return 0;
354 * Multi-threaded counter test. Create seveal threads that each increment
355 * the TEST2 global counter. The test script can verify that an individual
356 * "th_counter" event is generated with a partial sum for each thread and
357 * that a final aggregate "counter" event is generated.
360 struct ut_201_data {
361 int v1;
362 int v2;
365 static void *ut_201counter_thread_proc(void *_ut_201_data)
367 struct ut_201_data *data = _ut_201_data;
369 trace2_thread_start("ut_201");
371 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v1);
372 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v2);
374 trace2_thread_exit();
375 return NULL;
378 static int ut_201counter(int argc, const char **argv)
380 const char *usage_error =
381 "expect <v1> <v2> <threads>";
383 struct ut_201_data data = { 0, 0 };
384 int nr_threads = 0;
385 int k;
386 pthread_t *pids = NULL;
388 if (argc != 3)
389 die("%s", usage_error);
390 if (get_i(&data.v1, argv[0]))
391 die("%s", usage_error);
392 if (get_i(&data.v2, argv[1]))
393 die("%s", usage_error);
394 if (get_i(&nr_threads, argv[2]))
395 die("%s", usage_error);
397 CALLOC_ARRAY(pids, nr_threads);
399 for (k = 0; k < nr_threads; k++) {
400 if (pthread_create(&pids[k], NULL, ut_201counter_thread_proc, &data))
401 die("failed to create thread[%d]", k);
404 for (k = 0; k < nr_threads; k++) {
405 if (pthread_join(pids[k], NULL))
406 die("failed to join thread[%d]", k);
409 free(pids);
411 return 0;
415 * Usage:
416 * test-tool trace2 <ut_name_1> <ut_usage_1>
417 * test-tool trace2 <ut_name_2> <ut_usage_2>
418 * ...
420 #define USAGE_PREFIX "test-tool trace2"
422 /* clang-format off */
423 static struct unit_test ut_table[] = {
424 { ut_001return, "001return", "<exit_code>" },
425 { ut_002exit, "002exit", "<exit_code>" },
426 { ut_003error, "003error", "<error_message>+" },
427 { ut_004child, "004child", "[<child_command_line>]" },
428 { ut_005exec, "005exec", "<git_command_args>" },
429 { ut_006data, "006data", "[<category> <key> <value>]+" },
430 { ut_007BUG, "007bug", "" },
431 { ut_008bug, "008bug", "" },
432 { ut_009bug_BUG, "009bug_BUG","" },
433 { ut_010bug_BUG, "010bug_BUG","" },
435 { ut_100timer, "100timer", "<count> <ms_delay>" },
436 { ut_101timer, "101timer", "<count> <ms_delay> <threads>" },
438 { ut_200counter, "200counter", "<v1> [<v2> [<v3> [...]]]" },
439 { ut_201counter, "201counter", "<v1> <v2> <threads>" },
441 /* clang-format on */
443 /* clang-format off */
444 #define for_each_ut(k, ut_k) \
445 for (k = 0, ut_k = &ut_table[k]; \
446 k < ARRAY_SIZE(ut_table); \
447 k++, ut_k = &ut_table[k])
448 /* clang-format on */
450 static int print_usage(void)
452 int k;
453 struct unit_test *ut_k;
455 fprintf(stderr, "usage:\n");
456 for_each_ut (k, ut_k)
457 fprintf(stderr, "\t%s %s %s\n", USAGE_PREFIX, ut_k->ut_name,
458 ut_k->ut_usage);
460 return 129;
464 * Issue various trace2 events for testing.
466 * We assume that these trace2 routines has already been called:
467 * [] trace2_initialize() [common-main.c:main()]
468 * [] trace2_cmd_start() [common-main.c:main()]
469 * [] trace2_cmd_name() [test-tool.c:cmd_main()]
470 * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()]
471 * So that:
472 * [] the various trace2 streams are open.
473 * [] the process SID has been created.
474 * [] the "version" event has been generated.
475 * [] the "start" event has been generated.
476 * [] the "cmd_name" event has been generated.
477 * [] this writes various "def_param" events for interesting config values.
479 * We return from here and let test-tool.c::cmd_main() pass the exit
480 * code to common-main.c::main(), which will use it to call
481 * trace2_cmd_exit().
483 int cmd__trace2(int argc, const char **argv)
485 int k;
486 struct unit_test *ut_k;
488 argc--; /* skip over "trace2" arg */
489 argv++;
491 if (argc)
492 for_each_ut (k, ut_k)
493 if (!strcmp(argv[0], ut_k->ut_name))
494 return ut_k->ut_fn(argc - 1, argv + 1);
496 return print_usage();