libperf: Add perf_thread_map__nr/perf_thread_map__pid functions
authorJiri Olsa <jolsa@kernel.org>
Thu, 22 Aug 2019 11:11:41 +0000 (13:11 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 22 Aug 2019 20:16:57 +0000 (17:16 -0300)
So it's part of libperf library as basic functions operating on
perf_thread_map objects.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@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/20190822111141.25823-6-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
17 files changed:
tools/perf/builtin-ftrace.c
tools/perf/builtin-script.c
tools/perf/builtin-stat.c
tools/perf/builtin-trace.c
tools/perf/lib/include/perf/threadmap.h
tools/perf/lib/libperf.map
tools/perf/lib/threadmap.c
tools/perf/tests/thread-map.c
tools/perf/util/auxtrace.c
tools/perf/util/event.c
tools/perf/util/evlist.c
tools/perf/util/evsel.c
tools/perf/util/scripting-engines/trace-event-python.c
tools/perf/util/stat-display.c
tools/perf/util/stat.c
tools/perf/util/thread_map.c
tools/perf/util/thread_map.h

index 1367bb5046a705124b07939c1096b6360fc906df..565db782c1b926ae42228d6668c4bb2929934664 100644 (file)
@@ -158,7 +158,7 @@ static int set_tracing_pid(struct perf_ftrace *ftrace)
        if (target__has_cpu(&ftrace->target))
                return 0;
 
-       for (i = 0; i < thread_map__nr(ftrace->evlist->core.threads); i++) {
+       for (i = 0; i < perf_thread_map__nr(ftrace->evlist->core.threads); i++) {
                scnprintf(buf, sizeof(buf), "%d",
                          ftrace->evlist->core.threads->map[i]);
                if (append_tracing_file("set_ftrace_pid", buf) < 0)
index e957b870869bebfdfc8cf6b2857814d7f16f1fb8..9b93ddeaeafa704b69f3686b55694227aec1e8bb 100644 (file)
@@ -1906,7 +1906,7 @@ static struct scripting_ops       *scripting_ops;
 
 static void __process_stat(struct evsel *counter, u64 tstamp)
 {
-       int nthreads = thread_map__nr(counter->core.threads);
+       int nthreads = perf_thread_map__nr(counter->core.threads);
        int ncpus = perf_evsel__nr_cpus(counter);
        int cpu, thread;
        static int header_printed;
@@ -1928,7 +1928,7 @@ static void __process_stat(struct evsel *counter, u64 tstamp)
 
                        printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
                                counter->core.cpus->map[cpu],
-                               thread_map__pid(counter->core.threads, thread),
+                               perf_thread_map__pid(counter->core.threads, thread),
                                counts->val,
                                counts->ena,
                                counts->run,
index 90636a811b364063601afbb95979f0aa82a867ae..8a4f1a7d0cba9a8eff03c08c8eef908ce9346ec1 100644 (file)
@@ -264,7 +264,7 @@ static int read_single_counter(struct evsel *counter, int cpu,
  */
 static int read_counter(struct evsel *counter, struct timespec *rs)
 {
-       int nthreads = thread_map__nr(evsel_list->core.threads);
+       int nthreads = perf_thread_map__nr(evsel_list->core.threads);
        int ncpus, cpu, thread;
 
        if (target__has_cpu(&target) && !target__has_per_thread(&target))
@@ -1893,7 +1893,7 @@ int cmd_stat(int argc, const char **argv)
                thread_map__read_comms(evsel_list->core.threads);
                if (target.system_wide) {
                        if (runtime_stat_new(&stat_config,
-                               thread_map__nr(evsel_list->core.threads))) {
+                               perf_thread_map__nr(evsel_list->core.threads))) {
                                goto out;
                        }
                }
index bc44ed29e05a798c3526c51d510a2f033035d5de..de126258ca1056bb4d412f4db9717bddf628c605 100644 (file)
@@ -3188,7 +3188,7 @@ static int trace__set_filter_pids(struct trace *trace)
                        err = bpf_map__set_filter_pids(trace->filter_pids.map, trace->filter_pids.nr,
                                                       trace->filter_pids.entries);
                }
-       } else if (thread_map__pid(trace->evlist->core.threads, 0) == -1) {
+       } else if (perf_thread_map__pid(trace->evlist->core.threads, 0) == -1) {
                err = trace__set_filter_loop_pids(trace);
        }
 
@@ -3417,7 +3417,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
                evlist__enable(evlist);
        }
 
-       trace->multiple_threads = thread_map__pid(evlist->core.threads, 0) == -1 ||
+       trace->multiple_threads = perf_thread_map__pid(evlist->core.threads, 0) == -1 ||
                                  evlist->core.threads->nr > 1 ||
                                  perf_evlist__first(evlist)->core.attr.inherit;
 
index 456295273daa24025cda47c583f2a5680c6358ed..a7c50de8d0106ee72527989e2f9b15db6bd7a3ad 100644 (file)
@@ -11,6 +11,8 @@ LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);
 
 LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int thread, pid_t pid);
 LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int thread);
+LIBPERF_API int perf_thread_map__nr(struct perf_thread_map *threads);
+LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread);
 
 LIBPERF_API struct perf_thread_map *perf_thread_map__get(struct perf_thread_map *map);
 LIBPERF_API void perf_thread_map__put(struct perf_thread_map *map);
index 3373dd51fcdac2b84eac8c93bbede608f113f91e..dc4d66363bc4def6905225596d5054ab6c914cbb 100644 (file)
@@ -12,6 +12,8 @@ LIBPERF_0.0.1 {
                perf_thread_map__new_dummy;
                perf_thread_map__set_pid;
                perf_thread_map__comm;
+               perf_thread_map__nr;
+               perf_thread_map__pid;
                perf_thread_map__get;
                perf_thread_map__put;
                perf_evsel__new;
index 4865b73e258612593c412c1da364221e61decfa8..e92c368b0a6c3342242b58d8dcd89269daa4826f 100644 (file)
@@ -79,3 +79,13 @@ void perf_thread_map__put(struct perf_thread_map *map)
        if (map && refcount_dec_and_test(&map->refcnt))
                perf_thread_map__delete(map);
 }
+
+int perf_thread_map__nr(struct perf_thread_map *threads)
+{
+       return threads ? threads->nr : 1;
+}
+
+pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread)
+{
+       return map->map[thread].pid;
+}
index d61773cacf0be939faa972cd167a78e15500c785..d803eafedc60a21129b0143bb14c6e839d10a061 100644 (file)
@@ -26,7 +26,7 @@ int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unuse
 
        TEST_ASSERT_VAL("wrong nr", map->nr == 1);
        TEST_ASSERT_VAL("wrong pid",
-                       thread_map__pid(map, 0) == getpid());
+                       perf_thread_map__pid(map, 0) == getpid());
        TEST_ASSERT_VAL("wrong comm",
                        perf_thread_map__comm(map, 0) &&
                        !strcmp(perf_thread_map__comm(map, 0), NAME));
@@ -41,7 +41,7 @@ int test__thread_map(struct test *test __maybe_unused, int subtest __maybe_unuse
        thread_map__read_comms(map);
 
        TEST_ASSERT_VAL("wrong nr", map->nr == 1);
-       TEST_ASSERT_VAL("wrong pid", thread_map__pid(map, 0) == -1);
+       TEST_ASSERT_VAL("wrong pid", perf_thread_map__pid(map, 0) == -1);
        TEST_ASSERT_VAL("wrong comm",
                        perf_thread_map__comm(map, 0) &&
                        !strcmp(perf_thread_map__comm(map, 0), "dummy"));
@@ -68,7 +68,7 @@ static int process_event(struct perf_tool *tool __maybe_unused,
 
        TEST_ASSERT_VAL("wrong nr", threads->nr == 1);
        TEST_ASSERT_VAL("wrong pid",
-                       thread_map__pid(threads, 0) == getpid());
+                       perf_thread_map__pid(threads, 0) == getpid());
        TEST_ASSERT_VAL("wrong comm",
                        perf_thread_map__comm(threads, 0) &&
                        !strcmp(perf_thread_map__comm(threads, 0), NAME));
index 60428576426e58852ddc37a20295582141d4dd86..094e6ceb3cf2a40ba3cea5fb50bf1b4b5c47129c 100644 (file)
@@ -132,12 +132,12 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
        if (per_cpu) {
                mp->cpu = evlist->core.cpus->map[idx];
                if (evlist->core.threads)
-                       mp->tid = thread_map__pid(evlist->core.threads, 0);
+                       mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
                else
                        mp->tid = -1;
        } else {
                mp->cpu = -1;
-               mp->tid = thread_map__pid(evlist->core.threads, idx);
+               mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
        }
 }
 
index f433da85c45e2aa919c55e925357c8c73cf5c790..332edef8d394d868b4e96e5f44c209ec244189b0 100644 (file)
@@ -647,7 +647,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
        for (thread = 0; thread < threads->nr; ++thread) {
                if (__event__synthesize_thread(comm_event, mmap_event,
                                               fork_event, namespaces_event,
-                                              thread_map__pid(threads, thread), 0,
+                                              perf_thread_map__pid(threads, thread), 0,
                                               process, tool, machine,
                                               mmap_data)) {
                        err = -1;
@@ -658,12 +658,12 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
                 * comm.pid is set to thread group id by
                 * perf_event__synthesize_comm
                 */
-               if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
+               if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
                        bool need_leader = true;
 
                        /* is thread group leader in thread_map? */
                        for (j = 0; j < threads->nr; ++j) {
-                               if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
+                               if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
                                        need_leader = false;
                                        break;
                                }
@@ -997,7 +997,7 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool,
                if (!comm)
                        comm = (char *) "";
 
-               entry->pid = thread_map__pid(threads, i);
+               entry->pid = perf_thread_map__pid(threads, i);
                strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
        }
 
index 68b7c949017eb48c5c24d894a61ad5aee13bb907..ff415680fe0a3e836afe8c2a1aaeda2104c3b9cb 100644 (file)
@@ -316,7 +316,7 @@ static int perf_evlist__nr_threads(struct evlist *evlist,
        if (evsel->system_wide)
                return 1;
        else
-               return thread_map__nr(evlist->core.threads);
+               return perf_thread_map__nr(evlist->core.threads);
 }
 
 void evlist__disable(struct evlist *evlist)
@@ -399,7 +399,7 @@ int perf_evlist__enable_event_idx(struct evlist *evlist,
 int perf_evlist__alloc_pollfd(struct evlist *evlist)
 {
        int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
-       int nr_threads = thread_map__nr(evlist->core.threads);
+       int nr_threads = perf_thread_map__nr(evlist->core.threads);
        int nfds = 0;
        struct evsel *evsel;
 
@@ -531,7 +531,7 @@ static void perf_evlist__set_sid_idx(struct evlist *evlist,
        else
                sid->cpu = -1;
        if (!evsel->system_wide && evlist->core.threads && thread >= 0)
-               sid->tid = thread_map__pid(evlist->core.threads, thread);
+               sid->tid = perf_thread_map__pid(evlist->core.threads, thread);
        else
                sid->tid = -1;
 }
@@ -696,7 +696,7 @@ static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist,
 
        evlist->nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
        if (perf_cpu_map__empty(evlist->core.cpus))
-               evlist->nr_mmaps = thread_map__nr(evlist->core.threads);
+               evlist->nr_mmaps = perf_thread_map__nr(evlist->core.threads);
        map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
        if (!map)
                return NULL;
@@ -810,7 +810,7 @@ static int perf_evlist__mmap_per_cpu(struct evlist *evlist,
 {
        int cpu, thread;
        int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
-       int nr_threads = thread_map__nr(evlist->core.threads);
+       int nr_threads = perf_thread_map__nr(evlist->core.threads);
 
        pr_debug2("perf event ring buffer mmapped per cpu\n");
        for (cpu = 0; cpu < nr_cpus; cpu++) {
@@ -838,7 +838,7 @@ static int perf_evlist__mmap_per_thread(struct evlist *evlist,
                                        struct mmap_params *mp)
 {
        int thread;
-       int nr_threads = thread_map__nr(evlist->core.threads);
+       int nr_threads = perf_thread_map__nr(evlist->core.threads);
 
        pr_debug2("perf event ring buffer mmapped per thread\n");
        for (thread = 0; thread < nr_threads; thread++) {
index 7b4350681d6465fbd3bb8fd75881d2fd71e83fc9..e983e721becaa21cc7f94699eab3cd269013cccc 100644 (file)
@@ -1653,7 +1653,7 @@ static bool ignore_missing_thread(struct evsel *evsel,
                                  struct perf_thread_map *threads,
                                  int thread, int err)
 {
-       pid_t ignore_pid = thread_map__pid(threads, thread);
+       pid_t ignore_pid = perf_thread_map__pid(threads, thread);
 
        if (!evsel->ignore_missing_thread)
                return false;
@@ -1816,7 +1816,7 @@ retry_sample_id:
                        int fd, group_fd;
 
                        if (!evsel->cgrp && !evsel->system_wide)
-                               pid = thread_map__pid(threads, thread);
+                               pid = perf_thread_map__pid(threads, thread);
 
                        group_fd = get_group_fd(evsel, cpu, thread);
 retry_open:
index 51771fc0d0df69d58b21197ccdcae57ed73e07ad..78c8bc9380bd1d323decd18d9b18d98a59a960a3 100644 (file)
@@ -1406,7 +1406,7 @@ static void python_process_stat(struct perf_stat_config *config,
        for (thread = 0; thread < threads->nr; thread++) {
                for (cpu = 0; cpu < cpus->nr; cpu++) {
                        process_stat(counter, cpus->map[cpu],
-                                    thread_map__pid(threads, thread), tstamp,
+                                    perf_thread_map__pid(threads, thread), tstamp,
                                     perf_counts(counter->counts, cpu, thread));
                }
        }
index 605a1fdbda7a49902e0eec442b1bcba38ba7f0ba..51d6781aa90d935d957cddfb397d63fdff0d1fdf 100644 (file)
@@ -119,7 +119,7 @@ static void aggr_printout(struct perf_stat_config *config,
                        config->csv_output ? 0 : 16,
                        perf_thread_map__comm(evsel->core.threads, id),
                        config->csv_output ? 0 : -8,
-                       thread_map__pid(evsel->core.threads, id),
+                       perf_thread_map__pid(evsel->core.threads, id),
                        config->csv_sep);
                break;
        case AGGR_GLOBAL:
@@ -745,7 +745,7 @@ static void print_aggr_thread(struct perf_stat_config *config,
                              struct evsel *counter, char *prefix)
 {
        FILE *output = config->output;
-       int nthreads = thread_map__nr(counter->core.threads);
+       int nthreads = perf_thread_map__nr(counter->core.threads);
        int ncpus = perf_cpu_map__nr(counter->core.cpus);
        int thread, sorted_threads, id;
        struct perf_aggr_thread_value *buf;
index 1e6a25abe00f70760b784b99de4eb216c652d479..0cbfd1eca1dd1fb09c2fc26c0f26659f642fdb02 100644 (file)
@@ -159,7 +159,7 @@ static void perf_evsel__free_prev_raw_counts(struct evsel *evsel)
 static int perf_evsel__alloc_stats(struct evsel *evsel, bool alloc_raw)
 {
        int ncpus = perf_evsel__nr_cpus(evsel);
-       int nthreads = thread_map__nr(evsel->core.threads);
+       int nthreads = perf_thread_map__nr(evsel->core.threads);
 
        if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
            perf_evsel__alloc_counts(evsel, ncpus, nthreads) < 0 ||
@@ -309,7 +309,7 @@ process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
 static int process_counter_maps(struct perf_stat_config *config,
                                struct evsel *counter)
 {
-       int nthreads = thread_map__nr(counter->core.threads);
+       int nthreads = perf_thread_map__nr(counter->core.threads);
        int ncpus = perf_evsel__nr_cpus(counter);
        int cpu, thread;
 
index c58385ea05be463d0eac67b602d08cf688700489..3e64525bf604a7d57455ac57e5469f27009e305c 100644 (file)
@@ -310,7 +310,7 @@ size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
        size_t printed = fprintf(fp, "%d thread%s: ",
                                 threads->nr, threads->nr > 1 ? "s" : "");
        for (i = 0; i < threads->nr; ++i)
-               printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
+               printed += fprintf(fp, "%s%d", i ? ", " : "", perf_thread_map__pid(threads, i));
 
        return printed + fprintf(fp, "\n");
 }
@@ -341,7 +341,7 @@ static int get_comm(char **comm, pid_t pid)
 
 static void comm_init(struct perf_thread_map *map, int i)
 {
-       pid_t pid = thread_map__pid(map, i);
+       pid_t pid = perf_thread_map__pid(map, i);
        char *comm = NULL;
 
        /* dummy pid comm initialization */
index ba45c760be724f26aad7874cae03cb4c0398de95..ca165fdf6cb02c1b91aa8cbb399e0a8904b6d575 100644 (file)
@@ -25,16 +25,6 @@ struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str);
 
 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp);
 
-static inline int thread_map__nr(struct perf_thread_map *threads)
-{
-       return threads ? threads->nr : 1;
-}
-
-static inline pid_t thread_map__pid(struct perf_thread_map *map, int thread)
-{
-       return map->map[thread].pid;
-}
-
 void thread_map__read_comms(struct perf_thread_map *threads);
 bool thread_map__has(struct perf_thread_map *threads, pid_t pid);
 int thread_map__remove(struct perf_thread_map *threads, int idx);