Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "debug.h"
0003 #include "evlist.h"
0004 #include "evsel.h"
0005 #include "target.h"
0006 #include "thread_map.h"
0007 #include "tests.h"
0008 #include "util/mmap.h"
0009 
0010 #include <errno.h>
0011 #include <signal.h>
0012 #include <linux/string.h>
0013 #include <perf/cpumap.h>
0014 #include <perf/evlist.h>
0015 #include <perf/mmap.h>
0016 
0017 static int exited;
0018 static int nr_exit;
0019 
0020 static void sig_handler(int sig __maybe_unused)
0021 {
0022     exited = 1;
0023 }
0024 
0025 /*
0026  * evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
0027  * we asked by setting its exec_error to this handler.
0028  */
0029 static void workload_exec_failed_signal(int signo __maybe_unused,
0030                     siginfo_t *info __maybe_unused,
0031                     void *ucontext __maybe_unused)
0032 {
0033     exited  = 1;
0034     nr_exit = -1;
0035 }
0036 
0037 /*
0038  * This test will start a workload that does nothing then it checks
0039  * if the number of exit event reported by the kernel is 1 or not
0040  * in order to check the kernel returns correct number of event.
0041  */
0042 static int test__task_exit(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0043 {
0044     int err = -1;
0045     union perf_event *event;
0046     struct evsel *evsel;
0047     struct evlist *evlist;
0048     struct target target = {
0049         .uid        = UINT_MAX,
0050         .uses_mmap  = true,
0051     };
0052     const char *argv[] = { "true", NULL };
0053     char sbuf[STRERR_BUFSIZE];
0054     struct perf_cpu_map *cpus;
0055     struct perf_thread_map *threads;
0056     struct mmap *md;
0057     int retry_count = 0;
0058 
0059     signal(SIGCHLD, sig_handler);
0060 
0061     evlist = evlist__new_default();
0062     if (evlist == NULL) {
0063         pr_debug("evlist__new_default\n");
0064         return -1;
0065     }
0066 
0067     /*
0068      * Create maps of threads and cpus to monitor. In this case
0069      * we start with all threads and cpus (-1, -1) but then in
0070      * evlist__prepare_workload we'll fill in the only thread
0071      * we're monitoring, the one forked there.
0072      */
0073     cpus = perf_cpu_map__dummy_new();
0074     threads = thread_map__new_by_tid(-1);
0075     if (!cpus || !threads) {
0076         err = -ENOMEM;
0077         pr_debug("Not enough memory to create thread/cpu maps\n");
0078         goto out_delete_evlist;
0079     }
0080 
0081     perf_evlist__set_maps(&evlist->core, cpus, threads);
0082 
0083     err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
0084     if (err < 0) {
0085         pr_debug("Couldn't run the workload!\n");
0086         goto out_delete_evlist;
0087     }
0088 
0089     evsel = evlist__first(evlist);
0090     evsel->core.attr.task = 1;
0091 #ifdef __s390x__
0092     evsel->core.attr.sample_freq = 1000000;
0093 #else
0094     evsel->core.attr.sample_freq = 1;
0095 #endif
0096     evsel->core.attr.inherit = 0;
0097     evsel->core.attr.watermark = 0;
0098     evsel->core.attr.wakeup_events = 1;
0099     evsel->core.attr.exclude_kernel = 1;
0100 
0101     err = evlist__open(evlist);
0102     if (err < 0) {
0103         pr_debug("Couldn't open the evlist: %s\n",
0104              str_error_r(-err, sbuf, sizeof(sbuf)));
0105         goto out_delete_evlist;
0106     }
0107 
0108     if (evlist__mmap(evlist, 128) < 0) {
0109         pr_debug("failed to mmap events: %d (%s)\n", errno,
0110              str_error_r(errno, sbuf, sizeof(sbuf)));
0111         err = -1;
0112         goto out_delete_evlist;
0113     }
0114 
0115     evlist__start_workload(evlist);
0116 
0117 retry:
0118     md = &evlist->mmap[0];
0119     if (perf_mmap__read_init(&md->core) < 0)
0120         goto out_init;
0121 
0122     while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0123         if (event->header.type == PERF_RECORD_EXIT)
0124             nr_exit++;
0125 
0126         perf_mmap__consume(&md->core);
0127     }
0128     perf_mmap__read_done(&md->core);
0129 
0130 out_init:
0131     if (!exited || !nr_exit) {
0132         evlist__poll(evlist, -1);
0133 
0134         if (retry_count++ > 1000) {
0135             pr_debug("Failed after retrying 1000 times\n");
0136             err = -1;
0137             goto out_delete_evlist;
0138         }
0139 
0140         goto retry;
0141     }
0142 
0143     if (nr_exit != 1) {
0144         pr_debug("received %d EXIT records\n", nr_exit);
0145         err = -1;
0146     }
0147 
0148 out_delete_evlist:
0149     perf_cpu_map__put(cpus);
0150     perf_thread_map__put(threads);
0151     evlist__delete(evlist);
0152     return err;
0153 }
0154 
0155 DEFINE_SUITE("Number of exit events of a simple workload", task_exit);