Commit 9d5da30e authored by James Clark's avatar James Clark Committed by Arnaldo Carvalho de Melo

perf jevents: Add a new expression builtin strcmp_cpuid_str()

This will allow writing formulas that are conditional on a specific
CPU type or CPU version. It calls through to the existing
strcmp_cpuid_str() function in Perf which has a default weak version,
and an arch specific version for x86 and arm64.

The function takes an 'ID' type value, which is a string. But in this
case Arm CPU IDs are hex numbers prefixed with '0x'. metric.py
assumes strings are only used by event names, and that they can't start
with a number ('0'), so an additional change has to be made to the
regex to convert hex numbers back to 'ID' types.
Signed-off-by: default avatarJames Clark <james.clark@arm.com>
Reviewed-by: default avatarJohn Garry <john.g.garry@oracle.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: Haixin Yu <yuhaixin.yhx@linux.alibaba.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nick Forrington <nick.forrington@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rob Herring <robh@kernel.org>
Cc: Sohom Datta <sohomdatta1@gmail.com>
Cc: Will Deacon <will@kernel.org>
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230816114841.1679234-5-james.clark@arm.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 81f7da54
...@@ -2,28 +2,12 @@ ...@@ -2,28 +2,12 @@
#include <internal/cpumap.h> #include <internal/cpumap.h>
#include "../../../util/cpumap.h" #include "../../../util/cpumap.h"
#include "../../../util/header.h"
#include "../../../util/pmu.h" #include "../../../util/pmu.h"
#include "../../../util/pmus.h" #include "../../../util/pmus.h"
#include <api/fs/fs.h> #include <api/fs/fs.h>
#include <math.h> #include <math.h>
static struct perf_pmu *pmu__find_core_pmu(void)
{
struct perf_pmu *pmu = NULL;
while ((pmu = perf_pmus__scan_core(pmu))) {
/*
* The cpumap should cover all CPUs. Otherwise, some CPUs may
* not support some events or have different event IDs.
*/
if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
return NULL;
return pmu;
}
return NULL;
}
const struct pmu_metrics_table *pmu_metrics_table__find(void) const struct pmu_metrics_table *pmu_metrics_table__find(void)
{ {
struct perf_pmu *pmu = pmu__find_core_pmu(); struct perf_pmu *pmu = pmu__find_core_pmu();
......
...@@ -413,6 +413,10 @@ def has_event(event: Event) -> Function: ...@@ -413,6 +413,10 @@ def has_event(event: Event) -> Function:
# pylint: disable=invalid-name # pylint: disable=invalid-name
return Function('has_event', event) return Function('has_event', event)
def strcmp_cpuid_str(event: str) -> Function:
# pylint: disable=redefined-builtin
# pylint: disable=invalid-name
return Function('strcmp_cpuid_str', event)
class Metric: class Metric:
"""An individual metric that will specifiable on the perf command line.""" """An individual metric that will specifiable on the perf command line."""
...@@ -541,14 +545,23 @@ def ParsePerfJson(orig: str) -> Expression: ...@@ -541,14 +545,23 @@ def ParsePerfJson(orig: str) -> Expression:
""" """
# pylint: disable=eval-used # pylint: disable=eval-used
py = orig.strip() py = orig.strip()
# First try to convert everything that looks like a string (event name) into Event(r"EVENT_NAME").
# This isn't very selective so is followed up by converting some unwanted conversions back again
py = re.sub(r'([a-zA-Z][^-+/\* \\\(\),]*(?:\\.[^-+/\* \\\(\),]*)*)', py = re.sub(r'([a-zA-Z][^-+/\* \\\(\),]*(?:\\.[^-+/\* \\\(\),]*)*)',
r'Event(r"\1")', py) r'Event(r"\1")', py)
# If it started with a # it should have been a literal, rather than an event name
py = re.sub(r'#Event\(r"([^"]*)"\)', r'Literal("#\1")', py) py = re.sub(r'#Event\(r"([^"]*)"\)', r'Literal("#\1")', py)
# Convert accidentally converted hex constants ("0Event(r"xDEADBEEF)"") back to a constant,
# but keep it wrapped in Event(), otherwise Python drops the 0x prefix and it gets interpreted as
# a double by the Bison parser
py = re.sub(r'0Event\(r"[xX]([0-9a-fA-F]*)"\)', r'Event("0x\1")', py)
# Convert accidentally converted scientific notation constants back
py = re.sub(r'([0-9]+)Event\(r"(e[0-9]+)"\)', r'\1\2', py) py = re.sub(r'([0-9]+)Event\(r"(e[0-9]+)"\)', r'\1\2', py)
keywords = ['if', 'else', 'min', 'max', 'd_ratio', 'source_count', 'has_event'] # Convert all the known keywords back from events to just the keyword
keywords = ['if', 'else', 'min', 'max', 'd_ratio', 'source_count', 'has_event', 'strcmp_cpuid_str',
'cpuid_not_more_than']
for kw in keywords: for kw in keywords:
py = re.sub(rf'Event\(r"{kw}"\)', kw, py) py = re.sub(rf'Event\(r"{kw}"\)', kw, py)
try: try:
parsed = ast.parse(py, mode='eval') parsed = ast.parse(py, mode='eval')
except SyntaxError as e: except SyntaxError as e:
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include <util/expr-bison.h> #include <util/expr-bison.h>
#include <util/expr-flex.h> #include <util/expr-flex.h>
#include "util/hashmap.h" #include "util/hashmap.h"
#include "util/header.h"
#include "util/pmu.h"
#include "smt.h" #include "smt.h"
#include "tsc.h" #include "tsc.h"
#include <api/fs/fs.h> #include <api/fs/fs.h>
...@@ -495,3 +497,19 @@ double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const ...@@ -495,3 +497,19 @@ double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const
evlist__delete(tmp); evlist__delete(tmp);
return ret; return ret;
} }
double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx __maybe_unused,
bool compute_ids __maybe_unused, const char *test_id)
{
double ret;
struct perf_pmu *pmu = pmu__find_core_pmu();
char *cpuid = perf_pmu__getcpuid(pmu);
if (!cpuid)
return NAN;
ret = !strcmp_cpuid_str(test_id, cpuid);
free(cpuid);
return ret;
}
...@@ -55,5 +55,6 @@ double expr_id_data__value(const struct expr_id_data *data); ...@@ -55,5 +55,6 @@ double expr_id_data__value(const struct expr_id_data *data);
double expr_id_data__source_count(const struct expr_id_data *data); double expr_id_data__source_count(const struct expr_id_data *data);
double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx); double expr__get_literal(const char *literal, const struct expr_scanner_ctx *ctx);
double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id); double expr__has_event(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id);
double expr__strcmp_cpuid_str(const struct expr_parse_ctx *ctx, bool compute_ids, const char *id);
#endif #endif
...@@ -114,6 +114,7 @@ if { return IF; } ...@@ -114,6 +114,7 @@ if { return IF; }
else { return ELSE; } else { return ELSE; }
source_count { return SOURCE_COUNT; } source_count { return SOURCE_COUNT; }
has_event { return HAS_EVENT; } has_event { return HAS_EVENT; }
strcmp_cpuid_str { return STRCMP_CPUID_STR; }
{literal} { return literal(yyscanner, sctx); } {literal} { return literal(yyscanner, sctx); }
{number} { return value(yyscanner); } {number} { return value(yyscanner); }
{symbol} { return str(yyscanner, ID, sctx->runtime); } {symbol} { return str(yyscanner, ID, sctx->runtime); }
......
...@@ -39,7 +39,7 @@ int expr_lex(YYSTYPE * yylval_param , void *yyscanner); ...@@ -39,7 +39,7 @@ int expr_lex(YYSTYPE * yylval_param , void *yyscanner);
} ids; } ids;
} }
%token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT EXPR_ERROR %token ID NUMBER MIN MAX IF ELSE LITERAL D_RATIO SOURCE_COUNT HAS_EVENT STRCMP_CPUID_STR EXPR_ERROR
%left MIN MAX IF %left MIN MAX IF
%left '|' %left '|'
%left '^' %left '^'
...@@ -207,6 +207,12 @@ expr: NUMBER ...@@ -207,6 +207,12 @@ expr: NUMBER
$$.ids = NULL; $$.ids = NULL;
free($3); free($3);
} }
| STRCMP_CPUID_STR '(' ID ')'
{
$$.val = expr__strcmp_cpuid_str(ctx, compute_ids, $3);
$$.ids = NULL;
free($3);
}
| expr '|' expr | expr '|' expr
{ {
if (is_const($1.val) && is_const($3.val)) { if (is_const($1.val) && is_const($3.val)) {
......
...@@ -1790,3 +1790,20 @@ void perf_pmu__delete(struct perf_pmu *pmu) ...@@ -1790,3 +1790,20 @@ void perf_pmu__delete(struct perf_pmu *pmu)
zfree(&pmu->alias_name); zfree(&pmu->alias_name);
free(pmu); free(pmu);
} }
struct perf_pmu *pmu__find_core_pmu(void)
{
struct perf_pmu *pmu = NULL;
while ((pmu = perf_pmus__scan_core(pmu))) {
/*
* The cpumap should cover all CPUs. Otherwise, some CPUs may
* not support some events or have different event IDs.
*/
if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu)
return NULL;
return pmu;
}
return NULL;
}
...@@ -289,5 +289,6 @@ int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, ...@@ -289,5 +289,6 @@ int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename,
struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name); struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name);
struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus); struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus);
void perf_pmu__delete(struct perf_pmu *pmu); void perf_pmu__delete(struct perf_pmu *pmu);
struct perf_pmu *pmu__find_core_pmu(void);
#endif /* __PMU_H */ #endif /* __PMU_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment