Skip to contentSkip to conversation

trace-cmd utest: Add test for max buffer (record -m)

PATCH New Steven Rostedt 1 participant 5 messages Nov 15, 2022

lore
Share & tools
lore
#0 lore raw

cover trace-cmd utest: Add test for max buffer (record -m)

From: "Steven Rostedt (Google)" <[email protected]>

Add a unit test to test the trace-cmd recorcd -m <size> option where the per_cpu buffers are capped at a maximum size. There was a bug due to the splice code that the size was not being calculated correctly, and was becoming much larger than specified.

In doing this, some restructuring needs to be done to make the helper functions a bit more flexible, and some other clean ups.

Steven Rostedt (Google) (4):
trace-cmd: Make the unit test depend on the static library
trace-cmd: Only show unit test output when -v is supplied trace-cmd utest: Separate out the grep and pipes
trace-cmd: Add test for max size option of record

 utest/Makefile         |   2 +-
 utest/tracecmd-utest.c | 187 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 170 insertions(+), 19 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/4 trace-cmd: Make the unit test depend on the static library

From: "Steven Rostedt (Google)" <[email protected]>

Several times I've been confused that the unit test was not working after I had fixed some of the library code. The reason is that the unit test binary does not have a dependency on the static library. Even though the library is updated, the unit test will not reflect the changes because it was not rebuilt against the updated static library.

Add a dependency to the static library to the unit test binary.

---
 utest/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
utest/Makefile +1 -1
index d475bad5a0f9..337db9bf24a1 100644
@@ -23,7 +23,7 @@ $(bdir):
 $(OBJS): | $(bdir)
 $(DEPS): | $(bdir)
 
-$(bdir)/trace-utest: $(OBJS)
+$(bdir)/trace-utest: $(OBJS) $(LIBTRACECMD_STATIC)
 	$(Q)$(do_app_build)
 
 $(bdir)/%.o: %.c
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/4 trace-cmd: Only show unit test output when -v is supplied

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

The "Use libraries to read file" test shows the output of the reading of the trace.dat file. That should only happen if the '-v' option is supplied, otherwise, it should be muted.

---
 utest/tracecmd-utest.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
utest/tracecmd-utest.c +2 -1
index e45b3537365c..2ec3f7118690 100644
@@ -264,7 +264,8 @@ static int read_events(struct tracecmd_input *handle, struct tep_record *record,
 	tep_print_event(tep, seq, record, "%s-%d %s %s\n",
 			TEP_PRINT_COMM, TEP_PRINT_PID,
 			TEP_PRINT_NAME, TEP_PRINT_INFO);
-	trace_seq_do_printf(seq);
+	if (show_output)
+		trace_seq_do_printf(seq);
 	return 0;
 }
 
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/4 trace-cmd utest: Separate out the grep and pipes

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

To make the grep and pipe functions in the unit test more flexible, separate them out and instead of passing in the arguments and using va_list directly, pass in a void pointer and use as structure to pass in the arguments. This will allow for extending the grep and pipe functions to use different type of arguments as well as functionality.

---
 utest/tracecmd-utest.c | 112 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 95 insertions(+), 17 deletions(-)
utest/tracecmd-utest.c +95 -17
index 2ec3f7118690..88d7217762d2 100644
@@ -10,6 +10,7 @@
 #include <time.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <ctype.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/types.h>
@@ -111,9 +112,9 @@ static int run_trace(const char *cmd, ...)
 	return ret;
 }
 
-static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
+static int pipe_it(int *ofd, int *efd, int (*func)(void *),
+		   void *data)
 {
-	char **argv;
 	int obrass[2];
 	int ebrass[2];
 	pid_t pid;
@@ -130,9 +131,6 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
 		goto fail;
 
 	if (!pid) {
-		argv = get_args(cmd, ap);
-		if (!argv)
-			exit(-1);
 
 		close(obrass[0]);
 		close(STDOUT_FILENO);
@@ -144,7 +142,7 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
 		if (dup2(obrass[1], STDERR_FILENO) < 0)
 			exit(-1);
 
-		ret = execvp(tracecmd_exec, argv);
+		ret = func(data);
 		exit(ret);
 	}
 
@@ -165,26 +163,52 @@ static int pipe_it(int *ofd, int *efd, const char *cmd, va_list ap)
 	return -1;
 }
 
-static int grep_it(const char *match, const char *cmd, ...)
+struct do_grep {
+	const char		*cmd;
+	va_list			*ap;
+};
+
+static int do_grep(void *data)
+{
+	struct do_grep *gdata = data;
+	char **argv;
+	int ret;
+
+	argv = get_args(gdata->cmd, *gdata->ap);
+	if (!argv)
+		exit(-1);
+
+	ret = execvp(tracecmd_exec, argv);
+	tracefs_list_free(argv);
+	return ret;
+}
+
+struct do_grep_it {
+	const char		*match;
+	const char		*cmd;
+	va_list			*ap;
+};
+
+static int do_grep_it(void *data)
 {
+	struct do_grep_it *dgdata = data;
+	struct do_grep gdata;
 	FILE *fp;
 	regex_t reg;
-	va_list ap;
 	char *buf = NULL;
 	ssize_t n;
 	size_t l = 0;
-	bool found = false;
 	int ofd;
 	int efd;
 	int pid;
 	int ret;
 
-	if (regcomp(&reg, match, REG_ICASE|REG_NOSUB))
+	if (regcomp(&reg, dgdata->match, REG_ICASE|REG_NOSUB))
 		return -1;
 
-	va_start(ap, cmd);
-	pid = pipe_it(&ofd, &efd, cmd, ap);
-	va_end(ap);
+	gdata.cmd = dgdata->cmd;
+	gdata.ap = dgdata->ap;
+	pid = pipe_it(&ofd, &efd, do_grep, &gdata);
 
 	if (pid < 0) {
 		regfree(&reg);
@@ -197,10 +221,65 @@ static int grep_it(const char *match, const char *cmd, ...)
 
 	do {
 		n = getline(&buf, &l, fp);
-		if (show_output && n > 0)
-			printf("%s", buf);
 		if (n > 0 && regexec(&reg, buf, 0, NULL, 0) == 0)
+			printf("%s", buf);
+	} while (n >= 0);
+
+	free(buf);
+ out:
+	ret = wait_for_exec(pid);
+	if (fp)
+		fclose(fp);
+	else
+		perror("fp");
+	close(ofd);
+	close(efd);
+	regfree(&reg);
+
+	return ret > 0 ? 0 : ret;
+}
+
+struct do_grep_match {
+	const char		*match;
+	const char		*cmd;
+	va_list			*ap;
+};
+
+static int grep_match(const char *match, const char *cmd, ...)
+{
+	struct do_grep_it gdata;
+	FILE *fp;
+	va_list ap;
+	char *buf = NULL;
+	ssize_t n;
+	size_t l = 0;
+	bool found = false;
+	int ofd;
+	int efd;
+	int pid;
+	int ret;
+
+	va_start(ap, cmd);
+	gdata.match = match;
+	gdata.cmd = cmd;
+	gdata.ap = &ap;
+	pid = pipe_it(&ofd, &efd, do_grep_it, &gdata);
+	va_end(ap);
+
+	if (pid < 0)
+		return -1;
+
+	fp = fdopen(ofd, "r");
+	if (!fp)
+		goto out;
+
+	do {
+		n = getline(&buf, &l, fp);
+		if (n > 0) {
+			if (show_output)
+				printf("%s", buf);
 			found = true;
+		}
 	} while (n >= 0);
 
 	free(buf);
@@ -215,7 +294,6 @@ static int grep_it(const char *match, const char *cmd, ...)
 		close(ofd);
 	}
 	close(efd);
-	regfree(&reg);
 
 	return found ? 0 : 1;
 }
@@ -240,7 +318,7 @@ static void test_trace_convert6(void)
 		ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL);
 		CU_TEST(ret == 0);
 	}
-	ret = grep_it("[ \t]6[ \t]*\\[Version\\]", "dump", TRACECMD_IN2, NULL);
+	ret = grep_match("[ \t]6[ \t]*\\[Version\\]", "dump", TRACECMD_IN2, NULL);
 	CU_TEST(ret == 0);
 }
 
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/4 trace-cmd: Add test for max size option of record

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add a unit test to test the trace-cmd record -m X option.

---
 utest/tracecmd-utest.c | 72 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
utest/tracecmd-utest.c +72 -0
index 88d7217762d2..c72684ad53fc 100644
@@ -308,6 +308,76 @@ static void test_trace_record_report(void)
 	CU_TEST(ret == 0);
 }
 
+static int read_stats(const char *out, const char *match, const char *cmd, ...)
+{
+	struct do_grep_it gdata;
+	FILE *fp;
+	va_list ap;
+	bool found = false;
+	char *buf = NULL;
+	char *p;
+	ssize_t n;
+	size_t l = 0;
+	int ofd;
+	int efd;
+	int pid;
+	int ret;
+	int val;
+
+	va_start(ap, cmd);
+	gdata.match = match;
+	gdata.cmd = cmd;
+	gdata.ap = &ap;
+	pid = pipe_it(&ofd, &efd, do_grep_it, &gdata);
+	va_end(ap);
+
+	if (pid < 0)
+		return -1;
+
+	fp = fdopen(ofd, "r");
+	if (!fp)
+		goto out;
+
+	do {
+		n = getline(&buf, &l, fp);
+		if (n > 0) {
+			for (p = buf; isspace(*p); p++)
+				;
+			val = atoi(p);
+			found = true;
+			if (show_output)
+				printf("%s", buf);
+			CU_TEST(val < 10000000);
+		}
+	} while (n >= 0);
+
+	free(buf);
+ out:
+	ret = wait_for_exec(pid);
+	if (fp)
+		fclose(fp);
+	else {
+		perror("fp");
+	}
+	if (!found)
+		ret = -1;
+	close(ofd);
+	close(efd);
+	return ret > 0 ? 0 : ret;
+}
+
+static void test_trace_record_max(void)
+{
+	int ret;
+
+	ret = run_trace("record", TRACECMD_OUT, "-p", "function", "-m", "5000",
+			"sleep", "10", NULL);
+	CU_TEST(ret == 0);
+
+	ret = read_stats(TRACECMD_FILE, ".*bytes in size.*", "report", "--stat", NULL);
+	CU_TEST(ret == 0);
+}
+
 static void test_trace_convert6(void)
 {
 	struct stat st;
@@ -422,4 +492,6 @@ 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, "Test max length",
+		    test_trace_record_max);
 }
Signature · 1 line
2.35.1