blob: aba40eb4c56fc5b03fdcc22f6cb043504a45737c [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Arnaldo Carvalho de Melo1fbe7df2016-07-06 12:19:19 -03002/* For the CLR_() macros */
3#include <pthread.h>
4
Jiri Olsaa65b9c62012-11-10 01:46:44 +01005#include "evlist.h"
6#include "evsel.h"
7#include "thread_map.h"
8#include "cpumap.h"
9#include "tests.h"
Jiri Olsa8dd2a132015-09-07 10:38:06 +020010#include <linux/err.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030011#include <linux/kernel.h>
Jiri Olsaa65b9c62012-11-10 01:46:44 +010012
13/*
14 * This test will generate random numbers of calls to some getpid syscalls,
15 * then establish an mmap for a group of events that are created to monitor
16 * the syscalls.
17 *
18 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
19 * sample.id field to map back to its respective perf_evsel instance.
20 *
21 * Then it checks if the number of syscalls reported as perf events by
22 * the kernel corresponds to the number of syscalls made.
23 */
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -030024int test__basic_mmap(int subtest __maybe_unused)
Jiri Olsaa65b9c62012-11-10 01:46:44 +010025{
26 int err = -1;
27 union perf_event *event;
28 struct thread_map *threads;
29 struct cpu_map *cpus;
30 struct perf_evlist *evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010031 cpu_set_t cpu_set;
Riku Voipiocec83932015-04-16 16:52:55 +030032 const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
33 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
Jiri Olsaa65b9c62012-11-10 01:46:44 +010034#define nsyscalls ARRAY_SIZE(syscall_names)
Jiri Olsaa65b9c62012-11-10 01:46:44 +010035 unsigned int nr_events[nsyscalls],
36 expected_nr_events[nsyscalls], i, j;
37 struct perf_evsel *evsels[nsyscalls], *evsel;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000038 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa65b9c62012-11-10 01:46:44 +010039
Jiri Olsaa65b9c62012-11-10 01:46:44 +010040 threads = thread_map__new(-1, getpid(), UINT_MAX);
41 if (threads == NULL) {
42 pr_debug("thread_map__new\n");
43 return -1;
44 }
45
46 cpus = cpu_map__new(NULL);
47 if (cpus == NULL) {
48 pr_debug("cpu_map__new\n");
49 goto out_free_threads;
50 }
51
52 CPU_ZERO(&cpu_set);
53 CPU_SET(cpus->map[0], &cpu_set);
54 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
55 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
56 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030057 cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa65b9c62012-11-10 01:46:44 +010058 goto out_free_cpus;
59 }
60
Namhyung Kim334fe7a2013-03-11 16:43:12 +090061 evlist = perf_evlist__new();
Jiri Olsaa65b9c62012-11-10 01:46:44 +010062 if (evlist == NULL) {
63 pr_debug("perf_evlist__new\n");
64 goto out_free_cpus;
65 }
66
Namhyung Kim334fe7a2013-03-11 16:43:12 +090067 perf_evlist__set_maps(evlist, cpus, threads);
68
Jiri Olsaa65b9c62012-11-10 01:46:44 +010069 for (i = 0; i < nsyscalls; ++i) {
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030070 char name[64];
71
72 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030073 evsels[i] = perf_evsel__newtp("syscalls", name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +020074 if (IS_ERR(evsels[i])) {
Jiri Olsaa65b9c62012-11-10 01:46:44 +010075 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030076 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010077 }
78
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030079 evsels[i]->attr.wakeup_events = 1;
Adrian Hunter75562572013-08-27 11:23:09 +030080 perf_evsel__set_sample_id(evsels[i], false);
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030081
Jiri Olsaa65b9c62012-11-10 01:46:44 +010082 perf_evlist__add(evlist, evsels[i]);
83
84 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
85 pr_debug("failed to open counter: %s, "
86 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030087 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030088 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010089 }
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030090
91 nr_events[i] = 0;
92 expected_nr_events[i] = 1 + rand() % 127;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010093 }
94
95 if (perf_evlist__mmap(evlist, 128, true) < 0) {
96 pr_debug("failed to mmap events: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030097 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030098 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010099 }
100
101 for (i = 0; i < nsyscalls; ++i)
102 for (j = 0; j < expected_nr_events[i]; ++j) {
103 int foo = syscalls[i]();
104 ++foo;
105 }
106
107 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
108 struct perf_sample sample;
109
110 if (event->header.type != PERF_RECORD_SAMPLE) {
111 pr_debug("unexpected %s event\n",
112 perf_event__name(event->header.type));
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300113 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100114 }
115
116 err = perf_evlist__parse_sample(evlist, event, &sample);
117 if (err) {
118 pr_err("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300119 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100120 }
121
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300122 err = -1;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100123 evsel = perf_evlist__id2evsel(evlist, sample.id);
124 if (evsel == NULL) {
125 pr_debug("event with id %" PRIu64
126 " doesn't map to an evsel\n", sample.id);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300127 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100128 }
129 nr_events[evsel->idx]++;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800130 perf_evlist__mmap_consume(evlist, 0);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100131 }
132
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300133 err = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300134 evlist__for_each_entry(evlist, evsel) {
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100135 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
136 pr_debug("expected %d %s events, got %d\n",
137 expected_nr_events[evsel->idx],
138 perf_evsel__name(evsel), nr_events[evsel->idx]);
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300139 err = -1;
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300140 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100141 }
142 }
143
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300144out_delete_evlist:
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100145 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300146 cpus = NULL;
147 threads = NULL;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100148out_free_cpus:
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200149 cpu_map__put(cpus);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100150out_free_threads:
Jiri Olsa186fbb72015-06-23 00:36:05 +0200151 thread_map__put(threads);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100152 return err;
153}