Timestamp synchronization of host - guest tracing session
v19 cover Timestamp synchronization of host - guest tracing session
Basic infrastructure for host - guest timestamp synchronization and a POC implementation of PTP-like algorithm.
[
v19 changes:
- Rebased on top of latest master. The librtacefs is already merged, this allows to drop few patches from the set and use librtacefs APIs instead.
- Reimplemented all new trace.dat options to be in binary format, instead of text. Leverage the new "trace-cmd dump" sub command to track what is written in the file.
- Addressed Steven's comments.
v18 changes: addressed Steven Rostdet comments:
- Replaced semaphores with pthread mutexes.
- Made bitmask with time sync protocols unlimited, so we can support more than 32 protocols. That required to redesign the trace request packet format.
- A lot of small fixes.
v17 changes:
- Implemented new PTP logic for calculating the clocks offset, using histogram of all PTP samples. It gives better results than the logic with the fastest response time, so set the new one as default in the POC patch.
v16 changes:
- Fixed compilation in case no VSOCK is available (Thanks to Slavomir Kaslev)
- Fixed a typo in trace-cmd-record.1.txt (Thanks to Slavomir Kaslev)
- Added forgotten file in the patch "trace-cmd: Add new library APIs for ftrace instances." - trace-instance.c
- Fixed few compilation warnings related to TSYNC_DEBUG code.
- Removed a blank line at the end of "tsync_readme" file.
v15 changes:
- Removed the patch for "--proc-map" from the series, as it should not be part of it.
v14 changes:
- Bring back the PTP-like algorithm and removed the ftrace event based logic.
- Reimplemented the PTP-like algorithm to use raw ftrace markers, instead of clock_gettime() API.
- Refactored the logic to be algorithm independent and plugin friendly.
- Implemented continuous timestamps synchronization, while the trace is running.
- Moved logic from trace-cmd application to libtracecmd, as new library APIs.
- Implemented new trace id functionality.
- Implemented new guest section in host trace.dat file.
v13 changes:
- Remove few patches from the set, as they were merged.
- Rebased to the latest master, Slavomirs patchest "Add VM kernel tracing over vsockets and FIFOs" got merged!
v12 changes:
- Rebased on top of Slavomir's v13 "Add VM kernel tracing over vsockets and FIFOs"
v11 changes:
- Rebased on top of Slavomir's v10 "Add VM kernel tracing over vsockets and FIFOs"
- Addressed Slavomir's commnents from version 10 of the patch series.
v10 changes:
- Fixed broken compilation, call to timestamp_correction_calc() in timestamp_correct was smashed.
- Replaced deprecated tep_data_event_from_type() API with tep_find_event().
- Fixed a warning on assignment const to non const.
v9 changes:
- Fixed implementation of binary search algorithm in timestamp_correct()
v8 changes:
- Added rmdir() call in tracecmd_remove_instance(), to completely remove the instance. However, there is an issue with deleting the instances using rmdir(), which is investigated.
- Few changes in read_qemu_guests_pids(), timestamp_correct(), tsync_offset_load()
tracecmd_clock_context_new() and find_raw_events() suggested by Slavomir.
v7 changes:
- Added warning messages in case time synchronization cannot be negotiated or fails.
- Few optimizations and checks in read_qemu_guests_pids(), tsync_offset_load(), and find_raw_events(), suggested by Slavomir Kaslev.
- Reworked timestamp_correct() to not use static variables.
- Check TRACECMD_OPTION_TIME_SHIFT before reading time sync samples from the trace.dat file
v6 changes:
- Refactored tracecmd_msg_snd_time_sync() and tracecmd_msg_rcv_time_sync() functions:
removed any time sync calculations logic as separate functions in trace-timesync.c file
- Defined TSYNC_PROBE, TSYNC_REQ and TSYNC_RESP messages, in order to make the time sync protocol comprehensible.
- Addressed Steven Rostedt comments.
- Addressed Slavomir Kaslev commnets.
v5 changes:
- Rebased to Slavomir's v8 "Add VM kernel tracing over vsockets and FIFOs" patch series.
- Implemented an algorithm for time drift correction.
- Addressed Slavomir's commnets.
- Refactored the code: moved all time sync specific implementation in trace-timesync.c
- Isolated all hardcoded event specific stuff in a structure, so it could be easily moved to external plugins.
- Added a check for VSOCK support: do not perform vsock dependent time synchronisation in case there is no VSOCK support.
v4 changes:
- Removed the implementation of PTP-like algorithm. The current logic relies on matching time stamps of kvm_exit/virtio_transport_recv_pkt events on host to virtio_transport_alloc_pkt/vp_notify events on guest.
- Rebased to Slavomir's v7 "Add VM kernel tracing over vsockets and FIFOs" patch series.
- Decreased the time synch probes from 5000 to 300.
- Addressed Steven Rostedt comments.
- Code cleanup.
v3 changes:
- Removed any magic constants, used in the PTP-like algorithm, as Slavomir Kaslev suggested.
- Implemented new algorithm, based on mapping kvm_exit events in host context to vsock_send events in guest context, suggested by Steven Rostedt.
v2 changes:
- Addressed Steven Rostedt comments.
- Modified PTP-like timestamps sync algorithm to gain more accuracy, with the help of Yordan Karadzhov and Slavomir Kaslev.
]
Tzvetomir Stoyanov (5):
trace-cmd: Add support for negative time offsets in trace.dat file
trace-cmd: Find and store pids of tasks, which run virtual CPUs of
given VM
trace-cmd: Implement new API tracecmd_add_option_v()
trace-cmd: Add definitions of htonll() and ntohll()
trace-cmd: Implement new option in trace.dat file:
TRACECMD_OPTION_TIME_SHIFT
Tzvetomir Stoyanov (VMware) (10):
trace-cmd: Add new library API for local CPU count
trace-cmd: Add new API to generate a unique ID of the tracing session
trace-cmd: Store the session tracing ID in the trace.dat file
trace-cmd: Exchange tracing IDs between host and guest
trace-cmd: Add guest information in host's trace.dat file
trace-cmd: Add host trace clock as guest trace argument
trace-cmd: Refactor few trace-cmd internal functions.
trace-cmd: Basic infrastructure for host - guest timestamp
synchronization
trace-cmd: [POC] PTP-like algorithm for host - guest timestamp
synchronization
trace-cmd: Debug scripts for PTP-like algorithm for host - guest
timestamp synchronization
Documentation/trace-cmd-record.1.txt | 7 + include/trace-cmd/trace-cmd.h | 84 ++- include/traceevent/event-parse.h | 1 + lib/trace-cmd/Makefile | 2 + lib/trace-cmd/include/trace-cmd-local.h | 45 +- lib/trace-cmd/include/trace-tsync-local.h | 38 ++ lib/trace-cmd/include/trace-write-local.h | 43 ++ lib/trace-cmd/trace-input.c | 332 ++++++++++- lib/trace-cmd/trace-msg.c | 402 +++++++++++-- lib/trace-cmd/trace-output.c | 119 +++- lib/trace-cmd/trace-timesync-ptp.c | 651 ++++++++++++++++++++++ lib/trace-cmd/trace-timesync.c | 506 +++++++++++++++++ lib/trace-cmd/trace-util.c | 86 +++ scripts/debug/tsync_hist.py | 57 ++ scripts/debug/tsync_readme | 12 + scripts/debug/tsync_res.py | 46 ++ tracecmd/Makefile | 3 +- tracecmd/include/trace-local.h | 24 +- tracecmd/trace-agent.c | 59 +- tracecmd/trace-dump.c | 122 ++++ tracecmd/trace-profile.c | 2 +- tracecmd/trace-read.c | 4 +- tracecmd/trace-record.c | 279 +++++++--- tracecmd/trace-stat.c | 4 +- tracecmd/trace-tsync.c | 272 +++++++++ tracecmd/trace-usage.c | 4 + 26 files changed, 3009 insertions(+), 195 deletions(-) create mode 100644 lib/trace-cmd/include/trace-tsync-local.h create mode 100644 lib/trace-cmd/include/trace-write-local.h create mode 100644 lib/trace-cmd/trace-timesync-ptp.c create mode 100644 lib/trace-cmd/trace-timesync.c create mode 100644 scripts/debug/tsync_hist.py create mode 100644 scripts/debug/tsync_readme create mode 100644 scripts/debug/tsync_res.py create mode 100644 tracecmd/trace-tsync.c
Signature · 1 line
2.24.1
PATCH v19 1/15 trace-cmd: Add support for negative time offsets in trace.dat file
From: Tzvetomir Stoyanov <[email protected]>
When synchronizing timestamps between different machines, there are cases when the time offset is negative. This patch changes the way time offset is written and read from trace.dat file - as signed decimal, instead of hex.
--- include/trace-cmd/trace-cmd.h | 2 +- lib/trace-cmd/trace-input.c | 6 +++--- tracecmd/trace-read.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
include/trace-cmd/trace-cmd.h +1 -1
@@ -161,7 +161,7 @@ const char *tracecmd_buffer_instance_name(struct tracecmd_input *handle, int indstruct tracecmd_input *tracecmd_buffer_instance_handle (struct tracecmd_input *handle,int indx);int tracecmd_is_buffer_instance (struct tracecmd_input *handle); -void tracecmd_set_ts_offset (struct tracecmd_input *handle,unsigned long long offset); +void tracecmd_set_ts_offset (struct tracecmd_input *handle,long long offset);void tracecmd_set_ts2secs (struct tracecmd_input *handle,unsigned long long hz);void tracecmd_print_events (struct tracecmd_input *handle,const char *regex);
lib/trace-cmd/trace-input.c +3 -3
@@ -93,7 +93,7 @@ struct tracecmd_input {bool read_page;bool use_pipe;struct cpu_data *cpu_data; -unsigned long long ts_offset; +long long ts_offset;double ts2secs;char * cpustats;char * uname;
@@ -2036,7 +2036,7 @@ static int init_cpu(struct tracecmd_input *handle, int cpu) }void tracecmd_set_ts_offset (struct tracecmd_input *handle, -unsigned long long offset) +long long offset) { handle->ts_offset = offset; }
@@ -2215,7 +2215,7 @@ tracecmd_search_task_map(struct tracecmd_input *handle,static int handle_options (struct tracecmd_input *handle) { -unsigned long long offset; +long long offset;unsigned short option;unsigned int size;char *cpustats =NULL ;
tracecmd/trace-read.c +2 -2
@@ -58,7 +58,7 @@ static struct list_head handle_list;struct input_files {struct list_head list;const char *file; -unsigned long long tsoffset; +long long tsoffset;unsigned long long ts2secs; };static struct list_head input_files;
@@ -1466,7 +1466,7 @@ void trace_report (int argc, char **argv)struct input_files *inputs;struct handle_list *handles;enum output_type otype; -unsigned long long tsoffset =0 ; +long long tsoffset =0 ;unsigned long long ts2secs =0 ;unsigned long long ts2sc;int show_stat =0 ;
Signature · 1 line
2.24.1
PATCH v19 2/15 trace-cmd: Add new library API for local CPU count
In reply to Tzvetomir Stoyanov (VMware)In order to reuse the code, the function detecting the number of local CPU is moved from trace-cmd application to libtracecmd. The following new library API is introduced:
int tracecmd_count_cpus(void);
--- include/trace-cmd/trace-cmd.h | 3 +++ lib/trace-cmd/trace-util.c | 49 ++++++++++++++++++++++++++++++++++ tracecmd/include/trace-local.h | 2 -- tracecmd/trace-agent.c | 2 +- tracecmd/trace-profile.c | 2 +- tracecmd/trace-record.c | 46 +++---------------------------- tracecmd/trace-stat.c | 4 +-- 7 files changed, 59 insertions(+), 49 deletions(-)
include/trace-cmd/trace-cmd.h +3 -0
@@ -409,6 +409,9 @@ void tracecmd_plog(const char *fmt, ...);void tracecmd_plog_error (const char *fmt, ...);int tracecmd_set_logfile (char *logfile); +/* --- System --- */ +int tracecmd_count_cpus (void ); +/* --- Hack! --- */ int tracecmd_blk_hack (struct tracecmd_input *handle);
lib/trace-cmd/trace-util.c +49 -0
52 lines not on this page · raw message
tracecmd/include/trace-local.h +0 -2
@@ -247,8 +247,6 @@ void update_first_instance(struct buffer_instance *instance, int topt);void show_instance_file (struct buffer_instance *instance,const char *name); -int count_cpus (void ); -/* moved from trace-cmd.h */ void tracecmd_create_top_instance (char *name);void tracecmd_remove_instances (void );
tracecmd/trace-agent.c +1 -1
@@ -203,7 +203,7 @@ static void agent_serve(unsigned int port)signal (SIGCHLD , handle_sigchld); - nr_cpus =count_cpus (); + nr_cpus =tracecmd_count_cpus (); page_size =getpagesize (); sd =make_vsock (port);
tracecmd/trace-profile.c +1 -1
@@ -1314,7 +1314,7 @@ void trace_init_profile(struct tracecmd_input *handle, struct hook_list *hook,* system. */ if (!h->cpus) - h->cpus =count_cpus (); + h->cpus =tracecmd_count_cpus ();list_head_init (&h->migrate_starts); h->cpu_starts =malloc (sizeof (*h->cpu_starts) * h->cpus);
tracecmd/trace-record.c +3 -43
70 lines not on this page · raw message
tracecmd/trace-stat.c +2 -2
@@ -738,7 +738,7 @@ static void report_cpumask(struct buffer_instance *instance) cont =strstrip (str);/* check to make sure all CPUs on this machine are set */ - cpus =count_cpus (); + cpus =tracecmd_count_cpus ();for (i =strlen (cont) -1 ; i >=0 && cpus >0 ; i--) {if (cont[i] ==',' )
@@ -898,7 +898,7 @@ void trace_stat (int argc, char **argv) instance =create_instance (optarg);if (!instance)die ("Failed to create instance" ); -add_instance (instance,count_cpus ()); +add_instance (instance,tracecmd_count_cpus ());/* top instance requires direct access */ if (!topt &&is_top_instance (first_instance)) first_instance = instance;
Signature · 1 line
2.24.1
PATCH v19 3/15 trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM
In reply to Tzvetomir Stoyanov (VMware)From: Tzvetomir Stoyanov <[email protected]>
In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in read_qemu_guests() function to initialize it. Implemented a new internal API, get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM.
--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
tracecmd/include/trace-local.h +2 -0
@@ -247,6 +247,8 @@ void update_first_instance(struct buffer_instance *instance, int topt);void show_instance_file (struct buffer_instance *instance,const char *name); +int get_guest_vcpu_pid (unsigned int guest_cid,unsigned int guest_vcpu); +/* moved from trace-cmd.h */ void tracecmd_create_top_instance (char *name);void tracecmd_remove_instances (void );
tracecmd/trace-record.c +54 -0
82 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v19 03/15] trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM
Quoted 32 lines · On Fri, 31 Jan 2020 14:10:59 +0200 "Tzvetomir Stoyanov (VMware)"…
From: Tzvetomir Stoyanov <[email protected]>
In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in
read_qemu_guests()function to initialize it. Implemented a new internal API,get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM.Signed-off-by Tzvetomir Stoyanov <[email protected]>--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)tracecmd/include/trace-local.h +2
-0@@ -247,6 +247,8 @@ void update_first_instance(struct buffer_instance *instance, int topt);void show_instance_file (struct buffer_instance *instance,const char *name); +int get_guest_vcpu_pid (unsigned int guest_cid,unsigned int guest_vcpu); +/* moved from trace-cmd.h */ void tracecmd_create_top_instance (char *name);void tracecmd_remove_instances (void );tracecmd/trace-record.c +0
-0
tracecmd/trace-record.c static bool is_digits(const char *s) · line 3039
Quoted 9 lines
tracecmd/trace-record.c +2
-0@@ -3031,10 +3031,12 @@ static bool is_digits(const char *s)return true; } +#define VCPUS_MAX 256struct guest {char *name;int cid;int pid; +int cpu_pid[VCPUS_MAX ];
Although this is probably fine for the near future, I don't like arbitrary limits that don't have to do with max sizes of the storage unit (like max int).
Why is the cpu_pid a static array and not one that is allocated? We could use realloc, couldn't we, in the loading of it?
-- Steve
Quoted 76 lines
tracecmd/trace-record.c +52
-0};static struct guest *guests;@@ -3052,6 +3054,43 @@ static char *get_qemu_guest_name(char *arg)return arg; } +static void read_qemu_guests_pids (char *guest_task,struct guest *guest) +{ +struct dirent *entry; +char path[PATH_MAX ]; +char *buf =NULL ; +size_t n =0 ; +int vcpu; +DIR *dir; +FILE *f; + +snprintf (path,sizeof (path),"/proc/%s/task" , guest_task); + dir =opendir (path); +if (!dir) +return ; + +while ((entry =readdir (dir))) { +if (!(entry->d_type ==DT_DIR &&is_digits (entry->d_name))) +continue ; + +snprintf (path,sizeof (path),"/proc/%s/task/%s/comm" , + guest_task, entry->d_name); + f =fopen (path,"r" ); +if (!f) +continue ; + +if (getline (&buf, &n, f) >=0 && +strncmp (buf,"CPU " ,4 ) ==0 ) { + vcpu =atoi (buf +4 ); +if (vcpu >=0 && vcpu <VCPUS_MAX ) + guest->cpu_pid[vcpu] =atoi (entry->d_name); + } + +fclose (f); + } +free (buf); +} +static void read_qemu_guests (void ) {static bool initialized;@@ -3115,6 +3154,8 @@ static void read_qemu_guests(void)if (!is_qemu) goto next; +read_qemu_guests_pids (entry->d_name, &guest); + guests =realloc (guests, (guests_len +1 ) *sizeof (*guests));if (!guests)die ("Can not allocate guest buffer" );@@ -3160,6 +3201,19 @@ static char *parse_guest_name(char *guest, int *cid, int *port)return guest; } +int get_guest_vcpu_pid (unsigned int guest_cid,unsigned int guest_vcpu) +{ +int i; + +if (!guests || guest_vcpu >=VCPUS_MAX ) +return -1 ; + +for (i =0 ; i < guests_len; i++) +if (guest_cid == guests[i].cid) +return guests[i].cpu_pid[guest_vcpu]; +return -1 ; +} +static void set_prio (int prio) {struct sched_param sp;
Re: [PATCH v19 03/15] trace-cmd: Find and store pids of tasks, which run virtual CPUs of given VM
Quoted 50 lines · Steven Rostedt
Quoted 32 lines · On Fri, 31 Jan 2020 14:10:59 +0200 "Tzvetomir Stoyanov (VMware)"…
From: Tzvetomir Stoyanov <[email protected]>
In order to match host and guest events, a mapping between guest VCPU and the host task, running this VCPU is needed. Extended existing struct guest to hold such mapping and added logic in
read_qemu_guests()function to initialize it. Implemented a new internal API,get_guest_vcpu_pid(), to retrieve VCPU-task mapping for given VM.Signed-off-by Tzvetomir Stoyanov <[email protected]>--- tracecmd/include/trace-local.h | 2 ++ tracecmd/trace-record.c | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)tracecmd/include/trace-local.h +2
-0@@ -247,6 +247,8 @@ void update_first_instance(struct buffer_instance *instance, int topt);void show_instance_file (struct buffer_instance *instance,const char *name); +int get_guest_vcpu_pid (unsigned int guest_cid,unsigned int guest_vcpu); +/* moved from trace-cmd.h */ void tracecmd_create_top_instance (char *name);void tracecmd_remove_instances (void );tracecmd/trace-record.c +0
-0tracecmd/trace-record.c static bool is_digits(const char *s) · line 3039
Quoted 9 lines
tracecmd/trace-record.c +2
-0@@ -3031,10 +3031,12 @@ static bool is_digits(const char *s)return true; } +#define VCPUS_MAX 256struct guest {char *name;int cid;int pid; +int cpu_pid[VCPUS_MAX ];Although this is probably fine for the near future, I don't like arbitrary limits that don't have to do with max sizes of the storage unit (like max int).
Quoted 3 lines
Why is the
cpu_pida static array and not one that is allocated? We could use realloc, couldn't we, in the loading of it?
I did it to simplify the PoC implementation and never changed it to be a dynamic array,
I'll reimplement it now.
Thanks Steven!
Quoted 79 lines
-- Steve
Quoted 76 lines
tracecmd/trace-record.c +52
-0};static struct guest *guests;@@ -3052,6 +3054,43 @@ static char *get_qemu_guest_name(char *arg)return arg; } +static void read_qemu_guests_pids (char *guest_task,struct guest *guest) +{ +struct dirent *entry; +char path[PATH_MAX ]; +char *buf =NULL ; +size_t n =0 ; +int vcpu; +DIR *dir; +FILE *f; + +snprintf (path,sizeof (path),"/proc/%s/task" , guest_task); + dir =opendir (path); +if (!dir) +return ; + +while ((entry =readdir (dir))) { +if (!(entry->d_type ==DT_DIR &&is_digits (entry->d_name))) +continue ; + +snprintf (path,sizeof (path),"/proc/%s/task/%s/comm" , + guest_task, entry->d_name); + f =fopen (path,"r" ); +if (!f) +continue ; + +if (getline (&buf, &n, f) >=0 && +strncmp (buf,"CPU " ,4 ) ==0 ) { + vcpu =atoi (buf +4 ); +if (vcpu >=0 && vcpu <VCPUS_MAX ) + guest->cpu_pid[vcpu] =atoi (entry->d_name); + } + +fclose (f); + } +free (buf); +} +static void read_qemu_guests (void ) {static bool initialized;@@ -3115,6 +3154,8 @@ static void read_qemu_guests(void)if (!is_qemu) goto next; +read_qemu_guests_pids (entry->d_name, &guest); + guests =realloc (guests, (guests_len +1 ) *sizeof (*guests));if (!guests)die ("Can not allocate guest buffer" );@@ -3160,6 +3201,19 @@ static char *parse_guest_name(char *guest, int *cid, int *port)return guest; } +int get_guest_vcpu_pid (unsigned int guest_cid,unsigned int guest_vcpu) +{ +int i; + +if (!guests || guest_vcpu >=VCPUS_MAX ) +return -1 ; + +for (i =0 ; i < guests_len; i++) +if (guest_cid == guests[i].cid) +return guests[i].cpu_pid[guest_vcpu]; +return -1 ; +} +static void set_prio (int prio) {struct sched_param sp;
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
PATCH v19 4/15 trace-cmd: Implement new API tracecmd_add_option_v()
In reply to Tzvetomir Stoyanov (VMware)From: Tzvetomir Stoyanov <[email protected]>
A new tracecmd API tracecmd_add_option_v() is introduced. It adds new option in trace.dat, similar to tracecmd_add_option(), but the option's data is passed as list of buffers. The standard struct iovec is used as input parameter, containing the option's data buffers.
--- include/trace-cmd/trace-cmd.h | 5 ++ include/traceevent/event-parse.h | 1 + lib/trace-cmd/trace-output.c | 117 +++++++++++++++++++++++++++---- 3 files changed, 108 insertions(+), 15 deletions(-)
include/trace-cmd/trace-cmd.h +5 -0
@@ -278,11 +278,16 @@ struct tracecmd_output *tracecmd_create_init_file_override(const char *output_fistruct tracecmd_option *tracecmd_add_option (struct tracecmd_output *handle,unsigned short id,int size,const void *data); +struct tracecmd_option * +tracecmd_add_option_v (struct tracecmd_output *handle, +unsigned short id,const struct iovec *vector,int count); +struct tracecmd_option *tracecmd_add_buffer_option (struct tracecmd_output *handle,const char *name,int cpus);int tracecmd_write_cpus (struct tracecmd_output *handle,int cpus);int tracecmd_write_options (struct tracecmd_output *handle); +int tracecmd_append_options (struct tracecmd_output *handle);int tracecmd_update_option (struct tracecmd_output *handle,struct tracecmd_option *option,int size,const void *data);
include/traceevent/event-parse.h +1 -0
@@ -11,6 +11,7 @@#include <stdio.h> #include <regex.h> #include <string.h> +#include <sys/uio.h> #include "traceevent/trace-seq.h"
lib/trace-cmd/trace-output.c +102 -15
161 lines not on this page · raw message
Signature · 1 line
2.24.1
PATCH v19 5/15 trace-cmd: Add new API to generate a unique ID of the tracing session
In reply to Tzvetomir Stoyanov (VMware)A new libtracecmd API is implemented:
unsigned long long tracecmd_generate_traceid(void);
At trace-cmd init phase a unique ID, used for the current tracing session.
--- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/trace-util.c | 37 ++++++++++++++++++++++++++++++++++ tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 1 + 4 files changed, 40 insertions(+)
include/trace-cmd/trace-cmd.h +1 -0
@@ -415,6 +415,7 @@ void tracecmd_plog_error(const char *fmt, ...);int tracecmd_set_logfile (char *logfile);/* --- System --- */ +unsigned long long tracecmd_generate_traceid (void );int tracecmd_count_cpus (void );/* --- Hack! --- */
lib/trace-cmd/trace-util.c +37 -0
@@ -18,6 +18,8 @@#include <sys/mount.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/sysinfo.h> +#include <time.h> #include "trace-cmd.h" #include "event-utils.h"
@@ -510,3 +512,38 @@ int tracecmd_count_cpus(void)return cpus; } + +#define FNV_64_PRIME 0x100000001b3ULL +/* +* tracecmd_generate_traceid - Generate a unique ID, used to identify +* the current tracing session +* +* Returns unique ID +*/ +unsigned long long tracecmd_generate_traceid (void ) +{ +unsigned long long hash =0 ; +unsigned char *ustr; +struct sysinfo sinfo; +struct timespec ts; +char *str =NULL ; + +clock_gettime (CLOCK_MONOTONIC_RAW , &ts); +sysinfo (&sinfo); +asprintf (&str,"%ld %ld %ld %ld %ld %ld %ld %ld %d" , + ts.tv_sec, ts.tv_nsec, + sinfo.loads[0 ], sinfo.loads[1 ], sinfo.loads[2 ], + sinfo.freeram, sinfo.sharedram, sinfo.freeswap, + sinfo.procs); +if (!str) +return 0 ; + ustr = (unsigned char *)str; + hash =0 ; +while (*ustr) { + hash ^= (unsigned long long )*ustr++; + hash *=FNV_64_PRIME ; + } + +free (str); +return hash; +}
tracecmd/include/trace-local.h +1 -0
@@ -185,6 +185,7 @@ struct pid_addr_maps {struct buffer_instance {struct buffer_instance *next;struct tracefs_instance *tracefs; +unsigned long long trace_id;char *cpumask;struct event_list *events;struct event_list **event_next;
tracecmd/trace-record.c +1 -0
@@ -5181,6 +5181,7 @@ void init_top_instance(void) top_instance.tracefs =tracefs_instance_alloc (NULL ); top_instance.cpu_count =tracecmd_count_cpus (); top_instance.flags =BUFFER_FL_KEEP ; + top_instance.trace_id =tracecmd_generate_traceid ();init_instance (&top_instance); }
Signature · 1 line
2.24.1
PATCH v19 6/15 trace-cmd: Store the session tracing ID in the trace.dat file
In reply to Tzvetomir Stoyanov (VMware)The ID of the current tracing session is stored in the trace.dat file. A new file option is introduced:
TRACECMD_OPTION_TRACEID
The data is stored in the file as unsigned long long integer
A new libtracecmd API is introduced, to get the ID when the trace.dat file is read:
unsigned long long tracecmd_get_traceid(struct tracecmd_input *handle);
--- include/trace-cmd/trace-cmd.h | 2 ++ lib/trace-cmd/trace-input.c | 16 ++++++++++++++++ tracecmd/trace-dump.c | 11 +++++++++++ tracecmd/trace-record.c | 9 +++++++++ 4 files changed, 38 insertions(+)
include/trace-cmd/trace-cmd.h +2 -0
@@ -109,6 +109,7 @@ enum {TRACECMD_OPTION_CPUCOUNT ,TRACECMD_OPTION_VERSION ,TRACECMD_OPTION_PROCMAPS , + TRACECMD_OPTION_TRACEID, }; enum {
@@ -151,6 +152,7 @@ int tracecmd_copy_headers(struct tracecmd_input *handle, int fd);void tracecmd_set_flag (struct tracecmd_input *handle,int flag);void tracecmd_clear_flag (struct tracecmd_input *handle,int flag);unsigned long tracecmd_get_flags (struct tracecmd_input *handle); +unsigned long long tracecmd_get_traceid (struct tracecmd_input *handle);void tracecmd_parse_trace_clock (struct tracecmd_input *handle,char *file,int size);
lib/trace-cmd/trace-input.c +16 -0
@@ -82,6 +82,7 @@ struct tracecmd_input {struct tep_plugin_list *plugin_list;struct tracecmd_input *parent;unsigned long flags; +unsigned long long trace_id;int fd;int long_size;int page_size;
@@ -2316,6 +2317,10 @@ static int handle_options(struct tracecmd_input *handle)if (buf[size-1 ] =='\0' )trace_pid_map_load (handle, buf);break ; +case TRACECMD_OPTION_TRACEID : + handle->trace_id =tep_read_number (handle->pevent, + &cpus,8 ); +break ;default :warning ("unknown option %d" , option);break ;
@@ -3419,3 +3424,14 @@ void tracecmd_set_show_data_func(struct tracecmd_input *handle, { handle->show_data_func = func; } + +/** +* tracecmd_get_traceid - get the trace id of the session +* @handle: input handle for the trace.dat file +* +* Returns the trace id, written in the trace file +*/ +unsigned long long tracecmd_get_traceid (struct tracecmd_input *handle) +{ +return handle->trace_id; +}
tracecmd/trace-dump.c +11 -0
@@ -358,6 +358,14 @@ static void dump_option_int(int fd, int size, char *desc)do_print (OPTIONS ,"%d\n" , val); } +static void dump_option_xlong (int fd,int size,char *desc) +{ +long long val; + +do_print (OPTIONS ,"\t\t[Option %s, %d bytes]\n" , desc, size); +read_file_number (fd, &val, size); +do_print (OPTIONS ,"0x%llX\n" , val); +}static void dump_options (int fd) {unsigned short option;
@@ -408,6 +416,9 @@ static void dump_options(int fd)case TRACECMD_OPTION_PROCMAPS :dump_option_string (fd, size,"PROCMAPS" );break ; +case TRACECMD_OPTION_TRACEID : +dump_option_xlong (fd, size,"TRACEID" ); +break ;default :do_print (OPTIONS ," %d %d\t[Unknown option, size - skipping]\n" , option, size);
tracecmd/trace-record.c +9 -0
@@ -3872,6 +3872,13 @@ add_pid_maps(struct tracecmd_output *handle, struct buffer_instance *instance)trace_seq_destroy (&s); } +static void +add_trace_id (struct tracecmd_output *handle,struct buffer_instance *instance) +{ +tracecmd_add_option (handle,TRACECMD_OPTION_TRACEID , +sizeof (long long ), &instance->trace_id); +} +static void add_buffer_stat (struct tracecmd_output *handle,struct buffer_instance *instance) {
@@ -3977,6 +3984,8 @@ static void add_options(struct tracecmd_output *handle, struct common_record_conadd_option_hooks (handle);add_uname (handle);add_version (handle); +if (!no_top_instance ()) +add_trace_id (handle, &top_instance); }static void write_guest_file (struct buffer_instance *instance)
Signature · 1 line
2.24.1
PATCH v19 7/15 trace-cmd: Add definitions of htonll() and ntohll()
In reply to Tzvetomir Stoyanov (VMware)From: Tzvetomir Stoyanov <[email protected]>
Reorganized libtracecmd internal headers:
- Renamed trace-cmd-local.h to trace-write-local.h
This internal header implements static __do_write_check() function, used in trace-input.c, trace-output.c and trace-msg.c files. The header cannot be included in other files, which do not call __do_write_check(). The new name trace-write-local.h reflects more closely the purpose of the file.
- Added trace-cmd-local.h file, to share code inside libtracecmd.
- Added definitions of htonll() and ntohll(), if not already defined, intrace-cmd-local.h
--- lib/trace-cmd/include/trace-cmd-local.h | 45 ++++++----------------- lib/trace-cmd/include/trace-write-local.h | 43 ++++++++++++++++++++++ lib/trace-cmd/trace-input.c | 2 +- lib/trace-cmd/trace-msg.c | 2 +- lib/trace-cmd/trace-output.c | 2 + 5 files changed, 58 insertions(+), 36 deletions(-) create mode 100644 lib/trace-cmd/include/trace-write-local.h
lib/trace-cmd/include/trace-cmd-local.h +11 -34
@@ -6,10 +6,8 @@#ifndef _TRACE_CMD_LOCAL_H#define _TRACE_CMD_LOCAL_H -/* Local for trace-input.c and trace-output.c */ - -#include "trace-cmd.h" -#include "event-utils.h" +/* Can be overridden */ +void warning (const char *fmt, ...);/* trace.dat file format version */ #define FILE_VERSION 6
@@ -18,36 +16,15 @@#define STR (x) _STR(x)#define FILE_VERSION_STRING STR(FILE_VERSION) -static ssize_t __do_write (int fd,const void *data,size_t size) -{ -ssize_t tot =0 ; -ssize_t w; - -do { - w =write (fd, data + tot, size - tot); - tot += w; - -if (!w) -break ; -if (w <0 ) -return w; - }while (tot != size); - -return tot; -} - -static ssize_t -__do_write_check (int fd,const void *data,size_t size) -{ -ssize_t ret; - - ret =__do_write (fd, data, size); -if (ret <0 ) -return ret; -if (ret != size) -return -1 ; +#ifndef htonll +# if __BYTE_ORDER == __LITTLE_ENDIAN +#define htonll (x) __bswap_64(x) +#define ntohll (x) __bswap_64(x) +#else +#define htonll (x) (x) +#define ntohll (x) (x) +#endif +#endif -return 0 ; -}#endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/include/trace-write-local.h new file +43 -0
43 lines not on this page · raw message
lib/trace-cmd/trace-input.c +1 -1
@@ -15,7 +15,7 @@#include <linux/time64.h> -#include "trace-cmd-local.h" +#include "trace-write-local.h" #include "trace-local.h" #include "kbuffer.h" #include "list.h"
lib/trace-cmd/trace-msg.c +1 -1
@@ -22,7 +22,7 @@#include <sys/types.h> #include <linux/types.h> -#include "trace-cmd-local.h" +#include "trace-write-local.h" #include "trace-local.h" #include "trace-msg.h"
lib/trace-cmd/trace-output.c +2 -0
@@ -21,7 +21,9 @@#include <glob.h> #include "tracefs.h" +#include "trace-cmd.h" #include "trace-cmd-local.h" +#include "trace-write-local.h" #include "list.h" #include "trace-msg.h"
Signature · 1 line
2.24.1
PATCH v19 8/15 trace-cmd: Exchange tracing IDs between host and guest
In reply to Tzvetomir Stoyanov (VMware)Extend the trace request and trace reply messages, to include also the IDs of host and guest tracing sessions. Those IDs are used to unambiguously match the tracing sessions, when reading trace.dat files.
--- include/trace-cmd/trace-cmd.h | 12 ++++++++---- lib/trace-cmd/trace-msg.c | 31 ++++++++++++++++++++++--------- tracecmd/include/trace-local.h | 3 ++- tracecmd/trace-agent.c | 10 +++++++--- tracecmd/trace-record.c | 10 +++++++--- 5 files changed, 46 insertions(+), 20 deletions(-)
include/trace-cmd/trace-cmd.h +8 -4
@@ -370,16 +370,20 @@ 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,bool use_fifos); +int argc,char **argv,bool use_fifos, +unsigned long long trace_id);int tracecmd_msg_recv_trace_req (struct tracecmd_msg_handle *msg_handle, -int *argc,char ***argv,bool *use_fifos); +int *argc,char ***argv,bool *use_fifos, +unsigned long long *trace_id);int tracecmd_msg_send_trace_resp (struct tracecmd_msg_handle *msg_handle,int nr_cpus,int page_size, -unsigned int *ports,bool use_fifos); +unsigned int *ports,bool use_fifos, +unsigned long long trace_id);int tracecmd_msg_recv_trace_resp (struct tracecmd_msg_handle *msg_handle,int *nr_cpus,int *page_size, -unsigned int **ports,bool *use_fifos); +unsigned int **ports,bool *use_fifos, +unsigned long long *trace_id);/* --- Plugin handling --- */ extern struct tep_plugin_option trace_ftrace_options[];
lib/trace-cmd/trace-msg.c +22 -9
@@ -23,6 +23,7 @@#include <linux/types.h> #include "trace-write-local.h" +#include "trace-cmd-local.h" #include "trace-local.h" #include "trace-msg.h"
@@ -63,12 +64,14 @@ struct tracecmd_msg_rinit {struct tracecmd_msg_trace_req {be32 flags;be32 argc; +u64 trace_id; } __attribute__((packed));struct tracecmd_msg_trace_resp {be32 flags;be32 cpus;be32 page_size; +u64 trace_id; } __attribute__((packed));struct tracecmd_msg_header {
@@ -811,7 +814,8 @@ int tracecmd_msg_wait_close_resp(struct tracecmd_msg_handle *msg_handle)return tracecmd_msg_wait_for_cmd (msg_handle,MSG_CLOSE_RESP ); } -static int make_trace_req (struct tracecmd_msg *msg,int argc,char **argv,bool use_fifos) +static int make_trace_req (struct tracecmd_msg *msg,int argc,char **argv, +bool use_fifos,unsigned long long trace_id) {size_t args_size =0 ;char *p;
@@ -823,6 +827,7 @@ static int make_trace_req(struct tracecmd_msg *msg, int argc, char **argv, bool 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->trace_req.trace_id =htonll (trace_id); msg->buf =calloc (args_size,1 );if (!msg->buf)return -ENOMEM ;
@@ -835,13 +840,14 @@ static int make_trace_req(struct tracecmd_msg *msg, int argc, char **argv, bool }int tracecmd_msg_send_trace_req (struct tracecmd_msg_handle *msg_handle, -int argc,char **argv,bool use_fifos) +int argc,char **argv,bool use_fifos, +unsigned long long trace_id) {struct tracecmd_msg msg;int ret;tracecmd_msg_init (MSG_TRACE_REQ , &msg); - ret =make_trace_req (&msg, argc, argv, use_fifos); + ret =make_trace_req (&msg, argc, argv, use_fifos, trace_id);if (ret <0 )return ret;
@@ -854,7 +860,8 @@ 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,bool *use_fifos) +int *argc,char ***argv,bool *use_fifos, +unsigned long long *trace_id) {struct tracecmd_msg msg;char *p, *buf_end, **args;
@@ -901,7 +908,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 ; - + *trace_id =ntohll (msg.trace_req.trace_id);/* * On success we're passing msg.buf to the caller through argv[0] so we * reset it here before calling msg_free().
@@ -921,7 +928,8 @@ out: }static int make_trace_resp (struct tracecmd_msg *msg,int page_size,int nr_cpus, -unsigned int *ports,bool use_fifos) +unsigned int *ports,bool use_fifos, +unsigned long long trace_id) {int data_size;
@@ -935,19 +943,22 @@ static int make_trace_resp(struct tracecmd_msg *msg, int page_size, int nr_cpus, 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); + msg->trace_resp.trace_id =htonll (trace_id);return 0 ; }int tracecmd_msg_send_trace_resp (struct tracecmd_msg_handle *msg_handle,int nr_cpus,int page_size, -unsigned int *ports,bool use_fifos) +unsigned int *ports,bool use_fifos, +unsigned long long trace_id) {struct tracecmd_msg msg;int ret;tracecmd_msg_init (MSG_TRACE_RESP , &msg); - ret =make_trace_resp (&msg, page_size, nr_cpus, ports, use_fifos); + ret =make_trace_resp (&msg, page_size, nr_cpus, ports, + use_fifos, trace_id);if (ret <0 )return ret;
@@ -956,7 +967,8 @@ 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,bool *use_fifos) +unsigned int **ports,bool *use_fifos, +unsigned long long *trace_id) {struct tracecmd_msg msg;char *p, *buf_end;
@@ -981,6 +993,7 @@ int tracecmd_msg_recv_trace_resp(struct tracecmd_msg_handle *msg_handle, *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); + *trace_id =ntohll (msg.trace_resp.trace_id); *ports =calloc (*nr_cpus,sizeof (**ports));if (!*ports) { ret = -ENOMEM ;
tracecmd/include/trace-local.h +2 -1
@@ -101,7 +101,8 @@ void trace_dump(int argc, char **argv);int trace_record_agent (struct tracecmd_msg_handle *msg_handle,int cpus,int *fds, -int argc,char **argv,bool use_fifos); +int argc,char **argv,bool use_fifos, +unsigned long long trace_id);struct hook_list ;
tracecmd/trace-agent.c +7 -3
@@ -128,6 +128,7 @@ cleanup:static void agent_handle (int sd,int nr_cpus,int page_size) {struct tracecmd_msg_handle *msg_handle; +unsigned long long trace_id;unsigned int *ports;char **argv =NULL ;int argc =0 ;
@@ -144,7 +145,8 @@ 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, &use_fifos); + ret =tracecmd_msg_recv_trace_req (msg_handle, &argc, &argv, + &use_fifos, &trace_id);if (ret <0 )die ("Failed to receive trace request" );
@@ -154,12 +156,14 @@ static void agent_handle(int sd, int nr_cpus, int page_size)if (!use_fifos)make_vsocks (nr_cpus, fds, ports); + trace_id =tracecmd_generate_traceid (); ret =tracecmd_msg_send_trace_resp (msg_handle, nr_cpus, page_size, - ports, use_fifos); + ports, use_fifos, trace_id);if (ret <0 )die ("Failed to send trace response" ); -trace_record_agent (msg_handle, nr_cpus, fds, argc, argv, use_fifos); +trace_record_agent (msg_handle, nr_cpus, fds, argc, argv, + use_fifos, trace_id);free (argv[0 ]);free (argv);
tracecmd/trace-record.c +7 -3
@@ -3663,12 +3663,14 @@ static void connect_to_agent(struct buffer_instance *instance)die ("Failed to allocate message handle" ); ret =tracecmd_msg_send_trace_req (msg_handle, instance->argc, - instance->argv, use_fifos); + instance->argv, use_fifos, + top_instance.trace_id);if (ret <0 )die ("Failed to send trace request" ); ret =tracecmd_msg_recv_trace_resp (msg_handle, &nr_cpus, &page_size, - &ports, &use_fifos); + &ports, &use_fifos, + &instance->trace_id);if (ret <0 )die ("Failed to receive trace response" );
@@ -6214,7 +6216,8 @@ void trace_record(int argc, char **argv)int trace_record_agent (struct tracecmd_msg_handle *msg_handle,int cpus,int *fds,int argc,char **argv, -bool use_fifos) +bool use_fifos, +unsigned long long trace_id) {struct common_record_context ctx;char **argv_plus;
@@ -6244,6 +6247,7 @@ int trace_record_agent(struct tracecmd_msg_handle *msg_handle, ctx.instance->flags |=BUFFER_FL_AGENT ; ctx.instance->msg_handle = msg_handle; msg_handle->version =V3_PROTOCOL ; + top_instance.trace_id = trace_id;record_trace (argc, argv, &ctx);free (argv_plus);
Signature · 1 line
2.24.1
PATCH v19 9/15 trace-cmd: Implement new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT
In reply to Tzvetomir Stoyanov (VMware)From: Tzvetomir Stoyanov <[email protected]>
The TRACECMD_OPTION_TIME_SHIFT is used when synchronizing trace time stamps between two trace.dat files. It contains multiple long long (time, offset) pairs, describing time stamps _offset_, measured in the given local _time_. The content of the option buffer is:
8 bytes - long long integer, ID of the tracing session
4 bytes - integer, count of timestamp offsets
long long array of size _count_, local time in which the offset is measured long long array of size _count_, offset of the time stamps
--- include/trace-cmd/trace-cmd.h | 3 + lib/trace-cmd/trace-input.c | 183 +++++++++++++++++++++++++++++++++- tracecmd/trace-dump.c | 49 +++++++++ 3 files changed, 233 insertions(+), 2 deletions(-)
include/trace-cmd/trace-cmd.h +3 -0
@@ -110,6 +110,7 @@ enum {TRACECMD_OPTION_VERSION ,TRACECMD_OPTION_PROCMAPS ,TRACECMD_OPTION_TRACEID , + TRACECMD_OPTION_TIME_SHIFT, }; enum {
@@ -153,6 +154,8 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);void tracecmd_clear_flag (struct tracecmd_input *handle,int flag);unsigned long tracecmd_get_flags (struct tracecmd_input *handle);unsigned long long tracecmd_get_traceid (struct tracecmd_input *handle); +unsigned long long tracecmd_get_tsync_peer (struct tracecmd_input *handle); +int tracecmd_enable_tsync (struct tracecmd_input *handle,bool enable);void tracecmd_parse_trace_clock (struct tracecmd_input *handle,char *file,int size);
lib/trace-cmd/trace-input.c +181 -2
246 lines not on this page · raw message
tracecmd/trace-dump.c +49 -0
61 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v19 09/15] trace-cmd: Implement new option in trace.dat file: TRACECMD_OPTION_TIME_SHIFT
Quoted 11 lines · On Fri, 31 Jan 2020 14:11:05 +0200 "Tzvetomir Stoyanov (VMware)"…
lib/trace-cmd/trace-input.c +7
-0@@ -2271,6 +2390,28 @@ static int handle_options(struct tracecmd_input *handle) offset =strtoll (buf,NULL ,0 ); handle->ts_offset += offset;break ; +case TRACECMD_OPTION_TIME_SHIFT : +/* +* long long int (8 bytes) trace session ID +* int (4 bytes) count of timestamp offsets. +* long long array of size [count] of times, +* when the offsets were calculated. +* long long array of size [count] of timestamp offsets.
lib/trace-cmd/trace-input.c static int handle_options(struct tracecmd_input *handle) · line 2409
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+*/ +if (size <12 || handle->flags &TRACECMD_FL_IGNORE_DATE ) +break ; + handle->host.trace_id =tep_read_number (handle->pevent, + buf,8 ); + handle->host.ts_samples_count =tep_read_number (handle->pevent, + buf +8 ,4 ); + samples_size = (8 * handle->host.ts_samples_count); +if (size != (12 + (2 * samples_size))) +break ;
Instead of failing silently here, we should have some kind of warning.
-- Steve
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ handle->host.ts_samples =malloc (2 * samples_size); +if (!handle->host.ts_samples) +return -ENOMEM ; +tsync_offset_load (handle, buf +12 ); +break ;
PATCH v19 10/15 trace-cmd: Add guest information in host's trace.dat file
In reply to Tzvetomir Stoyanov (VMware)New trace.dat option is introduced: TRACECMD_OPTION_GUEST. Written in the host's trace.dat file, it contains information about guests, traced at the same time: guest trace ID, number of VCPUs and PIDs of the host tasks, running those VCPU. The data is stored in the file as NULL terminated string:
"Guest %s %llu %d\n" -> guest name, number of VCPUs "%d %d\n" -> VCPU, PID of host task ..... "%d %d\n" -> VCPU, PID of host task
--- include/trace-cmd/trace-cmd.h | 5 ++ lib/trace-cmd/trace-input.c | 125 ++++++++++++++++++++++++++++++++++ tracecmd/trace-dump.c | 62 +++++++++++++++++ tracecmd/trace-record.c | 61 +++++++++++++++++ 4 files changed, 253 insertions(+)
include/trace-cmd/trace-cmd.h +5 -0
@@ -111,6 +111,7 @@ enum {TRACECMD_OPTION_PROCMAPS ,TRACECMD_OPTION_TRACEID ,TRACECMD_OPTION_TIME_SHIFT , + TRACECMD_OPTION_GUEST, }; enum {
@@ -154,6 +155,10 @@ void tracecmd_set_flag(struct tracecmd_input *handle, int flag);void tracecmd_clear_flag (struct tracecmd_input *handle,int flag);unsigned long tracecmd_get_flags (struct tracecmd_input *handle);unsigned long long tracecmd_get_traceid (struct tracecmd_input *handle); +int tracecmd_get_guest_cpumap (struct tracecmd_input *handle, +unsigned long long trace_id, +char **name, +int *vcpu_count,int **cpu_pid);unsigned long long tracecmd_get_tsync_peer (struct tracecmd_input *handle);int tracecmd_enable_tsync (struct tracecmd_input *handle,bool enable);
lib/trace-cmd/trace-input.c +125 -0
161 lines not on this page · raw message
tracecmd/trace-dump.c +62 -0
74 lines not on this page · raw message
tracecmd/trace-record.c +61 -0
79 lines not on this page · raw message
Signature · 1 line
2.24.1
Re: [PATCH v19 10/15] trace-cmd: Add guest information in host's trace.dat file
Quoted 10 lines · On Fri, 31 Jan 2020 14:11:06 +0200 "Tzvetomir Stoyanov (VMware)"…
New trace.dat option is introduced:
TRACECMD_OPTION_GUEST. Written in the host's trace.dat file, it contains information about guests, traced at the same time: guest trace ID, number of VCPUs and PIDs of the host tasks, running those VCPU. The data is stored in the file as NULL terminated string:"Guest %s %llu %d\n" -> guest name, number of VCPUs "%d %d\n" -> VCPU, PID of host task ..... "%d %d\n" -> VCPU, PID of host task
Looks like you updated the code to not store this information as a string (but as binary), but the change log hasn't been updated.
-- Steve
Quoted 2 lines
Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>---
Re: [PATCH v19 10/15] trace-cmd: Add guest information in host's trace.dat file
In reply to Tzvetomir Stoyanov (VMware)Quoted 19 lines · On Fri, 31 Jan 2020 14:11:06 +0200 "Tzvetomir Stoyanov (VMware)"…
lib/trace-cmd/trace-input.c +19
-0+static int trace_guest_load (struct tracecmd_input *handle,char *buf,int size) +{ +struct guest_trace_info *guest =NULL ; +int cpu; +int i; + + guest =calloc (1 ,sizeof (struct guest_trace_info )); +if (!guest) + goto error; + +/* +* Guest name, null terminated string +* long long (8 bytes) trace-id +* int (4 bytes) number of guest CPUs +* array of size number of guest CPUs: +* int (4 bytes) Guest CPU id +* int (4 bytes) Host PID, running the guest CPU +*/ +
lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2250
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+ guest->name =strndup (buf, size); +if (!guest->name) + goto error; + buf +=strlen (guest->name) +1 ; + size -=strlen (guest->name) +1 ; + +if (size <sizeof (long long )) + goto error; + guest->trace_id =tep_read_number (handle->pevent, buf,sizeof (long long )); + buf +=sizeof (long long );
Don't we need:
size -= sizeof(long long);
lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2255
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ +if (size <sizeof (int )) + goto error; + guest->vcpu_count =tep_read_number (handle->pevent, buf,sizeof (int )); + buf +=sizeof (int );
size -= sizeof(int);
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ + guest->cpu_pid =calloc (guest->vcpu_count,sizeof (int )); +if (!guest->cpu_pid) + goto error; +
lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2270
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+for (i =0 ; i < guest->vcpu_count; i++) { +if (size <2 *sizeof (int )) + goto error; + cpu =tep_read_number (handle->pevent, buf,sizeof (int )); + buf +=sizeof (int ); +if (cpu >= guest->vcpu_count) + goto error; + guest->cpu_pid[cpu] =tep_read_number (handle->pevent, + buf,sizeof (int )); + buf +=sizeof (int );
size -= 2 * sizeof(int);
-- Steve
Quoted 15 lines
lib/trace-cmd/trace-input.c +15
-0+ } + + guest->next = handle->guest; + handle->guest = guest; +return 0 ; + +error: +if (guest) { +free (guest->cpu_pid); +free (guest->name); +free (guest); + } +return -1 ; +} +
Re: [PATCH v19 10/15] trace-cmd: Add guest information in host's trace.dat file
Quoted 55 lines · Steven Rostedt
Quoted 19 lines · On Fri, 31 Jan 2020 14:11:06 +0200 "Tzvetomir Stoyanov (VMware)"…
lib/trace-cmd/trace-input.c +19
-0+static int trace_guest_load (struct tracecmd_input *handle,char *buf,int size) +{ +struct guest_trace_info *guest =NULL ; +int cpu; +int i; + + guest =calloc (1 ,sizeof (struct guest_trace_info )); +if (!guest) + goto error; + +/* +* Guest name, null terminated string +* long long (8 bytes) trace-id +* int (4 bytes) number of guest CPUs +* array of size number of guest CPUs: +* int (4 bytes) Guest CPU id +* int (4 bytes) Host PID, running the guest CPU +*/ +lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2250
Quoted 10 lines
lib/trace-cmd/trace-input.c +10
-0+ guest->name =strndup (buf, size); +if (!guest->name) + goto error; + buf +=strlen (guest->name) +1 ; + size -=strlen (guest->name) +1 ; + +if (size <sizeof (long long )) + goto error; + guest->trace_id =tep_read_number (handle->pevent, buf,sizeof (long long )); + buf +=sizeof (long long );Don't we need:
size -= sizeof(long long);lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2255
Quoted 5 lines
lib/trace-cmd/trace-input.c +5
-0+ +if (size <sizeof (int )) + goto error; + guest->vcpu_count =tep_read_number (handle->pevent, buf,sizeof (int )); + buf +=sizeof (int );size -= sizeof(int);Quoted 9 lines
lib/trace-cmd/trace-input.c +9
-0+ + guest->cpu_pid =calloc (guest->vcpu_count,sizeof (int )); +if (!guest->cpu_pid) + goto error; + +for (i =0 ; i < guest->vcpu_count; i++) { +if (size <2 *sizeof (int )) + goto error; + cpu =tep_read_number (handle->pevent, buf,sizeof (int ));
Quoted 9 lines
lib/trace-cmd/trace-input.c static void procmap_free(struct pid_addr_maps *maps) · line 2270
Quoted 6 lines
lib/trace-cmd/trace-input.c +6
-0+ buf +=sizeof (int ); +if (cpu >= guest->vcpu_count) + goto error; + guest->cpu_pid[cpu] =tep_read_number (handle->pevent, + buf,sizeof (int )); + buf +=sizeof (int );size -= 2 * sizeof(int);
I'll add them, thanks!
Quoted 17 lines
-- Steve
Quoted 15 lines
lib/trace-cmd/trace-input.c +15
-0+ } + + guest->next = handle->guest; + handle->guest = guest; +return 0 ; + +error: +if (guest) { +free (guest->cpu_pid); +free (guest->name); +free (guest); + } +return -1 ; +} +
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov VMware Open Source Technology Center
PATCH v19 11/15 trace-cmd: Add host trace clock as guest trace argument
In reply to Tzvetomir Stoyanov (VMware)When tracing host and guest machines, both should use the same tracing clock for event timestamps. If a clock is specified as host tracing argument, with option "-C clock_name", the same is injected as guest tracing argument. If the user wants to use different tracing clocks, it can specify it using "-C clock_name" as guest tracing argument. In that case, the one specified by the user has higher priority.
--- tracecmd/include/trace-local.h | 1 + tracecmd/trace-record.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)
tracecmd/include/trace-local.h +1 -0
@@ -167,6 +167,7 @@ enum buffer_instance_flags {BUFFER_FL_PROFILE =1 <<1 ,BUFFER_FL_GUEST =1 <<2 ,BUFFER_FL_AGENT =1 <<3 , + BUFFER_FL_HAS_CLOCK = 1 << 4, }; struct func_list {
tracecmd/trace-record.c +21 -0
@@ -5532,6 +5532,8 @@ static void parse_record_options(int argc,char *sav;int name_counter =0 ;int neg_event =0 ; +struct buffer_instance *instance; +bool guest_sync_set = false;init_common_record_context (ctx, curr_cmd);
@@ -5575,6 +5577,8 @@ static void parse_record_options(int argc,*/ if (c !='B' && c !='A' &&is_guest (ctx->instance)) {add_arg (ctx->instance, c, opts, long_options, optarg); +if (c =='C' ) + ctx->instance->flags |=BUFFER_FL_HAS_CLOCK ;continue ; }
@@ -5689,6 +5693,9 @@ static void parse_record_options(int argc,break ;case 'C' : ctx->instance->clock = optarg; + ctx->instance->flags |=BUFFER_FL_HAS_CLOCK ; +if (is_top_instance (ctx->instance)) + guest_sync_set = true;break ;case 'v' : neg_event =1 ;
@@ -5913,6 +5920,20 @@ static void parse_record_options(int argc,add_argv (instance,"--date" , true); } } +if (guest_sync_set) { +/* If -C is specified, prepend clock to all guest VM flags */ +for_all_instances (instance) { +if (top_instance.clock) { +if (is_guest (instance) && + !(instance->flags &BUFFER_FL_HAS_CLOCK )) { +add_argv (instance, + (char *)top_instance.clock, + true); +add_argv (instance,"-C" , true); + } + } + } + }if (!ctx->filtered && ctx->instance->filter_mod) add_func(&ctx->instance->filter_funcs,
Signature · 1 line
2.24.1
PATCH v19 12/15 trace-cmd: Refactor few trace-cmd internal functions.
In reply to Tzvetomir Stoyanov (VMware)In order to reuse code inside trace-cmd application context, few functions are made non static and "trace_" is prepended to their names:
int trace_make_vsock(unsigned int port); int trace_get_vsock_port(int sd, unsigned int *port); int trace_open_vsock(unsigned int cid, unsigned int port); char *trace_get_guest_file(const char *file, const char *guest);
--- tracecmd/include/trace-local.h | 6 ++++++ tracecmd/trace-agent.c | 10 +++++----- tracecmd/trace-record.c | 18 +++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-)
tracecmd/include/trace-local.h +6 -0
@@ -263,6 +263,12 @@ void tracecmd_disable_tracing(void);void tracecmd_enable_tracing (void );void tracecmd_stat_cpu (struct trace_seq *s,int cpu); +int trace_make_vsock (unsigned int port); +int trace_get_vsock_port (int sd,unsigned int *port); +int trace_open_vsock (unsigned int cid,unsigned int port); + +char *trace_get_guest_file (const char *file,const char *guest); +/* No longer in event-utils.h */ void __noreturn die (const char *fmt, ...);/* Can be overriden */ void *malloc_or_die (unsigned int size);/* Can be overridden */
tracecmd/trace-agent.c +5 -5
@@ -40,7 +40,7 @@ static int get_local_cid(unsigned int *cid)return ret; } -static int make_vsock (unsigned int port) +int trace_make_vsock (unsigned int port) {struct sockaddr_vm addr = { .svm_family =AF_VSOCK ,
@@ -64,7 +64,7 @@ static int make_vsock(unsigned int port)return sd; } -static int get_vsock_port (int sd,unsigned int *port) +int trace_get_vsock_port (int sd,unsigned int *port) {struct sockaddr_vm addr;socklen_t addr_len =sizeof (addr);
@@ -87,11 +87,11 @@ static void make_vsocks(int nr, int *fds, unsigned int *ports)int i, fd, ret;for (i =0 ; i < nr; i++) { - fd =make_vsock (VMADDR_PORT_ANY ); + fd =trace_make_vsock (VMADDR_PORT_ANY );if (fd <0 )die ("Failed to open vsocket" ); - ret =get_vsock_port (fd, &port); + ret =trace_get_vsock_port (fd, &port);if (ret <0 )die ("Failed to get vsocket address" );
@@ -210,7 +210,7 @@ static void agent_serve(unsigned int port) nr_cpus =tracecmd_count_cpus (); page_size =getpagesize (); - sd =make_vsock (port); + sd =trace_make_vsock (port);if (sd <0 )die ("Failed to open vsocket" );
tracecmd/trace-record.c +9 -9
@@ -537,7 +537,7 @@ static char *get_temp_file(struct buffer_instance *instance, int cpu)return file; } -static char *get_guest_file (const char *file,const char *guest) +char *trace_get_guest_file (const char *file,const char *guest) {const char *p;char *out =NULL ;
@@ -2930,7 +2930,7 @@ static int connect_port(const char *host, unsigned int port) } #ifdef VSOCK -static int open_vsock(unsigned int cid, unsigned int port) +int trace_open_vsock(unsigned int cid, unsigned int port) { struct sockaddr_vm addr = { .svm_family = AF_VSOCK,
@@ -2993,7 +2993,7 @@ static bool can_splice_read_vsock(void) }#else -static inline int open_vsock (unsigned int cid,unsigned int port) +int trace_open_vsock (unsigned int cid,unsigned int port) {die ("vsock is not supported" );return -1 ;
@@ -3274,7 +3274,7 @@ create_recorder_instance(struct buffer_instance *instance, const char *file, intif (instance->use_fifos) fd = instance->fds[cpu];else - fd =open_vsock (instance->cid, instance->client_ports[cpu]); + fd =trace_open_vsock (instance->cid, instance->client_ports[cpu]);if (fd <0 )die ("Failed to connect to agent" );
@@ -3666,7 +3666,7 @@ static void connect_to_agent(struct buffer_instance *instance) use_fifos = nr_fifos >0 ; } - sd =open_vsock (instance->cid, instance->port); + sd =trace_open_vsock (instance->cid, instance->port);if (sd <0 )die ("Failed to connect to vsocket @%u:%u" , instance->cid, instance->port);
@@ -3717,8 +3717,8 @@ static void setup_guest(struct buffer_instance *instance)int fd;/* Create a place to store the guest meta data */ - file =get_guest_file (output_file, -tracefs_instance_get_name (instance->tracefs)); + file =trace_get_guest_file (output_file, +tracefs_instance_get_name (instance->tracefs));if (!file)die ("Failed to allocate memory" );
@@ -4054,8 +4054,8 @@ static void write_guest_file(struct buffer_instance *instance)char **temp_files;int i, fd; - file =get_guest_file (output_file, -tracefs_instance_get_name (instance->tracefs)); + file =trace_get_guest_file (output_file, +tracefs_instance_get_name (instance->tracefs));if (!file)die ("Failed to allocate memory" );
Signature · 1 line
2.24.1
PATCH v19 13/15 trace-cmd: Basic infrastructure for host - guest timestamp synchronization
In reply to Tzvetomir Stoyanov (VMware)The infrastructure for host - guest timestamp synchronization is divided in two parts:
- logic in libtracecmd
- logic in trace-cmd application
The libtracecmd is extended with new trace message, MSG_TIME_SYNC, used to exchange time synch information between host and guest. The trace request and response messages are extended to handle the time synch negotiation. The logic is implemented in trace-timesync.c file:
- Register / unregister time sync protocols.
- Choosing what protocol will be used for a tracing session.
- Init common context, used by all protocols - a ftrace instance, vsockets.
- Invoke protocol routines to perform time synchronization.
- Store calculated offsets in an array.
The trace-cmd application is extended to perform timestamp synchronization.
The main logic is in trace-tsync.c file
- Negotiate what time synch algorithm will be used for the tracing session.
- Run pthreads, to do continuous timestamp synchronization, during the trace.
- Store calculated offsets in guest's trace.dat file, using TRACECMD_OPTION_TIME_SHIFT option
- A new trace-cmd option is added, to control the timestamp synchronization:
--tsync-interval: set the time sync loop interval, in ms. The default
value is 0 - timestamp synchronization is performed twice, before and after the trace. If a negative number is specified, the timestamp synchronization is disabled.
--- Documentation/trace-cmd-record.1.txt | 7 + include/trace-cmd/trace-cmd.h | 58 ++- lib/trace-cmd/Makefile | 1 + lib/trace-cmd/include/trace-tsync-local.h | 38 ++ lib/trace-cmd/trace-msg.c | 395 ++++++++++++++--- lib/trace-cmd/trace-timesync.c | 498 ++++++++++++++++++++++ tracecmd/Makefile | 3 +- tracecmd/include/trace-local.h | 11 + tracecmd/trace-agent.c | 39 +- tracecmd/trace-record.c | 40 +- tracecmd/trace-tsync.c | 272 ++++++++++++ tracecmd/trace-usage.c | 4 + 12 files changed, 1299 insertions(+), 67 deletions(-) create mode 100644 lib/trace-cmd/include/trace-tsync-local.h create mode 100644 lib/trace-cmd/trace-timesync.c create mode 100644 tracecmd/trace-tsync.c
Documentation/trace-cmd-record.1.txt +7 -0
@@ -338,6 +338,13 @@ OPTIONS the offset will also be in nanoseconds even if the displayed units are in microseconds. +*--tsync-interval*:: + Set the loop interval, in ms, for timestamps synchronization with guests: + If a negative number is specified, timestamps synchronization is disabled + If 0 is specified, no loop is performed - timestamps offset is calculated only twice," + at the beginning and at the end of the trace. + Timestamps synchronization with guests works only if there is support for VSOCK.\n" + *--stderr*:: Have output go to stderr instead of stdout, but the output of the command executed will not be changed. This is useful if you want to monitor the
include/trace-cmd/trace-cmd.h +54 -4
73 lines not on this page · raw message
lib/trace-cmd/Makefile +1 -0
@@ -16,6 +16,7 @@ OBJS += trace-util.o OBJS += trace-filter-hash.o OBJS += trace-msg.o OBJS += trace-plugin.o +OBJS += trace-timesync.o # Additional util objects OBJS += trace-blk-hack.o
lib/trace-cmd/include/trace-tsync-local.h new file +38 -0
@@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ +/* +* Copyright (C) 2019, VMware, Tzvetomir Stoyanov <[email protected]> +* +*/ +#ifndef _TRACE_TSYNC_LOCAL_H +#define _TRACE_TSYNC_LOCAL_H + +#include <stdbool.h> + +struct clock_sync_context { +void *proto_data;/* time sync protocol specific data */ +bool is_server;/* server side time sync role */ +struct tracefs_instance *instance;/* ftrace buffer, used for time sync events */ + +/* Arrays with calculated time offsets at given time */ +int sync_size;/* Allocated size of sync_ts and sync_offsets */ +int sync_count;/* Number of elements in sync_ts and sync_offsets */ +long long *sync_ts; +long long *sync_offsets; + +/* Identifiers of local and remote time sync peers: cid and port */ +unsigned int local_cid; +unsigned int local_port; +unsigned int remote_cid; +unsigned int remote_port; +}; + +int tracecmd_tsync_proto_register (unsigned int proto_id,int weight, +int (*init)(struct tracecmd_time_sync *), +int (*free)(struct tracecmd_time_sync *), +int (*calc)(struct tracecmd_time_sync *, +long long *,long long *)); +int tracecmd_tsync_proto_unregister (unsigned int proto_id); + +int ptp_clock_sync_register (void ); + +#endif /* _TRACE_TSYNC_LOCAL_H */
lib/trace-cmd/trace-msg.c +340 -55
529 lines not on this page · raw message
lib/trace-cmd/trace-timesync.c new file +498 -0
498 lines not on this page · raw message
tracecmd/Makefile +2 -1
@@ -31,6 +31,7 @@ TRACE_CMD_OBJS += trace-show.o TRACE_CMD_OBJS += trace-list.o TRACE_CMD_OBJS += trace-usage.o TRACE_CMD_OBJS += trace-dump.o +TRACE_CMD_OBJS += trace-tsync.o ifeq ($(VSOCK_DEFINED), 1) TRACE_CMD_OBJS += trace-agent.o
@@ -43,7 +44,7 @@ all_objs := $(sort $(ALL_OBJS)) all_deps := $(all_objs:$(bdir)/%.o=$(bdir)/.%.d) CONFIG_INCLUDES = -CONFIG_LIBS = -lrt +CONFIG_LIBS = -lrt -lpthread CONFIG_FLAGS = all: $(TARGETS)
tracecmd/include/trace-local.h +11 -0
@@ -229,6 +229,10 @@ struct buffer_instance {unsigned int port;int *fds;bool use_fifos; + +pthread_t tsync_thread; +bool tsync_thread_running; +struct tracecmd_time_sync tsync; };void init_top_instance (void );
@@ -263,6 +267,13 @@ void tracecmd_disable_tracing(void);void tracecmd_enable_tracing (void );void tracecmd_stat_cpu (struct trace_seq *s,int cpu); +int tracecmd_host_tsync (struct buffer_instance *instance, +unsigned int tsync_port); +void tracecmd_host_tsync_complete (struct buffer_instance *instance); +unsigned int tracecmd_guest_tsync (char *tsync_protos, +unsigned int tsync_protos_size,char *clock, +unsigned int *tsync_port,pthread_t *thr_id); +int trace_make_vsock (unsigned int port);int trace_get_vsock_port (int sd,unsigned int *port);int trace_open_vsock (unsigned int cid,unsigned int port);
tracecmd/trace-agent.c +36 -3
@@ -19,6 +19,7 @@#include <sys/wait.h> #include <unistd.h> #include <linux/vm_sockets.h> +#include <pthread.h> #include "trace-local.h" #include "trace-msg.h"
@@ -125,11 +126,30 @@ cleanup:return ret; } +static char *get_clock (int argc,char **argv) +{ +int i; + +if (!argc || !argv) +return NULL ; + +for (i =0 ; i < argc -1 ; i++) { +if (!strcmp ("-C" , argv[i])) +return argv[i+1 ]; + } +return NULL ; +} +static void agent_handle (int sd,int nr_cpus,int page_size) {struct tracecmd_msg_handle *msg_handle; +unsigned int tsync_protos_size =0 ; +unsigned int tsync_proto =0 ;unsigned long long trace_id; +unsigned int tsync_port =0 ; +char *tsync_protos =NULL ;unsigned int *ports; +pthread_t sync_thr;char **argv =NULL ;int argc =0 ;bool use_fifos;
@@ -146,7 +166,8 @@ static void agent_handle(int sd, int nr_cpus, int page_size)die ("Failed to allocate message handle" ); ret =tracecmd_msg_recv_trace_req (msg_handle, &argc, &argv, - &use_fifos, &trace_id); + &use_fifos, &trace_id, + &tsync_protos, &tsync_protos_size);if (ret <0 )die ("Failed to receive trace request" );
@@ -155,16 +176,28 @@ static void agent_handle(int sd, int nr_cpus, int page_size)if (!use_fifos)make_vsocks (nr_cpus, fds, ports); - +if (tsync_protos) { + tsync_proto =tracecmd_guest_tsync (tsync_protos, + tsync_protos_size, +get_clock (argc, argv), + &tsync_port, &sync_thr); +if (!tsync_proto) +warning ("Failed to negotiate timestamps synchronization with the host" ); + } trace_id =tracecmd_generate_traceid (); ret =tracecmd_msg_send_trace_resp (msg_handle, nr_cpus, page_size, - ports, use_fifos, trace_id); + ports, use_fifos, trace_id, + tsync_proto, tsync_port);if (ret <0 )die ("Failed to send trace response" );trace_record_agent (msg_handle, nr_cpus, fds, argc, argv, use_fifos, trace_id); +if (tsync_proto) +pthread_join (sync_thr,NULL ); + +free (tsync_protos);free (argv[0 ]);free (argv);free (ports);
tracecmd/trace-record.c +36 -4
@@ -672,6 +672,11 @@ static void tell_guests_to_stop(void)tracecmd_msg_send_close_msg (instance->msg_handle); } +for_all_instances (instance) { +if (is_guest (instance)) +tracecmd_host_tsync_complete (instance); + } +/* Wait for guests to acknowledge */ for_all_instances (instance) {if (is_guest (instance)) {
@@ -3655,6 +3660,10 @@ static void connect_to_agent(struct buffer_instance *instance) {struct tracecmd_msg_handle *msg_handle;int sd, ret, nr_fifos, nr_cpus, page_size; +unsigned int tsync_protos_reply =0 ; +unsigned int tsync_port =0 ; +char *protos =NULL ; +int protos_count =0 ;unsigned int *ports;int i, *fds =NULL ;bool use_fifos = false;
@@ -3675,17 +3684,32 @@ static void connect_to_agent(struct buffer_instance *instance)if (!msg_handle)die ("Failed to allocate message handle" ); +if (instance->tsync.loop_interval >=0 ) +tracecmd_tsync_proto_getall (&protos, &protos_count); + ret =tracecmd_msg_send_trace_req (msg_handle, instance->argc, instance->argv, use_fifos, - top_instance.trace_id); + top_instance.trace_id, + protos, protos_count);if (ret <0 )die ("Failed to send trace request" ); +free (protos); + ret =tracecmd_msg_recv_trace_resp (msg_handle, &nr_cpus, &page_size, &ports, &use_fifos, - &instance->trace_id); + &instance->trace_id, + &tsync_protos_reply, &tsync_port);if (ret <0 ) -die ("Failed to receive trace response" ); +die ("Failed to receive trace response %d" , ret); + +if (protos_count && tsync_protos_reply) { +if (tsync_proto_is_supported (tsync_protos_reply)) { + instance->tsync.sync_proto = tsync_protos_reply; +tracecmd_host_tsync (instance, tsync_port); + }else +warning ("Failed to negotiate timestamps synchronization with the guest" ); + }if (use_fifos) {if (nr_cpus != nr_fifos) {
@@ -3733,7 +3757,8 @@ static void setup_guest(struct buffer_instance *instance)close (fd); } -static void setup_agent (struct buffer_instance *instance,struct common_record_context *ctx) +static void setup_agent (struct buffer_instance *instance, +struct common_record_context *ctx) {struct tracecmd_output *network_handle;
@@ -5258,6 +5283,7 @@ void init_top_instance(void) }enum { + OPT_tsyncinterval =242 , OPT_user =243 , OPT_procmap =244 , OPT_quiet =245 ,
@@ -5560,6 +5586,7 @@ static void parse_record_options(int argc, {"proc-map", no_argument, NULL, OPT_procmap}, {"user", required_argument, NULL, OPT_user}, {"module", required_argument, NULL, OPT_module}, + {"tsync-interval", required_argument, NULL, OPT_tsyncinterval}, {NULL, 0, NULL, 0} };
@@ -5902,6 +5929,10 @@ static void parse_record_options(int argc, ctx->instance->filter_mod = optarg; ctx->filtered =0 ;break ; +case OPT_tsyncinterval: + top_instance.tsync.loop_interval =atoi (optarg); + guest_sync_set = true; +break ;case OPT_quiet:case 'q' : quiet = true;
@@ -5932,6 +5963,7 @@ static void parse_record_options(int argc,add_argv (instance,"-C" , true); } } + instance->tsync.loop_interval = top_instance.tsync.loop_interval; } }
tracecmd/trace-tsync.c new file +272 -0
272 lines not on this page · raw message
tracecmd/trace-usage.c +4 -0
@@ -60,6 +60,10 @@ static struct usage_help usage_help[] = {" --no-filter include trace-cmd threads in the trace\n" " --proc-map save the traced processes address map into the trace.dat file\n" " --user execute the specified [command ...] as given user\n" +" --tsync-interval set the loop interval, in ms, for timestamps synchronization with guests:" +" If a negative number is specified, timestamps synchronization is disabled" +" If 0 is specified, no loop is performed - timestamps offset is calculated only twice," +" at the beginnig and at the end of the trace\n" }, { "start",
Signature · 1 line
2.24.1
PATCH v19 14/15 trace-cmd: [POC] PTP-like algorithm for host - guest timestamp synchronization
In reply to Tzvetomir Stoyanov (VMware)PTP protocol is designed for synchronizing clocks of machines in a local network. The same approach can be used for host - guest timestamp synchronization. This implementation uses ftrace raw markers to track trace timestamps of PTP events. The patch is a POC, two different algorithms for PTP calculations are proposed:
- Choosing the sample with the fastest response time for calculating the clocks offset.
- Calculating the clocks offset using the average of all PTP samples.
The implementation can be tuned using those parameters:
- #define FASTEST_RESPONSE - is defined, the sample with the fastest response time is used for calculating the clocks offset. Otherwise the histogram of all samples is used.
- #define PTP_SYNC_LOOP 340 - defines the number of samples, used for one calculation.
- --tsync-interval - a trace-cmd argument, choose the intervals between offset calculations, performed continuously during the trace.
- #define TSYNC_DEBUG - if defined, a debug information is collected and stored in files, in the guest machine:
s-cid*.txt - For each offset calculation: host and guest clocks and calculated offset. res-cid*.txt - For each tracing session: all calculated clock offsets.
--- include/trace-cmd/trace-cmd.h | 1 + lib/trace-cmd/Makefile | 1 + lib/trace-cmd/trace-timesync-ptp.c | 651 +++++++++++++++++++++++++++++ lib/trace-cmd/trace-timesync.c | 8 + tracecmd/trace-agent.c | 2 + tracecmd/trace-record.c | 23 +- 6 files changed, 678 insertions(+), 8 deletions(-) create mode 100644 lib/trace-cmd/trace-timesync-ptp.c
include/trace-cmd/trace-cmd.h +1 -0
@@ -414,6 +414,7 @@ int tracecmd_msg_recv_time_sync(struct tracecmd_msg_handle *msg_handle,enum {TRACECMD_TIME_SYNC_PROTO_NONE =0 , +TRACECMD_TIME_SYNC_PROTO_PTP =1 , };enum {TRACECMD_TIME_SYNC_CMD_PROBE =1 ,
lib/trace-cmd/Makefile +1 -0
@@ -17,6 +17,7 @@ OBJS += trace-filter-hash.o OBJS += trace-msg.o OBJS += trace-plugin.o OBJS += trace-timesync.o +OBJS += trace-timesync-ptp.o # Additional util objects OBJS += trace-blk-hack.o
lib/trace-cmd/trace-timesync-ptp.c new file +651 -0
651 lines not on this page · raw message
lib/trace-cmd/trace-timesync.c +8 -0
@@ -46,6 +46,14 @@ static struct tsync_proto *tsync_proto_find(unsigned int proto_id)return NULL ; } +/** +* tracecmd_tsync_init - Initialize the global, per task, time sync data. +*/ +void tracecmd_tsync_init (void ) +{ +ptp_clock_sync_register (); +} + int tracecmd_tsync_proto_register(unsigned int proto_id, int weight, int (*init)(struct tracecmd_time_sync *), int (*free)(struct tracecmd_time_sync *),
tracecmd/trace-agent.c +2 -0
@@ -247,6 +247,8 @@ static void agent_serve(unsigned int port)if (sd <0 )die ("Failed to open vsocket" ); +tracecmd_tsync_init (); +if (!get_local_cid (&cid))printf ("listening on @%u:%u\n" , cid, port);
tracecmd/trace-record.c +15 -8
@@ -6063,10 +6063,6 @@ static bool has_local_instances(void)return false; } -/* -* This function contains common code for the following commands: -* record, start, stream, profile. -*/ static void record_trace (int argc,char **argv,struct common_record_context *ctx) {
@@ -6200,12 +6196,23 @@ static void record_trace(int argc, char **argv,finalize_record_trace (ctx); } +/* +* This function contains common code for the following commands: +* record, start, stream, profile. +*/ +static void record_trace_command (int argc,char **argv, +struct common_record_context *ctx) +{ +tracecmd_tsync_init (); +record_trace (argc, argv, ctx); +} +void trace_start (int argc,char **argv) {struct common_record_context ctx;parse_record_options (argc, argv, CMD_start, &ctx); -record_trace (argc, argv, &ctx); +record_trace_command (argc, argv, &ctx);exit (0 ); }
@@ -6285,7 +6292,7 @@ void trace_stream(int argc, char **argv)struct common_record_context ctx;parse_record_options (argc, argv, CMD_stream, &ctx); -record_trace (argc, argv, &ctx); +record_trace_command (argc, argv, &ctx);exit (0 ); }
@@ -6304,7 +6311,7 @@ void trace_profile(int argc, char **argv)if (!buffer_instances) top_instance.flags |=BUFFER_FL_PROFILE ; -record_trace (argc, argv, &ctx); +record_trace_command (argc, argv, &ctx);do_trace_profile ();exit (0 ); }
@@ -6323,7 +6330,7 @@ void trace_record(int argc, char **argv)struct common_record_context ctx;parse_record_options (argc, argv, CMD_record, &ctx); -record_trace (argc, argv, &ctx); +record_trace_command (argc, argv, &ctx);exit (0 ); }
Signature · 1 line
2.24.1
PATCH v19 15/15 trace-cmd: Debug scripts for PTP-like algorithm for host - guest timestamp synchronization
In reply to Tzvetomir Stoyanov (VMware)These scripts can be used to visualise debug files, written when the PTP-like algorithm is compiled with TSYNC_DEBUG defined. The files are located in the guest machine:
s-cid*.txt - For each offset calculation: host and guest clocks and calculated offset. res-cid*.txt - For each tracing session: all calculated clock offsets.
tsync_hist.py plots a histogram, using data from a s-cid*.txt file:
"python tsync_hist.py s-cid2_1.txt"
tsync_res.py plots a line, using data from res-cid*.txt file:
"python tsync_res.py res-cid2.txt"
--- scripts/debug/tsync_hist.py | 57 +++++++++++++++++++++++++++++++++++++ scripts/debug/tsync_readme | 12 ++++++++ scripts/debug/tsync_res.py | 46 ++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 scripts/debug/tsync_hist.py create mode 100644 scripts/debug/tsync_readme create mode 100644 scripts/debug/tsync_res.py
scripts/debug/tsync_hist.py new file +57 -0
@@ -0,0 +1,57 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (C) 2019, VMware Inc, Tzvetomir Stoyanov <[email protected]> +# Copyright (C) 2019, VMware Inc, Yordan Karadzhov <[email protected]> + + +import matplotlib.pyplot as plt +import matplotlib.lines as mlines +import numpy as np +import sys + +def newline(p1, p2): + ax = plt.gca() + xmin, xmax = ax.get_xbound() + + if(p2[0] == p1[0]): + xmin = xmax = p1[0] + ymin, ymax = ax.get_ybound() + else: + ymax = p1[1]+(p2[1]-p1[1])/(p2[0]-p1[0])*(xmax-p1[0]) + ymin = p1[1]+(p2[1]-p1[1])/(p2[0]-p1[0])*(xmin-p1[0]) + + l = mlines.Line2D([xmin,xmax], [ymin,ymax], color='red') + ax.add_line(l) + return l + + +data = np.loadtxt(fname = sys.argv[1]) +selected_ts = data[-1, 1] +selected_ofs = data[-1, 0] +data = data[:-1,:] + +x = data[:, 1] - data[:, 0] + +mean = x.mean() +std = x.std() + +num_bins = 500 +min = x.min() #+ .4 * (x.max() - x.min()) +max = x.max() #- .4 * (x.max() - x.min()) +bins = np.linspace(min, max, num_bins, endpoint = False, dtype=int) + +fig, ax = plt.subplots() + +# the histogram of the data +n, bins, patches = ax.hist(x, bins, histtype=u'step'); + +ax.set_xlabel('clock offset [$\mu$s]') +ax.set_ylabel('entries') +ax.set_title("$\sigma$=%i" % std) + +x1, y1 = [selected_ofs, min], [selected_ofs, max] +newline(x1, y1) + +# Tweak spacing to prevent clipping of ylabel +fig.tight_layout() +plt.show()
scripts/debug/tsync_readme new file +12 -0
@@ -0,0 +1,12 @@ +PTP-like algorithm debug +======================== + +tsync_*.py scripts can be used to visualise debug files, written when the PTP-like algorithm +is compiled with TSYNC_DEBUG defined. The files are located in the guest machine: + s-cid*.txt - For each offset calculation: host and guest clocks and calculated offset. + res-cid*.txt - For each tracing session: all calculated clock offsets. + +tsync_hist.py plots a histogram, using data from a s-cid*.txt file: + "python tsync_hist.py s-cid2_1.txt" +tsync_res.py plots a line, using data from res-cid*.txt file: + "python tsync_res.py res-cid2.txt"
scripts/debug/tsync_res.py new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.24.1