libperf: Adopt perf_mmap__read_event() from tools/perf
authorJiri Olsa <jolsa@kernel.org>
Mon, 7 Oct 2019 12:53:20 +0000 (14:53 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 10 Oct 2019 14:49:46 +0000 (11:49 -0300)
Move perf_mmap__read_event() from tools/perf to libperf and export it in
the perf/mmap.h header.

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://lore.kernel.org/lkml/20191007125344.14268-13-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
21 files changed:
tools/perf/arch/x86/tests/perf-time-to-tsc.c
tools/perf/builtin-kvm.c
tools/perf/builtin-top.c
tools/perf/builtin-trace.c
tools/perf/lib/include/perf/mmap.h
tools/perf/lib/libperf.map
tools/perf/lib/mmap.c
tools/perf/tests/backward-ring-buffer.c
tools/perf/tests/bpf.c
tools/perf/tests/code-reading.c
tools/perf/tests/keep-tracking.c
tools/perf/tests/mmap-basic.c
tools/perf/tests/openat-syscall-tp-fields.c
tools/perf/tests/perf-record.c
tools/perf/tests/sw-clock.c
tools/perf/tests/switch-tracking.c
tools/perf/tests/task-exit.c
tools/perf/util/evlist.c
tools/perf/util/mmap.c
tools/perf/util/mmap.h
tools/perf/util/python.c

index c90d925f7ae643513ae6a68d08029c7b3306706c..909ead08a6f6e1397d786d698845cb76f1174db0 100644 (file)
@@ -121,7 +121,7 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        struct perf_sample sample;
 
                        if (event->header.type != PERF_RECORD_COMM ||
index 4c087a8c9fedd7f8f59e09c72237cd452d012c5f..858da896b518723442f369177fc09d0b606f7844 100644 (file)
@@ -764,7 +764,7 @@ static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
        if (err < 0)
                return (err == -EAGAIN) ? 0 : -1;
 
-       while ((event = perf_mmap__read_event(md)) != NULL) {
+       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                err = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
                if (err) {
                        perf_mmap__consume(&md->core);
index 1a54069ccd9c6ef4799042f4693ecde3b10c6e4b..d96f24c8770d9dfc14fc7d34961eb62fcd34c712 100644 (file)
@@ -873,7 +873,7 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
        if (perf_mmap__read_init(&md->core) < 0)
                return;
 
-       while ((event = perf_mmap__read_event(md)) != NULL) {
+       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                int ret;
 
                ret = perf_evlist__parse_sample_timestamp(evlist, event, &last_timestamp);
index 23116289f710c6e822ad7fd3c7c592fc69db8d1a..144d417ddb22846578dd63020fc5bb193c059a21 100644 (file)
@@ -3804,7 +3804,7 @@ again:
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        ++trace->nr_events;
 
                        err = trace__deliver_event(trace, event);
index 4f946e7f724b6d813e5e877c111fad3310804cde..9508ad90d8b9e19b079dc7fb96d73b85318cd097 100644 (file)
@@ -5,9 +5,11 @@
 #include <perf/core.h>
 
 struct perf_mmap;
+union perf_event;
 
 LIBPERF_API void perf_mmap__consume(struct perf_mmap *map);
 LIBPERF_API int perf_mmap__read_init(struct perf_mmap *map);
 LIBPERF_API void perf_mmap__read_done(struct perf_mmap *map);
+LIBPERF_API union perf_event *perf_mmap__read_event(struct perf_mmap *map);
 
 #endif /* __LIBPERF_MMAP_H */
index 7e3ea2e9c9175eb5510d4ee8ac16ab1b59996ac6..8bb0d73e0c6cefa76e1b8d0b2f3e745fc3749095 100644 (file)
@@ -43,6 +43,7 @@ LIBPERF_0.0.1 {
                perf_mmap__consume;
                perf_mmap__read_init;
                perf_mmap__read_done;
+               perf_mmap__read_event;
        local:
                *;
 };
index 97297cba44e3359f88061a309756aa3316e5e511..0752c193b0fba5890a789bef9940ad0d035b85f7 100644 (file)
@@ -3,9 +3,11 @@
 #include <inttypes.h>
 #include <asm/bug.h>
 #include <errno.h>
+#include <string.h>
 #include <linux/ring_buffer.h>
 #include <linux/perf_event.h>
 #include <perf/mmap.h>
+#include <perf/event.h>
 #include <internal/mmap.h>
 #include <internal/lib.h>
 #include <linux/kernel.h>
@@ -192,3 +194,80 @@ void perf_mmap__read_done(struct perf_mmap *map)
 
        map->prev = perf_mmap__read_head(map);
 }
+
+/* When check_messup is true, 'end' must points to a good entry */
+static union perf_event *perf_mmap__read(struct perf_mmap *map,
+                                        u64 *startp, u64 end)
+{
+       unsigned char *data = map->base + page_size;
+       union perf_event *event = NULL;
+       int diff = end - *startp;
+
+       if (diff >= (int)sizeof(event->header)) {
+               size_t size;
+
+               event = (union perf_event *)&data[*startp & map->mask];
+               size = event->header.size;
+
+               if (size < sizeof(event->header) || diff < (int)size)
+                       return NULL;
+
+               /*
+                * Event straddles the mmap boundary -- header should always
+                * be inside due to u64 alignment of output.
+                */
+               if ((*startp & map->mask) + size != ((*startp + size) & map->mask)) {
+                       unsigned int offset = *startp;
+                       unsigned int len = min(sizeof(*event), size), cpy;
+                       void *dst = map->event_copy;
+
+                       do {
+                               cpy = min(map->mask + 1 - (offset & map->mask), len);
+                               memcpy(dst, &data[offset & map->mask], cpy);
+                               offset += cpy;
+                               dst += cpy;
+                               len -= cpy;
+                       } while (len);
+
+                       event = (union perf_event *)map->event_copy;
+               }
+
+               *startp += size;
+       }
+
+       return event;
+}
+
+/*
+ * Read event from ring buffer one by one.
+ * Return one event for each call.
+ *
+ * Usage:
+ * perf_mmap__read_init()
+ * while(event = perf_mmap__read_event()) {
+ *     //process the event
+ *     perf_mmap__consume()
+ * }
+ * perf_mmap__read_done()
+ */
+union perf_event *perf_mmap__read_event(struct perf_mmap *map)
+{
+       union perf_event *event;
+
+       /*
+        * Check if event was unmapped due to a POLLHUP/POLLERR.
+        */
+       if (!refcount_read(&map->refcnt))
+               return NULL;
+
+       /* non-overwirte doesn't pause the ringbuffer */
+       if (!map->overwrite)
+               map->end = perf_mmap__read_head(map);
+
+       event = perf_mmap__read(map, &map->start, map->end);
+
+       if (!map->overwrite)
+               map->prev = map->start;
+
+       return event;
+}
index 13e67cd213bd13c9c2af42ba77917a4e739f0339..a4cd30c0beb397696785bda80737134f97803b30 100644 (file)
@@ -39,7 +39,7 @@ static int count_samples(struct evlist *evlist, int *sample_count,
                union perf_event *event;
 
                perf_mmap__read_init(&map->core);
-               while ((event = perf_mmap__read_event(map)) != NULL) {
+               while ((event = perf_mmap__read_event(&map->core)) != NULL) {
                        const u32 type = event->header.type;
 
                        switch (type) {
index fd45529e29c14f12ae9e9bc22e08590cb09a410f..5d20bf8397f09d2899f191986f24c601bb313b14 100644 (file)
@@ -188,7 +188,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        const u32 type = event->header.type;
 
                        if (type == PERF_RECORD_SAMPLE)
index 9947cda29bad4800195b7d73d7f3146c0153989c..1f017e1b2a55ed73ad667bfcf08742e549cf7452 100644 (file)
@@ -429,7 +429,7 @@ static int process_events(struct machine *machine, struct evlist *evlist,
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        ret = process_event(machine, evlist, event, state);
                        perf_mmap__consume(&md->core);
                        if (ret < 0)
index e950907f6f579c407d5b13743d99fabab72c1346..50a0c9fcde7da3c79e3a80706b5e08fa451f8b08 100644 (file)
@@ -41,7 +41,7 @@ static int find_comm(struct evlist *evlist, const char *comm)
                md = &evlist->mmap[i];
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        if (event->header.type == PERF_RECORD_COMM &&
                            (pid_t)event->comm.pid == getpid() &&
                            (pid_t)event->comm.tid == getpid() &&
index bb15d405a42c64b7e8bc2707ade3ead1118737e1..5f4c0dbb471582179ea301dec2678f7bf3e94401 100644 (file)
@@ -117,7 +117,7 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
        if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
-       while ((event = perf_mmap__read_event(md)) != NULL) {
+       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                struct perf_sample sample;
 
                if (event->header.type != PERF_RECORD_SAMPLE) {
index c95eb1bbf3962460824c7fd6bce05382b341d5f7..c6b2d7aab608fac947679a07f61b7c3a3dd0e10e 100644 (file)
@@ -96,7 +96,7 @@ int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest
                        if (perf_mmap__read_init(&md->core) < 0)
                                continue;
 
-                       while ((event = perf_mmap__read_event(md)) != NULL) {
+                       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                                const u32 type = event->header.type;
                                int tp_flags;
                                struct perf_sample sample;
index 92a53be3b32b5287e7c7f0f0c52d31285f16a142..2195fc205e72b672770905d40a3ac0a0b8dfb293 100644 (file)
@@ -174,7 +174,7 @@ int test__PERF_RECORD(struct test *test __maybe_unused, int subtest __maybe_unus
                        if (perf_mmap__read_init(&md->core) < 0)
                                continue;
 
-                       while ((event = perf_mmap__read_event(md)) != NULL) {
+                       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                                const u32 type = event->header.type;
                                const char *name = perf_event__name(type);
 
index ace20921ad5523590e1fd73824c906528e19090a..bfb9986093d8dde30100c62525a466a54e20cb5d 100644 (file)
@@ -103,7 +103,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
        if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
-       while ((event = perf_mmap__read_event(md)) != NULL) {
+       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                struct perf_sample sample;
 
                if (event->header.type != PERF_RECORD_SAMPLE)
index 8400fb17c170e0539b0a8917b32f8a4f1461fce1..fcb0d03dba4edea6f96dd615c66a63b1d98b3d9c 100644 (file)
@@ -273,7 +273,7 @@ static int process_events(struct evlist *evlist,
                if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
-               while ((event = perf_mmap__read_event(md)) != NULL) {
+               while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                        cnt += 1;
                        ret = add_event(evlist, &events, event);
                         perf_mmap__consume(&md->core);
index c6a13948821c4fcd358cfd049d9ef2b82991a8e3..4965f8b9055bb999285839a7d6dbb324cfcfff97 100644 (file)
@@ -121,7 +121,7 @@ retry:
        if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
-       while ((event = perf_mmap__read_event(md)) != NULL) {
+       while ((event = perf_mmap__read_event(&md->core)) != NULL) {
                if (event->header.type == PERF_RECORD_EXIT)
                        nr_exit++;
 
index 6e070ee9ad3905faa362241bab4a8c7d2e7241ac..a9b189ac859bba175471b161d653db2414f63d6f 100644 (file)
@@ -1811,7 +1811,7 @@ static void *perf_evlist__poll_thread(void *arg)
 
                        if (perf_mmap__read_init(&map->core))
                                continue;
-                       while ((event = perf_mmap__read_event(map)) != NULL) {
+                       while ((event = perf_mmap__read_event(&map->core)) != NULL) {
                                struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
 
                                if (evsel && evsel->side_band.cb)
index 2dedef9b06fd1dc8b273942f96b6e3135212f88d..2a8bf0ab861c3a9e740094ed92d4f880e2f3a79e 100644 (file)
@@ -29,83 +29,6 @@ size_t mmap__mmap_len(struct mmap *map)
        return perf_mmap__mmap_len(&map->core);
 }
 
-/* When check_messup is true, 'end' must points to a good entry */
-static union perf_event *perf_mmap__read(struct mmap *map,
-                                        u64 *startp, u64 end)
-{
-       unsigned char *data = map->core.base + page_size;
-       union perf_event *event = NULL;
-       int diff = end - *startp;
-
-       if (diff >= (int)sizeof(event->header)) {
-               size_t size;
-
-               event = (union perf_event *)&data[*startp & map->core.mask];
-               size = event->header.size;
-
-               if (size < sizeof(event->header) || diff < (int)size)
-                       return NULL;
-
-               /*
-                * Event straddles the mmap boundary -- header should always
-                * be inside due to u64 alignment of output.
-                */
-               if ((*startp & map->core.mask) + size != ((*startp + size) & map->core.mask)) {
-                       unsigned int offset = *startp;
-                       unsigned int len = min(sizeof(*event), size), cpy;
-                       void *dst = map->core.event_copy;
-
-                       do {
-                               cpy = min(map->core.mask + 1 - (offset & map->core.mask), len);
-                               memcpy(dst, &data[offset & map->core.mask], cpy);
-                               offset += cpy;
-                               dst += cpy;
-                               len -= cpy;
-                       } while (len);
-
-                       event = (union perf_event *)map->core.event_copy;
-               }
-
-               *startp += size;
-       }
-
-       return event;
-}
-
-/*
- * Read event from ring buffer one by one.
- * Return one event for each call.
- *
- * Usage:
- * perf_mmap__read_init()
- * while(event = perf_mmap__read_event()) {
- *     //process the event
- *     perf_mmap__consume()
- * }
- * perf_mmap__read_done()
- */
-union perf_event *perf_mmap__read_event(struct mmap *map)
-{
-       union perf_event *event;
-
-       /*
-        * Check if event was unmapped due to a POLLHUP/POLLERR.
-        */
-       if (!refcount_read(&map->core.refcnt))
-               return NULL;
-
-       /* non-overwirte doesn't pause the ringbuffer */
-       if (!map->core.overwrite)
-               map->core.end = perf_mmap__read_head(&map->core);
-
-       event = perf_mmap__read(map, &map->core.start, map->core.end);
-
-       if (!map->core.overwrite)
-               map->core.prev = map->core.start;
-
-       return event;
-}
-
 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
                               struct auxtrace_mmap_params *mp __maybe_unused,
                               void *userpg __maybe_unused,
index 0b15702be1a5ebba5ad3bf597065e3c8c31aebba..bee4e83f7109d33ebc495658f5534bc74a75a215 100644 (file)
@@ -47,8 +47,6 @@ void mmap__munmap(struct mmap *map);
 
 union perf_event *perf_mmap__read_forward(struct mmap *map);
 
-union perf_event *perf_mmap__read_event(struct mmap *map);
-
 int perf_mmap__push(struct mmap *md, void *to,
                    int push(struct mmap *map, void *to, void *buf, size_t size));
 
index 64eec2a239d42a022db4cab3ad5a1ab3563222f1..25118605f3f8ed8e0863d89022bda5e2e470daa7 100644 (file)
@@ -1026,7 +1026,7 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
        if (perf_mmap__read_init(&md->core) < 0)
                goto end;
 
-       event = perf_mmap__read_event(md);
+       event = perf_mmap__read_event(&md->core);
        if (event != NULL) {
                PyObject *pyevent = pyrf_event__new(event);
                struct pyrf_event *pevent = (struct pyrf_event *)pyevent;