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

And add pr_debug2()/pr_debug3() macros support, because the code is
using them.

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-11-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
23 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/core.h
tools/perf/lib/include/perf/mmap.h
tools/perf/lib/internal.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 3397898824f6ef26ceeb3f0dbdf773b1c4e2fc1b..6a0c3ff78e01e4ff599e0d49708803b15cc0aa31 100644 (file)
@@ -118,7 +118,7 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
 
        for (i = 0; i < evlist->core.nr_mmaps; i++) {
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
                while ((event = perf_mmap__read_event(md)) != NULL) {
index 0c04c4c6c1eb46dfad4ca729f040fa255c0808a0..b6a8078dd446c7225d9d4946341dc3c8a4c2e40a 100644 (file)
@@ -760,7 +760,7 @@ static s64 perf_kvm__mmap_read_idx(struct perf_kvm_stat *kvm, int idx,
 
        *mmap_time = ULLONG_MAX;
        md = &evlist->mmap[idx];
-       err = perf_mmap__read_init(md);
+       err = perf_mmap__read_init(&md->core);
        if (err < 0)
                return (err == -EAGAIN) ? 0 : -1;
 
index 5fcf1579941561936e0789c01a8a81a6e2d6cffa..4a4bb7b20c39b7f31ac04fcc77375b0321e2572b 100644 (file)
@@ -870,7 +870,7 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
        union perf_event *event;
 
        md = opts->overwrite ? &evlist->overwrite_mmap[idx] : &evlist->mmap[idx];
-       if (perf_mmap__read_init(md) < 0)
+       if (perf_mmap__read_init(&md->core) < 0)
                return;
 
        while ((event = perf_mmap__read_event(md)) != NULL) {
index 6a57084385660eb44471bf36addfac671861fee9..cd69d68e7f1de95a4aeccc45031d127c7f49ff17 100644 (file)
@@ -3801,7 +3801,7 @@ again:
                struct mmap *md;
 
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
                while ((event = perf_mmap__read_event(md)) != NULL) {
index cfd70e720c1ce0fbb4c3d2b707ee6bd8359e5421..2a80e4b6f819500730b82034df576f87368ed885 100644 (file)
@@ -12,6 +12,8 @@ enum libperf_print_level {
        LIBPERF_WARN,
        LIBPERF_INFO,
        LIBPERF_DEBUG,
+       LIBPERF_DEBUG2,
+       LIBPERF_DEBUG3,
 };
 
 typedef int (*libperf_print_fn_t)(enum libperf_print_level level,
index d3678d1834d9c1eacde3755e0938d0aba33df638..646e9052b003c1559ec110abf9c2d759f5aacfeb 100644 (file)
@@ -7,5 +7,6 @@
 struct perf_mmap;
 
 LIBPERF_API void perf_mmap__consume(struct perf_mmap *map);
+LIBPERF_API int perf_mmap__read_init(struct perf_mmap *map);
 
 #endif /* __LIBPERF_MMAP_H */
index dc92f241732e617c88c8d10ff24f7daeadab6f08..37db745e1502453817d4ee5ccb2d3c5a7555fa60 100644 (file)
@@ -14,5 +14,7 @@ do {                            \
 #define pr_warning(fmt, ...)    __pr(LIBPERF_WARN, fmt, ##__VA_ARGS__)
 #define pr_info(fmt, ...)       __pr(LIBPERF_INFO, fmt, ##__VA_ARGS__)
 #define pr_debug(fmt, ...)      __pr(LIBPERF_DEBUG, fmt, ##__VA_ARGS__)
+#define pr_debug2(fmt, ...)     __pr(LIBPERF_DEBUG2, fmt, ##__VA_ARGS__)
+#define pr_debug3(fmt, ...)     __pr(LIBPERF_DEBUG3, fmt, ##__VA_ARGS__)
 
 #endif /* __LIBPERF_INTERNAL_H */
index d7b327f224e252cfc7c3e07831feb85ae1a4a362..bc3fbb213a3ec1e4218501856d1426bd065f151f 100644 (file)
@@ -41,6 +41,7 @@ LIBPERF_0.0.1 {
                perf_evlist__set_maps;
                perf_evlist__poll;
                perf_mmap__consume;
+               perf_mmap__read_init;
        local:
                *;
 };
index 4cada1c89fdb94290780609778236701002dd95f..fdbc6c550dea85244a0fd682aa5700d24d11959d 100644 (file)
@@ -1,11 +1,15 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <sys/mman.h>
+#include <inttypes.h>
+#include <asm/bug.h>
+#include <errno.h>
 #include <linux/ring_buffer.h>
 #include <linux/perf_event.h>
 #include <perf/mmap.h>
 #include <internal/mmap.h>
 #include <internal/lib.h>
 #include <linux/kernel.h>
+#include "internal.h"
 
 void perf_mmap__init(struct perf_mmap *map, bool overwrite,
                     libperf_unmap_cb_t unmap_cb)
@@ -91,3 +95,83 @@ void perf_mmap__consume(struct perf_mmap *map)
        if (refcount_read(&map->refcnt) == 1 && perf_mmap__empty(map))
                perf_mmap__put(map);
 }
+
+static int overwrite_rb_find_range(void *buf, int mask, u64 *start, u64 *end)
+{
+       struct perf_event_header *pheader;
+       u64 evt_head = *start;
+       int size = mask + 1;
+
+       pr_debug2("%s: buf=%p, start=%"PRIx64"\n", __func__, buf, *start);
+       pheader = (struct perf_event_header *)(buf + (*start & mask));
+       while (true) {
+               if (evt_head - *start >= (unsigned int)size) {
+                       pr_debug("Finished reading overwrite ring buffer: rewind\n");
+                       if (evt_head - *start > (unsigned int)size)
+                               evt_head -= pheader->size;
+                       *end = evt_head;
+                       return 0;
+               }
+
+               pheader = (struct perf_event_header *)(buf + (evt_head & mask));
+
+               if (pheader->size == 0) {
+                       pr_debug("Finished reading overwrite ring buffer: get start\n");
+                       *end = evt_head;
+                       return 0;
+               }
+
+               evt_head += pheader->size;
+               pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
+       }
+       WARN_ONCE(1, "Shouldn't get here\n");
+       return -1;
+}
+
+/*
+ * Report the start and end of the available data in ringbuffer
+ */
+static int __perf_mmap__read_init(struct perf_mmap *md)
+{
+       u64 head = perf_mmap__read_head(md);
+       u64 old = md->prev;
+       unsigned char *data = md->base + page_size;
+       unsigned long size;
+
+       md->start = md->overwrite ? head : old;
+       md->end = md->overwrite ? old : head;
+
+       if ((md->end - md->start) < md->flush)
+               return -EAGAIN;
+
+       size = md->end - md->start;
+       if (size > (unsigned long)(md->mask) + 1) {
+               if (!md->overwrite) {
+                       WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
+
+                       md->prev = head;
+                       perf_mmap__consume(md);
+                       return -EAGAIN;
+               }
+
+               /*
+                * Backward ring buffer is full. We still have a chance to read
+                * most of data from it.
+                */
+               if (overwrite_rb_find_range(data, md->mask, &md->start, &md->end))
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
+int perf_mmap__read_init(struct perf_mmap *map)
+{
+       /*
+        * Check if event was unmapped due to a POLLHUP/POLLERR.
+        */
+       if (!refcount_read(&map->refcnt))
+               return -ENOENT;
+
+       return __perf_mmap__read_init(map);
+}
index 13f9a060361a3655c78ac2903e9841f0cba9e2bb..ff3a986983ab28b51d6e2cfba44a9b6074b879c6 100644 (file)
@@ -38,7 +38,7 @@ static int count_samples(struct evlist *evlist, int *sample_count,
                struct mmap *map = &evlist->overwrite_mmap[i];
                union perf_event *event;
 
-               perf_mmap__read_init(map);
+               perf_mmap__read_init(&map->core);
                while ((event = perf_mmap__read_event(map)) != NULL) {
                        const u32 type = event->header.type;
 
index 6f0d239f8277c20b6ce7283195ed7c35844ce6b7..73d26c63d624efeb3209d541391cdfc662c525f9 100644 (file)
@@ -185,7 +185,7 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
                struct mmap *md;
 
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
                while ((event = perf_mmap__read_event(md)) != NULL) {
index b5a57bb54c254fda4dadb0017cd3ecb07055ccab..cf992e0b27ff73a9caaea31da3fa4d58b865a311 100644 (file)
@@ -426,7 +426,7 @@ static int process_events(struct machine *machine, struct evlist *evlist,
 
        for (i = 0; i < evlist->core.nr_mmaps; i++) {
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
                while ((event = perf_mmap__read_event(md)) != NULL) {
index 31c005e07b17b0e7fbd794bbd0b9988d56527be3..e85da7e77269b54d7348b970f1247e3c6cdc7b46 100644 (file)
@@ -39,7 +39,7 @@ static int find_comm(struct evlist *evlist, const char *comm)
        found = 0;
        for (i = 0; i < evlist->core.nr_mmaps; i++) {
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
                while ((event = perf_mmap__read_event(md)) != NULL) {
                        if (event->header.type == PERF_RECORD_COMM &&
index b176acc4f52ef9541ef4c03073b134d25e9ec101..77f42f0ac15d540f95ec1eb285c08b695537f12f 100644 (file)
@@ -114,7 +114,7 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
                }
 
        md = &evlist->mmap[0];
-       if (perf_mmap__read_init(md) < 0)
+       if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
        while ((event = perf_mmap__read_event(md)) != NULL) {
index bbf8ba3207213abbcc64e11f6abdc03b9917c5b9..d6a563120d935e591285c31494daba5bf53c7f3b 100644 (file)
@@ -93,7 +93,7 @@ int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest
                        struct mmap *md;
 
                        md = &evlist->mmap[i];
-                       if (perf_mmap__read_init(md) < 0)
+                       if (perf_mmap__read_init(&md->core) < 0)
                                continue;
 
                        while ((event = perf_mmap__read_event(md)) != NULL) {
index 6ebbcc65749e08293bb85739156018af16bdf3e7..2587cb8b2c0fecd5a89092acd7b05126f9397fbc 100644 (file)
@@ -171,7 +171,7 @@ int test__PERF_RECORD(struct test *test __maybe_unused, int subtest __maybe_unus
                        struct mmap *md;
 
                        md = &evlist->mmap[i];
-                       if (perf_mmap__read_init(md) < 0)
+                       if (perf_mmap__read_init(&md->core) < 0)
                                continue;
 
                        while ((event = perf_mmap__read_event(md)) != NULL) {
index 1aeb558010c1d2d7d0f2b87454e6f11382d938b8..808669507c30ad4eec802a4d95852597c5452939 100644 (file)
@@ -100,7 +100,7 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
        evlist__disable(evlist);
 
        md = &evlist->mmap[0];
-       if (perf_mmap__read_init(md) < 0)
+       if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
        while ((event = perf_mmap__read_event(md)) != NULL) {
index 55728b3da057d7e835038fdb7f0981262f4a6324..bedfdec349720c999214555a82724b26ef93782b 100644 (file)
@@ -270,7 +270,7 @@ static int process_events(struct evlist *evlist,
 
        for (i = 0; i < evlist->core.nr_mmaps; i++) {
                md = &evlist->mmap[i];
-               if (perf_mmap__read_init(md) < 0)
+               if (perf_mmap__read_init(&md->core) < 0)
                        continue;
 
                while ((event = perf_mmap__read_event(md)) != NULL) {
index a0f6895948485db89f461ebfc1725ce097172270..035d42375d4bb63a60e45f9ab47f40d1a2814d3c 100644 (file)
@@ -118,7 +118,7 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 
 retry:
        md = &evlist->mmap[0];
-       if (perf_mmap__read_init(md) < 0)
+       if (perf_mmap__read_init(&md->core) < 0)
                goto out_init;
 
        while ((event = perf_mmap__read_event(md)) != NULL) {
index 34ba47b9896ba6849c8f9b7312dfabd165db4d21..d9a4a4b188eddbfd259b4189f4cc17e8fad196bd 100644 (file)
@@ -1809,7 +1809,7 @@ static void *perf_evlist__poll_thread(void *arg)
                        struct mmap *map = &evlist->mmap[i];
                        union perf_event *event;
 
-                       if (perf_mmap__read_init(map))
+                       if (perf_mmap__read_init(&map->core))
                                continue;
                        while ((event = perf_mmap__read_event(map)) != NULL) {
                                struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
index abe7cbe6c95ffb19c5488088f9470c5153247238..59379118c2f1bea932fbbdc19144e4fbde4f71e9 100644 (file)
@@ -365,86 +365,6 @@ int mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
        return perf_mmap__aio_mmap(map, mp);
 }
 
-static int overwrite_rb_find_range(void *buf, int mask, u64 *start, u64 *end)
-{
-       struct perf_event_header *pheader;
-       u64 evt_head = *start;
-       int size = mask + 1;
-
-       pr_debug2("%s: buf=%p, start=%"PRIx64"\n", __func__, buf, *start);
-       pheader = (struct perf_event_header *)(buf + (*start & mask));
-       while (true) {
-               if (evt_head - *start >= (unsigned int)size) {
-                       pr_debug("Finished reading overwrite ring buffer: rewind\n");
-                       if (evt_head - *start > (unsigned int)size)
-                               evt_head -= pheader->size;
-                       *end = evt_head;
-                       return 0;
-               }
-
-               pheader = (struct perf_event_header *)(buf + (evt_head & mask));
-
-               if (pheader->size == 0) {
-                       pr_debug("Finished reading overwrite ring buffer: get start\n");
-                       *end = evt_head;
-                       return 0;
-               }
-
-               evt_head += pheader->size;
-               pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
-       }
-       WARN_ONCE(1, "Shouldn't get here\n");
-       return -1;
-}
-
-/*
- * Report the start and end of the available data in ringbuffer
- */
-static int __perf_mmap__read_init(struct mmap *md)
-{
-       u64 head = perf_mmap__read_head(&md->core);
-       u64 old = md->core.prev;
-       unsigned char *data = md->core.base + page_size;
-       unsigned long size;
-
-       md->core.start = md->core.overwrite ? head : old;
-       md->core.end = md->core.overwrite ? old : head;
-
-       if ((md->core.end - md->core.start) < md->core.flush)
-               return -EAGAIN;
-
-       size = md->core.end - md->core.start;
-       if (size > (unsigned long)(md->core.mask) + 1) {
-               if (!md->core.overwrite) {
-                       WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
-
-                       md->core.prev = head;
-                       perf_mmap__consume(&md->core);
-                       return -EAGAIN;
-               }
-
-               /*
-                * Backward ring buffer is full. We still have a chance to read
-                * most of data from it.
-                */
-               if (overwrite_rb_find_range(data, md->core.mask, &md->core.start, &md->core.end))
-                       return -EINVAL;
-       }
-
-       return 0;
-}
-
-int perf_mmap__read_init(struct mmap *map)
-{
-       /*
-        * Check if event was unmapped due to a POLLHUP/POLLERR.
-        */
-       if (!refcount_read(&map->core.refcnt))
-               return -ENOENT;
-
-       return __perf_mmap__read_init(map);
-}
-
 int perf_mmap__push(struct mmap *md, void *to,
                    int push(struct mmap *map, void *to, void *buf, size_t size))
 {
@@ -454,7 +374,7 @@ int perf_mmap__push(struct mmap *md, void *to,
        void *buf;
        int rc = 0;
 
-       rc = perf_mmap__read_init(md);
+       rc = perf_mmap__read_init(&md->core);
        if (rc < 0)
                return (rc == -EAGAIN) ? 1 : -1;
 
index 89fb93267ff1f4cbb98ef287df2c259c76777bad..6d818ef51f05ceb80dc5c34f64de82d4411e5cfd 100644 (file)
@@ -54,6 +54,5 @@ int perf_mmap__push(struct mmap *md, void *to,
 
 size_t mmap__mmap_len(struct mmap *map);
 
-int perf_mmap__read_init(struct mmap *md);
 void perf_mmap__read_done(struct mmap *map);
 #endif /*__PERF_MMAP_H */
index 82a4fa6c87bde715acfdff818c98b28bf37bbf25..64eec2a239d42a022db4cab3ad5a1ab3563222f1 100644 (file)
@@ -1023,7 +1023,7 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
        if (!md)
                return NULL;
 
-       if (perf_mmap__read_init(md) < 0)
+       if (perf_mmap__read_init(&md->core) < 0)
                goto end;
 
        event = perf_mmap__read_event(md);