Added unit tests for these APIs: tracefs_marker_init() tracefs_marker_print() tracefs_marker_vprint() tracefs_marker_write() tracefs_marker_close() Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- utest/tracefs-utest.c | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c index ed2693b..bad4348 100644 --- a/utest/tracefs-utest.c +++ b/utest/tracefs-utest.c @@ -169,6 +169,95 @@ static const char *get_rand_str(void) return str; } +struct marker_find { + int len; + int count; + void *data; +}; + +static int test_marker_callback(struct tep_event *event, struct tep_record *record, + int cpu, void *context) +{ + struct marker_find *walk = context; + struct tep_format_field *field; + + if (!walk) + return -1; + if (record->size < walk->len) + return 0; + + field = tep_find_field(event, "buf"); + if (field && memcmp(walk->data, record->data + field->offset, walk->len) == 0) + walk->count++; + + return 0; +} + +static bool find_test_marker(struct tracefs_instance *instance, void *data, int len, int expected) +{ + int ret; + struct marker_find walk; + + walk.data = data; + walk.len = len; + walk.count = 0; + ret = tracefs_iterate_raw_events(test_tep, instance, NULL, 0, + test_marker_callback, &walk); + CU_TEST(ret == 0); + + return walk.count == expected; +} + +static int marker_vprint(struct tracefs_instance *instance, char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = tracefs_marker_vprint(instance, fmt, ap); + va_end(ap); + + return ret; +} + +#define MARKERS_WRITE_COUNT 100 +static void test_instance_ftrace_marker(struct tracefs_instance *instance) +{ + const char *string = get_rand_str(); + unsigned int data = 0xdeadbeef; + char *str; + int i; + + CU_TEST(tracefs_marker_init(instance) == 0); + tracefs_marker_close(instance); + + for (i = 0; i < MARKERS_WRITE_COUNT; i++) { + CU_TEST(tracefs_marker_write(instance, &data, sizeof(data)) == 0); + } + CU_TEST(find_test_marker(instance, &data, sizeof(data), MARKERS_WRITE_COUNT)); + + for (i = 0; i < MARKERS_WRITE_COUNT; i++) { + CU_TEST(tracefs_marker_print(instance, "Test marker: %s 0x%X", string, data) == 0); + } + asprintf(&str, "Test marker: %s 0x%X", string, data); + CU_TEST(find_test_marker(instance, str, strlen(str) + 1, MARKERS_WRITE_COUNT)); + free(str); + + for (i = 0; i < MARKERS_WRITE_COUNT; i++) { + CU_TEST(marker_vprint(instance, "Test marker V: %s 0x%X", string, data) == 0); + } + asprintf(&str, "Test marker V: %s 0x%X", string, data); + CU_TEST(find_test_marker(instance, str, strlen(str) + 1, MARKERS_WRITE_COUNT)); + free(str); + + tracefs_marker_close(instance); +} + +static void test_ftrace_marker(void) +{ + test_instance_ftrace_marker(test_instance); +} + static void test_trace_file(void) { const char *tmp = get_rand_str(); @@ -1075,4 +1164,6 @@ void test_tracefs_lib(void) test_tracing_options); CU_add_test(suite, "custom system directory", test_custom_trace_dir); + CU_add_test(suite, "ftrace marker", + test_ftrace_marker); } -- 2.30.2