2013-03-15 13:58:11 +08:00
|
|
|
#include "evlist.h"
|
|
|
|
#include "evsel.h"
|
|
|
|
#include "thread_map.h"
|
|
|
|
#include "cpumap.h"
|
|
|
|
#include "tests.h"
|
|
|
|
|
2017-04-18 21:46:11 +08:00
|
|
|
#include <errno.h>
|
2013-03-15 13:58:11 +08:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
static int exited;
|
|
|
|
static int nr_exit;
|
|
|
|
|
2014-01-04 01:56:49 +08:00
|
|
|
static void sig_handler(int sig __maybe_unused)
|
2013-03-15 13:58:11 +08:00
|
|
|
{
|
|
|
|
exited = 1;
|
2014-01-04 01:56:49 +08:00
|
|
|
}
|
2013-03-15 13:58:11 +08:00
|
|
|
|
2014-01-04 01:56:49 +08:00
|
|
|
/*
|
|
|
|
* perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
|
|
|
|
* we asked by setting its exec_error to this handler.
|
|
|
|
*/
|
|
|
|
static void workload_exec_failed_signal(int signo __maybe_unused,
|
|
|
|
siginfo_t *info __maybe_unused,
|
|
|
|
void *ucontext __maybe_unused)
|
|
|
|
{
|
|
|
|
exited = 1;
|
|
|
|
nr_exit = -1;
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test will start a workload that does nothing then it checks
|
|
|
|
* if the number of exit event reported by the kernel is 1 or not
|
|
|
|
* in order to check the kernel returns correct number of event.
|
|
|
|
*/
|
2017-08-04 02:16:31 +08:00
|
|
|
int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused)
|
2013-03-15 13:58:11 +08:00
|
|
|
{
|
|
|
|
int err = -1;
|
|
|
|
union perf_event *event;
|
|
|
|
struct perf_evsel *evsel;
|
|
|
|
struct perf_evlist *evlist;
|
2013-11-13 03:46:16 +08:00
|
|
|
struct target target = {
|
2013-03-15 13:58:11 +08:00
|
|
|
.uid = UINT_MAX,
|
|
|
|
.uses_mmap = true,
|
|
|
|
};
|
|
|
|
const char *argv[] = { "true", NULL };
|
2014-08-14 10:22:45 +08:00
|
|
|
char sbuf[STRERR_BUFSIZE];
|
2015-09-08 15:59:01 +08:00
|
|
|
struct cpu_map *cpus;
|
|
|
|
struct thread_map *threads;
|
2013-03-15 13:58:11 +08:00
|
|
|
|
|
|
|
signal(SIGCHLD, sig_handler);
|
|
|
|
|
2013-09-01 18:36:14 +08:00
|
|
|
evlist = perf_evlist__new_default();
|
2013-03-15 13:58:11 +08:00
|
|
|
if (evlist == NULL) {
|
2013-09-01 18:36:14 +08:00
|
|
|
pr_debug("perf_evlist__new_default\n");
|
2013-03-15 13:58:11 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create maps of threads and cpus to monitor. In this case
|
|
|
|
* we start with all threads and cpus (-1, -1) but then in
|
|
|
|
* perf_evlist__prepare_workload we'll fill in the only thread
|
|
|
|
* we're monitoring, the one forked there.
|
|
|
|
*/
|
2015-09-08 15:59:01 +08:00
|
|
|
cpus = cpu_map__dummy_new();
|
|
|
|
threads = thread_map__new_by_tid(-1);
|
|
|
|
if (!cpus || !threads) {
|
2013-03-15 13:58:11 +08:00
|
|
|
err = -ENOMEM;
|
|
|
|
pr_debug("Not enough memory to create thread/cpu maps\n");
|
2015-09-08 15:59:01 +08:00
|
|
|
goto out_free_maps;
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:59:01 +08:00
|
|
|
perf_evlist__set_maps(evlist, cpus, threads);
|
|
|
|
|
|
|
|
cpus = NULL;
|
|
|
|
threads = NULL;
|
|
|
|
|
2014-01-04 01:56:49 +08:00
|
|
|
err = perf_evlist__prepare_workload(evlist, &target, argv, false,
|
|
|
|
workload_exec_failed_signal);
|
2013-03-15 13:58:11 +08:00
|
|
|
if (err < 0) {
|
|
|
|
pr_debug("Couldn't run the workload!\n");
|
2014-01-04 02:56:06 +08:00
|
|
|
goto out_delete_evlist;
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
evsel = perf_evlist__first(evlist);
|
|
|
|
evsel->attr.task = 1;
|
perf evsel: Fix probing of precise_ip level for default cycles event
Since commit 18e7a45af91a ("perf/x86: Reject non sampling events with
precise_ip") returns -EINVAL for sys_perf_event_open() with an attribute
with (attr.precise_ip > 0 && attr.sample_period == 0), just like is done
in the routine used to probe the max precise level when no events were
passed to 'perf record' or 'perf top', i.e.:
perf_evsel__new_cycles()
perf_event_attr__set_max_precise_ip()
The x86 code, in x86_pmu_hw_config(), which is called all the way from
sys_perf_event_open() did, starting with the aforementioned commit:
/* There's no sense in having PEBS for non sampling events: */
if (!is_sampling_event(event))
return -EINVAL;
Which makes it fail for cycles:ppp, cycles:pp and cycles:p, always using
just the non precise cycles variant.
To make sure that this is the case, I tested it, before this patch,
with:
# perf probe -L x86_pmu_hw_config
<x86_pmu_hw_config@/home/acme/git/linux/arch/x86/events/core.c:0>
0 int x86_pmu_hw_config(struct perf_event *event)
1 {
2 if (event->attr.precise_ip) {
<SNIP>
17 if (event->attr.precise_ip > precise)
18 return -EOPNOTSUPP;
/* There's no sense in having PEBS for non sampling events: */
21 if (!is_sampling_event(event))
22 return -EINVAL;
}
<SNIP>
# perf probe x86_pmu_hw_config:22
Added new events:
probe:x86_pmu_hw_config (on x86_pmu_hw_config:22)
probe:x86_pmu_hw_config_1 (on x86_pmu_hw_config:22)
You can now use it in all perf tools, such as:
perf record -e probe:x86_pmu_hw_config_1 -aR sleep 1
# perf trace -e perf_event_open,probe:x86_pmu_hwconfig*/max-stack=16/ perf record usleep 1
0.000 ( 0.015 ms): perf/4150 perf_event_open(attr_uptr: 0x7ffebc8ba110, cpu: -1, group_fd: -1 ) ...
0.015 ( ): probe:x86_pmu_hw_config:(ffffffff9c0065e1))
x86_pmu_hw_config ([kernel.kallsyms])
hsw_hw_config ([kernel.kallsyms])
x86_pmu_event_init ([kernel.kallsyms])
perf_try_init_event ([kernel.kallsyms])
perf_event_alloc ([kernel.kallsyms])
SYSC_perf_event_open ([kernel.kallsyms])
sys_perf_event_open ([kernel.kallsyms])
do_syscall_64 ([kernel.kallsyms])
return_from_SYSCALL_64 ([kernel.kallsyms])
syscall (/usr/lib64/libc-2.24.so)
perf_event_attr__set_max_precise_ip (/home/acme/bin/perf)
perf_evsel__new_cycles (/home/acme/bin/perf)
perf_evlist__add_default (/home/acme/bin/perf)
cmd_record (/home/acme/bin/perf)
run_builtin (/home/acme/bin/perf)
handle_internal_command (/home/acme/bin/perf)
0.000 ( 0.021 ms): perf/4150 ... [continued]: perf_event_open()) = -1 EINVAL Invalid argument
0.023 ( 0.002 ms): perf/4150 perf_event_open(attr_uptr: 0x7ffebc8ba110, cpu: -1, group_fd: -1 ) ...
0.025 ( ): probe:x86_pmu_hw_config:(ffffffff9c0065e1))
x86_pmu_hw_config ([kernel.kallsyms])
hsw_hw_config ([kernel.kallsyms])
x86_pmu_event_init ([kernel.kallsyms])
perf_try_init_event ([kernel.kallsyms])
perf_event_alloc ([kernel.kallsyms])
SYSC_perf_event_open ([kernel.kallsyms])
sys_perf_event_open ([kernel.kallsyms])
do_syscall_64 ([kernel.kallsyms])
return_from_SYSCALL_64 ([kernel.kallsyms])
syscall (/usr/lib64/libc-2.24.so)
perf_event_attr__set_max_precise_ip (/home/acme/bin/perf)
perf_evsel__new_cycles (/home/acme/bin/perf)
perf_evlist__add_default (/home/acme/bin/perf)
cmd_record (/home/acme/bin/perf)
run_builtin (/home/acme/bin/perf)
handle_internal_command (/home/acme/bin/perf)
0.023 ( 0.004 ms): perf/4150 ... [continued]: perf_event_open()) = -1 EINVAL Invalid argument
0.028 ( 0.002 ms): perf/4150 perf_event_open(attr_uptr: 0x7ffebc8ba110, cpu: -1, group_fd: -1 ) ...
0.030 ( ): probe:x86_pmu_hw_config:(ffffffff9c0065e1))
x86_pmu_hw_config ([kernel.kallsyms])
hsw_hw_config ([kernel.kallsyms])
x86_pmu_event_init ([kernel.kallsyms])
perf_try_init_event ([kernel.kallsyms])
perf_event_alloc ([kernel.kallsyms])
SYSC_perf_event_open ([kernel.kallsyms])
sys_perf_event_open ([kernel.kallsyms])
do_syscall_64 ([kernel.kallsyms])
return_from_SYSCALL_64 ([kernel.kallsyms])
syscall (/usr/lib64/libc-2.24.so)
perf_event_attr__set_max_precise_ip (/home/acme/bin/perf)
perf_evsel__new_cycles (/home/acme/bin/perf)
perf_evlist__add_default (/home/acme/bin/perf)
cmd_record (/home/acme/bin/perf)
run_builtin (/home/acme/bin/perf)
handle_internal_command (/home/acme/bin/perf)
0.028 ( 0.004 ms): perf/4150 ... [continued]: perf_event_open()) = -1 EINVAL Invalid argument
41.018 ( 0.012 ms): perf/4150 perf_event_open(attr_uptr: 0x7ffebc8b5dd0, pid: -1, group_fd: -1, flags: FD_CLOEXEC) = 4
41.065 ( 0.011 ms): perf/4150 perf_event_open(attr_uptr: 0x3c7db78, pid: -1, group_fd: -1, flags: FD_CLOEXEC) = 4
41.080 ( 0.006 ms): perf/4150 perf_event_open(attr_uptr: 0x3c7db78, pid: -1, group_fd: -1, flags: FD_CLOEXEC) = 4
41.103 ( 0.010 ms): perf/4150 perf_event_open(attr_uptr: 0x3c4e748, pid: 4151 (perf), group_fd: -1, flags: FD_CLOEXEC) = 4
41.115 ( 0.006 ms): perf/4150 perf_event_open(attr_uptr: 0x3c4e748, pid: 4151 (perf), cpu: 1, group_fd: -1, flags: FD_CLOEXEC) = 5
41.122 ( 0.004 ms): perf/4150 perf_event_open(attr_uptr: 0x3c4e748, pid: 4151 (perf), cpu: 2, group_fd: -1, flags: FD_CLOEXEC) = 6
41.128 ( 0.008 ms): perf/4150 perf_event_open(attr_uptr: 0x3c4e748, pid: 4151 (perf), cpu: 3, group_fd: -1, flags: FD_CLOEXEC) = 8
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.017 MB perf.data (2 samples) ]
#
I.e. that return -EINVAL in x86_pmu_hw_config() is hit three times.
So fix it by just setting attr.sample_period
Now, after this patch:
# perf trace --max-stack=2 -e perf_event_open,probe:x86_pmu_hw_config* perf record usleep 1
[ perf record: Woken up 1 times to write data ]
0.000 ( 0.017 ms): perf/8469 perf_event_open(attr_uptr: 0x7ffe36c27d10, pid: -1, cpu: 3, group_fd: -1, flags: FD_CLOEXEC) = 4
syscall (/usr/lib64/libc-2.24.so)
perf_event_open_cloexec_flag (/home/acme/bin/perf)
0.050 ( 0.031 ms): perf/8469 perf_event_open(attr_uptr: 0x24ebb78, pid: -1, group_fd: -1, flags: FD_CLOEXEC) = 4
syscall (/usr/lib64/libc-2.24.so)
perf_evlist__config (/home/acme/bin/perf)
0.092 ( 0.040 ms): perf/8469 perf_event_open(attr_uptr: 0x24ebb78, pid: -1, group_fd: -1, flags: FD_CLOEXEC) = 4
syscall (/usr/lib64/libc-2.24.so)
perf_evlist__config (/home/acme/bin/perf)
0.143 ( 0.007 ms): perf/8469 perf_event_open(attr_uptr: 0x24bc748, cpu: -1, group_fd: -1 ) = 4
syscall (/usr/lib64/libc-2.24.so)
perf_event_attr__set_max_precise_ip (/home/acme/bin/perf)
0.161 ( 0.007 ms): perf/8469 perf_event_open(attr_uptr: 0x24bc748, pid: 8470 (perf), group_fd: -1, flags: FD_CLOEXEC) = 4
syscall (/usr/lib64/libc-2.24.so)
perf_evsel__open (/home/acme/bin/perf)
0.171 ( 0.005 ms): perf/8469 perf_event_open(attr_uptr: 0x24bc748, pid: 8470 (perf), cpu: 1, group_fd: -1, flags: FD_CLOEXEC) = 5
syscall (/usr/lib64/libc-2.24.so)
perf_evsel__open (/home/acme/bin/perf)
0.180 ( 0.007 ms): perf/8469 perf_event_open(attr_uptr: 0x24bc748, pid: 8470 (perf), cpu: 2, group_fd: -1, flags: FD_CLOEXEC) = 6
syscall (/usr/lib64/libc-2.24.so)
perf_evsel__open (/home/acme/bin/perf)
0.190 ( 0.005 ms): perf/8469 perf_event_open(attr_uptr: 0x24bc748, pid: 8470 (perf), cpu: 3, group_fd: -1, flags: FD_CLOEXEC) = 8
syscall (/usr/lib64/libc-2.24.so)
perf_evsel__open (/home/acme/bin/perf)
[ perf record: Captured and wrote 0.017 MB perf.data (7 samples) ]
#
The probe one called from perf_event_attr__set_max_precise_ip() works
the first time, with attr.precise_ip = 3, wit hthe next ones being the
per cpu ones for the cycles:ppp event.
And here is the text from a report and alternative proposed patch by
Thomas-Mich Richter:
---
On s390 the counter and sampling facility do not support a precise IP
skid level and sometimes returns EOPNOTSUPP when structure member
precise_ip in struct perf_event_attr is not set to zero.
On s390 commnd 'perf record -- true' fails with error EOPNOTSUPP. This
happens only when no events are specified on command line.
The functions called are
...
--> perf_evlist__add_default
--> perf_evsel__new_cycles
--> perf_event_attr__set_max_precise_ip
The last function determines the value of structure member precise_ip by
invoking the perf_event_open() system call and checking the return code.
The first successful open is the value for precise_ip.
However the value is determined without setting member sample_period and
indicates no sampling.
On s390 the counter facility and sampling facility are different. The
above procedure determines a precise_ip value of 3 using the counter
facility. Later it uses the sampling facility with a value of 3 and
fails with EOPNOTSUPP.
---
v2: Older compilers (e.g. gcc 4.4.7) don't support referencing members
of unnamed union members in the container struct initialization, so
move from:
struct perf_event_attr attr = {
...
.sample_period = 1,
};
to right after it as:
struct perf_event_attr attr = {
...
};
attr.sample_period = 1;
v3: We need to reset .sample_period to 0 to let the users of
perf_evsel__new_cycles() to properly setup attr.sample_period or
attr.sample_freq. Reported by Ingo Molnar.
Reported-and-Acked-by: Thomas-Mich Richter <tmricht@linux.vnet.ibm.com>
Acked-by: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Wang Nan <wangnan0@huawei.com>
Fixes: 18e7a45af91a ("perf/x86: Reject non sampling events with precise_ip")
Link: http://lkml.kernel.org/n/tip-yv6nnkl7tzqocrm0hl3x7vf1@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2017-06-10 03:54:28 +08:00
|
|
|
evsel->attr.sample_freq = 1;
|
2013-03-15 13:58:11 +08:00
|
|
|
evsel->attr.inherit = 0;
|
|
|
|
evsel->attr.watermark = 0;
|
|
|
|
evsel->attr.wakeup_events = 1;
|
|
|
|
evsel->attr.exclude_kernel = 1;
|
|
|
|
|
|
|
|
err = perf_evlist__open(evlist);
|
|
|
|
if (err < 0) {
|
2014-08-14 10:22:45 +08:00
|
|
|
pr_debug("Couldn't open the evlist: %s\n",
|
tools: Introduce str_error_r()
The tools so far have been using the strerror_r() GNU variant, that
returns a string, be it the buffer passed or something else.
But that, besides being tricky in cases where we expect that the
function using strerror_r() returns the error formatted in a provided
buffer (we have to check if it returned something else and copy that
instead), breaks the build on systems not using glibc, like Alpine
Linux, where musl libc is used.
So, introduce yet another wrapper, str_error_r(), that has the GNU
interface, but uses the portable XSI variant of strerror_r(), so that
users rest asured that the provided buffer is used and it is what is
returned.
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-d4t42fnf48ytlk8rjxs822tf@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-07-06 22:56:20 +08:00
|
|
|
str_error_r(-err, sbuf, sizeof(sbuf)));
|
2014-01-04 02:56:06 +08:00
|
|
|
goto out_delete_evlist;
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (perf_evlist__mmap(evlist, 128, true) < 0) {
|
|
|
|
pr_debug("failed to mmap events: %d (%s)\n", errno,
|
tools: Introduce str_error_r()
The tools so far have been using the strerror_r() GNU variant, that
returns a string, be it the buffer passed or something else.
But that, besides being tricky in cases where we expect that the
function using strerror_r() returns the error formatted in a provided
buffer (we have to check if it returned something else and copy that
instead), breaks the build on systems not using glibc, like Alpine
Linux, where musl libc is used.
So, introduce yet another wrapper, str_error_r(), that has the GNU
interface, but uses the portable XSI variant of strerror_r(), so that
users rest asured that the provided buffer is used and it is what is
returned.
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-d4t42fnf48ytlk8rjxs822tf@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-07-06 22:56:20 +08:00
|
|
|
str_error_r(errno, sbuf, sizeof(sbuf)));
|
2014-01-04 03:54:12 +08:00
|
|
|
goto out_delete_evlist;
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
perf_evlist__start_workload(evlist);
|
|
|
|
|
|
|
|
retry:
|
|
|
|
while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
|
2013-10-24 15:43:33 +08:00
|
|
|
if (event->header.type == PERF_RECORD_EXIT)
|
|
|
|
nr_exit++;
|
2013-03-15 13:58:11 +08:00
|
|
|
|
2013-10-24 15:43:33 +08:00
|
|
|
perf_evlist__mmap_consume(evlist, 0);
|
2013-03-15 13:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!exited || !nr_exit) {
|
2014-08-19 04:25:59 +08:00
|
|
|
perf_evlist__poll(evlist, -1);
|
2013-03-15 13:58:11 +08:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr_exit != 1) {
|
|
|
|
pr_debug("received %d EXIT records\n", nr_exit);
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
|
2015-09-08 15:59:01 +08:00
|
|
|
out_free_maps:
|
|
|
|
cpu_map__put(cpus);
|
|
|
|
thread_map__put(threads);
|
2014-01-04 02:56:06 +08:00
|
|
|
out_delete_evlist:
|
2013-03-15 13:58:11 +08:00
|
|
|
perf_evlist__delete(evlist);
|
|
|
|
return err;
|
|
|
|
}
|