switch-tracking.c 13 KB

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