diff options
author | Jiri Olsa <jolsa@kernel.org> | 2015-06-22 18:36:04 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-06-25 14:15:50 -0400 |
commit | f30a79b012e5d9b3887f6a59293d9ef3ca0e2c3e (patch) | |
tree | 3a7dcc611645c5a3e1849b63621b9acfb29b523a /tools/perf | |
parent | 4cc97614812e96c135e369f3d723fcda07d33437 (diff) |
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>
Diffstat (limited to 'tools/perf')
-rw-r--r-- | tools/perf/tests/code-reading.c | 2 | ||||
-rw-r--r-- | tools/perf/tests/keep-tracking.c | 2 | ||||
-rw-r--r-- | tools/perf/tests/mmap-basic.c | 2 | ||||
-rw-r--r-- | tools/perf/tests/switch-tracking.c | 2 | ||||
-rw-r--r-- | tools/perf/util/cpumap.c | 26 | ||||
-rw-r--r-- | tools/perf/util/cpumap.h | 6 | ||||
-rw-r--r-- | tools/perf/util/evlist.c | 4 | ||||
-rw-r--r-- | tools/perf/util/evsel.c | 1 | ||||
-rw-r--r-- | tools/perf/util/parse-events.c | 5 | ||||
-rw-r--r-- | tools/perf/util/python.c | 2 | ||||
-rw-r--r-- | tools/perf/util/record.c | 4 | ||||
-rw-r--r-- | tools/perf/util/session.c | 2 | ||||
-rw-r--r-- | tools/perf/util/svghelper.c | 2 |
13 files changed, 45 insertions, 15 deletions
diff --git a/tools/perf/tests/code-reading.c b/tools/perf/tests/code-reading.c index 22f8a00446e1..6b3250f54240 100644 --- a/tools/perf/tests/code-reading.c +++ b/tools/perf/tests/code-reading.c | |||
@@ -545,7 +545,7 @@ out_err: | |||
545 | if (evlist) { | 545 | if (evlist) { |
546 | perf_evlist__delete(evlist); | 546 | perf_evlist__delete(evlist); |
547 | } else { | 547 | } else { |
548 | cpu_map__delete(cpus); | 548 | cpu_map__put(cpus); |
549 | thread_map__delete(threads); | 549 | thread_map__delete(threads); |
550 | } | 550 | } |
551 | machines__destroy_kernel_maps(&machines); | 551 | machines__destroy_kernel_maps(&machines); |
diff --git a/tools/perf/tests/keep-tracking.c b/tools/perf/tests/keep-tracking.c index 5b171d1e338b..a330235cefc0 100644 --- a/tools/perf/tests/keep-tracking.c +++ b/tools/perf/tests/keep-tracking.c | |||
@@ -144,7 +144,7 @@ out_err: | |||
144 | perf_evlist__disable(evlist); | 144 | perf_evlist__disable(evlist); |
145 | perf_evlist__delete(evlist); | 145 | perf_evlist__delete(evlist); |
146 | } else { | 146 | } else { |
147 | cpu_map__delete(cpus); | 147 | cpu_map__put(cpus); |
148 | thread_map__delete(threads); | 148 | thread_map__delete(threads); |
149 | } | 149 | } |
150 | 150 | ||
diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index 5855cf471210..5a9ef5833452 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c | |||
@@ -140,7 +140,7 @@ out_delete_evlist: | |||
140 | cpus = NULL; | 140 | cpus = NULL; |
141 | threads = NULL; | 141 | threads = NULL; |
142 | out_free_cpus: | 142 | out_free_cpus: |
143 | cpu_map__delete(cpus); | 143 | cpu_map__put(cpus); |
144 | out_free_threads: | 144 | out_free_threads: |
145 | thread_map__delete(threads); | 145 | thread_map__delete(threads); |
146 | return err; | 146 | return err; |
diff --git a/tools/perf/tests/switch-tracking.c b/tools/perf/tests/switch-tracking.c index 0d31403ea593..1b06122beb76 100644 --- a/tools/perf/tests/switch-tracking.c +++ b/tools/perf/tests/switch-tracking.c | |||
@@ -560,7 +560,7 @@ out: | |||
560 | perf_evlist__disable(evlist); | 560 | perf_evlist__disable(evlist); |
561 | perf_evlist__delete(evlist); | 561 | perf_evlist__delete(evlist); |
562 | } else { | 562 | } else { |
563 | cpu_map__delete(cpus); | 563 | cpu_map__put(cpus); |
564 | thread_map__delete(threads); | 564 | thread_map__delete(threads); |
565 | } | 565 | } |
566 | 566 | ||
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index c4e55b71010c..3667e2123e5b 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c | |||
@@ -5,6 +5,7 @@ | |||
5 | #include <assert.h> | 5 | #include <assert.h> |
6 | #include <stdio.h> | 6 | #include <stdio.h> |
7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
8 | #include "asm/bug.h" | ||
8 | 9 | ||
9 | static struct cpu_map *cpu_map__default_new(void) | 10 | static struct cpu_map *cpu_map__default_new(void) |
10 | { | 11 | { |
@@ -22,6 +23,7 @@ static struct cpu_map *cpu_map__default_new(void) | |||
22 | cpus->map[i] = i; | 23 | cpus->map[i] = i; |
23 | 24 | ||
24 | cpus->nr = nr_cpus; | 25 | cpus->nr = nr_cpus; |
26 | atomic_set(&cpus->refcnt, 1); | ||
25 | } | 27 | } |
26 | 28 | ||
27 | return cpus; | 29 | return cpus; |
@@ -35,6 +37,7 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus) | |||
35 | if (cpus != NULL) { | 37 | if (cpus != NULL) { |
36 | cpus->nr = nr_cpus; | 38 | cpus->nr = nr_cpus; |
37 | memcpy(cpus->map, tmp_cpus, payload_size); | 39 | memcpy(cpus->map, tmp_cpus, payload_size); |
40 | atomic_set(&cpus->refcnt, 1); | ||
38 | } | 41 | } |
39 | 42 | ||
40 | return cpus; | 43 | return cpus; |
@@ -194,14 +197,32 @@ struct cpu_map *cpu_map__dummy_new(void) | |||
194 | if (cpus != NULL) { | 197 | if (cpus != NULL) { |
195 | cpus->nr = 1; | 198 | cpus->nr = 1; |
196 | cpus->map[0] = -1; | 199 | cpus->map[0] = -1; |
200 | atomic_set(&cpus->refcnt, 1); | ||
197 | } | 201 | } |
198 | 202 | ||
199 | return cpus; | 203 | return cpus; |
200 | } | 204 | } |
201 | 205 | ||
202 | void cpu_map__delete(struct cpu_map *map) | 206 | static void cpu_map__delete(struct cpu_map *map) |
203 | { | 207 | { |
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); | ||
205 | } | 226 | } |
206 | 227 | ||
207 | int cpu_map__get_socket(struct cpu_map *map, int idx) | 228 | 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, | |||
263 | /* ensure we process id in increasing order */ | 284 | /* ensure we process id in increasing order */ |
264 | qsort(c->map, c->nr, sizeof(int), cmp_ids); | 285 | qsort(c->map, c->nr, sizeof(int), cmp_ids); |
265 | 286 | ||
287 | atomic_set(&cpus->refcnt, 1); | ||
266 | *res = c; | 288 | *res = c; |
267 | return 0; | 289 | return 0; |
268 | } | 290 | } |
diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h index 61a654849002..0af9cecb4c51 100644 --- a/tools/perf/util/cpumap.h +++ b/tools/perf/util/cpumap.h | |||
@@ -3,18 +3,19 @@ | |||
3 | 3 | ||
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | #include <stdbool.h> | 5 | #include <stdbool.h> |
6 | #include <linux/atomic.h> | ||
6 | 7 | ||
7 | #include "perf.h" | 8 | #include "perf.h" |
8 | #include "util/debug.h" | 9 | #include "util/debug.h" |
9 | 10 | ||
10 | struct cpu_map { | 11 | struct cpu_map { |
12 | atomic_t refcnt; | ||
11 | int nr; | 13 | int nr; |
12 | int map[]; | 14 | int map[]; |
13 | }; | 15 | }; |
14 | 16 | ||
15 | struct cpu_map *cpu_map__new(const char *cpu_list); | 17 | struct cpu_map *cpu_map__new(const char *cpu_list); |
16 | struct cpu_map *cpu_map__dummy_new(void); | 18 | struct cpu_map *cpu_map__dummy_new(void); |
17 | void cpu_map__delete(struct cpu_map *map); | ||
18 | struct cpu_map *cpu_map__read(FILE *file); | 19 | struct cpu_map *cpu_map__read(FILE *file); |
19 | size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp); | 20 | size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp); |
20 | int cpu_map__get_socket(struct cpu_map *map, int idx); | 21 | int cpu_map__get_socket(struct cpu_map *map, int idx); |
@@ -22,6 +23,9 @@ int cpu_map__get_core(struct cpu_map *map, int idx); | |||
22 | int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp); | 23 | int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp); |
23 | int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep); | 24 | int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep); |
24 | 25 | ||
26 | struct cpu_map *cpu_map__get(struct cpu_map *map); | ||
27 | void cpu_map__put(struct cpu_map *map); | ||
28 | |||
25 | static inline int cpu_map__socket(struct cpu_map *sock, int s) | 29 | static inline int cpu_map__socket(struct cpu_map *sock, int s) |
26 | { | 30 | { |
27 | if (!sock || s > sock->nr || s < 0) | 31 | if (!sock || s > sock->nr || s < 0) |
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index d29df901be3e..59498f7b3e9b 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c | |||
@@ -114,7 +114,7 @@ void perf_evlist__delete(struct perf_evlist *evlist) | |||
114 | { | 114 | { |
115 | perf_evlist__munmap(evlist); | 115 | perf_evlist__munmap(evlist); |
116 | perf_evlist__close(evlist); | 116 | perf_evlist__close(evlist); |
117 | cpu_map__delete(evlist->cpus); | 117 | cpu_map__put(evlist->cpus); |
118 | thread_map__delete(evlist->threads); | 118 | thread_map__delete(evlist->threads); |
119 | evlist->cpus = NULL; | 119 | evlist->cpus = NULL; |
120 | evlist->threads = NULL; | 120 | evlist->threads = NULL; |
@@ -1353,7 +1353,7 @@ static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) | |||
1353 | out: | 1353 | out: |
1354 | return err; | 1354 | return err; |
1355 | out_free_cpus: | 1355 | out_free_cpus: |
1356 | cpu_map__delete(evlist->cpus); | 1356 | cpu_map__put(evlist->cpus); |
1357 | evlist->cpus = NULL; | 1357 | evlist->cpus = NULL; |
1358 | goto out; | 1358 | goto out; |
1359 | } | 1359 | } |
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 1b56047af96b..31b0afb68825 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c | |||
@@ -885,6 +885,7 @@ void perf_evsel__exit(struct perf_evsel *evsel) | |||
885 | perf_evsel__free_fd(evsel); | 885 | perf_evsel__free_fd(evsel); |
886 | perf_evsel__free_id(evsel); | 886 | perf_evsel__free_id(evsel); |
887 | close_cgroup(evsel->cgrp); | 887 | close_cgroup(evsel->cgrp); |
888 | cpu_map__put(evsel->cpus); | ||
888 | zfree(&evsel->group_name); | 889 | zfree(&evsel->group_name); |
889 | zfree(&evsel->name); | 890 | zfree(&evsel->name); |
890 | perf_evsel__object.fini(evsel); | 891 | perf_evsel__object.fini(evsel); |
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index 2a4d1ec02846..09f8d2357108 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c | |||
@@ -17,6 +17,7 @@ | |||
17 | #include "parse-events-flex.h" | 17 | #include "parse-events-flex.h" |
18 | #include "pmu.h" | 18 | #include "pmu.h" |
19 | #include "thread_map.h" | 19 | #include "thread_map.h" |
20 | #include "cpumap.h" | ||
20 | #include "asm/bug.h" | 21 | #include "asm/bug.h" |
21 | 22 | ||
22 | #define MAX_NAME_LEN 100 | 23 | #define MAX_NAME_LEN 100 |
@@ -285,7 +286,9 @@ __add_event(struct list_head *list, int *idx, | |||
285 | if (!evsel) | 286 | if (!evsel) |
286 | return NULL; | 287 | return NULL; |
287 | 288 | ||
288 | evsel->cpus = cpus; | 289 | if (cpus) |
290 | evsel->cpus = cpu_map__get(cpus); | ||
291 | |||
289 | if (name) | 292 | if (name) |
290 | evsel->name = strdup(name); | 293 | evsel->name = strdup(name); |
291 | list_add_tail(&evsel->node, list); | 294 | list_add_tail(&evsel->node, list); |
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index d906d0ad5d40..b106d56df240 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c | |||
@@ -384,7 +384,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, | |||
384 | 384 | ||
385 | static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) | 385 | static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) |
386 | { | 386 | { |
387 | cpu_map__delete(pcpus->cpus); | 387 | cpu_map__put(pcpus->cpus); |
388 | pcpus->ob_type->tp_free((PyObject*)pcpus); | 388 | pcpus->ob_type->tp_free((PyObject*)pcpus); |
389 | } | 389 | } |
390 | 390 | ||
diff --git a/tools/perf/util/record.c b/tools/perf/util/record.c index d457c523a33d..1f7becbe5e18 100644 --- a/tools/perf/util/record.c +++ b/tools/perf/util/record.c | |||
@@ -64,7 +64,7 @@ static bool perf_probe_api(setup_probe_fn_t fn) | |||
64 | if (!cpus) | 64 | if (!cpus) |
65 | return false; | 65 | return false; |
66 | cpu = cpus->map[0]; | 66 | cpu = cpus->map[0]; |
67 | cpu_map__delete(cpus); | 67 | cpu_map__put(cpus); |
68 | 68 | ||
69 | do { | 69 | do { |
70 | ret = perf_do_probe_api(fn, cpu, try[i++]); | 70 | 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) | |||
226 | struct cpu_map *cpus = cpu_map__new(NULL); | 226 | struct cpu_map *cpus = cpu_map__new(NULL); |
227 | 227 | ||
228 | cpu = cpus ? cpus->map[0] : 0; | 228 | cpu = cpus ? cpus->map[0] : 0; |
229 | cpu_map__delete(cpus); | 229 | cpu_map__put(cpus); |
230 | } else { | 230 | } else { |
231 | cpu = evlist->cpus->map[0]; | 231 | cpu = evlist->cpus->map[0]; |
232 | } | 232 | } |
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index b5549b58bb2b..ed9dc2555ec7 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c | |||
@@ -1895,7 +1895,7 @@ int perf_session__cpu_bitmap(struct perf_session *session, | |||
1895 | err = 0; | 1895 | err = 0; |
1896 | 1896 | ||
1897 | out_delete_map: | 1897 | out_delete_map: |
1898 | cpu_map__delete(map); | 1898 | cpu_map__put(map); |
1899 | return err; | 1899 | return err; |
1900 | } | 1900 | } |
1901 | 1901 | ||
diff --git a/tools/perf/util/svghelper.c b/tools/perf/util/svghelper.c index 283d3e73e2f2..eec6c1149f44 100644 --- a/tools/perf/util/svghelper.c +++ b/tools/perf/util/svghelper.c | |||
@@ -748,7 +748,7 @@ static int str_to_bitmap(char *s, cpumask_t *b) | |||
748 | set_bit(c, cpumask_bits(b)); | 748 | set_bit(c, cpumask_bits(b)); |
749 | } | 749 | } |
750 | 750 | ||
751 | cpu_map__delete(m); | 751 | cpu_map__put(m); |
752 | 752 | ||
753 | return ret; | 753 | return ret; |
754 | } | 754 | } |