在没有CPU_CLK_UNHALTED.* 的 PEBS 的 SandyBridge 上有支持 cycles:p 的 hack。 hack 在intel_pebs_aliases_snb() 的perf 的内核部分中实现。当用户使用非零 precise 修饰符请求 -e cycles 即 PERF_COUNT_HW_CPU_CYCLES(转换为 CPU_CLK_UNHALTED.CORE)时,此函数将使用 PEBS 将硬件事件更改为 UOPS_RETIRED.ALL:
29 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
2739 static void intel_pebs_aliases_snb(struct perf_event *event)
2740 {
2741 if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
2742 /*
2743 * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
2744 * (0x003c) so that we can use it with PEBS.
2745 *
2746 * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
2747 * PEBS capable. However we can use UOPS_RETIRED.ALL
2748 * (0x01c2), which is a PEBS capable event, to get the same
2749 * count.
2750 *
2751 * UOPS_RETIRED.ALL counts the number of cycles that retires
2752 * CNTMASK micro-ops. By setting CNTMASK to a value (16)
2753 * larger than the maximum number of micro-ops that can be
2754 * retired per cycle (4) and then inverting the condition, we
2755 * count all cycles that retire 16 or less micro-ops, which
2756 * is every cycle.
2757 *
2758 * Thereby we gain a PEBS capable cycle counter.
2759 */
2760 u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
2761
2762 alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
2763 event->hw.config = alt_config;
2764 }
2765 }
intel_pebs_aliases_snb hack 在3557 __init int intel_pmu_init(void) 中注册为case INTEL_FAM6_SANDYBRIDGE: / case INTEL_FAM6_SANDYBRIDGE_X:
3772 x86_pmu.event_constraints = intel_snb_event_constraints;
3773 x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
3774 x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
当precise_ip 设置为非零时,从intel_pmu_hw_config() 调用pebs_aliases:
2814 static int intel_pmu_hw_config(struct perf_event *event)
2815 {
2821 if (event->attr.precise_ip) {
2828 if (x86_pmu.pebs_aliases)
2829 x86_pmu.pebs_aliases(event);
2830 }
该 hack 于 2012 年实施,lkml 线程“[PATCH] perf, x86: Make cycles:p working on SNB”, “[tip:perf/core] perf/x86: Implement cycles:p for SNB/IVB” , cccb9ba9e4ee0d750265f53de9258df69655c40b, http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=cccb9ba9e4ee0d750265f53de9258df69655c40b:
perf/x86:实施周期:p 用于 SNB/IVB
现在终于有了可以工作的 PEBS (IvyBridge) 芯片,我们可以
启用硬件并实现周期:p 用于 SNB/IVB。
而且我认为,除了arch/x86/events/intel/core.c 中的 linux 源代码、static void intel_pebs_aliases 的 grep (通常实现 cycles:p / CPU_CLK_UNHALTED 0x003c)并检查 @987654349 之外,没有这种“精确”转换 hack 的完整列表@ 用于实际模型和精确的x86_pmu.pebs_aliases 选择的变体:
- intel_pebs_aliases_core2,
INST_RETIRED.ANY_P (0x00c0) CNTMASK=16 而不是 cycles:p
- intel_pebs_aliases_snb,
UOPS_RETIRED.ALL (0x01c2) CNTMASK=16 而不是 cycles:p
- intel_pebs_aliases_precdist 的最高值
precise_ip、INST_RETIRED.PREC_DIST (0x01c0) 而不是 SKL、IVB、HSW、BDW 上的 cycles:ppp