Skip to contentSkip to conversation

Fix session topology test for powerpc and add utility function to get cpuinfo entries

PATCH Superseded Athira Rajeev 3 participants 6 messages Apr 28, 2022 – May 4, 2022 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix session topology test for powerpc and add utility function to get cpuinfo entries

The session topology test fails in powerpc pSeries platform. Test logs:
<<>>
Session topology : FAILED!
<<>>

This test uses cpu topology information and in powerpc, some of the topology info is restricted in environment like virtualized platform. Hence this test needs to be skipped in pSeries platform for powerpc. The information about platform is available in /proc/cpuinfo.

Patch 1 adds generic utility function in "util/header.c" to read /proc/cpuinfo for any entry. Though the testcase fix needs value from "platform" entry, making this as a generic function to return value for any entry from the /proc/cpuinfo file which can be used commonly in future usecases.

Patch 2 uses the newly added utility function to look for platform and skip the test in pSeries platform for powerpc.

Athira Rajeev (2):
tools/perf: Add utility function to read /proc/cpuinfo for any field tools/perf/tests: Fix session topology test to skip the test in guest
environment

 tools/perf/tests/topology.c | 17 ++++++++++++
 tools/perf/util/header.c    | 54 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/header.h    |  1 +
 3 files changed, 72 insertions(+)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/2 tools/perf: Add utility function to read /proc/cpuinfo for any field

/proc/cpuinfo provides information about type of processor, number of CPU's etc. Reading /proc/cpuinfo file outputs useful information by field name like cpu, platform, model (depending on architecture) and its value separated by colon.

Add new utility function "cpuinfo_field" in "util/header.c" which accepts field name as input string to search in /proc/cpuinfo content. This returns the first matching value as resulting string. Example, calling the function "cpuinfo_field(platform)" in powerpc returns the platform value. This can be used to fetch processor information from "cpuinfo" by other utilities/testcases.

---
 tools/perf/util/header.c | 54 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/header.h |  1 +
 2 files changed, 55 insertions(+)
tools/perf/util/header.c +54 -0
index a27132e5a5ef..0c8dfd0c1e78 100644
@@ -983,6 +983,60 @@ static int write_dir_format(struct feat_fd *ff,
 	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
 }
 
+/*
+ * Return entry from /proc/cpuinfo
+ * indicated by "search" parameter.
+ */
+char *cpuinfo_field(const char *search)
+{
+	FILE *file;
+	char *buf = NULL;
+	char *copy_buf = NULL, *p;
+	size_t len = 0;
+	int ret = -1;
+
+	if (!search)
+		return NULL;
+
+	file = fopen("/proc/cpuinfo", "r");
+	if (!file)
+		return NULL;
+
+	while (getline(&buf, &len, file) > 0) {
+		ret = strncmp(buf, search, strlen(search));
+		if (!ret)
+			break;
+	}
+
+	if (ret)
+		goto done;
+
+	/*
+	 * Trim the new line and separate
+	 * value for search field from ":"
+	 * in cpuinfo line output.
+	 * Example output line:
+	 * platform : <value>
+	 */
+	copy_buf = buf;
+	p = strchr(copy_buf, ':');
+	if (p && *(p+1) == ' ' && *(p+2))
+		copy_buf = p + 2;
+	p = strchr(copy_buf, '\n');
+	if (p)
+		*p = '\0';
+
+	/* Copy the filtered string to buf */
+	strcpy(buf, copy_buf);
+
+	fclose(file);
+	return buf;
+
+done:
+	free(buf);
+	fclose(file);
+	return NULL;
+}
 /*
  * Check whether a CPU is online
  *
tools/perf/util/header.h +1 -0
index 0eb4bc29a5a4..b0f754364bd4 100644
@@ -166,4 +166,5 @@ int get_cpuid(char *buffer, size_t sz);
 
 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
 int strcmp_cpuid_str(const char *s1, const char *s2);
+char *cpuinfo_field(const char *search);
 #endif /* __PERF_HEADER_H */
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 1/2] tools/perf: Add utility function to read /proc/cpuinfo for any field

Quoted 39 lines · Athira Rajeev

/proc/cpuinfo provides information about type of processor, number of CPU's etc. Reading /proc/cpuinfo file outputs useful information by field name like cpu, platform, model (depending on architecture) and its value separated by colon.

Add new utility function "cpuinfo_field" in "util/header.c" which accepts field name as input string to search in /proc/cpuinfo content. This returns the first matching value as resulting string. Example, calling the function "cpuinfo_field(platform)" in powerpc returns the platform value. This can be used to fetch processor information from "cpuinfo" by other utilities/testcases.

---
 tools/perf/util/header.c | 54 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/header.h |  1 +
 2 files changed, 55 insertions(+)
tools/perf/util/header.c +13 -0
index a27132e5a5ef..0c8dfd0c1e78 100644
@@ -983,6 +983,60 @@ static int write_dir_format(struct feat_fd *ff,
 	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
 }
 
+/*
+ * Return entry from /proc/cpuinfo
+ * indicated by "search" parameter.
+ */
+char *cpuinfo_field(const char *search)
+{
+	FILE *file;
+	char *buf = NULL;
+	char *copy_buf = NULL, *p;
+	size_t len = 0;
+	int ret = -1;
+
+	if (!search)

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1008

Quoted 10 lines
tools/perf/util/header.c +10 -0
+		return NULL;
+
+	file = fopen("/proc/cpuinfo", "r");
+	if (!file)
+		return NULL;
+
+	while (getline(&buf, &len, file) > 0) {
+		ret = strncmp(buf, search, strlen(search));
+		if (!ret)
+			break;

Hi Athira,
Do we need ret variable. Since we will come out of the loop only when
we reach EOF.

Quoted 5 lines
tools/perf/util/header.c +5 -0
+	}
+
+	if (ret)
+		goto done;
+

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1023

Quoted 10 lines
tools/perf/util/header.c +10 -0
+	/*
+	 * Trim the new line and separate
+	 * value for search field from ":"
+	 * in cpuinfo line output.
+	 * Example output line:
+	 * platform : <value>
+	 */
+	copy_buf = buf;
+	p = strchr(copy_buf, ':');
+	if (p && *(p+1) == ' ' && *(p+2))

Can you try using strim instead to remove whitespaces. This function will remove leading and trailing whitespaces from the string.

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1025

Quoted 2 lines
tools/perf/util/header.c +2 -0
+		copy_buf = p + 2;
+	p = strchr(copy_buf, '\n');

do we need to replace \n here ?

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff,

Quoted 5 lines
tools/perf/util/header.c +5 -0
+	if (p)
+		*p = '\0';
+
+	/* Copy the filtered string to buf */
+	strcpy(buf, copy_buf)

You are initializing buf to NULL. So do we need to do fclose and return buf separately here? Can you move free(buf) in above condition and reuse done code.

Quoted 21 lines
tools/perf/util/header.c +8 -0
+
+	fclose(file);
+	return buf;> +
+done:
+	free(buf);
+	fclose(file);
+	return NULL;
+}
 /*
  * Check whether a CPU is online
  *
tools/perf/util/header.h +1 -0
index 0eb4bc29a5a4..b0f754364bd4 100644
@@ -166,4 +166,5 @@ int get_cpuid(char *buffer, size_t sz);
 
 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
 int strcmp_cpuid_str(const char *s1, const char *s2);
+char *cpuinfo_field(const char *search);
 #endif /* __PERF_HEADER_H */
#3 lore raw

Re: [PATCH 1/2] tools/perf: Add utility function to read /proc/cpuinfo for any field

Quoted 71 lines · kajoljain
Quoted 39 lines · Athira Rajeev

/proc/cpuinfo provides information about type of processor, number of CPU's etc. Reading /proc/cpuinfo file outputs useful information by field name like cpu, platform, model (depending on architecture) and its value separated by colon.

Add new utility function "cpuinfo_field" in "util/header.c" which accepts field name as input string to search in /proc/cpuinfo content. This returns the first matching value as resulting string. Example, calling the function "cpuinfo_field(platform)" in powerpc returns the platform value. This can be used to fetch processor information from "cpuinfo" by other utilities/testcases.

---
tools/perf/util/header.c | 54 ++++++++++++++++++++++++++++++++++++++++ tools/perf/util/header.h | 1 +

2 files changed, 55 insertions(+)
tools/perf/util/header.c +13 -0
index a27132e5a5ef..0c8dfd0c1e78 100644
@@ -983,6 +983,60 @@ static int write_dir_format(struct feat_fd *ff,
 	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
 }
 
+/*
+ * Return entry from /proc/cpuinfo
+ * indicated by "search" parameter.
+ */
+char *cpuinfo_field(const char *search)
+{
+	FILE *file;
+	char *buf = NULL;
+	char *copy_buf = NULL, *p;
+	size_t len = 0;
+	int ret = -1;
+
+	if (!search)

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1008

Quoted 10 lines
tools/perf/util/header.c +10 -0
+		return NULL;
+
+	file = fopen("/proc/cpuinfo", "r");
+	if (!file)
+		return NULL;
+
+	while (getline(&buf, &len, file) > 0) {
+		ret = strncmp(buf, search, strlen(search));
+		if (!ret)
+			break;

Hi Athira,
Do we need ret variable. Since we will come out of the loop only when
we reach EOF.

Quoted 11 lines
tools/perf/util/header.c +11 -0
+	}
+
+	if (ret)
+		goto done;
+
+	/*
+	 * Trim the new line and separate
+	 * value for search field from ":"
+	 * in cpuinfo line output.
+	 * Example output line:
+	 * platform : <value>
Quoted 8 lines

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1023

Quoted 4 lines
tools/perf/util/header.c +4 -0
+	 */
+	copy_buf = buf;
+	p = strchr(copy_buf, ':');
+	if (p && *(p+1) == ' ' && *(p+2))

Can you try using strim instead to remove whitespaces. This function will remove leading and trailing whitespaces from the string.

Hi Kajol,

Thanks for review comments.
I will send V2 addressing these changes

Athira

Quoted 37 lines

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff, · line 1025

Quoted 3 lines
tools/perf/util/header.c +2 -0
+		copy_buf = p + 2;
+	p = strchr(copy_buf, '\n');

do we need to replace \n here ?

tools/perf/util/header.c static int write_dir_format(struct feat_fd *ff,

Quoted 5 lines
tools/perf/util/header.c +5 -0
+	if (p)
+		*p = '\0';
+
+	/* Copy the filtered string to buf */
+	strcpy(buf, copy_buf)

You are initializing buf to NULL. So do we need to do fclose and return buf separately here? Can you move free(buf) in above condition and reuse done code.

Quoted 21 lines
tools/perf/util/header.c +8 -0
+
+	fclose(file);
+	return buf;> +
+done:
+	free(buf);
+	fclose(file);
+	return NULL;
+}
 /*
 * Check whether a CPU is online
 *
tools/perf/util/header.h +1 -0
index 0eb4bc29a5a4..b0f754364bd4 100644
@@ -166,4 +166,5 @@ int get_cpuid(char *buffer, size_t sz);
 
 char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
 int strcmp_cpuid_str(const char *s1, const char *s2);
+char *cpuinfo_field(const char *search);
 #endif /* __PERF_HEADER_H */
#4 lore raw

PATCH 2/2 tools/perf/tests: Fix session topology test to skip the test in guest environment

In reply to Athira Rajeev

The session topology test fails in powerpc pSeries platform. Test logs:
<<>>
Session topology : FAILED!
<<>>

This testcases tests cpu topology by checking the core_id and socket_id stored in perf_env from perf session. The data from perf session is compared with the cpu topology information from "/sys/devices/system/cpu/cpuX/topology" like core_id, physical_package_id. In case of virtual environment, detail like physical_package_id is restricted to be exposed. Hence physical_package_id is set to -1. The testcase fails on such platforms since socket_id can't be fetched from topology info.

Skip the testcase in powerpc for pSeries. Use the utility function "cpuinfo_field" to check platform from /proc/cpuinfo.

---
 tools/perf/tests/topology.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/perf/tests/topology.c +17 -0
index ee1e3dcbc0bd..0ddcafa158db 100644
@@ -109,6 +109,23 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)
 			&& strncmp(session->header.env.arch, "aarch64", 7))
 		return TEST_SKIP;
 
+	/*
+	 * In powerpc pSeries platform, not all the topology information
+	 * are exposed via sysfs. Due to restriction, detail like
+	 * physical_package_id will be set to -1. Hence skip this
+	 * test for pSeries.
+	 */
+	if (strncmp(session->header.env.arch, "powerpc", 7)) {
+		char *cpuinfo_platform = NULL;
+
+		cpuinfo_platform = cpuinfo_field("platform");
+		if (!strcmp(cpuinfo_platform, "pSeries")) {
+			free(cpuinfo_platform);
+			return TEST_SKIP;
+		}
+		free(cpuinfo_platform);
+	}
+
 	TEST_ASSERT_VAL("Session header CPU map not set", session->header.env.cpu);
 
 	for (i = 0; i < session->header.env.nr_cpus_avail; i++) {
Signature · 1 line
2.35.1
#5 lore raw
In reply to Athira Rajeev

-----Original Message-----
From: Athira Rajeev <[email protected]>
To: [email protected], [email protected], [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Subject: [PATCH 0/2] Fix session topology test for powerpc and add utility function to get cpuinfo entries
Date: Thu, 28 Apr 2022 20:38:27 +0530

The session topology test fails in powerpc pSeries platform.Test logs:<<>>Session topology : FAILED!<<>>
This test uses cpu topology information and in powerpc,some of the topology info is restricted in environmentlike virtualized platform. Hence this test needs to beskipped in pSeries platform for powerpc. The informationabout platform is available in /proc/cpuinfo. Patch 1 adds generic utility function in "util/header.c"to read /proc/cpuinfo for any entry. Though the testcasefix needs value from "platform" entry, making this as ageneric function to return value for any entry from the/proc/cpuinfo file which can be used commonly in futureusecases.
Patch 2 uses the newly added utility function to look forplatform and skip the test in pSeries platform for powerpc.
Athira Rajeev (2): tools/perf: Add utility function to read /proc/cpuinfo for any field tools/perf/tests: Fix session topology
test to skip the test in guest environment
Tested the patches on powerpc and powernv, verified perf test session topology test with the patch set.Tested-by: Disha Goel < [email protected]>

 tools/perf/tests/topology.c | 17 ++++++++++++

tools/perf/util/header.c | 54 +++++++++++++++++++++++++++++++++++++
tools/perf/util/header.h | 1 + 3 files changed, 72 insertions(+)