libperf: Add perf_cpu_map__new()/perf_cpu_map__read() functions
authorJiri Olsa <jolsa@kernel.org>
Sun, 21 Jul 2019 11:24:30 +0000 (13:24 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 29 Jul 2019 21:34:45 +0000 (18:34 -0300)
Moving the following functions from tools/perf:

  cpu_map__new()
  cpu_map__read()

to libperf with the following names:

  perf_cpu_map__new()
  perf_cpu_map__read()

Committer notes:

Fixed up this one:

  tools/perf/arch/arm/util/cs-etm.c

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20190721112506.12306-44-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
37 files changed:
tools/perf/arch/arm/util/cs-etm.c
tools/perf/arch/x86/tests/perf-time-to-tsc.c
tools/perf/bench/epoll-ctl.c
tools/perf/bench/epoll-wait.c
tools/perf/bench/futex-hash.c
tools/perf/bench/futex-lock-pi.c
tools/perf/bench/futex-requeue.c
tools/perf/bench/futex-wake-parallel.c
tools/perf/bench/futex-wake.c
tools/perf/builtin-ftrace.c
tools/perf/builtin-sched.c
tools/perf/lib/cpumap.c
tools/perf/lib/include/internal/cpumap.h
tools/perf/lib/include/perf/cpumap.h
tools/perf/lib/libperf.map
tools/perf/tests/bitmap.c
tools/perf/tests/code-reading.c
tools/perf/tests/cpumap.c
tools/perf/tests/event-times.c
tools/perf/tests/event_update.c
tools/perf/tests/keep-tracking.c
tools/perf/tests/mem2node.c
tools/perf/tests/mmap-basic.c
tools/perf/tests/openat-syscall-all-cpus.c
tools/perf/tests/switch-tracking.c
tools/perf/tests/topology.c
tools/perf/util/cpumap.c
tools/perf/util/cpumap.h
tools/perf/util/cputopo.c
tools/perf/util/evlist.c
tools/perf/util/header.c
tools/perf/util/parse-events.c
tools/perf/util/pmu.c
tools/perf/util/python.c
tools/perf/util/record.c
tools/perf/util/session.c
tools/perf/util/svghelper.c

index f5aafdec7f509cbbb130954348089211a0672d44..c25bc1528b963e9ce7c7588d12fbcf2bdec0ee1c 100644 (file)
@@ -156,7 +156,7 @@ static int cs_etm_set_option(struct auxtrace_record *itr,
 {
        int i, err = -EINVAL;
        struct perf_cpu_map *event_cpus = evsel->evlist->cpus;
-       struct perf_cpu_map *online_cpus = cpu_map__new(NULL);
+       struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
 
        /* Set option of each CPU we have */
        for (i = 0; i < cpu__max_cpu(); i++) {
@@ -490,7 +490,7 @@ cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
        int i;
        int etmv3 = 0, etmv4 = 0;
        struct perf_cpu_map *event_cpus = evlist->cpus;
-       struct perf_cpu_map *online_cpus = cpu_map__new(NULL);
+       struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
 
        /* cpu map is not empty, we have specific CPUs to work with */
        if (!cpu_map__empty(event_cpus)) {
@@ -637,7 +637,7 @@ static int cs_etm_info_fill(struct auxtrace_record *itr,
        u64 nr_cpu, type;
        struct perf_cpu_map *cpu_map;
        struct perf_cpu_map *event_cpus = session->evlist->cpus;
-       struct perf_cpu_map *online_cpus = cpu_map__new(NULL);
+       struct perf_cpu_map *online_cpus = perf_cpu_map__new(NULL);
        struct cs_etm_recording *ptr =
                        container_of(itr, struct cs_etm_recording, itr);
        struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
index 07129e007eb089b0ef4acc45d91b11a6dc76a697..261bdd6806517c1449c0e6fd9a517b65eaa61c61 100644 (file)
@@ -5,6 +5,7 @@
 #include <unistd.h>
 #include <linux/types.h>
 #include <sys/prctl.h>
+#include <perf/cpumap.h>
 
 #include "parse-events.h"
 #include "evlist.h"
@@ -65,7 +66,7 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
        threads = thread_map__new(-1, getpid(), UINT_MAX);
        CHECK_NOT_NULL__(threads);
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        CHECK_NOT_NULL__(cpus);
 
        evlist = evlist__new();
index 1fd724f1d48bb64b6e179b09989c5c879b2518aa..84658d45f349e0475156d55ee99cecd838d9128f 100644 (file)
@@ -20,6 +20,7 @@
 #include <sys/resource.h>
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
+#include <perf/cpumap.h>
 
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
@@ -315,7 +316,7 @@ int bench_epoll_ctl(int argc, const char **argv)
        act.sa_sigaction = toggle_done;
        sigaction(SIGINT, &act, NULL);
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                goto errmem;
 
index 79a254fff2d17400851f885190aab756e809406b..c27a65639cfbf975bf02b39928c96bef6a46aec8 100644 (file)
@@ -75,6 +75,7 @@
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
 #include <sys/types.h>
+#include <perf/cpumap.h>
 
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
@@ -429,7 +430,7 @@ int bench_epoll_wait(int argc, const char **argv)
        act.sa_sigaction = toggle_done;
        sigaction(SIGINT, &act, NULL);
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                goto errmem;
 
index b4fea8e3a3685fc2bbf9fb4eda87f1efe5ecc481..80e138904c66df5942c8a595bda9c7c756e93dc6 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
 #include <sys/time.h>
+#include <perf/cpumap.h>
 
 #include "../util/stat.h"
 #include <subcmd/parse-options.h>
@@ -132,7 +133,7 @@ int bench_futex_hash(int argc, const char **argv)
                exit(EXIT_FAILURE);
        }
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                goto errmem;
 
index 5967699247092ca2d8db72d34e01e7c37538d005..c5d6d0abbaa9f7edc1bc6bc458f09de9ee7df9ff 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
 #include <errno.h>
+#include <perf/cpumap.h>
 #include "bench.h"
 #include "futex.h"
 #include "cpumap.h"
@@ -156,7 +157,7 @@ int bench_futex_lock_pi(int argc, const char **argv)
        if (argc)
                goto err;
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                err(EXIT_FAILURE, "calloc");
 
index 1fd32a4f9c1474d4cb6f4fa482d7680bf2e204fd..75d3418c1a8843ca52158db70dab92d7f76d4c90 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/kernel.h>
 #include <linux/time64.h>
 #include <errno.h>
+#include <perf/cpumap.h>
 #include "bench.h"
 #include "futex.h"
 #include "cpumap.h"
@@ -123,7 +124,7 @@ int bench_futex_requeue(int argc, const char **argv)
        if (argc)
                goto err;
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                err(EXIT_FAILURE, "cpu_map__new");
 
index 884c73e5bd1b45a45d75c340ddc481cdd68da337..163fe16c275a07eb210f58880543d8ab1e945a74 100644 (file)
@@ -237,7 +237,7 @@ int bench_futex_wake_parallel(int argc, const char **argv)
        act.sa_sigaction = toggle_done;
        sigaction(SIGINT, &act, NULL);
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                err(EXIT_FAILURE, "calloc");
 
index 2288fa8412ff5fd32e3941142c002690e8421de3..77dcdc13618a601f5d48c58b519dda844e8ef138 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/kernel.h>
 #include <linux/time64.h>
 #include <errno.h>
+#include <perf/cpumap.h>
 #include "bench.h"
 #include "futex.h"
 #include "cpumap.h"
@@ -131,7 +132,7 @@ int bench_futex_wake(int argc, const char **argv)
                exit(EXIT_FAILURE);
        }
 
-       cpu = cpu_map__new(NULL);
+       cpu = perf_cpu_map__new(NULL);
        if (!cpu)
                err(EXIT_FAILURE, "calloc");
 
index 6943352b8d9470de3b10583779a3a6fd600e9693..77989254fdd86284bb1e6797c7ea40cac92cd3be 100644 (file)
@@ -202,7 +202,7 @@ static int set_tracing_cpu(struct perf_ftrace *ftrace)
 
 static int reset_tracing_cpu(void)
 {
-       struct perf_cpu_map *cpumap = cpu_map__new(NULL);
+       struct perf_cpu_map *cpumap = perf_cpu_map__new(NULL);
        int ret;
 
        ret = set_tracing_cpumask(cpumap);
index 897d11c8ca2e49677265bb605cda7dc21a6a3458..0d6b4c3b1a51107a47e0cb19992beebbaebda620 100644 (file)
@@ -3183,7 +3183,7 @@ static int setup_map_cpus(struct perf_sched *sched)
        if (!sched->map.cpus_str)
                return 0;
 
-       map = cpu_map__new(sched->map.cpus_str);
+       map = perf_cpu_map__new(sched->map.cpus_str);
        if (!map) {
                pr_err("failed to get cpus map from %s\n", sched->map.cpus_str);
                return -1;
@@ -3217,7 +3217,7 @@ static int setup_color_cpus(struct perf_sched *sched)
        if (!sched->map.color_cpus_str)
                return 0;
 
-       map = cpu_map__new(sched->map.color_cpus_str);
+       map = perf_cpu_map__new(sched->map.color_cpus_str);
        if (!map) {
                pr_err("failed to get thread map from %s\n", sched->map.color_cpus_str);
                return -1;
index f3cfb4c71106cdeb4a0df92e939c52816ed99473..a5d4f7ff71746a764286cff80b2b59df055e5f0f 100644 (file)
@@ -5,6 +5,10 @@
 #include <internal/cpumap.h>
 #include <asm/bug.h>
 #include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <limits.h>
 
 struct perf_cpu_map *perf_cpu_map__dummy_new(void)
 {
@@ -40,3 +44,183 @@ void perf_cpu_map__put(struct perf_cpu_map *map)
        if (map && refcount_dec_and_test(&map->refcnt))
                cpu_map__delete(map);
 }
+
+static struct perf_cpu_map *cpu_map__default_new(void)
+{
+       struct perf_cpu_map *cpus;
+       int nr_cpus;
+
+       nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
+       if (nr_cpus < 0)
+               return NULL;
+
+       cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
+       if (cpus != NULL) {
+               int i;
+
+               for (i = 0; i < nr_cpus; ++i)
+                       cpus->map[i] = i;
+
+               cpus->nr = nr_cpus;
+               refcount_set(&cpus->refcnt, 1);
+       }
+
+       return cpus;
+}
+
+static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
+{
+       size_t payload_size = nr_cpus * sizeof(int);
+       struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
+
+       if (cpus != NULL) {
+               cpus->nr = nr_cpus;
+               memcpy(cpus->map, tmp_cpus, payload_size);
+               refcount_set(&cpus->refcnt, 1);
+       }
+
+       return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__read(FILE *file)
+{
+       struct perf_cpu_map *cpus = NULL;
+       int nr_cpus = 0;
+       int *tmp_cpus = NULL, *tmp;
+       int max_entries = 0;
+       int n, cpu, prev;
+       char sep;
+
+       sep = 0;
+       prev = -1;
+       for (;;) {
+               n = fscanf(file, "%u%c", &cpu, &sep);
+               if (n <= 0)
+                       break;
+               if (prev >= 0) {
+                       int new_max = nr_cpus + cpu - prev - 1;
+
+                       if (new_max >= max_entries) {
+                               max_entries = new_max + MAX_NR_CPUS / 2;
+                               tmp = realloc(tmp_cpus, max_entries * sizeof(int));
+                               if (tmp == NULL)
+                                       goto out_free_tmp;
+                               tmp_cpus = tmp;
+                       }
+
+                       while (++prev < cpu)
+                               tmp_cpus[nr_cpus++] = prev;
+               }
+               if (nr_cpus == max_entries) {
+                       max_entries += MAX_NR_CPUS;
+                       tmp = realloc(tmp_cpus, max_entries * sizeof(int));
+                       if (tmp == NULL)
+                               goto out_free_tmp;
+                       tmp_cpus = tmp;
+               }
+
+               tmp_cpus[nr_cpus++] = cpu;
+               if (n == 2 && sep == '-')
+                       prev = cpu;
+               else
+                       prev = -1;
+               if (n == 1 || sep == '\n')
+                       break;
+       }
+
+       if (nr_cpus > 0)
+               cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
+       else
+               cpus = cpu_map__default_new();
+out_free_tmp:
+       free(tmp_cpus);
+       return cpus;
+}
+
+static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
+{
+       struct perf_cpu_map *cpus = NULL;
+       FILE *onlnf;
+
+       onlnf = fopen("/sys/devices/system/cpu/online", "r");
+       if (!onlnf)
+               return cpu_map__default_new();
+
+       cpus = perf_cpu_map__read(onlnf);
+       fclose(onlnf);
+       return cpus;
+}
+
+struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
+{
+       struct perf_cpu_map *cpus = NULL;
+       unsigned long start_cpu, end_cpu = 0;
+       char *p = NULL;
+       int i, nr_cpus = 0;
+       int *tmp_cpus = NULL, *tmp;
+       int max_entries = 0;
+
+       if (!cpu_list)
+               return cpu_map__read_all_cpu_map();
+
+       /*
+        * must handle the case of empty cpumap to cover
+        * TOPOLOGY header for NUMA nodes with no CPU
+        * ( e.g., because of CPU hotplug)
+        */
+       if (!isdigit(*cpu_list) && *cpu_list != '\0')
+               goto out;
+
+       while (isdigit(*cpu_list)) {
+               p = NULL;
+               start_cpu = strtoul(cpu_list, &p, 0);
+               if (start_cpu >= INT_MAX
+                   || (*p != '\0' && *p != ',' && *p != '-'))
+                       goto invalid;
+
+               if (*p == '-') {
+                       cpu_list = ++p;
+                       p = NULL;
+                       end_cpu = strtoul(cpu_list, &p, 0);
+
+                       if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
+                               goto invalid;
+
+                       if (end_cpu < start_cpu)
+                               goto invalid;
+               } else {
+                       end_cpu = start_cpu;
+               }
+
+               for (; start_cpu <= end_cpu; start_cpu++) {
+                       /* check for duplicates */
+                       for (i = 0; i < nr_cpus; i++)
+                               if (tmp_cpus[i] == (int)start_cpu)
+                                       goto invalid;
+
+                       if (nr_cpus == max_entries) {
+                               max_entries += MAX_NR_CPUS;
+                               tmp = realloc(tmp_cpus, max_entries * sizeof(int));
+                               if (tmp == NULL)
+                                       goto invalid;
+                               tmp_cpus = tmp;
+                       }
+                       tmp_cpus[nr_cpus++] = (int)start_cpu;
+               }
+               if (*p)
+                       ++p;
+
+               cpu_list = p;
+       }
+
+       if (nr_cpus > 0)
+               cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
+       else if (*cpu_list != '\0')
+               cpus = cpu_map__default_new();
+       else
+               cpus = perf_cpu_map__dummy_new();
+invalid:
+       free(tmp_cpus);
+out:
+       return cpus;
+}
index 53ce95374b053564c90f1542c6d8c5239be68c0a..3306319f7df86ca7b495ac0e590006afe33af66e 100644 (file)
@@ -10,4 +10,8 @@ struct perf_cpu_map {
        int             map[];
 };
 
+#ifndef MAX_NR_CPUS
+#define MAX_NR_CPUS    2048
+#endif
+
 #endif /* __LIBPERF_INTERNAL_CPUMAP_H */
index e16c2515a4992f177fb996399354a93303d73727..b4a9283a5dfa52a6394a6ceef2841d7c39913e59 100644 (file)
@@ -3,10 +3,13 @@
 #define __LIBPERF_CPUMAP_H
 
 #include <perf/core.h>
+#include <stdio.h>
 
 struct perf_cpu_map;
 
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__dummy_new(void);
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list);
+LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file);
 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
 LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map);
 
index 168339f89a2e34a16ff8e9862a7795ebddb6ede4..e38473a8f32fee9c0547c8af8d08491ca3df4340 100644 (file)
@@ -4,6 +4,8 @@ LIBPERF_0.0.1 {
                perf_cpu_map__dummy_new;
                perf_cpu_map__get;
                perf_cpu_map__put;
+               perf_cpu_map__new;
+               perf_cpu_map__read;
                perf_thread_map__new_dummy;
                perf_thread_map__set_pid;
                perf_thread_map__comm;
index 95304d29092e80a4290923df010eec0460f38a60..db2aadff370898d849155144dfb8ec27f34da140 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/compiler.h>
 #include <linux/bitmap.h>
+#include <perf/cpumap.h>
 #include "tests.h"
 #include "cpumap.h"
 #include "debug.h"
@@ -9,7 +10,7 @@
 
 static unsigned long *get_bitmap(const char *str, int nbits)
 {
-       struct perf_cpu_map *map = cpu_map__new(str);
+       struct perf_cpu_map *map = perf_cpu_map__new(str);
        unsigned long *bm = NULL;
        int i;
 
index 131bbeec62d2d96d1c4d147ebb72bc70c8038748..bfaf22c2023c47748a312439867a530119182abc 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/param.h>
+#include <perf/cpumap.h>
 
 #include "parse-events.h"
 #include "evlist.h"
@@ -613,9 +614,9 @@ static int do_test_code_reading(bool try_kcore)
                goto out_put;
        }
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (!cpus) {
-               pr_debug("cpu_map__new failed\n");
+               pr_debug("perf_cpu_map__new failed\n");
                goto out_put;
        }
 
index 6c921087b0fe8edc02da2d07eaf9a3430914e5db..b71fe09a8087f26f8823867583d5fda936f1d52b 100644 (file)
@@ -5,6 +5,7 @@
 #include "event.h"
 #include <string.h>
 #include <linux/bitops.h>
+#include <perf/cpumap.h>
 #include "debug.h"
 
 struct machine;
@@ -78,7 +79,7 @@ int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __may
        struct perf_cpu_map *cpus;
 
        /* This one is better stores in mask. */
-       cpus = cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
+       cpus = perf_cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
 
        TEST_ASSERT_VAL("failed to synthesize map",
                !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
@@ -86,7 +87,7 @@ int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __may
        perf_cpu_map__put(cpus);
 
        /* This one is better stores in cpu values. */
-       cpus = cpu_map__new("1,256");
+       cpus = perf_cpu_map__new("1,256");
 
        TEST_ASSERT_VAL("failed to synthesize map",
                !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
@@ -97,7 +98,7 @@ int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __may
 
 static int cpu_map_print(const char *str)
 {
-       struct perf_cpu_map *map = cpu_map__new(str);
+       struct perf_cpu_map *map = perf_cpu_map__new(str);
        char buf[100];
 
        if (!map)
index 165534f620360d5a420a15ad560a9ed6b65f8ada..00adba86403ba3aa7c7500a2e880f509802f524e 100644 (file)
@@ -4,6 +4,7 @@
 #include <inttypes.h>
 #include <string.h>
 #include <sys/wait.h>
+#include <perf/cpumap.h>
 #include "tests.h"
 #include "evlist.h"
 #include "evsel.h"
@@ -115,9 +116,9 @@ static int attach__cpu_disabled(struct evlist *evlist)
 
        pr_debug("attaching to CPU 0 as enabled\n");
 
-       cpus = cpu_map__new("0");
+       cpus = perf_cpu_map__new("0");
        if (cpus == NULL) {
-               pr_debug("failed to call cpu_map__new\n");
+               pr_debug("failed to call perf_cpu_map__new\n");
                return -1;
        }
 
@@ -144,9 +145,9 @@ static int attach__cpu_enabled(struct evlist *evlist)
 
        pr_debug("attaching to CPU 0 as enabled\n");
 
-       cpus = cpu_map__new("0");
+       cpus = perf_cpu_map__new("0");
        if (cpus == NULL) {
-               pr_debug("failed to call cpu_map__new\n");
+               pr_debug("failed to call perf_cpu_map__new\n");
                return -1;
        }
 
index 415d12e96834d5c12789fd74e874408d104e2378..2bc5145284c0f44cf7581576036e7748affc7605 100644 (file)
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/compiler.h>
+#include <perf/cpumap.h>
 #include "evlist.h"
 #include "evsel.h"
 #include "machine.h"
@@ -108,7 +109,7 @@ int test__event_update(struct test *test __maybe_unused, int subtest __maybe_unu
        TEST_ASSERT_VAL("failed to synthesize attr update name",
                        !perf_event__synthesize_event_update_name(&tmp.tool, evsel, process_event_name));
 
-       evsel->own_cpus = cpu_map__new("1,2,3");
+       evsel->own_cpus = perf_cpu_map__new("1,2,3");
 
        TEST_ASSERT_VAL("failed to synthesize attr update cpus",
                        !perf_event__synthesize_event_update_cpus(&tmp.tool, evsel, process_event_cpus));
index 4fc7b3b4e1537198dda2645edf05cccd176e0726..46478ba1ed1671ee80e18b46c39f4ae3a59ec09d 100644 (file)
@@ -2,6 +2,7 @@
 #include <linux/types.h>
 #include <unistd.h>
 #include <sys/prctl.h>
+#include <perf/cpumap.h>
 
 #include "parse-events.h"
 #include "evlist.h"
@@ -75,7 +76,7 @@ int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_un
        threads = thread_map__new(-1, getpid(), UINT_MAX);
        CHECK_NOT_NULL__(threads);
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        CHECK_NOT_NULL__(cpus);
 
        evlist = evlist__new();
index 6fe2c1e7918be34e79b34fb929b0749eda32dc06..5ec193f7968ddbb089a8f84ce371a1ffb5947c4e 100644 (file)
@@ -2,6 +2,7 @@
 #include <linux/compiler.h>
 #include <linux/bitmap.h>
 #include <linux/zalloc.h>
+#include <perf/cpumap.h>
 #include "cpumap.h"
 #include "mem2node.h"
 #include "tests.h"
@@ -19,7 +20,7 @@ static struct node {
 
 static unsigned long *get_bitmap(const char *str, int nbits)
 {
-       struct perf_cpu_map *map = cpu_map__new(str);
+       struct perf_cpu_map *map = perf_cpu_map__new(str);
        unsigned long *bm = NULL;
        int i;
 
index 9d8eb43b12cb60e8721cb8be027b5e4c62953ef5..aa792aebd7f030fb9a1ff6bcab8c155e3f60be52 100644 (file)
@@ -3,6 +3,7 @@
 #include <inttypes.h>
 /* For the CLR_() macros */
 #include <pthread.h>
+#include <perf/cpumap.h>
 
 #include "evlist.h"
 #include "evsel.h"
@@ -46,7 +47,7 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
                return -1;
        }
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (cpus == NULL) {
                pr_debug("cpu_map__new\n");
                goto out_free_threads;
index 674b0fa035ec1c1b219aaa7dc59f8030f1cd367e..d161b1a78703a4946135d475eac7673593da525e 100644 (file)
@@ -33,7 +33,7 @@ int test__openat_syscall_event_on_all_cpus(struct test *test __maybe_unused, int
                return -1;
        }
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (cpus == NULL) {
                pr_debug("cpu_map__new\n");
                goto out_thread_map_delete;
index dd07acced4afb4cbcb0f3d950325d02964771583..9e0bbea15005d638d0140d517a0e4eed61c232ed 100644 (file)
@@ -5,6 +5,7 @@
 #include <time.h>
 #include <stdlib.h>
 #include <linux/zalloc.h>
+#include <perf/cpumap.h>
 
 #include "parse-events.h"
 #include "evlist.h"
@@ -341,9 +342,9 @@ int test__switch_tracking(struct test *test __maybe_unused, int subtest __maybe_
                goto out_err;
        }
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (!cpus) {
-               pr_debug("cpu_map__new failed!\n");
+               pr_debug("perf_cpu_map__new failed!\n");
                goto out_err;
        }
 
index 1b57ded58d594c6a84b50796981b54f481098c5a..a4f9f5182b47ccab1b0b69d5fe33cc7d1611b58a 100644 (file)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <perf/cpumap.h>
 #include "tests.h"
 #include "util.h"
 #include "session.h"
@@ -126,7 +127,7 @@ int test__session_topology(struct test *test __maybe_unused, int subtest __maybe
        if (session_write_header(path))
                goto free_path;
 
-       map = cpu_map__new(NULL);
+       map = perf_cpu_map__new(NULL);
        if (map == NULL) {
                pr_debug("failed to get system cpumap\n");
                goto free_path;
index 44082e5eabdea32bb7cb6ce0b44c54c2a39df664..71d4d7b35a57d0fd8d0a66411e6cf943198d6574 100644 (file)
@@ -17,185 +17,6 @@ static int max_present_cpu_num;
 static int max_node_num;
 static int *cpunode_map;
 
-static struct perf_cpu_map *cpu_map__default_new(void)
-{
-       struct perf_cpu_map *cpus;
-       int nr_cpus;
-
-       nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
-       if (nr_cpus < 0)
-               return NULL;
-
-       cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
-       if (cpus != NULL) {
-               int i;
-               for (i = 0; i < nr_cpus; ++i)
-                       cpus->map[i] = i;
-
-               cpus->nr = nr_cpus;
-               refcount_set(&cpus->refcnt, 1);
-       }
-
-       return cpus;
-}
-
-static struct perf_cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
-{
-       size_t payload_size = nr_cpus * sizeof(int);
-       struct perf_cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
-
-       if (cpus != NULL) {
-               cpus->nr = nr_cpus;
-               memcpy(cpus->map, tmp_cpus, payload_size);
-               refcount_set(&cpus->refcnt, 1);
-       }
-
-       return cpus;
-}
-
-struct perf_cpu_map *cpu_map__read(FILE *file)
-{
-       struct perf_cpu_map *cpus = NULL;
-       int nr_cpus = 0;
-       int *tmp_cpus = NULL, *tmp;
-       int max_entries = 0;
-       int n, cpu, prev;
-       char sep;
-
-       sep = 0;
-       prev = -1;
-       for (;;) {
-               n = fscanf(file, "%u%c", &cpu, &sep);
-               if (n <= 0)
-                       break;
-               if (prev >= 0) {
-                       int new_max = nr_cpus + cpu - prev - 1;
-
-                       if (new_max >= max_entries) {
-                               max_entries = new_max + MAX_NR_CPUS / 2;
-                               tmp = realloc(tmp_cpus, max_entries * sizeof(int));
-                               if (tmp == NULL)
-                                       goto out_free_tmp;
-                               tmp_cpus = tmp;
-                       }
-
-                       while (++prev < cpu)
-                               tmp_cpus[nr_cpus++] = prev;
-               }
-               if (nr_cpus == max_entries) {
-                       max_entries += MAX_NR_CPUS;
-                       tmp = realloc(tmp_cpus, max_entries * sizeof(int));
-                       if (tmp == NULL)
-                               goto out_free_tmp;
-                       tmp_cpus = tmp;
-               }
-
-               tmp_cpus[nr_cpus++] = cpu;
-               if (n == 2 && sep == '-')
-                       prev = cpu;
-               else
-                       prev = -1;
-               if (n == 1 || sep == '\n')
-                       break;
-       }
-
-       if (nr_cpus > 0)
-               cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-       else
-               cpus = cpu_map__default_new();
-out_free_tmp:
-       free(tmp_cpus);
-       return cpus;
-}
-
-static struct perf_cpu_map *cpu_map__read_all_cpu_map(void)
-{
-       struct perf_cpu_map *cpus = NULL;
-       FILE *onlnf;
-
-       onlnf = fopen("/sys/devices/system/cpu/online", "r");
-       if (!onlnf)
-               return cpu_map__default_new();
-
-       cpus = cpu_map__read(onlnf);
-       fclose(onlnf);
-       return cpus;
-}
-
-struct perf_cpu_map *cpu_map__new(const char *cpu_list)
-{
-       struct perf_cpu_map *cpus = NULL;
-       unsigned long start_cpu, end_cpu = 0;
-       char *p = NULL;
-       int i, nr_cpus = 0;
-       int *tmp_cpus = NULL, *tmp;
-       int max_entries = 0;
-
-       if (!cpu_list)
-               return cpu_map__read_all_cpu_map();
-
-       /*
-        * must handle the case of empty cpumap to cover
-        * TOPOLOGY header for NUMA nodes with no CPU
-        * ( e.g., because of CPU hotplug)
-        */
-       if (!isdigit(*cpu_list) && *cpu_list != '\0')
-               goto out;
-
-       while (isdigit(*cpu_list)) {
-               p = NULL;
-               start_cpu = strtoul(cpu_list, &p, 0);
-               if (start_cpu >= INT_MAX
-                   || (*p != '\0' && *p != ',' && *p != '-'))
-                       goto invalid;
-
-               if (*p == '-') {
-                       cpu_list = ++p;
-                       p = NULL;
-                       end_cpu = strtoul(cpu_list, &p, 0);
-
-                       if (end_cpu >= INT_MAX || (*p != '\0' && *p != ','))
-                               goto invalid;
-
-                       if (end_cpu < start_cpu)
-                               goto invalid;
-               } else {
-                       end_cpu = start_cpu;
-               }
-
-               for (; start_cpu <= end_cpu; start_cpu++) {
-                       /* check for duplicates */
-                       for (i = 0; i < nr_cpus; i++)
-                               if (tmp_cpus[i] == (int)start_cpu)
-                                       goto invalid;
-
-                       if (nr_cpus == max_entries) {
-                               max_entries += MAX_NR_CPUS;
-                               tmp = realloc(tmp_cpus, max_entries * sizeof(int));
-                               if (tmp == NULL)
-                                       goto invalid;
-                               tmp_cpus = tmp;
-                       }
-                       tmp_cpus[nr_cpus++] = (int)start_cpu;
-               }
-               if (*p)
-                       ++p;
-
-               cpu_list = p;
-       }
-
-       if (nr_cpus > 0)
-               cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
-       else if (*cpu_list != '\0')
-               cpus = cpu_map__default_new();
-       else
-               cpus = perf_cpu_map__dummy_new();
-invalid:
-       free(tmp_cpus);
-out:
-       return cpus;
-}
-
 static struct perf_cpu_map *cpu_map__from_entries(struct cpu_map_entries *cpus)
 {
        struct perf_cpu_map *map;
@@ -751,7 +572,7 @@ const struct perf_cpu_map *cpu_map__online(void) /* thread unsafe */
        static const struct perf_cpu_map *online = NULL;
 
        if (!online)
-               online = cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
+               online = perf_cpu_map__new(NULL); /* from /sys/devices/system/cpu/online */
 
        return online;
 }
index b7af2cb68c1948276e325a8dfa7b86d6ed7bb6cc..a3d27f4131be96cbd871886c145a01425bf38735 100644 (file)
 #include "perf.h"
 #include "util/debug.h"
 
-struct perf_cpu_map *cpu_map__new(const char *cpu_list);
 struct perf_cpu_map *cpu_map__empty_new(int nr);
 struct perf_cpu_map *cpu_map__new_data(struct cpu_map_data *data);
-struct perf_cpu_map *cpu_map__read(FILE *file);
 size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size);
 size_t cpu_map__snprint_mask(struct perf_cpu_map *map, char *buf, size_t size);
 size_t cpu_map__fprintf(struct perf_cpu_map *map, FILE *fp);
index 0cd99c460cd4740036221524fd1515c6f9ad19d1..4f70155eaf83be67033f85511b0f7f3bf0de388d 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <api/fs/fs.h>
 #include <linux/zalloc.h>
+#include <perf/cpumap.h>
 
 #include "cputopo.h"
 #include "cpumap.h"
@@ -182,7 +183,7 @@ struct cpu_topology *cpu_topology__new(void)
        ncpus = cpu__max_present_cpu();
 
        /* build online CPU map */
-       map = cpu_map__new(NULL);
+       map = perf_cpu_map__new(NULL);
        if (map == NULL) {
                pr_debug("failed to get system cpumap\n");
                return NULL;
@@ -312,7 +313,7 @@ struct numa_topology *numa_topology__new(void)
        if (c)
                *c = '\0';
 
-       node_map = cpu_map__new(buf);
+       node_map = perf_cpu_map__new(buf);
        if (!node_map)
                goto out;
 
index ae75777a0ba497be35a8f93b9c3adf22e5f61591..67c67e9a38cd6ff2c0e7e645a8612c8227216800 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/err.h>
 #include <linux/zalloc.h>
 #include <perf/evlist.h>
+#include <perf/cpumap.h>
 
 #ifdef LACKS_SIGQUEUE_PROTOTYPE
 int sigqueue(pid_t pid, int sig, const union sigval value);
@@ -1089,7 +1090,7 @@ int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
        if (target__uses_dummy_map(target))
                cpus = perf_cpu_map__dummy_new();
        else
-               cpus = cpu_map__new(target->cpu_list);
+               cpus = perf_cpu_map__new(target->cpu_list);
 
        if (!cpus)
                goto out_delete_threads;
@@ -1372,7 +1373,7 @@ static int perf_evlist__create_syswide_maps(struct evlist *evlist)
         * error, and we may not want to do that fallback to a
         * default cpu identity map :-\
         */
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (!cpus)
                goto out;
 
index d81afe56392c98dee7a8e1797f5407cc8f869ad5..fa914ba8cd56aabcf0a1898e8921bdd1c3dbf4ac 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/time64.h>
 #include <dirent.h>
 #include <bpf/libbpf.h>
+#include <perf/cpumap.h>
 
 #include "evlist.h"
 #include "evsel.h"
@@ -2348,7 +2349,7 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
                if (!str)
                        goto error;
 
-               n->map = cpu_map__new(str);
+               n->map = perf_cpu_map__new(str);
                if (!n->map)
                        goto error;
 
index ec7ce18b999a403eca203261506b70380c7f6846..db2460d6b62585a2ca5159f562421eeb27b59eb7 100644 (file)
@@ -24,6 +24,7 @@
 #include "bpf-loader.h"
 #include "debug.h"
 #include <api/fs/tracing_path.h>
+#include <perf/cpumap.h>
 #include "parse-events-bison.h"
 #define YY_EXTRA_TYPE int
 #include "parse-events-flex.h"
@@ -323,7 +324,7 @@ __add_event(struct list_head *list, int *idx,
 {
        struct evsel *evsel;
        struct perf_cpu_map *cpus = pmu ? pmu->cpus :
-                              cpu_list ? cpu_map__new(cpu_list) : NULL;
+                              cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
 
        event_attr_init(attr);
 
index d355f9506a1c26b0e50d7a419830fc277ea428a1..b7da21a7d6278f6b2843d3472a172f3b62860206 100644 (file)
@@ -15,6 +15,7 @@
 #include <api/fs/fs.h>
 #include <locale.h>
 #include <regex.h>
+#include <perf/cpumap.h>
 #include "pmu.h"
 #include "parse-events.h"
 #include "cpumap.h"
@@ -581,7 +582,7 @@ static struct perf_cpu_map *__pmu_cpumask(const char *path)
        if (!file)
                return NULL;
 
-       cpus = cpu_map__read(file);
+       cpus = perf_cpu_map__read(file);
        fclose(file);
        return cpus;
 }
index 23a4fa13b92d8da67d714fe1bc905bc9961496d5..75ecc32a4427b805b7d041bc139f50f84eb3f38b 100644 (file)
@@ -4,6 +4,7 @@
 #include <inttypes.h>
 #include <poll.h>
 #include <linux/err.h>
+#include <perf/cpumap.h>
 #include "evlist.h"
 #include "callchain.h"
 #include "evsel.h"
@@ -549,7 +550,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
                                         kwlist, &cpustr))
                return -1;
 
-       pcpus->cpus = cpu_map__new(cpustr);
+       pcpus->cpus = perf_cpu_map__new(cpustr);
        if (pcpus->cpus == NULL)
                return -1;
        return 0;
index 445788819969cdd81d6c070cdfa61b5827c9f0ae..03dcdb3f33a76b3dd1d212891bba33315ca41009 100644 (file)
@@ -6,6 +6,7 @@
 #include <errno.h>
 #include <api/fs/fs.h>
 #include <subcmd/parse-options.h>
+#include <perf/cpumap.h>
 #include "util.h"
 #include "cloexec.h"
 
@@ -63,7 +64,7 @@ static bool perf_probe_api(setup_probe_fn_t fn)
        struct perf_cpu_map *cpus;
        int cpu, ret, i = 0;
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (!cpus)
                return false;
        cpu = cpus->map[0];
@@ -118,7 +119,7 @@ bool perf_can_record_cpu_wide(void)
        struct perf_cpu_map *cpus;
        int cpu, fd;
 
-       cpus = cpu_map__new(NULL);
+       cpus = perf_cpu_map__new(NULL);
        if (!cpus)
                return false;
        cpu = cpus->map[0];
@@ -275,7 +276,7 @@ bool perf_evlist__can_select_event(struct evlist *evlist, const char *str)
        evsel = perf_evlist__last(temp_evlist);
 
        if (!evlist || cpu_map__empty(evlist->cpus)) {
-               struct perf_cpu_map *cpus = cpu_map__new(NULL);
+               struct perf_cpu_map *cpus = perf_cpu_map__new(NULL);
 
                cpu =  cpus ? cpus->map[0] : 0;
                perf_cpu_map__put(cpus);
index 1f3dc7a8cee6e5106b486c21be1fa74e5ddb34a2..11e6093c941b85ac36ff30a881086f0ed5204b09 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/mman.h>
+#include <perf/cpumap.h>
 
 #include "evlist.h"
 #include "evsel.h"
@@ -2289,7 +2290,7 @@ int perf_session__cpu_bitmap(struct perf_session *session,
                }
        }
 
-       map = cpu_map__new(cpu_list);
+       map = perf_cpu_map__new(cpu_list);
        if (map == NULL) {
                pr_err("Invalid cpu_list\n");
                return -1;
index a9ca5c4fffeed0016fa1bbe43bc7a45fcafca994..ae6a534a7a8087114e031494f3635a40b92577eb 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/bitmap.h>
 #include <linux/time64.h>
 #include <linux/zalloc.h>
+#include <perf/cpumap.h>
 
 #include "perf.h"
 #include "svghelper.h"
@@ -731,7 +732,7 @@ static int str_to_bitmap(char *s, cpumask_t *b)
        struct perf_cpu_map *m;
        int c;
 
-       m = cpu_map__new(s);
+       m = perf_cpu_map__new(s);
        if (!m)
                return -1;