2019-10-07 12:57:51

by Jiri Olsa

[permalink] [raw]
Subject: [PATCHv2 00/36] libperf: Add sampling interface

hi,
sending changes for exporting basic sampling interface
in libperf. It's now possible to use following code in
applications via libperf:

--- (example is without error checks for simplicity)

struct perf_event_attr attr = {
.type = PERF_TYPE_TRACEPOINT,
.sample_period = 1,
.wakeup_watermark = 1,
.disabled = 1,
};
/* ... setup attr */

cpus = perf_cpu_map__new(NULL);

evlist = perf_evlist__new();
evsel = perf_evsel__new(&attr);
perf_evlist__add(evlist, evsel);

perf_evlist__set_maps(evlist, cpus, NULL);

err = perf_evlist__open(evlist);
err = perf_evlist__mmap(evlist, 4);

err = perf_evlist__enable(evlist);

/* ... monitored area, plus all the other cpus */

err = perf_evlist__disable(evlist);

perf_evlist__for_each_mmap(evlist, map) {
if (perf_mmap__read_init(map) < 0)
continue;

while ((event = perf_mmap__read_event(map)) != NULL) {
perf_mmap__consume(map);
}

perf_mmap__read_done(map);
}

perf_evlist__delete(evlist);
perf_cpu_map__put(cpus);

--- (end)

Nothing is carved in stone so far, the interface is exported
as is available in perf now and we can change it as we want.

New tests are added in test-evlist.c to do thread and cpu based
sampling.

All the functionality should not change, however there's considerable
mmap code rewrite.

Now we have perf_evlist__mmap_ops called by both perf and libperf
mmaps functions with specific 'struct perf_evlist_mmap_ops'
callbacks:

- get - to get mmap object, both libperf and perf use different
objects, because perf needs to carry more data for aio,
compression and auxtrace
- mmap - to actually mmap the object, it's simple mmap for libperf,
but more work for perf wrt aio, compression and auxtrace
- idx - callback to get current IDs, used only in perf for auxtrace
setup


It would be great if guys could run your usual workloads to see if all
is fine.. so far so good in my tests ;-)


It's also available in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/lib

v2 changes:
- rebased to latest perf/core
- portion of patches already taken
- explained mmap refcnt management in following patch changelog:
libperf: Centralize map refcnt setting

thanks,
jirka


Cc: Kan Liang <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Ian Rogers <[email protected]>
Cc: Stephane Eranian <[email protected]>
Cc: Song Liu <[email protected]>
Cc: Alexey Budankov <[email protected]>
Cc: Andi Kleen <[email protected]>
---
Jiri Olsa (36):
libperf: Add perf_mmap__init() function
libperf: Add 'struct perf_mmap_param'
libperf: Add perf_mmap__mmap_len() function
libperf: Add perf_mmap__mmap() function
libperf: Add perf_mmap__get() function
libperf: Add perf_mmap__unmap() function
libperf: Add perf_mmap__put() function
perf tools: Use perf_mmap way to detect aux mmap
libperf: Add perf_mmap__consume() function
libperf: Add perf_mmap__read_init() function
libperf: Add perf_mmap__read_done() function
libperf: Add perf_mmap__read_event() function
libperf: Add perf_evlist__mmap()/munmap() functions
libperf: Add perf_evlist__mmap_ops function
libperf: Add perf_evlist_mmap_ops::idx callback
libperf: Add perf_evlist_mmap_ops::get callback
libperf: Add perf_evlist_mmap_ops::mmap callback
perf tools: Add perf_evlist__mmap_cb_idx function
perf tools: Add perf_evlist__mmap_cb_get function
perf tools: Add perf_evlist__mmap_cb_mmap function
perf tools: Switch to libperf mmap interface
libperf: Centralize map refcnt setting
libperf: Move pollfd allocation to libperf
libperf: Add perf_evlist__exit function
libperf: Add perf_evlist__purge function
libperf: Add perf_evlist__filter_pollfd function
libperf: Add perf_evlist__for_each_mmap function
libperf: Move mmap allocation to perf_evlist__mmap_ops::get
libperf: Move mask setup to perf_evlist__mmap_ops function
libperf: Link static tests with libapi.a
libperf: Add _GNU_SOURCE define to compilation
libperf: Add tests_mmap_thread test
libperf: Add tests_mmap_cpus test
libperf: Keep count of failed tests
libperf: Do not export perf_evsel__init/perf_evlist__init
libperf: Add pr_err macro

tools/perf/arch/x86/tests/perf-time-to-tsc.c | 9 ++--
tools/perf/builtin-kvm.c | 11 ++---
tools/perf/builtin-record.c | 10 ++---
tools/perf/builtin-top.c | 9 ++--
tools/perf/builtin-trace.c | 9 ++--
tools/perf/lib/Build | 1 +
tools/perf/lib/Makefile | 7 +++-
tools/perf/lib/evlist.c | 357 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/perf/lib/include/internal/evlist.h | 43 ++++++++++++++++++++
tools/perf/lib/include/internal/evsel.h | 1 +
tools/perf/lib/include/internal/mmap.h | 46 ++++++++++++++++-----
tools/perf/lib/include/internal/tests.h | 20 +++++++--
tools/perf/lib/include/perf/core.h | 3 ++
tools/perf/lib/include/perf/evlist.h | 15 ++++++-
tools/perf/lib/include/perf/evsel.h | 2 -
tools/perf/lib/include/perf/mmap.h | 14 +++++++
tools/perf/lib/internal.h | 5 +++
tools/perf/lib/libperf.map | 10 ++++-
tools/perf/lib/mmap.c | 274 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/perf/lib/tests/Makefile | 8 ++--
tools/perf/lib/tests/test-cpumap.c | 2 +-
tools/perf/lib/tests/test-evlist.c | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
tools/perf/lib/tests/test-evsel.c | 2 +-
tools/perf/lib/tests/test-threadmap.c | 2 +-
tools/perf/tests/backward-ring-buffer.c | 7 ++--
tools/perf/tests/bpf.c | 7 ++--
tools/perf/tests/code-reading.c | 9 ++--
tools/perf/tests/keep-tracking.c | 9 ++--
tools/perf/tests/mmap-basic.c | 9 ++--
tools/perf/tests/openat-syscall-tp-fields.c | 9 ++--
tools/perf/tests/perf-record.c | 9 ++--
tools/perf/tests/sw-clock.c | 9 ++--
tools/perf/tests/switch-tracking.c | 9 ++--
tools/perf/tests/task-exit.c | 9 ++--
tools/perf/util/evlist.c | 256 +++++++++++++++++++++++++++---------------------------------------------------------------------------------------
tools/perf/util/mmap.c | 260 +++++++-------------------------------------------------------------------------------------------------------------
tools/perf/util/mmap.h | 28 +++----------
tools/perf/util/python.c | 7 ++--
38 files changed, 1161 insertions(+), 554 deletions(-)
create mode 100644 tools/perf/lib/include/perf/mmap.h
create mode 100644 tools/perf/lib/mmap.c


2019-10-07 12:58:34

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 36/36] libperf: Add pr_err macro

And missing include for "perf/core.h" header, which provides
LIBPERF_* debug levels and adding missing pr_err support.

Link: http://lkml.kernel.org/n/[email protected]
Signed-off-by: Jiri Olsa <[email protected]>
---
tools/perf/lib/include/perf/core.h | 1 +
tools/perf/lib/internal.h | 3 +++
2 files changed, 4 insertions(+)

diff --git a/tools/perf/lib/include/perf/core.h b/tools/perf/lib/include/perf/core.h
index 2a80e4b6f819..a3f6d68edad7 100644
--- a/tools/perf/lib/include/perf/core.h
+++ b/tools/perf/lib/include/perf/core.h
@@ -9,6 +9,7 @@
#endif

enum libperf_print_level {
+ LIBPERF_ERR,
LIBPERF_WARN,
LIBPERF_INFO,
LIBPERF_DEBUG,
diff --git a/tools/perf/lib/internal.h b/tools/perf/lib/internal.h
index 37db745e1502..2c27e158de6b 100644
--- a/tools/perf/lib/internal.h
+++ b/tools/perf/lib/internal.h
@@ -2,6 +2,8 @@
#ifndef __LIBPERF_INTERNAL_H
#define __LIBPERF_INTERNAL_H

+#include <perf/core.h>
+
void libperf_print(enum libperf_print_level level,
const char *format, ...)
__attribute__((format(printf, 2, 3)));
@@ -11,6 +13,7 @@ do { \
libperf_print(level, "libperf: " fmt, ##__VA_ARGS__); \
} while (0)

+#define pr_err(fmt, ...) __pr(LIBPERF_ERR, fmt, ##__VA_ARGS__)
#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__)
--
2.21.0

2019-10-07 12:58:34

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 34/36] libperf: Keep count of failed tests

Keeping the count of failed tests, so we
get better output with failures, like:

# make tests
...
running static:
- running test-cpumap.c...OK
- running test-threadmap.c...OK
- running test-evlist.c...FAILED test-evlist.c:53 failed to create evsel2
FAILED test-evlist.c:163 failed to create evsel2
FAILED test-evlist.c:287 failed count
FAILED (3)
- running test-evsel.c...OK
running dynamic:
- running test-cpumap.c...OK
- running test-threadmap.c...OK
- running test-evlist.c...FAILED test-evlist.c:53 failed to create evsel2
FAILED test-evlist.c:163 failed to create evsel2
FAILED test-evlist.c:287 failed count
FAILED (3)
- running test-evsel.c...OK
...

Link: http://lkml.kernel.org/n/[email protected]
Signed-off-by: Jiri Olsa <[email protected]>
---
tools/perf/lib/include/internal/tests.h | 20 +++++++++++++++++---
tools/perf/lib/tests/test-cpumap.c | 2 +-
tools/perf/lib/tests/test-evlist.c | 2 +-
tools/perf/lib/tests/test-evsel.c | 2 +-
tools/perf/lib/tests/test-threadmap.c | 2 +-
5 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/tools/perf/lib/include/internal/tests.h b/tools/perf/lib/include/internal/tests.h
index b7a20cd24ee1..2093e8868a67 100644
--- a/tools/perf/lib/include/internal/tests.h
+++ b/tools/perf/lib/include/internal/tests.h
@@ -4,14 +4,28 @@

#include <stdio.h>

-#define __T_START fprintf(stdout, "- running %s...", __FILE__)
-#define __T_OK fprintf(stdout, "OK\n")
-#define __T_FAIL fprintf(stdout, "FAIL\n")
+int tests_failed;
+
+#define __T_START \
+do { \
+ fprintf(stdout, "- running %s...", __FILE__); \
+ fflush(NULL); \
+ tests_failed = 0; \
+} while (0)
+
+#define __T_END \
+do { \
+ if (tests_failed) \
+ fprintf(stdout, " FAILED (%d)\n", tests_failed); \
+ else \
+ fprintf(stdout, "OK\n"); \
+} while (0)

#define __T(text, cond) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
+ tests_failed++; \
return -1; \
} \
} while (0)
diff --git a/tools/perf/lib/tests/test-cpumap.c b/tools/perf/lib/tests/test-cpumap.c
index aa34c20df07e..c8d45091e7c2 100644
--- a/tools/perf/lib/tests/test-cpumap.c
+++ b/tools/perf/lib/tests/test-cpumap.c
@@ -26,6 +26,6 @@ int main(int argc, char **argv)
perf_cpu_map__put(cpus);
perf_cpu_map__put(cpus);

- __T_OK;
+ __T_END;
return 0;
}
diff --git a/tools/perf/lib/tests/test-evlist.c b/tools/perf/lib/tests/test-evlist.c
index d8c52ebfa53a..9a80c310ac0f 100644
--- a/tools/perf/lib/tests/test-evlist.c
+++ b/tools/perf/lib/tests/test-evlist.c
@@ -407,6 +407,6 @@ int main(int argc, char **argv)
test_mmap_thread();
test_mmap_cpus();

- __T_OK;
+ __T_END;
return 0;
}
diff --git a/tools/perf/lib/tests/test-evsel.c b/tools/perf/lib/tests/test-evsel.c
index 1b6c4285ac2b..135722ac965b 100644
--- a/tools/perf/lib/tests/test-evsel.c
+++ b/tools/perf/lib/tests/test-evsel.c
@@ -130,6 +130,6 @@ int main(int argc, char **argv)
test_stat_thread();
test_stat_thread_enable();

- __T_OK;
+ __T_END;
return 0;
}
diff --git a/tools/perf/lib/tests/test-threadmap.c b/tools/perf/lib/tests/test-threadmap.c
index 8c5f47247d9e..7dc4d6fbedde 100644
--- a/tools/perf/lib/tests/test-threadmap.c
+++ b/tools/perf/lib/tests/test-threadmap.c
@@ -26,6 +26,6 @@ int main(int argc, char **argv)
perf_thread_map__put(threads);
perf_thread_map__put(threads);

- __T_OK;
+ __T_END;
return 0;
}
--
2.21.0

2019-10-07 12:58:53

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 24/36] libperf: Add perf_evlist__exit function

Adding perf_evlist__exit function, so far it's not
exported and added only for internal use for perf
and libperf.

Using it to release cpus/threads and pollfd array.

Link: http://lkml.kernel.org/n/[email protected]
Signed-off-by: Jiri Olsa <[email protected]>
---
tools/perf/lib/evlist.c | 12 +++++++++++-
tools/perf/lib/include/internal/evlist.h | 2 ++
tools/perf/util/evlist.c | 6 +-----
3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index 5ae1da97d2e6..7ba98f0e6365 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -109,13 +109,23 @@ perf_evlist__next(struct perf_evlist *evlist, struct perf_evsel *prev)
return next;
}

+void perf_evlist__exit(struct perf_evlist *evlist)
+{
+ perf_cpu_map__put(evlist->cpus);
+ perf_thread_map__put(evlist->threads);
+ evlist->cpus = NULL;
+ evlist->threads = NULL;
+ fdarray__exit(&evlist->pollfd);
+}
+
void perf_evlist__delete(struct perf_evlist *evlist)
{
if (evlist == NULL)
return;

perf_evlist__munmap(evlist);
- fdarray__exit(&evlist->pollfd);
+ perf_evlist__close(evlist);
+ perf_evlist__exit(evlist);
free(evlist);
}

diff --git a/tools/perf/lib/include/internal/evlist.h b/tools/perf/lib/include/internal/evlist.h
index b2019700cdc0..0721512ffb19 100644
--- a/tools/perf/lib/include/internal/evlist.h
+++ b/tools/perf/lib/include/internal/evlist.h
@@ -48,6 +48,8 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
struct perf_evlist_mmap_ops *ops,
struct perf_mmap_param *mp);

+void perf_evlist__exit(struct perf_evlist *evlist);
+
/**
* __perf_evlist__for_each_entry - iterate thru all the evsels
* @list: list_head instance to iterate
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 71679cce7257..3669ca767340 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -138,7 +138,7 @@ void evlist__exit(struct evlist *evlist)
{
zfree(&evlist->mmap);
zfree(&evlist->overwrite_mmap);
- fdarray__exit(&evlist->core.pollfd);
+ perf_evlist__exit(&evlist->core);
}

void evlist__delete(struct evlist *evlist)
@@ -148,10 +148,6 @@ void evlist__delete(struct evlist *evlist)

evlist__munmap(evlist);
evlist__close(evlist);
- perf_cpu_map__put(evlist->core.cpus);
- perf_thread_map__put(evlist->core.threads);
- evlist->core.cpus = NULL;
- evlist->core.threads = NULL;
evlist__purge(evlist);
evlist__exit(evlist);
free(evlist);
--
2.21.0

2019-10-07 12:59:19

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 15/36] libperf: Add perf_evlist_mmap_ops::idx callback

Add the perf_evlist_mmap_ops::idx callback to be called in mmap_per_cpu
and mmap_per_thread with current cpu and thread indexes.

It's used by current aux code, so perf will use this callback to set the
aux index.

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Michael Petlan <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/lib/evlist.c | 18 +++++++++++++-----
tools/perf/lib/include/internal/evlist.h | 4 ++++
2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index 88d63f5cd9ca..3832d3e9a3b4 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -426,7 +426,8 @@ mmap_per_evsel(struct perf_evlist *evlist, int idx,
}

static int
-mmap_per_thread(struct perf_evlist *evlist, struct perf_mmap_param *mp)
+mmap_per_thread(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
+ struct perf_mmap_param *mp)
{
int thread;
int nr_threads = perf_thread_map__nr(evlist->threads);
@@ -435,6 +436,9 @@ mmap_per_thread(struct perf_evlist *evlist, struct perf_mmap_param *mp)
int output = -1;
int output_overwrite = -1;

+ if (ops->idx)
+ ops->idx(evlist, mp, thread, false);
+
if (mmap_per_evsel(evlist, thread, mp, 0, thread,
&output, &output_overwrite))
goto out_unmap;
@@ -448,7 +452,8 @@ mmap_per_thread(struct perf_evlist *evlist, struct perf_mmap_param *mp)
}

static int
-mmap_per_cpu(struct perf_evlist *evlist, struct perf_mmap_param *mp)
+mmap_per_cpu(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
+ struct perf_mmap_param *mp)
{
int nr_threads = perf_thread_map__nr(evlist->threads);
int nr_cpus = perf_cpu_map__nr(evlist->cpus);
@@ -458,6 +463,9 @@ mmap_per_cpu(struct perf_evlist *evlist, struct perf_mmap_param *mp)
int output = -1;
int output_overwrite = -1;

+ if (ops->idx)
+ ops->idx(evlist, mp, cpu, true);
+
for (thread = 0; thread < nr_threads; thread++) {
if (mmap_per_evsel(evlist, cpu, mp, cpu,
thread, &output, &output_overwrite))
@@ -496,15 +504,15 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
}

if (perf_cpu_map__empty(cpus))
- return mmap_per_thread(evlist, mp);
+ return mmap_per_thread(evlist, ops, mp);

- return mmap_per_cpu(evlist, mp);
+ return mmap_per_cpu(evlist, ops, mp);
}

int perf_evlist__mmap(struct perf_evlist *evlist, int pages)
{
struct perf_mmap_param mp;
- struct perf_evlist_mmap_ops ops;
+ struct perf_evlist_mmap_ops ops = { 0 };

evlist->mmap_len = (pages + 1) * page_size;
mp.mask = evlist->mmap_len - page_size - 1;
diff --git a/tools/perf/lib/include/internal/evlist.h b/tools/perf/lib/include/internal/evlist.h
index e5f092ff6202..053f620696f3 100644
--- a/tools/perf/lib/include/internal/evlist.h
+++ b/tools/perf/lib/include/internal/evlist.h
@@ -27,7 +27,11 @@ struct perf_evlist {
struct perf_mmap *mmap_ovw;
};

+typedef void
+(*perf_evlist_mmap__cb_idx_t)(struct perf_evlist*, struct perf_mmap_param*, int, bool);
+
struct perf_evlist_mmap_ops {
+ perf_evlist_mmap__cb_idx_t idx;
};

int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
--
2.21.0

2019-10-07 12:59:47

by Jiri Olsa

[permalink] [raw]
Subject: [PATCH 03/36] libperf: Add perf_mmap__mmap_len() function

Move perf_mmap__mmap_len() from tools/perf wto libperf, it will be used
in the following patches. And rename the existing perf's function to
mmap__mmap_len().

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Michael Petlan <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-record.c | 4 ++--
tools/perf/lib/include/internal/mmap.h | 2 ++
tools/perf/lib/mmap.c | 6 ++++++
tools/perf/util/mmap.c | 20 ++++++++++----------
tools/perf/util/mmap.h | 2 +-
5 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 23332861de6e..f05e8b7955e4 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -276,7 +276,7 @@ static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size

if (record__comp_enabled(aio->rec)) {
size = zstd_compress(aio->rec->session, aio->data + aio->size,
- perf_mmap__mmap_len(map) - aio->size,
+ mmap__mmap_len(map) - aio->size,
buf, size);
} else {
memcpy(aio->data + aio->size, buf, size);
@@ -488,7 +488,7 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
struct record *rec = to;

if (record__comp_enabled(rec)) {
- size = zstd_compress(rec->session, map->data, perf_mmap__mmap_len(map), bf, size);
+ size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size);
bf = map->data;
}

diff --git a/tools/perf/lib/include/internal/mmap.h b/tools/perf/lib/include/internal/mmap.h
index b26806b36bb6..e7a67260940c 100644
--- a/tools/perf/lib/include/internal/mmap.h
+++ b/tools/perf/lib/include/internal/mmap.h
@@ -34,6 +34,8 @@ struct perf_mmap_param {
int mask;
};

+size_t perf_mmap__mmap_len(struct perf_mmap *map);
+
void perf_mmap__init(struct perf_mmap *map, bool overwrite);

#endif /* __LIBPERF_INTERNAL_MMAP_H */
diff --git a/tools/perf/lib/mmap.c b/tools/perf/lib/mmap.c
index 3da6177510e6..cc4284da4d99 100644
--- a/tools/perf/lib/mmap.c
+++ b/tools/perf/lib/mmap.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <internal/mmap.h>
+#include <internal/lib.h>

void perf_mmap__init(struct perf_mmap *map, bool overwrite)
{
@@ -7,3 +8,8 @@ void perf_mmap__init(struct perf_mmap *map, bool overwrite)
map->overwrite = overwrite;
refcount_set(&map->refcnt, 0);
}
+
+size_t perf_mmap__mmap_len(struct perf_mmap *map)
+{
+ return map->mask + 1 + page_size;
+}
diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
index a496ced5ed2a..a8e81c4cbae8 100644
--- a/tools/perf/util/mmap.c
+++ b/tools/perf/util/mmap.c
@@ -23,9 +23,9 @@
#include "../perf.h"
#include <internal/lib.h> /* page_size */

-size_t perf_mmap__mmap_len(struct mmap *map)
+size_t mmap__mmap_len(struct mmap *map)
{
- return map->core.mask + 1 + page_size;
+ return perf_mmap__mmap_len(&map->core);
}

/* When check_messup is true, 'end' must points to a good entry */
@@ -170,7 +170,7 @@ static int perf_mmap__aio_enabled(struct mmap *map)
#ifdef HAVE_LIBNUMA_SUPPORT
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
{
- map->aio.data[idx] = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
+ map->aio.data[idx] = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map->aio.data[idx] == MAP_FAILED) {
map->aio.data[idx] = NULL;
@@ -183,7 +183,7 @@ static int perf_mmap__aio_alloc(struct mmap *map, int idx)
static void perf_mmap__aio_free(struct mmap *map, int idx)
{
if (map->aio.data[idx]) {
- munmap(map->aio.data[idx], perf_mmap__mmap_len(map));
+ munmap(map->aio.data[idx], mmap__mmap_len(map));
map->aio.data[idx] = NULL;
}
}
@@ -196,7 +196,7 @@ static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)

if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
data = map->aio.data[idx];
- mmap_len = perf_mmap__mmap_len(map);
+ mmap_len = mmap__mmap_len(map);
node_mask = 1UL << cpu__get_node(cpu);
if (mbind(data, mmap_len, MPOL_BIND, &node_mask, 1, 0)) {
pr_err("Failed to bind [%p-%p] AIO buffer to node %d: error %m\n",
@@ -210,7 +210,7 @@ static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)
#else /* !HAVE_LIBNUMA_SUPPORT */
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
{
- map->aio.data[idx] = malloc(perf_mmap__mmap_len(map));
+ map->aio.data[idx] = malloc(mmap__mmap_len(map));
if (map->aio.data[idx] == NULL)
return -1;

@@ -315,11 +315,11 @@ void perf_mmap__munmap(struct mmap *map)
{
perf_mmap__aio_munmap(map);
if (map->data != NULL) {
- munmap(map->data, perf_mmap__mmap_len(map));
+ munmap(map->data, mmap__mmap_len(map));
map->data = NULL;
}
if (map->core.base != NULL) {
- munmap(map->core.base, perf_mmap__mmap_len(map));
+ munmap(map->core.base, mmap__mmap_len(map));
map->core.base = NULL;
map->core.fd = -1;
refcount_set(&map->core.refcnt, 0);
@@ -371,7 +371,7 @@ int perf_mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
refcount_set(&map->core.refcnt, 2);
map->core.prev = 0;
map->core.mask = mp->core.mask;
- map->core.base = mmap(NULL, perf_mmap__mmap_len(map), mp->core.prot,
+ map->core.base = mmap(NULL, mmap__mmap_len(map), mp->core.prot,
MAP_SHARED, fd, 0);
if (map->core.base == MAP_FAILED) {
pr_debug2("failed to mmap perf event ring buffer, error %d\n",
@@ -389,7 +389,7 @@ int perf_mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
map->comp_level = mp->comp_level;

if (map->comp_level && !perf_mmap__aio_enabled(map)) {
- map->data = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
+ map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map->data == MAP_FAILED) {
pr_debug2("failed to mmap data buffer, error %d\n",
diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index 4ff75d8aeb05..2b97dc6d9ee2 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -67,7 +67,7 @@ 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));

-size_t perf_mmap__mmap_len(struct mmap *map);
+size_t mmap__mmap_len(struct mmap *map);

int perf_mmap__read_init(struct mmap *md);
void perf_mmap__read_done(struct mmap *map);
--
2.21.0

Subject: [tip: perf/core] libperf: Adopt perf_mmap__mmap_len() function from tools/perf

The following commit has been merged into the perf/core branch of tip:

Commit-ID: bf59b3053e63783520c2810fc3f676553bc7eedd
Gitweb: https://git.kernel.org/tip/bf59b3053e63783520c2810fc3f676553bc7eedd
Author: Jiri Olsa <[email protected]>
AuthorDate: Mon, 07 Oct 2019 14:53:11 +02:00
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Thu, 10 Oct 2019 09:41:38 -03:00

libperf: Adopt perf_mmap__mmap_len() function from tools/perf

Move perf_mmap__mmap_len() from tools/perf wto libperf, it will be used
in the following patches. And rename the existing perf's function to
mmap__mmap_len().

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Michael Petlan <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-record.c | 4 ++--
tools/perf/lib/include/internal/mmap.h | 2 ++
tools/perf/lib/mmap.c | 6 ++++++
tools/perf/util/mmap.c | 20 ++++++++++----------
tools/perf/util/mmap.h | 2 +-
5 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 2333286..f05e8b7 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -276,7 +276,7 @@ static int record__aio_pushfn(struct mmap *map, void *to, void *buf, size_t size

if (record__comp_enabled(aio->rec)) {
size = zstd_compress(aio->rec->session, aio->data + aio->size,
- perf_mmap__mmap_len(map) - aio->size,
+ mmap__mmap_len(map) - aio->size,
buf, size);
} else {
memcpy(aio->data + aio->size, buf, size);
@@ -488,7 +488,7 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size)
struct record *rec = to;

if (record__comp_enabled(rec)) {
- size = zstd_compress(rec->session, map->data, perf_mmap__mmap_len(map), bf, size);
+ size = zstd_compress(rec->session, map->data, mmap__mmap_len(map), bf, size);
bf = map->data;
}

diff --git a/tools/perf/lib/include/internal/mmap.h b/tools/perf/lib/include/internal/mmap.h
index b26806b..e7a6726 100644
--- a/tools/perf/lib/include/internal/mmap.h
+++ b/tools/perf/lib/include/internal/mmap.h
@@ -34,6 +34,8 @@ struct perf_mmap_param {
int mask;
};

+size_t perf_mmap__mmap_len(struct perf_mmap *map);
+
void perf_mmap__init(struct perf_mmap *map, bool overwrite);

#endif /* __LIBPERF_INTERNAL_MMAP_H */
diff --git a/tools/perf/lib/mmap.c b/tools/perf/lib/mmap.c
index 3da6177..cc4284d 100644
--- a/tools/perf/lib/mmap.c
+++ b/tools/perf/lib/mmap.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <internal/mmap.h>
+#include <internal/lib.h>

void perf_mmap__init(struct perf_mmap *map, bool overwrite)
{
@@ -7,3 +8,8 @@ void perf_mmap__init(struct perf_mmap *map, bool overwrite)
map->overwrite = overwrite;
refcount_set(&map->refcnt, 0);
}
+
+size_t perf_mmap__mmap_len(struct perf_mmap *map)
+{
+ return map->mask + 1 + page_size;
+}
diff --git a/tools/perf/util/mmap.c b/tools/perf/util/mmap.c
index a496ced..a8e81c4 100644
--- a/tools/perf/util/mmap.c
+++ b/tools/perf/util/mmap.c
@@ -23,9 +23,9 @@
#include "../perf.h"
#include <internal/lib.h> /* page_size */

-size_t perf_mmap__mmap_len(struct mmap *map)
+size_t mmap__mmap_len(struct mmap *map)
{
- return map->core.mask + 1 + page_size;
+ return perf_mmap__mmap_len(&map->core);
}

/* When check_messup is true, 'end' must points to a good entry */
@@ -170,7 +170,7 @@ static int perf_mmap__aio_enabled(struct mmap *map)
#ifdef HAVE_LIBNUMA_SUPPORT
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
{
- map->aio.data[idx] = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
+ map->aio.data[idx] = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map->aio.data[idx] == MAP_FAILED) {
map->aio.data[idx] = NULL;
@@ -183,7 +183,7 @@ static int perf_mmap__aio_alloc(struct mmap *map, int idx)
static void perf_mmap__aio_free(struct mmap *map, int idx)
{
if (map->aio.data[idx]) {
- munmap(map->aio.data[idx], perf_mmap__mmap_len(map));
+ munmap(map->aio.data[idx], mmap__mmap_len(map));
map->aio.data[idx] = NULL;
}
}
@@ -196,7 +196,7 @@ static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)

if (affinity != PERF_AFFINITY_SYS && cpu__max_node() > 1) {
data = map->aio.data[idx];
- mmap_len = perf_mmap__mmap_len(map);
+ mmap_len = mmap__mmap_len(map);
node_mask = 1UL << cpu__get_node(cpu);
if (mbind(data, mmap_len, MPOL_BIND, &node_mask, 1, 0)) {
pr_err("Failed to bind [%p-%p] AIO buffer to node %d: error %m\n",
@@ -210,7 +210,7 @@ static int perf_mmap__aio_bind(struct mmap *map, int idx, int cpu, int affinity)
#else /* !HAVE_LIBNUMA_SUPPORT */
static int perf_mmap__aio_alloc(struct mmap *map, int idx)
{
- map->aio.data[idx] = malloc(perf_mmap__mmap_len(map));
+ map->aio.data[idx] = malloc(mmap__mmap_len(map));
if (map->aio.data[idx] == NULL)
return -1;

@@ -315,11 +315,11 @@ void perf_mmap__munmap(struct mmap *map)
{
perf_mmap__aio_munmap(map);
if (map->data != NULL) {
- munmap(map->data, perf_mmap__mmap_len(map));
+ munmap(map->data, mmap__mmap_len(map));
map->data = NULL;
}
if (map->core.base != NULL) {
- munmap(map->core.base, perf_mmap__mmap_len(map));
+ munmap(map->core.base, mmap__mmap_len(map));
map->core.base = NULL;
map->core.fd = -1;
refcount_set(&map->core.refcnt, 0);
@@ -371,7 +371,7 @@ int perf_mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
refcount_set(&map->core.refcnt, 2);
map->core.prev = 0;
map->core.mask = mp->core.mask;
- map->core.base = mmap(NULL, perf_mmap__mmap_len(map), mp->core.prot,
+ map->core.base = mmap(NULL, mmap__mmap_len(map), mp->core.prot,
MAP_SHARED, fd, 0);
if (map->core.base == MAP_FAILED) {
pr_debug2("failed to mmap perf event ring buffer, error %d\n",
@@ -389,7 +389,7 @@ int perf_mmap__mmap(struct mmap *map, struct mmap_params *mp, int fd, int cpu)
map->comp_level = mp->comp_level;

if (map->comp_level && !perf_mmap__aio_enabled(map)) {
- map->data = mmap(NULL, perf_mmap__mmap_len(map), PROT_READ|PROT_WRITE,
+ map->data = mmap(NULL, mmap__mmap_len(map), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
if (map->data == MAP_FAILED) {
pr_debug2("failed to mmap data buffer, error %d\n",
diff --git a/tools/perf/util/mmap.h b/tools/perf/util/mmap.h
index 4ff75d8..2b97dc6 100644
--- a/tools/perf/util/mmap.h
+++ b/tools/perf/util/mmap.h
@@ -67,7 +67,7 @@ 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));

-size_t perf_mmap__mmap_len(struct mmap *map);
+size_t mmap__mmap_len(struct mmap *map);

int perf_mmap__read_init(struct mmap *md);
void perf_mmap__read_done(struct mmap *map);

Subject: [tip: perf/core] libperf: Introduce perf_evlist_mmap_ops::idx callback

The following commit has been merged into the perf/core branch of tip:

Commit-ID: 1fcbb75cc574072ab457dbbaa74fc7064b691e86
Gitweb: https://git.kernel.org/tip/1fcbb75cc574072ab457dbbaa74fc7064b691e86
Author: Jiri Olsa <[email protected]>
AuthorDate: Mon, 07 Oct 2019 14:53:23 +02:00
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Thu, 10 Oct 2019 12:20:08 -03:00

libperf: Introduce perf_evlist_mmap_ops::idx callback

Add the perf_evlist_mmap_ops::idx callback to be called in
mmap_per_cpu() and mmap_per_thread() with current cpu and thread
indexes.

It's used by current aux code, so perf will use this callback to set the
aux index.

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Michael Petlan <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/lib/evlist.c | 18 +++++++++++++-----
tools/perf/lib/include/internal/evlist.h | 4 ++++
2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index 88d63f5..3832d3e 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -426,7 +426,8 @@ mmap_per_evsel(struct perf_evlist *evlist, int idx,
}

static int
-mmap_per_thread(struct perf_evlist *evlist, struct perf_mmap_param *mp)
+mmap_per_thread(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
+ struct perf_mmap_param *mp)
{
int thread;
int nr_threads = perf_thread_map__nr(evlist->threads);
@@ -435,6 +436,9 @@ mmap_per_thread(struct perf_evlist *evlist, struct perf_mmap_param *mp)
int output = -1;
int output_overwrite = -1;

+ if (ops->idx)
+ ops->idx(evlist, mp, thread, false);
+
if (mmap_per_evsel(evlist, thread, mp, 0, thread,
&output, &output_overwrite))
goto out_unmap;
@@ -448,7 +452,8 @@ out_unmap:
}

static int
-mmap_per_cpu(struct perf_evlist *evlist, struct perf_mmap_param *mp)
+mmap_per_cpu(struct perf_evlist *evlist, struct perf_evlist_mmap_ops *ops,
+ struct perf_mmap_param *mp)
{
int nr_threads = perf_thread_map__nr(evlist->threads);
int nr_cpus = perf_cpu_map__nr(evlist->cpus);
@@ -458,6 +463,9 @@ mmap_per_cpu(struct perf_evlist *evlist, struct perf_mmap_param *mp)
int output = -1;
int output_overwrite = -1;

+ if (ops->idx)
+ ops->idx(evlist, mp, cpu, true);
+
for (thread = 0; thread < nr_threads; thread++) {
if (mmap_per_evsel(evlist, cpu, mp, cpu,
thread, &output, &output_overwrite))
@@ -496,15 +504,15 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
}

if (perf_cpu_map__empty(cpus))
- return mmap_per_thread(evlist, mp);
+ return mmap_per_thread(evlist, ops, mp);

- return mmap_per_cpu(evlist, mp);
+ return mmap_per_cpu(evlist, ops, mp);
}

int perf_evlist__mmap(struct perf_evlist *evlist, int pages)
{
struct perf_mmap_param mp;
- struct perf_evlist_mmap_ops ops;
+ struct perf_evlist_mmap_ops ops = { 0 };

evlist->mmap_len = (pages + 1) * page_size;
mp.mask = evlist->mmap_len - page_size - 1;
diff --git a/tools/perf/lib/include/internal/evlist.h b/tools/perf/lib/include/internal/evlist.h
index e5f092f..053f620 100644
--- a/tools/perf/lib/include/internal/evlist.h
+++ b/tools/perf/lib/include/internal/evlist.h
@@ -27,7 +27,11 @@ struct perf_evlist {
struct perf_mmap *mmap_ovw;
};

+typedef void
+(*perf_evlist_mmap__cb_idx_t)(struct perf_evlist*, struct perf_mmap_param*, int, bool);
+
struct perf_evlist_mmap_ops {
+ perf_evlist_mmap__cb_idx_t idx;
};

int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);

Subject: [tip: perf/core] libperf: Introduce perf_evlist__exit()

The following commit has been merged into the perf/core branch of tip:

Commit-ID: 93dd6e2831ff399f7685aa2157b997b6392efac8
Gitweb: https://git.kernel.org/tip/93dd6e2831ff399f7685aa2157b997b6392efac8
Author: Jiri Olsa <[email protected]>
AuthorDate: Mon, 07 Oct 2019 14:53:32 +02:00
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Thu, 10 Oct 2019 12:56:01 -03:00

libperf: Introduce perf_evlist__exit()

Add the perf_evlist__exit() function, so far it's not exported and added
only for internal use for perf and libperf.

USe it to release cpus/threads and pollfd array.

Signed-off-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Michael Petlan <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Link: http://lore.kernel.org/lkml/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/lib/evlist.c | 12 +++++++++++-
tools/perf/lib/include/internal/evlist.h | 2 ++
tools/perf/util/evlist.c | 6 +-----
3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/tools/perf/lib/evlist.c b/tools/perf/lib/evlist.c
index 5ae1da9..7ba98f0 100644
--- a/tools/perf/lib/evlist.c
+++ b/tools/perf/lib/evlist.c
@@ -109,13 +109,23 @@ perf_evlist__next(struct perf_evlist *evlist, struct perf_evsel *prev)
return next;
}

+void perf_evlist__exit(struct perf_evlist *evlist)
+{
+ perf_cpu_map__put(evlist->cpus);
+ perf_thread_map__put(evlist->threads);
+ evlist->cpus = NULL;
+ evlist->threads = NULL;
+ fdarray__exit(&evlist->pollfd);
+}
+
void perf_evlist__delete(struct perf_evlist *evlist)
{
if (evlist == NULL)
return;

perf_evlist__munmap(evlist);
- fdarray__exit(&evlist->pollfd);
+ perf_evlist__close(evlist);
+ perf_evlist__exit(evlist);
free(evlist);
}

diff --git a/tools/perf/lib/include/internal/evlist.h b/tools/perf/lib/include/internal/evlist.h
index b201970..0721512 100644
--- a/tools/perf/lib/include/internal/evlist.h
+++ b/tools/perf/lib/include/internal/evlist.h
@@ -48,6 +48,8 @@ int perf_evlist__mmap_ops(struct perf_evlist *evlist,
struct perf_evlist_mmap_ops *ops,
struct perf_mmap_param *mp);

+void perf_evlist__exit(struct perf_evlist *evlist);
+
/**
* __perf_evlist__for_each_entry - iterate thru all the evsels
* @list: list_head instance to iterate
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 5192c65..031ace3 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -138,7 +138,7 @@ void evlist__exit(struct evlist *evlist)
{
zfree(&evlist->mmap);
zfree(&evlist->overwrite_mmap);
- fdarray__exit(&evlist->core.pollfd);
+ perf_evlist__exit(&evlist->core);
}

void evlist__delete(struct evlist *evlist)
@@ -148,10 +148,6 @@ void evlist__delete(struct evlist *evlist)

evlist__munmap(evlist);
evlist__close(evlist);
- perf_cpu_map__put(evlist->core.cpus);
- perf_thread_map__put(evlist->core.threads);
- evlist->core.cpus = NULL;
- evlist->core.threads = NULL;
evlist__purge(evlist);
evlist__exit(evlist);
free(evlist);