Add VM kernel tracing over vsockets and FIFOs
v9 cover Add VM kernel tracing over vsockets and FIFOs
This patchset adds support for tracing guest kernels to trace-cmd.
Changes in v9:
- none
Changes in v8:
- switched protocol message encoding of ports to text
- switched ports data type to unsigned since vsockets port numbers can overflow int and be confused with errors codes by other code
- added automatic naming for guests with no names (for VMware Workstation guests currently)
- added --debug option to trace-cmd agent
Changes in v7:
- fixed potential memory leak in add_arg()
- call msg_free() in tracecmd_msg_recv_trace_req() eventhough we're stealing the message buffer
- added is_guest and is_agent macros and use throughout tracecmd_record.c instead of testing instance->flags bits
- avoid memory allocation in make_dir()
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: Make ports unsigned int
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
Steven Rostedt (VMware) (1):
trace-cmd: Detect if vsockets are available
Makefile | 7 + include/trace-cmd/trace-cmd.h | 18 +- lib/trace-cmd/trace-recorder.c | 129 ++++-- tracecmd/Makefile | 5 + tracecmd/include/trace-local.h | 29 ++ tracecmd/trace-agent.c | 294 +++++++++++++ tracecmd/trace-cmd.c | 4 + tracecmd/trace-listen.c | 10 +- tracecmd/trace-msg.c | 249 ++++++++++- tracecmd/trace-record.c | 773 +++++++++++++++++++++++++++++++-- tracecmd/trace-setup-guest.c | 247 +++++++++++ tracecmd/trace-usage.c | 22 +- 12 files changed, 1695 insertions(+), 92 deletions(-) create mode 100644 tracecmd/trace-agent.c create mode 100644 tracecmd/trace-setup-guest.c
--
2.19.1
RFC PATCH v9 1/11 trace-cmd: Make ports unsigned int
Switch ports data type to unsigned int since vsocket ports are 32 bit unsigned integers and sometimes cause overflow when stored in int variables.
--- include/trace-cmd/trace-cmd.h | 2 +- tracecmd/trace-listen.c | 10 +++++----- tracecmd/trace-msg.c | 28 ++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 14 deletions(-)
include/trace-cmd/trace-cmd.h +1 -1
@@ -324,7 +324,7 @@ int tracecmd_msg_wait_close(struct tracecmd_msg_handle *msg_handle);/* for server */ int tracecmd_msg_initial_setting (struct tracecmd_msg_handle *msg_handle);int tracecmd_msg_send_port_array (struct tracecmd_msg_handle *msg_handle, -int *ports); +unsigned *ports);int tracecmd_msg_read_data (struct tracecmd_msg_handle *msg_handle,int ofd);int tracecmd_msg_collect_data (struct tracecmd_msg_handle *msg_handle,int ofd);bool tracecmd_msg_done (struct tracecmd_msg_handle *msg_handle);
tracecmd/trace-listen.c +5 -5
@@ -517,10 +517,10 @@ static int *create_all_readers(const char *node, const char *port, {int use_tcp = msg_handle->flags &TRACECMD_MSG_FL_USE_TCP ;char buf[BUFSIZ ]; -int *port_array; +unsigned int *port_array;int *pid_array; -int start_port; -int udp_port; +unsigned int start_port; +unsigned int udp_port;int cpus = msg_handle->cpu_count;int cpu;int pid;
@@ -528,11 +528,11 @@ static int *create_all_readers(const char *node, const char *port,if (!pagesize)return NULL ; - port_array =malloc (sizeof (int ) * cpus); + port_array =malloc (sizeof (*port_array) * cpus);if (!port_array)return NULL ; - pid_array =malloc (sizeof (int ) * cpus); + pid_array =malloc (sizeof (*pid_array) * cpus);if (!pid_array) {free (port_array);return NULL ;
tracecmd/trace-msg.c +20 -8
@@ -161,12 +161,24 @@ static int make_tinit(struct tracecmd_msg_handle *msg_handle,return 0 ; } -static int write_ints (char *buf,size_t buf_len,int *arr,int arr_len) +static unsigned int atou (const char *s) +{ +long r; + + r =atol (s); +if (r >=0 && r <=UINT_MAX ) +return r; + +return 0 ; +} + +static int write_uints (char *buf,size_t buf_len, +unsigned int *arr,int arr_len) {int i, ret, tot =0 ;for (i =0 ; i < arr_len; i++) { - ret =snprintf (buf, buf_len,"%d" , arr[i]); + ret =snprintf (buf, buf_len,"%u" , arr[i]);if (ret <0 )return ret;
@@ -184,15 +196,15 @@ static int write_ints(char *buf, size_t buf_len, int *arr, int arr_len)return tot; } -static int make_rinit (struct tracecmd_msg *msg,int cpus,int *ports) +static int make_rinit (struct tracecmd_msg *msg,int cpus,unsigned int *ports) {int data_size; - data_size =write_ints (NULL ,0 , ports, cpus); + data_size =write_uints (NULL ,0 , ports, cpus); msg->buf =malloc (data_size);if (!msg->buf)return -ENOMEM ; -write_ints (msg->buf, data_size, ports, cpus); +write_uints (msg->buf, data_size, ports, cpus); msg->rinit.cpus =htonl (cpus); msg->hdr.size =htonl (ntohl (msg->hdr.size) + data_size);
@@ -442,7 +454,7 @@ int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle, } cpus =ntohl (msg.rinit.cpus); - ports =malloc_or_die (sizeof (*ports) * cpus); + ports =malloc (sizeof (*ports) * cpus);if (!ports) { ret = -ENOMEM ; goto out;
@@ -456,7 +468,7 @@ int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle, goto error; } - ports[i] =atoi (p); + ports[i] =atou (p); p =strchr (p,'\0' ); }
@@ -588,7 +600,7 @@ error: }int tracecmd_msg_send_port_array (struct tracecmd_msg_handle *msg_handle, -int *ports) +unsigned int *ports) {struct tracecmd_msg msg;int ret;
Signature · 1 line
2.19.1
Re: [RFC PATCH v9 01/11] trace-cmd: Make ports unsigned int
tracecmd/trace-msg.c static int make_tinit(struct tracecmd_msg_handle *msg_handle, · line 174
Quoted 12 lines · On Tue, 2 Apr 2019 16:45:30 +0300 Slavomir Kaslev <[email protected]> wrote:
tracecmd/trace-msg.c +11
-1-static int write_ints (char *buf,size_t buf_len,int *arr,int arr_len) +static unsigned int atou (const char *s) +{ +long r; + + r =atol (s); +if (r >=0 && r <=UINT_MAX ) +return r; + +return 0 ; +} +
I wonder if we should do this instead:
/* test a to u */
static int tatou(const char *s, int *res)
{
long r;
r = atol(s);
if (r >= 0 && r <= UINT_MAX) {
*res = (unsigned)r;
return 0;
}
return -1;
}That way we could check for invalid ints.
-- Steve
Re: [RFC PATCH v9 01/11] trace-cmd: Make ports unsigned int
Quoted 21 lines · Steven Rostedt
tracecmd/trace-msg.c static int make_tinit(struct tracecmd_msg_handle *msg_handle, · line 174
Quoted 13 lines · On Tue, 2 Apr 2019 16:45:30 +0300 Slavomir Kaslev <[email protected]> wrote:
-static int
write_ints(char *buf, size_t buf_len, int *arr, int arr_len)tracecmd/trace-msg.c +11
-0+static unsigned int atou (const char *s) +{ +long r; + + r =atol (s); +if (r >=0 && r <=UINT_MAX ) +return r; + +return 0 ; +} +I wonder if we should do this instead:
/* test a to u */
static inttatou(const char *s, int *res)
Quoted 10 lines
{
long r;
r = atol(s);
if (r >= 0 && r <= UINT_MAX) {
*res = (unsigned)r;
return 0;
}
return -1;
}Makes sense. I did consider it while writing this but decided to go with the traditional "broken" prototype for such functions.
RFC PATCH v9 2/11 trace-cmd: Detect if vsockets are available
In reply to Slavomir KaslevFrom: "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 v9 3/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
Re: [RFC PATCH v9 03/11] trace-cmd: Add tracecmd_create_recorder_virt function
Quoted 2 lines · On Tue, 2 Apr 2019 16:45:32 +0300 Slavomir Kaslev <[email protected]> wrote:
Add
tracecmd_create_recorder_virtfunction that will be used for tracing VM guests.
A little week on the change log. Should have a description about why tracecmd_create_recorder_virt() does not need a buffer.
-- Steve
Quoted 6 lines
Signed-off-by Slavomir Kaslev <[email protected]>--- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/trace-recorder.c | 53 ++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 15 deletions(-)
Re: [RFC PATCH v9 03/11] trace-cmd: Add tracecmd_create_recorder_virt function
Quoted 8 lines · Steven Rostedt
Quoted 2 lines · On Tue, 2 Apr 2019 16:45:32 +0300 Slavomir Kaslev <[email protected]> wrote:
Add
tracecmd_create_recorder_virtfunction that will be used for tracing VM guests.A little week on the change log. Should have a description about why
tracecmd_create_recorder_virt()does not need a buffer.
Fair point. I'll add better description for the next iteration and a docstring comment for this function.
RFC PATCH v9 4/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 | 213 +++++++++++++++++++++++++++++++++- 2 files changed, 224 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 +212 -1
240 lines not on this page · raw message
Signature · 1 line
2.19.1
RFC PATCH v9 5/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 | 5 ++++ tracecmd/trace-record.c | 55 ++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-)
tracecmd/include/trace-local.h +5 -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 {
@@ -198,6 +200,9 @@ extern struct buffer_instance *first_instance;#define for_all_instances (i) for (i = first_instance; i; \ i = i == &top_instance ? buffer_instances : (i)->next) +#define is_agent (instance) ((instance)->flags & BUFFER_FL_AGENT) +#define is_guest (instance) ((instance)->flags & BUFFER_FL_GUEST) +struct buffer_instance *create_instance (const char *name);void add_instance (struct buffer_instance *instance,int cpu_count);char *get_instance_file (struct buffer_instance *instance,const char *file);
tracecmd/trace-record.c +53 -2
169 lines not on this page · raw message
Signature · 1 line
2.19.1
RFC PATCH v9 6/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 | 4 + tracecmd/include/trace-local.h | 16 + tracecmd/trace-agent.c | 263 +++++++++++++++ tracecmd/trace-cmd.c | 3 + tracecmd/trace-record.c | 599 ++++++++++++++++++++++++++++++--- tracecmd/trace-usage.c | 13 +- 6 files changed, 854 insertions(+), 44 deletions(-) create mode 100644 tracecmd/trace-agent.c
tracecmd/Makefile +4 -0
@@ -33,6 +33,10 @@ 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))
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; + +unsigned int cid; +unsigned int port; +int *fds; };extern struct buffer_instance top_instance;
tracecmd/trace-agent.c new file +263 -0
263 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 +556 -43
857 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
RFC PATCH v9 7/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 v9 8/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 | 178 +++++++++++++++++++++++++++++++++ tracecmd/trace-usage.c | 8 ++ 5 files changed, 194 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 +178 -0
178 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
RFC PATCH v9 9/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
@@ -93,6 +93,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;
@@ -171,6 +189,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 v9 10/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
@@ -111,7 +111,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;
@@ -131,6 +169,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 )
@@ -140,6 +184,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 ;
@@ -159,7 +204,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 ;
@@ -179,6 +224,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); }
@@ -195,5 +243,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 v9 11/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 | 11 +++-- lib/trace-cmd/trace-recorder.c | 70 ++++++++++++++++++++++----- tracecmd/include/trace-local.h | 4 +- tracecmd/trace-agent.c | 39 +++++++++++++-- tracecmd/trace-msg.c | 26 ++++++---- tracecmd/trace-record.c | 87 ++++++++++++++++++++++++++++++---- 6 files changed, 197 insertions(+), 40 deletions(-)
include/trace-cmd/trace-cmd.h +6 -5
@@ -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);
@@ -332,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[];
lib/trace-cmd/trace-recorder.c +59 -11
137 lines not on this page · raw message
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 {unsigned int cid;unsigned int port;int *fds; +bool use_fifos; };extern struct buffer_instance top_instance;
tracecmd/trace-agent.c +35 -4
@@ -100,12 +100,38 @@ 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; + +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; + goto cleanup; + } + + fds[i] = fd; + } + +return 0 ; + +cleanup: +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;
@@ -118,17 +144,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
@@ -155,6 +155,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 , +}; +static int make_tinit (struct tracecmd_msg_handle *msg_handle,struct tracecmd_msg *msg) {
@@ -788,7 +792,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;
@@ -798,6 +802,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)
@@ -811,13 +816,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;
@@ -830,7 +835,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;
@@ -876,6 +881,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 ;/* * On success we're passing msg.buf to the caller through argv[0] so we
@@ -895,8 +901,8 @@ 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 data_size;
@@ -907,6 +913,7 @@ static int make_trace_resp(struct tracecmd_msg *msg,write_uints (msg->buf, data_size, ports, nr_cpus); msg->hdr.size =htonl (ntohl (msg->hdr.size) + data_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);
@@ -915,13 +922,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;
@@ -930,7 +937,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;char *p, *buf_end;
@@ -952,6 +959,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 +77 -10
162 lines not on this page · raw message
Signature · 1 line
2.19.1
Re: [RFC PATCH v9 11/11] trace-cmd: Add splice() recording from FIFO without additional pipe buffer
Quoted 9 lines · On Tue, 2 Apr 2019 16:45:40 +0300 Slavomir Kaslev <[email protected]> wrote:
lib/trace-cmd/trace-recorder.c +9
-0+/* +* Returns -1 on error. +* or bytes of data read. +*/ +static long direct_splice_data (struct tracecmd_recorder *recorder) +{ +struct pollfd pfd = { + .fd = recorder->trace_fd, + .events =POLLIN ,
lib/trace-cmd/trace-recorder.c static long splice_data(struct tracecmd_recorder *recorder) · line 427
Quoted 10 lines
lib/trace-cmd/trace-recorder.c +10
-0+ }; +long read; +int ret; + + ret =poll (&pfd,1 ,POLL_TIMEOUT_MS ); +if (ret <0 ) +return -1 ; + +if (!(pfd.revents |POLLIN )) +return 0 ;
Why the polling code, and not just block on splice?
-- Steve
Quoted 14 lines
lib/trace-cmd/trace-recorder.c +14
-0+ + read =splice (recorder->trace_fd,NULL , recorder->fd,NULL , + recorder->pipe_size, recorder->trace_fd_flags); +if (read <0 ) { +if (errno ==EAGAIN || errno ==EINTR ) +return 0 ; + +warning ("recorder error in splice input" ); +return -1 ; + } + +return read; +} +
Re: [RFC PATCH v9 11/11] trace-cmd: Add splice() recording from FIFO without additional pipe buffer
Quoted 16 lines · Steven Rostedt
Quoted 13 lines · On Tue, 2 Apr 2019 16:45:40 +0300 Slavomir Kaslev <[email protected]> wrote:
lib/trace-cmd/trace-recorder.c +13
-0+/* +* Returns -1 on error. +* or bytes of data read. +*/ +static long direct_splice_data (struct tracecmd_recorder *recorder) +{ +struct pollfd pfd = { + .fd = recorder->trace_fd, + .events =POLLIN , + }; +long read; +int ret; +
Quoted 8 lines
lib/trace-cmd/trace-recorder.c static long splice_data(struct tracecmd_recorder *recorder) · line 427
Quoted 6 lines
lib/trace-cmd/trace-recorder.c +6
-0+ ret =poll (&pfd,1 ,POLL_TIMEOUT_MS ); +if (ret <0 ) +return -1 ; + +if (!(pfd.revents |POLLIN )) +return 0 ;Why the polling code, and not just block on splice?
To work around older kernels that don't have the splice(2) O_NONBLOCKING fix[1] for pipes.
An alternative would be to configure SIGINT not to auto restart syscalls (which is the default afaik). Then splice(2) will return EAGAIN and will can restart it with SPLICE_F_NONBLOCK flag on.
Re: [RFC PATCH v9 11/11] trace-cmd: Add splice() recording from FIFO without additional pipe buffer
Quoted 27 lines · Slavomir Kaslev
Quoted 17 lines · Steven Rostedt
Quoted 14 lines · On Tue, 2 Apr 2019 16:45:40 +0300 Slavomir Kaslev <[email protected]> wrote:
lib/trace-cmd/trace-recorder.c +5
-0+/* +* Returns -1 on error. +* or bytes of data read. +*/ +static long direct_splice_data(struct tracecmd_recorder*recorder)
lib/trace-cmd/trace-recorder.c +8
-0+{ +struct pollfd pfd = { + .fd = recorder->trace_fd, + .events =POLLIN , + }; +long read; +int ret; +Quoted 8 lines
lib/trace-cmd/trace-recorder.c static long splice_data(struct tracecmd_recorder *recorder) · line 427
Quoted 6 lines
lib/trace-cmd/trace-recorder.c +6
-0+ ret =poll (&pfd,1 ,POLL_TIMEOUT_MS ); +if (ret <0 ) +return -1 ; + +if (!(pfd.revents |POLLIN )) +return 0 ;Why the polling code, and not just block on splice?
Quoted 6 lines
To work around older kernels that don't have the splice(2)
O_NONBLOCKINGfix[1] for pipes.An alternative would be to configure SIGINT not to auto restart syscalls (which is the default afaik). Then splice(2) will return EAGAIN and will can restart it with
SPLICE_F_NONBLOCKflag on.
^EINTR
Re: [RFC PATCH v9 11/11] trace-cmd: Add splice() recording from FIFO without additional pipe buffer
In reply to Slavomir KaslevQuoted 9 lines · On Wed, 3 Apr 2019 13:11:07 +0000 Slavomir Kaslev <[email protected]> wrote:
Quoted 2 lines
Why the polling code, and not just block on splice?
To work around older kernels that don't have the splice(2)
O_NONBLOCKINGfix[1] for pipes.An alternative would be to configure SIGINT not to auto restart syscalls (which is the default afaik). Then splice(2) will return EAGAIN and will can restart it with
SPLICE_F_NONBLOCKflag on.
There needs to be a comment explaining its existence then, and perhaps we can do a check at the beginning (someplace else) to see if the fix is in, and then skip this part if it is.
-- Steve