libperf: Adopt perf_evsel__read() function from tools/perf
authorJiri Olsa <jolsa@kernel.org>
Sun, 21 Jul 2019 11:24:51 +0000 (13:24 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 29 Jul 2019 21:34:46 +0000 (18:34 -0300)
Move the perf_evsel__read() function to libperf as a public interface
together with struct perf_counts_values for returning counter values.

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-65-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/lib/evsel.c
tools/perf/lib/include/internal/evsel.h
tools/perf/lib/include/perf/evsel.h
tools/perf/lib/libperf.map
tools/perf/tests/event-times.c
tools/perf/util/counts.h
tools/perf/util/evsel.c
tools/perf/util/evsel.h

index 50f09e939229963e618382d375c261b7639513d8..390fcf9107c1df21dc84c3068ccf6c60507e006a 100644 (file)
@@ -12,6 +12,7 @@
 #include <internal/xyarray.h>
 #include <internal/cpumap.h>
 #include <internal/threadmap.h>
+#include <internal/lib.h>
 #include <linux/string.h>
 
 void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr)
@@ -137,3 +138,44 @@ void perf_evsel__close(struct perf_evsel *evsel)
        perf_evsel__close_fd(evsel);
        perf_evsel__free_fd(evsel);
 }
+
+int perf_evsel__read_size(struct perf_evsel *evsel)
+{
+       u64 read_format = evsel->attr.read_format;
+       int entry = sizeof(u64); /* value */
+       int size = 0;
+       int nr = 1;
+
+       if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
+               size += sizeof(u64);
+
+       if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
+               size += sizeof(u64);
+
+       if (read_format & PERF_FORMAT_ID)
+               entry += sizeof(u64);
+
+       if (read_format & PERF_FORMAT_GROUP) {
+               nr = evsel->nr_members;
+               size += sizeof(u64);
+       }
+
+       size += entry * nr;
+       return size;
+}
+
+int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
+                    struct perf_counts_values *count)
+{
+       size_t size = perf_evsel__read_size(evsel);
+
+       memset(count, 0, sizeof(*count));
+
+       if (FD(evsel, cpu, thread) < 0)
+               return -EINVAL;
+
+       if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
+               return -errno;
+
+       return 0;
+}
index 878e2cf41ffc23f3135ca788d75a563fffeb24fe..89bae3720d67c500812a8cfa189a4719494efac7 100644 (file)
@@ -23,5 +23,6 @@ struct perf_evsel {
 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
 void perf_evsel__close_fd(struct perf_evsel *evsel);
 void perf_evsel__free_fd(struct perf_evsel *evsel);
+int perf_evsel__read_size(struct perf_evsel *evsel);
 
 #endif /* __LIBPERF_INTERNAL_EVSEL_H */
index aa5c48f822d262fc2f371156e61d075fbded8cce..16ae3f873280ef75a34f30d6f0890ec4d9782420 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef __LIBPERF_EVSEL_H
 #define __LIBPERF_EVSEL_H
 
+#include <stdint.h>
 #include <perf/core.h>
 
 struct perf_evsel;
@@ -9,6 +10,17 @@ struct perf_event_attr;
 struct perf_cpu_map;
 struct perf_thread_map;
 
+struct perf_counts_values {
+       union {
+               struct {
+                       uint64_t val;
+                       uint64_t ena;
+                       uint64_t run;
+               };
+               uint64_t values[3];
+       };
+};
+
 LIBPERF_API void perf_evsel__init(struct perf_evsel *evsel,
                                  struct perf_event_attr *attr);
 LIBPERF_API struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr);
@@ -16,5 +28,7 @@ LIBPERF_API void perf_evsel__delete(struct perf_evsel *evsel);
 LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
                                 struct perf_thread_map *threads);
 LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel);
+LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
+                                struct perf_counts_values *count);
 
 #endif /* __LIBPERF_EVSEL_H */
index 0b90999dcdcb006ec5006353f5a1ae232ab29a58..2e23cf420ccef2a53737e55962cf81f1416e6ef0 100644 (file)
@@ -16,6 +16,7 @@ LIBPERF_0.0.1 {
                perf_evsel__init;
                perf_evsel__open;
                perf_evsel__close;
+               perf_evsel__read;
                perf_evlist__new;
                perf_evlist__delete;
                perf_evlist__init;
index 00adba86403ba3aa7c7500a2e880f509802f524e..714e3611352c0fe83029ebec7e2cc64c6c39ba06 100644 (file)
@@ -196,7 +196,7 @@ static int test_times(int (attach)(struct evlist *),
 
        TEST_ASSERT_VAL("failed to detach", !detach(evlist));
 
-       perf_evsel__read(evsel, 0, 0, &count);
+       perf_evsel__read(&evsel->core, 0, 0, &count);
 
        err = !(count.ena == count.run);
 
index bbfac9ecf6428f8137ec23df811e54b0163991b4..13430f353c19b1ca5201e21db5c652712fab4fb7 100644 (file)
@@ -3,17 +3,7 @@
 #define __PERF_COUNTS_H
 
 #include <internal/xyarray.h>
-
-struct perf_counts_values {
-       union {
-               struct {
-                       u64 val;
-                       u64 ena;
-                       u64 run;
-               };
-               u64 values[3];
-       };
-};
+#include <perf/evsel.h>
 
 struct perf_counts {
        s8                        scaled;
index 8d8ed36377f5f021e44543e61071a8cf46eb7ab1..0957ec24f518038bc2dfacaa377f920ac3289195 100644 (file)
@@ -1346,53 +1346,12 @@ void perf_counts_values__scale(struct perf_counts_values *count,
                *pscaled = scaled;
 }
 
-static int perf_evsel__read_size(struct evsel *evsel)
-{
-       u64 read_format = evsel->core.attr.read_format;
-       int entry = sizeof(u64); /* value */
-       int size = 0;
-       int nr = 1;
-
-       if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
-               size += sizeof(u64);
-
-       if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
-               size += sizeof(u64);
-
-       if (read_format & PERF_FORMAT_ID)
-               entry += sizeof(u64);
-
-       if (read_format & PERF_FORMAT_GROUP) {
-               nr = evsel->core.nr_members;
-               size += sizeof(u64);
-       }
-
-       size += entry * nr;
-       return size;
-}
-
-int perf_evsel__read(struct evsel *evsel, int cpu, int thread,
-                    struct perf_counts_values *count)
-{
-       size_t size = perf_evsel__read_size(evsel);
-
-       memset(count, 0, sizeof(*count));
-
-       if (FD(evsel, cpu, thread) < 0)
-               return -EINVAL;
-
-       if (readn(FD(evsel, cpu, thread), count->values, size) <= 0)
-               return -errno;
-
-       return 0;
-}
-
 static int
 perf_evsel__read_one(struct evsel *evsel, int cpu, int thread)
 {
        struct perf_counts_values *count = perf_counts(evsel->counts, cpu, thread);
 
-       return perf_evsel__read(evsel, cpu, thread, count);
+       return perf_evsel__read(&evsel->core, cpu, thread, count);
 }
 
 static void
@@ -1453,7 +1412,7 @@ perf_evsel__read_group(struct evsel *leader, int cpu, int thread)
 {
        struct perf_stat_evsel *ps = leader->stats;
        u64 read_format = leader->core.attr.read_format;
-       int size = perf_evsel__read_size(leader);
+       int size = perf_evsel__read_size(&leader->core);
        u64 *data = ps->group_data;
 
        if (!(read_format & PERF_FORMAT_ID))
index 03fc8edad49278d9c25e000559755154bfbbe1f3..57e315d8158e15f2221c00e9bd8fbf64bf1f8019 100644 (file)
@@ -336,9 +336,6 @@ static inline bool perf_evsel__match2(struct evsel *e1,
         (a)->core.attr.type == (b)->core.attr.type &&  \
         (a)->core.attr.config == (b)->core.attr.config)
 
-int perf_evsel__read(struct evsel *evsel, int cpu, int thread,
-                    struct perf_counts_values *count);
-
 int perf_evsel__read_counter(struct evsel *evsel, int cpu, int thread);
 
 int __perf_evsel__read_on_cpu(struct evsel *evsel,