Skip to contentSkip to conversation

perf pmu: add loaded_json_aliases firstly for perf_pmu__num_events()

PATCH In review Xu Yang 2 participants 5 messages Oct 10, 2023 – Nov 6, 2023 perf events

lore
Share & tools
lore

Xu Yang

author
#0 lore raw

PATCH 1/1 perf pmu: add loaded_json_aliases firstly for perf_pmu__num_events()

After pmu_read_sysfs(), event aliases in json may be loaded for non-core pmu. And pmu->event_table is NULL for non-core pmu.
If perf_pmu__num_events() didn't add loaded_json_aliases to the total number, perf list will show below error:

Unexpected event imx8_ddr0/imx8_ddr0/write-command//
...

This is because perf_pmu__num_events() didn't count correct events number. This will allow perf_pmu__num_events() to get corrent events number.

---
 tools/perf/util/pmu.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/pmu.c +1 -0
index 8de6f39abd1b..920ab1c2e93b 100644
@@ -1609,6 +1609,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
 		pmu_aliases_parse(pmu);
 
 	nr = pmu->sysfs_aliases;
+	nr += pmu->loaded_json_aliases;
 
 	if (pmu->cpu_aliases_added)
 		 nr += pmu->loaded_json_aliases;
Signature · 1 line
2.34.1
author
#1 lore raw
Quoted 30 lines

After pmu_read_sysfs(), event aliases in json may be loaded for non-core pmu. And pmu->event_table is NULL for non-core pmu.
If perf_pmu__num_events() didn't add loaded_json_aliases to the total number, perf list will show below error:

Unexpected event imx8_ddr0/imx8_ddr0/write-command//
...

This is because perf_pmu__num_events() didn't count correct events number. This will allow perf_pmu__num_events() to get corrent events number.

---
 tools/perf/util/pmu.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/pmu.c +1 -1
index 8de6f39abd1b..920ab1c2e93b 100644
@@ -1609,6 +1609,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
 		pmu_aliases_parse(pmu);
 
 	nr = pmu->sysfs_aliases;
+	nr += pmu->loaded_json_aliases;
 
 	if (pmu->cpu_aliases_added)
 		 nr += pmu->loaded_json_aliases;
--
Quoted 1 line

2.34.1

A gentle ping.

reviewer
#2 lore raw
Quoted 7 lines · Xu Yang
Quoted 7 lines

After pmu_read_sysfs(), event aliases in json may be loaded for non-core pmu. And pmu->event_table is NULL for non-core pmu.
If perf_pmu__num_events() didn't add loaded_json_aliases to the total number, perf list will show below error:

Quoted 7 lines
Quoted 7 lines

Unexpected event imx8_ddr0/imx8_ddr0/write-command//
...

This is because perf_pmu__num_events() didn't count correct events number. This will allow perf_pmu__num_events() to get corrent events number.

Hi Xu,

I'm not sure how but this email fails my email filters, I also wasn't able to update my filters (gmail) to capture this email. Patches should be sent to [email protected] as well as the linux-perf-user list. The scripts/get_maintainer.pl will show you who to send the patch to.

Quoted 11 lines
Quoted 11 lines
---
 tools/perf/util/pmu.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/pmu.c +0 -0
index 8de6f39abd1b..920ab1c2e93b 100644
@@ -1609,6 +1609,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
              pmu_aliases_parse(pmu);
 
Quoted 5 lines

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1615

Quoted 5 lines
tools/perf/util/pmu.c +1 -0
      nr = pmu->sysfs_aliases;
+     nr += pmu->loaded_json_aliases;
 
      if (pmu->cpu_aliases_added)
               nr += pmu->loaded_json_aliases;

Given the count is added here, I'm not sure why you think adding it twice makes sense?

Hopefully the code in perf_pmu__num_events is fairly self explanatory. But I'll break it down:

if (!pmu->sysfs_aliases_loaded)

    pmu_aliases_parse(pmu);

nr = pmu->sysfs_aliases;

Rather than parse sysfs for the number of events, they are just loaded and then the count used. This is simplest because sysfs and json may contain the same event.

if (pmu->cpu_aliases_added)

    nr += pmu->loaded_json_aliases;

Were all the json events loaded? If so, add those on to the count.

else if (pmu->events_table)

    nr += pmu_events_table__num_events(pmu->events_table, pmu) -

pmu->loaded_json_aliases;

We haven't loaded the json events so go get a count of them. The number of loaded_json_aliases is subtracted as events in the table may match ones in sysfs, in which case the sysfs event is updated with json information (see update_alias possibly called by perf_pmu__new_alias). We don't want to double count the event, hence the subtract.

return pmu->selectable ? nr + 1 : nr;

Return the count, +1 if the PMU is selectable without an event such as intel_pt//

For me to understand your problem it would be useful to have the cpuid information for your processor (from this I can find the json) plus the contents of the /sys/devices directory for the PMU. So I can see what kind of situation we have. You may be able to create a reproduction so I can test, see:

tools/perf/pmu-events/arch/test/
tools/perf/tests/pmu-events.c

Thanks,
Ian

Quoted 4 lines
Quoted 2 lines

--
2.34.1

A gentle ping.

author
#3 lore raw

Hi Lan,

Quoted 39 lines · Xu Yang
Quoted 7 lines
Quoted 7 lines

After pmu_read_sysfs(), event aliases in json may be loaded for non-core pmu. And pmu->event_table is NULL for non-core pmu.
If perf_pmu__num_events() didn't add loaded_json_aliases to the total number, perf list will show below error:

Quoted 7 lines
Quoted 7 lines

Unexpected event imx8_ddr0/imx8_ddr0/write-command//
...

This is because perf_pmu__num_events() didn't count correct events number. This will allow perf_pmu__num_events() to get corrent events number.

Hi Xu,

I'm not sure how but this email fails my email filters, I also wasn't able to update my filters (gmail) to capture this email. Patches should be sent to [email protected] as well as the linux-perf-user list. The scripts/get_maintainer.pl will show you who to send the patch to.

Quoted 14 lines
Quoted 14 lines
---
 tools/perf/util/pmu.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/pmu.c +1 -0
index 8de6f39abd1b..920ab1c2e93b 100644
@@ -1609,6 +1609,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
              pmu_aliases_parse(pmu);
 
      nr = pmu->sysfs_aliases;
+     nr += pmu->loaded_json_aliases;
 
Quoted 8 lines
Quoted 2 lines

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1615

Quoted 2 lines
tools/perf/util/pmu.c +0 -0
      if (pmu->cpu_aliases_added)
               nr += pmu->loaded_json_aliases;

Given the count is added here, I'm not sure why you think adding it twice makes sense?

Hopefully the code in perf_pmu__num_events is fairly self explanatory. But I'll break it down:

Many thanks for your detailed analysis.

Quoted 16 lines

if (!pmu->sysfs_aliases_loaded)

    pmu_aliases_parse(pmu);

nr = pmu->sysfs_aliases;

Rather than parse sysfs for the number of events, they are just loaded and then the count used. This is simplest because sysfs and json may contain the same event.

if (pmu->cpu_aliases_added)

    nr += pmu->loaded_json_aliases;

Were all the json events loaded? If so, add those on to the count.

else if (pmu->events_table)

    nr += pmu_events_table__num_events(pmu->events_table, pmu) -
Quoted 8 lines

pmu->loaded_json_aliases;

We haven't loaded the json events so go get a count of them. The number of loaded_json_aliases is subtracted as events in the table may match ones in sysfs, in which case the sysfs event is updated with json information (see update_alias possibly called by perf_pmu__new_alias). We don't want to double count the event, hence the subtract.

So if /sys/bus/event_source/devices/imx8_ddr0/ events/write and imx8mp_ddr.write in pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json
is describing the same event=0x38, perf will only list one entry for this alias, right?

I assume below code only works for core pmu, because pmu->cpu_aliases_added is false and pmu->events_table is NULL all the time for non-core pmu, right? If so, nr will never contain json_aliases for non-core pmu.

if (pmu->cpu_aliases_added)

	nr += pmu->loaded_json_aliases;
else if (pmu->events_table)
	nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
Quoted 6 lines

return pmu->selectable ? nr + 1 : nr;

Return the count, +1 if the PMU is selectable without an event such as intel_pt//

Quoted 9 lines

For me to understand your problem it would be useful to have the cpuid information for your processor (from this I can find the json) plus the contents of the /sys/devices directory for the PMU. So I can see what kind of situation we have. You may be able to create a reproduction so I can test, see:

tools/perf/pmu-events/arch/test/
tools/perf/tests/pmu-events.c

Our pmu is inside ddr controller and mainly used for monitoring read/write events. Therefore it's a non-core pmu.
To describe the issue, I'll show some basic information below:

root@imx8mpevk: /sys/bus/event_source/devices/imx8_ddr0# ls
caps cpumask events format identifier perf_event_mux_interval_ms power subsystem type uevent

root@imx8mpevk: /sys/bus/event_source/devices/imx8_ddr0# ls events
events/write event=0x38
events/read-accesses event=0x04
events/cycles event=0x00
events/axid-write event=0x42
events/read-cycles event=0x2a
events/lp-req-nocredit event=0x26
events/write-credit-cnt event=0x12
events/read-write-transition event=0x30
events/selfresh event=0x01
events/hp-xact-credit event=0x25
events/raw-hazard event=0x39
events/hp-req-nocredit event=0x24
events/lp-read-credit-cnt event=0x10
events/axid-read event=0x41
events/read-queue-depth event=0x08
events/hp-read-credit-cnt event=0x11
events/write-cycles event=0x2b
events/activate event=0x32
events/read-activate event=0x36
events/write-accesses event=0x05
events/refresh event=0x37
events/wr-xact-credit event=0x29
events/read-command event=0x20
events/lp-xact-credit event=0x27
events/write-queue-depth event=0x09
events/perf-mwr event=0x34
events/precharge event=0x31
events/write-command event=0x21
events/hp-read event=0x23
events/load-mode event=0x33
events/read event=0x35
events/read-modify-write-command event=0x22

Total 32 events under /sys/bus/event_source/devices/imx8_ddr0/events

# cat tools/perf/pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json
[
   {
           "BriefDescription": "ddr cycles event",
           "EventCode": "0x00",
           "EventName": "imx8mp_ddr.cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr read-cycles event",
           "EventCode": "0x2a",
           "EventName": "imx8mp_ddr.read_cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr write-cycles event",
           "EventCode": "0x2b",
           "EventName": "imx8mp_ddr.write_cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr read event",
           "EventCode": "0x35",
           "EventName": "imx8mp_ddr.read",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr write event",
           "EventCode": "0x38",
           "EventName": "imx8mp_ddr.write",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   }
]

Total 5 aliases in pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json file.

When run perf list command, perf will parse all pmus under /sys/bus/event_source/devices/.

We can assume there is only one imx8_ddr0 pmu.

# ls /sys/bus/event_source/devices/
imx8_ddr0

For our imx8_ddr0 pmu:
perf_pmu__lookup()

	pmu->is_core = is_pmu_core(name);   	 // is_core is false
	pmu->is_uncore = pmu_is_uncore(dirfd, name); 		 // is_uncore is true
	pmu->events_table = perf_pmu__find_events_table(pmu); 	// events_table is NULL
	pmu_add_sys_aliases(pmu);  		// will add 5 aliases to pmu->aliases list

After all pmus are parsed, perf will count total events for all pmus.

For our imx8_ddr0 pmu:
perf_pmu__num_events()

	if (!pmu->sysfs_aliases_loaded)		// sysfs_aliases_loaded is false
		pmu_aliases_parse(pmu);	// will 32 aliased to pmu->aliases list
						// pmu->aliases has 37 aliases now

	nr = pmu->sysfs_aliases;		// nr = 32
	
	if (pmu->cpu_aliases_added)		// cpu_aliases_added is false
		nr += pmu->loaded_json_aliases;
	else if (pmu->events_table)		// events_table is NULL
		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
	
	return pmu->selectable ? nr + 1 : nr;	// selectable is false, so return 32

aliases = zalloc(sizeof(struct sevent) * len); // len = 32
state = (struct events_callback_state) {
.aliases = aliases,
.aliases_len = len,
.index = 0,
};

Then perf will parse cpu aliases for each pmu. For our imx8_ddr0 pmu, the number of cpu alisaes is 0. Then perf will fill events info to aliases from pmu->aliases.

list_for_each_entry(event, &pmu->aliases, list)

	perf_pmus__print_pmu_events__callback (state, &info);
    ---->		// Since pmu->aliases has 37 aliases, but aliases_len is 32. Perf will show error.
		if (state->index >= state->aliases_len)
			pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);

Thanks,
Xu Yang

author
#4 lore raw

Hi Lan,

Quoted 26 lines

Hi Lan,

Quoted 23 lines · Xu Yang
Quoted 7 lines
Quoted 7 lines

After pmu_read_sysfs(), event aliases in json may be loaded for non-core pmu. And pmu->event_table is NULL for non-core pmu.
If perf_pmu__num_events() didn't add loaded_json_aliases to the total number, perf list will show below error:

Quoted 7 lines
Quoted 7 lines

Unexpected event imx8_ddr0/imx8_ddr0/write-command//
...

This is because perf_pmu__num_events() didn't count correct events number. This will allow perf_pmu__num_events() to get corrent events number.

Hi Xu,

I'm not sure how but this email fails my email filters, I also wasn't able to update my filters (gmail) to capture this email. Patches should be sent to [email protected] as well as the linux-perf-user list. The scripts/get_maintainer.pl will show you who to send the patch to.

Just checking to see if this email was missed too.

Quoted 213 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

---
 tools/perf/util/pmu.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/pmu.c +1 -0
index 8de6f39abd1b..920ab1c2e93b 100644
@@ -1609,6 +1609,7 @@ size_t perf_pmu__num_events(struct perf_pmu *pmu)
              pmu_aliases_parse(pmu);
 
      nr = pmu->sysfs_aliases;
+     nr += pmu->loaded_json_aliases;
 
Quoted 8 lines
Quoted 2 lines

tools/perf/util/pmu.c size_t perf_pmu__num_events(struct perf_pmu *pmu) · line 1615

Quoted 2 lines
tools/perf/util/pmu.c +0 -0
      if (pmu->cpu_aliases_added)
               nr += pmu->loaded_json_aliases;

Given the count is added here, I'm not sure why you think adding it twice makes sense?

Hopefully the code in perf_pmu__num_events is fairly self explanatory. But I'll break it down:

Many thanks for your detailed analysis.

Quoted 16 lines

if (!pmu->sysfs_aliases_loaded)

    pmu_aliases_parse(pmu);

nr = pmu->sysfs_aliases;

Rather than parse sysfs for the number of events, they are just loaded and then the count used. This is simplest because sysfs and json may contain the same event.

if (pmu->cpu_aliases_added)

    nr += pmu->loaded_json_aliases;

Were all the json events loaded? If so, add those on to the count.

else if (pmu->events_table)

    nr += pmu_events_table__num_events(pmu->events_table, pmu) -
Quoted 8 lines

pmu->loaded_json_aliases;

We haven't loaded the json events so go get a count of them. The number of loaded_json_aliases is subtracted as events in the table may match ones in sysfs, in which case the sysfs event is updated with json information (see update_alias possibly called by perf_pmu__new_alias). We don't want to double count the event, hence the subtract.

So if /sys/bus/event_source/devices/imx8_ddr0/ events/write and imx8mp_ddr.write in pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json
is describing the same event=0x38, perf will only list one entry for this alias, right?

I assume below code only works for core pmu, because pmu->cpu_aliases_added is false and pmu->events_table is NULL all the time for non-core pmu, right? If so, nr will never contain json_aliases for non-core pmu.

if (pmu->cpu_aliases_added)

	nr += pmu->loaded_json_aliases;
else if (pmu->events_table)
	nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;
Quoted 6 lines

return pmu->selectable ? nr + 1 : nr;

Return the count, +1 if the PMU is selectable without an event such as intel_pt//

Quoted 9 lines

For me to understand your problem it would be useful to have the cpuid information for your processor (from this I can find the json) plus the contents of the /sys/devices directory for the PMU. So I can see what kind of situation we have. You may be able to create a reproduction so I can test, see:

tools/perf/pmu-events/arch/test/
tools/perf/tests/pmu-events.c

Our pmu is inside ddr controller and mainly used for monitoring read/write events. Therefore it's a non-core pmu.
To describe the issue, I'll show some basic information below:

root@imx8mpevk: /sys/bus/event_source/devices/imx8_ddr0# ls
caps cpumask events format identifier perf_event_mux_interval_ms power subsystem type uevent

root@imx8mpevk: /sys/bus/event_source/devices/imx8_ddr0# ls events
events/write event=0x38
events/read-accesses event=0x04
events/cycles event=0x00
events/axid-write event=0x42
events/read-cycles event=0x2a
events/lp-req-nocredit event=0x26
events/write-credit-cnt event=0x12
events/read-write-transition event=0x30
events/selfresh event=0x01
events/hp-xact-credit event=0x25
events/raw-hazard event=0x39
events/hp-req-nocredit event=0x24
events/lp-read-credit-cnt event=0x10
events/axid-read event=0x41
events/read-queue-depth event=0x08
events/hp-read-credit-cnt event=0x11
events/write-cycles event=0x2b
events/activate event=0x32
events/read-activate event=0x36
events/write-accesses event=0x05
events/refresh event=0x37
events/wr-xact-credit event=0x29
events/read-command event=0x20
events/lp-xact-credit event=0x27
events/write-queue-depth event=0x09
events/perf-mwr event=0x34
events/precharge event=0x31
events/write-command event=0x21
events/hp-read event=0x23
events/load-mode event=0x33
events/read event=0x35
events/read-modify-write-command event=0x22

Total 32 events under /sys/bus/event_source/devices/imx8_ddr0/events

# cat tools/perf/pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json
[
   {
           "BriefDescription": "ddr cycles event",
           "EventCode": "0x00",
           "EventName": "imx8mp_ddr.cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr read-cycles event",
           "EventCode": "0x2a",
           "EventName": "imx8mp_ddr.read_cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr write-cycles event",
           "EventCode": "0x2b",
           "EventName": "imx8mp_ddr.write_cycles",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr read event",
           "EventCode": "0x35",
           "EventName": "imx8mp_ddr.read",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   },
   {
           "BriefDescription": "ddr write event",
           "EventCode": "0x38",
           "EventName": "imx8mp_ddr.write",
           "Unit": "imx8_ddr",
           "Compat": "i.MX8MP"
   }
]

Total 5 aliases in pmu-events/arch/arm64/freescale/imx8mp/sys/ddrc.json file.

When run perf list command, perf will parse all pmus under /sys/bus/event_source/devices/.

We can assume there is only one imx8_ddr0 pmu.

# ls /sys/bus/event_source/devices/
imx8_ddr0

For our imx8_ddr0 pmu:
perf_pmu__lookup()

	pmu->is_core = is_pmu_core(name);   	 // is_core is false
	pmu->is_uncore = pmu_is_uncore(dirfd, name); 		 // is_uncore is true
	pmu->events_table = perf_pmu__find_events_table(pmu); 	// events_table is NULL
	pmu_add_sys_aliases(pmu);  		// will add 5 aliases to pmu->aliases list

After all pmus are parsed, perf will count total events for all pmus.

For our imx8_ddr0 pmu:
perf_pmu__num_events()

	if (!pmu->sysfs_aliases_loaded)		// sysfs_aliases_loaded is false
		pmu_aliases_parse(pmu);	// will 32 aliased to pmu->aliases list
						// pmu->aliases has 37 aliases now

	nr = pmu->sysfs_aliases;		// nr = 32

	if (pmu->cpu_aliases_added)		// cpu_aliases_added is false
		nr += pmu->loaded_json_aliases;
	else if (pmu->events_table)		// events_table is NULL
		nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases;

	return pmu->selectable ? nr + 1 : nr;	// selectable is false, so return 32

aliases = zalloc(sizeof(struct sevent) * len); // len = 32
state = (struct events_callback_state) {
.aliases = aliases,
.aliases_len = len,
.index = 0,
};

Then perf will parse cpu aliases for each pmu. For our imx8_ddr0 pmu, the number of cpu alisaes is 0. Then perf will fill events info to aliases from pmu->aliases.

list_for_each_entry(event, &pmu->aliases, list)

	perf_pmus__print_pmu_events__callback (state, &info);
    ---->		// Since pmu->aliases has 37 aliases, but aliases_len is 32. Perf will show error.
		if (state->index >= state->aliases_len)
			pr_err("Unexpected event %s/%s/\n", info->pmu->name, info->name);

Thanks,
Xu Yang