Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 2 | #include "tests.h" |
Arnaldo Carvalho de Melo | 3ac55b1 | 2016-07-07 11:32:29 -0300 | [diff] [blame] | 3 | #include <stdio.h> |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 4 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | 3ac55b1 | 2016-07-07 11:32:29 -0300 | [diff] [blame] | 5 | #include "event.h" |
Arnaldo Carvalho de Melo | ea49e01 | 2019-09-18 11:36:13 -0300 | [diff] [blame] | 6 | #include "util/synthetic-events.h" |
Arnaldo Carvalho de Melo | 3ac55b1 | 2016-07-07 11:32:29 -0300 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <linux/bitops.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 9 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | 3ac55b1 | 2016-07-07 11:32:29 -0300 | [diff] [blame] | 10 | #include "debug.h" |
| 11 | |
| 12 | struct machine; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 13 | |
| 14 | static int process_event_mask(struct perf_tool *tool __maybe_unused, |
| 15 | union perf_event *event, |
| 16 | struct perf_sample *sample __maybe_unused, |
| 17 | struct machine *machine __maybe_unused) |
| 18 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 19 | struct perf_record_cpu_map *map_event = &event->cpu_map; |
| 20 | struct perf_record_record_cpu_map *mask; |
| 21 | struct perf_record_cpu_map_data *data; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 22 | struct perf_cpu_map *map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 23 | int i; |
| 24 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 25 | data = &map_event->data; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 26 | |
| 27 | TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK); |
| 28 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 29 | mask = (struct perf_record_record_cpu_map *)data->data; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 30 | |
| 31 | TEST_ASSERT_VAL("wrong nr", mask->nr == 1); |
| 32 | |
| 33 | for (i = 0; i < 20; i++) { |
| 34 | TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask)); |
| 35 | } |
| 36 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 37 | map = cpu_map__new_data(data); |
| 38 | TEST_ASSERT_VAL("wrong nr", map->nr == 20); |
| 39 | |
| 40 | for (i = 0; i < 20; i++) { |
| 41 | TEST_ASSERT_VAL("wrong cpu", map->map[i] == i); |
| 42 | } |
| 43 | |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 44 | perf_cpu_map__put(map); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int process_event_cpus(struct perf_tool *tool __maybe_unused, |
| 49 | union perf_event *event, |
| 50 | struct perf_sample *sample __maybe_unused, |
| 51 | struct machine *machine __maybe_unused) |
| 52 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 53 | struct perf_record_cpu_map *map_event = &event->cpu_map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 54 | struct cpu_map_entries *cpus; |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 55 | struct perf_record_cpu_map_data *data; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 56 | struct perf_cpu_map *map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 57 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 58 | data = &map_event->data; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 59 | |
| 60 | TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS); |
| 61 | |
| 62 | cpus = (struct cpu_map_entries *)data->data; |
| 63 | |
| 64 | TEST_ASSERT_VAL("wrong nr", cpus->nr == 2); |
| 65 | TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1); |
| 66 | TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256); |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 67 | |
| 68 | map = cpu_map__new_data(data); |
| 69 | TEST_ASSERT_VAL("wrong nr", map->nr == 2); |
| 70 | TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1); |
| 71 | TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256); |
Elena Reshetova | ec09a42 | 2017-02-21 17:34:56 +0200 | [diff] [blame] | 72 | TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1); |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 73 | perf_cpu_map__put(map); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 78 | static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 79 | { |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 80 | struct perf_cpu_map *cpus; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 81 | |
| 82 | /* This one is better stores in mask. */ |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 83 | cpus = perf_cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19"); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 84 | |
| 85 | TEST_ASSERT_VAL("failed to synthesize map", |
| 86 | !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL)); |
| 87 | |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 88 | perf_cpu_map__put(cpus); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 89 | |
| 90 | /* This one is better stores in cpu values. */ |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 91 | cpus = perf_cpu_map__new("1,256"); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 92 | |
| 93 | TEST_ASSERT_VAL("failed to synthesize map", |
| 94 | !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL)); |
| 95 | |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 96 | perf_cpu_map__put(cpus); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 97 | return 0; |
| 98 | } |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 99 | |
| 100 | static int cpu_map_print(const char *str) |
| 101 | { |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 102 | struct perf_cpu_map *map = perf_cpu_map__new(str); |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 103 | char buf[100]; |
| 104 | |
| 105 | if (!map) |
| 106 | return -1; |
| 107 | |
| 108 | cpu_map__snprint(map, buf, sizeof(buf)); |
Namhyung Kim | 690d91f | 2021-03-01 23:04:08 +0900 | [diff] [blame] | 109 | perf_cpu_map__put(map); |
| 110 | |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 111 | return !strcmp(buf, str); |
| 112 | } |
| 113 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 114 | static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 115 | { |
| 116 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1")); |
| 117 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5")); |
| 118 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40")); |
| 119 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5")); |
| 120 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); |
| 121 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); |
| 122 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40")); |
| 123 | return 0; |
| 124 | } |
Andi Kleen | a2408a7 | 2019-11-20 16:15:14 -0800 | [diff] [blame] | 125 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 126 | static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused) |
Andi Kleen | a2408a7 | 2019-11-20 16:15:14 -0800 | [diff] [blame] | 127 | { |
| 128 | struct perf_cpu_map *a = perf_cpu_map__new("4,2,1"); |
| 129 | struct perf_cpu_map *b = perf_cpu_map__new("4,5,7"); |
| 130 | struct perf_cpu_map *c = perf_cpu_map__merge(a, b); |
| 131 | char buf[100]; |
| 132 | |
| 133 | TEST_ASSERT_VAL("failed to merge map: bad nr", c->nr == 5); |
| 134 | cpu_map__snprint(c, buf, sizeof(buf)); |
| 135 | TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7")); |
Andi Kleen | a2408a7 | 2019-11-20 16:15:14 -0800 | [diff] [blame] | 136 | perf_cpu_map__put(b); |
| 137 | perf_cpu_map__put(c); |
| 138 | return 0; |
| 139 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 140 | |
| 141 | DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize); |
| 142 | DEFINE_SUITE("Print cpu map", cpu_map_print); |
| 143 | DEFINE_SUITE("Merge cpu map", cpu_map_merge); |