Linux-Trace-Devel Archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Subject: [PATCH] trace-cmd utests: Add more tests to test the iterating
Date: Thu, 11 Jan 2024 17:25:30 -0500	[thread overview]
Message-ID: <20240111172530.5a54859e@gandalf.local.home> (raw)

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add more tests to test tracecmd_iterate_events() and
tracecmd_iterate_events_reverse(), as well as stopping and restarting.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 utest/tracecmd-utest.c | 110 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 110 insertions(+)

diff --git a/utest/tracecmd-utest.c b/utest/tracecmd-utest.c
index d67678e36554..5dce3e94df6a 100644
--- a/utest/tracecmd-utest.c
+++ b/utest/tracecmd-utest.c
@@ -461,6 +461,15 @@ static int read_events(struct tracecmd_input *handle, struct tep_record *record,
 	return 0;
 }
 
+static int read_events_10(struct tracecmd_input *handle, struct tep_record *record,
+			  int cpu, void *data)
+{
+	struct callback_data *cd = data;
+
+	read_events(handle, record, cpu, data);
+	return  cd->counter < 10 ? 0 : 1;
+}
+
 static void test_trace_library_read(void)
 {
 	struct tracecmd_input *handle;
@@ -488,6 +497,103 @@ static void test_trace_library_read(void)
 	trace_seq_destroy(&data.seq);
 }
 
+static void test_trace_library_read_inc(void)
+{
+	struct tracecmd_input *handle;
+	struct callback_data data;
+	struct stat st;
+	long save_count;
+	long total = 0;
+	int ret;
+
+	data.counter = 0;
+	trace_seq_init(&data.seq);
+
+	/* If the trace data is already created, just use it, otherwise make it again */
+	if (stat(TRACECMD_FILE, &st) < 0) {
+		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
+		CU_TEST(ret == 0);
+	}
+
+	/* First read all again */
+	handle = tracecmd_open(TRACECMD_FILE, 0);
+	CU_TEST(handle != NULL);
+	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
+	CU_TEST(ret == 0);
+	CU_TEST(data.counter > 0);
+
+	/* Save the counter */
+	save_count = data.counter;
+
+	tracecmd_iterate_reset(handle);
+
+	/* Read 10 at a time */
+	do {
+		data.counter = 0;
+		ret = tracecmd_iterate_events(handle, NULL, 0, read_events_10, &data);
+		CU_TEST(ret >= 0);
+		CU_TEST(data.counter <= 10);
+		total += data.counter;
+	} while (data.counter);
+	CU_TEST(ret == 0);
+
+	CU_TEST(total == save_count);
+
+	trace_seq_destroy(&data.seq);
+	tracecmd_close(handle);
+}
+
+static void test_trace_library_read_back(void)
+{
+	struct tracecmd_input *handle;
+	struct callback_data data;
+	struct stat st;
+	long save_count;
+	int ret;
+
+	data.counter = 0;
+	trace_seq_init(&data.seq);
+
+	/* If the trace data is already created, just use it, otherwise make it again */
+	if (stat(TRACECMD_FILE, &st) < 0) {
+		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
+		CU_TEST(ret == 0);
+	}
+
+	/* First read all again */
+	handle = tracecmd_open(TRACECMD_FILE, 0);
+	CU_TEST(handle != NULL);
+	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
+	CU_TEST(ret == 0);
+	CU_TEST(data.counter > 0);
+
+	/* Save the counter */
+	save_count = data.counter;
+
+	tracecmd_iterate_reset(handle);
+
+	/* Read backwards */
+	data.counter = 0;
+	ret = tracecmd_iterate_events_reverse(handle, NULL, 0, read_events, &data, false);
+	CU_TEST(ret == 0);
+	CU_TEST(data.counter == save_count);
+
+	/* Read forward again */
+	data.counter = 0;
+	ret = tracecmd_iterate_events(handle, NULL, 0, read_events, &data);
+	CU_TEST(ret == 0);
+	CU_TEST(data.counter == save_count);
+
+	/* Read backwards from where we left off */
+	data.counter = 0;
+	ret = tracecmd_iterate_events_reverse(handle, NULL, 0, read_events, &data, true);
+	CU_TEST(ret == 0);
+	CU_TEST(data.counter == save_count);
+
+	trace_seq_destroy(&data.seq);
+	tracecmd_close(handle);
+}
+
 static int test_suite_destroy(void)
 {
 	unlink(TRACECMD_FILE);
@@ -540,6 +646,10 @@ void test_tracecmd_lib(void)
 		    test_trace_convert6);
 	CU_add_test(suite, "Use libraries to read file",
 		    test_trace_library_read);
+	CU_add_test(suite, "Use libraries to read file incremental",
+		    test_trace_library_read_inc);
+	CU_add_test(suite, "Use libraries to read file backwards",
+		    test_trace_library_read_back);
 	CU_add_test(suite, "Test max length",
 		    test_trace_record_max);
 }
-- 
2.43.0


                 reply	other threads:[~2024-01-11 22:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240111172530.5a54859e@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.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 read-only IMAP folder(s) and NNTP newsgroup(s).