trace-cmd attach: Add new command "attach"
PATCH 1/1 trace-cmd attach: Add new command "attach"
From: "Steven Rostedt (Google)" <[email protected]>
Add a new trace-cmd command called "attach". This will attach a trace.dat file created on a guest to a trace.dat file created on the host at the same time. In some cases, trace-cmd agent isn't possible. This gives the ability to do a trace on the guest with trace-cmd record, and on the host as well, and then combine the two such that the guest events are interleaved properly within the host events.
There's some requirements for the traces. The host side needs to enable kvm_exit/entry events as well as to use the tsc2nsec clock. The guest needs to use the x86-tsc clock.
As can be seen by the above requirements, this currently works only for x86, but when other architectures have raw clocks that can do the same, then they will be supported.
--- .../trace-cmd/trace-cmd-attach.1.txt | 124 +++++ Documentation/trace-cmd/trace-cmd.1.txt | 5 +- tracecmd/Makefile | 1 + tracecmd/include/trace-local.h | 4 + tracecmd/trace-attach.c | 471 ++++++++++++++++++ tracecmd/trace-cmd.c | 22 + tracecmd/trace-usage.c | 9 + 7 files changed, 635 insertions(+), 1 deletion(-) create mode 100644 Documentation/trace-cmd/trace-cmd-attach.1.txt create mode 100644 tracecmd/trace-attach.c
Documentation/trace-cmd/trace-cmd-attach.1.txt new file +124 -0
@@ -0,0 +1,124 @@ +TRACE-CMD-ATTACH(1) +=================== + +NAME +---- +trace-cmd-attach - attach a guest trace.dat file to a host trace.dat file + +SYNOPSIS +-------- +*trace-cmd attach* ['OPTIONS'] host-trace-file guest-trace-file guest-pid [guest-pid ...] + +DESCRIPTION +----------- +The trace-cmd(1) attach is used to take a trace.dat file created on a guest and +attach it to a trace.dat file that was created on the host. In most cases, +trace-cmd-agent(1) can be used to automate this, but if for some reason, the +agent isn't appropriate, it may be required to start trace-cmd recording on +the guest with trace-cmd-record(1). If the host recording is activated at the +same time, one can use trace-cmd attach(1) to connect the guest and host files +as if they were created by the trace-cmd agent. + +*host-trace-file*:: + The trace.dat file created by the host. Must have kvm_exit and kvm_entry + events, and use the "tsc2nsec" clock. + +*guest-trace-file*:: + The trace.dat file created by the guest. Must use the "x86-tsc" clock. + For now, this is only supported on x86, it may support other achitectures + later. + +*guest-pid*:: + The process ID of the host thread that represents the guests threads. + Each process ID that represents all of the guest vCPUs should be listed. + Note, you can add more than just the threads that represent the guest + vCPUs, as the tool will search the *host-trace-file* for kvm_exit and + kvm_entry events to match these PIDs with the vCPUs that they represent. + +OPTIONS +------- +*-c* 'cpus':: + Specify a the number of CPUS the guest has. + +*-s* 'timeshift':: + A comma separated list of the format _offset_,_scale_,_frac_,_timestamp_ + These values map to what are given in /sys/kernel/kvm/<pid>/vcpu/* + + *offset*: Is the offset of the guest. "tsc-offest" in the directory. Note + that the value listed here is the negative of what is listed in + the directory. + + *scale*: The scaling factor. "tsc-scaling-ratio" + + *frac*: The fraction bits. "tsc-scaling-ratio-frac-bits" + + *timestamp*: The timestamp to start using the above. In some cases, the + values may change over time. By adding a timestamp, it will + take effect after the timestamp has been hit. Normally + this would be zero. + + Currently, only one timeshift is given per CPU. One *-s* option should be + given for each CPU. If there are less options than CPUs, then the last option + given will be used for the rest of the CPUs. If only one option is given, then + the values for that option will be used for all CPUs. + +EXAMPLES +-------- + +Enable all events for tracing: + +[source,shell] +---- + $ # find the process for a given guest + $ ps aux |grep qemu + libvirt+ 63170 5.6 1.6 13994848 4257540 ? Sl May02 2884:49 /usr/bin/qemu-system-x86_64... + + $ # Use 63170 to find all the PIDs for the give guest + $ ls /proc/63170/task + 1541591 63170 63198 63209 63211 63213 63214 63215 63216 63217 63218 63219 63234 + + $ # Find the tsc offset + $ su + # cat /sys/kernel/debug/kvm/63170-15/vcpu0/tsc-offset + -27950965013436847 + + # trace-cmd record -C tsc2nesc -e kvm -e sched -e irq -e timer + +# on guest: + + # trace-cmd record -C x86-tsc -e sched -e irq -e timer sleep 10 + +# back on host, hit Ctrl^C to stop tracing after the guest is done + + # # Make the trace.dat user owned by your user account. + # chown user.user trace.dat + # exit + + $ scp root@guest:trace.dat trace-guest.dat + + $ # now attach the two files (guest has 8 CPUs) + $ trace-cmd attach -c 8 -s 27950965013436847 trace.dat trace-guest.dat 1541591 63170 63198 63209 63211 63213 63214 63215 63216 63217 63218 63219 63234 + + $ trace-cmd report -i trace.dat -i trace-guest.dat + $ # now you should see the guest trace interleaved within the host trace. +---- + + +SEE ALSO +-------- +trace-cmd(1), trace-cmd-report(1), trace-cmd-start(1), trace-cmd-stop(1), +trace-cmd-extract(1), trace-cmd-reset(1), trace-cmd-split(1), +trace-cmd-list(1), trace-cmd-listen(1), trace-cmd-profile(1) + +AUTHOR +------ +Written by Steven Rostedt (Google) <[email protected]> + +RESOURCES +--------- +https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/ + +COPYING +------- +Copyright \(C) 2010 Red Hat, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/trace-cmd/trace-cmd.1.txt +4 -1
@@ -76,6 +76,8 @@ COMMANDS convert - convert trace files + attach - attach a host trace.dat file to a guest trace.dat file + dump - read out the meta data from a trace file OPTIONS
@@ -92,7 +94,8 @@ trace-cmd-record(1), trace-cmd-report(1), trace-cmd-hist(1), trace-cmd-start(1), trace-cmd-stop(1), trace-cmd-extract(1), trace-cmd-reset(1), trace-cmd-restore(1), trace-cmd-stack(1), trace-cmd-convert(1), trace-cmd-split(1), trace-cmd-list(1), trace-cmd-listen(1), -trace-cmd.dat(5), trace-cmd-check-events(1) trace-cmd-stat(1) +trace-cmd.dat(5), trace-cmd-check-events(1), trace-cmd-stat(1), +trace-cmd-attach(1) AUTHOR ------
tracecmd/Makefile +1 -0
@@ -37,6 +37,7 @@ TRACE_CMD_OBJS += trace-dump.o TRACE_CMD_OBJS += trace-clear.o TRACE_CMD_OBJS += trace-vm.o TRACE_CMD_OBJS += trace-convert.o +TRACE_CMD_OBJS += trace-attach.o TRACE_CMD_OBJS += trace-agent.o TRACE_CMD_OBJS += trace-tsync.o TRACE_CMD_OBJS += trace-setup-guest.o
tracecmd/include/trace-local.h +4 -0
@@ -118,6 +118,8 @@ void trace_usage(int argc, char **argv);void trace_dump (int argc,char **argv); +void trace_attach (int argc,char **argv); +void trace_convert (int argc,char **argv); int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
@@ -363,6 +365,8 @@ struct tracecmd_time_sync * trace_tsync_as_guest(int fd, const char *tsync_proto, const char *clock, unsigned int remote_id, unsigned int local_id); +char *strparse (char *str,char delim,char **save); +/* moved from trace-cmd.h */ void tracecmd_remove_instances (void );int tracecmd_add_event (const char *event_str,int stack);
tracecmd/trace-attach.c new file +471 -0
471 lines not on this page · raw message
tracecmd/trace-cmd.c +22 -0
@@ -50,6 +50,27 @@ void *malloc_or_die(unsigned int size)return data; } +/* Same as strtok_r(), but allows empty tokens */ +char *strparse (char *str,char delim,char **save) +{ +char *next; + +if (!str) { + str = *save; +if ((*save)[0 ] =='\0' ) +return NULL ; + } + + next =strchr (str, delim); +if (next) { + *next ='\0' ; + *save = next +1 ; + }else { + *save = str +strlen (str); + } +return str; +} +void tracecmd_debug (const char *fmt, ...) {va_list ap;
@@ -156,6 +177,7 @@ struct command commands[] = { {"list", trace_list}, {"help", trace_usage}, {"dump", trace_dump}, + {"attach", trace_attach}, {"convert", trace_convert}, {"-h", trace_usage}, };
tracecmd/trace-usage.c +9 -0
@@ -429,6 +429,15 @@ static struct usage_help usage_help[] = {" -h, --help show usage information\n" " --verbose 'level' Set the desired log level\n" }, + { +"attach" , +"Attach a host and guest trace.dat file" , +" %s attach [options] host_file guest_file vcpu_pid,...\n" +" -s offset,scale,fraction[,timestamp] conversion to sync guest timestamp\n" +" host_file The trace.dat file from the host\n" +" guest_file The trace.dat file from the guest\n" +" vcpu_pid list of process ids from the host that represent the vCPUs of the guest\n" + }, {"convert" , "convert trace file to different version",
Signature · 1 line
2.39.2