tracefs: Add tracefs_time_conversion() API
Subsystems (2)
PATCH 1/1 tracefs: Add tracefs_time_conversion() API
From: "Steven Rostedt (Google)" <[email protected]>
Add a helper function that does a perf system call to extract how the kernel calculates nanoseconds from the raw time stamp counter. It returns the shift, multiplier, and offset for a given CPU.
Signed-off-by Steven Rostedt (Google) <[email protected]>
--- Documentation/libtracefs-guest.txt | 67 ++++++++++++++++++++- Makefile | 8 ++- include/tracefs.h | 3 + src/Makefile | 3 + src/tracefs-perf.c | 93 ++++++++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/tracefs-perf.c
Documentation/libtracefs-guest.txt +66 -1
@@ -3,7 +3,7 @@ libtracefs(3) NAME ---- -tracefs_find_cid_pid, tracefs_instance_find_cid_pid - +tracefs_find_cid_pid, tracefs_instance_find_cid_pid, tracefs_time_conversion - helper functions to handle tracing guests SYNOPSIS
@@ -14,6 +14,7 @@ SYNOPSIS char pass:[*]*tracefs_find_cid_pid*(int _cid_); char pass:[*]*tracefs_instance_find_cid_pid*(struct tracefs_instance pass:[*]_instance_, int _cid_); +int *tracefs_time_conversion*(int _cpu_, int pass:[*]_shift_, int pass:[*]_multi_, long long pass:[*]offset); -- DESCRIPTION
@@ -27,6 +28,11 @@ The *tracefs_instance_find_cid_pid*() is the same as *tracefs_find_cid_pid*() bu the instance to use to perform the tracing in. If NULL it will use the top level buffer to perform the tracing. +The *tracefs_time_conversion*() will return the values used by the kernel to convert +the raw time stamp counter into nanoseconds for the given _cpu_. Pointers for _shift_, _multi_ +and _offset_ can be NULL to be ignored, otherwise they are set with the shift, multiplier +and offset repectively. + RETURN VALUE ------------ Both *tracefs_find_cid_pid*() and *tracefs_instance_find_cid_pid*() will return the
@@ -76,8 +82,67 @@ static int find_cids(void) return 0; } +struct time_info { + int shift; + int multi; +}; + +static void show_time_conversion(void) +{ + struct time_info *tinfo; + int cpus; + int cpu; + int ret; + + cpus = sysconf(_SC_NPROCESSORS_CONF); + tinfo = calloc(cpus, sizeof(*tinfo)); + if (!tinfo) + exit(-1); + + for (cpu = 0; cpu < cpus; cpu++) { + ret = tracefs_time_conversion(cpu, + &tinfo[cpu].shift, + &tinfo[cpu].multi, + NULL); + if (ret) + break; + } + if (cpu != cpus) { + if (!cpu) { + perror("tracefs_time_conversion"); + exit(-1); + } + printf("Only read %d of %d CPUs", cpu, cpus); + cpus = cpu + 1; + } + + /* Check if all the shift and mult values are the same */ + for (cpu = 1; cpu < cpus; cpu++) { + if (tinfo[cpu - 1].shift != tinfo[cpu].shift) + break; + if (tinfo[cpu - 1].multi != tinfo[cpu].multi) + break; + } + + if (cpu == cpus) { + printf("All cpus have:\n"); + printf(" shift: %d\n", tinfo[0].shift); + printf(" multi: %d\n", tinfo[0].multi); + printf("\n"); + return; + } + + for (cpu = 0; cpu < cpus; cpu++) { + printf("CPU: %d\n", cpu); + printf(" shift: %d\n", tinfo[cpu].shift); + printf(" multi: %d\n", tinfo[cpu].multi); + printf("\n"); + } +} + int main(int argc, char *argv[]) { + show_time_conversion(); find_cids(); exit(0); }
Makefile +7 -1
@@ -79,13 +79,19 @@ else VSOCK_DEFINED := 0 endif +ifndef NO_PERF +PERF_DEFINED := $(shell if (echo "$(pound)include <linux/perf_event.h>" | $(CC) -E - >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi) +else +PREF_DEFINED := 0 +endif + etcdir ?= /etc etcdir_SQ = '$(subst ','\'',$(etcdir))' export man_dir man_dir_SQ html_install html_install_SQ INSTALL export img_install img_install_SQ export DESTDIR DESTDIR_SQ -export VSOCK_DEFINED +export VSOCK_DEFINED PERF_DEFINED pound := \#
include/tracefs.h +3 -0
@@ -644,4 +644,7 @@ int tracefs_cpu_pipe(struct tracefs_cpu *tcpu, int wfd, bool nonblock);int tracefs_instance_find_cid_pid (struct tracefs_instance *instance,int cid);int tracefs_find_cid_pid (int cid); +/* More guest helpers */ +int tracefs_time_conversion (int cpu,int *shift,int *mult,long long *offset); +#endif /* _TRACE_FS_H */
src/Makefile +3 -0
@@ -18,6 +18,9 @@ OBJS += tracefs-record.o ifeq ($(VSOCK_DEFINED), 1) OBJS += tracefs-vsock.o endif +ifeq ($(PERF_DEFINED), 1) +OBJS += tracefs-perf.o +endif # Order matters for the the three below OBJS += sqlhist-lex.o
src/tracefs-perf.c new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.39.2