[PATCH v8 25/52] perf jevents: Add ILP metrics for AMD

Ian Rogers posted 52 patches 3 weeks, 4 days ago
There is a newer version of this series
[PATCH v8 25/52] perf jevents: Add ILP metrics for AMD
Posted by Ian Rogers 3 weeks, 4 days ago
Use the counter mask (cmask) to see how many cycles an instruction
takes to retire. Present as a set of ILP metrics.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/pmu-events/amd_metrics.py | 37 ++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/tools/perf/pmu-events/amd_metrics.py b/tools/perf/pmu-events/amd_metrics.py
index 53f7c2198147..9a39e23075b8 100755
--- a/tools/perf/pmu-events/amd_metrics.py
+++ b/tools/perf/pmu-events/amd_metrics.py
@@ -121,6 +121,42 @@ def AmdBr():
                        description="breakdown of retired branch instructions")
 
 
+def AmdIlp() -> MetricGroup:
+    tsc = Event("msr/tsc/")
+    c0 = Event("msr/mperf/")
+    low = tsc - c0
+    inst_ret = Event("ex_ret_instr")
+    inst_ret_c = [Event(f"{inst_ret.name}/cmask={x}/") for x in range(1, 6)]
+    ilp = [d_ratio(max(inst_ret_c[x] - inst_ret_c[x + 1], 0), cycles)
+           for x in range(0, 4)]
+    ilp.append(d_ratio(inst_ret_c[4], cycles))
+    ilp0 = 1
+    for x in ilp:
+        ilp0 -= x
+    return MetricGroup("lpm_ilp", [
+        Metric("lpm_ilp_idle", "Lower power cycles as a percentage of all cycles",
+               d_ratio(low, tsc), "100%"),
+        Metric("lpm_ilp_inst_ret_0",
+               "Instructions retired in 0 cycles as a percentage of all cycles",
+               ilp0, "100%"),
+        Metric("lpm_ilp_inst_ret_1",
+               "Instructions retired in 1 cycles as a percentage of all cycles",
+               ilp[0], "100%"),
+        Metric("lpm_ilp_inst_ret_2",
+               "Instructions retired in 2 cycles as a percentage of all cycles",
+               ilp[1], "100%"),
+        Metric("lpm_ilp_inst_ret_3",
+               "Instructions retired in 3 cycles as a percentage of all cycles",
+               ilp[2], "100%"),
+        Metric("lpm_ilp_inst_ret_4",
+               "Instructions retired in 4 cycles as a percentage of all cycles",
+               ilp[3], "100%"),
+        Metric("lpm_ilp_inst_ret_5",
+               "Instructions retired in 5 or more cycles as a percentage of all cycles",
+               ilp[4], "100%"),
+    ])
+
+
 def AmdDtlb() -> Optional[MetricGroup]:
     global _zen_model
     if _zen_model >= 4:
@@ -600,6 +636,7 @@ def main() -> None:
 
     all_metrics = MetricGroup("", [
         AmdBr(),
+        AmdIlp(),
         AmdDtlb(),
         AmdItlb(),
         AmdLdSt(),
-- 
2.51.2.1041.gc1ab5b90ca-goog
Re: [PATCH v8 25/52] perf jevents: Add ILP metrics for AMD
Posted by Sandipan Das 1 week, 5 days ago
On 11/13/2025 8:50 AM, Ian Rogers wrote:
> Use the counter mask (cmask) to see how many cycles an instruction
> takes to retire. Present as a set of ILP metrics.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/pmu-events/amd_metrics.py | 37 ++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
> 
> diff --git a/tools/perf/pmu-events/amd_metrics.py b/tools/perf/pmu-events/amd_metrics.py
> index 53f7c2198147..9a39e23075b8 100755
> --- a/tools/perf/pmu-events/amd_metrics.py
> +++ b/tools/perf/pmu-events/amd_metrics.py
> @@ -121,6 +121,42 @@ def AmdBr():
>                         description="breakdown of retired branch instructions")
>  
>  
> +def AmdIlp() -> MetricGroup:
> +    tsc = Event("msr/tsc/")
> +    c0 = Event("msr/mperf/")
> +    low = tsc - c0
> +    inst_ret = Event("ex_ret_instr")
> +    inst_ret_c = [Event(f"{inst_ret.name}/cmask={x}/") for x in range(1, 6)]
> +    ilp = [d_ratio(max(inst_ret_c[x] - inst_ret_c[x + 1], 0), cycles)
> +           for x in range(0, 4)]
> +    ilp.append(d_ratio(inst_ret_c[4], cycles))

According to AMD PPRs, "the corresponding PERF_CTR[5:0] register increments by 1,
if the number of events occurring in a clock cycle is greater than or equal to
the CntMask value".

Please correct me if I am getting this wrong but I am interpreting the values above as

inst_ret_c[0] = cycles in which at least 1 instruction retired
inst_ret_c[1] = cycles in which at least 2 instructions retired
...
inst_ret_c[5] = cycles in which at least 6 instructions retired

ilp[0] = fraction of cycles in which exactly 1 instruction retired
ilp[1] = fraction of cycles in which exactly 2 instructions retired
...
ilp[4] = fraction of cycles in which exactly 5 instructions retired
ilp[5] = fraction of cycles in which at least 5 instructions retired

> +    ilp0 = 1
> +    for x in ilp:
> +        ilp0 -= x
> +    return MetricGroup("lpm_ilp", [
> +        Metric("lpm_ilp_idle", "Lower power cycles as a percentage of all cycles",
> +               d_ratio(low, tsc), "100%"),
> +        Metric("lpm_ilp_inst_ret_0",
> +               "Instructions retired in 0 cycles as a percentage of all cycles",
> +               ilp0, "100%"),

If the interpretation above is correct, ilp0 (fraction of cycles in which no instructions
retired) can be computed as 1 - (inst_ret_c[0] / cycles) as inst_ret_c[0] is the count of
cycles in which at least 1 instruction retired.

> +        Metric("lpm_ilp_inst_ret_1",
> +               "Instructions retired in 1 cycles as a percentage of all cycles",
> +               ilp[0], "100%"),
> +        Metric("lpm_ilp_inst_ret_2",
> +               "Instructions retired in 2 cycles as a percentage of all cycles",
> +               ilp[1], "100%"),
> +        Metric("lpm_ilp_inst_ret_3",
> +               "Instructions retired in 3 cycles as a percentage of all cycles",
> +               ilp[2], "100%"),
> +        Metric("lpm_ilp_inst_ret_4",
> +               "Instructions retired in 4 cycles as a percentage of all cycles",
> +               ilp[3], "100%"),
> +        Metric("lpm_ilp_inst_ret_5",
> +               "Instructions retired in 5 or more cycles as a percentage of all cycles",
> +               ilp[4], "100%"),

ilp[5] corresponds to "5 or more".

> +    ])
> +
> +
>  def AmdDtlb() -> Optional[MetricGroup]:
>      global _zen_model
>      if _zen_model >= 4:
> @@ -600,6 +636,7 @@ def main() -> None:
>  
>      all_metrics = MetricGroup("", [
>          AmdBr(),
> +        AmdIlp(),
>          AmdDtlb(),
>          AmdItlb(),
>          AmdLdSt(),