Linux Perf
cpumap.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0
2 #include "tests.h"
3 #include <stdio.h>
4 #include "cpumap.h"
5 #include "event.h"
6 #include <string.h>
7 #include <linux/bitops.h>
8 #include "debug.h"
9 
10 struct machine;
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 {
17  struct cpu_map_event *map_event = &event->cpu_map;
18  struct cpu_map_mask *mask;
19  struct cpu_map_data *data;
20  struct cpu_map *map;
21  int i;
22 
23  data = &map_event->data;
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 
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);
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 {
51  struct cpu_map_event *map_event = &event->cpu_map;
52  struct cpu_map_entries *cpus;
53  struct cpu_map_data *data;
54  struct cpu_map *map;
55 
56  data = &map_event->data;
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);
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);
70  TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
71  cpu_map__put(map);
72  return 0;
73 }
74 
75 
76 int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
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",
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",
93 
94  cpu_map__put(cpus);
95  return 0;
96 }
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 
110 int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
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 }
int nr
Definition: cpumap.h:14
int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
Definition: cpumap.c:76
u16 nr
Definition: event.h:429
dictionary data
Definition: stat-cpi.py:4
#define TEST_ASSERT_VAL(text, cond)
Definition: tests.h:7
struct cpu_map * cpu_map__new_data(struct cpu_map_data *data)
Definition: cpumap.c:234
u16 cpu[]
Definition: event.h:425
Definition: cpumap.h:12
int perf_event__synthesize_cpu_map(struct perf_tool *tool, struct cpu_map *map, perf_event__handler_t process, struct machine *machine)
Definition: event.c:1115
unsigned long mask[]
Definition: event.h:431
size_t cpu_map__snprint(struct cpu_map *map, char *buf, size_t size)
Definition: cpumap.c:638
void cpu_map__put(struct cpu_map *map)
Definition: cpumap.c:298
Definition: tool.h:44
struct cpu_map_data data
Definition: event.h:441
refcount_t refcnt
Definition: cpumap.h:13
static int process_event_mask(struct perf_tool *tool __maybe_unused, union perf_event *event, struct perf_sample *sample __maybe_unused, struct machine *machine __maybe_unused)
Definition: cpumap.c:12
static struct perf_tool tool
Definition: builtin-diff.c:362
static int str(yyscan_t scanner, int token)
list cpus
Definition: stat-cpi.py:7
#define event
int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
Definition: cpumap.c:110
int map[]
Definition: cpumap.h:15
static int process_event_cpus(struct perf_tool *tool __maybe_unused, union perf_event *event, struct perf_sample *sample __maybe_unused, struct machine *machine __maybe_unused)
Definition: cpumap.c:46
Definition: jevents.c:228
Definition: tests.h:30
u16 type
Definition: event.h:435
struct cpu_map * cpu_map__new(const char *cpu_list)
Definition: cpumap.c:125
char data[]
Definition: event.h:436
static int cpu_map_print(const char *str)
Definition: cpumap.c:98