0001
0002 #include "tests.h"
0003 #include <stdio.h>
0004 #include "cpumap.h"
0005 #include "event.h"
0006 #include "util/synthetic-events.h"
0007 #include <string.h>
0008 #include <linux/bitops.h>
0009 #include <perf/cpumap.h>
0010 #include "debug.h"
0011
0012 struct machine;
0013
0014 static int process_event_mask(struct perf_tool *tool __maybe_unused,
0015 union perf_event *event,
0016 struct perf_sample *sample __maybe_unused,
0017 struct machine *machine __maybe_unused)
0018 {
0019 struct perf_record_cpu_map *map_event = &event->cpu_map;
0020 struct perf_record_cpu_map_data *data;
0021 struct perf_cpu_map *map;
0022 int i;
0023 unsigned int long_size;
0024
0025 data = &map_event->data;
0026
0027 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
0028
0029 long_size = data->mask32_data.long_size;
0030
0031 TEST_ASSERT_VAL("wrong long_size", long_size == 4 || long_size == 8);
0032
0033 TEST_ASSERT_VAL("wrong nr", data->mask32_data.nr == 1);
0034
0035 for (i = 0; i < 20; i++) {
0036 TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(i, data));
0037 }
0038
0039 map = cpu_map__new_data(data);
0040 TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 20);
0041
0042 for (i = 0; i < 20; i++) {
0043 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, i).cpu == i);
0044 }
0045
0046 perf_cpu_map__put(map);
0047 return 0;
0048 }
0049
0050 static int process_event_cpus(struct perf_tool *tool __maybe_unused,
0051 union perf_event *event,
0052 struct perf_sample *sample __maybe_unused,
0053 struct machine *machine __maybe_unused)
0054 {
0055 struct perf_record_cpu_map *map_event = &event->cpu_map;
0056 struct perf_record_cpu_map_data *data;
0057 struct perf_cpu_map *map;
0058
0059 data = &map_event->data;
0060
0061 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
0062
0063 TEST_ASSERT_VAL("wrong nr", data->cpus_data.nr == 2);
0064 TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[0] == 1);
0065 TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[1] == 256);
0066
0067 map = cpu_map__new_data(data);
0068 TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 2);
0069 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1);
0070 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 1).cpu == 256);
0071 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
0072 perf_cpu_map__put(map);
0073 return 0;
0074 }
0075
0076
0077 static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0078 {
0079 struct perf_cpu_map *cpus;
0080
0081
0082 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");
0083
0084 TEST_ASSERT_VAL("failed to synthesize map",
0085 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
0086
0087 perf_cpu_map__put(cpus);
0088
0089
0090 cpus = perf_cpu_map__new("1,256");
0091
0092 TEST_ASSERT_VAL("failed to synthesize map",
0093 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
0094
0095 perf_cpu_map__put(cpus);
0096 return 0;
0097 }
0098
0099 static int cpu_map_print(const char *str)
0100 {
0101 struct perf_cpu_map *map = perf_cpu_map__new(str);
0102 char buf[100];
0103
0104 if (!map)
0105 return -1;
0106
0107 cpu_map__snprint(map, buf, sizeof(buf));
0108 perf_cpu_map__put(map);
0109
0110 return !strcmp(buf, str);
0111 }
0112
0113 static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0114 {
0115 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
0116 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
0117 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
0118 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
0119 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
0120 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
0121 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
0122 return 0;
0123 }
0124
0125 static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
0126 {
0127 struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
0128 struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
0129 struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
0130 char buf[100];
0131
0132 TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) == 5);
0133 cpu_map__snprint(c, buf, sizeof(buf));
0134 TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
0135 perf_cpu_map__put(b);
0136 perf_cpu_map__put(c);
0137 return 0;
0138 }
0139
0140 DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize);
0141 DEFINE_SUITE("Print cpu map", cpu_map_print);
0142 DEFINE_SUITE("Merge cpu map", cpu_map_merge);