blob: 634bce9caebd343278e24f6c32ab3569f9bc003d [file] [log] [blame]
Arnaldo Carvalho de Melo1fbe7df2016-07-06 12:19:19 -03001/* For the CLR_() macros */
2#include <pthread.h>
3
Jiri Olsaa65b9c62012-11-10 01:46:44 +01004#include "evlist.h"
5#include "evsel.h"
6#include "thread_map.h"
7#include "cpumap.h"
8#include "tests.h"
Jiri Olsa8dd2a132015-09-07 10:38:06 +02009#include <linux/err.h>
Jiri Olsaa65b9c62012-11-10 01:46:44 +010010
11/*
12 * This test will generate random numbers of calls to some getpid syscalls,
13 * then establish an mmap for a group of events that are created to monitor
14 * the syscalls.
15 *
16 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
17 * sample.id field to map back to its respective perf_evsel instance.
18 *
19 * Then it checks if the number of syscalls reported as perf events by
20 * the kernel corresponds to the number of syscalls made.
21 */
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -030022int test__basic_mmap(int subtest __maybe_unused)
Jiri Olsaa65b9c62012-11-10 01:46:44 +010023{
24 int err = -1;
25 union perf_event *event;
26 struct thread_map *threads;
27 struct cpu_map *cpus;
28 struct perf_evlist *evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010029 cpu_set_t cpu_set;
Riku Voipiocec83932015-04-16 16:52:55 +030030 const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
31 pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
Jiri Olsaa65b9c62012-11-10 01:46:44 +010032#define nsyscalls ARRAY_SIZE(syscall_names)
Jiri Olsaa65b9c62012-11-10 01:46:44 +010033 unsigned int nr_events[nsyscalls],
34 expected_nr_events[nsyscalls], i, j;
35 struct perf_evsel *evsels[nsyscalls], *evsel;
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000036 char sbuf[STRERR_BUFSIZE];
Jiri Olsaa65b9c62012-11-10 01:46:44 +010037
Jiri Olsaa65b9c62012-11-10 01:46:44 +010038 threads = thread_map__new(-1, getpid(), UINT_MAX);
39 if (threads == NULL) {
40 pr_debug("thread_map__new\n");
41 return -1;
42 }
43
44 cpus = cpu_map__new(NULL);
45 if (cpus == NULL) {
46 pr_debug("cpu_map__new\n");
47 goto out_free_threads;
48 }
49
50 CPU_ZERO(&cpu_set);
51 CPU_SET(cpus->map[0], &cpu_set);
52 sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
53 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
54 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030055 cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsaa65b9c62012-11-10 01:46:44 +010056 goto out_free_cpus;
57 }
58
Namhyung Kim334fe7a2013-03-11 16:43:12 +090059 evlist = perf_evlist__new();
Jiri Olsaa65b9c62012-11-10 01:46:44 +010060 if (evlist == NULL) {
61 pr_debug("perf_evlist__new\n");
62 goto out_free_cpus;
63 }
64
Namhyung Kim334fe7a2013-03-11 16:43:12 +090065 perf_evlist__set_maps(evlist, cpus, threads);
66
Jiri Olsaa65b9c62012-11-10 01:46:44 +010067 for (i = 0; i < nsyscalls; ++i) {
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030068 char name[64];
69
70 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -030071 evsels[i] = perf_evsel__newtp("syscalls", name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +020072 if (IS_ERR(evsels[i])) {
Jiri Olsaa65b9c62012-11-10 01:46:44 +010073 pr_debug("perf_evsel__new\n");
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030074 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010075 }
76
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030077 evsels[i]->attr.wakeup_events = 1;
Adrian Hunter75562572013-08-27 11:23:09 +030078 perf_evsel__set_sample_id(evsels[i], false);
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030079
Jiri Olsaa65b9c62012-11-10 01:46:44 +010080 perf_evlist__add(evlist, evsels[i]);
81
82 if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
83 pr_debug("failed to open counter: %s, "
84 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030085 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030086 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010087 }
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030088
89 nr_events[i] = 0;
90 expected_nr_events[i] = 1 + rand() % 127;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010091 }
92
93 if (perf_evlist__mmap(evlist, 128, true) < 0) {
94 pr_debug("failed to mmap events: %d (%s)\n", errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030095 str_error_r(errno, sbuf, sizeof(sbuf)));
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -030096 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +010097 }
98
99 for (i = 0; i < nsyscalls; ++i)
100 for (j = 0; j < expected_nr_events[i]; ++j) {
101 int foo = syscalls[i]();
102 ++foo;
103 }
104
105 while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
106 struct perf_sample sample;
107
108 if (event->header.type != PERF_RECORD_SAMPLE) {
109 pr_debug("unexpected %s event\n",
110 perf_event__name(event->header.type));
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300111 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100112 }
113
114 err = perf_evlist__parse_sample(evlist, event, &sample);
115 if (err) {
116 pr_err("Can't parse sample, err = %d\n", err);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300117 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100118 }
119
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300120 err = -1;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100121 evsel = perf_evlist__id2evsel(evlist, sample.id);
122 if (evsel == NULL) {
123 pr_debug("event with id %" PRIu64
124 " doesn't map to an evsel\n", sample.id);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300125 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100126 }
127 nr_events[evsel->idx]++;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800128 perf_evlist__mmap_consume(evlist, 0);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100129 }
130
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300131 err = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300132 evlist__for_each_entry(evlist, evsel) {
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100133 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
134 pr_debug("expected %d %s events, got %d\n",
135 expected_nr_events[evsel->idx],
136 perf_evsel__name(evsel), nr_events[evsel->idx]);
Arnaldo Carvalho de Meloc5d3d50d2012-12-10 14:58:42 -0300137 err = -1;
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300138 goto out_delete_evlist;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100139 }
140 }
141
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300142out_delete_evlist:
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100143 perf_evlist__delete(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300144 cpus = NULL;
145 threads = NULL;
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100146out_free_cpus:
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200147 cpu_map__put(cpus);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100148out_free_threads:
Jiri Olsa186fbb72015-06-23 00:36:05 +0200149 thread_map__put(threads);
Jiri Olsaa65b9c62012-11-10 01:46:44 +0100150 return err;
151}