Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/types.h>
0003 #include <limits.h>
0004 #include <unistd.h>
0005 #include <sys/prctl.h>
0006 #include <perf/cpumap.h>
0007 #include <perf/evlist.h>
0008 #include <perf/mmap.h>
0009 
0010 #include "debug.h"
0011 #include "parse-events.h"
0012 #include "evlist.h"
0013 #include "evsel.h"
0014 #include "record.h"
0015 #include "thread_map.h"
0016 #include "tests.h"
0017 #include "util/mmap.h"
0018 
0019 #define CHECK__(x) {                \
0020     while ((x) < 0) {           \
0021         pr_debug(#x " failed!\n");  \
0022         goto out_err;           \
0023     }                   \
0024 }
0025 
0026 #define CHECK_NOT_NULL__(x) {           \
0027     while ((x) == NULL) {           \
0028         pr_debug(#x " failed!\n");  \
0029         goto out_err;           \
0030     }                   \
0031 }
0032 
0033 static int find_comm(struct evlist *evlist, const char *comm)
0034 {
0035     union perf_event *event;
0036     struct mmap *md;
0037     int i, found;
0038 
0039     found = 0;
0040     for (i = 0; i < evlist->core.nr_mmaps; i++) {
0041         md = &evlist->mmap[i];
0042         if (perf_mmap__read_init(&md->core) < 0)
0043             continue;
0044         while ((event = perf_mmap__read_event(&md->core)) != NULL) {
0045             if (event->header.type == PERF_RECORD_COMM &&
0046                 (pid_t)event->comm.pid == getpid() &&
0047                 (pid_t)event->comm.tid == getpid() &&
0048                 strcmp(event->comm.comm, comm) == 0)
0049                 found += 1;
0050             perf_mmap__consume(&md->core);
0051         }
0052         perf_mmap__read_done(&md->core);
0053     }
0054     return found;
0055 }
0056 
0057 /**
0058  * test__keep_tracking - test using a dummy software event to keep tracking.
0059  *
0060  * This function implements a test that checks that tracking events continue
0061  * when an event is disabled but a dummy software event is not disabled.  If the
0062  * test passes %0 is returned, otherwise %-1 is returned.
0063  */
0064 static int test__keep_tracking(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0065 {
0066     struct record_opts opts = {
0067         .mmap_pages      = UINT_MAX,
0068         .user_freq       = UINT_MAX,
0069         .user_interval       = ULLONG_MAX,
0070         .target          = {
0071             .uses_mmap   = true,
0072         },
0073     };
0074     struct perf_thread_map *threads = NULL;
0075     struct perf_cpu_map *cpus = NULL;
0076     struct evlist *evlist = NULL;
0077     struct evsel *evsel = NULL;
0078     int found, err = -1;
0079     const char *comm;
0080 
0081     threads = thread_map__new(-1, getpid(), UINT_MAX);
0082     CHECK_NOT_NULL__(threads);
0083 
0084     cpus = perf_cpu_map__new(NULL);
0085     CHECK_NOT_NULL__(cpus);
0086 
0087     evlist = evlist__new();
0088     CHECK_NOT_NULL__(evlist);
0089 
0090     perf_evlist__set_maps(&evlist->core, cpus, threads);
0091 
0092     CHECK__(parse_event(evlist, "dummy:u"));
0093     CHECK__(parse_event(evlist, "cycles:u"));
0094 
0095     evlist__config(evlist, &opts, NULL);
0096 
0097     evsel = evlist__first(evlist);
0098 
0099     evsel->core.attr.comm = 1;
0100     evsel->core.attr.disabled = 1;
0101     evsel->core.attr.enable_on_exec = 0;
0102 
0103     if (evlist__open(evlist) < 0) {
0104         pr_debug("Unable to open dummy and cycles event\n");
0105         err = TEST_SKIP;
0106         goto out_err;
0107     }
0108 
0109     CHECK__(evlist__mmap(evlist, UINT_MAX));
0110 
0111     /*
0112      * First, test that a 'comm' event can be found when the event is
0113      * enabled.
0114      */
0115 
0116     evlist__enable(evlist);
0117 
0118     comm = "Test COMM 1";
0119     CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
0120 
0121     evlist__disable(evlist);
0122 
0123     found = find_comm(evlist, comm);
0124     if (found != 1) {
0125         pr_debug("First time, failed to find tracking event.\n");
0126         goto out_err;
0127     }
0128 
0129     /*
0130      * Secondly, test that a 'comm' event can be found when the event is
0131      * disabled with the dummy event still enabled.
0132      */
0133 
0134     evlist__enable(evlist);
0135 
0136     evsel = evlist__last(evlist);
0137 
0138     CHECK__(evsel__disable(evsel));
0139 
0140     comm = "Test COMM 2";
0141     CHECK__(prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0));
0142 
0143     evlist__disable(evlist);
0144 
0145     found = find_comm(evlist, comm);
0146     if (found != 1) {
0147         pr_debug("Second time, failed to find tracking event.\n");
0148         goto out_err;
0149     }
0150 
0151     err = 0;
0152 
0153 out_err:
0154     if (evlist) {
0155         evlist__disable(evlist);
0156         evlist__delete(evlist);
0157     }
0158     perf_cpu_map__put(cpus);
0159     perf_thread_map__put(threads);
0160 
0161     return err;
0162 }
0163 
0164 DEFINE_SUITE("Use a dummy software event to keep tracking", keep_tracking);