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" |
| 6 | #include <string.h> |
| 7 | #include <linux/bitops.h> |
| 8 | #include "debug.h" |
| 9 | |
| 10 | struct machine; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 11 | |
| 12 | static int process_event_mask(struct perf_tool *tool __maybe_unused, |
| 13 | union perf_event *event, |
| 14 | struct perf_sample *sample __maybe_unused, |
| 15 | struct machine *machine __maybe_unused) |
| 16 | { |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 17 | struct cpu_map_event *map_event = &event->cpu_map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 18 | struct cpu_map_mask *mask; |
| 19 | struct cpu_map_data *data; |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 20 | struct cpu_map *map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 21 | int i; |
| 22 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 23 | data = &map_event->data; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 24 | |
| 25 | TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK); |
| 26 | |
| 27 | mask = (struct cpu_map_mask *)data->data; |
| 28 | |
| 29 | TEST_ASSERT_VAL("wrong nr", mask->nr == 1); |
| 30 | |
| 31 | for (i = 0; i < 20; i++) { |
| 32 | TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask)); |
| 33 | } |
| 34 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 35 | map = cpu_map__new_data(data); |
| 36 | TEST_ASSERT_VAL("wrong nr", map->nr == 20); |
| 37 | |
| 38 | for (i = 0; i < 20; i++) { |
| 39 | TEST_ASSERT_VAL("wrong cpu", map->map[i] == i); |
| 40 | } |
| 41 | |
| 42 | cpu_map__put(map); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int process_event_cpus(struct perf_tool *tool __maybe_unused, |
| 47 | union perf_event *event, |
| 48 | struct perf_sample *sample __maybe_unused, |
| 49 | struct machine *machine __maybe_unused) |
| 50 | { |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 51 | struct cpu_map_event *map_event = &event->cpu_map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 52 | struct cpu_map_entries *cpus; |
| 53 | struct cpu_map_data *data; |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 54 | struct cpu_map *map; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 55 | |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 56 | data = &map_event->data; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 57 | |
| 58 | TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS); |
| 59 | |
| 60 | cpus = (struct cpu_map_entries *)data->data; |
| 61 | |
| 62 | TEST_ASSERT_VAL("wrong nr", cpus->nr == 2); |
| 63 | TEST_ASSERT_VAL("wrong cpu", cpus->cpu[0] == 1); |
| 64 | TEST_ASSERT_VAL("wrong cpu", cpus->cpu[1] == 256); |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 65 | |
| 66 | map = cpu_map__new_data(data); |
| 67 | TEST_ASSERT_VAL("wrong nr", map->nr == 2); |
| 68 | TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1); |
| 69 | TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256); |
Elena Reshetova | ec09a42 | 2017-02-21 17:34:56 +0200 | [diff] [blame] | 70 | TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1); |
Jiri Olsa | f77b57a | 2015-10-25 15:51:25 +0100 | [diff] [blame] | 71 | cpu_map__put(map); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 76 | int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 77 | { |
| 78 | struct cpu_map *cpus; |
| 79 | |
| 80 | /* This one is better stores in mask. */ |
| 81 | cpus = cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19"); |
| 82 | |
| 83 | TEST_ASSERT_VAL("failed to synthesize map", |
| 84 | !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL)); |
| 85 | |
| 86 | cpu_map__put(cpus); |
| 87 | |
| 88 | /* This one is better stores in cpu values. */ |
| 89 | cpus = cpu_map__new("1,256"); |
| 90 | |
| 91 | TEST_ASSERT_VAL("failed to synthesize map", |
| 92 | !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL)); |
| 93 | |
| 94 | cpu_map__put(cpus); |
| 95 | return 0; |
| 96 | } |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 97 | |
| 98 | static int cpu_map_print(const char *str) |
| 99 | { |
| 100 | struct cpu_map *map = cpu_map__new(str); |
| 101 | char buf[100]; |
| 102 | |
| 103 | if (!map) |
| 104 | return -1; |
| 105 | |
| 106 | cpu_map__snprint(map, buf, sizeof(buf)); |
| 107 | return !strcmp(buf, str); |
| 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 110 | int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 111 | { |
| 112 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1")); |
| 113 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5")); |
| 114 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40")); |
| 115 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5")); |
| 116 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); |
| 117 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); |
| 118 | TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40")); |
| 119 | return 0; |
| 120 | } |