perf intel-pt: Add perf event clocks to better support VM tracing
Subsystems (5)
Adrian Hunter
reviewerv3 cover perf intel-pt: Add perf event clocks to better support VM tracing
Hi
Here is V3.
These patches add 2 new perf event clocks based on TSC for use with VMs.
The first patch is a minor fix, the next 2 patches add each of the 2 new clocks. The remaining patches add minimal tools support and are based on top of the Intel PT Event Trace tools' patches.
The future work, to add the ability to use perf inject to inject perf events from a VM guest perf.data file into a VM host perf.data file, has yet to be implemented.
Changes in V3:
perf/x86: Add support for TSC as a perf event clock perf/x86: Add support for TSC in nanoseconds as a perf event clock
Drop magic flag for non-standard clock ids
Move new clockids into clock.h
Adjust comments to warn about new clocks
perf tools: Add new perf clock IDs
perf tools: Add API probes for new clock IDs perf tools: Add new clock IDs to "perf time to TSC" test perf tools: Add perf_read_tsc_conv_for_clockid() perf intel-pt: Add support for new clock IDs perf intel-pt: Add config variables for timing parameters perf intel-pt: Add documentation for new clock IDs
Drop magic flag for non-standard clock ids Adjust documentation to warn about new clocks
perf intel-pt: Use CLOCK_PERF_HW_CLOCK_NS by default
Dropped patch
Changes in V2:
perf/x86: Fix native_perf_sched_clock_from_tsc() with __sched_clock_offset
Add __sched_clock_offset unconditionally
perf/x86: Add support for TSC as a perf event clock
Use an attribute bit 'ns_clockid' to identify non-standard clockids
perf/x86: Add support for TSC in nanoseconds as a perf event clock
Do not affect use of __sched_clock_offset
Adjust to use 'ns_clockid'
perf tools: Add new perf clock IDs
perf tools: Add API probes for new clock IDs perf tools: Add new clock IDs to "perf time to TSC" test perf tools: Add perf_read_tsc_conv_for_clockid() perf intel-pt: Add support for new clock IDs perf intel-pt: Use CLOCK_PERF_HW_CLOCK_NS by default perf intel-pt: Add config variables for timing parameters perf intel-pt: Add documentation for new clock IDs
Adjust to use 'ns_clockid'
Adrian Hunter (10):
perf/x86: Fix native_perf_sched_clock_from_tsc() with __sched_clock_offset perf/x86: Add support for TSC as a perf event clock
perf/x86: Add support for TSC in nanoseconds as a perf event clock perf tools: Add new perf clock IDs
perf tools: Add API probes for new clock IDs
perf tools: Add new clock IDs to "perf time to TSC" test
perf tools: Add perf_read_tsc_conv_for_clockid()
perf intel-pt: Add support for new clock IDs
perf intel-pt: Add config variables for timing parameters perf intel-pt: Add documentation for new clock IDs
arch/x86/events/core.c | 39 +++++++++++-- arch/x86/include/asm/perf_event.h | 5 ++ arch/x86/kernel/tsc.c | 2 +- include/uapi/linux/time.h | 17 ++++++ kernel/events/core.c | 13 +++++ tools/perf/Documentation/perf-config.txt | 18 ++++++ tools/perf/Documentation/perf-intel-pt.txt | 45 +++++++++++++++ tools/perf/Documentation/perf-record.txt | 11 +++- tools/perf/arch/x86/util/intel-pt.c | 89 ++++++++++++++++++++++++++++-- tools/perf/builtin-record.c | 2 +- tools/perf/tests/perf-time-to-tsc.c | 42 +++++++++++--- tools/perf/util/clockid.c | 5 ++ tools/perf/util/clockid.h | 8 +++ tools/perf/util/intel-pt.c | 30 ++++++++-- tools/perf/util/intel-pt.h | 7 ++- tools/perf/util/perf_api_probe.c | 23 ++++++++ tools/perf/util/perf_api_probe.h | 2 + tools/perf/util/tsc.c | 56 +++++++++++++++++++ tools/perf/util/tsc.h | 1 + 19 files changed, 387 insertions(+), 28 deletions(-)
Regards
Adrian
Adrian Hunter
reviewerPATCH v3 1/10 perf/x86: Fix native_perf_sched_clock_from_tsc() with __sched_clock_offset
native_perf_sched_clock_from_tsc() is used to produce a time value that can be consistent with perf_clock(). Consequently, it should be adjusted by __sched_clock_offset, the same as perf_clock() would be.
--- arch/x86/kernel/tsc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/tsc.c +1 -1
@@ -242,7 +242,7 @@ u64 native_sched_clock(void)*/ u64 native_sched_clock_from_tsc (u64 tsc) { -return cycles_2_ns (tsc); +return cycles_2_ns (tsc) + __sched_clock_offset; }/* We need to define a real function for sched_clock, to override the
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 2/10 perf/x86: Add support for TSC as a perf event clock
In reply to Adrian HunterCurrently, using Intel PT to trace a VM guest is limited to kernel space because decoding requires side band events such as MMAP and CONTEXT_SWITCH. While these events can be collected for the host, there is not a way to do that yet for a guest. One approach, would be to collect them inside the guest, but that would require being able to synchronize with host timestamps.
The motivation for this patch is to provide a clock that can be used within a VM guest, and that correlates to a VM host clock. In the case of TSC, if the hypervisor leaves rdtsc alone, the TSC value will be subject only to the VMCS TSC Offset and Scaling. Adjusting for that would make it possible to inject events from a guest perf.data file, into a host perf.data file.
Thus making possible the collection of VM guest side band for Intel PT decoding.
There are other potential benefits of TSC as a perf event clock:
- ability to work directly with TSC
- ability to inject non-Intel-PT-related events from a guest
--- arch/x86/events/core.c | 14 ++++++++++++++ arch/x86/include/asm/perf_event.h | 3 +++ include/uapi/linux/time.h | 11 +++++++++++ kernel/events/core.c | 7 +++++++ 4 files changed, 35 insertions(+)
arch/x86/events/core.c +14 -0
@@ -2728,6 +2728,15 @@ void arch_perf_update_userpage(struct perf_event *event, !!(event->hw.flags &PERF_EVENT_FLAG_USER_READ_CNT ); userpg->pmc_width = x86_pmu.cntval_bits; +if (event->attr.use_clockid && event->attr.clockid ==CLOCK_PERF_HW_CLOCK ) { + userpg->cap_user_time_zero =1 ; + userpg->time_mult =1 ; + userpg->time_shift =0 ; + userpg->time_offset =0 ; + userpg->time_zero =0 ; +return ; + } +if (!using_native_sched_clock () || !sched_clock_stable ())return ;
@@ -2980,6 +2989,11 @@ unsigned long perf_misc_flags(struct pt_regs *regs)return misc; } +u64 perf_hw_clock (void ) +{ +return rdtsc_ordered (); +} +void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) { cap->version = x86_pmu.version;
arch/x86/include/asm/perf_event.h +3 -0
@@ -451,6 +451,9 @@ extern unsigned long perf_instruction_pointer(struct pt_regs *regs);extern unsigned long perf_misc_flags (struct pt_regs *regs);#define perf_misc_flags (regs) perf_misc_flags(regs) +extern u64 perf_hw_clock (void ); +#define perf_hw_clock perf_hw_clock +#include <asm/stacktrace.h> /*
include/uapi/linux/time.h +11 -0
@@ -67,6 +67,17 @@ struct timezone {#define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC)#define CLOCKS_MONO CLOCK_MONOTONIC +/* +* If supported, clockid value for use in struct perf_event_attr to select an +* architecture dependent hardware clock. Note this means the unit of time is +* ticks not nanoseconds. WARNING: This clock may not be stable or well-behaved +* in any way, including varying across different CPUs. +* +* On x86, this is provided by the rdtsc instruction, and is not +* paravirtualized. Note the warning above can also apply to TSC. +*/ +#define CLOCK_PERF_HW_CLOCK 0x10000000 +/* * The various flags for setting POSIX.1b interval timers: */
kernel/events/core.c +7 -0
@@ -12034,6 +12034,13 @@ static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) event->clock = &ktime_get_clocktai_ns;break ; +#ifdef perf_hw_clock +case CLOCK_PERF_HW_CLOCK : + event->clock = &perf_hw_clock; + nmi_safe = true; +break ; +#endif +default :return -EINVAL ; }
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 3/10 perf/x86: Add support for TSC in nanoseconds as a perf event clock
In reply to Adrian HunterCurrently, when Intel PT is used within a VM guest, it is not possible to make use of TSC because perf clock is subject to paravirtualization.
If the hypervisor leaves rdtsc alone, the TSC value will be subject only to the VMCS TSC Offset and Scaling, the same as the TSC packet from Intel PT. The new clock is based on rdtsc and not subject to paravirtualization.
Hence it would be possible to use this new clock for Intel PT decoding within a VM guest.
--- arch/x86/events/core.c | 39 ++++++++++++++++++++----------- arch/x86/include/asm/perf_event.h | 2 ++ include/uapi/linux/time.h | 6 +++++ kernel/events/core.c | 6 +++++ 4 files changed, 40 insertions(+), 13 deletions(-)
arch/x86/events/core.c +26 -13
@@ -41,6 +41,7 @@#include <asm/desc.h> #include <asm/ldt.h> #include <asm/unwind.h> +#include <asm/tsc.h> #include "perf_event.h"
@@ -2728,16 +2729,26 @@ void arch_perf_update_userpage(struct perf_event *event, !!(event->hw.flags &PERF_EVENT_FLAG_USER_READ_CNT ); userpg->pmc_width = x86_pmu.cntval_bits; -if (event->attr.use_clockid && event->attr.clockid ==CLOCK_PERF_HW_CLOCK ) { - userpg->cap_user_time_zero =1 ; - userpg->time_mult =1 ; - userpg->time_shift =0 ; - userpg->time_offset =0 ; - userpg->time_zero =0 ; -return ; +if (event->attr.use_clockid) { +if (event->attr.clockid ==CLOCK_PERF_HW_CLOCK ) { + userpg->cap_user_time_zero =1 ; + userpg->time_mult =1 ; + userpg->time_shift =0 ; + userpg->time_offset =0 ; + userpg->time_zero =0 ; +return ; + } +if (event->attr.clockid ==CLOCK_PERF_HW_CLOCK_NS ) + userpg->cap_user_time_zero =1 ; + } + +if (using_native_sched_clock () &&sched_clock_stable ()) { + userpg->cap_user_time =1 ; +if (!event->attr.use_clockid) + userpg->cap_user_time_zero =1 ; } -if (!using_native_sched_clock () || !sched_clock_stable ()) +if (!userpg->cap_user_time && !userpg->cap_user_time_zero)return ;cyc2ns_read_begin (&data);
@@ -2748,19 +2759,16 @@ void arch_perf_update_userpage(struct perf_event *event,* Internal timekeeping for enabled/running/stopped times * is always in the local_clock domain. */ - userpg->cap_user_time =1 ; userpg->time_mult = data.cyc2ns_mul; userpg->time_shift = data.cyc2ns_shift; userpg->time_offset = offset - now;/* * cap_user_time_zero doesn't make sense when we're using a different -* time base for the records. +* time base for the records, except for CLOCK_PERF_HW_CLOCK_NS. */ -if (!event->attr.use_clockid) { - userpg->cap_user_time_zero =1 ; +if (userpg->cap_user_time_zero) userpg->time_zero = offset; - }cyc2ns_read_end (); }
@@ -2994,6 +3002,11 @@ u64 perf_hw_clock(void)return rdtsc_ordered (); } +u64 perf_hw_clock_ns (void ) +{ +return native_sched_clock_from_tsc (perf_hw_clock ()); +} +void perf_get_x86_pmu_capability (struct x86_pmu_capability *cap) { cap->version = x86_pmu.version;
arch/x86/include/asm/perf_event.h +2 -0
@@ -453,6 +453,8 @@ extern unsigned long perf_misc_flags(struct pt_regs *regs);extern u64 perf_hw_clock (void );#define perf_hw_clock perf_hw_clock +extern u64 perf_hw_clock_ns (void ); +#define perf_hw_clock_ns perf_hw_clock_ns#include <asm/stacktrace.h>
include/uapi/linux/time.h +6 -0
@@ -77,6 +77,12 @@ struct timezone {* paravirtualized. Note the warning above can also apply to TSC. */ #define CLOCK_PERF_HW_CLOCK 0x10000000 +/* +* Same as CLOCK_PERF_HW_CLOCK but in nanoseconds. Note support of +* CLOCK_PERF_HW_CLOCK_NS does not necesssarily imply support of +* CLOCK_PERF_HW_CLOCK or vice versa. +*/ +#define CLOCK_PERF_HW_CLOCK_NS 0x10000001/* * The various flags for setting POSIX.1b interval timers:
kernel/events/core.c +6 -0
@@ -12040,6 +12040,12 @@ static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) nmi_safe = true;break ;#endif +#ifdef perf_hw_clock_ns +case CLOCK_PERF_HW_CLOCK_NS : + event->clock = &perf_hw_clock_ns; + nmi_safe = true; +break ; +#endif default :return -EINVAL ;
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 4/10 perf tools: Add new perf clock IDs
In reply to Adrian HunterAdd support for new clock IDs CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS.
--- tools/perf/Documentation/perf-record.txt | 11 ++++++++++- tools/perf/builtin-record.c | 2 +- tools/perf/util/clockid.c | 5 +++++ tools/perf/util/clockid.h | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-record.txt +10 -1
@@ -444,7 +444,16 @@ Record running and enabled time for read events (:S) Sets the clock id to use for the various time fields in the perf_event_type records. See clock_gettime(). In particular CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW are supported, some events might also allow -CLOCK_BOOTTIME, CLOCK_REALTIME and CLOCK_TAI. +CLOCK_BOOTTIME, CLOCK_REALTIME and CLOCK_TAI. In addition, the kernel might +support CLOCK_PERF_HW_CLOCK to select an architecture dependent hardware +clock, for which the unit of time is ticks not nanoseconds. On x86, +CLOCK_PERF_HW_CLOCK is provided by the rdtsc instruction, and is not +paravirtualized. There is also CLOCK_PERF_HW_CLOCK_NS which is the same as +CLOCK_PERF_HW_CLOCK, but converted to nanoseconds. Note support of +CLOCK_PERF_HW_CLOCK_NS does not necessarily imply support of +CLOCK_PERF_HW_CLOCK or vice versa. Be warned, CLOCK_PERF_HW_CLOCK and +CLOCK_PERF_HW_CLOCK_NS may not be stable or well-behaved in any way, +including varying across different CPUs. That warning can also apply to TSC. -S:: --snapshot::
tools/perf/builtin-record.c +1 -1
@@ -1973,7 +1973,7 @@ static int record__init_clock(struct record *rec)struct timeval ref_tod;u64 ref; -if (!rec->opts.use_clockid) +if (!rec->opts.use_clockid || rec->opts.clockid >=CLOCK_PERF_HW_CLOCK )return 0 ;if (rec->opts.use_clockid && rec->opts.clockid_res_ns)
tools/perf/util/clockid.c +5 -0
@@ -49,6 +49,9 @@ static const struct clockid_map clockids[] = {CLOCKID_MAP ("real" ,CLOCK_REALTIME ),CLOCKID_MAP ("boot" ,CLOCK_BOOTTIME ), +CLOCKID_MAP ("perf_hw_clock" ,CLOCK_PERF_HW_CLOCK ), +CLOCKID_MAP ("perf_hw_clock_ns" ,CLOCK_PERF_HW_CLOCK_NS ), + CLOCKID_END, };
@@ -57,6 +60,8 @@ static int get_clockid_res(clockid_t clk_id, u64 *res_ns)struct timespec res; *res_ns =0 ; +if (clk_id >=CLOCK_PERF_HW_CLOCK ) +return 0 ;if (!clock_getres (clk_id, &res)) *res_ns = res.tv_nsec + res.tv_sec *NSEC_PER_SEC ;else
tools/perf/util/clockid.h +8 -0
@@ -8,4 +8,12 @@ int parse_clockid(const struct option *opt, const char *str, int unset);const char *clockid_name (clockid_t clk_id); +#ifndef CLOCK_PERF_HW_CLOCK +#define CLOCK_PERF_HW_CLOCK 0x10000000 +#endif + +#ifndef CLOCK_PERF_HW_CLOCK_NS +#define CLOCK_PERF_HW_CLOCK_NS 0x10000001 +#endif +#endif
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 5/10 perf tools: Add API probes for new clock IDs
In reply to Adrian HunterAdd ability to check whether the kernel supports new clock IDs CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS.
They will be used in a subsequent patch.
--- tools/perf/util/perf_api_probe.c | 23 +++++++++++++++++++++++ tools/perf/util/perf_api_probe.h | 2 ++ 2 files changed, 25 insertions(+)
tools/perf/util/perf_api_probe.c +23 -0
@@ -5,6 +5,7 @@#include "util/evlist.h" #include "util/evsel.h" #include "util/parse-events.h" +#include "util/clockid.h" #include "util/perf_api_probe.h" #include <perf/cpumap.h> #include <errno.h>
@@ -109,6 +110,18 @@ static void perf_probe_cgroup(struct evsel *evsel) evsel->core.attr.cgroup =1 ; } +static void perf_probe_hw_clock (struct evsel *evsel) +{ + evsel->core.attr.use_clockid =1 ; + evsel->core.attr.clockid =CLOCK_PERF_HW_CLOCK ; +} + +static void perf_probe_hw_clock_ns (struct evsel *evsel) +{ + evsel->core.attr.use_clockid =1 ; + evsel->core.attr.clockid =CLOCK_PERF_HW_CLOCK_NS ; +} +bool perf_can_sample_identifier (void ) {return perf_probe_api (perf_probe_sample_identifier);
@@ -195,3 +208,13 @@ bool perf_can_record_cgroup(void) {return perf_probe_api (perf_probe_cgroup); } + +bool perf_can_perf_clock_hw_clock (void ) +{ +return perf_probe_api (perf_probe_hw_clock); +} + +bool perf_can_perf_clock_hw_clock_ns (void ) +{ +return perf_probe_api (perf_probe_hw_clock_ns); +}
tools/perf/util/perf_api_probe.h +2 -0
@@ -13,5 +13,7 @@ bool perf_can_record_text_poke_events(void);bool perf_can_sample_identifier (void );bool perf_can_record_build_id (void );bool perf_can_record_cgroup (void ); +bool perf_can_perf_clock_hw_clock (void ); +bool perf_can_perf_clock_hw_clock_ns (void );#endif // __PERF_API_PROBE_H
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 6/10 perf tools: Add new clock IDs to "perf time to TSC" test
In reply to Adrian HunterThe same "Convert perf time to TSC" test can be used with new clock IDs CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS.
--- tools/perf/tests/perf-time-to-tsc.c | 42 ++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-)
tools/perf/tests/perf-time-to-tsc.c +33 -9
@@ -22,6 +22,8 @@#include "tests.h" #include "pmu.h" #include "pmu-hybrid.h" +#include "clockid.h" +#include "perf_api_probe.h" /* * Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just
@@ -47,15 +49,7 @@ } \ } -/** -* test__perf_time_to_tsc - test converting perf time to TSC. -* -* This function implements a test that checks that the conversion of perf time -* to and from TSC is consistent with the order of events. If the test passes -* %0 is returned, otherwise %-1 is returned. If TSC conversion is not -* supported then then the test passes but " (not supported)" is printed. -*/ -static int test__perf_time_to_tsc (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) +static int perf_time_to_tsc_test (bool use_clockid,s32 clockid) {struct record_opts opts = { .mmap_pages =UINT_MAX ,
@@ -104,6 +98,8 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su evsel->core.attr.comm =1 ; evsel->core.attr.disabled =1 ; evsel->core.attr.enable_on_exec =0 ; + evsel->core.attr.use_clockid = use_clockid; + evsel->core.attr.clockid = clockid;/* * For hybrid "cycles:u", it creates two events.
@@ -200,4 +196,32 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int sureturn err; } +/** +* test__perf_time_to_tsc - test converting perf time to TSC. +* +* This function implements a test that checks that the conversion of perf time +* to and from TSC is consistent with the order of events. If the test passes +* %0 is returned, otherwise %-1 is returned. If TSC conversion is not +* supported then the test passes but " (not supported)" is printed. +*/ +static int test__perf_time_to_tsc (struct test_suite *test__maybe_unused , +int subtest__maybe_unused ) +{ +int err; + + err =perf_time_to_tsc_test (false,0 ); + +if (!err &&perf_can_perf_clock_hw_clock ()) { +pr_debug ("Testing CLOCK_PERF_HW_CLOCK\n" ); + err =perf_time_to_tsc_test (true,CLOCK_PERF_HW_CLOCK ); + } + +if (!err &&perf_can_perf_clock_hw_clock_ns ()) { +pr_debug ("Testing CLOCK_PERF_HW_CLOCK_NS\n" ); + err =perf_time_to_tsc_test (true,CLOCK_PERF_HW_CLOCK_NS ); + } + +return err; +} +DEFINE_SUITE ("Convert perf time to TSC" , perf_time_to_tsc);
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 7/10 perf tools: Add perf_read_tsc_conv_for_clockid()
In reply to Adrian HunterAdd a function to read TSC conversion information for a particular clock ID. It will be used in a subsequent patch.
--- tools/perf/util/tsc.c | 56 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/tsc.h | 1 + 2 files changed, 57 insertions(+)
tools/perf/util/tsc.c +56 -0
@@ -3,6 +3,8 @@#include <inttypes.h> #include <string.h> +#include <sys/mman.h> +#include <linux/compiler.h> #include <linux/perf_event.h> #include <linux/stddef.h>
@@ -14,6 +16,9 @@#include "synthetic-events.h" #include "debug.h" #include "tsc.h" +#include "cpumap.h" +#include "perf-sys.h" +#include <internal/lib.h> /* page_size */ u64 perf_time_to_tsc (u64 ns,struct perf_tsc_conversion *tc) {
@@ -71,6 +76,57 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,return 0 ; } +static int perf_read_tsc_conv_attr_cpu (struct perf_event_attr *attr, +struct perf_cpu cpu, +struct perf_tsc_conversion *tc) +{ +size_t len =2 * page_size; +int fd, err = -EINVAL ; +void *addr; + + fd =sys_perf_event_open (attr,0 , cpu.cpu,-1 ,0 ); +if (fd ==-1 ) +return -EINVAL ; + + addr =mmap (NULL , len,PROT_READ ,MAP_SHARED , fd,0 ); +if (addr ==MAP_FAILED ) + goto out_close; + + err =perf_read_tsc_conversion (addr, tc); + +munmap (addr, len); +out_close: +close (fd); +return err; +} + +static struct perf_cpu find_a_cpu (void ) +{ +struct perf_cpu_map *cpus; +struct perf_cpu cpu = { .cpu =0 }; + + cpus =perf_cpu_map__new (NULL ); +if (!cpus) +return cpu; + cpu = cpus->map[0 ]; +perf_cpu_map__put (cpus); +return cpu; +} + +int perf_read_tsc_conv_for_clockid (s32 clockid,struct perf_tsc_conversion *tc) +{ +struct perf_event_attr attr = { + .size =sizeof (attr), + .type =PERF_TYPE_SOFTWARE , + .config =PERF_COUNT_SW_DUMMY , + .exclude_kernel =1 , + .use_clockid =1 , + .clockid = clockid, + }; + +return perf_read_tsc_conv_attr_cpu (&attr,find_a_cpu (), tc); +} + int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, struct perf_tool *tool, perf_event__handler_t process,
tools/perf/util/tsc.h +1 -0
@@ -21,6 +21,7 @@ struct perf_event_mmap_page;int perf_read_tsc_conversion (const struct perf_event_mmap_page *pc,struct perf_tsc_conversion *tc); +int perf_read_tsc_conv_for_clockid (s32 clockid,struct perf_tsc_conversion *tc);u64 perf_time_to_tsc (u64 ns,struct perf_tsc_conversion *tc);u64 tsc_to_perf_time (u64 cyc,struct perf_tsc_conversion *tc);
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 8/10 perf intel-pt: Add support for new clock IDs
In reply to Adrian HunterAdd support for new clock IDs CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS. Mainly this means also keeping TSC conversion information for CLOCK_PERF_HW_CLOCK_NS when CLOCK_PERF_HW_CLOCK is being used, so that conversions from nanoseconds can still be done when the perf event clock is TSC.
--- tools/perf/arch/x86/util/intel-pt.c | 37 ++++++++++++++++++++++++++--- tools/perf/util/intel-pt.c | 24 +++++++++++++++---- tools/perf/util/intel-pt.h | 2 +- 3 files changed, 55 insertions(+), 8 deletions(-)
tools/perf/arch/x86/util/intel-pt.c +34 -3
@@ -31,6 +31,7 @@#include "../../../util/tsc.h" #include <internal/lib.h> // page_size #include "../../../util/intel-pt.h" +#include "../../../util/clockid.h" #define KiB (x) ((x) * 1024)#define MiB (x) ((x) * 1024 * 1024)
@@ -290,6 +291,20 @@ static const char *intel_pt_find_filter(struct evlist *evlist,return NULL ; } +static bool intel_pt_clockid (struct evlist *evlist,struct perf_pmu *intel_pt_pmu,s32 clockid) +{ +struct evsel *evsel; + +evlist__for_each_entry (evlist, evsel) { +if (evsel->core.attr.type == intel_pt_pmu->type && + evsel->core.attr.use_clockid && + evsel->core.attr.clockid == clockid) +return true; + } + +return false; +} +static size_t intel_pt_filter_bytes (const char *filter) {size_t len = filter ?strlen (filter) :0 ;
@@ -304,9 +319,11 @@ intel_pt_info_priv_size(struct auxtrace_record *itr, struct evlist *evlist) container_of(itr, struct intel_pt_recording, itr);const char *filter =intel_pt_find_filter (evlist, ptr->intel_pt_pmu); - ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX *sizeof (u64)) + + ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_FIXED *sizeof (u64)) +intel_pt_filter_bytes (filter); ptr->priv_size +=sizeof (u64);/* Cap Event Trace */ + ptr->priv_size +=sizeof (u64);/* ns Time Shift */ + ptr->priv_size +=sizeof (u64);/* ns Time Multiplier */ return ptr->priv_size; }
@@ -414,6 +431,18 @@ static int intel_pt_info_fill(struct auxtrace_record *itr, *info++ = event_trace; +if (intel_pt_clockid (session->evlist, ptr->intel_pt_pmu,CLOCK_PERF_HW_CLOCK )) { +struct perf_tsc_conversion ns_tc; + +if (perf_read_tsc_conv_for_clockid (CLOCK_PERF_HW_CLOCK_NS , &ns_tc)) +return -EINVAL ; + *info++ = ns_tc.time_shift; + *info++ = ns_tc.time_mult; + }else { + *info++ = tc.time_shift; + *info++ = tc.time_mult; + } +return 0 ; }
@@ -664,8 +693,10 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,return -EINVAL ; } -if (opts->use_clockid) { -pr_err ("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n" ); +if (opts->use_clockid && opts->clockid !=CLOCK_PERF_HW_CLOCK_NS && + opts->clockid !=CLOCK_PERF_HW_CLOCK ) { +pr_err ("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME +" except CLOCK_PERF_HW_CLOCK_NS and CLOCK_PERF_HW_CLOCK\n" );return -EINVAL ; }
tools/perf/util/intel-pt.c +20 -4
@@ -89,6 +89,8 @@ struct intel_pt {struct perf_tsc_conversion tc;bool cap_user_time_zero; +u16 ns_time_shift; +u32 ns_time_mult;struct itrace_synth_opts synth_opts;
@@ -1100,10 +1102,10 @@ static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns) {u64 quot, rem; - quot = ns / pt->tc.time_mult; - rem = ns % pt->tc.time_mult; -return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) / - pt->tc.time_mult; + quot = ns / pt->ns_time_mult; + rem = ns % pt->ns_time_mult; +return (quot << pt->ns_time_shift) + (rem << pt->ns_time_shift) / + pt->ns_time_mult; }static struct ip_callchain *intel_pt_alloc_chain (struct intel_pt *pt)
@@ -3987,6 +3989,20 @@ int intel_pt_process_auxtrace_info(union perf_event *event, pt->cap_event_trace); } +if ((void *)info < info_end) { + pt->ns_time_shift = *info++; + pt->ns_time_mult = *info++; +if (dump_trace) { +fprintf (stdout," ns Time Shift %d\n" , pt->ns_time_shift); +fprintf (stdout," ns Time Multiplier %d\n" , pt->ns_time_mult); + } + }else { + pt->ns_time_shift = pt->tc.time_shift; + pt->ns_time_mult = pt->tc.time_mult; + } +if (!pt->ns_time_mult) + pt->ns_time_mult =1 ; + pt->timeless_decoding =intel_pt_timeless_decoding (pt);if (pt->timeless_decoding && !pt->tc.time_mult) pt->tc.time_mult =1 ;
tools/perf/util/intel-pt.h +1 -1
@@ -27,7 +27,7 @@ enum {INTEL_PT_CYC_BIT ,INTEL_PT_MAX_NONTURBO_RATIO ,INTEL_PT_FILTER_STR_LEN , - INTEL_PT_AUXTRACE_PRIV_MAX, + INTEL_PT_AUXTRACE_PRIV_FIXED, };struct auxtrace_record ;
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 9/10 perf intel-pt: Add config variables for timing parameters
In reply to Adrian HunterParameters needed to correctly interpret timing packets might be missing in a virtual machine because the CPUID leaf or MSR is not supported by the hypervisor / KVM.
Add perf config variables to overcome that for max_nonturbo_ratio (missing from MSR_PLATFORM_INFO) and tsc_art_ratio (missing from CPUID leaf
0x15), which were seen to be missing from QEMU / KVM.
--- tools/perf/Documentation/perf-config.txt | 18 ++++++++ tools/perf/arch/x86/util/intel-pt.c | 52 +++++++++++++++++++++++- tools/perf/util/intel-pt.c | 6 +++ tools/perf/util/intel-pt.h | 5 +++ 4 files changed, 79 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-config.txt +18 -0
@@ -709,7 +709,11 @@ stat.*:: intel-pt.*:: + Variables that affect Intel PT. + intel-pt.cache-divisor:: + If set, the decoder instruction cache size is based on DSO size + divided by this number. Default is 64. intel-pt.mispred-all:: If set, Intel PT decoder will set the mispred flag on all
@@ -721,6 +725,20 @@ intel-pt.*:: the maximum is exceeded there will be a "Never-ending loop" error. The default is 100000. + intel-pt.max_nonturbo_ratio:: + The kernel provides /sys/bus/event_source/devices/intel_pt/max_nonturbo_ratio + which can be zero in a virtual machine. The decoder needs this + information to correctly interpret timing packets, so the value + can be provided by this variable in that case. Note in the absence + of VMCS TSC Scaling, this is probably the same as the host value. + + intel-pt.tsc_art_ratio:: + The kernel provides /sys/bus/event_source/devices/intel_pt/tsc_art_ratio + which can be 0:0 in a virtual machine. The decoder needs this + information to correctly interpret timing packets, so the value + can be provided by this variable in that case. Note in the absence + of VMCS TSC Scaling, this is probably the same as the host value. + auxtrace.*:: auxtrace.dumpdir::
tools/perf/arch/x86/util/intel-pt.c +50 -2
86 lines not on this page · raw message
tools/perf/util/intel-pt.c +6 -0
@@ -3934,6 +3934,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event, INTEL_PT_CYC_BIT); } +if (!pt->tsc_ctc_ratio_n || !pt->tsc_ctc_ratio_d) +intel_pt_tsc_ctc_ratio_from_config (&pt->tsc_ctc_ratio_n, &pt->tsc_ctc_ratio_d); +if (intel_pt_has (auxtrace_info,INTEL_PT_MAX_NONTURBO_RATIO )) { pt->max_non_turbo_ratio = auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO ];
@@ -3942,6 +3945,9 @@ int intel_pt_process_auxtrace_info(union perf_event *event, INTEL_PT_MAX_NONTURBO_RATIO); } +if (!pt->max_non_turbo_ratio) +intel_pt_max_nonturbo_ratio_from_config (&pt->max_non_turbo_ratio); + info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN ] +1 ; info_end = (void *)auxtrace_info + auxtrace_info->header.size;
tools/perf/util/intel-pt.h +5 -0
@@ -7,6 +7,8 @@#ifndef INCLUDE__PERF_INTEL_PT_H__ #define INCLUDE__PERF_INTEL_PT_H__ +#include <linux/types.h> +#define INTEL_PT_PMU_NAME "intel_pt" enum {
@@ -44,4 +46,7 @@ int intel_pt_process_auxtrace_info(union perf_event *event,struct perf_event_attr *intel_pt_pmu_default_config (struct perf_pmu *pmu); +void intel_pt_tsc_ctc_ratio_from_config (u32 *n,u32 *d); +void intel_pt_max_nonturbo_ratio_from_config (unsigned int *max_non_turbo_ratio); +#endif
Signature · 1 line
2.25.1
Adrian Hunter
reviewerPATCH v3 10/10 perf intel-pt: Add documentation for new clock IDs
In reply to Adrian HunterAdd brief documentation for new clock IDs CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS, as well as new config variables intel-pt.max_nonturbo_ratio and intel-pt.tsc_art_ratio.
--- tools/perf/Documentation/perf-intel-pt.txt | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+)
tools/perf/Documentation/perf-intel-pt.txt +45 -0
@@ -509,6 +509,29 @@ notnt Disable TNT packets. Without TNT packets, it is not possible to walk "0" otherwise. +perf event clock +~~~~~~~~~~~~~~~~ + +Newer kernel and tools support 2 special clocks: CLOCK_PERF_HW_CLOCK which is +TSC and CLOCK_PERF_HW_CLOCK_NS which is TSC converted to nanoseconds. +CLOCK_PERF_HW_CLOCK_NS is the same as the default perf event clock, but it is +not subject to paravirtualization, so it still works with Intel PT in a VM +guest. Note however, TSC may not be being fully virtualized, so the results +may be unexpected. + + --clockid CLOCK_PERF_HW_CLOCK_NS + +To use TSC instead of nanoseconds, use the option: + + --clockid CLOCK_PERF_HW_CLOCK + +Beware forgetting that the time stamp of events will show TSC ticks +(divided by 1,000,000,000) not seconds. + +Other clocks are not supported for use with Intel PT because they cannot be +converted to/from TSC. + + AUX area sampling option ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1398,6 +1421,28 @@ There were none. :17006 17006 [001] 11500.262869216: ffffffff8220116e error_entry+0xe ([guest.kernel.kallsyms]) pushq %rax +Tracing within a Virtual Machine +-------------------------------- + +When supported, using Intel PT within a virtual machine does not support TSC +because the perf event clock is subject to paravirtualization. That is +overcome by the new CLOCK_PERF_HW_CLOCK_NS clock - refer 'perf event clock' +above. In addition, in a VM, the following might be zero: + + /sys/bus/event_source/devices/intel_pt/max_nonturbo_ratio + /sys/bus/event_source/devices/intel_pt/tsc_art_ratio + +The decoder needs this information to correctly interpret timing packets, +so the values can be provided by config variables in that case. Note in +the absence of VMCS TSC Scaling, this is probably the same as the host values. +The config variables are: + + intel-pt.max_nonturbo_ratio + intel-pt.tsc_art_ratio + +For more information about perf config variables, refer linkperf:perf-config[1] + + Event Trace -----------
Signature · 1 line
2.25.1