root/tools/perf/tests/mmap-basic.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test__basic_mmap

   1 // SPDX-License-Identifier: GPL-2.0
   2 #include <errno.h>
   3 #include <inttypes.h>
   4 /* For the CLR_() macros */
   5 #include <pthread.h>
   6 #include <stdlib.h>
   7 #include <perf/cpumap.h>
   8 
   9 #include "debug.h"
  10 #include "evlist.h"
  11 #include "evsel.h"
  12 #include "thread_map.h"
  13 #include "tests.h"
  14 #include "util/mmap.h"
  15 #include <linux/err.h>
  16 #include <linux/kernel.h>
  17 #include <linux/string.h>
  18 #include <perf/evlist.h>
  19 
  20 /*
  21  * This test will generate random numbers of calls to some getpid syscalls,
  22  * then establish an mmap for a group of events that are created to monitor
  23  * the syscalls.
  24  *
  25  * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
  26  * sample.id field to map back to its respective perf_evsel instance.
  27  *
  28  * Then it checks if the number of syscalls reported as perf events by
  29  * the kernel corresponds to the number of syscalls made.
  30  */
  31 int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unused)
  32 {
  33         int err = -1;
  34         union perf_event *event;
  35         struct perf_thread_map *threads;
  36         struct perf_cpu_map *cpus;
  37         struct evlist *evlist;
  38         cpu_set_t cpu_set;
  39         const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
  40         pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
  41 #define nsyscalls ARRAY_SIZE(syscall_names)
  42         unsigned int nr_events[nsyscalls],
  43                      expected_nr_events[nsyscalls], i, j;
  44         struct evsel *evsels[nsyscalls], *evsel;
  45         char sbuf[STRERR_BUFSIZE];
  46         struct mmap *md;
  47 
  48         threads = thread_map__new(-1, getpid(), UINT_MAX);
  49         if (threads == NULL) {
  50                 pr_debug("thread_map__new\n");
  51                 return -1;
  52         }
  53 
  54         cpus = perf_cpu_map__new(NULL);
  55         if (cpus == NULL) {
  56                 pr_debug("perf_cpu_map__new\n");
  57                 goto out_free_threads;
  58         }
  59 
  60         CPU_ZERO(&cpu_set);
  61         CPU_SET(cpus->map[0], &cpu_set);
  62         sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
  63         if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  64                 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  65                          cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
  66                 goto out_free_cpus;
  67         }
  68 
  69         evlist = evlist__new();
  70         if (evlist == NULL) {
  71                 pr_debug("perf_evlist__new\n");
  72                 goto out_free_cpus;
  73         }
  74 
  75         perf_evlist__set_maps(&evlist->core, cpus, threads);
  76 
  77         for (i = 0; i < nsyscalls; ++i) {
  78                 char name[64];
  79 
  80                 snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
  81                 evsels[i] = perf_evsel__newtp("syscalls", name);
  82                 if (IS_ERR(evsels[i])) {
  83                         pr_debug("perf_evsel__new(%s)\n", name);
  84                         goto out_delete_evlist;
  85                 }
  86 
  87                 evsels[i]->core.attr.wakeup_events = 1;
  88                 perf_evsel__set_sample_id(evsels[i], false);
  89 
  90                 evlist__add(evlist, evsels[i]);
  91 
  92                 if (evsel__open(evsels[i], cpus, threads) < 0) {
  93                         pr_debug("failed to open counter: %s, "
  94                                  "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  95                                  str_error_r(errno, sbuf, sizeof(sbuf)));
  96                         goto out_delete_evlist;
  97                 }
  98 
  99                 nr_events[i] = 0;
 100                 expected_nr_events[i] = 1 + rand() % 127;
 101         }
 102 
 103         if (evlist__mmap(evlist, 128) < 0) {
 104                 pr_debug("failed to mmap events: %d (%s)\n", errno,
 105                          str_error_r(errno, sbuf, sizeof(sbuf)));
 106                 goto out_delete_evlist;
 107         }
 108 
 109         for (i = 0; i < nsyscalls; ++i)
 110                 for (j = 0; j < expected_nr_events[i]; ++j) {
 111                         int foo = syscalls[i]();
 112                         ++foo;
 113                 }
 114 
 115         md = &evlist->mmap[0];
 116         if (perf_mmap__read_init(md) < 0)
 117                 goto out_init;
 118 
 119         while ((event = perf_mmap__read_event(md)) != NULL) {
 120                 struct perf_sample sample;
 121 
 122                 if (event->header.type != PERF_RECORD_SAMPLE) {
 123                         pr_debug("unexpected %s event\n",
 124                                  perf_event__name(event->header.type));
 125                         goto out_delete_evlist;
 126                 }
 127 
 128                 err = perf_evlist__parse_sample(evlist, event, &sample);
 129                 if (err) {
 130                         pr_err("Can't parse sample, err = %d\n", err);
 131                         goto out_delete_evlist;
 132                 }
 133 
 134                 err = -1;
 135                 evsel = perf_evlist__id2evsel(evlist, sample.id);
 136                 if (evsel == NULL) {
 137                         pr_debug("event with id %" PRIu64
 138                                  " doesn't map to an evsel\n", sample.id);
 139                         goto out_delete_evlist;
 140                 }
 141                 nr_events[evsel->idx]++;
 142                 perf_mmap__consume(md);
 143         }
 144         perf_mmap__read_done(md);
 145 
 146 out_init:
 147         err = 0;
 148         evlist__for_each_entry(evlist, evsel) {
 149                 if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
 150                         pr_debug("expected %d %s events, got %d\n",
 151                                  expected_nr_events[evsel->idx],
 152                                  perf_evsel__name(evsel), nr_events[evsel->idx]);
 153                         err = -1;
 154                         goto out_delete_evlist;
 155                 }
 156         }
 157 
 158 out_delete_evlist:
 159         evlist__delete(evlist);
 160         cpus    = NULL;
 161         threads = NULL;
 162 out_free_cpus:
 163         perf_cpu_map__put(cpus);
 164 out_free_threads:
 165         perf_thread_map__put(threads);
 166         return err;
 167 }

/* [<][>][^][v][top][bottom][index][help] */