Skip to content

Commit fdf1e29

Browse files
captain5050acmel
authored andcommitted
perf expr: Add metric literals for topology.
Allow the number of cpus, cores, dies and packages to be queried by a metric expression. Signed-off-by: Ian Rogers <[email protected]> Acked-by: Jiri Olsa <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: John Garry <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Kan Liang <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Paul A . Clarke <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Riccardo Mancini <[email protected]> Cc: Song Liu <[email protected]> Cc: Wan Jiabing <[email protected]> Cc: Yury Norov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 3613f6c commit fdf1e29

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

tools/perf/tests/expr.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
6666
{
6767
struct expr_id_data *val_ptr;
6868
const char *p;
69-
double val;
69+
double val, num_cpus, num_cores, num_dies, num_packages;
7070
int ret;
7171
struct expr_parse_ctx *ctx;
7272

@@ -161,6 +161,16 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
161161
NULL, ctx) == 0);
162162
TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0);
163163

164+
/* Test toplogy constants appear well ordered. */
165+
expr__ctx_clear(ctx);
166+
TEST_ASSERT_VAL("#num_cpus", expr__parse(&num_cpus, ctx, "#num_cpus") == 0);
167+
TEST_ASSERT_VAL("#num_cores", expr__parse(&num_cores, ctx, "#num_cores") == 0);
168+
TEST_ASSERT_VAL("#num_cpus >= #num_cores", num_cpus >= num_cores);
169+
TEST_ASSERT_VAL("#num_dies", expr__parse(&num_dies, ctx, "#num_dies") == 0);
170+
TEST_ASSERT_VAL("#num_cores >= #num_dies", num_cores >= num_dies);
171+
TEST_ASSERT_VAL("#num_packages", expr__parse(&num_packages, ctx, "#num_packages") == 0);
172+
TEST_ASSERT_VAL("#num_dies >= #num_packages", num_dies >= num_packages);
173+
164174
expr__ctx_free(ctx);
165175

166176
return 0;

tools/perf/util/expr.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stdlib.h>
66
#include <string.h>
77
#include "metricgroup.h"
8+
#include "cpumap.h"
9+
#include "cputopo.h"
810
#include "debug.h"
911
#include "expr.h"
1012
#include "expr-bison.h"
@@ -375,9 +377,34 @@ double expr_id_data__value(const struct expr_id_data *data)
375377

376378
double expr__get_literal(const char *literal)
377379
{
380+
static struct cpu_topology *topology;
381+
378382
if (!strcmp("#smt_on", literal))
379383
return smt_on() > 0 ? 1.0 : 0.0;
380384

385+
if (!strcmp("#num_cpus", literal))
386+
return cpu__max_present_cpu();
387+
388+
/*
389+
* Assume that topology strings are consistent, such as CPUs "0-1"
390+
* wouldn't be listed as "0,1", and so after deduplication the number of
391+
* these strings gives an indication of the number of packages, dies,
392+
* etc.
393+
*/
394+
if (!topology) {
395+
topology = cpu_topology__new();
396+
if (!topology) {
397+
pr_err("Error creating CPU topology");
398+
return NAN;
399+
}
400+
}
401+
if (!strcmp("#num_packages", literal))
402+
return topology->package_cpus_lists;
403+
if (!strcmp("#num_dies", literal))
404+
return topology->die_cpus_lists;
405+
if (!strcmp("#num_cores", literal))
406+
return topology->core_cpus_lists;
407+
381408
pr_err("Unrecognized literal '%s'", literal);
382409
return NAN;
383410
}

0 commit comments

Comments
 (0)