Merge branch 'en/header-split-cache-h-part-2'
[alt-git.git] / t / helper / test-trace2.c
blob20c7495f38593550a8a4dd35a957cc62cf3a2ded
1 #include "test-tool.h"
2 #include "strvec.h"
3 #include "run-command.h"
4 #include "exec-cmd.h"
5 #include "config.h"
6 #include "repository.h"
7 #include "trace2.h"
9 typedef int(fn_unit_test)(int argc, const char **argv);
11 struct unit_test {
12 fn_unit_test *ut_fn;
13 const char *ut_name;
14 const char *ut_usage;
17 #define MyOk 0
18 #define MyError 1
20 static int get_i(int *p_value, const char *data)
22 char *endptr;
24 if (!data || !*data)
25 return MyError;
27 *p_value = strtol(data, &endptr, 10);
28 if (*endptr || errno == ERANGE)
29 return MyError;
31 return MyOk;
35 * Cause process to exit with the requested value via "return".
37 * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit()
38 * with our result.
40 * Test harness can confirm:
41 * [] the process-exit value.
42 * [] the "code" field in the "exit" trace2 event.
43 * [] the "code" field in the "atexit" trace2 event.
44 * [] the "name" field in the "cmd_name" trace2 event.
45 * [] "def_param" events for all of the "interesting" pre-defined
46 * config settings.
48 static int ut_001return(int argc, const char **argv)
50 int rc;
52 if (get_i(&rc, argv[0]))
53 die("expect <exit_code>");
55 return rc;
59 * Cause the process to exit with the requested value via "exit()".
61 * Test harness can confirm:
62 * [] the "code" field in the "exit" trace2 event.
63 * [] the "code" field in the "atexit" trace2 event.
64 * [] the "name" field in the "cmd_name" trace2 event.
65 * [] "def_param" events for all of the "interesting" pre-defined
66 * config settings.
68 static int ut_002exit(int argc, const char **argv)
70 int rc;
72 if (get_i(&rc, argv[0]))
73 die("expect <exit_code>");
75 exit(rc);
79 * Send an "error" event with each value in argv. Normally, git only issues
80 * a single "error" event immediately before issuing an "exit" event (such
81 * as in die() or BUG()), but multiple "error" events are allowed.
83 * Test harness can confirm:
84 * [] a trace2 "error" event for each value in argv.
85 * [] the "name" field in the "cmd_name" trace2 event.
86 * [] (optional) the file:line in the "exit" event refers to this function.
88 static int ut_003error(int argc, const char **argv)
90 int k;
92 if (!argv[0] || !*argv[0])
93 die("expect <error_message>");
95 for (k = 0; k < argc; k++)
96 error("%s", argv[k]);
98 return 0;
102 * Run a child process and wait for it to finish and exit with its return code.
103 * test-tool trace2 004child [<child-command-line>]
105 * For example:
106 * test-tool trace2 004child git version
107 * test-tool trace2 004child test-tool trace2 001return 0
108 * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child
109 * test-tool trace2 004child git -c alias.xyz=version xyz
111 * Test harness can confirm:
112 * [] the "name" field in the "cmd_name" trace2 event.
113 * [] that the outer process has a single component SID (or depth "d0" in
114 * the PERF stream).
115 * [] that "child_start" and "child_exit" events are generated for the child.
116 * [] if the child process is an instrumented executable:
117 * [] that "version", "start", ..., "exit", and "atexit" events are
118 * generated by the child process.
119 * [] that the child process events have a multiple component SID (or
120 * depth "dN+1" in the PERF stream).
121 * [] that the child exit code is propagated to the parent process "exit"
122 * and "atexit" events..
123 * [] (optional) that the "t_abs" field in the child process "atexit" event
124 * is less than the "t_rel" field in the "child_exit" event of the parent
125 * process.
126 * [] if the child process is like the alias example above,
127 * [] (optional) the child process attempts to run "git-xyx" as a dashed
128 * command.
129 * [] the child process emits an "alias" event with "xyz" => "version"
130 * [] the child process runs "git version" as a child process.
131 * [] the child process has a 3 component SID (or depth "d2" in the PERF
132 * stream).
134 static int ut_004child(int argc, const char **argv)
136 struct child_process cmd = CHILD_PROCESS_INIT;
137 int result;
140 * Allow empty <child_command_line> so we can do arbitrarily deep
141 * command nesting and let the last one be null.
143 if (!argc)
144 return 0;
146 strvec_pushv(&cmd.args, argv);
147 result = run_command(&cmd);
148 exit(result);
152 * Exec a git command. This may either create a child process (Windows)
153 * or replace the existing process.
154 * test-tool trace2 005exec <git_command_args>
156 * For example:
157 * test-tool trace2 005exec version
159 * Test harness can confirm (on Windows):
160 * [] the "name" field in the "cmd_name" trace2 event.
161 * [] that the outer process has a single component SID (or depth "d0" in
162 * the PERF stream).
163 * [] that "exec" and "exec_result" events are generated for the child
164 * process (since the Windows compatibility layer fakes an exec() with
165 * a CreateProcess(), WaitForSingleObject(), and exit()).
166 * [] that the child process has multiple component SID (or depth "dN+1"
167 * in the PERF stream).
169 * Test harness can confirm (on platforms with a real exec() function):
170 * [] TODO talk about process replacement and how it affects SID.
172 static int ut_005exec(int argc, const char **argv)
174 int result;
176 if (!argc)
177 return 0;
179 result = execv_git_cmd(argv);
180 return result;
183 static int ut_006data(int argc, const char **argv)
185 const char *usage_error =
186 "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]";
188 if (argc % 3 != 0)
189 die("%s", usage_error);
191 while (argc) {
192 if (!argv[0] || !*argv[0] || !argv[1] || !*argv[1] ||
193 !argv[2] || !*argv[2])
194 die("%s", usage_error);
196 trace2_data_string(argv[0], the_repository, argv[1], argv[2]);
197 argv += 3;
198 argc -= 3;
201 return 0;
204 static int ut_007BUG(int argc, const char **argv)
207 * Exercise BUG() to ensure that the message is printed to trace2.
209 BUG("the bug message");
212 static int ut_008bug(int argc UNUSED, const char **argv UNUSED)
214 bug("a bug message");
215 bug("another bug message");
216 BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required");
217 return 0;
220 static int ut_009bug_BUG(int argc UNUSED, const char **argv UNUSED)
222 bug("a bug message");
223 bug("another bug message");
224 /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */
225 return 0;
228 static int ut_010bug_BUG(int argc UNUSED, const char **argv UNUSED)
230 bug("a %s message", "bug");
231 BUG("a %s message", "BUG");
235 * Single-threaded timer test. Create several intervals using the
236 * TEST1 timer. The test script can verify that an aggregate Trace2
237 * "timer" event is emitted indicating that we started+stopped the
238 * timer the requested number of times.
240 static int ut_100timer(int argc, const char **argv)
242 const char *usage_error =
243 "expect <count> <ms_delay>";
245 int count = 0;
246 int delay = 0;
247 int k;
249 if (argc != 2)
250 die("%s", usage_error);
251 if (get_i(&count, argv[0]))
252 die("%s", usage_error);
253 if (get_i(&delay, argv[1]))
254 die("%s", usage_error);
256 for (k = 0; k < count; k++) {
257 trace2_timer_start(TRACE2_TIMER_ID_TEST1);
258 sleep_millisec(delay);
259 trace2_timer_stop(TRACE2_TIMER_ID_TEST1);
262 return 0;
265 struct ut_101_data {
266 int count;
267 int delay;
270 static void *ut_101timer_thread_proc(void *_ut_101_data)
272 struct ut_101_data *data = _ut_101_data;
273 int k;
275 trace2_thread_start("ut_101");
277 for (k = 0; k < data->count; k++) {
278 trace2_timer_start(TRACE2_TIMER_ID_TEST2);
279 sleep_millisec(data->delay);
280 trace2_timer_stop(TRACE2_TIMER_ID_TEST2);
283 trace2_thread_exit();
284 return NULL;
288 * Multi-threaded timer test. Create several threads that each create
289 * several intervals using the TEST2 timer. The test script can verify
290 * that an individual Trace2 "th_timer" events for each thread and an
291 * aggregate "timer" event are generated.
293 static int ut_101timer(int argc, const char **argv)
295 const char *usage_error =
296 "expect <count> <ms_delay> <threads>";
298 struct ut_101_data data = { 0, 0 };
299 int nr_threads = 0;
300 int k;
301 pthread_t *pids = NULL;
303 if (argc != 3)
304 die("%s", usage_error);
305 if (get_i(&data.count, argv[0]))
306 die("%s", usage_error);
307 if (get_i(&data.delay, argv[1]))
308 die("%s", usage_error);
309 if (get_i(&nr_threads, argv[2]))
310 die("%s", usage_error);
312 CALLOC_ARRAY(pids, nr_threads);
314 for (k = 0; k < nr_threads; k++) {
315 if (pthread_create(&pids[k], NULL, ut_101timer_thread_proc, &data))
316 die("failed to create thread[%d]", k);
319 for (k = 0; k < nr_threads; k++) {
320 if (pthread_join(pids[k], NULL))
321 die("failed to join thread[%d]", k);
324 free(pids);
326 return 0;
330 * Single-threaded counter test. Add several values to the TEST1 counter.
331 * The test script can verify that the final sum is reported in the "counter"
332 * event.
334 static int ut_200counter(int argc, const char **argv)
336 const char *usage_error =
337 "expect <v1> [<v2> [...]]";
338 int value;
339 int k;
341 if (argc < 1)
342 die("%s", usage_error);
344 for (k = 0; k < argc; k++) {
345 if (get_i(&value, argv[k]))
346 die("invalid value[%s] -- %s",
347 argv[k], usage_error);
348 trace2_counter_add(TRACE2_COUNTER_ID_TEST1, value);
351 return 0;
355 * Multi-threaded counter test. Create seveal threads that each increment
356 * the TEST2 global counter. The test script can verify that an individual
357 * "th_counter" event is generated with a partial sum for each thread and
358 * that a final aggregate "counter" event is generated.
361 struct ut_201_data {
362 int v1;
363 int v2;
366 static void *ut_201counter_thread_proc(void *_ut_201_data)
368 struct ut_201_data *data = _ut_201_data;
370 trace2_thread_start("ut_201");
372 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v1);
373 trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v2);
375 trace2_thread_exit();
376 return NULL;
379 static int ut_201counter(int argc, const char **argv)
381 const char *usage_error =
382 "expect <v1> <v2> <threads>";
384 struct ut_201_data data = { 0, 0 };
385 int nr_threads = 0;
386 int k;
387 pthread_t *pids = NULL;
389 if (argc != 3)
390 die("%s", usage_error);
391 if (get_i(&data.v1, argv[0]))
392 die("%s", usage_error);
393 if (get_i(&data.v2, argv[1]))
394 die("%s", usage_error);
395 if (get_i(&nr_threads, argv[2]))
396 die("%s", usage_error);
398 CALLOC_ARRAY(pids, nr_threads);
400 for (k = 0; k < nr_threads; k++) {
401 if (pthread_create(&pids[k], NULL, ut_201counter_thread_proc, &data))
402 die("failed to create thread[%d]", k);
405 for (k = 0; k < nr_threads; k++) {
406 if (pthread_join(pids[k], NULL))
407 die("failed to join thread[%d]", k);
410 free(pids);
412 return 0;
416 * Usage:
417 * test-tool trace2 <ut_name_1> <ut_usage_1>
418 * test-tool trace2 <ut_name_2> <ut_usage_2>
419 * ...
421 #define USAGE_PREFIX "test-tool trace2"
423 /* clang-format off */
424 static struct unit_test ut_table[] = {
425 { ut_001return, "001return", "<exit_code>" },
426 { ut_002exit, "002exit", "<exit_code>" },
427 { ut_003error, "003error", "<error_message>+" },
428 { ut_004child, "004child", "[<child_command_line>]" },
429 { ut_005exec, "005exec", "<git_command_args>" },
430 { ut_006data, "006data", "[<category> <key> <value>]+" },
431 { ut_007BUG, "007bug", "" },
432 { ut_008bug, "008bug", "" },
433 { ut_009bug_BUG, "009bug_BUG","" },
434 { ut_010bug_BUG, "010bug_BUG","" },
436 { ut_100timer, "100timer", "<count> <ms_delay>" },
437 { ut_101timer, "101timer", "<count> <ms_delay> <threads>" },
439 { ut_200counter, "200counter", "<v1> [<v2> [<v3> [...]]]" },
440 { ut_201counter, "201counter", "<v1> <v2> <threads>" },
442 /* clang-format on */
444 /* clang-format off */
445 #define for_each_ut(k, ut_k) \
446 for (k = 0, ut_k = &ut_table[k]; \
447 k < ARRAY_SIZE(ut_table); \
448 k++, ut_k = &ut_table[k])
449 /* clang-format on */
451 static int print_usage(void)
453 int k;
454 struct unit_test *ut_k;
456 fprintf(stderr, "usage:\n");
457 for_each_ut (k, ut_k)
458 fprintf(stderr, "\t%s %s %s\n", USAGE_PREFIX, ut_k->ut_name,
459 ut_k->ut_usage);
461 return 129;
465 * Issue various trace2 events for testing.
467 * We assume that these trace2 routines has already been called:
468 * [] trace2_initialize() [common-main.c:main()]
469 * [] trace2_cmd_start() [common-main.c:main()]
470 * [] trace2_cmd_name() [test-tool.c:cmd_main()]
471 * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()]
472 * So that:
473 * [] the various trace2 streams are open.
474 * [] the process SID has been created.
475 * [] the "version" event has been generated.
476 * [] the "start" event has been generated.
477 * [] the "cmd_name" event has been generated.
478 * [] this writes various "def_param" events for interesting config values.
480 * We return from here and let test-tool.c::cmd_main() pass the exit
481 * code to common-main.c::main(), which will use it to call
482 * trace2_cmd_exit().
484 int cmd__trace2(int argc, const char **argv)
486 int k;
487 struct unit_test *ut_k;
489 argc--; /* skip over "trace2" arg */
490 argv++;
492 if (argc)
493 for_each_ut (k, ut_k)
494 if (!strcmp(argv[0], ut_k->ut_name))
495 return ut_k->ut_fn(argc - 1, argv + 1);
497 return print_usage();