Skip to contentSkip to conversation

Intel core misc PMUs support

PATCH In review [email protected] 7 participants 33 messages Jul 17, 2015 – Jul 20, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Intel core misc PMUs support

From: Kan Liang <[email protected]>

This patchkit intends to support Intel core misc PMUs. There are miscellaneous free running (read-only) counters in core. Some new PMUs called core misc PMUs are composed to include these counters. The counters include TSC, IA32_APERF, IA32_MPERF, IA32_PPERF, SMI_COUNT, CORE_C*_RESIDENCY and PKG_C*_RESIDENCY. There could be more in future platform.

Although these counters may be used simultaneously by other tools, it still make sense to implement them in perf. Because we can conveniently collect them together with other events. Furthermore, the handling of the free running counters is very different, so it makes sense to put them into separate PMUs.

Here are some useful examples.

1. the ASTATE/MSTATE/TSC events can be used to calculate the frequency during each sampling period.

 $ perf record -e
{ref-cycles,core_misc/tsc/,core_misc/power-mperf/,core_misc/power-aperf/}:S
 --running-time -a ~/tchain_edit
 $ perf report --stdio --group --show-freq

# Samples: 71K of event 'anon group { ref-cycles, core_misc/tsc/, core_misc/power-mperf/, core_misc/power-aperf/ }'
# Event count (approx.): 215265868412
#
# Overhead TSC MHz AVG MHz BZY MHz
Command Shared Object Symbol

 # ................................  .........  .........  .........
 ............  ................  ..................................
 #
     98.85%   5.41%  98.89%  98.95%       2293       1474       2302
 tchain_edit   tchain_edit       [.] f3
      0.39%   1.64%   0.39%   0.37%       2295          1       3053
 kworker/25:1  [kernel.vmlinux]  [k] delay_tsc
      0.08%  24.20%   0.07%   0.06%       2295         82       2746
 swapper       [kernel.vmlinux]  [k] acpi_idle_do_entry
      0.05%   0.00%   0.05%   0.05%       2295       2289       2295
 tchain_edit   tchain_edit       [.] f2

2. Caculate the CPU%
CPU_Utilization = CPU_CLK_UNHALTED.REF_TSC / TSC

 $ perf stat -x, -e "ref-cycles,core_misc/tsc/" -C0 taskset -c 0 sleep 1
  3481579,,ref-cycles
  2301685567,,core_misc/tsc/
 The CPU% for sleep is 0.15%.
 $ perf stat -x, -e "ref-cycles,core_misc/tsc/" -C0 taskset -c 0 busyloop
  11924042536,,ref-cycles
  11929411840,,core_misc/tsc/
 The CPU% for busyloop is 99.95%

3. Caculate fraction of time when the core is running in C6 state CORE_C6_time% = CORE_C6_RESIDENCY / TSC

 $ perf stat -x, -e"power_core/c6-residency/,core_misc/tsc/" -C0
   -- taskset -c 0 sleep 1
  2287199396,,power_core/c6-residency/
  2297755875,,core_misc/tsc/
 For sleep, 99.5% of time run in C6 state.
 $ perf stat -x, -e"power_core/c6-residency/,core_misc/tsc/" -C0
   -- taskset -c 0 busyloop
  1330044,,power_core/c6-residency/
  9932928928,,core_misc/tsc/
 For busyloop, 0.01% of time run in C6 state.

Kan Liang (9):
perf/x86: Add Intel core misc PMUs support perf/x86: core_misc PMU disable and enable support perf/x86: Add is_hardware_event
perf/x86: special case per-cpu core misc PMU events perf,tools: open event with it's own cpus and threads perf,tools: Dump per-sample freq in report -D perf,tools: save APERF/MPERF/TSC in struct perf_sample perf,tools: caculate and save tsc/avg/bzy freq in he_stat perf,tools: Show freq in perf report --stdio

 arch/x86/include/asm/perf_event.h                |   2 +
 arch/x86/kernel/cpu/Makefile                     |   1 +
 arch/x86/kernel/cpu/perf_event.h                 |  10 +
 arch/x86/kernel/cpu/perf_event_intel.c           |   4 +
 arch/x86/kernel/cpu/perf_event_intel_core_misc.c | 933 +++++++++++++++++++++++
 arch/x86/kernel/cpu/perf_event_intel_core_misc.h |  96 +++
 include/linux/perf_event.h                       |  17 +-
 include/linux/sched.h                            |   1 +
 include/uapi/linux/perf_event.h                  |   1 +
 kernel/events/core.c                             |  15 +-
 tools/perf/Documentation/perf-report.txt         |  10 +
 tools/perf/builtin-annotate.c                    |   2 +-
 tools/perf/builtin-diff.c                        |   2 +-
 tools/perf/builtin-record.c                      |   2 +-
 tools/perf/builtin-report.c                      |  17 +
 tools/perf/perf.h                                |   1 +
 tools/perf/tests/hists_link.c                    |   4 +-
 tools/perf/ui/hist.c                             |  69 +-
 tools/perf/util/event.h                          |   3 +
 tools/perf/util/hist.c                           |  52 +-
 tools/perf/util/hist.h                           |   5 +
 tools/perf/util/session.c                        |  60 +-
 tools/perf/util/session.h                        |   4 +
 tools/perf/util/sort.c                           |   3 +
 tools/perf/util/sort.h                           |   3 +
 tools/perf/util/symbol.h                         |   9 +-
 tools/perf/util/util.c                           |   4 +
 27 files changed, 1304 insertions(+), 26 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_core_misc.c
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_core_misc.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/9 perf/x86: Add Intel core misc PMUs support

From: Kan Liang <[email protected]>

There are miscellaneous free running (read-only) counters in core. These counters may be used simultaneously by other tools, such as turbostat. However, it still make sense to implement them in perf. Because we can conveniently collect them together with other events, and allow to use them from tools without special MSR access code. Furthermore, the handling of the free running counters is very different, so it makes sense to put them into a separate pmu.

These counters include TSC, IA32_APERF, IA32_MPERF, IA32_PPERF, SMI_COUNT, CORE_C*_RESIDENCY and PKG_C*_RESIDENCY.

This patch adds new PMUs to support these counters, including helper functions that add/delete events.
According to counters' scope and category, three PMUs are registered with the perf_event core subsystem.
- 'core_misc': The counter is available for each logical processor. The counter include TSC, IA32_APERF, IA32_MPERF, IA32_PPERF and SMI_COUNT.
- 'power_core': The counter is available for each processor core. The counter include CORE_C*_RESIDENCY, which is power related.
- 'power_pkg': The counter is available for each physical package. The counter include PKG_C*_RESIDENCY, which is power related.

The events are exposed in sysfs for use by perf stat and other tools. The files are:
/sys/devices/core_misc/events/power-aperf /sys/devices/core_misc/events/power-mperf /sys/devices/core_misc/events/power-pperf /sys/devices/core_misc/events/smi-count /sys/devices/core_misc/events/tsc /sys/devices/power_core/events/c*-residency /sys/devices/power_pkg/events/c*-residency

These events only support system-wide mode counting. For power_core/power_pkg, measuring only one CPU per core/socket is sufficient. The /sys/devices/power_*/cpumask file can be used by tools to figure out which CPUs to monitor by default.

The PMU type (attr->type) is dynamically allocated and is available from /sys/devices/core_misc/type and /sys/device/power_*/type.

Sampling is not supported.

Here are some examples.
1. To caculate the CPU%
CPU_Utilization = CPU_CLK_UNHALTED.REF_TSC / TSC

 $ perf stat -x, -e "ref-cycles,core_misc/tsc/" -C0 taskset -c 0 sleep 1
  3481579,,ref-cycles
  2301685567,,core_misc/tsc/
 The CPU% for sleep is 0.15%.
 $ perf stat -x, -e "ref-cycles,core_misc/tsc/" -C0 taskset -c 0
busyloop
  11924042536,,ref-cycles
  11929411840,,core_misc/tsc/
 The CPU% for busyloop is 99.95%

2. To caculate the fraction of time when the core is running in C6
state
CORE_C6_time% = CORE_C6_RESIDENCY / TSC

 $ perf stat -x, -e"power_core/c6-residency/,core_misc/tsc/" -C0
   -- taskset -c 0 sleep 1
  2287199396,,power_core/c6-residency/
  2297755875,,core_misc/tsc/
 For sleep, 99.5% of time run in C6 state.
 $ perf stat -x, -e"power_core/c6-residency/,core_misc/tsc/" -C0
   -- taskset -c 0 busyloop
  1330044,,power_core/c6-residency/
  9932928928,,core_misc/tsc/
 For busyloop, 0.01% of time run in C6 state.
---
 arch/x86/kernel/cpu/Makefile                     |   1 +
 arch/x86/kernel/cpu/perf_event_intel_core_misc.c | 890 +++++++++++++++++++++++
 arch/x86/kernel/cpu/perf_event_intel_core_misc.h |  96 +++
 3 files changed, 987 insertions(+)
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_core_misc.c
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_core_misc.h
arch/x86/kernel/cpu/Makefile +1 -0
index 9bff687..a516820 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_p6.o perf_event_knc.o perf_event_p4.o
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_rapl.o perf_event_intel_cqm.o
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_pt.o perf_event_intel_bts.o
+obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_core_misc.o
 
 obj-$(CONFIG_PERF_EVENTS_INTEL_UNCORE)	+= perf_event_intel_uncore.o \
 					   perf_event_intel_uncore_snb.o \
arch/x86/kernel/cpu/perf_event_intel_core_misc.c new file +890 -0

890 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event_intel_core_misc.h new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/9 perf/x86: core_misc PMU disable and enable support

In reply to [email protected]

From: Kan Liang <[email protected]>

This patch implements core_misc PMU disable and enable functions. core_misc PMU counters are free running counters, so it's impossible to stop/start them. The "disable" means not read counters.
With disable/enable functions, it's possible to "disable" core_misc events when other PMU events stop. For example, we are able to stop read the core_misc counter during irq handler.

---
 arch/x86/include/asm/perf_event.h                |  2 ++
 arch/x86/kernel/cpu/perf_event.h                 | 10 ++++++
 arch/x86/kernel/cpu/perf_event_intel.c           |  4 +++
 arch/x86/kernel/cpu/perf_event_intel_core_misc.c | 41 ++++++++++++++++++++++++
 4 files changed, 57 insertions(+)
arch/x86/include/asm/perf_event.h +2 -0
index dc0f6ed..2905f4c 100644
@@ -11,6 +11,8 @@
 
 #define X86_PMC_IDX_MAX					       64
 
+#define X86_CORE_MISC_COUNTER_MAX			       64
+
 #define MSR_ARCH_PERFMON_PERFCTR0			      0xc1
 #define MSR_ARCH_PERFMON_PERFCTR1			      0xc2
 
arch/x86/kernel/cpu/perf_event.h +10 -0
index 3e7fd27..fb14f8a 100644
@@ -239,6 +239,12 @@ struct cpu_hw_events {
 	int excl_thread_id; /* 0 or 1 */
 
 	/*
+	 * Intel core misc
+	 */
+	struct perf_event	*core_misc_events[X86_CORE_MISC_COUNTER_MAX]; /* in counter order */
+	unsigned long		core_misc_active_mask[BITS_TO_LONGS(X86_CORE_MISC_COUNTER_MAX)];
+
+	/*
 	 * AMD specific bits
 	 */
 	struct amd_nb			*amd_nb;
@@ -927,6 +933,10 @@ int p6_pmu_init(void);
 
 int knc_pmu_init(void);
 
+void intel_core_misc_pmu_enable(void);
+
+void intel_core_misc_pmu_disable(void);
+
 ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
 			  char *page);
 
arch/x86/kernel/cpu/perf_event_intel.c +4 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 	if (!x86_pmu.late_ack)
 		apic_write(APIC_LVTPC, APIC_DM_NMI);
 	__intel_pmu_disable_all();
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_disable();
 	handled = intel_pmu_drain_bts_buffer();
 	handled += intel_bts_interrupt();
 	status = intel_pmu_get_status();
@@ -1671,6 +1673,8 @@ again:
 
 done:
 	__intel_pmu_enable_all(0, true);
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_enable();
 	/*
 	 * Only unmask the NMI after the overflow counters
 	 * have been reset. This avoids spurious NMIs on
arch/x86/kernel/cpu/perf_event_intel_core_misc.c +41 -0

77 lines not on this page · raw message

Signature · 1 line
1.8.3.1
reviewer
#3 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 5 lines · On Thu, Jul 16, 2015 at 09:33:44PM +0100, [email protected] wrote:

From: Kan Liang <[email protected]>

This patch implements core_misc PMU disable and enable functions. core_misc PMU counters are free running counters, so it's impossible to stop/start them.

Doesn't that effectively mean you can't group them? You'll get arbitrary noise because counters will be incrementing as you read them.

[...]

Quoted 16 lines
arch/x86/kernel/cpu/perf_event.h +4 -0
@@ -927,6 +933,10 @@ int p6_pmu_init(void);
 
 int knc_pmu_init(void);
 
+void intel_core_misc_pmu_enable(void);
+
+void intel_core_misc_pmu_disable(void);
+
 ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
 			  char *page);
 
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index b9826a9..651a86d 100644

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 	if (!x86_pmu.late_ack)
 		apic_write(APIC_LVTPC, APIC_DM_NMI);
 	__intel_pmu_disable_all();
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very hacky.

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Quoted 30 lines
arch/x86/kernel/cpu/perf_event_intel.c +26 -0
+void intel_core_misc_pmu_enable(void)
+{
+	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
+	struct perf_event *event;
+	u64 start;
+	int bit;
+
+	for_each_set_bit(bit, cpuc->core_misc_active_mask,
+			 X86_CORE_MISC_COUNTER_MAX) {
+		event = cpuc->core_misc_events[bit];
+		start = core_misc_pmu_read_counter(event);
+		local64_set(&event->hw.prev_count, start);
+	}
+}
+
+void intel_core_misc_pmu_disable(void)
+{
+	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
+	int bit;
+
+	for_each_set_bit(bit, cpuc->core_misc_active_mask,
+			 X86_CORE_MISC_COUNTER_MAX) {
+		core_misc_pmu_event_update(cpuc->core_misc_events[bit]);
+	}
+}
+
 static void core_misc_pmu_event_del(struct perf_event *event, int mode)
 {
 	core_misc_pmu_event_stop(event, PERF_EF_UPDATE);

arch/x86/kernel/cpu/perf_event_intel.c static void __init core_misc_pmus_register(void) · line 904

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +3 -0
@@ -863,6 +899,11 @@ static void __init core_misc_pmus_register(void)
 			.capabilities	= PERF_PMU_CAP_NO_INTERRUPT,
 		};
 
+		if (type->type == perf_intel_core_misc_thread) {
+			type->pmu.pmu_disable = (void *) intel_core_misc_pmu_disable;
+			type->pmu.pmu_enable = (void *) intel_core_misc_pmu_enable;

Why are you suprressing an entirely valid compiler warning here?

The signatures of intel_core_misc_pmu_{enable,disable} aren't right. Fix them to take a struct pmu *.

Mark.

maintainer
#4 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 14 lines · Mark Rutland

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 	if (!x86_pmu.late_ack)
 		apic_write(APIC_LVTPC, APIC_DM_NMI);
 	__intel_pmu_disable_all();
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very hacky.

Quoted 3 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

maintainer
#5 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Mark Rutland

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 	if (!x86_pmu.late_ack)
 		apic_write(APIC_LVTPC, APIC_DM_NMI);
 	__intel_pmu_disable_all();
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very hacky.

Quoted 6 lines
Quoted 3 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

lkml.kernel.org/r/2c37309d20afadf88ad4a82cf0ce02b9152801e2.1430256154.git.luto@kernel.org

That does the right thing for free running MSRs.

Take it and expand.

author
#6 lore raw

RE: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 27 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Quoted 6 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct

pt_regs *regs)

Quoted 9 lines
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
 	if (!x86_pmu.late_ack)
 		apic_write(APIC_LVTPC, APIC_DM_NMI);
 	__intel_pmu_disable_all();
+	if (cpuc->core_misc_active_mask)
+		intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very hacky.

Quoted 6 lines
Quoted 3 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

Quoted 6 lines

lkml.kernel.org/r/2c37309d20afadf88ad4a82cf0ce02b9152801e2.143025615 [email protected]

That does the right thing for free running MSRs.

Take it and expand.

The first patch did the similar thing as the link you shared with. Here is the first patch.
https://lkml.org/lkml/2015/7/16/953

This patch is expend the per-core core_misc PMU based on the first patch. I implemented this patch is because that one of the biggest concern from upstream for mix PMU group is that it breaks group semantics. When one PMU is stop, the other PMU is still running.
So I introduce the enable/disable function. Other PMUs can discard the counter value for core_misc event when they are stop or in irq.

If you think it should not live in the regular PMU, I can just remove the codes. We just keep core_misc event running and no harm in it.

Thanks,
Kan

#7 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 45 lines · Kan
Quoted 27 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Quoted 6 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct

pt_regs *regs)

Quoted 9 lines
Quoted 9 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
        if (!x86_pmu.late_ack)
                apic_write(APIC_LVTPC, APIC_DM_NMI);
        __intel_pmu_disable_all();
+       if (cpuc->core_misc_active_mask)
+               intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very hacky.

Quoted 6 lines
Quoted 3 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

Quoted 6 lines

lkml.kernel.org/r/2c37309d20afadf88ad4a82cf0ce02b9152801e2.143025615 [email protected]

That does the right thing for free running MSRs.

Take it and expand.

The first patch did the similar thing as the link you shared with. Here is the first patch.
https://lkml.org/lkml/2015/7/16/953

This patch is expend the per-core core_misc PMU based on the first patch. I implemented this patch is because that one of the biggest concern from upstream for mix PMU group is that it breaks group semantics. When one PMU is stop, the other PMU is still running.
So I introduce the enable/disable function. Other PMUs can discard the counter value for core_misc event when they are stop or in irq.

Quoted 2 lines

If you think it should not live in the regular PMU, I can just remove the codes. We just keep core_misc event running and no harm in it.

I know very little about perf pmu organization, but I think that AMD supports APERF and MPERF, too, so it may make sense to have that thing live outside a file with "intel" in the name.

Also, should the driver detect those using the cpuid bit?

--Andy

author
#8 lore raw

RE: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 53 lines · Kan
Quoted 11 lines
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Quoted 6 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct

pt_regs *regs)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct pt_regs *regs) · line 1590

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
        if (!x86_pmu.late_ack)
                apic_write(APIC_LVTPC, APIC_DM_NMI);
        __intel_pmu_disable_all();
+       if (cpuc->core_misc_active_mask)
+               intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very

hacky.

Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

lkml.kernel.org/r/2c37309d20afadf88ad4a82cf0ce02b9152801e2.143025615

Quoted 11 lines
Quoted 5 lines

[email protected]

That does the right thing for free running MSRs.

Take it and expand.

The first patch did the similar thing as the link you shared with. Here is the first patch.
https://lkml.org/lkml/2015/7/16/953

This patch is expend the per-core core_misc PMU based on the first

patch.

Quoted 7 lines

I implemented this patch is because that one of the biggest concern from upstream for mix PMU group is that it breaks group semantics. When one PMU is stop, the other PMU is still running.
So I introduce the enable/disable function. Other PMUs can discard the counter value for core_misc event when they are stop or in irq.

If you think it should not live in the regular PMU, I can just remove the

Quoted 9 lines

codes.

Quoted 1 line

We just keep core_misc event running and no harm in it.

I know very little about perf pmu organization, but I think that AMD supports APERF and MPERF, too, so it may make sense to have that thing live outside a file with "intel" in the name.

Also, should the driver detect those using the cpuid bit?

Hi Andy,

Yes, it detects the cpuid to determine which counters are available. If we want to implement a common file for both Intel and AMD, we can also check cupid.06h.ecx[bit 0] for a/mperf availability on Intel platform.

Do you have a V2 patch already? I'm asking is because you once mentioned it... :)

Hi Peter,

I think I misread your meaning after go through all your comments for Andy's patch.
Sorry for that.

Yes, I can add the APERF and MPERF part into Andy's patch. As you suggested, make that perf_sw_context.
So we don't need to special case for mix PMUs.

But I think we still need a patch for support CORE_C*_RESIDENCY and PKG_C*_RESIDENCY, which are Intel specific?
I will send it separately.

Thanks,
Kan ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#9 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 65 lines · Kan
Quoted 53 lines · Kan
Quoted 11 lines
Quoted 11 lines · Peter Zijlstra
Quoted 7 lines · Mark Rutland
Quoted 6 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index b9826a9..651a86d 100644
@@ -1586,6 +1586,8 @@ static int intel_pmu_handle_irq(struct

pt_regs *regs)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kernel/cpu/perf_event_intel.c static int intel_pmu_handle_irq(struct · line 1590

Quoted 5 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -0
        if (!x86_pmu.late_ack)
                apic_write(APIC_LVTPC, APIC_DM_NMI);
        __intel_pmu_disable_all();
+       if (cpuc->core_misc_active_mask)
+               intel_core_misc_pmu_disable();

Huh? Free running counters have nothing to do with the PMU interrupt; there's nothing they can do to trigger it. This feels very

hacky.

Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

If this is necessary, surely it should live in __intel_pmu_disable_all?

[...]

Yeah this is crazy. It should not live in the regular PMU at all, not be Intel specific.

lkml.kernel.org/r/2c37309d20afadf88ad4a82cf0ce02b9152801e2.143025615

Quoted 11 lines
Quoted 5 lines

[email protected]

That does the right thing for free running MSRs.

Take it and expand.

The first patch did the similar thing as the link you shared with. Here is the first patch.
https://lkml.org/lkml/2015/7/16/953

This patch is expend the per-core core_misc PMU based on the first

patch.

Quoted 7 lines

I implemented this patch is because that one of the biggest concern from upstream for mix PMU group is that it breaks group semantics. When one PMU is stop, the other PMU is still running.
So I introduce the enable/disable function. Other PMUs can discard the counter value for core_misc event when they are stop or in irq.

If you think it should not live in the regular PMU, I can just remove the

Quoted 9 lines

codes.

Quoted 1 line

We just keep core_misc event running and no harm in it.

I know very little about perf pmu organization, but I think that AMD supports APERF and MPERF, too, so it may make sense to have that thing live outside a file with "intel" in the name.

Also, should the driver detect those using the cpuid bit?

Hi Andy,

Quoted 7 lines

Yes, it detects the cpuid to determine which counters are available. If we want to implement a common file for both Intel and AMD, we can also check cupid.06h.ecx[bit 0] for a/mperf availability on Intel platform.

Do you have a V2 patch already? I'm asking is because you once mentioned it... :)

No, and I also don't have PPERF hardware, etc, so there's not really much I can do. Feel free to do whatever you like with my v1.

--Andy

author
#10 lore raw

RE: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 8 lines
Quoted 4 lines

Do you have a V2 patch already? I'm asking is because you once mentioned it... :)

No, and I also don't have PPERF hardware, etc, so there's not really much I can do. Feel free to do whatever you like with my v1.

OK. I will add my code based on V1, and send it back to you. I don't have AMD machine. Could you please test on them?

Thanks,
Kan ���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m���� ������?�I�

#11 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

Quoted 11 lines · Kan
Quoted 8 lines
Quoted 4 lines

Do you have a V2 patch already? I'm asking is because you once mentioned it... :)

No, and I also don't have PPERF hardware, etc, so there's not really much I can do. Feel free to do whatever you like with my v1.

OK. I will add my code based on V1, and send it back to you. I don't have AMD machine. Could you please test on them?

No, because I don't either. But I'm sure someone will volunteer.

--Andy

Quoted 2 lines

Thanks,
Kan

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
maintainer
#12 lore raw

Re: [PATCH 2/9] perf/x86: core_misc PMU disable and enable support

In reply to Liang, Kan
Quoted 10 lines · Kan
Quoted 8 lines
Quoted 4 lines

Do you have a V2 patch already? I'm asking is because you once mentioned it... :)

No, and I also don't have PPERF hardware, etc, so there's not really much I can do. Feel free to do whatever you like with my v1.

OK. I will add my code based on V1, and send it back to you. I don't have AMD machine. Could you please test on them?

I have an AMD machine (interlagos based).

#13 lore raw

PATCH 3/9 perf/x86: Add is_hardware_event

In reply to [email protected]

From: Kan Liang <[email protected]>

Using is_hardware_event to replace !is_software_event to indicate a hardware event.

---
 include/linux/perf_event.h | 7 ++++++-
 kernel/events/core.c       | 6 +++---
 2 files changed, 9 insertions(+), 4 deletions(-)
include/linux/perf_event.h +6 -1
index 2027809..fea0ddf 100644
@@ -761,13 +761,18 @@ static inline bool is_sampling_event(struct perf_event *event)
 }
 
 /*
- * Return 1 for a software event, 0 for a hardware event
+ * Return 1 for a software event, 0 for other event
  */
 static inline int is_software_event(struct perf_event *event)
 {
 	return event->pmu->task_ctx_nr == perf_sw_context;
 }
 
+static inline int is_hardware_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr == perf_hw_context;
+}
+
 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
kernel/events/core.c +3 -3
index d3dae34..9077867 100644
@@ -1347,7 +1347,7 @@ static void perf_group_attach(struct perf_event *event)
 	WARN_ON_ONCE(group_leader->ctx != event->ctx);
 
 	if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
-			!is_software_event(event))
+			is_hardware_event(event))
 		group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
 
 	list_add_tail(&event->group_entry, &group_leader->sibling_list);
@@ -1553,7 +1553,7 @@ event_sched_out(struct perf_event *event,
 	event->pmu->del(event, 0);
 	event->oncpu = -1;
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu--;
 	if (!--ctx->nr_active)
 		perf_event_ctx_deactivate(ctx);
@@ -1881,7 +1881,7 @@ event_sched_in(struct perf_event *event,
 		goto out;
 	}
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu++;
 	if (!ctx->nr_active++)
 		perf_event_ctx_activate(ctx);
Signature · 1 line
1.8.3.1
reviewer
#14 lore raw

Re: [PATCH 3/9] perf/x86: Add is_hardware_event

Quoted 4 lines · On Thu, Jul 16, 2015 at 09:33:45PM +0100, [email protected] wrote:

From: Kan Liang <[email protected]>

Using is_hardware_event to replace !is_software_event to indicate a hardware event.

Why...?

For an uncore event e, is_hardware_event(e) != !is_software_event(e), so this will be a change of behaviour...

Quoted 54 lines
---
 include/linux/perf_event.h | 7 ++++++-
 kernel/events/core.c       | 6 +++---
 2 files changed, 9 insertions(+), 4 deletions(-)
include/linux/perf_event.h +6 -1
index 2027809..fea0ddf 100644
@@ -761,13 +761,18 @@ static inline bool is_sampling_event(struct perf_event *event)
 }
 
 /*
- * Return 1 for a software event, 0 for a hardware event
+ * Return 1 for a software event, 0 for other event
  */
 static inline int is_software_event(struct perf_event *event)
 {
 	return event->pmu->task_ctx_nr == perf_sw_context;
 }
 
+static inline int is_hardware_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr == perf_hw_context;
+}
+
 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
kernel/events/core.c +2 -2
index d3dae34..9077867 100644
@@ -1347,7 +1347,7 @@ static void perf_group_attach(struct perf_event *event)
 	WARN_ON_ONCE(group_leader->ctx != event->ctx);
 
 	if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
-			!is_software_event(event))
+			is_hardware_event(event))
 		group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
 
 	list_add_tail(&event->group_entry, &group_leader->sibling_list);
@@ -1553,7 +1553,7 @@ event_sched_out(struct perf_event *event,
 	event->pmu->del(event, 0);
 	event->oncpu = -1;
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu--;
 	if (!--ctx->nr_active)
 		perf_event_ctx_deactivate(ctx);

kernel/events/core.c event_sched_in(struct perf_event *event, · line 1887

Quoted 8 lines
kernel/events/core.c +1 -1
@@ -1881,7 +1881,7 @@ event_sched_in(struct perf_event *event,
 		goto out;
 	}
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu++;
 	if (!ctx->nr_active++)
 		perf_event_ctx_activate(ctx);

... whereby we won't accuont uncore events as active, and thereforef will never perform throttling.

That doesn't sound right.

Mark.

author
#15 lore raw

RE: [PATCH 3/9] perf/x86: Add is_hardware_event

Quoted 8 lines · On Thu, Jul 16, 2015 at 09:33:45PM +0100, [email protected] wrote:
Quoted 4 lines

From: Kan Liang <[email protected]>

Using is_hardware_event to replace !is_software_event to indicate a hardware event.

Why...?

First, the comments of is_software_event is not correct. 0 or !is_software_event is not for a hardware event. is_hardware_event is for a hardware event.

Also, the following patch make mix core_misc event be part of hw/sw event, !is_software_event could be either hw event or core_misc event. We need an accurate definition here.

Quoted 3 lines

For an uncore event e, is_hardware_event(e) != !is_software_event(e), so this will be a change of behaviour...

Uncore event cannot be part of hw/sw event group. So it doesn't change the behavior.

Quoted 66 lines
Quoted 16 lines
---
 include/linux/perf_event.h | 7 ++++++-
 kernel/events/core.c       | 6 +++---
 2 files changed, 9 insertions(+), 4 deletions(-)
include/linux/perf_event.h +0 -0
index 2027809..fea0ddf 100644
@@ -761,13 +761,18 @@ static inline bool is_sampling_event(struct
 perf_event *event)  }
 
 /*

include/linux/perf_event.h static inline bool is_sampling_event(struct · line 772

Quoted 10 lines
include/linux/perf_event.h +4 -1
- * Return 1 for a software event, 0 for a hardware event
+ * Return 1 for a software event, 0 for other event
  */
 static inline int is_software_event(struct perf_event *event)  {
 	return event->pmu->task_ctx_nr == perf_sw_context;  }
 
+static inline int is_hardware_event(struct perf_event *event) {
+	return event->pmu->task_ctx_nr == perf_hw_context; }
+
 extern struct static_key

perf_swevent_enabled[PERF_COUNT_SW_MAX];

kernel/events/core.c static void perf_group_attach(struct

Quoted 7 lines
include/linux/perf_event.h +0 -1
 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64); diff
--git a/kernel/events/core.c b/kernel/events/core.c index

d3dae34..9077867 100644

kernel/events/core.c +0 -0
@@ -1347,7 +1347,7 @@ static void perf_group_attach(struct

perf_event *event)

kernel/events/core.c static void perf_group_attach(struct perf_event *event) · line 1353

Quoted 8 lines
kernel/events/core.c +1 -1
 	WARN_ON_ONCE(group_leader->ctx != event->ctx);
 
 	if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
-			!is_software_event(event))
+			is_hardware_event(event))
 		group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
 
 	list_add_tail(&event->group_entry, &group_leader->sibling_list);

@@

Quoted 10 lines

-1553,7 +1553,7 @@ event_sched_out(struct perf_event *event,

kernel/events/core.c +1 -1
 	event->pmu->del(event, 0);
 	event->oncpu = -1;
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu--;
 	if (!--ctx->nr_active)
 		perf_event_ctx_deactivate(ctx);

kernel/events/core.c event_sched_in(struct perf_event *event, · line 1887

Quoted 8 lines
kernel/events/core.c +1 -1
@@ -1881,7 +1881,7 @@ event_sched_in(struct perf_event *event,
 		goto out;
 	}
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu++;
 	if (!ctx->nr_active++)
 		perf_event_ctx_activate(ctx);

... whereby we won't accuont uncore events as active, and thereforef will never perform throttling.

Quoted 1 line

That doesn't sound right.

I think active_oncpu should only impact if the group is exclusive. The changes will make pure perf_invalid_context event group never exclusive. If that's a problem, I will change this part back.

Thanks,
Kan

Quoted 2 lines

Mark.

reviewer
#16 lore raw

Re: [PATCH 3/9] perf/x86: Add is_hardware_event

Quoted 4 lines · Kan
Quoted 4 lines · On Thu, Jul 16, 2015 at 09:33:45PM +0100, [email protected] wrote:
Quoted 2 lines

From: Kan Liang <[email protected]>

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Using is_hardware_event to replace !is_software_event to indicate a hardware event.

Why...?

First, the comments of is_software_event is not correct. 0 or !is_software_event is not for a hardware event. is_hardware_event is for a hardware event.

Circular logic is fantastic.

Quoted 2 lines

Also, the following patch make mix core_misc event be part of hw/sw event, !is_software_event could be either hw event or core_misc event.

!is_software_event is also true for an uncore event currently, and the code relies on this fact. Blindly replacing !is_software_event with is_hardware_event changes the behaviour of the code for uncore events.

Quoted 4 lines
Quoted 2 lines

For an uncore event e, is_hardware_event(e) != !is_software_event(e), so this will be a change of behaviour...

Uncore event cannot be part of hw/sw event group. So it doesn't change the behavior.

My complaint had _nothing_ to do with groups. It had to do with the accounting for throttling, where it _does_ change the behaviour.

However, now that you mention the group logic...

Quoted 20 lines
Quoted 20 lines

Review

Quoted 11 lines

/*
- * Return 1 for a software event, 0 for a hardware event
+ * Return 1 for a software event, 0 for other event
*/
static inline int is_software_event(struct perf_event *event) {

(quoted diff) +3 -0
 	return event->pmu->task_ctx_nr == perf_sw_context;  }
 
+static inline int is_hardware_event(struct perf_event *event) {
+	return event->pmu->task_ctx_nr == perf_hw_context; }
+
 extern struct static_key

perf_swevent_enabled[PERF_COUNT_SW_MAX];

kernel/events/core.c static void perf_group_attach(struct

Quoted 7 lines

extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64); diff
--git a/kernel/events/core.c b/kernel/events/core.c index d3dae34..9077867 100644

kernel/events/core.c +0 -0
@@ -1347,7 +1347,7 @@ static void perf_group_attach(struct

perf_event *event)

Quoted 7 lines
Quoted 7 lines

kernel/events/core.c static void perf_group_attach(struct perf_event *event) · line 1352

Quoted 7 lines
kernel/events/core.c +1 -1
 	WARN_ON_ONCE(group_leader->ctx != event->ctx);
 
 	if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
-			!is_software_event(event))
+			is_hardware_event(event))
 		group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
 

...this changes the behaviour of attaching an uncore event to a software group.

Before, we'd correctly clear the PERF_GROUP_SOFTWARE flag on the leader. After this patch, we don't. That is a bug.

My original complaint was with the changes below.

Quoted 11 lines
Quoted 11 lines

kernel/events/core.c static void perf_group_attach(struct perf_event *event) · line 1353

Quoted 1 line
kernel/events/core.c +0 -0
 	list_add_tail(&event->group_entry, &group_leader->sibling_list);

@@

kernel/events/core.c event_sched_out(struct perf_event *event, · line 1559

Quoted 9 lines

-1553,7 +1553,7 @@ event_sched_out(struct perf_event *event,

kernel/events/core.c +1 -1
 	event->pmu->del(event, 0);
 	event->oncpu = -1;
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu--;
 	if (!--ctx->nr_active)
 		perf_event_ctx_deactivate(ctx);

Previously we'd call perf_event_ctx_deactivate() for an uncore PMU's contexts, but now we never will.

Quoted 9 lines
Quoted 9 lines

kernel/events/core.c event_sched_in(struct perf_event *event, · line 1887

Quoted 9 lines
kernel/events/core.c +1 -1
@@ -1881,7 +1881,7 @@ event_sched_in(struct perf_event *event,
 		goto out;
 	}
 
-	if (!is_software_event(event))
+	if (is_hardware_event(event))
 		cpuctx->active_oncpu++;
 	if (!ctx->nr_active++)
 		perf_event_ctx_activate(ctx);

Similarly for perf_event_ctx_deactivate().

As I mention below, That means we will no longer perform throttling for an uncore PMU's cpu context (see perf_event_task_tick()).

Quoted 8 lines
Quoted 4 lines

... whereby we won't accuont uncore events as active, and thereforef will never perform throttling.

That doesn't sound right.

I think active_oncpu should only impact if the group is exclusive. The changes will make pure perf_invalid_context event group never exclusive. If that's a problem, I will change this part back.

I'm not sure what you mean here -- I can't see what a group being exclusive has to do with any of the points above.

What am I missing?

Thanks,
Mark.

reviewer
#17 lore raw

Re: [PATCH 3/9] perf/x86: Add is_hardware_event

Quoted 8 lines · Mark Rutland
Quoted 7 lines · Kan
Quoted 7 lines · On Thu, Jul 16, 2015 at 09:33:45PM +0100, [email protected] wrote:
Quoted 4 lines

From: Kan Liang <[email protected]>

Using is_hardware_event to replace !is_software_event to indicate a hardware event.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Why...?

First, the comments of is_software_event is not correct. 0 or !is_software_event is not for a hardware event. is_hardware_event is for a hardware event.

Circular logic is fantastic.

Sorry for the snark here. I completely misread this.

I agree that the comment is wrong. However, changing !is_software_event to is_hardware_event is not always correct.

For example, perf_group_attach tests for the addition of a non-software event to a software group, so we can mark the group as not consisting solely of software events. For that to be done correctly, we need to check !is_software_event.

I was wrong about the throttling, having confused active_oncpu and nr_active. Sorry for the noise on that. However, as you mention that does prevent the use of exclusive events for uncore PMUs, and I don't see why that should change.

Thanks,
Mark.

#18 lore raw

PATCH 4/9 perf/x86: special case per-cpu core misc PMU events

In reply to [email protected]

From: Kan Liang <[email protected]>

This patch special case per-cpu core_misc PMU events and allow them to be part of any hardware/software group for system-wide monitoring. An useful example would be to include the ASTATE/MSTATE event in a sampling group. This can be used to calculate the frequency during each sampling period, and track it over time.

A new context type (perf_free_context) is introduced to indicate these per-cpu core misc PMU events. They are
- Free running counter
- Don't have any state to switch on context switch and never fails to schedule
- No sampling support
- Only support system-wide monitoring
- per-cpu
We also defined a new PERF event type PERF_TYPE_CORE_MISC_FREE for them.

It's safe to mix cpu PMU events and CORE_MISC_FREE events in a group. Because when cpu PMU events disable/enable, we can disable/enable them at the same time without failure.

Since there is no sampling support for these events. They are only available for group reading and system-wide monitoring.

---
 arch/x86/kernel/cpu/perf_event_intel_core_misc.c |  8 +++++---
 include/linux/perf_event.h                       | 10 ++++++++++
 include/linux/sched.h                            |  1 +
 include/uapi/linux/perf_event.h                  |  1 +
 kernel/events/core.c                             |  9 +++++++++
 5 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_core_misc.c +5 -3
index 4efe842..dad4495 100644
@@ -889,7 +889,6 @@ static void __init core_misc_pmus_register(void)
 
 		type->pmu = (struct pmu) {
 			.attr_groups	= type->pmu_group,
-			.task_ctx_nr	= perf_invalid_context,
 			.event_init	= core_misc_pmu_event_init,
 			.add		= core_misc_pmu_event_add, /* must have */
 			.del		= core_misc_pmu_event_del, /* must have */
@@ -902,9 +901,12 @@ static void __init core_misc_pmus_register(void)
 		if (type->type == perf_intel_core_misc_thread) {
 			type->pmu.pmu_disable = (void *) intel_core_misc_pmu_disable;
 			type->pmu.pmu_enable = (void *) intel_core_misc_pmu_enable;
+			type->pmu.task_ctx_nr = perf_free_context;
+			err = perf_pmu_register(&type->pmu, type->name, PERF_TYPE_CORE_MISC_FREE);
+		} else {
+			type->pmu.task_ctx_nr = perf_invalid_context;
+			err = perf_pmu_register(&type->pmu, type->name, -1);
 		}
-
-		err = perf_pmu_register(&type->pmu, type->name, -1);
 		if (WARN_ON(err))
 			pr_info("Failed to register PMU %s error %d\n",
 				type->pmu.name, err);
include/linux/perf_event.h +10 -0
index fea0ddf..3538f1c 100644
@@ -773,6 +773,16 @@ static inline int is_hardware_event(struct perf_event *event)
 	return event->pmu->task_ctx_nr == perf_hw_context;
 }
 
+static inline int is_free_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr == perf_free_context;
+}
+
+static inline int has_context_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr > perf_invalid_context;
+}
+
 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
include/linux/sched.h +1 -0
index ae21f15..717f492 100644
@@ -1335,6 +1335,7 @@ union rcu_special {
 struct rcu_node;
 
 enum perf_event_task_context {
+	perf_free_context = -2,
 	perf_invalid_context = -1,
 	perf_hw_context = 0,
 	perf_sw_context,
include/uapi/linux/perf_event.h +1 -0
index d97f84c..232b674 100644
@@ -32,6 +32,7 @@ enum perf_type_id {
 	PERF_TYPE_HW_CACHE			= 3,
 	PERF_TYPE_RAW				= 4,
 	PERF_TYPE_BREAKPOINT			= 5,
+	PERF_TYPE_CORE_MISC_FREE		= 6,
 
 	PERF_TYPE_MAX,				/* non-ABI */
 };
kernel/events/core.c +9 -0
index 9077867..995b436 100644
@@ -8019,6 +8019,15 @@ SYSCALL_DEFINE5(perf_event_open,
 	}
 
 	/*
+	 * Special case per-cpu free counter events and allow them to be part of
+	 * any hardware/software group for system-wide monitoring.
+	 */
+	if (group_leader && !task &&
+	    is_free_event(event) &&
+	    has_context_event(group_leader))
+		pmu = group_leader->pmu;
+
+	/*
 	 * Get the target context (task or percpu):
 	 */
 	ctx = find_get_context(pmu, task, event);
Signature · 1 line
1.8.3.1
reviewer
#19 lore raw

Re: [PATCH 4/9] perf/x86: special case per-cpu core misc PMU events

Quoted 18 lines · On Thu, Jul 16, 2015 at 09:33:46PM +0100, [email protected] wrote:

From: Kan Liang <[email protected]>

This patch special case per-cpu core_misc PMU events and allow them to be part of any hardware/software group for system-wide monitoring. An useful example would be to include the ASTATE/MSTATE event in a sampling group. This can be used to calculate the frequency during each sampling period, and track it over time.

A new context type (perf_free_context) is introduced to indicate these per-cpu core misc PMU events. They are
- Free running counter
- Don't have any state to switch on context switch and never fails to schedule
- No sampling support
- Only support system-wide monitoring
- per-cpu
We also defined a new PERF event type PERF_TYPE_CORE_MISC_FREE for them.

Quoted 3 lines

It's safe to mix cpu PMU events and CORE_MISC_FREE events in a group. Because when cpu PMU events disable/enable, we can disable/enable them at the same time without failure.

Which effectively means you're context-switching their state (given what your enable/disable code does).

As with my earlier comments, I don't think these can be grouped with events (not even from the same PMU given their free-running nature).

They're CPU-affine, so you can associate them with work done on that CPU.

So as far as I can see, you should be able to handle the per-cpu misc events in the perf_hardware_context, providing you reject grouping in your event_init functions.

What does this extra context give you?

Mark.

Quoted 107 lines

Since there is no sampling support for these events. They are only available for group reading and system-wide monitoring.

---
 arch/x86/kernel/cpu/perf_event_intel_core_misc.c |  8 +++++---
 include/linux/perf_event.h                       | 10 ++++++++++
 include/linux/sched.h                            |  1 +
 include/uapi/linux/perf_event.h                  |  1 +
 kernel/events/core.c                             |  9 +++++++++
 5 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_core_misc.c +5 -3
index 4efe842..dad4495 100644
@@ -889,7 +889,6 @@ static void __init core_misc_pmus_register(void)
 
 		type->pmu = (struct pmu) {
 			.attr_groups	= type->pmu_group,
-			.task_ctx_nr	= perf_invalid_context,
 			.event_init	= core_misc_pmu_event_init,
 			.add		= core_misc_pmu_event_add, /* must have */
 			.del		= core_misc_pmu_event_del, /* must have */
@@ -902,9 +901,12 @@ static void __init core_misc_pmus_register(void)
 		if (type->type == perf_intel_core_misc_thread) {
 			type->pmu.pmu_disable = (void *) intel_core_misc_pmu_disable;
 			type->pmu.pmu_enable = (void *) intel_core_misc_pmu_enable;
+			type->pmu.task_ctx_nr = perf_free_context;
+			err = perf_pmu_register(&type->pmu, type->name, PERF_TYPE_CORE_MISC_FREE);
+		} else {
+			type->pmu.task_ctx_nr = perf_invalid_context;
+			err = perf_pmu_register(&type->pmu, type->name, -1);
 		}
-
-		err = perf_pmu_register(&type->pmu, type->name, -1);
 		if (WARN_ON(err))
 			pr_info("Failed to register PMU %s error %d\n",
 				type->pmu.name, err);
include/linux/perf_event.h +10 -0
index fea0ddf..3538f1c 100644
@@ -773,6 +773,16 @@ static inline int is_hardware_event(struct perf_event *event)
 	return event->pmu->task_ctx_nr == perf_hw_context;
 }
 
+static inline int is_free_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr == perf_free_context;
+}
+
+static inline int has_context_event(struct perf_event *event)
+{
+	return event->pmu->task_ctx_nr > perf_invalid_context;
+}
+
 extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
 extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
include/linux/sched.h +1 -0
index ae21f15..717f492 100644
@@ -1335,6 +1335,7 @@ union rcu_special {
 struct rcu_node;
 
 enum perf_event_task_context {
+	perf_free_context = -2,
 	perf_invalid_context = -1,
 	perf_hw_context = 0,
 	perf_sw_context,
include/uapi/linux/perf_event.h +1 -0
index d97f84c..232b674 100644
@@ -32,6 +32,7 @@ enum perf_type_id {
 	PERF_TYPE_HW_CACHE			= 3,
 	PERF_TYPE_RAW				= 4,
 	PERF_TYPE_BREAKPOINT			= 5,
+	PERF_TYPE_CORE_MISC_FREE		= 6,
 
 	PERF_TYPE_MAX,				/* non-ABI */
 };
kernel/events/core.c +9 -0
index 9077867..995b436 100644
@@ -8019,6 +8019,15 @@ SYSCALL_DEFINE5(perf_event_open,
 	}
 
 	/*
+	 * Special case per-cpu free counter events and allow them to be part of
+	 * any hardware/software group for system-wide monitoring.
+	 */
+	if (group_leader && !task &&
+	    is_free_event(event) &&
+	    has_context_event(group_leader))
+		pmu = group_leader->pmu;
+
+	/*
 	 * Get the target context (task or percpu):
 	 */
 	ctx = find_get_context(pmu, task, event);
Signature · 1 line
1.8.3.1
maintainer
#20 lore raw

Re: [PATCH 4/9] perf/x86: special case per-cpu core misc PMU events

Quoted 6 lines · Mark Rutland

As with my earlier comments, I don't think these can be grouped with events (not even from the same PMU given their free-running nature).

They're CPU-affine, so you can associate them with work done on that CPU.

Just record the deltas from them while you're on.

#21 lore raw

Re: [PATCH 4/9] perf/x86: special case per-cpu core misc PMU events

Hi,

Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Mark Rutland

As with my earlier comments, I don't think these can be grouped with events (not even from the same PMU given their free-running nature).

They're CPU-affine, so you can associate them with work done on that CPU.

Just record the deltas from them while you're on.

Yes, free-running are already handled by the kernel, the RAPL counters are a good example.
The uncore IMC counters (for SNB/IVB/HSW/BDW) client processors is another example.
Just compute deltas, and make sure you do not miss a wrap-around of the counter if it is
not wide enough to never wrap-around.

#22 lore raw

Re: [PATCH 4/9] perf/x86: special case per-cpu core misc PMU events

In reply to Mark Rutland
Quoted 2 lines

As with my earlier comments, I don't think these can be grouped with events (not even from the same PMU given their free-running nature).

Mark, we already went through this last time. There is nothing stopping handling free running counters as part of other groups.

A perf event logically has a 64bit counter that accumulates counts from a less wide hardware counter. A free running counter just has to be sampled at the beginning and at the end of the measurement period, and the difference between the two values added to the perf counter. To handle CPU switches the counter is just sampled, and accumulated into the software counter, before switching to another CPU. Then you start the next measurement period with a sample from the new CPU etc.

-Andi

reviewer
#23 lore raw

Re: [PATCH 4/9] perf/x86: special case per-cpu core misc PMU events

Quoted 5 lines · Andi Kleen
Quoted 2 lines

As with my earlier comments, I don't think these can be grouped with events (not even from the same PMU given their free-running nature).

Mark, we already went through this last time. There is nothing stopping handling free running counters as part of other groups.

Ok. It's inexact (but only marginally), and not the end of the world if you know that's the case.

My concern last time was mainly with the grouping of uncore and CPU events.

Thanks,
Mark.

#24 lore raw

PATCH 5/9 perf,tools: open event with it's own cpus and threads

In reply to [email protected]

From: Kan Liang <[email protected]>

evsel may have different cpus and threads as evlist's. Use it's own cpus and threads, when open evsel in perf record.

---
 tools/perf/builtin-record.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-record.c +1 -1
index 283fe96..eec3ee8 100644
@@ -279,7 +279,7 @@ static int record__open(struct record *rec)
 
 	evlist__for_each(evlist, pos) {
 try_again:
-		if (perf_evsel__open(pos, evlist->cpus, evlist->threads) < 0) {
+		if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
 			if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
 				if (verbose)
 					ui__warning("%s\n", msg);
Signature · 1 line
1.8.3.1
#25 lore raw

PATCH 6/9 perf,tools: Dump per-sample freq in report -D

In reply to [email protected]

From: Kan Liang <[email protected]>

The group read results of TSC/ASTATE/MSTATE event can be used to calculate the frequency during each sampling period. Show it in report -D.

Here is an example:

 $ perf record -e
'{ref-cycles,core_misc/tsc/,core_misc/power-mperf/,core_misc/power-aperf/}:S'
--running-time -a ~/tchain_edit

Here is one sample from perf report -D

18 506413677835 0x3f1d8 [0x90]: PERF_RECORD_SAMPLE(IP, 0x1): 8/8:
0xffffffff810cba6d period: 62219 addr: 0
... sample_read:
...... time enabled 000000000025a464
...... time running 000000000025a464
.... group nr 4
..... id 00000000000001a0, value 000000000008a605 ..... id 00000000000001e2, value 0000000000565ac5 ..... id 0000000000000222, value 0000000000079bc8 ..... id 0000000000000262, value 0000000000068d69 ..... TSC_MHz 2294
..... AVG_MHz 174
..... Bzy_MHz 2663

---
 tools/perf/util/session.c | 40 +++++++++++++++++++++++++++++++++++-----
 tools/perf/util/session.h |  4 ++++
 2 files changed, 39 insertions(+), 5 deletions(-)
tools/perf/util/session.c +35 -5
index ed9dc25..6a142d8 100644
@@ -851,8 +851,14 @@ static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
 		printf("%" PRIu64 " ", sample->time);
 }
 
-static void sample_read__printf(struct perf_sample *sample, u64 read_format)
+static void sample_read__printf(struct perf_evlist *evlist,
+				struct perf_sample *sample,
+				u64 read_format)
 {
+	struct perf_evsel *evsel;
+	struct perf_sample_id *sid;
+	u64 tsc = 0, aperf = 0, mperf = 0;
+
 	printf("... sample_read:\n");
 
 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -875,10 +881,33 @@ static void sample_read__printf(struct perf_sample *sample, u64 read_format)
 			printf("..... id %016" PRIx64
 			       ", value %016" PRIx64 "\n",
 			       value->id, value->value);
+
+			if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
+				sid = perf_evlist__id2sid(evlist, value->id);
+				evsel = sid->evsel;
+				if ((evsel != NULL) &&
+				    (evsel->attr.type == PERF_TYPE_CORE_MISC_FREE)) {
+					if (evsel->attr.config == PERF_POWER_APERF)
+						aperf = value->value;
+					if (evsel->attr.config == PERF_POWER_MPERF)
+						mperf = value->value;
+					if (evsel->attr.config == PERF_TSC)
+						tsc = value->value;
+				}
+			}
 		}
 	} else
 		printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
 			sample->read.one.id, sample->read.one.value);
+
+	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
+		if (tsc > 0)
+			printf("..... TSC_MHz %lu\n", (1000 * tsc) / sample->read.time_running);
+		if (aperf > 0)
+			printf("..... AVG_MHz %lu\n", (1000 * aperf) / sample->read.time_running);
+		if ((tsc > 0) && (aperf > 0) && (mperf > 0))
+			printf("..... Bzy_MHz %lu\n", (1000 * tsc / aperf * mperf) / sample->read.time_running);
+	}
 }
 
 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
@@ -899,8 +928,8 @@ static void dump_event(struct perf_evlist *evlist, union perf_event *event,
 	       event->header.size, perf_event__name(event->header.type));
 }
 
-static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
-			struct perf_sample *sample)
+static void dump_sample(struct perf_evlist *evlist, struct perf_evsel *evsel,
+			union perf_event *event, struct perf_sample *sample)
 {
 	u64 sample_type;
 
@@ -938,7 +967,7 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
 
 	if (sample_type & PERF_SAMPLE_READ)
-		sample_read__printf(sample, evsel->attr.read_format);
+		sample_read__printf(evlist, sample, evsel->attr.read_format);
 }
 
 static struct machine *machines__find_for_cpumode(struct machines *machines,
@@ -1053,11 +1082,12 @@ static int machines__deliver_event(struct machines *machines,
 
 	switch (event->header.type) {
 	case PERF_RECORD_SAMPLE:
-		dump_sample(evsel, event, sample);
 		if (evsel == NULL) {
 			++evlist->stats.nr_unknown_id;
 			return 0;
 		}
+		dump_sample(evlist, evsel, event, sample);
+
 		if (machine == NULL) {
 			++evlist->stats.nr_unprocessable_samples;
 			return 0;
tools/perf/util/session.h +4 -0
index b44afc7..220cfb3 100644
@@ -42,6 +42,10 @@ struct perf_session {
 #define PRINT_IP_OPT_ONELINE	(1<<4)
 #define PRINT_IP_OPT_SRCLINE	(1<<5)
 
+#define PERF_POWER_APERF	1
+#define PERF_POWER_MPERF	2
+#define PERF_TSC		5
+
 struct perf_tool;
 
 struct perf_session *perf_session__new(struct perf_data_file *file,
Signature · 1 line
1.8.3.1
#26 lore raw

PATCH 7/9 perf,tools: save APERF/MPERF/TSC in struct perf_sample

In reply to [email protected]

From: Kan Liang <[email protected]>

Save APERF/MPERF/TSC in struct perf_sample, so the following sample process function can easily handle it.

---
 tools/perf/util/event.h   |  3 +++
 tools/perf/util/session.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
tools/perf/util/event.h +3 -0
index c53f363..5a5431f 100644
@@ -191,6 +191,9 @@ struct perf_sample {
 	u64 data_src;
 	u32 flags;
 	u16 insn_len;
+	u64 tsc;
+	u64 aperf;
+	u64 mperf;
 	void *raw_data;
 	struct ip_callchain *callchain;
 	struct branch_stack *branch_stack;
tools/perf/util/session.c +20 -0
index 6a142d8..bffa58b 100644
@@ -1005,6 +1005,8 @@ static int deliver_sample_value(struct perf_evlist *evlist,
 				struct machine *machine)
 {
 	struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
+	struct perf_evsel *evsel;
+	u64 nr = 0;
 
 	if (sid) {
 		sample->id     = v->id;
@@ -1017,6 +1019,24 @@ static int deliver_sample_value(struct perf_evlist *evlist,
 		return 0;
 	}
 
+	if (perf_evsel__is_group_leader(sid->evsel)) {
+		evsel = sid->evsel;
+		evlist__for_each_continue(evlist, evsel) {
+			if ((evsel->leader != sid->evsel) ||
+			    (++nr >= sample->read.group.nr))
+				break;
+
+			if (evsel->attr.type == PERF_TYPE_CORE_MISC_FREE) {
+				if (evsel->attr.config == PERF_POWER_APERF)
+					sample->aperf = sample->read.group.values[nr].value;
+				if (evsel->attr.config == PERF_POWER_MPERF)
+					sample->mperf = sample->read.group.values[nr].value;
+				if (evsel->attr.config == PERF_TSC)
+					sample->tsc = sample->read.group.values[nr].value;
+			}
+		}
+	}
+
 	return tool->sample(tool, event, sample, sid->evsel, machine);
 }
 
Signature · 1 line
1.8.3.1
#27 lore raw

PATCH 8/9 perf,tools: caculate and save tsc/avg/bzy freq in he_stat

In reply to [email protected]

From: Kan Liang <[email protected]>

Introduce a new hist_iter ops (hist_iter_freq) to caculate the tsc/avg/bzy freq when processing samples, and save them in hist_entry.

---
 tools/perf/builtin-annotate.c |  2 +-
 tools/perf/builtin-diff.c     |  2 +-
 tools/perf/tests/hists_link.c |  4 ++--
 tools/perf/util/hist.c        | 52 ++++++++++++++++++++++++++++++++++++++-----
 tools/perf/util/hist.h        |  2 ++
 tools/perf/util/sort.h        |  3 +++
 tools/perf/util/symbol.h      |  6 +++++
 7 files changed, 61 insertions(+), 10 deletions(-)
tools/perf/builtin-annotate.c +1 -1
index 2c1bec3..06e2f87 100644
@@ -71,7 +71,7 @@ static int perf_evsel__add_sample(struct perf_evsel *evsel,
 		return 0;
 	}
 
-	he = __hists__add_entry(hists, al, NULL, NULL, NULL, 1, 1, 0, true);
+	he = __hists__add_entry(hists, al, NULL, NULL, NULL, 1, 1, 0, NULL, true);
 	if (he == NULL)
 		return -ENOMEM;
 
tools/perf/builtin-diff.c +1 -1
index daaa7dc..2fffcc4 100644
@@ -315,7 +315,7 @@ static int hists__add_entry(struct hists *hists,
 			    u64 weight, u64 transaction)
 {
 	if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,
-			       transaction, true) != NULL)
+			       transaction, NULL, true) != NULL)
 		return 0;
 	return -ENOMEM;
 }
tools/perf/tests/hists_link.c +2 -2
index 8c102b0..5d9f9e3 100644
@@ -90,7 +90,7 @@ static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
 				goto out;
 
 			he = __hists__add_entry(hists, &al, NULL,
-						NULL, NULL, 1, 1, 0, true);
+						NULL, NULL, 1, 1, 0, NULL, true);
 			if (he == NULL) {
 				addr_location__put(&al);
 				goto out;
@@ -116,7 +116,7 @@ static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
 				goto out;
 
 			he = __hists__add_entry(hists, &al, NULL,
-						NULL, NULL, 1, 1, 0, true);
+						NULL, NULL, 1, 1, 0, NULL, true);
 			if (he == NULL) {
 				addr_location__put(&al);
 				goto out;
tools/perf/util/hist.c +46 -6

106 lines not on this page · raw message

tools/perf/util/hist.h +2 -0
index 5ed8d9c..3601658 100644
@@ -102,6 +102,7 @@ extern const struct hist_iter_ops hist_iter_normal;
 extern const struct hist_iter_ops hist_iter_branch;
 extern const struct hist_iter_ops hist_iter_mem;
 extern const struct hist_iter_ops hist_iter_cumulative;
+extern const struct hist_iter_ops hist_iter_freq;
 
 struct hist_entry *__hists__add_entry(struct hists *hists,
 				      struct addr_location *al,
@@ -109,6 +110,7 @@ struct hist_entry *__hists__add_entry(struct hists *hists,
 				      struct branch_info *bi,
 				      struct mem_info *mi, u64 period,
 				      u64 weight, u64 transaction,
+				      struct freq_info *freq,
 				      bool sample_self);
 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
 			 int max_stack_depth, void *arg);
tools/perf/util/sort.h +3 -0
index e97cd47..5720076 100644
@@ -54,6 +54,9 @@ struct he_stat {
 	u64			period_guest_us;
 	u64			weight;
 	u32			nr_events;
+	u64			tsc_freq;
+	u64			avg_freq;
+	u64			bzy_freq;
 };
 
 struct hist_entry_diff {
tools/perf/util/symbol.h +6 -0
index b98ce51..b71d575 100644
@@ -180,6 +180,12 @@ struct mem_info {
 	union perf_mem_data_src data_src;
 };
 
+struct freq_info {
+	u64	tsc_freq;
+	u64	avg_freq;
+	u64	bzy_freq;
+};
+
 struct addr_location {
 	struct machine *machine;
 	struct thread *thread;
Signature · 1 line
1.8.3.1
#28 lore raw

Re: [PATCH 8/9] perf,tools: caculate and save tsc/avg/bzy freq in he_stat

tools/perf/util/hist.c iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, · line 500

Quoted 5 lines
tools/perf/util/hist.c +5 -0
+	if (sample->read.time_running > 0) {
+		freq.tsc_freq = (1000 * sample->tsc) / sample->read.time_running;
+		freq.avg_freq = (1000 * sample->aperf) / sample->read.time_running;
+		if (sample->aperf > 0)
+			freq.bzy_freq = freq.tsc_freq * sample->mperf / sample->aperf;

Sorry didn't notice that earlier. The formula is not correct. aperf/mperf is not necessarily the frequency, it is essentially a load average of the CPU. It should be reported as such. Also only the ratio is architecturally defined.

The right way to compute frequency is cycles / ref-cycles TSC can be used to accurately compute CPU utilization tsc / ref-cycles

It would be useful to report all three metrics.

-Andi

author
#29 lore raw

RE: [PATCH 8/9] perf,tools: caculate and save tsc/avg/bzy freq in he_stat

Quoted 11 lines

Review

Quoted 9 lines
(quoted diff) +2 -0
+	if (sample->read.time_running > 0) {
+		freq.tsc_freq = (1000 * sample->tsc) / sample-

read.time_running;

(quoted diff) +1 -0
+		freq.avg_freq = (1000 * sample->aperf) / sample-

read.time_running;

(quoted diff) +2 -0
+		if (sample->aperf > 0)
+			freq.bzy_freq = freq.tsc_freq * sample->mperf /

sample->aperf;

Quoted 5 lines

Sorry didn't notice that earlier. The formula is not correct. aperf/mperf is not necessarily the frequency, it is essentially a load average of the CPU. It should be reported as such. Also only the ratio is architecturally defined.

The concept of tsc, avg and bzy are from turbostat. Here is the definition from turbostat readme.
- AVG_MHz = APERF_delta/measurement_interval. This is the actual number of elapsed cycles divided by the entire sample interval
- TSC_MHz = TSC_delta/measurement_interval.
On a system with an invariant TSC, this value will be constant and will closely match the base frequency value
- Bzy_MHz = TSC_delta/APERF_delta/MPERF_delta/measurement_interval

Only the Bzy_MHz is wrong and has a typo error.
Other formula should be correct.

If it's confusion, I will change the name and make it consistent as turbostat.

Quoted 2 lines

The right way to compute frequency is cycles / ref-cycles TSC can be used to accurately compute CPU utilization tsc / ref-cycles

I think I can add the support for frequency and CPU% calculation, and show them in --stdio.

Thanks,
Kan

Quoted 4 lines

It would be useful to report all three metrics.

-Andi

#30 lore raw

Re: [PATCH 8/9] perf,tools: caculate and save tsc/avg/bzy freq in he_stat

Quoted 2 lines

The concept of tsc, avg and bzy are from turbostat. Here is the definition from turbostat readme.

turbostat can do this because it is model specific, but perf user code is not. It has to be architectural definitions only. And the architectural definition of ASTATE MSTATE in the SDM is that it is only valid as a ratio.

Please use the formulas I described.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#31 lore raw

PATCH 9/9 perf,tools: Show freq in perf report --stdio

In reply to [email protected]

From: Kan Liang <[email protected]>

Show freq for each symbol in perf report by --stdio --show-freq

In sampling group, only group leader do sampling. So only need to print group leader's freq in --group.

Here is an example.

 $ perf report --stdio --group --show-freq

# Samples: 71K of event 'anon group { ref-cycles, core_misc/tsc/, core_misc/power-mperf/, core_misc/power-aperf/ }'
# Event count (approx.): 215265868412
#
# Overhead TSC MHz AVG MHz BZY MHz
Command Shared Object Symbol

 # ................................  .........  .........  .........
 ............  ................  ..................................
 #
     98.85%   5.41%  98.89%  98.95%       2293       1474       2302
 tchain_edit   tchain_edit       [.] f3
      0.39%   1.64%   0.39%   0.37%       2295          1       3053
 kworker/25:1  [kernel.vmlinux]  [k] delay_tsc
      0.08%  24.20%   0.07%   0.06%       2295         82       2746
 swapper       [kernel.vmlinux]  [k] acpi_idle_do_entry
      0.05%   0.00%   0.05%   0.05%       2295       2289       2295
 tchain_edit   tchain_edit       [.] f2
---
 tools/perf/Documentation/perf-report.txt | 10 +++++
 tools/perf/builtin-report.c              | 17 ++++++++
 tools/perf/perf.h                        |  1 +
 tools/perf/ui/hist.c                     | 69 +++++++++++++++++++++++++++++---
 tools/perf/util/hist.h                   |  3 ++
 tools/perf/util/session.c                |  2 +-
 tools/perf/util/sort.c                   |  3 ++
 tools/perf/util/symbol.h                 |  3 +-
 tools/perf/util/util.c                   |  4 ++
 9 files changed, 105 insertions(+), 7 deletions(-)
tools/perf/Documentation/perf-report.txt +10 -0
index c33b69f..fb82390 100644
@@ -303,6 +303,16 @@ OPTIONS
 	special event -e cpu/mem-loads/ or -e cpu/mem-stores/. See
 	'perf mem' for simpler access.
 
+--show-freq::
+	Show frequency result from sample read.
+	To generate the frequency output, the perf.data file must have been
+	obtained using perf record -a --running-time, using special events
+	-e core_misc/tsc/, core_misc/power-mperf/ or core_misc/power-aperf/,
+	and group read.
+	TSC MHz: average MHz that the TSC ran during the sample interval.
+	AVG MHz: number of cycles executed divided by time elapsed.
+	BZY MHz: average clock rate while the CPU was busy (in "c0" state).
+
 --percent-limit::
 	Do not show entries which have an overhead under that percent.
 	(Default: 0).
tools/perf/builtin-report.c +17 -0
index 95a4771..00e77e2 100644
@@ -164,6 +164,8 @@ static int process_sample_event(struct perf_tool *tool,
 		iter.ops = &hist_iter_mem;
 	else if (symbol_conf.cumulate_callchain)
 		iter.ops = &hist_iter_cumulative;
+	else if (symbol_conf.show_freq)
+		iter.ops = &hist_iter_freq;
 	else
 		iter.ops = &hist_iter_normal;
 
@@ -721,6 +723,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
 		    "Enable kernel symbol demangling"),
 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
+	OPT_BOOLEAN(0, "show-freq", &symbol_conf.show_freq, "freqency profile"),
 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
 		     "Don't show entries under that percent", parse_percent_limit),
 	OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
@@ -733,6 +736,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 	struct perf_data_file file = {
 		.mode  = PERF_DATA_MODE_READ,
 	};
+	struct perf_evsel *pos;
 	int ret = hists__init();
 
 	if (ret < 0)
@@ -818,6 +822,19 @@ repeat:
 		symbol_conf.cumulate_callchain = false;
 	}
 
+	if (symbol_conf.show_freq) {
+		evlist__for_each(session->evlist, pos) {
+			if (pos->attr.type == PERF_TYPE_CORE_MISC_FREE) {
+				if (pos->attr.config == PERF_POWER_APERF)
+					perf_aperf = true;
+				if (pos->attr.config == PERF_POWER_MPERF)
+					perf_mperf = true;
+				if (pos->attr.config == PERF_TSC)
+					perf_tsc = true;
+			}
+		}
+	}
+
 	if (setup_sorting() < 0) {
 		if (sort_order)
 			parse_options_usage(report_usage, options, "s", 1);
tools/perf/perf.h +1 -0
index 937b16a..54d248ec 100644
@@ -33,6 +33,7 @@ static inline unsigned long long rdclock(void)
 
 extern const char *input_name;
 extern bool perf_host, perf_guest;
+extern bool perf_tsc, perf_aperf, perf_mperf;
 extern const char perf_version_string[];
 
 void pthread__unblock_sigwinch(void);
tools/perf/ui/hist.c +64 -5

132 lines not on this page · raw message

tools/perf/util/hist.h +3 -0
index 3601658..71e2fa3 100644
@@ -237,6 +237,9 @@ enum {
 	PERF_HPP__OVERHEAD_ACC,
 	PERF_HPP__SAMPLES,
 	PERF_HPP__PERIOD,
+	PERF_HPP__TSC,
+	PERF_HPP__AVG,
+	PERF_HPP__BZY,
 
 	PERF_HPP__MAX_INDEX
 };
tools/perf/util/session.c +1 -1
index bffa58b..e14128a 100644
@@ -1026,7 +1026,7 @@ static int deliver_sample_value(struct perf_evlist *evlist,
 			    (++nr >= sample->read.group.nr))
 				break;
 
-			if (evsel->attr.type == PERF_TYPE_CORE_MISC_FREE) {
+			if (symbol_conf.show_freq && evsel->attr.type == PERF_TYPE_CORE_MISC_FREE) {
 				if (evsel->attr.config == PERF_POWER_APERF)
 					sample->aperf = sample->read.group.values[nr].value;
 				if (evsel->attr.config == PERF_POWER_MPERF)
tools/perf/util/sort.c +3 -0
index 4c65a14..f618fba 100644
@@ -1225,6 +1225,9 @@ static struct hpp_dimension hpp_sort_dimensions[] = {
 	DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
 	DIM(PERF_HPP__SAMPLES, "sample"),
 	DIM(PERF_HPP__PERIOD, "period"),
+	DIM(PERF_HPP__TSC, "tsc"),
+	DIM(PERF_HPP__AVG, "avg"),
+	DIM(PERF_HPP__BZY, "bzy"),
 };
 
 #undef DIM
tools/perf/util/symbol.h +2 -1
index b71d575..60a6f1a 100644
@@ -106,7 +106,8 @@ struct symbol_conf {
 			filter_relative,
 			show_hist_headers,
 			branch_callstack,
-			has_filter;
+			has_filter,
+			show_freq;
 	const char	*vmlinux_name,
 			*kallsyms_name,
 			*source_prefix,
tools/perf/util/util.c +4 -0
index edc2d63..c299af9 100644
@@ -34,6 +34,10 @@ bool test_attr__enabled;
 bool perf_host  = true;
 bool perf_guest = false;
 
+bool perf_tsc = false;
+bool perf_aperf = false;
+bool perf_mperf = false;
+
 char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
 
 void event_attr_init(struct perf_event_attr *attr)
Signature · 1 line
1.8.3.1
#32 lore raw
In reply to [email protected]
Quoted 7 lines · * [email protected] <[email protected]> wrote:

From: Kan Liang <[email protected]>

This patchkit intends to support Intel core misc PMUs. There are miscellaneous free running (read-only) counters in core. Some new PMUs called core misc PMUs are composed to include these counters. The counters include TSC, IA32_APERF, IA32_MPERF, IA32_PPERF, SMI_COUNT, CORE_C*_RESIDENCY and PKG_C*_RESIDENCY. There could be more in future platform.

Could you please do something like:

s/perf_event_intel_core_misc.c/perf_event_x86/

and in general propagate it to a core perf x86 position?

This feature is not Intel specific per se, although the initial MSRs you are supporting are Intel specific (and that is fine).

Thanks,

Ingo