^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sys/prctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <perf/evlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <perf/mmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "parse-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "evlist.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "thread_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "record.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "util/mmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int spin_sleep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct timeval start, now, diff, maxtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct timespec ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) maxtime.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) maxtime.tv_usec = 50000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) err = gettimeofday(&start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* Spin for 50ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (i = 0; i < 1000; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) err = gettimeofday(&now, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) timersub(&now, &start, &diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (timercmp(&diff, &maxtime, > /* For checkpatch */))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ts.tv_nsec = 50 * 1000 * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ts.tv_sec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Sleep for 50ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) err = nanosleep(&ts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (err == EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct switch_tracking {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct evsel *switch_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct evsel *cycles_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pid_t *tids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int nr_tids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int comm_seen[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int cycles_before_comm_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int cycles_between_comm_2_and_comm_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int cycles_after_comm_4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int check_comm(struct switch_tracking *switch_tracking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) union perf_event *event, const char *comm, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (event->header.type == PERF_RECORD_COMM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (pid_t)event->comm.pid == getpid() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) (pid_t)event->comm.tid == getpid() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) strcmp(event->comm.comm, comm) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (switch_tracking->comm_seen[nr]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) pr_debug("Duplicate comm event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) switch_tracking->comm_seen[nr] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_debug3("comm event: %s nr: %d\n", event->comm.comm, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int check_cpu(struct switch_tracking *switch_tracking, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int i, nr = cpu + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (cpu < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!switch_tracking->tids) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) switch_tracking->tids = calloc(nr, sizeof(pid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!switch_tracking->tids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) for (i = 0; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) switch_tracking->tids[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) switch_tracking->nr_tids = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (cpu >= switch_tracking->nr_tids) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) addr = realloc(switch_tracking->tids, nr * sizeof(pid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) switch_tracking->tids = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) for (i = switch_tracking->nr_tids; i < nr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) switch_tracking->tids[i] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) switch_tracking->nr_tids = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int process_sample_event(struct evlist *evlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct switch_tracking *switch_tracking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct perf_sample sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct evsel *evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pid_t next_tid, prev_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int cpu, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (perf_evlist__parse_sample(evlist, event, &sample)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) pr_debug("perf_evlist__parse_sample failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) evsel = perf_evlist__id2evsel(evlist, sample.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (evsel == switch_tracking->switch_evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) next_tid = evsel__intval(evsel, &sample, "next_pid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) prev_tid = evsel__intval(evsel, &sample, "prev_pid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) cpu = sample.cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) cpu, prev_tid, next_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) err = check_cpu(switch_tracking, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Check for no missing sched_switch events i.e. that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * evsel->core.system_wide flag has worked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (switch_tracking->tids[cpu] != -1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) switch_tracking->tids[cpu] != prev_tid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pr_debug("Missing sched_switch events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) switch_tracking->tids[cpu] = next_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (evsel == switch_tracking->cycles_evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pr_debug3("cycles event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!switch_tracking->comm_seen[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) switch_tracking->cycles_before_comm_1 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (switch_tracking->comm_seen[1] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) !switch_tracking->comm_seen[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) switch_tracking->cycles_between_comm_2_and_comm_3 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (switch_tracking->comm_seen[3])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) switch_tracking->cycles_after_comm_4 = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int process_event(struct evlist *evlist, union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct switch_tracking *switch_tracking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (event->header.type == PERF_RECORD_SAMPLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return process_sample_event(evlist, event, switch_tracking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (event->header.type == PERF_RECORD_COMM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int err, done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) err = check_comm(switch_tracking, event, "Test COMM 1", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) done += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) err = check_comm(switch_tracking, event, "Test COMM 2", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) done += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) err = check_comm(switch_tracking, event, "Test COMM 3", 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) done += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) err = check_comm(switch_tracking, event, "Test COMM 4", 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) done += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (done != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) pr_debug("Unexpected comm event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct event_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) union perf_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u64 event_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int add_event(struct evlist *evlist, struct list_head *events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) union perf_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct perf_sample sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct event_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) node = malloc(sizeof(struct event_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pr_debug("malloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) node->event = event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) list_add(&node->list, events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (perf_evlist__parse_sample(evlist, event, &sample)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) pr_debug("perf_evlist__parse_sample failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!sample.time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pr_debug("event with no time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) node->event_time = sample.time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static void free_event_nodes(struct list_head *events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct event_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) while (!list_empty(events)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) node = list_entry(events->next, struct event_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) list_del_init(&node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) free(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int compar(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) const struct event_node *nodea = a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) const struct event_node *nodeb = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) s64 cmp = nodea->event_time - nodeb->event_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int process_events(struct evlist *evlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct switch_tracking *switch_tracking)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) union perf_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) unsigned pos, cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) LIST_HEAD(events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct event_node *events_array, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct mmap *md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) for (i = 0; i < evlist->core.nr_mmaps; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) md = &evlist->mmap[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (perf_mmap__read_init(&md->core) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) while ((event = perf_mmap__read_event(&md->core)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = add_event(evlist, &events, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) perf_mmap__consume(&md->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) goto out_free_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) perf_mmap__read_done(&md->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) events_array = calloc(cnt, sizeof(struct event_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!events_array) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) pr_debug("calloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto out_free_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) list_for_each_entry(node, &events, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) events_array[pos++] = *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) qsort(events_array, cnt, sizeof(struct event_node), compar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) for (pos = 0; pos < cnt; pos++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = process_event(evlist, events_array[pos].event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) switch_tracking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) pr_debug("%u events recorded\n", cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) free(events_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) out_free_nodes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) free_event_nodes(&events);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * test__switch_tracking - test using sched_switch and tracking events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * This function implements a test that checks that sched_switch events and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * tracking events can be recorded for a workload (current process) using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * evsel->core.system_wide and evsel->tracking flags (respectively) with other events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * sometimes enabled or disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int test__switch_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) const char *sched_switch = "sched:sched_switch";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct switch_tracking switch_tracking = { .tids = NULL, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) struct record_opts opts = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .mmap_pages = UINT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .user_freq = UINT_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .user_interval = ULLONG_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .freq = 4000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .target = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .uses_mmap = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct perf_thread_map *threads = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) struct perf_cpu_map *cpus = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) struct evlist *evlist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) struct evsel *evsel, *cpu_clocks_evsel, *cycles_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct evsel *switch_evsel, *tracking_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) const char *comm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) threads = thread_map__new(-1, getpid(), UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (!threads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) pr_debug("thread_map__new failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) cpus = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (!cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) pr_debug("perf_cpu_map__new failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) evlist = evlist__new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!evlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pr_debug("evlist__new failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) perf_evlist__set_maps(&evlist->core, cpus, threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /* First event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) err = parse_events(evlist, "cpu-clock:u", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) pr_debug("Failed to parse event dummy:u\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) cpu_clocks_evsel = evlist__last(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* Second event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) err = parse_events(evlist, "cycles:u", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) pr_debug("Failed to parse event cycles:u\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) cycles_evsel = evlist__last(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /* Third event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!perf_evlist__can_select_event(evlist, sched_switch)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) pr_debug("No sched_switch\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) err = parse_events(evlist, sched_switch, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) pr_debug("Failed to parse event %s\n", sched_switch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) switch_evsel = evlist__last(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) evsel__set_sample_bit(switch_evsel, CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) evsel__set_sample_bit(switch_evsel, TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) switch_evsel->core.system_wide = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) switch_evsel->no_aux_samples = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) switch_evsel->immediate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /* Test moving an event to the front */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (cycles_evsel == evlist__first(evlist)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) pr_debug("cycles event already at front");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) perf_evlist__to_front(evlist, cycles_evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (cycles_evsel != evlist__first(evlist)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) pr_debug("Failed to move cycles event to front");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) evsel__set_sample_bit(cycles_evsel, CPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) evsel__set_sample_bit(cycles_evsel, TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /* Fourth event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err = parse_events(evlist, "dummy:u", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) pr_debug("Failed to parse event dummy:u\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) tracking_evsel = evlist__last(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) perf_evlist__set_tracking_event(evlist, tracking_evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) tracking_evsel->core.attr.freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) tracking_evsel->core.attr.sample_period = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) evsel__set_sample_bit(tracking_evsel, TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* Config events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) perf_evlist__config(evlist, &opts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* Check moved event is still at the front */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (cycles_evsel != evlist__first(evlist)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) pr_debug("Front event no longer at front");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /* Check tracking event is tracking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (!tracking_evsel->core.attr.mmap || !tracking_evsel->core.attr.comm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) pr_debug("Tracking event not tracking\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /* Check non-tracking events are not tracking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) evlist__for_each_entry(evlist, evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (evsel != tracking_evsel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (evsel->core.attr.mmap || evsel->core.attr.comm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) pr_debug("Non-tracking event is tracking\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (evlist__open(evlist) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) pr_debug("Not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) err = evlist__mmap(evlist, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) pr_debug("evlist__mmap failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) evlist__enable(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) err = evsel__disable(cpu_clocks_evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) pr_debug("perf_evlist__disable_event failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) err = spin_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) pr_debug("spin_sleep failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) comm = "Test COMM 1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) pr_debug("PR_SET_NAME failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) err = evsel__disable(cycles_evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) pr_debug("perf_evlist__disable_event failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) comm = "Test COMM 2";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) pr_debug("PR_SET_NAME failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err = spin_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) pr_debug("spin_sleep failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) comm = "Test COMM 3";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) pr_debug("PR_SET_NAME failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) err = evsel__enable(cycles_evsel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) pr_debug("perf_evlist__disable_event failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) comm = "Test COMM 4";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) pr_debug("PR_SET_NAME failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) err = spin_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) pr_debug("spin_sleep failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) evlist__disable(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) switch_tracking.switch_evsel = switch_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) switch_tracking.cycles_evsel = cycles_evsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) err = process_events(evlist, &switch_tracking);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) zfree(&switch_tracking.tids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* Check all 4 comm events were seen i.e. that evsel->tracking works */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) pr_debug("Missing comm events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* Check cycles event got enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (!switch_tracking.cycles_before_comm_1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) pr_debug("Missing cycles events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) /* Check cycles event got disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (switch_tracking.cycles_between_comm_2_and_comm_3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) pr_debug("cycles events even though event was disabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /* Check cycles event got enabled again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (!switch_tracking.cycles_after_comm_4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) pr_debug("Missing cycles events\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (evlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) evlist__disable(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) evlist__delete(evlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) perf_cpu_map__put(cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) perf_thread_map__put(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }