Skip to content

Commit f30a79b

Browse files
olsajiriacmel
authored andcommitted
perf tools: Add reference counting for cpu_map object
Adding refference counting for cpu_map object, so it could be easily shared among other objects. Using cpu_map__put instead cpu_map__delete and making cpu_map__delete static. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1435012588-9007-4-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 4cc9761 commit f30a79b

13 files changed

+45
-15
lines changed

tools/perf/tests/code-reading.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ static int do_test_code_reading(bool try_kcore)
545545
if (evlist) {
546546
perf_evlist__delete(evlist);
547547
} else {
548-
cpu_map__delete(cpus);
548+
cpu_map__put(cpus);
549549
thread_map__delete(threads);
550550
}
551551
machines__destroy_kernel_maps(&machines);

tools/perf/tests/keep-tracking.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ int test__keep_tracking(void)
144144
perf_evlist__disable(evlist);
145145
perf_evlist__delete(evlist);
146146
} else {
147-
cpu_map__delete(cpus);
147+
cpu_map__put(cpus);
148148
thread_map__delete(threads);
149149
}
150150

tools/perf/tests/mmap-basic.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int test__basic_mmap(void)
140140
cpus = NULL;
141141
threads = NULL;
142142
out_free_cpus:
143-
cpu_map__delete(cpus);
143+
cpu_map__put(cpus);
144144
out_free_threads:
145145
thread_map__delete(threads);
146146
return err;

tools/perf/tests/switch-tracking.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ int test__switch_tracking(void)
560560
perf_evlist__disable(evlist);
561561
perf_evlist__delete(evlist);
562562
} else {
563-
cpu_map__delete(cpus);
563+
cpu_map__put(cpus);
564564
thread_map__delete(threads);
565565
}
566566

tools/perf/util/cpumap.c

+24-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <assert.h>
66
#include <stdio.h>
77
#include <stdlib.h>
8+
#include "asm/bug.h"
89

910
static struct cpu_map *cpu_map__default_new(void)
1011
{
@@ -22,6 +23,7 @@ static struct cpu_map *cpu_map__default_new(void)
2223
cpus->map[i] = i;
2324

2425
cpus->nr = nr_cpus;
26+
atomic_set(&cpus->refcnt, 1);
2527
}
2628

2729
return cpus;
@@ -35,6 +37,7 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
3537
if (cpus != NULL) {
3638
cpus->nr = nr_cpus;
3739
memcpy(cpus->map, tmp_cpus, payload_size);
40+
atomic_set(&cpus->refcnt, 1);
3841
}
3942

4043
return cpus;
@@ -194,14 +197,32 @@ struct cpu_map *cpu_map__dummy_new(void)
194197
if (cpus != NULL) {
195198
cpus->nr = 1;
196199
cpus->map[0] = -1;
200+
atomic_set(&cpus->refcnt, 1);
197201
}
198202

199203
return cpus;
200204
}
201205

202-
void cpu_map__delete(struct cpu_map *map)
206+
static void cpu_map__delete(struct cpu_map *map)
203207
{
204-
free(map);
208+
if (map) {
209+
WARN_ONCE(atomic_read(&map->refcnt) != 0,
210+
"cpu_map refcnt unbalanced\n");
211+
free(map);
212+
}
213+
}
214+
215+
struct cpu_map *cpu_map__get(struct cpu_map *map)
216+
{
217+
if (map)
218+
atomic_inc(&map->refcnt);
219+
return map;
220+
}
221+
222+
void cpu_map__put(struct cpu_map *map)
223+
{
224+
if (map && atomic_dec_and_test(&map->refcnt))
225+
cpu_map__delete(map);
205226
}
206227

207228
int cpu_map__get_socket(struct cpu_map *map, int idx)
@@ -263,6 +284,7 @@ static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
263284
/* ensure we process id in increasing order */
264285
qsort(c->map, c->nr, sizeof(int), cmp_ids);
265286

287+
atomic_set(&cpus->refcnt, 1);
266288
*res = c;
267289
return 0;
268290
}

tools/perf/util/cpumap.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33

44
#include <stdio.h>
55
#include <stdbool.h>
6+
#include <linux/atomic.h>
67

78
#include "perf.h"
89
#include "util/debug.h"
910

1011
struct cpu_map {
12+
atomic_t refcnt;
1113
int nr;
1214
int map[];
1315
};
1416

1517
struct cpu_map *cpu_map__new(const char *cpu_list);
1618
struct cpu_map *cpu_map__dummy_new(void);
17-
void cpu_map__delete(struct cpu_map *map);
1819
struct cpu_map *cpu_map__read(FILE *file);
1920
size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp);
2021
int cpu_map__get_socket(struct cpu_map *map, int idx);
2122
int cpu_map__get_core(struct cpu_map *map, int idx);
2223
int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp);
2324
int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep);
2425

26+
struct cpu_map *cpu_map__get(struct cpu_map *map);
27+
void cpu_map__put(struct cpu_map *map);
28+
2529
static inline int cpu_map__socket(struct cpu_map *sock, int s)
2630
{
2731
if (!sock || s > sock->nr || s < 0)

tools/perf/util/evlist.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void perf_evlist__delete(struct perf_evlist *evlist)
114114
{
115115
perf_evlist__munmap(evlist);
116116
perf_evlist__close(evlist);
117-
cpu_map__delete(evlist->cpus);
117+
cpu_map__put(evlist->cpus);
118118
thread_map__delete(evlist->threads);
119119
evlist->cpus = NULL;
120120
evlist->threads = NULL;
@@ -1353,7 +1353,7 @@ static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
13531353
out:
13541354
return err;
13551355
out_free_cpus:
1356-
cpu_map__delete(evlist->cpus);
1356+
cpu_map__put(evlist->cpus);
13571357
evlist->cpus = NULL;
13581358
goto out;
13591359
}

tools/perf/util/evsel.c

+1
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ void perf_evsel__exit(struct perf_evsel *evsel)
885885
perf_evsel__free_fd(evsel);
886886
perf_evsel__free_id(evsel);
887887
close_cgroup(evsel->cgrp);
888+
cpu_map__put(evsel->cpus);
888889
zfree(&evsel->group_name);
889890
zfree(&evsel->name);
890891
perf_evsel__object.fini(evsel);

tools/perf/util/parse-events.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "parse-events-flex.h"
1818
#include "pmu.h"
1919
#include "thread_map.h"
20+
#include "cpumap.h"
2021
#include "asm/bug.h"
2122

2223
#define MAX_NAME_LEN 100
@@ -285,7 +286,9 @@ __add_event(struct list_head *list, int *idx,
285286
if (!evsel)
286287
return NULL;
287288

288-
evsel->cpus = cpus;
289+
if (cpus)
290+
evsel->cpus = cpu_map__get(cpus);
291+
289292
if (name)
290293
evsel->name = strdup(name);
291294
list_add_tail(&evsel->node, list);

tools/perf/util/python.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
384384

385385
static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
386386
{
387-
cpu_map__delete(pcpus->cpus);
387+
cpu_map__put(pcpus->cpus);
388388
pcpus->ob_type->tp_free((PyObject*)pcpus);
389389
}
390390

tools/perf/util/record.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static bool perf_probe_api(setup_probe_fn_t fn)
6464
if (!cpus)
6565
return false;
6666
cpu = cpus->map[0];
67-
cpu_map__delete(cpus);
67+
cpu_map__put(cpus);
6868

6969
do {
7070
ret = perf_do_probe_api(fn, cpu, try[i++]);
@@ -226,7 +226,7 @@ bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
226226
struct cpu_map *cpus = cpu_map__new(NULL);
227227

228228
cpu = cpus ? cpus->map[0] : 0;
229-
cpu_map__delete(cpus);
229+
cpu_map__put(cpus);
230230
} else {
231231
cpu = evlist->cpus->map[0];
232232
}

tools/perf/util/session.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ int perf_session__cpu_bitmap(struct perf_session *session,
18951895
err = 0;
18961896

18971897
out_delete_map:
1898-
cpu_map__delete(map);
1898+
cpu_map__put(map);
18991899
return err;
19001900
}
19011901

tools/perf/util/svghelper.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ static int str_to_bitmap(char *s, cpumask_t *b)
748748
set_bit(c, cpumask_bits(b));
749749
}
750750

751-
cpu_map__delete(m);
751+
cpu_map__put(m);
752752

753753
return ret;
754754
}

0 commit comments

Comments
 (0)