Skip to content

Commit a1ebf77

Browse files
James-A-Clarkacmel
authored andcommitted
perf test: Add a test for strcmp_cpuid_str() expression
Test that the new expression builtin returns a match when the current escaped CPU ID is given, and that it doesn't match when "0x0" is given. The CPU ID in test__expr() has to be changed to perf_pmu__getcpuid() which returns the CPU ID string, rather than the raw CPU ID that get_cpuid() returns because that can't be used with strcmp_cpuid_str(). It doesn't affect the is_intel test because both versions contain "Intel". Reviewed-by: Ian Rogers <[email protected]> Signed-off-by: James Clark <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Chen Zhongjin <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: Haixin Yu <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jing Zhang <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Garry <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Liam Howlett <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mike Leach <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yang Jihong <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 8a55c1e commit a1ebf77

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

tools/perf/tests/expr.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <math.h>
1010
#include <stdlib.h>
1111
#include <string.h>
12+
#include <string2.h>
1213
#include <linux/zalloc.h>
1314

1415
static int test_ids_union(void)
@@ -74,10 +75,13 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
7475
int ret;
7576
struct expr_parse_ctx *ctx;
7677
bool is_intel = false;
77-
char buf[128];
78+
char strcmp_cpuid_buf[256];
79+
struct perf_pmu *pmu = pmu__find_core_pmu();
80+
char *cpuid = perf_pmu__getcpuid(pmu);
81+
char *escaped_cpuid1, *escaped_cpuid2;
7882

79-
if (!get_cpuid(buf, sizeof(buf)))
80-
is_intel = strstr(buf, "Intel") != NULL;
83+
TEST_ASSERT_VAL("get_cpuid", cpuid);
84+
is_intel = strstr(cpuid, "Intel") != NULL;
8185

8286
TEST_ASSERT_EQUAL("ids_union", test_ids_union(), 0);
8387

@@ -257,9 +261,28 @@ static int test__expr(struct test_suite *t __maybe_unused, int subtest __maybe_u
257261
TEST_ASSERT_VAL("source count", hashmap__size(ctx->ids) == 1);
258262
TEST_ASSERT_VAL("source count", hashmap__find(ctx->ids, "EVENT1", &val_ptr));
259263

264+
265+
/* Test no cpuid match */
266+
ret = test(ctx, "strcmp_cpuid_str(0x0)", 0);
267+
268+
/*
269+
* Test cpuid match with current cpuid. Special chars have to be
270+
* escaped.
271+
*/
272+
escaped_cpuid1 = strreplace_chars('-', cpuid, "\\-");
273+
free(cpuid);
274+
escaped_cpuid2 = strreplace_chars(',', escaped_cpuid1, "\\,");
275+
free(escaped_cpuid1);
276+
escaped_cpuid1 = strreplace_chars('=', escaped_cpuid2, "\\=");
277+
free(escaped_cpuid2);
278+
scnprintf(strcmp_cpuid_buf, sizeof(strcmp_cpuid_buf),
279+
"strcmp_cpuid_str(%s)", escaped_cpuid1);
280+
free(escaped_cpuid1);
281+
ret |= test(ctx, strcmp_cpuid_buf, 1);
282+
260283
/* has_event returns 1 when an event exists. */
261284
expr__add_id_val(ctx, strdup("cycles"), 2);
262-
ret = test(ctx, "has_event(cycles)", 1);
285+
ret |= test(ctx, "has_event(cycles)", 1);
263286

264287
expr__ctx_free(ctx);
265288

0 commit comments

Comments
 (0)