libperf: Add perf_cpu_map__new()/perf_cpu_map__read() functions
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>
This commit is contained in:

committed by
Arnaldo Carvalho de Melo

parent
1fc632cef4
commit
9c3516d1b8
@@ -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;
|
||||
}
|
||||
|
@@ -11,10 +11,8 @@
|
||||
#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);
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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);
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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);
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user