[PATCH v2] perf mem: Fix printing PERF_MEM_LVLNUM_{L2_MHB|MSC}

Thomas Falcon posted 1 patch 2 months ago
tools/perf/util/mem-events.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
[PATCH v2] perf mem: Fix printing PERF_MEM_LVLNUM_{L2_MHB|MSC}
Posted by Thomas Falcon 2 months ago
With commit 8ec9497d3ef34 ("tools/include: Sync uapi/linux/perf.h
with the kernel sources"), 'perf mem report' gives an incorrect memory
access string.
...
0.02%	1	3644	L5 hit	[.] 0x0000000000009b0e	mlc	[.] 0x00007fce43f59480
...

This occurs because, if no entry exists in mem_lvlnum, perf_mem__lvl_scnprintf
will default to 'L%d, lvl', which in this case for PERF_MEM_LVLNUM_L2_MHB is 0x05.
Add entries for PERF_MEM_LVLNUM_L2_MHB and PERF_MEM_LVLNUM_MSC to mem_lvlnum,
so that the correct strings are printed.
...
0.02%	1	3644	L2 MHB hit	[.] 0x0000000000009b0e	mlc	[.] 0x00007fce43f59480
...

Fixes: 8ec9497d3ef34 ("tools/include: Sync uapi/linux/perf.h with the kernel sources")
Suggested-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Thomas Falcon <thomas.falcon@intel.com>
Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
---
v2: Leo Yan suggested adding PERF_MEM_LVLNUM_L{1-4} to mem_lvlnum
    and printing a clearer message in the case of an unknown level
    to more easily catch similar issues in the future
---
 tools/perf/util/mem-events.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/mem-events.c b/tools/perf/util/mem-events.c
index 051feb93ed8d..bf5090f5220b 100644
--- a/tools/perf/util/mem-events.c
+++ b/tools/perf/util/mem-events.c
@@ -366,6 +366,12 @@ static const char * const mem_lvl[] = {
 };
 
 static const char * const mem_lvlnum[] = {
+	[PERF_MEM_LVLNUM_L1] = "L1",
+	[PERF_MEM_LVLNUM_L2] = "L2",
+	[PERF_MEM_LVLNUM_L3] = "L3",
+	[PERF_MEM_LVLNUM_L4] = "L4",
+	[PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
+	[PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
 	[PERF_MEM_LVLNUM_UNC] = "Uncached",
 	[PERF_MEM_LVLNUM_CXL] = "CXL",
 	[PERF_MEM_LVLNUM_IO] = "I/O",
@@ -448,7 +454,7 @@ int perf_mem__lvl_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf
 		if (mem_lvlnum[lvl])
 			l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
 		else
-			l += scnprintf(out + l, sz - l, "L%d", lvl);
+			l += scnprintf(out + l, sz - l, "Unknown level %d", lvl);
 
 		l += scnprintf(out + l, sz - l, " %s", hit_miss);
 		return l;
-- 
2.46.0
Re: [PATCH v2] perf mem: Fix printing PERF_MEM_LVLNUM_{L2_MHB|MSC}
Posted by Namhyung Kim 2 months ago
On Thu, 26 Sep 2024 09:40:40 -0500, Thomas Falcon wrote:

> With commit 8ec9497d3ef34 ("tools/include: Sync uapi/linux/perf.h
> with the kernel sources"), 'perf mem report' gives an incorrect memory
> access string.
> ...
> 0.02%	1	3644	L5 hit	[.] 0x0000000000009b0e	mlc	[.] 0x00007fce43f59480
> ...
> 
> [...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung
Re: [PATCH v2] perf mem: Fix printing PERF_MEM_LVLNUM_{L2_MHB|MSC}
Posted by Leo Yan 2 months ago
On 9/26/2024 3:40 PM, Thomas Falcon wrote:
> 
> With commit 8ec9497d3ef34 ("tools/include: Sync uapi/linux/perf.h
> with the kernel sources"), 'perf mem report' gives an incorrect memory
> access string.
> ...
> 0.02%   1       3644    L5 hit  [.] 0x0000000000009b0e  mlc     [.] 0x00007fce43f59480
> ...
> 
> This occurs because, if no entry exists in mem_lvlnum, perf_mem__lvl_scnprintf
> will default to 'L%d, lvl', which in this case for PERF_MEM_LVLNUM_L2_MHB is 0x05.
> Add entries for PERF_MEM_LVLNUM_L2_MHB and PERF_MEM_LVLNUM_MSC to mem_lvlnum,
> so that the correct strings are printed.
> ...
> 0.02%   1       3644    L2 MHB hit      [.] 0x0000000000009b0e  mlc     [.] 0x00007fce43f59480
> ...
> 
> Fixes: 8ec9497d3ef34 ("tools/include: Sync uapi/linux/perf.h with the kernel sources")
> Suggested-by: Kan Liang <kan.liang@linux.intel.com>
> Signed-off-by: Thomas Falcon <thomas.falcon@intel.com>
> Reviewed-by: Kan Liang <kan.liang@linux.intel.com>

Reviewed-by: Leo Yan <leo.yan@arm.com>

> ---
> v2: Leo Yan suggested adding PERF_MEM_LVLNUM_L{1-4} to mem_lvlnum
>     and printing a clearer message in the case of an unknown level
>     to more easily catch similar issues in the future
> ---
>  tools/perf/util/mem-events.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/mem-events.c b/tools/perf/util/mem-events.c
> index 051feb93ed8d..bf5090f5220b 100644
> --- a/tools/perf/util/mem-events.c
> +++ b/tools/perf/util/mem-events.c
> @@ -366,6 +366,12 @@ static const char * const mem_lvl[] = {
>  };
> 
>  static const char * const mem_lvlnum[] = {
> +       [PERF_MEM_LVLNUM_L1] = "L1",
> +       [PERF_MEM_LVLNUM_L2] = "L2",
> +       [PERF_MEM_LVLNUM_L3] = "L3",
> +       [PERF_MEM_LVLNUM_L4] = "L4",
> +       [PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
> +       [PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
>         [PERF_MEM_LVLNUM_UNC] = "Uncached",
>         [PERF_MEM_LVLNUM_CXL] = "CXL",
>         [PERF_MEM_LVLNUM_IO] = "I/O",
> @@ -448,7 +454,7 @@ int perf_mem__lvl_scnprintf(char *out, size_t sz, const struct mem_info *mem_inf
>                 if (mem_lvlnum[lvl])
>                         l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
>                 else
> -                       l += scnprintf(out + l, sz - l, "L%d", lvl);
> +                       l += scnprintf(out + l, sz - l, "Unknown level %d", lvl);
> 
>                 l += scnprintf(out + l, sz - l, " %s", hit_miss);
>                 return l;
> --
> 2.46.0
>