4 #include "run-command.h"
8 typedef int(fn_unit_test
)(int argc
, const char **argv
);
19 static int get_i(int *p_value
, const char *data
)
26 *p_value
= strtol(data
, &endptr
, 10);
27 if (*endptr
|| errno
== ERANGE
)
34 * Cause process to exit with the requested value via "return".
36 * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit()
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
47 static int ut_001return(int argc
, const char **argv
)
51 if (get_i(&rc
, argv
[0]))
52 die("expect <exit_code>");
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
67 static int ut_002exit(int argc
, const char **argv
)
71 if (get_i(&rc
, argv
[0]))
72 die("expect <exit_code>");
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
)
91 if (!argv
[0] || !*argv
[0])
92 die("expect <error_message>");
94 for (k
= 0; k
< argc
; k
++)
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>]
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
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
125 * [] if the child process is like the alias example above,
126 * [] (optional) the child process attempts to run "git-xyx" as a dashed
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
133 static int ut_004child(int argc
, const char **argv
)
138 * Allow empty <child_command_line> so we can do arbitrarily deep
139 * command nesting and let the last one be null.
144 result
= run_command_v_opt(argv
, 0);
149 * Exec a git command. This may either create a child process (Windows)
150 * or replace the existing process.
151 * test-tool trace2 005exec <git_command_args>
154 * test-tool trace2 005exec version
156 * Test harness can confirm (on Windows):
157 * [] the "name" field in the "cmd_name" trace2 event.
158 * [] that the outer process has a single component SID (or depth "d0" in
160 * [] that "exec" and "exec_result" events are generated for the child
161 * process (since the Windows compatibility layer fakes an exec() with
162 * a CreateProcess(), WaitForSingleObject(), and exit()).
163 * [] that the child process has multiple component SID (or depth "dN+1"
164 * in the PERF stream).
166 * Test harness can confirm (on platforms with a real exec() function):
167 * [] TODO talk about process replacement and how it affects SID.
169 static int ut_005exec(int argc
, const char **argv
)
176 result
= execv_git_cmd(argv
);
180 static int ut_006data(int argc
, const char **argv
)
182 const char *usage_error
=
183 "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]";
186 die("%s", usage_error
);
189 if (!argv
[0] || !*argv
[0] || !argv
[1] || !*argv
[1] ||
190 !argv
[2] || !*argv
[2])
191 die("%s", usage_error
);
193 trace2_data_string(argv
[0], the_repository
, argv
[1], argv
[2]);
201 static int ut_007BUG(int argc
, const char **argv
)
204 * Exercise BUG() to ensure that the message is printed to trace2.
206 BUG("the bug message");
209 static int ut_008bug(int argc
, const char **argv
)
211 bug("a bug message");
212 bug("another bug message");
213 BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required");
217 static int ut_009bug_BUG(int argc
, const char **argv
)
219 bug("a bug message");
220 bug("another bug message");
221 /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */
225 static int ut_010bug_BUG(int argc
, const char **argv
)
227 bug("a %s message", "bug");
228 BUG("a %s message", "BUG");
232 * Single-threaded timer test. Create several intervals using the
233 * TEST1 timer. The test script can verify that an aggregate Trace2
234 * "timer" event is emitted indicating that we started+stopped the
235 * timer the requested number of times.
237 static int ut_100timer(int argc
, const char **argv
)
239 const char *usage_error
=
240 "expect <count> <ms_delay>";
247 die("%s", usage_error
);
248 if (get_i(&count
, argv
[0]))
249 die("%s", usage_error
);
250 if (get_i(&delay
, argv
[1]))
251 die("%s", usage_error
);
253 for (k
= 0; k
< count
; k
++) {
254 trace2_timer_start(TRACE2_TIMER_ID_TEST1
);
255 sleep_millisec(delay
);
256 trace2_timer_stop(TRACE2_TIMER_ID_TEST1
);
267 static void *ut_101timer_thread_proc(void *_ut_101_data
)
269 struct ut_101_data
*data
= _ut_101_data
;
272 trace2_thread_start("ut_101");
274 for (k
= 0; k
< data
->count
; k
++) {
275 trace2_timer_start(TRACE2_TIMER_ID_TEST2
);
276 sleep_millisec(data
->delay
);
277 trace2_timer_stop(TRACE2_TIMER_ID_TEST2
);
280 trace2_thread_exit();
285 * Multi-threaded timer test. Create several threads that each create
286 * several intervals using the TEST2 timer. The test script can verify
287 * that an individual Trace2 "th_timer" events for each thread and an
288 * aggregate "timer" event are generated.
290 static int ut_101timer(int argc
, const char **argv
)
292 const char *usage_error
=
293 "expect <count> <ms_delay> <threads>";
295 struct ut_101_data data
= { 0, 0 };
298 pthread_t
*pids
= NULL
;
301 die("%s", usage_error
);
302 if (get_i(&data
.count
, argv
[0]))
303 die("%s", usage_error
);
304 if (get_i(&data
.delay
, argv
[1]))
305 die("%s", usage_error
);
306 if (get_i(&nr_threads
, argv
[2]))
307 die("%s", usage_error
);
309 CALLOC_ARRAY(pids
, nr_threads
);
311 for (k
= 0; k
< nr_threads
; k
++) {
312 if (pthread_create(&pids
[k
], NULL
, ut_101timer_thread_proc
, &data
))
313 die("failed to create thread[%d]", k
);
316 for (k
= 0; k
< nr_threads
; k
++) {
317 if (pthread_join(pids
[k
], NULL
))
318 die("failed to join thread[%d]", k
);
328 * test-tool trace2 <ut_name_1> <ut_usage_1>
329 * test-tool trace2 <ut_name_2> <ut_usage_2>
332 #define USAGE_PREFIX "test-tool trace2"
334 /* clang-format off */
335 static struct unit_test ut_table
[] = {
336 { ut_001return
, "001return", "<exit_code>" },
337 { ut_002exit
, "002exit", "<exit_code>" },
338 { ut_003error
, "003error", "<error_message>+" },
339 { ut_004child
, "004child", "[<child_command_line>]" },
340 { ut_005exec
, "005exec", "<git_command_args>" },
341 { ut_006data
, "006data", "[<category> <key> <value>]+" },
342 { ut_007BUG
, "007bug", "" },
343 { ut_008bug
, "008bug", "" },
344 { ut_009bug_BUG
, "009bug_BUG","" },
345 { ut_010bug_BUG
, "010bug_BUG","" },
347 { ut_100timer
, "100timer", "<count> <ms_delay>" },
348 { ut_101timer
, "101timer", "<count> <ms_delay> <threads>" },
350 /* clang-format on */
352 /* clang-format off */
353 #define for_each_ut(k, ut_k) \
354 for (k = 0, ut_k = &ut_table[k]; \
355 k < ARRAY_SIZE(ut_table); \
356 k++, ut_k = &ut_table[k])
357 /* clang-format on */
359 static int print_usage(void)
362 struct unit_test
*ut_k
;
364 fprintf(stderr
, "usage:\n");
365 for_each_ut (k
, ut_k
)
366 fprintf(stderr
, "\t%s %s %s\n", USAGE_PREFIX
, ut_k
->ut_name
,
373 * Issue various trace2 events for testing.
375 * We assume that these trace2 routines has already been called:
376 * [] trace2_initialize() [common-main.c:main()]
377 * [] trace2_cmd_start() [common-main.c:main()]
378 * [] trace2_cmd_name() [test-tool.c:cmd_main()]
379 * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()]
381 * [] the various trace2 streams are open.
382 * [] the process SID has been created.
383 * [] the "version" event has been generated.
384 * [] the "start" event has been generated.
385 * [] the "cmd_name" event has been generated.
386 * [] this writes various "def_param" events for interesting config values.
388 * We return from here and let test-tool.c::cmd_main() pass the exit
389 * code to common-main.c::main(), which will use it to call
392 int cmd__trace2(int argc
, const char **argv
)
395 struct unit_test
*ut_k
;
397 argc
--; /* skip over "trace2" arg */
401 for_each_ut (k
, ut_k
)
402 if (!strcmp(argv
[0], ut_k
->ut_name
))
403 return ut_k
->ut_fn(argc
- 1, argv
+ 1);
405 return print_usage();