Skip to contentSkip to conversation

Separate trace-cmd and libtracecmd code

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 5 messages Sep 3, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover Separate trace-cmd and libtracecmd code

libtracecmd is a library, containing functions that can be used without the trace-cmd application. However, some of the functions declared as libtracecmd APIs in trace-cmd.h depend on trace-cmd context. That causes a problem when other application uses the library. The problem can be observed when running kerneshark and there is a python module, loaded by the python plugin - there is a bunch of warnings.
To resolve the problem, implementations of all trace-cmd independent functions are moved into libtracecmd. All libtracecmd functions, that depend on trace-cmd context are removed from the library and from trace-cmd.h file.

[
v3 changes:
- Removed accepted patches from the series.
- Added new patch: "Update descriptions of "debug" libtracecmd APIs".
- Renamed "plog*" library APIs to "tracecmd_plog*".
- Rmoved "tracecmd" prefix from local variables.
- Renamed trace_set_log_file to tracecmd_set_logfile.
- Changed tracecmd_set_quiet() and tracecmd_get_quiet() APIs to work per tracecmd_output handler.

v2 changes:
- Added a new patch:
Move trace-cmd-local.h from the application to the library.
- Remove trace-output.c dependency of version.h. Moved FILE_VERSION_STRING define from top Makefile to trace-cmd-local.h.
]

Tzvetomir Stoyanov (VMware) (4):
trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd
trace-cmd: Move plog() function to libtracecmd.
trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd
trace-cmd: Update descriptions of "debug" libtracecmd APIs

 include/trace-cmd/trace-cmd.h           |   9 +-
 include/trace-cmd/trace-msg.h           |   3 -
 lib/trace-cmd/include/trace-cmd-local.h |   2 -
 lib/trace-cmd/trace-msg.c               |   8 +-
 lib/trace-cmd/trace-output.c            |  28 ++++-
 lib/trace-cmd/trace-util.c              | 139 ++++++++++++++++++++++--
 tracecmd/include/trace-local.h          |   2 -
 tracecmd/trace-cmd.c                    |   2 -
 tracecmd/trace-listen.c                 |  89 +++------------
 tracecmd/trace-record.c                 |  17 ++-
 tracecmd/trace-stack.c                  |  56 +---------
 11 files changed, 199 insertions(+), 156 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v3 1/4 trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd

A trace-cmd global variable "quiet" is used from libtracecmd and should be defined there. A new library APIs are implemented to access it:
void tracecmd_set_quiet(int quiet);
int tracecmd_get_quiet(void);

---
 include/trace-cmd/trace-cmd.h           |  3 +++
 lib/trace-cmd/include/trace-cmd-local.h |  2 --
 lib/trace-cmd/trace-output.c            | 28 +++++++++++++++++++++++--
 tracecmd/include/trace-local.h          |  2 --
 tracecmd/trace-cmd.c                    |  2 --
 tracecmd/trace-record.c                 | 15 +++++++++----
 6 files changed, 40 insertions(+), 12 deletions(-)
include/trace-cmd/trace-cmd.h +3 -0
index ede510c..f44f7a4 100644
@@ -57,6 +57,9 @@ struct tracecmd_output;
 struct tracecmd_recorder;
 struct hook_list;
 
+void tracecmd_set_quiet(struct tracecmd_output *handle, bool set_quiet);
+bool tracecmd_get_quiet(struct tracecmd_output *handle);
+
 static inline int tracecmd_host_bigendian(void)
 {
 	unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 };
lib/trace-cmd/include/trace-cmd-local.h +0 -2
index bad325f..09574db 100644
@@ -18,8 +18,6 @@
 #define STR(x)	_STR(x)
 #define FILE_VERSION_STRING STR(FILE_VERSION)
 
-extern int quiet;
-
 static ssize_t __do_write(int fd, const void *data, size_t size)
 {
 	ssize_t tot = 0;
lib/trace-cmd/trace-output.c +26 -2
index 1f94346..41932ee 100644
@@ -53,6 +53,7 @@ struct tracecmd_output {
 	char			*tracing_dir;
 	int			options_written;
 	int			nr_options;
+	bool			quiet;
 	struct list_head 	options;
 	struct tracecmd_msg_handle *msg_handle;
 };
@@ -103,6 +104,29 @@ static unsigned long long convert_endian_8(struct tracecmd_output *handle,
 	return tep_read_number(handle->pevent, &val, 8);
 }
 
+/**
+ * tracecmd_set_quiet - Set if to print output to the screen
+ * @quiet: If non zero, print no output to the screen
+ *
+ */
+void tracecmd_set_quiet(struct tracecmd_output *handle, bool set_quiet)
+{
+	if (handle)
+		handle->quiet = set_quiet;
+}
+
+/**
+ * tracecmd_get_quiet - Get if to print output to the screen
+ * Returns non zero, if no output to the screen should be printed
+ *
+ */
+bool tracecmd_get_quiet(struct tracecmd_output *handle)
+{
+	if (handle)
+		return handle->quiet;
+	return false;
+}
+
 void tracecmd_output_free(struct tracecmd_output *handle)
 {
 	struct tracecmd_option *option;
@@ -1157,7 +1181,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 		goto out_free;
 
 	for (i = 0; i < cpus; i++) {
-		if (!quiet)
+		if (!tracecmd_get_quiet(handle))
 			fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n",
 				i, (unsigned long long) offsets[i]);
 		offset = lseek64(handle->fd, offsets[i], SEEK_SET);
@@ -1172,7 +1196,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			    check_size, sizes[i]);
 			goto out_free;
 		}
-		if (!quiet)
+		if (!tracecmd_get_quiet(handle))
 			fprintf(stderr, "    %llu bytes in size\n",
 				(unsigned long long)check_size);
 	}
tracecmd/include/trace-local.h +0 -2
index 7de5dd6..05760d8 100644
@@ -12,8 +12,6 @@
 #include "trace-cmd.h"
 #include "event-utils.h"
 
-extern int quiet;
-
 /* fix stupid glib guint64 typecasts and printf formats */
 typedef unsigned long long u64;
 
tracecmd/trace-cmd.c +0 -2
index 4add2e2..30691b6 100644
@@ -16,8 +16,6 @@
 int silence_warnings;
 int show_status;
 
-int quiet;
-
 void warning(const char *fmt, ...)
 {
 	va_list ap;
tracecmd/trace-record.c +11 -4
index 7467b47..61457c8 100644
@@ -80,6 +80,8 @@ static char *host;
 static unsigned int *client_ports;
 static int sfd;
 
+static bool quiet;
+
 /* Max size to let a per cpu file get */
 static int max_kb;
 
@@ -3162,13 +3164,16 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 	/* Now create the handle through this socket */
 	if (msg_handle->version == V3_PROTOCOL) {
 		network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events);
+		tracecmd_set_quiet(network_handle, quiet);
 		add_options(network_handle, ctx);
 		tracecmd_write_cpus(network_handle, instance->cpu_count);
 		tracecmd_write_options(network_handle);
 		tracecmd_msg_finish_sending_data(msg_handle);
-	} else
+	} else {
 		network_handle = tracecmd_create_init_fd_glob(msg_handle->fd,
 							      listed_events);
+		tracecmd_set_quiet(network_handle, quiet);
+	}
 
 	instance->network_handle = network_handle;
 
@@ -3444,9 +3449,10 @@ static void record_data(struct common_record_context *ctx)
 	if (!local)
 		return;
 
-	if (latency)
+	if (latency) {
 		handle = tracecmd_create_file_latency(output_file, local_cpu_count);
-	else {
+		tracecmd_set_quiet(handle, quiet);
+	} else {
 		if (!local_cpu_count)
 			return;
 
@@ -3478,6 +3484,7 @@ static void record_data(struct common_record_context *ctx)
 		handle = tracecmd_create_init_file_glob(output_file, listed_events);
 		if (!handle)
 			die("Error creating output file");
+		tracecmd_set_quiet(handle, quiet);
 
 		add_options(handle, ctx);
 
@@ -5155,7 +5162,7 @@ static void parse_record_options(int argc,
 			break;
 		case OPT_quiet:
 		case 'q':
-			quiet = 1;
+			quiet = true;
 			break;
 		default:
 			usage(argv);
Signature · 1 line
2.21.0
#2 lore raw

PATCH v3 2/4 trace-cmd: Move plog() function to libtracecmd.

In reply to Tzvetomir Stoyanov (VMware)

plog() function writes logs into a log file. It is used in libtracecmd and its implementation should be there.
The function is moved from trace-cmd into the library and renamed to tracecmd_plog(). Two additional APIs are implemented:
int tracecmd_set_log_file(char *logfile); - use it to set
the log file.
void tracecmd_plog_error(const char *fmt, ...); - use it to log
an error message into the file.

The plog() function is used also from pdie() in trace-cmd. pdie() depends on trace-cmd context and cannot be moved to the library. It is reimplemented as macros, in order to utilize the new tracecmd_plog() library function.

---
 include/trace-cmd/trace-cmd.h |  4 ++
 include/trace-cmd/trace-msg.h |  3 --
 lib/trace-cmd/trace-msg.c     |  8 ++--
 lib/trace-cmd/trace-util.c    | 70 +++++++++++++++++++++++++++
 tracecmd/trace-listen.c       | 89 +++++++----------------------------
 tracecmd/trace-record.c       |  2 +-
 6 files changed, 96 insertions(+), 80 deletions(-)
include/trace-cmd/trace-cmd.h +4 -0
index f44f7a4..de3183d 100644
@@ -389,6 +389,10 @@ struct hook_list {
 struct hook_list *tracecmd_create_event_hook(const char *arg);
 void tracecmd_free_hooks(struct hook_list *hooks);
 
+void tracecmd_plog(const char *fmt, ...);
+void tracecmd_plog_error(const char *fmt, ...);
+int tracecmd_set_logfile(char *logfile);
+
 /* --- Hack! --- */
 int tracecmd_blk_hack(struct tracecmd_input *handle);
 
include/trace-cmd/trace-msg.h +0 -3
index b7fe10b..aab8a69 100644
@@ -12,7 +12,4 @@
 
 extern unsigned int page_size;
 
-void plog(const char *fmt, ...);
-void pdie(const char *fmt, ...);
-
 #endif /* _TRACE_MSG_H_ */
lib/trace-cmd/trace-msg.c +4 -4
index 92562c7..74c5254 100644
@@ -318,7 +318,7 @@ static int tracecmd_msg_recv(int fd, struct tracecmd_msg *msg)
 
 	return 0;
 error:
-	plog("Receive an invalid message(size=%d)\n", size);
+	tracecmd_plog("Receive an invalid message(size=%d)\n", size);
 	return -ENOMSG;
 }
 
@@ -531,7 +531,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 	}
 
 	cpus = ntohl(msg.tinit.cpus);
-	plog("cpus=%d\n", cpus);
+	tracecmd_plog("cpus=%d\n", cpus);
 	if (cpus < 0) {
 		ret = -EINVAL;
 		goto error;
@@ -540,7 +540,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 	msg_handle->cpu_count = cpus;
 
 	pagesize = ntohl(msg.tinit.page_size);
-	plog("pagesize=%d\n", pagesize);
+	tracecmd_plog("pagesize=%d\n", pagesize);
 	if (pagesize <= 0) {
 		ret = -EINVAL;
 		goto error;
@@ -570,7 +570,7 @@ int tracecmd_msg_initial_setting(struct tracecmd_msg_handle *msg_handle)
 
 		/* do we understand this option? */
 		if (!process_option(msg_handle, p))
-			plog("Cannot understand option '%s'\n", p);
+			tracecmd_plog("Cannot understand option '%s'\n", p);
 
 		p = strchr(p, '\0');
 	}
lib/trace-cmd/trace-util.c +70 -0

79 lines not on this page · raw message

tracecmd/trace-listen.c +17 -72

162 lines not on this page · raw message

tracecmd/trace-record.c +1 -1
index 61457c8..96d2c1a 100644
@@ -3055,7 +3055,7 @@ static void check_protocol_version(struct tracecmd_msg_handle *msg_handle)
 	if (n < 0 || !buf[0]) {
 		/* the server uses the v1 protocol, so we'll use it */
 		msg_handle->version = V1_PROTOCOL;
-		plog("Use the v1 protocol\n");
+		tracecmd_plog("Use the v1 protocol\n");
 	} else {
 		if (memcmp(buf, "V3", n) != 0)
 			die("Cannot handle the protocol %s", buf);
Signature · 1 line
2.21.0
#3 lore raw

PATCH v3 3/4 trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd

In reply to Tzvetomir Stoyanov (VMware)

tracecmd_stack_tracer_status() function reads the stack tracer status from the proc file system. It does not depend on trace-cmd context and can be used standalone. The function is moved from trace-cmd application into libtracecmd.

---
 lib/trace-cmd/trace-util.c | 51 ++++++++++++++++++++++++++++++++++
 tracecmd/trace-stack.c     | 56 ++------------------------------------
 2 files changed, 53 insertions(+), 54 deletions(-)
lib/trace-cmd/trace-util.c +51 -0
index 60bcb68..6e2a352 100644
@@ -25,6 +25,7 @@
 #define LOCAL_PLUGIN_DIR ".trace-cmd/plugins"
 #define TRACEFS_PATH "/sys/kernel/tracing"
 #define DEBUGFS_PATH "/sys/kernel/debug"
+#define PROC_STACK_FILE "/proc/sys/kernel/stack_tracer_enabled"
 
 int tracecmd_disable_sys_plugins;
 int tracecmd_disable_plugins;
@@ -1765,3 +1766,53 @@ int tracecmd_set_logfile(char *logfile)
 		return -1;
 	return 0;
 }
+
+/**
+ * tracecmd_stack_tracer_status - Check stack trace status
+ * @status: Returned stack trace status:
+ *             0 - not configured, disabled
+ *             non 0 - enabled
+ *
+ * Returns -1 in case of an error, 0 if file does not exist
+ * (stack tracer not configured in kernel) or 1 on successful completion.
+ */
+int tracecmd_stack_tracer_status(int *status)
+{
+	struct stat stat_buf;
+	char buf[64];
+	long num;
+	int fd;
+	int n;
+
+	if (stat(PROC_STACK_FILE, &stat_buf) < 0) {
+		/* stack tracer not configured on running kernel */
+		*status = 0; /* not configured means disabled */
+		return 0;
+	}
+
+	fd = open(PROC_STACK_FILE, O_RDONLY);
+
+	if (fd < 0)
+		return -1;
+
+	n = read(fd, buf, sizeof(buf));
+	close(fd);
+
+	if (n <= 0)
+		return -1;
+
+	if (n >= sizeof(buf))
+		return -1;
+
+	buf[n] = 0;
+
+	errno = 0;
+	num = strtol(buf, NULL, 10);
+
+	/* Check for various possible errors */
+	if (num > INT_MAX || num < INT_MIN || (!num && errno))
+		return -1;
+
+	*status = num;
+	return 1; /* full success */
+}
tracecmd/trace-stack.c +2 -54

74 lines not on this page · raw message

Signature · 1 line
2.21.0
#4 lore raw

PATCH v3 4/4 trace-cmd: Update descriptions of "debug" libtracecmd APIs

In reply to Tzvetomir Stoyanov (VMware)

The description of "debug" libtracecmd APIs:
tracecmd_set_debug()
tracecmd_get_debug()
does not match the functions. There was a copy leftover from "quiet" APIs.
The name of the local static variable is changed form tracecmd_debug to debug, as it is not visible outside of the library.

---
 include/trace-cmd/trace-cmd.h |  2 +-
 lib/trace-cmd/trace-util.c    | 18 +++++++++---------
 2 files changed, 10 insertions(+), 10 deletions(-)
include/trace-cmd/trace-cmd.h +1 -1
index de3183d..3c5e827 100644
@@ -49,7 +49,7 @@ enum {
 void tracecmd_record_ref(struct tep_record *record);
 void free_record(struct tep_record *record);
 
-void tracecmd_set_debug(bool debug);
+void tracecmd_set_debug(bool set_debug);
 bool tracecmd_get_debug(void);
 
 struct tracecmd_input;
lib/trace-cmd/trace-util.c +9 -9
index 6e2a352..6a53004 100644
@@ -29,7 +29,7 @@
 
 int tracecmd_disable_sys_plugins;
 int tracecmd_disable_plugins;
-static bool tracecmd_debug;
+static bool debug;
 
 static FILE *logfp;
 
@@ -101,23 +101,23 @@ char **trace_util_list_plugin_options(void)
 }
 
 /**
- * tracecmd_set_quiet - Set if to print output to the screen
- * @quiet: If non zero, print no output to the screen
- *
+ * tracecmd_set_debug - Set debug mode of the tracecmd library
+ * @set_debug: The new "debug" mode. If true, the tracecmd library is
+ * in "debug" mode
  */
-void tracecmd_set_debug(bool debug)
+void tracecmd_set_debug(bool set_debug)
 {
-	tracecmd_debug = debug;
+	debug = set_debug;
 }
 
 /**
- * tracecmd_get_quiet - Get if to print output to the screen
- * Returns non zero, if no output to the screen should be printed
+ * tracecmd_get_debug - Get debug mode of tracecmd library
+ * Returns true, if the tracecmd library is in debug mode.
  *
  */
 bool tracecmd_get_debug(void)
 {
-	return tracecmd_debug;
+	return debug;
 }
 
 void trace_util_free_plugin_options_list(char **list)
Signature · 1 line
2.21.0