Skip to contentSkip to conversation

perf expr: Add debug logging for literals

PATCH Applied Ian Rogers 3 participants 8 messages Nov 24, 2021 – Nov 25, 2021 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v2 1/4 perf expr: Add debug logging for literals

Useful for diagnosing problems with metrics.

---
 tools/perf/util/expr.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
tools/perf/util/expr.c +24 -11
index 1d532b9fed29..cdbab4f959fe 100644
@@ -395,12 +395,17 @@ double expr_id_data__source_count(const struct expr_id_data *data)
 double expr__get_literal(const char *literal)
 {
 	static struct cpu_topology *topology;
+	double result = NAN;
 
-	if (!strcmp("#smt_on", literal))
-		return smt_on() > 0 ? 1.0 : 0.0;
+	if (!strcmp("#smt_on", literal)) {
+		result =  smt_on() > 0 ? 1.0 : 0.0;
+		goto out;
+	}
 
-	if (!strcmp("#num_cpus", literal))
-		return cpu__max_present_cpu();
+	if (!strcmp("#num_cpus", literal)) {
+		result = cpu__max_present_cpu();
+		goto out;
+	}
 
 	/*
 	 * Assume that topology strings are consistent, such as CPUs "0-1"
@@ -415,13 +420,21 @@ double expr__get_literal(const char *literal)
 			return NAN;
 		}
 	}
-	if (!strcmp("#num_packages", literal))
-		return topology->package_cpus_lists;
-	if (!strcmp("#num_dies", literal))
-		return topology->die_cpus_lists;
-	if (!strcmp("#num_cores", literal))
-		return topology->core_cpus_lists;
+	if (!strcmp("#num_packages", literal)) {
+		result = topology->package_cpus_lists;
+		goto out;
+	}
+	if (!strcmp("#num_dies", literal)) {
+		result = topology->die_cpus_lists;
+		goto out;
+	}
+	if (!strcmp("#num_cores", literal)) {
+		result = topology->core_cpus_lists;
+		goto out;
+	}
 
 	pr_err("Unrecognized literal '%s'", literal);
-	return NAN;
+out:
+	pr_debug2("literal: %s = %f\n", literal, result);
+	return result;
 }
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
author reviewer
#1 lore raw

PATCH v2 2/4 perf tools: Fix SMT not detected

sysfs__read_int returns 0 on success, and so the fast read path was always failing.

Fixes bb629484d924 (perf tools: Simplify checking if SMT is active.)
---
 tools/perf/util/smt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/smt.c +1 -1
index 20bacd5972ad..34f1b1b1176c 100644
@@ -15,7 +15,7 @@ int smt_on(void)
 	if (cached)
 		return cached_result;
 
-	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) > 0)
+	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0)
 		goto done;
 
 	ncpu = sysconf(_SC_NPROCESSORS_CONF);
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
maintainer Applied
#3 lore raw

Re: [PATCH v2 2/4] perf tools: Fix SMT not detected

In reply to Ian Rogers

Em Tue, Nov 23, 2021 at 04:12:29PM -0800, Ian Rogers escreveu:

Quoted 5 lines

sysfs__read_int returns 0 on success, and so the fast read path was always failing.

Fixes bb629484d924 (perf tools: Simplify checking if SMT is active.)

Thanks, applied to perf/urgent.

- Arnaldo

Quoted 19 lines
---
 tools/perf/util/smt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/smt.c +1 -1
index 20bacd5972ad..34f1b1b1176c 100644
@@ -15,7 +15,7 @@ int smt_on(void)
 	if (cached)
 		return cached_result;
 
-	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) > 0)
+	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0)
 		goto done;
 
 	ncpu = sysconf(_SC_NPROCESSORS_CONF);
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
Signature · 1 line
- Arnaldo
author reviewer
#4 lore raw

PATCH v2 3/4 perf tools: Fix SMT fallback with large core counts

In reply to Ian Rogers

strtoull can only read a 64-bit bitmap. On an AMD EPYC core_cpus may look like:
00000000,00000000,00000000,00000001,00000000,00000000,00000000,00000001 and so the sibling wasn't spotted. Fix by writing a simple hweight string parser.

---
 tools/perf/util/smt.c | 68 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 10 deletions(-)
tools/perf/util/smt.c +58 -10
index 34f1b1b1176c..2636be65305a 100644
@@ -5,6 +5,56 @@
 #include "api/fs/fs.h"
 #include "smt.h"
 
+/**
+ * hweight_str - Returns the number of bits set in str. Stops at first non-hex
+ *	       or ',' character.
+ */
+static int hweight_str(char *str)
+{
+	int result = 0;
+
+	while (*str) {
+		switch (*str++) {
+		case '0':
+		case ',':
+			break;
+		case '1':
+		case '2':
+		case '4':
+		case '8':
+			result++;
+			break;
+		case '3':
+		case '5':
+		case '6':
+		case '9':
+		case 'a':
+		case 'A':
+		case 'c':
+		case 'C':
+			result += 2;
+			break;
+		case '7':
+		case 'b':
+		case 'B':
+		case 'd':
+		case 'D':
+		case 'e':
+		case 'E':
+			result += 3;
+			break;
+		case 'f':
+		case 'F':
+			result += 4;
+			break;
+		default:
+			goto done;
+		}
+	}
+done:
+	return result;
+}
+
 int smt_on(void)
 {
 	static bool cached;
@@ -15,9 +65,12 @@ int smt_on(void)
 	if (cached)
 		return cached_result;
 
-	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0)
-		goto done;
+	if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0) {
+		cached = true;
+		return cached_result;
+	}
 
+	cached_result = 0;
 	ncpu = sysconf(_SC_NPROCESSORS_CONF);
 	for (cpu = 0; cpu < ncpu; cpu++) {
 		unsigned long long siblings;
@@ -35,18 +88,13 @@ int smt_on(void)
 				continue;
 		}
 		/* Entry is hex, but does not have 0x, so need custom parser */
-		siblings = strtoull(str, NULL, 16);
+		siblings = hweight_str(str);
 		free(str);
-		if (hweight64(siblings) > 1) {
+		if (siblings > 1) {
 			cached_result = 1;
-			cached = true;
 			break;
 		}
 	}
-	if (!cached) {
-		cached_result = 0;
-done:
-		cached = true;
-	}
+	cached = true;
 	return cached_result;
 }
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
#5 lore raw

Re: [PATCH v2 3/4] perf tools: Fix SMT fallback with large core counts

Quoted 16 lines · Ian Rogers

strtoull can only read a 64-bit bitmap. On an AMD EPYC core_cpus may look like:
00000000,00000000,00000000,00000001,00000000,00000000,00000000,00000001 and so the sibling wasn't spotted. Fix by writing a simple hweight string parser.

---
  tools/perf/util/smt.c | 68 ++++++++++++++++++++++++++++++++++++-------
  1 file changed, 58 insertions(+), 10 deletions(-)
tools/perf/util/smt.c +0 -0
index 34f1b1b1176c..2636be65305a 100644

tools/perf/util/smt.c line 10

Quoted 6 lines
tools/perf/util/smt.c +3 -0
@@ -5,6 +5,56 @@
  #include "api/fs/fs.h"
  #include "smt.h"
 
+/**
+ * hweight_str - Returns the number of bits set in str. Stops at first non-hex
+ *	       or ',' character.

nit: the wording here could be improved, I think. You really mean we iter until we reach a character which is neither a hex nor a ',', while I read this as iter until either a non-hex or a ','.

tools/perf/util/smt.c line 16

Quoted 6 lines
tools/perf/util/smt.c +6 -0
+ */
+static int hweight_str(char *str)
+{
+	int result = 0;
+
+	while (*str) {

Can we use __buitin_popcount() to try to simplify?

Quoted 38 lines
tools/perf/util/smt.c +38 -0
+		switch (*str++) {
+		case '0':
+		case ',':
+			break;
+		case '1':
+		case '2':
+		case '4':
+		case '8':
+			result++;
+			break;
+		case '3':
+		case '5':
+		case '6':
+		case '9':
+		case 'a':
+		case 'A':
+		case 'c':
+		case 'C':
+			result += 2;
+			break;
+		case '7':
+		case 'b':
+		case 'B':
+		case 'd':
+		case 'D':
+		case 'e':
+		case 'E':
+			result += 3;
+			break;
+		case 'f':
+		case 'F':
+			result += 4;
+			break;
+		default:
+			goto done;
+		}
+	}
+done:
author reviewer
#6 lore raw

PATCH v2 4/4 perf tools: Probe non-deprecated sysfs path 1st

In reply to Ian Rogers

Following Documentation/ABI/stable/sysfs-devices-system-cpu the /sys/devices/system/cpu/cpuX/topology/core_cpus is deprecated in favor of thread_siblings, so probe thread_siblings before falling back on core_cpus.

---
 tools/perf/util/smt.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/perf/util/smt.c +2 -3
index 2636be65305a..2b0a36ebf27a 100644
@@ -79,11 +79,10 @@ int smt_on(void)
 		char fn[256];
 
 		snprintf(fn, sizeof fn,
-			"devices/system/cpu/cpu%d/topology/core_cpus", cpu);
+			"devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
 		if (sysfs__read_str(fn, &str, &strlen) < 0) {
 			snprintf(fn, sizeof fn,
-				"devices/system/cpu/cpu%d/topology/thread_siblings",
-				cpu);
+				"devices/system/cpu/cpu%d/topology/core_cpus", cpu);
 			if (sysfs__read_str(fn, &str, &strlen) < 0)
 				continue;
 		}
Signature · 1 line
2.34.0.rc2.393.gf8c9666880-goog
#7 lore raw
In reply to Ian Rogers
Quoted 8 lines
  	}
tools/perf/util/expr.c +0 -6
-	if (!strcmp("#num_packages", literal))
-		return topology->package_cpus_lists;
-	if (!strcmp("#num_dies", literal))
-		return topology->die_cpus_lists;
-	if (!strcmp("#num_cores", literal))
-		return topology->core_cpus_lists;

tools/perf/util/expr.c double expr__get_literal(const char *literal) · line 432

Quoted 10 lines
tools/perf/util/expr.c +10 -0
+	if (!strcmp("#num_packages", literal)) {
+		result = topology->package_cpus_lists;
+		goto out;
+	}
+	if (!strcmp("#num_dies", literal)) {
+		result = topology->die_cpus_lists;
+		goto out;
+	}
+	if (!strcmp("#num_cores", literal)) {
+		result = topology->core_cpus_lists;

if we find that we now get the same print many times, how about:

		pr_debug2_once("literal: num_cores = %f\n", result);

... not sure if it scales though

Thanks,
John

Quoted 9 lines
tools/perf/util/expr.c +5 -1
+		goto out;
+	}
 
  	pr_err("Unrecognized literal '%s'", literal);
-	return NAN;
+out:
+	pr_debug2("literal: %s = %f\n", literal, result);
+	return result;
  }