diff mbox series

[RFC,v2,4/6] rt-utils: Add JSON common header output helper

Message ID 20210111173351.4971-5-dwagner@suse.de
State New
Headers show
Series Generate machine-readable output | expand

Commit Message

Daniel Wagner Jan. 11, 2021, 5:33 p.m. UTC
rt_write_json writes the common system information header of the
rt-test currently executed.

Reviewed-by: Daniel Wagner <dwagner@suse.de>
---
 src/include/rt-utils.h |  4 +++
 src/lib/rt-utils.c     | 58 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
diff mbox series

Patch

diff --git a/src/include/rt-utils.h b/src/include/rt-utils.h
index 39ddbd9eae29..ca9984d7cf35 100644
--- a/src/include/rt-utils.h
+++ b/src/include/rt-utils.h
@@ -80,4 +80,8 @@  static inline int64_t calctime(struct timespec t)
 	return time;
 }
 
+void rt_write_json(FILE *f, int argc, char *argv[],
+		   void (*cb)(FILE *, void *),
+		   void *data);
+
 #endif	/* __RT_UTILS.H */
diff --git a/src/lib/rt-utils.c b/src/lib/rt-utils.c
index 321a11b1172d..8fe0b7ed1f63 100644
--- a/src/lib/rt-utils.c
+++ b/src/lib/rt-utils.c
@@ -20,6 +20,7 @@ 
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/syscall.h> /* For SYS_gettid definitions */
+#include <sys/utsname.h>
 
 #include "rt-utils.h"
 #include "rt-sched.h"
@@ -482,3 +483,60 @@  void disable_trace_mark(void)
 {
 	close_tracemark_fd();
 }
+
+static char *get_cmdline(int argc, char *argv[])
+{
+	char *cmdline;
+	int len, i;
+
+	len = 0;
+	for (i = 0; i < argc; i++)
+		len += strlen(argv[i]) + 1;
+
+	cmdline = malloc(len);
+	if (!cmdline)
+		err_exit(ENOMEM, "Could not copy cmdline");
+
+	cmdline[0] = '\0';
+	for (i = 0; i < argc;) {
+		cmdline = strcat(cmdline, argv[i]);
+		i++;
+		if (i < argc)
+			cmdline = strcat(cmdline, " ");
+	}
+
+	return cmdline;
+}
+
+void rt_write_json(FILE *f, int argc, char *argv[],
+		  void (*cb)(FILE *, void *),
+		  void *data)
+{
+	struct utsname buf;
+	char *cmdline;
+
+	cmdline = get_cmdline(argc, argv);
+	if (!cmdline)
+		err_exit(ENOMEM, "get_cmdline()");
+
+	if (uname(&buf))
+		err_exit(errno, "Could not retrieve system information");
+
+	fprintf(f, "{\n");
+	fprintf(f, "  \"file_version\": 1,\n");
+	fprintf(f, "  \"cmdline:\": \"%s\",\n", cmdline);
+	fprintf(f, "  \"rt_test_version:\": \"%1.2f\",\n", VERSION);
+	fprintf(f, "  \"sysinfo\": {\n");
+	fprintf(f, "    \"sysname\": \"%s\",\n", buf.sysname);
+	fprintf(f, "    \"nodename\": \"%s\",\n", buf.nodename);
+	fprintf(f, "    \"release\": \"%s\",\n", buf.release);
+	fprintf(f, "    \"version\": \"%s\",\n", buf.version);
+	fprintf(f, "    \"machine\": \"%s\"\n", buf.machine);
+	fprintf(f, "  },\n");
+
+	(cb)(f, data);
+
+	fprintf(f, "}\n");
+
+	free(cmdline);
+}