From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH v2 2/2] libtracefs: Unit test for trace marker APIs
Date: Fri, 9 Apr 2021 07:27:46 +0300 [thread overview]
Message-ID: <20210409042746.3179319-3-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20210409042746.3179319-1-tz.stoyanov@gmail.com>
Added unit tests for thee APIs:
tracefs_print_init();
tracefs_printf();
tracefs_vprintf();
tracefs_print_close();
tracefs_binary_init();
tracefs_binary_write();
tracefs_binary_close();
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
utest/tracefs-utest.c | 116 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 116 insertions(+)
diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index f9441af..3f5c832 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -192,6 +192,120 @@ static const char *get_rand_str(void)
return str;
}
+struct marker_find {
+ int data_offset;
+ int event_id;
+ int count;
+ int len;
+ void *data;
+};
+
+static int test_marker_callback(struct tep_event *event, struct tep_record *record,
+ int cpu, void *context)
+{
+ struct marker_find *walk = context;
+
+ if (!walk)
+ return -1;
+ if (event->id != walk->event_id)
+ return 0;
+ if (record->size < (walk->data_offset + walk->len))
+ return 0;
+
+ if (memcmp(walk->data, record->data + walk->data_offset, walk->len) == 0)
+ walk->count++;
+
+ return 0;
+}
+
+static bool find_test_marker(struct tracefs_instance *instance,
+ void *data, int len, int expected, bool raw)
+{
+ struct tep_format_field *field;
+ struct tep_event *event;
+ struct marker_find walk;
+ int ret;
+
+ if (raw) {
+ event = tep_find_event_by_name(test_tep, "ftrace", "raw_data");
+ if (event)
+ field = tep_find_field(event, "id");
+
+ } else {
+ event = tep_find_event_by_name(test_tep, "ftrace", "print");
+ if (event)
+ field = tep_find_field(event, "buf");
+ }
+
+ if (!event || !field)
+ return false;
+
+ walk.data = data;
+ walk.len = len;
+ walk.count = 0;
+ walk.event_id = event->id;
+ walk.data_offset = field->offset;
+ 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_vprintf(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_print_init(instance) == 0);
+ tracefs_print_close(instance);
+
+ CU_TEST(tracefs_binary_init(instance) == 0);
+ tracefs_binary_close(instance);
+
+ for (i = 0; i < MARKERS_WRITE_COUNT; i++) {
+ CU_TEST(tracefs_binary_write(instance, &data, sizeof(data)) == 0);
+ }
+ CU_TEST(find_test_marker(instance, &data, sizeof(data), MARKERS_WRITE_COUNT, true));
+
+ for (i = 0; i < MARKERS_WRITE_COUNT; i++) {
+ CU_TEST(tracefs_printf(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, false));
+ 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, false));
+ free(str);
+
+ tracefs_print_close(instance);
+ tracefs_binary_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();
@@ -1098,4 +1212,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
prev parent reply other threads:[~2021-04-09 4:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-09 4:27 [PATCH v2 0/2] Documentation and unit tests for new trace marker APIs Tzvetomir Stoyanov (VMware)
2021-04-09 4:27 ` [PATCH v2 1/2] libtracefs: Document " Tzvetomir Stoyanov (VMware)
2021-04-09 4:27 ` Tzvetomir Stoyanov (VMware) [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210409042746.3179319-3-tz.stoyanov@gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).