Skip to content

Commit

Permalink
perf cpumap: Migrate to libperf cpumap api
Browse files Browse the repository at this point in the history
Switch from directly accessing the perf_cpu_map to using the appropriate
libperf API when possible. Using the API simplifies the job of
refactoring use of perf_cpu_map.

Signed-off-by: Ian Rogers <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Alexey Bayduraev <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: André Almeida <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Darren Hart <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: Dmitriy Vyukov <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: German Gomez <[email protected]>
Cc: James Clark <[email protected]>
Cc: Jin Yao <[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: Madhavan Srinivasan <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Miaoqian Lin <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Riccardo Mancini <[email protected]>
Cc: Shunsuke Nakamura <[email protected]>
Cc: Song Liu <[email protected]>
Cc: Stephane Eranian <[email protected]>
Cc: Stephen Brennan <[email protected]>
Cc: Steven Rostedt (VMware) <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Thomas Richter <[email protected]>
Cc: Yury Norov <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
captain5050 authored and acmel committed Jan 22, 2022
1 parent 1d1d9af commit 4402869
Show file tree
Hide file tree
Showing 31 changed files with 99 additions and 87 deletions.
4 changes: 2 additions & 2 deletions tools/lib/perf/evsel.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
}

if (evsel->fd == NULL &&
perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
perf_evsel__alloc_fd(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
return -ENOMEM;

perf_cpu_map__for_each_cpu(cpu, idx, cpus) {
Expand Down Expand Up @@ -384,7 +384,7 @@ int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter)
{
int err = 0, i;

for (i = 0; i < evsel->cpus->nr && !err; i++)
for (i = 0; i < perf_cpu_map__nr(evsel->cpus) && !err; i++)
err = perf_evsel__run_ioctl(evsel,
PERF_EVENT_IOC_SET_FILTER,
(void *)filter, i);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/epoll-ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ int bench_epoll_ctl(int argc, const char **argv)

/* default to the number of CPUs */
if (!nthreads)
nthreads = cpu->nr;
nthreads = perf_cpu_map__nr(cpu);

worker = calloc(nthreads, sizeof(*worker));
if (!worker)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/epoll-wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ int bench_epoll_wait(int argc, const char **argv)

/* default to the number of CPUs and leave one for the writer pthread */
if (!nthreads)
nthreads = cpu->nr - 1;
nthreads = perf_cpu_map__nr(cpu) - 1;

worker = calloc(nthreads, sizeof(*worker));
if (!worker) {
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/bench/evlist-open-close.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static int evlist__count_evsel_fds(struct evlist *evlist)
int cnt = 0;

evlist__for_each_entry(evlist, evsel)
cnt += evsel->core.threads->nr * evsel->core.cpus->nr;
cnt += evsel->core.threads->nr * perf_cpu_map__nr(evsel->core.cpus);

return cnt;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ static int bench_evlist_open_close__run(char *evstr)

init_stats(&time_stats);

printf(" Number of cpus:\t%d\n", evlist->core.cpus->nr);
printf(" Number of cpus:\t%d\n", perf_cpu_map__nr(evlist->core.cpus));
printf(" Number of threads:\t%d\n", evlist->core.threads->nr);
printf(" Number of events:\t%d (%d fds)\n",
evlist->core.nr_entries, evlist__count_evsel_fds(evlist));
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/futex-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ int bench_futex_hash(int argc, const char **argv)
}

if (!params.nthreads) /* default to the number of CPUs */
params.nthreads = cpu->nr;
params.nthreads = perf_cpu_map__nr(cpu);

worker = calloc(params.nthreads, sizeof(*worker));
if (!worker)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/futex-lock-pi.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ int bench_futex_lock_pi(int argc, const char **argv)
}

if (!params.nthreads)
params.nthreads = cpu->nr;
params.nthreads = perf_cpu_map__nr(cpu);

worker = calloc(params.nthreads, sizeof(*worker));
if (!worker)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/futex-requeue.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int bench_futex_requeue(int argc, const char **argv)
}

if (!params.nthreads)
params.nthreads = cpu->nr;
params.nthreads = perf_cpu_map__nr(cpu);

worker = calloc(params.nthreads, sizeof(*worker));
if (!worker)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/futex-wake-parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int bench_futex_wake_parallel(int argc, const char **argv)
err(EXIT_FAILURE, "calloc");

if (!params.nthreads)
params.nthreads = cpu->nr;
params.nthreads = perf_cpu_map__nr(cpu);

/* some sanity checks */
if (params.nwakes > params.nthreads ||
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/bench/futex-wake.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ int bench_futex_wake(int argc, const char **argv)
}

if (!params.nthreads)
params.nthreads = cpu->nr;
params.nthreads = perf_cpu_map__nr(cpu);

worker = calloc(params.nthreads, sizeof(*worker));
if (!worker)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/builtin-ftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static int set_tracing_cpumask(struct perf_cpu_map *cpumap)
int ret;
int last_cpu;

last_cpu = perf_cpu_map__cpu(cpumap, cpumap->nr - 1).cpu;
last_cpu = perf_cpu_map__cpu(cpumap, perf_cpu_map__nr(cpumap) - 1).cpu;
mask_size = last_cpu / 4 + 2; /* one more byte for EOS */
mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */

Expand Down
7 changes: 4 additions & 3 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,12 @@ static bool cpus_map_matched(struct evsel *a, struct evsel *b)
if (!a->core.cpus || !b->core.cpus)
return false;

if (a->core.cpus->nr != b->core.cpus->nr)
if (perf_cpu_map__nr(a->core.cpus) != perf_cpu_map__nr(b->core.cpus))
return false;

for (int i = 0; i < a->core.cpus->nr; i++) {
if (a->core.cpus->map[i].cpu != b->core.cpus->map[i].cpu)
for (int i = 0; i < perf_cpu_map__nr(a->core.cpus); i++) {
if (perf_cpu_map__cpu(a->core.cpus, i).cpu !=
perf_cpu_map__cpu(b->core.cpus, i).cpu)
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static unsigned long *get_bitmap(const char *str, int nbits)
bm = bitmap_zalloc(nbits);

if (map && bm) {
for (i = 0; i < map->nr; i++)
set_bit(map->map[i].cpu, bm);
for (i = 0; i < perf_cpu_map__nr(map); i++)
set_bit(perf_cpu_map__cpu(map, i).cpu, bm);
}

if (map)
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/tests/event_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused,

TEST_ASSERT_VAL("wrong id", ev->id == 123);
TEST_ASSERT_VAL("wrong type", ev->type == PERF_EVENT_UPDATE__CPUS);
TEST_ASSERT_VAL("wrong cpus", map->nr == 3);
TEST_ASSERT_VAL("wrong cpus", map->map[0].cpu == 1);
TEST_ASSERT_VAL("wrong cpus", map->map[1].cpu == 2);
TEST_ASSERT_VAL("wrong cpus", map->map[2].cpu == 3);
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__nr(map) == 3);
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 0).cpu == 1);
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 1).cpu == 2);
TEST_ASSERT_VAL("wrong cpus", perf_cpu_map__cpu(map, 2).cpu == 3);
perf_cpu_map__put(map);
return 0;
}
Expand Down
9 changes: 5 additions & 4 deletions tools/perf/tests/mem2node.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ static unsigned long *get_bitmap(const char *str, int nbits)
{
struct perf_cpu_map *map = perf_cpu_map__new(str);
unsigned long *bm = NULL;
int i;

bm = bitmap_zalloc(nbits);

if (map && bm) {
for (i = 0; i < map->nr; i++) {
set_bit(map->map[i].cpu, bm);
}
struct perf_cpu cpu;
int i;

perf_cpu_map__for_each_cpu(cpu, i, map)
set_bit(cpu.cpu, bm);
}

if (map)
Expand Down
5 changes: 3 additions & 2 deletions tools/perf/tests/mmap-basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
}

CPU_ZERO(&cpu_set);
CPU_SET(cpus->map[0].cpu, &cpu_set);
CPU_SET(perf_cpu_map__cpu(cpus, 0).cpu, &cpu_set);
sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
pr_debug("sched_setaffinity() failed on CPU %d: %s ",
cpus->map[0].cpu, str_error_r(errno, sbuf, sizeof(sbuf)));
perf_cpu_map__cpu(cpus, 0).cpu,
str_error_r(errno, sbuf, sizeof(sbuf)));
goto out_free_cpus;
}

Expand Down
37 changes: 21 additions & 16 deletions tools/perf/tests/topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,44 +122,48 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)
}

// Test that CPU ID contains socket, die, core and CPU
for (i = 0; i < map->nr; i++) {
for (i = 0; i < perf_cpu_map__nr(map); i++) {
id = aggr_cpu_id__cpu(perf_cpu_map__cpu(map, i), NULL);
TEST_ASSERT_VAL("Cpu map - CPU ID doesn't match", map->map[i].cpu == id.cpu.cpu);
TEST_ASSERT_VAL("Cpu map - CPU ID doesn't match",
perf_cpu_map__cpu(map, i).cpu == id.cpu.cpu);

TEST_ASSERT_VAL("Cpu map - Core ID doesn't match",
session->header.env.cpu[map->map[i].cpu].core_id == id.core);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].core_id == id.core);
TEST_ASSERT_VAL("Cpu map - Socket ID doesn't match",
session->header.env.cpu[map->map[i].cpu].socket_id == id.socket);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].socket_id ==
id.socket);

TEST_ASSERT_VAL("Cpu map - Die ID doesn't match",
session->header.env.cpu[map->map[i].cpu].die_id == id.die);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].die_id == id.die);
TEST_ASSERT_VAL("Cpu map - Node ID is set", id.node == -1);
TEST_ASSERT_VAL("Cpu map - Thread is set", id.thread == -1);
}

// Test that core ID contains socket, die and core
for (i = 0; i < map->nr; i++) {
for (i = 0; i < perf_cpu_map__nr(map); i++) {
id = aggr_cpu_id__core(perf_cpu_map__cpu(map, i), NULL);
TEST_ASSERT_VAL("Core map - Core ID doesn't match",
session->header.env.cpu[map->map[i].cpu].core_id == id.core);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].core_id == id.core);

TEST_ASSERT_VAL("Core map - Socket ID doesn't match",
session->header.env.cpu[map->map[i].cpu].socket_id == id.socket);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].socket_id ==
id.socket);

TEST_ASSERT_VAL("Core map - Die ID doesn't match",
session->header.env.cpu[map->map[i].cpu].die_id == id.die);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].die_id == id.die);
TEST_ASSERT_VAL("Core map - Node ID is set", id.node == -1);
TEST_ASSERT_VAL("Core map - Thread is set", id.thread == -1);
}

// Test that die ID contains socket and die
for (i = 0; i < map->nr; i++) {
for (i = 0; i < perf_cpu_map__nr(map); i++) {
id = aggr_cpu_id__die(perf_cpu_map__cpu(map, i), NULL);
TEST_ASSERT_VAL("Die map - Socket ID doesn't match",
session->header.env.cpu[map->map[i].cpu].socket_id == id.socket);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].socket_id ==
id.socket);

TEST_ASSERT_VAL("Die map - Die ID doesn't match",
session->header.env.cpu[map->map[i].cpu].die_id == id.die);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].die_id == id.die);

TEST_ASSERT_VAL("Die map - Node ID is set", id.node == -1);
TEST_ASSERT_VAL("Die map - Core is set", id.core == -1);
Expand All @@ -168,10 +172,11 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)
}

// Test that socket ID contains only socket
for (i = 0; i < map->nr; i++) {
for (i = 0; i < perf_cpu_map__nr(map); i++) {
id = aggr_cpu_id__socket(perf_cpu_map__cpu(map, i), NULL);
TEST_ASSERT_VAL("Socket map - Socket ID doesn't match",
session->header.env.cpu[map->map[i].cpu].socket_id == id.socket);
session->header.env.cpu[perf_cpu_map__cpu(map, i).cpu].socket_id ==
id.socket);

TEST_ASSERT_VAL("Socket map - Node ID is set", id.node == -1);
TEST_ASSERT_VAL("Socket map - Die ID is set", id.die == -1);
Expand All @@ -181,10 +186,10 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)
}

// Test that node ID contains only node
for (i = 0; i < map->nr; i++) {
for (i = 0; i < perf_cpu_map__nr(map); i++) {
id = aggr_cpu_id__node(perf_cpu_map__cpu(map, i), NULL);
TEST_ASSERT_VAL("Node map - Node ID doesn't match",
cpu__get_node(map->map[i]) == id.node);
cpu__get_node(perf_cpu_map__cpu(map, i)) == id.node);
TEST_ASSERT_VAL("Node map - Socket is set", id.socket == -1);
TEST_ASSERT_VAL("Node map - Die ID is set", id.die == -1);
TEST_ASSERT_VAL("Node map - Core is set", id.core == -1);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/auxtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
mp->idx = idx;

if (per_cpu) {
mp->cpu = evlist->core.cpus->map[idx];
mp->cpu = perf_cpu_map__cpu(evlist->core.cpus, idx);
if (evlist->core.threads)
mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
else
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/counts.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ int evsel__alloc_counts(struct evsel *evsel)
struct perf_cpu_map *cpus = evsel__cpus(evsel);
int nthreads = perf_thread_map__nr(evsel->core.threads);

evsel->counts = perf_counts__new(cpus ? cpus->nr : 1, nthreads);
evsel->counts = perf_counts__new(perf_cpu_map__nr(cpus), nthreads);
return evsel->counts != NULL ? 0 : -ENOMEM;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/cpumap.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct perf_cpu cpu__max_present_cpu(void);
*/
static inline bool cpu_map__is_dummy(struct perf_cpu_map *cpus)
{
return cpus->nr == 1 && cpus->map[0].cpu == -1;
return perf_cpu_map__nr(cpus) == 1 && perf_cpu_map__cpu(cpus, 0).cpu == -1;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/util/cputopo.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ struct numa_topology *numa_topology__new(void)
if (!node_map)
goto out;

nr = (u32) node_map->nr;
nr = (u32) perf_cpu_map__nr(node_map);

tp = zalloc(sizeof(*tp) + sizeof(tp->nodes[0])*nr);
if (!tp)
Expand All @@ -334,7 +334,7 @@ struct numa_topology *numa_topology__new(void)
tp->nr = nr;

for (i = 0; i < nr; i++) {
if (load_numa_node(&tp->nodes[i], node_map->map[i].cpu)) {
if (load_numa_node(&tp->nodes[i], perf_cpu_map__cpu(node_map, i).cpu)) {
numa_topology__delete(tp);
tp = NULL;
break;
Expand Down
11 changes: 6 additions & 5 deletions tools/perf/util/evlist-hybrid.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,23 @@ int evlist__fix_hybrid_cpus(struct evlist *evlist, const char *cpu_list)

events_nr++;

if (matched_cpus->nr > 0 && (unmatched_cpus->nr > 0 ||
matched_cpus->nr < cpus->nr ||
matched_cpus->nr < pmu->cpus->nr)) {
if (perf_cpu_map__nr(matched_cpus) > 0 &&
(perf_cpu_map__nr(unmatched_cpus) > 0 ||
perf_cpu_map__nr(matched_cpus) < perf_cpu_map__nr(cpus) ||
perf_cpu_map__nr(matched_cpus) < perf_cpu_map__nr(pmu->cpus))) {
perf_cpu_map__put(evsel->core.cpus);
perf_cpu_map__put(evsel->core.own_cpus);
evsel->core.cpus = perf_cpu_map__get(matched_cpus);
evsel->core.own_cpus = perf_cpu_map__get(matched_cpus);

if (unmatched_cpus->nr > 0) {
if (perf_cpu_map__nr(unmatched_cpus) > 0) {
cpu_map__snprint(matched_cpus, buf1, sizeof(buf1));
pr_warning("WARNING: use %s in '%s' for '%s', skip other cpus in list.\n",
buf1, pmu->name, evsel->name);
}
}

if (matched_cpus->nr == 0) {
if (perf_cpu_map__nr(matched_cpus) == 0) {
evlist__remove(evlist, evsel);
evsel__delete(evsel);

Expand Down
Loading

0 comments on commit 4402869

Please sign in to comment.