Add VM kernel tracing over vsockets and FIFOs
v6 cover Add VM kernel tracing over vsockets and FIFOs
This patchset adds support for tracing guest kernels to trace-cmd.
Changes in v6:
- added specialized data transfer path over FIFOs making single splice() per iteration
- made tracecmd_msg_recv_trace_req/tracecmd_msg_recv_trace_resp more defensive to invalid messages
Changes in v5:
- add FIFOs transport for tracing data
- fixed a bug in tracecmd_msg_wait_close
Changes in v4:
- detect and use splice(2) on vsock sockets if possible
- switch port numbers to unsigned int
- trace-cmd record --date flag is now set for all guests if provided by the user
- removed grow_cap and exponential buffer size growth
Changes in v3:
- addressed Steven's feedback
- detect and disable guest tracing if <linux/vm_sockets.h> is not available
- the --date flag is now treated as global for all guest instances
- fixed a bug that caused --date to be ignored for host tracing data when tracing guests
Changes in v2:
- rebased on top of protocol V3
- fixed system clock timestamps with the --date flag
Slavomir Kaslev (10):
trace-cmd: Add tracecmd_create_recorder_virt function
trace-cmd: Add TRACE_REQ and TRACE_RESP messages
trace-cmd: Add buffer instance flags for tracing in guest and agent
context
trace-cmd: Add VM kernel tracing over vsockets transport
trace-cmd: Use splice(2) for vsockets if available
trace-cmd: Add trace-cmd setup-guest command
trace-cmd: Try to autodetect number of guest CPUs in setup-guest if
not specified
trace-cmd: Add setup-guest flag for attaching FIFOs to the guest VM
config
trace-cmd: Add splice() recording from FIFO without additional pipe
buffer
trace-cmd: Add VM tracing over FIFOs transport
Steven Rostedt (VMware) (1):
trace-cmd: Detect if vsockets are available
Makefile | 7 + include/trace-cmd/trace-cmd.h | 16 +- lib/trace-cmd/trace-recorder.c | 140 ++++-- tracecmd/Makefile | 7 +- tracecmd/include/trace-local.h | 26 ++ tracecmd/trace-agent.c | 257 +++++++++++ tracecmd/trace-cmd.c | 4 + tracecmd/trace-msg.c | 216 ++++++++- tracecmd/trace-record.c | 779 ++++++++++++++++++++++++++++++--- tracecmd/trace-setup-guest.c | 255 +++++++++++ tracecmd/trace-usage.c | 22 +- 11 files changed, 1641 insertions(+), 88 deletions(-) create mode 100644 tracecmd/trace-agent.c create mode 100644 tracecmd/trace-setup-guest.c
Signature · 1 line
2.19.1
RFC PATCH v6 1/11 trace-cmd: Detect if vsockets are available
From: "Steven Rostedt (VMware)" <[email protected]>
Detect and define VSOCK if vsockets are available on the system. This macro is used to disable VM remote tracing features on older kernels.
--- Makefile | 7 +++++++ 1 file changed, 7 insertions(+)
Makefile +7 -0
@@ -204,6 +204,13 @@ CFLAGS ?= -g -Wall CPPFLAGS ?= LDFLAGS ?= +VSOCK_DEFINED := $(shell if (echo "\#include <linux/vm_sockets.h>" | $(CC) -E - >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) + +export VSOCK_DEFINED +ifeq ($(VSOCK_DEFINED), 1) +CFLAGS += -DVSOCK +endif + export CFLAGS export INCLUDES
Signature · 1 line
2.19.1
RFC PATCH v6 2/11 trace-cmd: Add tracecmd_create_recorder_virt function
In reply to Slavomir KaslevAdd tracecmd_create_recorder_virt function that will be used for tracing VM guests.
--- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/trace-recorder.c | 53 ++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 15 deletions(-)
include/trace-cmd/trace-cmd.h +1 -0
@@ -277,6 +277,7 @@ enum {void tracecmd_free_recorder (struct tracecmd_recorder *recorder);struct tracecmd_recorder *tracecmd_create_recorder (const char *file,int cpu,unsigned flags);struct tracecmd_recorder *tracecmd_create_recorder_fd (int fd,int cpu,unsigned flags); +struct tracecmd_recorder *tracecmd_create_recorder_virt (const char *file,int cpu,unsigned flags,int trace_fd);struct tracecmd_recorder *tracecmd_create_recorder_maxkb (const char *file,int cpu,unsigned flags,int maxkb);struct tracecmd_recorder *tracecmd_create_buffer_recorder_fd (int fd,int cpu,unsigned flags,const char *buffer);struct tracecmd_recorder *tracecmd_create_buffer_recorder (const char *file,int cpu,unsigned flags,const char *buffer);
lib/trace-cmd/trace-recorder.c +38 -15
81 lines not on this page · raw message
Signature · 1 line
2.19.1
RFC PATCH v6 3/11 trace-cmd: Add TRACE_REQ and TRACE_RESP messages
In reply to Slavomir KaslevAdd TRACE_REQ and TRACE_RESP messages which are used for initiating guest VM tracing.
--- include/trace-cmd/trace-cmd.h | 12 ++ tracecmd/trace-msg.c | 208 +++++++++++++++++++++++++++++++++- 2 files changed, 219 insertions(+), 1 deletion(-)
include/trace-cmd/trace-cmd.h +12 -0
@@ -331,6 +331,18 @@ int tracecmd_msg_collect_data(struct tracecmd_msg_handle *msg_handle, int ofd);bool tracecmd_msg_done (struct tracecmd_msg_handle *msg_handle);void tracecmd_msg_set_done (struct tracecmd_msg_handle *msg_handle); +int tracecmd_msg_send_trace_req (struct tracecmd_msg_handle *msg_handle, +int argc,char **argv); +int tracecmd_msg_recv_trace_req (struct tracecmd_msg_handle *msg_handle, +int *argc,char ***argv); + +int tracecmd_msg_send_trace_resp (struct tracecmd_msg_handle *msg_handle, +int nr_cpus,int page_size, +unsigned int *ports); +int tracecmd_msg_recv_trace_resp (struct tracecmd_msg_handle *msg_handle, +int *nr_cpus,int *page_size, +unsigned int **ports); +/* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[];
tracecmd/trace-msg.c +207 -1
235 lines not on this page · raw message
Signature · 1 line
2.19.1
Re: [RFC PATCH v6 03/11] trace-cmd: Add TRACE_REQ and TRACE_RESP messages
Quoted 50 lines · On Thu, 14 Feb 2019 16:13:27 +0200 Slavomir Kaslev <[email protected]> wrote:
+ /*
tracecmd/trace-msg.c +49
-0+* NOTE: On success, the returned `argv` should be freed with: +* free(argv[0]); +* free(argv); +*/ +int tracecmd_msg_recv_trace_req (struct tracecmd_msg_handle *msg_handle, +int *argc,char ***argv) +{ +struct tracecmd_msg msg; +char *p, *buf_end, **args; +int i, ret, nr_args; +ssize_t buf_len; + + ret =tracecmd_msg_recv (msg_handle->fd, &msg); +if (ret <0 ) +return ret; + +if (ntohl (msg.hdr.cmd) !=MSG_TRACE_REQ ) { + ret = -ENOTSUP ; + goto out; + } + + nr_args =ntohl (msg.trace_req.argc); +if (nr_args <=0 ) { + ret = -EINVAL ; + goto out; + } + + buf_len =ntohl (msg.hdr.size) -MSG_HDR_LEN -ntohl (msg.hdr.cmd_size); + buf_end = (char *)msg.buf + buf_len; +if (buf_len <=0 && ((char *)msg.buf)[buf_len-1 ] !='\0' ) { + ret = -EINVAL ; + goto out; + } + + args =calloc (nr_args,sizeof (*args)); +if (!args) { + ret = -ENOMEM ; + goto out; + } + + p = msg.buf; +for (i =0 ; i < nr_args; i++) { +if (p >= buf_end) { + ret = -EINVAL ; + goto out_args; + } + args[i] = p; + p =strchr (p,'\0' ); +if (!p) {
tracecmd/trace-msg.c error: · line 838
Quoted 10 lines
tracecmd/trace-msg.c +10
-0+ ret = -EINVAL ; + goto out_args; + } + p++; + } + +/* +* On success we're passing msg.buf to the caller through argv[0] so we +* don't msg_free in this case. +*/
This is OK for now, but I'm still wondering if we should just add:
msg.buf = NULL; msg_free(&msg);
For robustness. What happens if for some reason in the future that we add another allocation to msg? That would cause this to leak memory.
Yeah, yeah, this is highly paranoid, but code like this is exactly how memory leaks come about and other bugs come about. It's taking advantage of internals of how msg_free() works to optimize the case. And when the internals change, things break.
No need to change anything here, I may just add the code on top of this series.
-- Steve
Quoted 14 lines
tracecmd/trace-msg.c +14
-0+ *argc = nr_args; + *argv = args; +return 0 ; + +out_args: +free (args); +out: +error_operation (&msg); +if (ret == -EOPNOTSUPP ) +handle_unexpected_msg (msg_handle, &msg); +msg_free (&msg); +return ret; +} +
RFC PATCH v6 4/11 trace-cmd: Add buffer instance flags for tracing in guest and agent context
In reply to Slavomir KaslevAdd BUFFER_FL_GUEST and BUFFER_FL_AGENT flags to differentiate when trace-record.c is being called to trace guest or the VM tracing agent.
Also disable functions talking to the local tracefs when called in recording guest instances context.
--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 55 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-)
tracecmd/include/trace-local.h +2 -0
@@ -149,6 +149,8 @@ char *strstrip(char *str);enum buffer_instance_flags {BUFFER_FL_KEEP =1 <<0 ,BUFFER_FL_PROFILE =1 <<1 , +BUFFER_FL_GUEST =1 <<2 , +BUFFER_FL_AGENT =1 <<3 , };struct func_list {
tracecmd/trace-record.c +53 -2
169 lines not on this page · raw message
Signature · 1 line
2.19.1
Re: [RFC PATCH v6 04/11] trace-cmd: Add buffer instance flags for tracing in guest and agent context
Quoted 50 lines · On Thu, 14 Feb 2019 16:13:28 +0200 Slavomir Kaslev <[email protected]> wrote:
Add
BUFFER_FL_GUESTandBUFFER_FL_AGENTflags to differentiate whentrace-record.cis being called to trace guest or the VM tracing agent.Also disable functions talking to the local tracefs when called in recording guest instances context.
Signed-off-by Slavomir Kaslev <[email protected]>--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 55 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-)tracecmd/include/trace-local.h +2
-0@@ -149,6 +149,8 @@ char *strstrip(char *str);enum buffer_instance_flags {BUFFER_FL_KEEP =1 <<0 ,BUFFER_FL_PROFILE =1 <<1 , +BUFFER_FL_GUEST =1 <<2 , +BUFFER_FL_AGENT =1 <<3 , };struct func_list {tracecmd/trace-record.c +6
-0@@ -781,6 +781,9 @@ static void __clear_trace(struct buffer_instance *instance)FILE *fp;char *path; +if (instance->flags &BUFFER_FL_GUEST ) +return ; +/* reset the trace */ path =get_instance_file (instance,"trace" ); fp =fopen (path,"w" );@@ -1264,6 +1267,9 @@ set_plugin_instance(struct buffer_instance *instance, const char *name)char *path;char zero ='0' ; +if (instance->flags &BUFFER_FL_GUEST ) +return ; + path =get_instance_file (instance,"current_tracer" ); fp =fopen (path,"w" );if (!fp) {
tracecmd/trace-record.c static void disable_func_stack_trace_instance(struct buffer_instance *instance) · line 1374
Quoted 9 lines
tracecmd/trace-record.c +3
-0@@ -1360,6 +1366,9 @@ static void disable_func_stack_trace_instance(struct buffer_instance *instance)int size;int ret; +if (instance->flags &BUFFER_FL_GUEST ) +return ; + path =get_instance_file (instance,"current_tracer" ); ret =stat (path, &st);tracecmd_put_tracing_file (path);
I think we should add a:
#define is_guest(instance) ((instance)->flags & BUFFER_FL_GUEST)
Helper macro here, and all these should be turned into:
if (is_guest(instance)) return;
Less error prone, and looks cleaner.
We could add a is_agent() too in later patches.
-- Steve
Re: [RFC PATCH v6 04/11] trace-cmd: Add buffer instance flags for tracing in guest and agent context
Quoted 68 lines · Steven Rostedt
Quoted 50 lines · On Thu, 14 Feb 2019 16:13:28 +0200 Slavomir Kaslev <[email protected]> wrote:
Add
BUFFER_FL_GUESTandBUFFER_FL_AGENTflags to differentiate whentrace-record.cis being called to trace guest or the VM tracing agent.Also disable functions talking to the local tracefs when called in recording guest instances context.
Signed-off-by Slavomir Kaslev <[email protected]>--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 55 ++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-)tracecmd/include/trace-local.h +2
-0@@ -149,6 +149,8 @@ char *strstrip(char *str);enum buffer_instance_flags {BUFFER_FL_KEEP =1 <<0 ,BUFFER_FL_PROFILE =1 <<1 , +BUFFER_FL_GUEST =1 <<2 , +BUFFER_FL_AGENT =1 <<3 , };struct func_list {tracecmd/trace-record.c +6
-0@@ -781,6 +781,9 @@ static void __clear_trace(struct buffer_instance *instance)FILE *fp;char *path; +if (instance->flags &BUFFER_FL_GUEST ) +return ; +/* reset the trace */ path =get_instance_file (instance,"trace" ); fp =fopen (path,"w" );@@ -1264,6 +1267,9 @@ set_plugin_instance(struct buffer_instance *instance, const char *name)char *path;char zero ='0' ; +if (instance->flags &BUFFER_FL_GUEST ) +return ; + path =get_instance_file (instance,"current_tracer" ); fp =fopen (path,"w" );if (!fp) {tracecmd/trace-record.c static void disable_func_stack_trace_instance(struct buffer_instance *instance) · line 1374
Quoted 9 lines
tracecmd/trace-record.c +3
-0@@ -1360,6 +1366,9 @@ static void disable_func_stack_trace_instance(struct buffer_instance *instance)int size;int ret; +if (instance->flags &BUFFER_FL_GUEST ) +return ; + path =get_instance_file (instance,"current_tracer" ); ret =stat (path, &st);tracecmd_put_tracing_file (path);I think we should add a:
#define
is_guest(instance)((instance)->flags &BUFFER_FL_GUEST)
Quoted 8 lines
Helper macro here, and all these should be turned into:
if (is_guest(instance)) return;Less error prone, and looks cleaner.
We could add a
is_agent()too in later patches.
Makes sense. I added both is_agent and is_guest for the next iteration of this patchset.
RFC PATCH v6 5/11 trace-cmd: Add VM kernel tracing over vsockets transport
In reply to Slavomir KaslevThis patch adds VM tracing over vsockets. The new trace-cmd agent command needs run on each guest we want to trace:
you@guest2 # trace-cmd agent
Then trace-cmd record on the host can collect data from both the host and several guests simultaneously:
you@host $ trace-cmd record -A guest1 -e irq -e sched \
-A guest2 -p function -e all
--- tracecmd/Makefile | 6 +- tracecmd/include/trace-local.h | 16 + tracecmd/trace-agent.c | 225 ++++++++++++ tracecmd/trace-cmd.c | 3 + tracecmd/trace-record.c | 605 ++++++++++++++++++++++++++++++--- tracecmd/trace-usage.c | 13 +- 6 files changed, 819 insertions(+), 49 deletions(-) create mode 100644 tracecmd/trace-agent.c
tracecmd/Makefile +5 -1
@@ -33,13 +33,17 @@ TRACE_CMD_OBJS += trace-output.o TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-msg.o +ifeq ($(VSOCK_DEFINED), 1) +TRACE_CMD_OBJS += trace-agent.o +endif + ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o) all_objs := $(sort $(ALL_OBJS)) all_deps := $(all_objs:$(bdir)/%.o=$(bdir)/.%.d) CONFIG_INCLUDES = -CONFIG_LIBS = +CONFIG_LIBS = -lrt CONFIG_FLAGS = all: $(TARGETS)
tracecmd/include/trace-local.h +16 -0
@@ -12,6 +12,8 @@#include "trace-cmd.h" #include "event-utils.h" +#define TRACE_AGENT_DEFAULT_PORT 823 +extern int debug;extern int quiet;
@@ -64,6 +66,8 @@ void trace_split(int argc, char **argv);void trace_listen (int argc,char **argv); +void trace_agent (int argc,char **argv); +void trace_restore (int argc,char **argv);void trace_clear (int argc,char **argv);
@@ -88,6 +92,10 @@ void trace_list(int argc, char **argv);void trace_usage (int argc,char **argv); +int trace_record_agent (struct tracecmd_msg_handle *msg_handle, +int cpus,int *fds, +int argc,char **argv); +struct hook_list ; void trace_init_profile(struct tracecmd_input *handle, struct hook_list *hooks,
@@ -176,6 +184,7 @@ struct buffer_instance {struct func_list *notrace_funcs;const char *clock; +unsigned int *client_ports;struct trace_seq *s_save;struct trace_seq *s_print;
@@ -190,6 +199,13 @@ struct buffer_instance {int tracing_on_fd;int buffer_size;int cpu_count; + +int argc; +char **argv; + +int cid; +int port; +int *fds; };extern struct buffer_instance top_instance;
tracecmd/trace-agent.c new file +225 -0
225 lines not on this page · raw message
tracecmd/trace-cmd.c +3 -0
@@ -83,6 +83,9 @@ struct command commands[] = { {"hist", trace_hist}, {"mem", trace_mem}, {"listen", trace_listen}, +#ifdef VSOCK + {"agent", trace_agent}, +#endif {"split", trace_split}, {"restore", trace_restore}, {"stack", trace_stack},
tracecmd/trace-record.c +558 -47
866 lines not on this page · raw message
tracecmd/trace-usage.c +12 -1
@@ -231,11 +231,22 @@ static struct usage_help usage_help[] = {"listen on a network socket for trace clients" ," %s listen -p port[-D][-o file][-d dir][-l logfile]\n" " Creates a socket to listen for clients.\n" -" -D create it in daemon mode.\n" +" -p port number to listen on.\n" +" -D run in daemon mode.\n" " -o file name to use for clients.\n" " -d directory to store client files.\n" " -l logfile to write messages to.\n" }, +#ifdef VSOCK + { +"agent" , +"listen on a vsocket for trace clients" , +" %s agent -p port[-D]\n" +" Creates a vsocket to listen for clients.\n" +" -p port number to listen on.\n" +" -D run in daemon mode.\n" + }, +#endif {"list" , "list the available events, plugins or options",
Signature · 1 line
2.19.1
Re: [RFC PATCH v6 05/11] trace-cmd: Add VM kernel tracing over vsockets transport
Quoted 39 lines · On Thu, 14 Feb 2019 16:13:29 +0200 Slavomir Kaslev <[email protected]> wrote:
void
start_threads(enum trace_type type, struct common_record_context *ctx){tracecmd/trace-record.c +13
-7struct buffer_instance *instance; -int *brass =NULL ;int total_cpu_count =0 ;int i =0 ;int ret; -for_all_instances (instance) +for_all_instances (instance) { +/* Start the connection now to find out how many CPUs we need */ +if (instance->flags &BUFFER_FL_GUEST ) +connect_to_agent (instance); total_cpu_count += instance->cpu_count; + }/* make a thread for every CPU we have */ - pids =malloc (sizeof (*pids) * total_cpu_count * (buffers +1 )); + pids =calloc (total_cpu_count * (buffers +1 ),sizeof (*pids));if (!pids) -die ("Failed to allocat pids for %d cpus" , total_cpu_count); - -memset (pids,0 ,sizeof (*pids) * total_cpu_count * (buffers +1 )); +die ("Failed to allocate pids for %d cpus" , total_cpu_count);for_all_instances (instance) { +int *brass =NULL ;int x, pid; -if (host) { +if (instance->flags &BUFFER_FL_AGENT ) { +setup_agent (instance, ctx); + }else if (instance->flags &BUFFER_FL_GUEST ) { +setup_guest (instance); + }else if (host) { instance->msg_handle =setup_connection (instance, ctx);if (!instance->msg_handle)die ("Failed to make connection" );
tracecmd/trace-record.c static void print_stat(struct buffer_instance *instance) · line 3448
Quoted 5 lines
tracecmd/trace-record.c +2
-0@@ -3139,13 +3444,14 @@ static void print_stat(struct buffer_instance *instance) {int cpu; +if (quiet) +return ;
Hmm, this looks unrelated to this patch, and looks like it should have been in a patch by itself. As a clean up.
Quoted 106 lines
tracecmd/trace-record.c +69
-5+if (!is_top_instance (instance)) -if (!quiet) -printf ("\nBuffer: %s\n\n" , instance->name); +printf ("\nBuffer: %s\n\n" , instance->name);for (cpu =0 ; cpu < instance->cpu_count; cpu++) -if (!quiet) -trace_seq_do_printf (&instance->s_print[cpu]); +trace_seq_do_printf (&instance->s_print[cpu]); }enum {@@ -3171,7 +3477,44 @@ static void add_options(struct tracecmd_output *handle, struct common_record_contracecmd_add_option (handle,TRACECMD_OPTION_TRACECLOCK ,0 ,NULL );add_option_hooks (handle);add_uname (handle); +} + +static void write_guest_file (struct buffer_instance *instance) +{ +struct tracecmd_output *handle; +int cpu_count = instance->cpu_count; +char *file; +char **temp_files; +int i, fd; + + file =get_guest_file (output_file, instance->name); + fd =open (file,O_RDWR ); +if (fd <0 ) +die ("error opening %s" , file); +put_temp_file (file); + + handle =tracecmd_get_output_handle_fd (fd); +if (!handle) +die ("error writing to %s" , file); + temp_files =malloc (sizeof (*temp_files) * cpu_count); +if (!temp_files) +die ("failed to allocate temp_files for %d cpus" , + cpu_count); + +for (i =0 ; i < cpu_count; i++) { + temp_files[i] =get_temp_file (instance, i); +if (!temp_files[i]) +die ("failed to allocate memory" ); + } + +if (tracecmd_write_cpu_data (handle, cpu_count, temp_files) <0 ) +die ("failed to write CPU data" ); +tracecmd_output_close (handle); + +for (i =0 ; i < cpu_count; i++) +put_temp_file (temp_files[i]); +free (temp_files); }static void record_data (struct common_record_context *ctx)@@ -3185,7 +3528,9 @@ static void record_data(struct common_record_context *ctx)int i;for_all_instances (instance) { -if (instance->msg_handle) +if (instance->flags &BUFFER_FL_GUEST ) +write_guest_file (instance); +else if (host && instance->msg_handle)finish_network (instance->msg_handle);else local = true;@@ -4404,6 +4749,7 @@ void trace_stop(int argc, char **argv) c =getopt (argc-1 , argv+1 ,"hatB:" );if (c ==-1 )break ; +switch (c) {case 'h' :usage (argv);@@ -4566,6 +4912,63 @@ static void init_common_record_context(struct common_record_context *ctx,#define IS_STREAM (ctx) ((ctx)->curr_cmd == CMD_stream)#define IS_PROFILE (ctx) ((ctx)->curr_cmd == CMD_profile)#define IS_RECORD (ctx) ((ctx)->curr_cmd == CMD_record) +#define IS_AGENT (ctx) ((ctx)->curr_cmd == CMD_record_agent) + +static void add_argv (struct buffer_instance *instance,char *arg,bool prepend) +{ + instance->argv =realloc (instance->argv, + (instance->argc +1 ) *sizeof (char *)); +if (!instance->argv) +die ("Can not allocate instance args" ); +if (prepend) { +memmove (instance->argv +1 , instance->argv, + instance->argc *sizeof (*instance->argv)); + instance->argv[0 ] = arg; + }else { + instance->argv[instance->argc] = arg; + } + instance->argc++; +} + +static void add_arg (struct buffer_instance *instance, +int c,const char *opts, +struct option *long_options,char *optarg) +{ +char *ptr; +char *arg; +int ret;
tracecmd/trace-record.c static void init_common_record_context(struct common_record_context *ctx, · line 4949
Quoted 10 lines
tracecmd/trace-record.c +10
-0+int i; + +/* Short or long arg */ +if (!(c &0x80 )) { + ret =asprintf (&arg,"-%c" , c); +if (ret <0 ) +die ("Can not allocate argument" ); + ptr =strstr (opts, arg+1 ); +if (!ptr) +return ;/* Not found? */
This leaks arg, as arg was allocated with asprintf(). Need a "free(arg)" here.
Quoted 259 lines
tracecmd/trace-record.c +137
-7+add_argv (instance, arg, false); +if (ptr[1 ] ==':' ) +add_argv (instance, optarg, false); +return ; + } +for (i =0 ; long_options[i].name; i++) { +if (c == long_options[i].val) { + ret =asprintf (&arg,"--%s" , long_options[i].name); +if (ret <0 ) +die ("Can not allocate argument" ); +add_argv (instance, arg, false); +if (long_options[i].has_arg) { + arg =strdup (optarg); +if (!arg) +die ("Can not allocate arguments" ); +add_argv (instance, arg, false); +return ; + } + } + } +/* Not found? */ +} static void parse_record_options(int argc, char **argv,@@ -4607,10 +5010,20 @@ static void parse_record_options(int argc,if (IS_EXTRACT (ctx)) opts ="+haf:Fp:co:O:sr:g:l:n:P:N:tb:B:ksiT" ;else - opts ="+hae:f:Fp:cC:dDGo:O:s:r:vg:l:n:P:N:tb:R:B:ksSiTm:M:H:q" ; + opts ="+hae:f:FA:p:cC:dDGo:O:s:r:vg:l:n:P:N:tb:R:B:ksSiTm:M:H:q" ; c =getopt_long (argc-1 , argv+1 , opts, long_options, &option_index);if (c ==-1 )break ; + +/* +* If the current instance is to record a guest, then save +* all the arguments for this instance. +*/ +if (c !='B' && c !='A' && ctx->instance->flags &BUFFER_FL_GUEST ) { +add_arg (ctx->instance, c, opts, long_options, optarg); +continue ; + } +switch (c) {case 'h' :usage (argv);@@ -4663,6 +5076,26 @@ static void parse_record_options(int argc,add_trigger (event, optarg);break ; +case 'A' : { +char *name =NULL ; +int cid =-1 , port =-1 ; + +if (!IS_RECORD (ctx)) +die ("-A is only allowed for record operations" ); + + name =parse_guest_name (optarg, &cid, &port); +if (!name || cid ==-1 ) +die ("guest %s not found" , optarg); +if (port ==-1 ) + port =TRACE_AGENT_DEFAULT_PORT ; + + ctx->instance =create_instance (name); + ctx->instance->flags |=BUFFER_FL_GUEST ; + ctx->instance->cid = cid; + ctx->instance->port = port; +add_instance (ctx->instance,0 ); +break ; + }case 'F' :test_set_event_pid (); filter_task =1 ;@@ -4733,6 +5166,8 @@ static void parse_record_options(int argc, ctx->disable =1 ;break ;case 'o' : +if (IS_AGENT (ctx)) +die ("-o incompatible with agent recording" );if (host)die ("-o incompatible with -N" );if (IS_START (ctx))@@ -4794,6 +5229,8 @@ static void parse_record_options(int argc,case 'N' :if (!IS_RECORD (ctx))die ("-N only available with record" ); +if (IS_AGENT (ctx)) +die ("-N incompatible with agent recording" );if (ctx->output)die ("-N incompatible with -o" ); host = optarg;@@ -4890,6 +5327,16 @@ static void parse_record_options(int argc, } } +/* If --date is specified, prepend it to all guest VM flags */ +if (ctx->date) { +struct buffer_instance *instance; + +for_all_instances (instance) { +if (instance->flags &BUFFER_FL_GUEST ) +add_argv (instance,"--date" , true); + } + } +if (!ctx->filtered && ctx->instance->filter_mod)add_func (&ctx->instance->filter_funcs, ctx->instance->filter_mod,"*" );@@ -4920,7 +5367,8 @@ static enum trace_type get_trace_cmd_type(enum trace_cmd cmd) {CMD_stream, TRACE_TYPE_STREAM}, {CMD_extract, TRACE_TYPE_EXTRACT}, {CMD_profile, TRACE_TYPE_STREAM}, - {CMD_start, TRACE_TYPE_START} + {CMD_start, TRACE_TYPE_START}, + {CMD_record_agent, TRACE_TYPE_RECORD} };for (int i =0 ; i <ARRAY_SIZE (trace_type_per_command); i++) {@@ -4952,12 +5400,28 @@ static void finalize_record_trace(struct common_record_context *ctx)if (instance->flags &BUFFER_FL_KEEP )write_tracing_on (instance, instance->tracing_on_init_val); +if (instance->flags &BUFFER_FL_AGENT ) +tracecmd_output_close (instance->network_handle); }if (host)tracecmd_output_close (ctx->instance->network_handle); } +static bool has_local_instances (void ) +{ +struct buffer_instance *instance; + +for_all_instances (instance) { +if (instance->flags &BUFFER_FL_GUEST ) +continue ; +if (host && instance->msg_handle) +continue ; +return true; + } +return false; +} +/* * This function contains common code for the following commands: * record, start, stream, profile. @@ -4986,7 +5450,6 @@ static void record_trace(int argc, char **argv,/* Save the state of tracing_on before starting */ for_all_instances (instance) { -if (!ctx->manual && instance->flags &BUFFER_FL_PROFILE )enable_profile (instance);@@ -5003,14 +5466,16 @@ static void record_trace(int argc, char **argv, page_size =getpagesize (); - fset =set_ftrace (!ctx->disable, ctx->total_disable); +if (!(ctx->instance->flags &BUFFER_FL_GUEST )) + fset =set_ftrace (!ctx->disable, ctx->total_disable);tracecmd_disable_all_tracing (1 );for_all_instances (instance)set_clock (instance);/* Record records the date first */ -if (IS_RECORD (ctx) && ctx->date) +if (ctx->date && + ((IS_RECORD (ctx) &&has_local_instances ()) ||IS_AGENT (ctx))) ctx->date2ts =get_date_to_ts ();for_all_instances (instance) {@@ -5045,9 +5510,13 @@ static void record_trace(int argc, char **argv,exit (0 ); } -if (ctx->run_command) +if (ctx->run_command) {run_cmd (type, (argc - optind) -1 , &argv[optind +1 ]); -else { + }else if (ctx->instance && (ctx->instance->flags &BUFFER_FL_AGENT )) { +update_task_filter (); +tracecmd_enable_tracing (); +tracecmd_msg_wait_close (ctx->instance->msg_handle); + }else {update_task_filter ();tracecmd_enable_tracing ();/* We don't ptrace ourself */ @@ -5057,6 +5526,8 @@ static void record_trace(int argc, char **argv,printf ("Hit Ctrl^C to stop recording\n" );while (!finished)trace_or_sleep (type); + +tell_guests_to_stop (); }tracecmd_disable_tracing ();@@ -5068,6 +5539,9 @@ static void record_trace(int argc, char **argv,if (!keep)tracecmd_disable_all_tracing (0 ); +if (!latency) +wait_threads (); +if (IS_RECORD (ctx)) {record_data (ctx);delete_thread_data ();@@ -5202,3 +5676,40 @@ void trace_record(int argc, char **argv)record_trace (argc, argv, &ctx);exit (0 ); } + +int trace_record_agent (struct tracecmd_msg_handle *msg_handle, +int cpus,int *fds, +int argc,char **argv) +{ +struct common_record_context ctx; +char **argv_plus; + +/* Reset optind for getopt_long */ + optind =1 ; +/* +* argc is the number of elements in argv, but we need to convert +* argc and argv into "trace-cmd", "record", argv. +* where argc needs to grow by two. +*/ + argv_plus =calloc (argc +2 ,sizeof (char *)); +if (!argv_plus) +return -ENOMEM ; + + argv_plus[0 ] ="trace-cmd" ; + argv_plus[1 ] ="record" ; +memmove (argv_plus +2 , argv, argc *sizeof (char *)); + argc +=2 ; + +parse_record_options (argc, argv_plus, CMD_record_agent, &ctx); +if (ctx.run_command) +return -EINVAL ; + + ctx.instance->fds = fds; + ctx.instance->flags |=BUFFER_FL_AGENT ; + ctx.instance->msg_handle = msg_handle; + msg_handle->version =V3_PROTOCOL ; +record_trace (argc, argv, &ctx); + +free (argv_plus); +return 0 ; +}tracecmd/trace-usage.c +0
-0@@ -231,11 +231,22 @@ static struct usage_help usage_help[] = {"listen on a network socket for trace clients" ," %s listen -p port[-D][-o file][-d dir][-l logfile]\n" " Creates a socket to listen for clients.\n"
tracecmd/trace-usage.c static struct usage_help usage_help[] = { · line 242
Quoted 10 lines
tracecmd/trace-usage.c +5
-1-" -D create it in daemon mode.\n" +" -p port number to listen on.\n" +" -D run in daemon mode.\n" " -o file name to use for clients.\n" " -d directory to store client files.\n" " -l logfile to write messages to.\n" }, +#ifdef VSOCK + { + "agent",
In the future, it would be nice to allow agents to work with networks as well (in case vsock isn't available, but also across machines, where we would do a p2p to synchronize time stamps.
-- Steve
Quoted 10 lines
tracecmd/trace-usage.c +7
-0+ "listen on a vsocket for trace clients", + " %s agent -p port[-D]\n" + " Creates a vsocket to listen for clients.\n" + " -p port number to listen on.\n" + " -D run in daemon mode.\n" + }, +#endif { "list", "list the available events, plugins or options",
Re: [RFC PATCH v6 05/11] trace-cmd: Add VM kernel tracing over vsockets transport
Quoted 45 lines · Steven Rostedt
Quoted 42 lines · On Thu, 14 Feb 2019 16:13:29 +0200 Slavomir Kaslev <[email protected]> wrote:
void
start_threads(enum trace_type type, struct common_record_context *ctx){tracecmd/trace-record.c +13
-7struct buffer_instance *instance; -int *brass =NULL ;int total_cpu_count =0 ;int i =0 ;int ret; -for_all_instances (instance) +for_all_instances (instance) { +/* Start the connection now to find out how many CPUs we need */ +if (instance->flags &BUFFER_FL_GUEST ) +connect_to_agent (instance); total_cpu_count += instance->cpu_count; + }/* make a thread for every CPU we have */ - pids =malloc (sizeof (*pids) * total_cpu_count * (buffers +1 )); + pids =calloc (total_cpu_count * (buffers +1 ),sizeof (*pids));if (!pids) -die ("Failed to allocat pids for %d cpus" , total_cpu_count); - -memset (pids,0 ,sizeof (*pids) * total_cpu_count * (buffers +1 )); +die ("Failed to allocate pids for %d cpus" , total_cpu_count);for_all_instances (instance) { +int *brass =NULL ;int x, pid; -if (host) { +if (instance->flags &BUFFER_FL_AGENT ) { +setup_agent (instance, ctx); + }else if (instance->flags &BUFFER_FL_GUEST ) { +setup_guest (instance); + }else if (host) { instance->msg_handle =setup_connection (instance, ctx);if (!instance->msg_handle)die ("Failed to make connection" );@@ -3139,13 +3444,14 @@ static void print_stat(struct buffer_instance *instance) {int cpu;
Quoted 5 lines
tracecmd/trace-record.c static void print_stat(struct buffer_instance *instance) · line 3448
Quoted 2 lines
tracecmd/trace-record.c +2
-0+if (quiet) +return ;Hmm, this looks unrelated to this patch, and looks like it should have been in a patch by itself. As a clean up.
OK, I split two such unrelated cleanups as separate patches at the beginning of the next iteration so that they can be merged independently of the remaining vsockets work.
Quoted 409 lines
Quoted 107 lines
tracecmd/trace-record.c +69
-5+if (!is_top_instance (instance)) -if (!quiet) -printf ("\nBuffer: %s\n\n" , instance->name); +printf ("\nBuffer: %s\n\n" , instance->name);for (cpu =0 ; cpu < instance->cpu_count; cpu++) -if (!quiet) -trace_seq_do_printf (&instance->s_print[cpu]); +trace_seq_do_printf (&instance->s_print[cpu]); }enum {@@ -3171,7 +3477,44 @@ static void add_options(struct tracecmd_output *handle, struct common_record_contracecmd_add_option (handle,TRACECMD_OPTION_TRACECLOCK ,0 ,NULL );add_option_hooks (handle);add_uname (handle); +} + +static void write_guest_file (struct buffer_instance *instance) +{ +struct tracecmd_output *handle; +int cpu_count = instance->cpu_count; +char *file; +char **temp_files; +int i, fd; + + file =get_guest_file (output_file, instance->name); + fd =open (file,O_RDWR ); +if (fd <0 ) +die ("error opening %s" , file); +put_temp_file (file); + + handle =tracecmd_get_output_handle_fd (fd); +if (!handle) +die ("error writing to %s" , file); + temp_files =malloc (sizeof (*temp_files) * cpu_count); +if (!temp_files) +die ("failed to allocate temp_files for %d cpus" , + cpu_count); + +for (i =0 ; i < cpu_count; i++) { + temp_files[i] =get_temp_file (instance, i); +if (!temp_files[i]) +die ("failed to allocate memory" ); + } + +if (tracecmd_write_cpu_data (handle, cpu_count, temp_files) <0 ) +die ("failed to write CPU data" ); +tracecmd_output_close (handle); + +for (i =0 ; i < cpu_count; i++) +put_temp_file (temp_files[i]); +free (temp_files); }static void record_data (struct common_record_context *ctx)@@ -3185,7 +3528,9 @@ static void record_data(struct common_record_context *ctx)int i;for_all_instances (instance) { -if (instance->msg_handle) +if (instance->flags &BUFFER_FL_GUEST ) +write_guest_file (instance); +else if (host && instance->msg_handle)finish_network (instance->msg_handle);else local = true;@@ -4404,6 +4749,7 @@ void trace_stop(int argc, char **argv) c =getopt (argc-1 , argv+1 ,"hatB:" );if (c ==-1 )break ; +switch (c) {case 'h' :usage (argv);@@ -4566,6 +4912,63 @@ static void init_common_record_context(struct common_record_context *ctx,#define IS_STREAM (ctx) ((ctx)->curr_cmd == CMD_stream)#define IS_PROFILE (ctx) ((ctx)->curr_cmd == CMD_profile)#define IS_RECORD (ctx) ((ctx)->curr_cmd == CMD_record) +#define IS_AGENT (ctx) ((ctx)->curr_cmd == CMD_record_agent) + +static void add_argv (struct buffer_instance *instance,char *arg,bool prepend) +{ + instance->argv =realloc (instance->argv, + (instance->argc +1 ) *sizeof (char *)); +if (!instance->argv) +die ("Can not allocate instance args" ); +if (prepend) { +memmove (instance->argv +1 , instance->argv, + instance->argc *sizeof (*instance->argv)); + instance->argv[0 ] = arg; + }else { + instance->argv[instance->argc] = arg; + } + instance->argc++; +} + +static void add_arg (struct buffer_instance *instance, +int c,const char *opts, +struct option *long_options,char *optarg) +{ +char *ptr; +char *arg; +int ret;tracecmd/trace-record.c static void init_common_record_context(struct common_record_context *ctx, · line 4949
Quoted 10 lines
tracecmd/trace-record.c +10
-0+int i; + +/* Short or long arg */ +if (!(c &0x80 )) { + ret =asprintf (&arg,"-%c" , c); +if (ret <0 ) +die ("Can not allocate argument" ); + ptr =strstr (opts, arg+1 ); +if (!ptr) +return ;/* Not found? */ This leaks arg, as arg was allocated with
asprintf(). Need a "free(arg)" here.Quoted 259 lines
tracecmd/trace-record.c +137
-7+add_argv (instance, arg, false); +if (ptr[1 ] ==':' ) +add_argv (instance, optarg, false); +return ; + } +for (i =0 ; long_options[i].name; i++) { +if (c == long_options[i].val) { + ret =asprintf (&arg,"--%s" , long_options[i].name); +if (ret <0 ) +die ("Can not allocate argument" ); +add_argv (instance, arg, false); +if (long_options[i].has_arg) { + arg =strdup (optarg); +if (!arg) +die ("Can not allocate arguments" ); +add_argv (instance, arg, false); +return ; + } + } + } +/* Not found? */ +} static void parse_record_options(int argc, char **argv,@@ -4607,10 +5010,20 @@ static void parse_record_options(int argc,if (IS_EXTRACT (ctx)) opts ="+haf:Fp:co:O:sr:g:l:n:P:N:tb:B:ksiT" ;else - opts ="+hae:f:Fp:cC:dDGo:O:s:r:vg:l:n:P:N:tb:R:B:ksSiTm:M:H:q" ; + opts ="+hae:f:FA:p:cC:dDGo:O:s:r:vg:l:n:P:N:tb:R:B:ksSiTm:M:H:q" ; c =getopt_long (argc-1 , argv+1 , opts, long_options, &option_index);if (c ==-1 )break ; + +/* +* If the current instance is to record a guest, then save +* all the arguments for this instance. +*/ +if (c !='B' && c !='A' && ctx->instance->flags &BUFFER_FL_GUEST ) { +add_arg (ctx->instance, c, opts, long_options, optarg); +continue ; + } +switch (c) {case 'h' :usage (argv);@@ -4663,6 +5076,26 @@ static void parse_record_options(int argc,add_trigger (event, optarg);break ; +case 'A' : { +char *name =NULL ; +int cid =-1 , port =-1 ; + +if (!IS_RECORD (ctx)) +die ("-A is only allowed for record operations" ); + + name =parse_guest_name (optarg, &cid, &port); +if (!name || cid ==-1 ) +die ("guest %s not found" , optarg); +if (port ==-1 ) + port =TRACE_AGENT_DEFAULT_PORT ; + + ctx->instance =create_instance (name); + ctx->instance->flags |=BUFFER_FL_GUEST ; + ctx->instance->cid = cid; + ctx->instance->port = port; +add_instance (ctx->instance,0 ); +break ; + }case 'F' :test_set_event_pid (); filter_task =1 ;@@ -4733,6 +5166,8 @@ static void parse_record_options(int argc, ctx->disable =1 ;break ;case 'o' : +if (IS_AGENT (ctx)) +die ("-o incompatible with agent recording" );if (host)die ("-o incompatible with -N" );if (IS_START (ctx))@@ -4794,6 +5229,8 @@ static void parse_record_options(int argc,case 'N' :if (!IS_RECORD (ctx))die ("-N only available with record" ); +if (IS_AGENT (ctx)) +die ("-N incompatible with agent recording" );if (ctx->output)die ("-N incompatible with -o" ); host = optarg;@@ -4890,6 +5327,16 @@ static void parse_record_options(int argc, } } +/* If --date is specified, prepend it to all guest VM flags */ +if (ctx->date) { +struct buffer_instance *instance; + +for_all_instances (instance) { +if (instance->flags &BUFFER_FL_GUEST ) +add_argv (instance,"--date" , true); + } + } +if (!ctx->filtered && ctx->instance->filter_mod)add_func (&ctx->instance->filter_funcs, ctx->instance->filter_mod,"*" );@@ -4920,7 +5367,8 @@ static enum trace_type get_trace_cmd_type(enum trace_cmd cmd) {CMD_stream, TRACE_TYPE_STREAM}, {CMD_extract, TRACE_TYPE_EXTRACT}, {CMD_profile, TRACE_TYPE_STREAM}, - {CMD_start, TRACE_TYPE_START} + {CMD_start, TRACE_TYPE_START}, + {CMD_record_agent, TRACE_TYPE_RECORD} };for (int i =0 ; i <ARRAY_SIZE (trace_type_per_command); i++) {@@ -4952,12 +5400,28 @@ static void finalize_record_trace(struct common_record_context *ctx)if (instance->flags &BUFFER_FL_KEEP )write_tracing_on (instance, instance->tracing_on_init_val); +if (instance->flags &BUFFER_FL_AGENT ) +tracecmd_output_close (instance->network_handle); }if (host)tracecmd_output_close (ctx->instance->network_handle); } +static bool has_local_instances (void ) +{ +struct buffer_instance *instance; + +for_all_instances (instance) { +if (instance->flags &BUFFER_FL_GUEST ) +continue ; +if (host && instance->msg_handle) +continue ; +return true; + } +return false; +} +/* * This function contains common code for the following commands: * record, start, stream, profile. @@ -4986,7 +5450,6 @@ static void record_trace(int argc, char **argv,/* Save the state of tracing_on before starting */ for_all_instances (instance) { -if (!ctx->manual && instance->flags &BUFFER_FL_PROFILE )enable_profile (instance);@@ -5003,14 +5466,16 @@ static void record_trace(int argc, char **argv, page_size =getpagesize (); - fset =set_ftrace (!ctx->disable, ctx->total_disable); +if (!(ctx->instance->flags &BUFFER_FL_GUEST )) + fset =set_ftrace (!ctx->disable, ctx->total_disable);tracecmd_disable_all_tracing (1 );for_all_instances (instance)set_clock (instance);/* Record records the date first */ -if (IS_RECORD (ctx) && ctx->date) +if (ctx->date && + ((IS_RECORD (ctx) &&has_local_instances ()) ||IS_AGENT (ctx))) ctx->date2ts =get_date_to_ts ();for_all_instances (instance) {@@ -5045,9 +5510,13 @@ static void record_trace(int argc, char **argv,exit (0 ); } -if (ctx->run_command) +if (ctx->run_command) {run_cmd (type, (argc - optind) -1 , &argv[optind +1 ]); -else { + }else if (ctx->instance && (ctx->instance->flags &BUFFER_FL_AGENT )) { +update_task_filter (); +tracecmd_enable_tracing (); +tracecmd_msg_wait_close (ctx->instance->msg_handle); + }else {update_task_filter ();tracecmd_enable_tracing ();/* We don't ptrace ourself */ @@ -5057,6 +5526,8 @@ static void record_trace(int argc, char **argv,printf ("Hit Ctrl^C to stop recording\n" );while (!finished)trace_or_sleep (type); + +tell_guests_to_stop (); }tracecmd_disable_tracing ();@@ -5068,6 +5539,9 @@ static void record_trace(int argc, char **argv,if (!keep)tracecmd_disable_all_tracing (0 ); +if (!latency) +wait_threads (); +if (IS_RECORD (ctx)) {record_data (ctx);delete_thread_data ();@@ -5202,3 +5676,40 @@ void trace_record(int argc, char **argv)record_trace (argc, argv, &ctx);exit (0 ); } + +int trace_record_agent (struct tracecmd_msg_handle *msg_handle, +int cpus,int *fds, +int argc,char **argv) +{ +struct common_record_context ctx; +char **argv_plus; + +/* Reset optind for getopt_long */ + optind =1 ; +/* +* argc is the number of elements in argv, but we need to convert +* argc and argv into "trace-cmd", "record", argv. +* where argc needs to grow by two. +*/ + argv_plus =calloc (argc +2 ,sizeof (char *)); +if (!argv_plus) +return -ENOMEM ; + + argv_plus[0 ] ="trace-cmd" ; + argv_plus[1 ] ="record" ; +memmove (argv_plus +2 , argv, argc *sizeof (char *)); + argc +=2 ; + +parse_record_options (argc, argv_plus, CMD_record_agent, &ctx); +if (ctx.run_command) +return -EINVAL ; + + ctx.instance->fds = fds; + ctx.instance->flags |=BUFFER_FL_AGENT ; + ctx.instance->msg_handle = msg_handle; + msg_handle->version =V3_PROTOCOL ; +record_trace (argc, argv, &ctx); + +free (argv_plus); +return 0 ; +}tracecmd/trace-usage.c +0
-0@@ -231,11 +231,22 @@ static struct usage_help usage_help[] = {"listen on a network socket for trace clients" ," %s listen -p port[-D][-o file][-d dir][-l logfile]\n" " Creates a socket to listen for clients.\n" tracecmd/trace-usage.c static struct usage_help usage_help[] = { · line 242
Quoted 10 lines
tracecmd/trace-usage.c +5
-1-" -D create it in daemon mode.\n" +" -p port number to listen on.\n" +" -D run in daemon mode.\n" " -o file name to use for clients.\n" " -d directory to store client files.\n" " -l logfile to write messages to.\n" }, +#ifdef VSOCK + { + "agent",In the future, it would be nice to allow agents to work with networks as well (in case vsock isn't available, but also across machines, where we would do a p2p to synchronize time stamps.
-- Steve
Quoted 10 lines
tracecmd/trace-usage.c +7
-0+ "listen on a vsocket for trace clients", + " %s agent -p port[-D]\n" + " Creates a vsocket to listen for clients.\n" + " -p port number to listen on.\n" + " -D run in daemon mode.\n" + }, +#endif { "list", "list the available events, plugins or options",
Re: [RFC PATCH v6 05/11] trace-cmd: Add VM kernel tracing over vsockets transport
In reply to Steven RostedtQuoted 162 lines · Steven Rostedt
Quoted 39 lines · On Thu, 14 Feb 2019 16:13:29 +0200 Slavomir Kaslev <[email protected]> wrote:
void
start_threads(enum trace_type type, struct common_record_context *ctx){tracecmd/trace-record.c +13
-7struct buffer_instance *instance; -int *brass =NULL ;int total_cpu_count =0 ;int i =0 ;int ret; -for_all_instances (instance) +for_all_instances (instance) { +/* Start the connection now to find out how many CPUs we need */ +if (instance->flags &BUFFER_FL_GUEST ) +connect_to_agent (instance); total_cpu_count += instance->cpu_count; + }/* make a thread for every CPU we have */ - pids =malloc (sizeof (*pids) * total_cpu_count * (buffers +1 )); + pids =calloc (total_cpu_count * (buffers +1 ),sizeof (*pids));if (!pids) -die ("Failed to allocat pids for %d cpus" , total_cpu_count); - -memset (pids,0 ,sizeof (*pids) * total_cpu_count * (buffers +1 )); +die ("Failed to allocate pids for %d cpus" , total_cpu_count);for_all_instances (instance) { +int *brass =NULL ;int x, pid; -if (host) { +if (instance->flags &BUFFER_FL_AGENT ) { +setup_agent (instance, ctx); + }else if (instance->flags &BUFFER_FL_GUEST ) { +setup_guest (instance); + }else if (host) { instance->msg_handle =setup_connection (instance, ctx);if (!instance->msg_handle)die ("Failed to make connection" );tracecmd/trace-record.c static void print_stat(struct buffer_instance *instance) · line 3448
Quoted 5 lines
tracecmd/trace-record.c +2
-0@@ -3139,13 +3444,14 @@ static void print_stat(struct buffer_instance *instance) {int cpu; +if (quiet) +return ;Hmm, this looks unrelated to this patch, and looks like it should have been in a patch by itself. As a clean up.
Quoted 111 lines
tracecmd/trace-record.c +74
-5+if (!is_top_instance (instance)) -if (!quiet) -printf ("\nBuffer: %s\n\n" , instance->name); +printf ("\nBuffer: %s\n\n" , instance->name);for (cpu =0 ; cpu < instance->cpu_count; cpu++) -if (!quiet) -trace_seq_do_printf (&instance->s_print[cpu]); +trace_seq_do_printf (&instance->s_print[cpu]); }enum {@@ -3171,7 +3477,44 @@ static void add_options(struct tracecmd_output *handle, struct common_record_contracecmd_add_option (handle,TRACECMD_OPTION_TRACECLOCK ,0 ,NULL );add_option_hooks (handle);add_uname (handle); +} + +static void write_guest_file (struct buffer_instance *instance) +{ +struct tracecmd_output *handle; +int cpu_count = instance->cpu_count; +char *file; +char **temp_files; +int i, fd; + + file =get_guest_file (output_file, instance->name); + fd =open (file,O_RDWR ); +if (fd <0 ) +die ("error opening %s" , file); +put_temp_file (file); + + handle =tracecmd_get_output_handle_fd (fd); +if (!handle) +die ("error writing to %s" , file); + temp_files =malloc (sizeof (*temp_files) * cpu_count); +if (!temp_files) +die ("failed to allocate temp_files for %d cpus" , + cpu_count); + +for (i =0 ; i < cpu_count; i++) { + temp_files[i] =get_temp_file (instance, i); +if (!temp_files[i]) +die ("failed to allocate memory" ); + } + +if (tracecmd_write_cpu_data (handle, cpu_count, temp_files) <0 ) +die ("failed to write CPU data" ); +tracecmd_output_close (handle); + +for (i =0 ; i < cpu_count; i++) +put_temp_file (temp_files[i]); +free (temp_files); }static void record_data (struct common_record_context *ctx)@@ -3185,7 +3528,9 @@ static void record_data(struct common_record_context *ctx)int i;for_all_instances (instance) { -if (instance->msg_handle) +if (instance->flags &BUFFER_FL_GUEST ) +write_guest_file (instance); +else if (host && instance->msg_handle)finish_network (instance->msg_handle);else local = true;@@ -4404,6 +4749,7 @@ void trace_stop(int argc, char **argv) c =getopt (argc-1 , argv+1 ,"hatB:" );if (c ==-1 )break ; +switch (c) {case 'h' :usage (argv);@@ -4566,6 +4912,63 @@ static void init_common_record_context(struct common_record_context *ctx,#define IS_STREAM (ctx) ((ctx)->curr_cmd == CMD_stream)#define IS_PROFILE (ctx) ((ctx)->curr_cmd == CMD_profile)#define IS_RECORD (ctx) ((ctx)->curr_cmd == CMD_record) +#define IS_AGENT (ctx) ((ctx)->curr_cmd == CMD_record_agent) + +static void add_argv (struct buffer_instance *instance,char *arg,bool prepend) +{ + instance->argv =realloc (instance->argv, + (instance->argc +1 ) *sizeof (char *)); +if (!instance->argv) +die ("Can not allocate instance args" ); +if (prepend) { +memmove (instance->argv +1 , instance->argv, + instance->argc *sizeof (*instance->argv)); + instance->argv[0 ] = arg; + }else { + instance->argv[instance->argc] = arg; + } + instance->argc++; +} + +static void add_arg (struct buffer_instance *instance, +int c,const char *opts, +struct option *long_options,char *optarg) +{ +char *ptr; +char *arg; +int ret; +int i; + +/* Short or long arg */ +if (!(c &0x80 )) { + ret =asprintf (&arg,"-%c" , c);
Quoted 8 lines
tracecmd/trace-record.c static void init_common_record_context(struct common_record_context *ctx, · line 4949
Quoted 5 lines
tracecmd/trace-record.c +5
-0+if (ret <0 ) +die ("Can not allocate argument" ); + ptr =strstr (opts, arg+1 ); +if (!ptr) +return ;/* Not found? */ This leaks arg, as arg was allocated with
asprintf(). Need a "free(arg)" here.
Bah! Another left over from the VIRT-SERVER branch. Reorganized this function for the next iteration.
RFC PATCH v6 6/11 trace-cmd: Use splice(2) for vsockets if available
In reply to Slavomir KaslevDetect if splice(2) reading is supported for vsockets (Linux 4.20 and later) and use it, or fallback to read/write-ing otherwise.
--- lib/trace-cmd/trace-recorder.c | 6 ++-- tracecmd/trace-record.c | 56 ++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-)
lib/trace-cmd/trace-recorder.c +2 -4
@@ -386,10 +386,8 @@ static long splice_data(struct tracecmd_recorder *recorder) ret =splice (recorder->brass[0 ],NULL , recorder->fd,NULL , read, recorder->fd_flags);if (ret <0 ) { -if (errno !=EAGAIN && errno !=EINTR ) { -warning ("recorder error in splice output" ); -return -1 ; - } +if (errno !=EAGAIN && errno !=EINTR ) +return ret;return total_read; }else update_fd (recorder, ret);
tracecmd/trace-record.c +54 -2
79 lines not on this page · raw message
Signature · 1 line
2.19.1
RFC PATCH v6 7/11 trace-cmd: Add `trace-cmd setup-guest` command
In reply to Slavomir KaslevAdd trace-cmd setup-guest command that creates the necessary FIFOs for tracing a guest over FIFOs instead of vsockets.
--- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 6 ++ tracecmd/trace-cmd.c | 1 + tracecmd/trace-setup-guest.c | 186 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 8 ++ 5 files changed, 202 insertions(+) create mode 100644 tracecmd/trace-setup-guest.c
tracecmd/Makefile +1 -0
@@ -35,6 +35,7 @@ TRACE_CMD_OBJS += trace-msg.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o +TRACE_CMD_OBJS += trace-setup-guest.o endif ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o)
tracecmd/include/trace-local.h +6 -0
@@ -14,6 +14,10 @@#define TRACE_AGENT_DEFAULT_PORT 823 +#define GUEST_PIPE_NAME "trace-pipe-cpu" +#define GUEST_DIR_FMT "/var/lib/trace-cmd/virt/%s" +#define GUEST_FIFO_FMT GUEST_DIR_FMT "/" GUEST_PIPE_NAME "%d" +extern int debug;extern int quiet;
@@ -68,6 +72,8 @@ void trace_listen(int argc, char **argv);void trace_agent (int argc,char **argv); +void trace_setup_guest (int argc,char **argv); +void trace_restore (int argc,char **argv);void trace_clear (int argc,char **argv);
tracecmd/trace-cmd.c +1 -0
@@ -85,6 +85,7 @@ struct command commands[] = { {"listen", trace_listen}, #ifdef VSOCK {"agent", trace_agent}, + {"setup-guest", trace_setup_guest}, #endif {"split", trace_split}, {"restore", trace_restore},
tracecmd/trace-setup-guest.c new file +186 -0
186 lines not on this page · raw message
tracecmd/trace-usage.c +8 -0
@@ -246,6 +246,14 @@ static struct usage_help usage_help[] = { " -p port number to listen on.\n" " -D run in daemon mode.\n" }, + { + "setup-guest", + "create FIFOs for tracing guest VMs", + " %s setup-guest -c cpus[-p perm][-g group] guest\n" + " -c number of guest virtual CPUs\n" + " -p FIFOs permissions (default: 0660)\n" + " -g FIFOs group owner\n" + }, #endif { "list",
Signature · 1 line
2.19.1
Re: [RFC PATCH v6 07/11] trace-cmd: Add `trace-cmd setup-guest` command
Quoted 87 lines · On Thu, 14 Feb 2019 16:13:31 +0200 Slavomir Kaslev <[email protected]> wrote:
Add
trace-cmd setup-guestcommand that creates the necessary FIFOs for tracing a guest over FIFOs instead of vsockets.Signed-off-by Slavomir Kaslev <[email protected]>--- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 6 ++ tracecmd/trace-cmd.c | 1 + tracecmd/trace-setup-guest.c | 186 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 8 ++ 5 files changed, 202 insertions(+) create mode 100644 tracecmd/trace-setup-guest.ctracecmd/Makefile +1
-0@@ -35,6 +35,7 @@ TRACE_CMD_OBJS += trace-msg.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o +TRACE_CMD_OBJS += trace-setup-guest.o endif ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o)tracecmd/include/trace-local.h +6
-0@@ -14,6 +14,10 @@#define TRACE_AGENT_DEFAULT_PORT 823 +#define GUEST_PIPE_NAME "trace-pipe-cpu" +#define GUEST_DIR_FMT "/var/lib/trace-cmd/virt/%s" +#define GUEST_FIFO_FMT GUEST_DIR_FMT "/" GUEST_PIPE_NAME "%d" +extern int debug;extern int quiet;@@ -68,6 +72,8 @@ void trace_listen(int argc, char **argv);void trace_agent (int argc,char **argv); +void trace_setup_guest (int argc,char **argv); +void trace_restore (int argc,char **argv);void trace_clear (int argc,char **argv);tracecmd/trace-cmd.c +1
-0@@ -85,6 +85,7 @@ struct command commands[] = { {"listen", trace_listen}, #ifdef VSOCK {"agent", trace_agent}, + {"setup-guest", trace_setup_guest}, #endif {"split", trace_split}, {"restore", trace_restore},tracecmd/trace-setup-guest.c new file +20
-0@@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 VMware Inc, Slavomir Kaslev <[email protected]> +* +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <grp.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "trace-local.h" +#include "trace-msg.h" + +static int make_dir (const char *path,mode_t mode)
tracecmd/trace-setup-guest.c line 30
Quoted 10 lines
tracecmd/trace-setup-guest.c new file +10
-0+{ +char *buf, *end, *p; +int ret =0 ; + + buf =strdup (path); + end = buf +strlen (buf); + +for (p = buf; p < end; p++) { +for (; p < end && *p =='/' ; p++); +for (; p < end && *p !='/' ; p++);
Why not:
for (p = buf; p && *p; p++) {
for (; *p == '/'; p++);
p = index(p, '/');
if (p)
*p = '\0';tracecmd/trace-setup-guest.c line 40
Quoted 10 lines
tracecmd/trace-setup-guest.c new file +10
-0+ + *p ='\0' ; + ret =mkdir (buf, mode); +if (ret <0 ) { +if (errno !=EEXIST ) { + ret = -errno; +break ; + } + ret =0 ; + }
if (p) *p = '/';
?
No need for "end".
-- Steve
Quoted 7 lines
tracecmd/trace-setup-guest.c new file +7
-0+ *p ='/' ; + } + +free (buf); +return ret; +} +
Re: [RFC PATCH v6 07/11] trace-cmd: Add `trace-cmd setup-guest` command
Quoted 130 lines · Steven Rostedt
Quoted 87 lines · On Thu, 14 Feb 2019 16:13:31 +0200 Slavomir Kaslev <[email protected]> wrote:
Add
trace-cmd setup-guestcommand that creates the necessary FIFOs for tracing a guest over FIFOs instead of vsockets.Signed-off-by Slavomir Kaslev <[email protected]>--- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 6 ++ tracecmd/trace-cmd.c | 1 + tracecmd/trace-setup-guest.c | 186 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 8 ++ 5 files changed, 202 insertions(+) create mode 100644 tracecmd/trace-setup-guest.ctracecmd/Makefile +1
-0@@ -35,6 +35,7 @@ TRACE_CMD_OBJS += trace-msg.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o +TRACE_CMD_OBJS += trace-setup-guest.o endif ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o)tracecmd/include/trace-local.h +6
-0@@ -14,6 +14,10 @@#define TRACE_AGENT_DEFAULT_PORT 823 +#define GUEST_PIPE_NAME "trace-pipe-cpu" +#define GUEST_DIR_FMT "/var/lib/trace-cmd/virt/%s" +#define GUEST_FIFO_FMT GUEST_DIR_FMT "/" GUEST_PIPE_NAME "%d" +extern int debug;extern int quiet;@@ -68,6 +72,8 @@ void trace_listen(int argc, char **argv);void trace_agent (int argc,char **argv); +void trace_setup_guest (int argc,char **argv); +void trace_restore (int argc,char **argv);void trace_clear (int argc,char **argv);tracecmd/trace-cmd.c +1
-0@@ -85,6 +85,7 @@ struct command commands[] = { {"listen", trace_listen}, #ifdef VSOCK {"agent", trace_agent}, + {"setup-guest", trace_setup_guest}, #endif {"split", trace_split}, {"restore", trace_restore},tracecmd/trace-setup-guest.c new file +20
-0@@ -0,0 +1,186 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2019 VMware Inc, Slavomir Kaslev <[email protected]> +* +*/ + +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <grp.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "trace-local.h" +#include "trace-msg.h" + +static int make_dir (const char *path,mode_t mode)tracecmd/trace-setup-guest.c line 30
Quoted 10 lines
tracecmd/trace-setup-guest.c new file +10
-0+{ +char *buf, *end, *p; +int ret =0 ; + + buf =strdup (path); + end = buf +strlen (buf); + +for (p = buf; p < end; p++) { +for (; p < end && *p =='/' ; p++); +for (; p < end && *p !='/' ; p++);Why not:
for (p = buf; p && *p; p++) { for (; *p == '/'; p++); p = index(p, '/'); if (p) *p = '\0';tracecmd/trace-setup-guest.c line 40
Quoted 10 lines
tracecmd/trace-setup-guest.c new file +10
-0+ + *p ='\0' ; + ret =mkdir (buf, mode); +if (ret <0 ) { +if (errno !=EEXIST ) { + ret = -errno; +break ; + } + ret =0 ; + }if (p) *p = '/';?
No need for "end".
-- Steve
Quoted 8 lines
tracecmd/trace-setup-guest.c line 47
Quoted 7 lines
tracecmd/trace-setup-guest.c new file +7
-0+ *p ='/' ; + } + +free (buf); +return ret; +} +
This won't work as proposed: p will be NULL on the last iteration but will still get incremented from the outer for-loop and the check (p && *p) won't get triggered (p == 0x01 in this case).
A fixed version might look like this:
static int make_dir(const char *path, mode_t mode)
{
char buf[PATH_MAX+1], *p;
int ret = 0;
strncpy(buf, path, sizeof(buf));
for (p = buf; *p; p++) {
for (; *p == '/'; p++);
p = strchr(p, '/');
if (p)
*p = '\0';
ret = mkdir(buf, mode);
if (ret < 0) {
if (errno != EEXIST) {
ret = -errno;
break;
}
ret = 0;
}
if (!p)
break;
*p = '/';
}
return ret;
}OTOH I find the original version much more readable:
static int make_dir(const char *path, mode_t mode)
{
char buf[PATH_MAX+1], *end, *p;
int ret = 0;
end = stpncpy(buf, path, sizeof(buf));
for (p = buf; p < end; p++) {
for (; p < end && *p == '/'; p++);
for (; p < end && *p != '/'; p++);
*p = '\0';
ret = mkdir(buf, mode);
if (ret < 0) {
if (errno != EEXIST) {
ret = -errno;
break;
}
ret = 0;
}
*p = '/';
}
return ret;
}The intent behind *p = '\0'; ... *p = '/'; is more clearly expressed in this version without getting bogged down by strchr() edge case handling.
Since this is not on a performance critical path how about sticking to the more readable of the two?
-Slavi
Re: [RFC PATCH v6 07/11] trace-cmd: Add `trace-cmd setup-guest` command
Quoted 3 lines · On Mon, 18 Feb 2019 16:37:47 +0200 Slavomir Kaslev <[email protected]> wrote:
This won't work as proposed:
pwill be NULL on the last iteration but will still get incremented from the outer for-loop and the check (p && *p) won't get triggered (p ==0x01in this case).
I still don't like the "end", it just looks awkward.
Quoted 58 lines
A fixed version might look like this:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *p; int ret = 0; strncpy(buf, path, sizeof(buf)); for (p = buf; *p; p++) { for (; *p == '/'; p++); p = strchr(p, '/'); if (p) *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } if (!p) break; *p = '/'; } return ret; }OTOH I find the original version much more readable:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *end, *p; int ret = 0; end = stpncpy(buf, path, sizeof(buf)); for (p = buf; p < end; p++) { for (; p < end && *p == '/'; p++); for (; p < end && *p != '/'; p++); *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } *p = '/'; } return ret; }
Quoted 6 lines
The intent behind
*p = '\0'; ... *p = '/';is more clearly expressed in this version without getting bogged down bystrchr()edge case handling.Since this is not on a performance critical path how about sticking to the more readable of the two?
I'd still like to use '*p' as that's very common.
Also break up the other for loops into a while loops.
for (p = buf; *p; p++) {
while (*p == '/')
p++;
while (*p && *p != '/')
p++;
if (*p)
*p = '\0';
else
p--; /* for the for loop */
[...]This would work, and I think is still readable. It matches more the standard way of the Linux kernel as well.
-- Steve
Re: [RFC PATCH v6 07/11] trace-cmd: Add `trace-cmd setup-guest` command
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Mon, 18 Feb 2019 16:37:47 +0200 Slavomir Kaslev <[email protected]> wrote:
This won't work as proposed:
pwill be NULL on the last iteration but will still get incremented from the outer for-loop and the check (p && *p) won't get triggered (p ==0x01in this case).I still don't like the "end", it just looks awkward.
If that's the only argument I don't think it stands.
Quoted 59 lines
Quoted 59 lines
A fixed version might look like this:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *p; int ret = 0; strncpy(buf, path, sizeof(buf)); for (p = buf; *p; p++) { for (; *p == '/'; p++); p = strchr(p, '/'); if (p) *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } if (!p) break; *p = '/'; } return ret; }OTOH I find the original version much more readable:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *end, *p; int ret = 0; end = stpncpy(buf, path, sizeof(buf)); for (p = buf; p < end; p++) { for (; p < end && *p == '/'; p++); for (; p < end && *p != '/'; p++); *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } *p = '/'; } return ret; }
Quoted 8 lines
Quoted 6 lines
The intent behind
*p = '\0'; ... *p = '/';is more clearly expressed in this version without getting bogged down bystrchr()edge case handling.Since this is not on a performance critical path how about sticking to the more readable of the two?
I'd still like to use '*p' as that's very common.
Testing for '*p' is more common since in the common case one doesn't know the length of the string.
This is not the case here since we first do a copy anyway and hence we know the length from then on. We also actively manipulate to string sentinel and knowing where the string actually ends makes reasoning about the code much easier.
Quoted 2 lines
Also break up the other for loops into a while loops.
OK switching the for()s to while()s and dropping the first p < end check (which is never true) sounds fine.
Quoted 16 lines
for (p = buf; *p; p++) {
while (*p == '/')
p++;
while (*p && *p != '/')
p++;
if (*p)
*p = '\0';
else
p--; /* for the for loop */
[...]Quoted 1 line
This would work, and I think is still readable.
It's really not more readable and having a comment explaining what's going on only supports this claim.
That being said, I don't have strong feelings one way or another though, disappointingly, I did want to share my bikeshedding opinion. It takes less energy to make the proposed change than to explain why imo it's worse. If none of the above makes any sense or you insist on dropping end, I'll send an updated version of this patch for the v7 patchset tomorrow.
Thanks,
-- Slavi
Re: [RFC PATCH v6 07/11] trace-cmd: Add `trace-cmd setup-guest` command
Quoted 106 lines · Slavomir Kaslev
Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Mon, 18 Feb 2019 16:37:47 +0200 Slavomir Kaslev <[email protected]> wrote:
This won't work as proposed:
pwill be NULL on the last iteration but will still get incremented from the outer for-loop and the check (p && *p) won't get triggered (p ==0x01in this case).I still don't like the "end", it just looks awkward.
If that's the only argument I don't think it stands.
Quoted 59 lines
Quoted 59 lines
A fixed version might look like this:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *p; int ret = 0; strncpy(buf, path, sizeof(buf)); for (p = buf; *p; p++) { for (; *p == '/'; p++); p = strchr(p, '/'); if (p) *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } if (!p) break; *p = '/'; } return ret; }OTOH I find the original version much more readable:
static int
make_dir(const char *path, mode_t mode){ char buf[PATH_MAX+1], *end, *p; int ret = 0; end = stpncpy(buf, path, sizeof(buf)); for (p = buf; p < end; p++) { for (; p < end && *p == '/'; p++); for (; p < end && *p != '/'; p++); *p = '\0'; ret = mkdir(buf, mode); if (ret < 0) { if (errno != EEXIST) { ret = -errno; break; } ret = 0; } *p = '/'; } return ret; }Quoted 8 lines
Quoted 6 lines
The intent behind
*p = '\0'; ... *p = '/';is more clearly expressed in this version without getting bogged down bystrchr()edge case handling.Since this is not on a performance critical path how about sticking to the more readable of the two?
I'd still like to use '*p' as that's very common.
Testing for '*p' is more common since in the common case one doesn't know the length of the string.
This is not the case here since we first do a copy anyway and hence we know the length from then on. We also actively manipulate to string sentinel and knowing where the string actually ends makes reasoning about the code much easier.
Quoted 2 lines
Also break up the other for loops into a while loops.
OK switching the for()s to while()s and dropping the first
p < endcheck (which is never true) sounds fine.Quoted 13 lines
for (p = buf; *p; p++) { while (*p == '/') p++; while (*p && *p != '/') p++; if (*p) *p = '\0'; else p--; /* for the for loop */
Quoted 8 lines
Quoted 4 lines
[...]This would work, and I think is still readable.
It's really not more readable and having a comment explaining what's going on only supports this claim.
I thing in the end we're comparing this:
static int make_dir(const char *path, mode_t mode)
{
char buf[PATH_MAX+1], *end, *p;
end = stpncpy(buf, path, sizeof(buf));
for (p = buf; p < end; p++) {
while (*p == '/')
p++;
while (p < end && *p != '/')
p++;
*p = '\0';
if (mkdir(buf, mode) < 0 && errno != EEXIST)
return -errno;
*p = '/';
}
return 0;
}to this version:
static int make_dir(const char *path, mode_t mode)
{
char buf[PATH_MAX+1], *p;
strncpy(buf, path, sizeof(buf));
for (p = buf; *p; p++) {
bool eos = true;
while (*p == '/')
p++;
while (*p && *p != '/')
p++;
if (*p)
*p = '\0';
else
eos = false;
if (mkdir(buf, mode) < 0 && errno != EEXIST)
return -errno;
if (eos)
*p = '/';
}
return 0;
}Cheers,
-- Slavi
RFC PATCH v6 8/11 trace-cmd: Try to autodetect number of guest CPUs in setup-guest if not specified
In reply to Slavomir KaslevWhen no number of guest CPUs is provided with the -c flag to trace-cmd setup-guest, try to autodetect it using virsh for libvirt managed guests.
--- tracecmd/trace-setup-guest.c | 21 +++++++++++++++++++++ tracecmd/trace-usage.c | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-)
tracecmd/trace-setup-guest.c +21 -0
@@ -101,6 +101,24 @@ static int make_guest_fifos(const char *guest, int nr_cpus, mode_t mode)return ret; } +static int get_guest_cpu_count (const char *guest) +{ +const char *cmd_fmt ="virsh vcpucount --maximum '%s' 2>/dev/null" ; +int nr_cpus =-1 ; +char cmd[1024 ]; +FILE *f; + +snprintf (cmd,sizeof (cmd), cmd_fmt, guest); + f =popen (cmd,"r" ); +if (!f) +return -errno; + +fscanf (f,"%d" , &nr_cpus); +pclose (f); + +return nr_cpus; +} +static void do_setup_guest (const char *guest,int nr_cpus,mode_t mode,gid_t gid) {gid_t save_egid;
@@ -179,6 +197,9 @@ void trace_setup_guest(int argc, char **argv) guest = argv[optind+1 ]; +if (nr_cpus <=0 ) + nr_cpus =get_guest_cpu_count (guest); +if (nr_cpus <=0 )pdie ("invalid number of cpus for guest %s" , guest);
tracecmd/trace-usage.c +1 -1
@@ -249,7 +249,7 @@ static struct usage_help usage_help[] = { {"setup-guest" ,"create FIFOs for tracing guest VMs" , -" %s setup-guest -c cpus[-p perm][-g group] guest\n" +" %s setup-guest [-c cpus][-p perm][-g group] guest\n" " -c number of guest virtual CPUs\n" " -p FIFOs permissions (default: 0660)\n" " -g FIFOs group owner\n"
Signature · 1 line
2.19.1
RFC PATCH v6 9/11 trace-cmd: Add setup-guest flag for attaching FIFOs to the guest VM config
In reply to Slavomir KaslevThis patch adds a flag for attaching the newly created FIFOs for guest tracing as virtio serial devices to libvirt managed guests.
--- tracecmd/trace-setup-guest.c | 54 ++++++++++++++++++++++++++++++++++-- tracecmd/trace-usage.c | 3 +- 2 files changed, 53 insertions(+), 4 deletions(-)
tracecmd/trace-setup-guest.c +51 -3
@@ -119,7 +119,45 @@ static int get_guest_cpu_count(const char *guest)return nr_cpus; } -static void do_setup_guest (const char *guest,int nr_cpus,mode_t mode,gid_t gid) +static int attach_guest_fifos (const char *guest,int nr_cpus) +{ +const char *cmd_fmt = +"virsh attach-device --config '%s' '%s' >/dev/null 2>/dev/null" ; +const char *xml_fmt = +"<channel type='pipe'>\n" +" <source path='%s'/>\n" +" <target type='virtio' name='%s%d'/>\n" +"</channel>" ; +char tmp_path[PATH_MAX ], path[PATH_MAX ]; +char cmd[PATH_MAX ], xml[PATH_MAX ]; +int i, fd, ret =0 ; + +strcpy (tmp_path,"/tmp/pipexmlXXXXXX" ); + fd =mkstemp (tmp_path); +if (fd <0 ) +return fd; + +for (i =0 ; i < nr_cpus; i++) { +snprintf (path,sizeof (path),GUEST_FIFO_FMT , guest, i); +snprintf (xml,sizeof (xml), xml_fmt, path,GUEST_PIPE_NAME , i); +pwrite (fd, xml,strlen (xml),0 ); + +snprintf (cmd,sizeof (cmd), cmd_fmt, guest, tmp_path); + errno =0 ; +if (system (cmd) !=0 ) { + ret = -errno; +break ; + } + } + +close (fd); +unlink (tmp_path); + +return ret; +} + +static void do_setup_guest (const char *guest,int nr_cpus, +mode_t mode,gid_t gid,bool attach) {gid_t save_egid;int ret;
@@ -139,6 +177,12 @@ static void do_setup_guest(const char *guest, int nr_cpus, mode_t mode, gid_t giif (ret <0 )pdie ("failed to create FIFOs for %s" , guest); +if (attach) { + ret =attach_guest_fifos (guest, nr_cpus); +if (ret <0 ) +pdie ("failed to attach FIFOs to %s" , guest); + } +if (gid !=-1 ) { ret =setegid (save_egid);if (ret <0 )
@@ -148,6 +192,7 @@ static void do_setup_guest(const char *guest, int nr_cpus, mode_t mode, gid_t givoid trace_setup_guest (int argc,char **argv) { +bool attach = false;struct group *group;mode_t mode =0660 ;int nr_cpus =-1 ;
@@ -167,7 +212,7 @@ void trace_setup_guest(int argc, char **argv) {NULL, 0, NULL, 0} }; - c =getopt_long (argc-1 , argv+1 ,"+hc:p:g:" , + c =getopt_long (argc-1 , argv+1 ,"+hc:p:g:a" , long_options, &option_index);if (c ==-1 )break ;
@@ -187,6 +232,9 @@ void trace_setup_guest(int argc, char **argv)pdie ("group %s does not exist" , optarg); gid = group->gr_gid;break ; +case 'a' : + attach = true; +break ;default :usage (argv); }
@@ -203,5 +251,5 @@ void trace_setup_guest(int argc, char **argv)if (nr_cpus <=0 )pdie ("invalid number of cpus for guest %s" , guest); -do_setup_guest (guest, nr_cpus, mode, gid); +do_setup_guest (guest, nr_cpus, mode, gid, attach); }
tracecmd/trace-usage.c +2 -1
@@ -249,10 +249,11 @@ static struct usage_help usage_help[] = { {"setup-guest" ,"create FIFOs for tracing guest VMs" , -" %s setup-guest [-c cpus][-p perm][-g group] guest\n" +" %s setup-guest [-c cpus][-p perm][-g group][-a] guest\n" " -c number of guest virtual CPUs\n" " -p FIFOs permissions (default: 0660)\n" " -g FIFOs group owner\n" +" -a Attach FIFOs to guest VM config\n" }, #endif {
Signature · 1 line
2.19.1
RFC PATCH v6 10/11 trace-cmd: Add splice() recording from FIFO without additional pipe buffer
In reply to Slavomir KaslevWhen trace-cmd record is reading tracing data over FIFO we can do a direct splice from the FIFO to the output file descriptor instead of doing two through an additional pipe buffer. This patch implements specialized tracecmd_recorder data transfer version for this case.
--- include/trace-cmd/trace-cmd.h | 3 +- lib/trace-cmd/trace-recorder.c | 81 +++++++++++++++++++++++++++------- 2 files changed, 67 insertions(+), 17 deletions(-)
include/trace-cmd/trace-cmd.h +2 -1
@@ -270,8 +270,9 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd);enum {TRACECMD_RECORD_NOSPLICE = (1 <<0 ),/* Use read instead of splice */ -TRACECMD_RECORD_SNAPSHOT = (1 <<1 ),/* extract from snapshot */ +TRACECMD_RECORD_SNAPSHOT = (1 <<1 ),/* Extract from snapshot */ TRACECMD_RECORD_BLOCK = (1 <<2 ),/* Block on splice write */ +TRACECMD_RECORD_NOBRASS = (1 <<3 ),/* Splice directly without a brass pipe */ };void tracecmd_free_recorder (struct tracecmd_recorder *recorder);
lib/trace-cmd/trace-recorder.c +65 -16
157 lines not on this page · raw message
Signature · 1 line
2.19.1
Re: [RFC PATCH v6 10/11] trace-cmd: Add splice() recording from FIFO without additional pipe buffer
Quoted 13 lines · On Thu, 14 Feb 2019 16:13:34 +0200 Slavomir Kaslev <[email protected]> wrote:
lib/trace-cmd/trace-recorder.c +4
-1@@ -487,7 +538,10 @@ long tracecmd_flush_recording(struct tracecmd_recorder *recorder)int tracecmd_start_recording (struct tracecmd_recorder *recorder,unsigned long sleep) { -struct timespec req; +struct timespec req = { + .tv_sec = sleep /1000000 , + .tv_nsec = (sleep %1000000 ) *1000 , + };long read =1 ;long ret;
lib/trace-cmd/trace-recorder.c int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long s · line 554
Quoted 10 lines
lib/trace-cmd/trace-recorder.c +2
-4@@ -495,17 +549,12 @@ int tracecmd_start_recording(struct tracecmd_recorder *recorder, unsigned long s do { /* Only sleep if we did not read anything last time */ - if (!read && sleep) { - req.tv_sec = sleep / 1000000; - req.tv_nsec = (sleep % 1000000) * 1000; + if (!read && sleep) nanosleep(&req, NULL); - } +
The above change is really a clean up and should be a separate patch as well.
-- Steve
Quoted 10 lines
lib/trace-cmd/trace-recorder.c +1
-4read =0 ; do { - if (recorder->flags & TRACECMD_RECORD_NOSPLICE) - ret = read_data(recorder); - else - ret = splice_data(recorder); + ret = move_data(recorder); if (ret < 0) return ret; read += ret;
RFC PATCH v6 11/11 trace-cmd: Add VM tracing over FIFOs transport
In reply to Slavomir KaslevFIFOs offer ~3x the throughput of vsockets. This patch adds support for using FIFOs to stream tracing data back to the host when tracing VMs.
--- include/trace-cmd/trace-cmd.h | 8 +-- tracecmd/include/trace-local.h | 4 +- tracecmd/trace-agent.c | 40 +++++++++++++-- tracecmd/trace-msg.c | 26 ++++++---- tracecmd/trace-record.c | 93 +++++++++++++++++++++++++++++----- 5 files changed, 140 insertions(+), 31 deletions(-)
include/trace-cmd/trace-cmd.h +4 -4
@@ -333,16 +333,16 @@ bool tracecmd_msg_done(struct tracecmd_msg_handle *msg_handle);void tracecmd_msg_set_done (struct tracecmd_msg_handle *msg_handle);int tracecmd_msg_send_trace_req (struct tracecmd_msg_handle *msg_handle, -int argc,char **argv); +int argc,char **argv,bool use_fifos);int tracecmd_msg_recv_trace_req (struct tracecmd_msg_handle *msg_handle, -int *argc,char ***argv); +int *argc,char ***argv,bool *use_fifos);int tracecmd_msg_send_trace_resp (struct tracecmd_msg_handle *msg_handle,int nr_cpus,int page_size, -unsigned int *ports); +unsigned int *ports,bool use_fifos);int tracecmd_msg_recv_trace_resp (struct tracecmd_msg_handle *msg_handle,int *nr_cpus,int *page_size, -unsigned int **ports); +unsigned int **ports,bool *use_fifos);/* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[];
tracecmd/include/trace-local.h +3 -1
@@ -17,6 +17,7 @@#define GUEST_PIPE_NAME "trace-pipe-cpu"#define GUEST_DIR_FMT "/var/lib/trace-cmd/virt/%s"#define GUEST_FIFO_FMT GUEST_DIR_FMT "/" GUEST_PIPE_NAME "%d" +#define VIRTIO_FIFO_FMT "/dev/virtio-ports/" GUEST_PIPE_NAME "%d"extern int debug;extern int quiet;
@@ -100,7 +101,7 @@ void trace_usage(int argc, char **argv);int trace_record_agent (struct tracecmd_msg_handle *msg_handle,int cpus,int *fds, -int argc,char **argv); +int argc,char **argv,bool use_fifos);struct hook_list ;
@@ -212,6 +213,7 @@ struct buffer_instance {int cid;int port;int *fds; +bool use_fifos; };extern struct buffer_instance top_instance;
tracecmd/trace-agent.c +36 -4
@@ -83,12 +83,39 @@ static void make_vsocks(int nr, int *fds, unsigned int *ports) } } +static int open_agent_fifos (int nr_cpus,int *fds) +{ +char path[PATH_MAX ]; +int i, fd, ret =0 ; + +for (i =0 ; i < nr_cpus; i++) { +snprintf (path,sizeof (path),VIRTIO_FIFO_FMT , i); + fd =open (path,O_WRONLY ); +if (fd <0 ) { + ret = -errno; +break ; + } + + fds[i] = fd; + } + +if (!ret) +return ret; + +/* We failed to open all FIFOs so clean up and return error */ +while (--i >=0 ) +close (fds[i]); + +return ret; +} +static void agent_handle (int sd,int nr_cpus,int page_size) {struct tracecmd_msg_handle *msg_handle;unsigned int *ports;char **argv =NULL ;int argc =0 ; +bool use_fifos;int *fds;int ret;
@@ -101,17 +128,22 @@ static void agent_handle(int sd, int nr_cpus, int page_size)if (!msg_handle)die ("Failed to allocate message handle" ); - ret =tracecmd_msg_recv_trace_req (msg_handle, &argc, &argv); + ret =tracecmd_msg_recv_trace_req (msg_handle, &argc, &argv, &use_fifos);if (ret <0 )die ("Failed to receive trace request" ); -make_vsocks (nr_cpus, fds, ports); +if (use_fifos &&open_agent_fifos (nr_cpus, fds)) + use_fifos = false; + +if (!use_fifos) +make_vsocks (nr_cpus, fds, ports); - ret =tracecmd_msg_send_trace_resp (msg_handle, nr_cpus, page_size, ports); + ret =tracecmd_msg_send_trace_resp (msg_handle, nr_cpus, page_size, + ports, use_fifos);if (ret <0 )die ("Failed to send trace response" ); -trace_record_agent (msg_handle, nr_cpus, fds, argc, argv); +trace_record_agent (msg_handle, nr_cpus, fds, argc, argv, use_fifos);free (argv[0 ]);free (argv);
tracecmd/trace-msg.c +17 -9
@@ -159,6 +159,10 @@ static int msg_write(int fd, struct tracecmd_msg *msg)return __do_write_check (fd, msg->buf, data_size); } +enum msg_trace_flags { +MSG_TRACE_USE_FIFOS =1 <<0 , +}; +enum msg_opt_command {MSGOPT_USETCP =1 , };
@@ -740,7 +744,7 @@ error:return ret; } -static int make_trace_req (struct tracecmd_msg *msg,int argc,char **argv) +static int make_trace_req (struct tracecmd_msg *msg,int argc,char **argv,bool use_fifos) {size_t args_size =0 ;char *p;
@@ -750,6 +754,7 @@ static int make_trace_req(struct tracecmd_msg *msg, int argc, char **argv) args_size +=strlen (argv[i]) +1 ; msg->hdr.size =htonl (ntohl (msg->hdr.size) + args_size); + msg->trace_req.flags = use_fifos ?htonl (MSG_TRACE_USE_FIFOS ) :htonl (0 ); msg->trace_req.argc =htonl (argc); msg->buf =calloc (args_size,1 );if (!msg->buf)
@@ -763,13 +768,13 @@ static int make_trace_req(struct tracecmd_msg *msg, int argc, char **argv) }int tracecmd_msg_send_trace_req (struct tracecmd_msg_handle *msg_handle, -int argc,char **argv) +int argc,char **argv,bool use_fifos) {struct tracecmd_msg msg;int ret;tracecmd_msg_init (MSG_TRACE_REQ , &msg); - ret =make_trace_req (&msg, argc, argv); + ret =make_trace_req (&msg, argc, argv, use_fifos);if (ret <0 )return ret;
@@ -782,7 +787,7 @@ int tracecmd_msg_send_trace_req(struct tracecmd_msg_handle *msg_handle,* free(argv); */ int tracecmd_msg_recv_trace_req (struct tracecmd_msg_handle *msg_handle, -int *argc,char ***argv) +int *argc,char ***argv,bool *use_fifos) {struct tracecmd_msg msg;char *p, *buf_end, **args;
@@ -838,6 +843,7 @@ int tracecmd_msg_recv_trace_req(struct tracecmd_msg_handle *msg_handle,*/ *argc = nr_args; *argv = args; + *use_fifos =ntohl (msg.trace_req.flags) &MSG_TRACE_USE_FIFOS ;return 0 ; out_args:
@@ -850,13 +856,14 @@ out:return ret; } -static int make_trace_resp (struct tracecmd_msg *msg, -int page_size,int nr_cpus,unsigned int *ports) +static int make_trace_resp (struct tracecmd_msg *msg,int page_size,int nr_cpus, +unsigned int *ports,bool use_fifos) {int ports_size = nr_cpus *sizeof (*msg->port_array);int i; msg->hdr.size =htonl (ntohl (msg->hdr.size) + ports_size); + msg->trace_resp.flags = use_fifos ?htonl (MSG_TRACE_USE_FIFOS ) :htonl (0 ); msg->trace_resp.cpus =htonl (nr_cpus); msg->trace_resp.page_size =htonl (page_size);
@@ -872,13 +879,13 @@ static int make_trace_resp(struct tracecmd_msg *msg,int tracecmd_msg_send_trace_resp (struct tracecmd_msg_handle *msg_handle,int nr_cpus,int page_size, -unsigned int *ports) +unsigned int *ports,bool use_fifos) {struct tracecmd_msg msg;int ret;tracecmd_msg_init (MSG_TRACE_RESP , &msg); - ret =make_trace_resp (&msg, page_size, nr_cpus, ports); + ret =make_trace_resp (&msg, page_size, nr_cpus, ports, use_fifos);if (ret <0 )return ret;
@@ -887,7 +894,7 @@ int tracecmd_msg_send_trace_resp(struct tracecmd_msg_handle *msg_handle,int tracecmd_msg_recv_trace_resp (struct tracecmd_msg_handle *msg_handle,int *nr_cpus,int *page_size, -unsigned int **ports) +unsigned int **ports,bool *use_fifos) {struct tracecmd_msg msg;ssize_t buf_len;
@@ -909,6 +916,7 @@ int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle, goto out; } + *use_fifos =ntohl (msg.trace_resp.flags) &MSG_TRACE_USE_FIFOS ; *nr_cpus =ntohl (msg.trace_resp.cpus); *page_size =ntohl (msg.trace_resp.page_size); *ports =calloc (*nr_cpus,sizeof (**ports));
tracecmd/trace-record.c +80 -13
170 lines not on this page · raw message
Signature · 1 line
2.19.1