Skip to contentSkip to conversation

trace-cmd: Add "perf" sub command

PATCH Superseded Tzvetomir Stoyanov (VMware) 1 participant 5 messages Aug 7, 2020 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover trace-cmd: Add "perf" sub command

A new "trace-cmd perf" sub command is implemented, to collect performance information about a user space program, using perf. The command has one argumemnt, mandatory, using to specify a PID of running userspace process:
trace-cmd perf --pid <PID>
This is a work in progress, not completed yet.

What is implemented:
- Initial infrastructure for configuring perf, using its kernel interface, and running per CPU threads for reading collected perf data. Perf is used to collect periodically CALLCHAINs (values of IP register) of the requested PID.
- trace-cmd internal APIs, wrapper around libbfd, for parsing an ELF header.
- Resolving collected IP values to function names, using the binary file of the running application and all libraries that it uses.
- New trace.dat file option for storing address to function name mapping and a new API for address to name resolving using stored information.

What is not yet implemented:
- There are problems reading the perf mmap pages with collected data, currently only the first few records from each CPU page are read correctly.
- Assemble the trace.dat file with all mandatory headers. Think about a new type of trace data in the file and its format. This should be designed flexible, to be able to store various perf data, not only CALLCHAINs.
- Add support in "trace-dat report" for reporting the new data.
- Add APIs in libtracecmd for reading the new data.
- Enable collecting of kernel functions in the CALLCHAIN and implement resolving of kernel address to a kernel function name.
- Formatting and beautifying the code.
- A lot of testing and bug fixing.

Tzvetomir Stoyanov (VMware) (4):
trace-cmd: Internal refactoring of pid address map logic
trace-cmd: New internal APIs for reading ELF header
trace-cmd: Add a new option in trace.dat file for the address to
function name mapping
trace-cmd: Add new subcomand "trace-cmd perf"

 Makefile                       |  10 +
 include/trace-cmd/trace-cmd.h  |  13 +
 lib/trace-cmd/trace-input.c    | 144 ++++++
 tracecmd/Makefile              |   2 +
 tracecmd/include/trace-local.h |  33 +-
 tracecmd/trace-cmd.c           |   1 +
 tracecmd/trace-dump.c          |   3 +
 tracecmd/trace-obj-debug.c     | 770 +++++++++++++++++++++++++++++++++
 tracecmd/trace-perf.c          | 540 +++++++++++++++++++++++
 tracecmd/trace-record.c        | 107 +----
 10 files changed, 1510 insertions(+), 113 deletions(-)
 create mode 100644 tracecmd/trace-obj-debug.c
 create mode 100644 tracecmd/trace-perf.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/4 trace-cmd: Internal refactoring of pid address map logic

Make functions for collecting file to memory map of a PID non static, so they can be reused in the trace-cmd application context. Created new file trace-obj-debug.c with these functions.

---
 tracecmd/Makefile              |   1 +
 tracecmd/include/trace-local.h |  19 +++--
 tracecmd/trace-obj-debug.c     | 138 +++++++++++++++++++++++++++++++++
 tracecmd/trace-record.c        | 107 +------------------------
 4 files changed, 152 insertions(+), 113 deletions(-)
 create mode 100644 tracecmd/trace-obj-debug.c
tracecmd/Makefile +1 -0
index 5e59adf8..f9435558 100644
@@ -31,6 +31,7 @@ TRACE_CMD_OBJS += trace-show.o
 TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-dump.o
+TRACE_CMD_OBJS += trace-obj-debug.o
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-tsync.o
 endif
tracecmd/include/trace-local.h +11 -8
index d148aa16..c5c225e0 100644
@@ -178,14 +178,6 @@ struct func_list {
 	const char *mod;
 };
 
-struct pid_addr_maps {
-	struct pid_addr_maps		*next;
-	struct tracecmd_proc_addr_map	*lib_maps;
-	unsigned int			nr_lib_maps;
-	char				*proc_name;
-	int				pid;
-};
-
 struct opt_list {
 	struct opt_list *next;
 	const char	*option;
@@ -314,4 +306,15 @@ void *malloc_or_die(unsigned int size); /* Can be overridden */
 void __noreturn __die(const char *fmt, ...);
 void __noreturn _vdie(const char *fmt, va_list ap);
 
+/* --- Debug symbols--- */
+struct pid_addr_maps {
+	struct pid_addr_maps	*next;
+	struct tracecmd_proc_addr_map	*lib_maps;
+	unsigned int			nr_lib_maps;
+	char				*proc_name;
+	int				pid;
+};
+int trace_debug_get_filemap(struct pid_addr_maps **file_maps, int pid);
+void trace_debug_free_filemap(struct pid_addr_maps *maps);
+
 #endif /* __TRACE_LOCAL_H */
tracecmd/trace-obj-debug.c new file +138 -0

138 lines not on this page · raw message

tracecmd/trace-record.c +2 -105

125 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/4 trace-cmd: New internal APIs for reading ELF header

In reply to Tzvetomir Stoyanov (VMware)

Implemented new trace-cmd internal APIs for parsing ELF header and resolving VMA to function name and function name to VMA and file offset. The bfd library is used to read and parse the binary file. The new APIs are part of trace-cmd application and are visible only inside its context.

---
 Makefile                       |  10 +
 include/trace-cmd/trace-cmd.h  |   8 +
 tracecmd/include/trace-local.h |  12 +
 tracecmd/trace-obj-debug.c     | 632 +++++++++++++++++++++++++++++++++
 4 files changed, 662 insertions(+)
Makefile +10 -0
index b0340427..0d657969 100644
@@ -245,6 +245,16 @@ endif
 CUNIT_INSTALLED := $(shell if (printf "$(pound)include <CUnit/Basic.h>\n void main(){CU_initialize_registry();}" | $(CC) -x c - -lcunit >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
 export CUNIT_INSTALLED
 
+BFD_INSTALLED := $(shell if (echo -e "\#include <bfd.h>\n void main(){bfd_init();}" | $(CC) -xc  - -lbfd >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+
+export BFD_INSTALLED
+ifeq ($(BFD_INSTALLED), 1)
+CFLAGS += -DBFD_INSTALLED
+LIBS += -lbfd
+else
+$(warning libbfd is not installed)
+endif
+
 export CFLAGS
 export INCLUDES
 
include/trace-cmd/trace-cmd.h +8 -0
index f3c95f30..5ebd076e 100644
@@ -135,6 +135,14 @@ struct tracecmd_proc_addr_map {
 	char			*lib_name;
 };
 
+struct tracecmd_debug_symbols {
+	char *name;			/* symbol's name */
+	char *fname;			/* symbol's file */
+	unsigned long long vma_start;	/* symbol's start VMA */
+	unsigned long long vma_near;	/* symbol's requested VMA */
+	unsigned long long foffset;	/* symbol's offset in the binary file*/
+};
+
 typedef void (*tracecmd_show_data_func)(struct tracecmd_input *handle,
 					struct tep_record *record);
 typedef void (*tracecmd_handle_init_func)(struct tracecmd_input *handle,
tracecmd/include/trace-local.h +12 -0
index c5c225e0..ccae61d4 100644
@@ -317,4 +317,16 @@ struct pid_addr_maps {
 int trace_debug_get_filemap(struct pid_addr_maps **file_maps, int pid);
 void trace_debug_free_filemap(struct pid_addr_maps *maps);
 
+struct trace_debug_object;
+struct trace_debug_object *trace_debug_obj_create_file(char *file);
+struct trace_debug_object *trace_debug_obj_create_pid(int pid);
+void trace_debug_obj_destroy(struct trace_debug_object *debug);
+
+int trace_debug_resolve_symbols(struct trace_debug_object *obj);
+int trace_debug_add_resolve_symbol(struct trace_debug_object *obj,
+				   unsigned long long vma, char *name);
+
+void trace_debug_walk_resolved_symbols(struct trace_debug_object *obj,
+				       int (*callback)(struct tracecmd_debug_symbols *, void *),
+				       void *context);
 #endif /* __TRACE_LOCAL_H */
tracecmd/trace-obj-debug.c +632 -0

642 lines not on this page · raw message

Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/4 trace-cmd: Add a new option in trace.dat file for the address to function name mapping

In reply to Tzvetomir Stoyanov (VMware)

There are cases where resolving a user space address recorded in the trace file to function name is required, when this file is visualised. This mapping is available during the trace. A new trace.dat file option TRACECMD_OPTION_PID_SYMBOLS is added, for storing the mapping in the file. A new API is introduced, to resolve address to name, using information stored in the TRACECMD_OPTION_PID_SYMBOLS option:

struct tracecmd_debug_symbols * tracecmd_search_task_symbol(struct tracecmd_input *handle,

			     int pid, unsigned long long addr);
---
 include/trace-cmd/trace-cmd.h |   5 ++
 lib/trace-cmd/trace-input.c   | 144 ++++++++++++++++++++++++++++++++++
 tracecmd/trace-dump.c         |   3 +
 3 files changed, 152 insertions(+)
include/trace-cmd/trace-cmd.h +5 -0
index 5ebd076e..f47f6a19 100644
@@ -112,6 +112,7 @@ enum {
 	TRACECMD_OPTION_TRACEID,
 	TRACECMD_OPTION_TIME_SHIFT,
 	TRACECMD_OPTION_GUEST,
+	TRACECMD_OPTION_PID_SYMBOLS,
 };
 
 enum {
@@ -262,6 +263,10 @@ unsigned int tracecmd_record_ts_delta(struct tracecmd_input *handle,
 struct tracecmd_proc_addr_map *
 tracecmd_search_task_map(struct tracecmd_input *handle,
 			 int pid, unsigned long long addr);
+
+struct tracecmd_debug_symbols *
+tracecmd_search_task_symbol(struct tracecmd_input *handle,
+			    int pid, unsigned long long addr);
 #ifndef SWIG
 /* hack for function graph work around */
 extern __thread struct tracecmd_input *tracecmd_curr_thread_handle;
lib/trace-cmd/trace-input.c +144 -0

186 lines not on this page · raw message

tracecmd/trace-dump.c +3 -0
index 5642f12a..5c6992c8 100644
@@ -530,6 +530,9 @@ static void dump_options(int fd)
 		case TRACECMD_OPTION_GUEST:
 			dump_option_guest(fd, size);
 			break;
+		case TRACECMD_OPTION_PID_SYMBOLS:
+			dump_option_string(fd, size, "PIDSYMBOLS");
+			break;
 		default:
 			do_print(OPTIONS, " %d %d\t[Unknown option, size - skipping]\n",
 				 option, size);
Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/4 trace-cmd: Add new subcomand "trace-cmd perf"

In reply to Tzvetomir Stoyanov (VMware)

The new sub command uses perf to collect performance information of the selected process. It has one mandatory argument:
trace-cmd perf --pid <PID>

---
 tracecmd/Makefile              |   1 +
 tracecmd/include/trace-local.h |   2 +
 tracecmd/trace-cmd.c           |   1 +
 tracecmd/trace-perf.c          | 540 +++++++++++++++++++++++++++++++++
 4 files changed, 544 insertions(+)
 create mode 100644 tracecmd/trace-perf.c
tracecmd/Makefile +1 -0
index f9435558..64cd7430 100644
@@ -32,6 +32,7 @@ TRACE_CMD_OBJS += trace-list.o
 TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-dump.o
 TRACE_CMD_OBJS += trace-obj-debug.o
+TRACE_CMD_OBJS += trace-perf.o
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-tsync.o
 endif
tracecmd/include/trace-local.h +2 -0
index ccae61d4..2ab915ac 100644
@@ -101,6 +101,8 @@ void trace_usage(int argc, char **argv);
 
 void trace_dump(int argc, char **argv);
 
+void trace_perf(int argc, char **argv);
+
 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 		       int cpus, int *fds,
 		       int argc, char **argv, bool use_fifos,
tracecmd/trace-cmd.c +1 -0
index 7376c5a5..0c5b324f 100644
@@ -104,6 +104,7 @@ struct command commands[] = {
 	{"list", trace_list},
 	{"help", trace_usage},
 	{"dump", trace_dump},
+	{"perf", trace_perf},
 	{"-h", trace_usage},
 };
 
tracecmd/trace-perf.c new file +540 -0

540 lines not on this page · raw message

Signature · 1 line
2.26.2