[PATCH v2 8/9] perf metrics: Add has_optane literal

Ian Rogers posted 9 patches 2 years, 10 months ago
[PATCH v2 8/9] perf metrics: Add has_optane literal
Posted by Ian Rogers 2 years, 10 months ago
Add literal so that if optane memory isn't installed we can record
fewer events.  The file detection mechanism was suggested by Dan
Williams <dan.j.williams@intel.com> in:
https://lore.kernel.org/linux-perf-users/641bbe1eced26_1b98bb29440@dwillia2-xfh.jf.intel.com.notmuch/

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/expr.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index d46a1878bc9e..a43cdda0b044 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -14,6 +14,7 @@
 #include "util/hashmap.h"
 #include "smt.h"
 #include "tsc.h"
+#include <api/fs/fs.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
@@ -400,6 +401,20 @@ double arch_get_tsc_freq(void)
 }
 #endif
 
+static double has_optane(void)
+{
+	static bool has_optane, cached;
+	const char *sysfs = sysfs__mountpoint();
+	char path[PATH_MAX];
+
+	if (!cached) {
+		snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs);
+		has_optane = access(path, F_OK) == 0;
+		cached = true;
+	}
+	return has_optane ? 1.0 : 0.0;
+}
+
 double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
 {
 	const struct cpu_topology *topology;
@@ -449,6 +464,10 @@ double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx
 		result = perf_pmu__cpu_slots_per_cycle();
 		goto out;
 	}
+	if (!strcmp("#has_optane", literal)) {
+		result = has_optane();
+		goto out;
+	}
 
 	pr_err("Unrecognized literal '%s'", literal);
 out:
-- 
2.40.0.348.gf938b09366-goog
Re: [PATCH v2 8/9] perf metrics: Add has_optane literal
Posted by Liang, Kan 2 years, 10 months ago

On 2023-03-23 3:20 p.m., Ian Rogers wrote:
> Add literal so that if optane memory isn't installed we can record
> fewer events.  

I think we call it pmem (Persistent Memory) everywhere in the Linux
code. Maybe we should use #has_pmem instead?

Thanks,
Kan
> The file detection mechanism was suggested by Dan
> Williams <dan.j.williams@intel.com> in:
> https://lore.kernel.org/linux-perf-users/641bbe1eced26_1b98bb29440@dwillia2-xfh.jf.intel.com.notmuch/
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/expr.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
> index d46a1878bc9e..a43cdda0b044 100644
> --- a/tools/perf/util/expr.c
> +++ b/tools/perf/util/expr.c
> @@ -14,6 +14,7 @@
>  #include "util/hashmap.h"
>  #include "smt.h"
>  #include "tsc.h"
> +#include <api/fs/fs.h>
>  #include <linux/err.h>
>  #include <linux/kernel.h>
>  #include <linux/zalloc.h>
> @@ -400,6 +401,20 @@ double arch_get_tsc_freq(void)
>  }
>  #endif
>  
> +static double has_optane(void)
> +{
> +	static bool has_optane, cached;
> +	const char *sysfs = sysfs__mountpoint();
> +	char path[PATH_MAX];
> +
> +	if (!cached) {
> +		snprintf(path, sizeof(path), "%s/firmware/acpi/tables/NFIT", sysfs);
> +		has_optane = access(path, F_OK) == 0;
> +		cached = true;
> +	}
> +	return has_optane ? 1.0 : 0.0;
> +}
> +
>  double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx)
>  {
>  	const struct cpu_topology *topology;
> @@ -449,6 +464,10 @@ double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx
>  		result = perf_pmu__cpu_slots_per_cycle();
>  		goto out;
>  	}
> +	if (!strcmp("#has_optane", literal)) {
> +		result = has_optane();
> +		goto out;
> +	}
>  
>  	pr_err("Unrecognized literal '%s'", literal);
>  out:
Re: [PATCH v2 8/9] perf metrics: Add has_optane literal
Posted by Dan Williams 2 years, 10 months ago
Liang, Kan wrote:
> 
> 
> On 2023-03-23 3:20 p.m., Ian Rogers wrote:
> > Add literal so that if optane memory isn't installed we can record
> > fewer events.  
> 
> I think we call it pmem (Persistent Memory) everywhere in the Linux
> code. Maybe we should use #has_pmem instead?

That makes sense especially because has_optane implies more precision
than the the check is performing. In general Optane is probably the
widest deployed NVDIMM type, but this check will succeed with battery
backed NVDIMMs and emulated PMEM in VMs which I think is perfectly ok
for making a default event record decision.
Re: [PATCH v2 8/9] perf metrics: Add has_optane literal
Posted by Ian Rogers 2 years, 10 months ago
On Thu, Mar 23, 2023 at 1:44 PM Dan Williams <dan.j.williams@intel.com> wrote:
>
> Liang, Kan wrote:
> >
> >
> > On 2023-03-23 3:20 p.m., Ian Rogers wrote:
> > > Add literal so that if optane memory isn't installed we can record
> > > fewer events.
> >
> > I think we call it pmem (Persistent Memory) everywhere in the Linux
> > code. Maybe we should use #has_pmem instead?
>
> That makes sense especially because has_optane implies more precision
> than the the check is performing. In general Optane is probably the
> widest deployed NVDIMM type, but this check will succeed with battery
> backed NVDIMMs and emulated PMEM in VMs which I think is perfectly ok
> for making a default event record decision.

Thanks, I'll change this in v3.

Ian