intel_rdt: refactor pids monitoring code.
[collectd.git] / src / intel_rdt_test.c
1 #include "intel_rdt.c" /* sic */
2 #include "testing.h"
3
4 /***************************************************************************
5  * PQOS mocks
6  */
7 #if PQOS_VERSION >= 30000
8 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg,
9                      const enum pqos_cdp_config l2_cdp_cfg,
10                      const enum pqos_mba_config mba_cfg) {
11   return 0;
12 }
13 #elif PQOS_VERSION >= 2000
14 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg,
15                      const enum pqos_cdp_config l2_cdp_cfg) {
16   return 0;
17 }
18 #else
19 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg) {
20   return 0;
21 }
22 #endif
23
24 #ifdef LIBPQOS2
25 /***************************************************************************
26  * PQOS v2.0 mocks
27  */
28 int pqos_mon_reset(void) { return 0; }
29 int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; }
30 int pqos_mon_start(const unsigned num_cores, const unsigned *cores,
31                    const enum pqos_mon_event event, void *context,
32                    struct pqos_mon_data *group) {
33   return 0;
34 }
35 int pqos_mon_start_pids(const unsigned num_pids, const pid_t *pids,
36                         const enum pqos_mon_event event, void *context,
37                         struct pqos_mon_data *group) {
38   return 0;
39 }
40 int pqos_mon_add_pids(const unsigned num_pids, const pid_t *pids,
41                       struct pqos_mon_data *group) {
42   return 0;
43 }
44 int pqos_mon_remove_pids(const unsigned num_pids, const pid_t *pids,
45                          struct pqos_mon_data *group) {
46   return 0;
47 }
48 int pqos_mon_stop(struct pqos_mon_data *group) { return 0; }
49 int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) {
50   return 0;
51 }
52 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
53   return 0;
54 }
55 int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; }
56 int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) {
57   return 0;
58 }
59 int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; }
60 int pqos_alloc_assign(const unsigned technology, const unsigned *core_array,
61                       const unsigned core_num, unsigned *class_id) {
62   return 0;
63 }
64 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
65   return 0;
66 }
67 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
68                           const unsigned task_num, unsigned *class_id) {
69   return 0;
70 }
71 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
72   return 0;
73 }
74 int pqos_init(const struct pqos_config *config) { return 0; }
75 int pqos_fini(void) { return 0; }
76 int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type,
77                       const struct pqos_capability **cap_item) {
78   return 0;
79 }
80 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
81   return 0;
82 }
83
84 /***************************************************************************
85  * helper functions
86  */
87 rdt_ctx_t *stub_rdt_setup() {
88
89   rdt_ctx_t *rdt = calloc(1, sizeof(*rdt));
90   struct pqos_cpuinfo *pqos_cpu = calloc(1, sizeof(*pqos_cpu));
91   struct pqos_cap *pqos_cap = calloc(1, sizeof(*pqos_cap));
92   struct pqos_cap_mon *mon = calloc(1, sizeof(*mon));
93   struct pqos_capability *cap_mon = calloc(1, sizeof(*cap_mon));
94
95   cap_mon->u.mon = mon;
96   rdt->pqos_cap = pqos_cap;
97   rdt->pqos_cpu = pqos_cpu;
98   rdt->cap_mon = cap_mon;
99
100   return rdt;
101 }
102
103 void stub_rdt_teardown(rdt_ctx_t *rdt) {
104   free(rdt->cap_mon->u.mon);
105   free((void *)rdt->cap_mon);
106   free((void *)rdt->pqos_cpu);
107   free((void *)rdt->pqos_cap);
108   free(rdt);
109 }
110
111 /***************************************************************************
112  * tests
113  */
114 DEF_TEST(rdt_config_ngroups__one_process) {
115   /* setup */
116   rdt_ctx_t *rdt = stub_rdt_setup();
117
118   oconfig_value_t values[] = {
119       {.value.string = "proc1", .type = OCONFIG_TYPE_STRING},
120   };
121   oconfig_item_t config_item = {
122       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
123   };
124
125   /* check */
126   int result = rdt_config_ngroups(rdt, &config_item);
127   EXPECT_EQ_INT(0, result);
128   EXPECT_EQ_STR(values[0].value.string, rdt->ngroups[0].desc);
129   EXPECT_EQ_INT(1, rdt->num_ngroups);
130
131   /* cleanup */
132   rdt_free_ngroups(rdt);
133   stub_rdt_teardown(rdt);
134
135   return 0;
136 }
137
138 DEF_TEST(rdt_config_ngroups__two_groups) {
139   /* setup */
140   rdt_ctx_t *rdt = stub_rdt_setup();
141
142   oconfig_value_t values[] = {
143       {.value.string = "proc11,proc12,proc13", .type = OCONFIG_TYPE_STRING},
144       {.value.string = "proc21,proc22,proc23", .type = OCONFIG_TYPE_STRING},
145   };
146   oconfig_item_t config_item = {
147       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
148   };
149
150   /* check */
151   int result = rdt_config_ngroups(rdt, &config_item);
152   EXPECT_EQ_INT(0, result);
153   EXPECT_EQ_INT(2, rdt->num_ngroups);
154   EXPECT_EQ_STR("proc11,proc12,proc13", rdt->ngroups[0].desc);
155   EXPECT_EQ_STR("proc21,proc22,proc23", rdt->ngroups[1].desc);
156   EXPECT_EQ_STR("proc11", rdt->ngroups[0].names[0]);
157   EXPECT_EQ_STR("proc12", rdt->ngroups[0].names[1]);
158   EXPECT_EQ_STR("proc13", rdt->ngroups[0].names[2]);
159   EXPECT_EQ_STR("proc21", rdt->ngroups[1].names[0]);
160   EXPECT_EQ_STR("proc22", rdt->ngroups[1].names[1]);
161   EXPECT_EQ_STR("proc23", rdt->ngroups[1].names[2]);
162
163   /* cleanup */
164   rdt_free_ngroups(rdt);
165   stub_rdt_teardown(rdt);
166
167   return 0;
168 }
169
170 DEF_TEST(rdt_config_ngroups__too_long_proc_name) {
171   /* setup */
172   rdt_ctx_t *rdt = stub_rdt_setup();
173
174   oconfig_value_t values[] = {
175       {.value.string = "_seventeen_chars_", .type = OCONFIG_TYPE_STRING},
176   };
177   oconfig_item_t config_item = {
178       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
179   };
180
181   /* check */
182   int result = rdt_config_ngroups(rdt, &config_item);
183   EXPECT_EQ_INT(-EINVAL, result);
184
185   /* cleanup */
186   stub_rdt_teardown(rdt);
187
188   return 0;
189 }
190
191 DEF_TEST(rdt_config_ngroups__duplicate_proc_name_between_groups) {
192   /* setup */
193   rdt_ctx_t *rdt = stub_rdt_setup();
194
195   oconfig_value_t values[] = {
196       {.value.string = "proc11,proc12,proc", .type = OCONFIG_TYPE_STRING},
197       {.value.string = "proc21,proc,proc23", .type = OCONFIG_TYPE_STRING},
198   };
199   oconfig_item_t config_item = {
200       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
201   };
202
203   /* check */
204   int result = rdt_config_ngroups(rdt, &config_item);
205   EXPECT_EQ_INT(-EINVAL, result);
206
207   /* cleanup */
208   stub_rdt_teardown(rdt);
209
210   return 0;
211 }
212
213 DEF_TEST(rdt_config_ngroups__duplicate_proc_name_in_group) {
214   /* setup */
215   rdt_ctx_t *rdt = stub_rdt_setup();
216
217   oconfig_value_t values[] = {
218       {.value.string = "proc11,proc,proc,proc14", .type = OCONFIG_TYPE_STRING},
219   };
220   oconfig_item_t config_item = {
221       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
222   };
223
224   /* check */
225   int result = rdt_config_ngroups(rdt, &config_item);
226   EXPECT_EQ_INT(-EINVAL, result);
227
228   /* cleanup */
229   stub_rdt_teardown(rdt);
230
231   return 0;
232 }
233
234 DEF_TEST(rdt_config_ngroups__empty_group) {
235   /* setup */
236   rdt_ctx_t *rdt = stub_rdt_setup();
237
238   oconfig_value_t values[] = {
239       {.value.string = "proc11,proc12,proc13", .type = OCONFIG_TYPE_STRING},
240       {.value.string = "", .type = OCONFIG_TYPE_STRING},
241
242   };
243   oconfig_item_t config_item = {
244       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
245   };
246
247   /* check */
248   int result = rdt_config_ngroups(rdt, &config_item);
249   EXPECT_EQ_INT(-EINVAL, result);
250
251   /* cleanup */
252   stub_rdt_teardown(rdt);
253
254   return 0;
255 }
256
257 DEF_TEST(rdt_config_ngroups__empty_proc_name) {
258   /* setup */
259   rdt_ctx_t *rdt = stub_rdt_setup();
260
261   oconfig_value_t values[] = {
262       {.value.string = "proc11,,proc13", .type = OCONFIG_TYPE_STRING},
263   };
264   oconfig_item_t config_item = {
265       .values = values, .values_num = STATIC_ARRAY_SIZE(values),
266   };
267
268   /* check */
269   int result = rdt_config_ngroups(rdt, &config_item);
270   EXPECT_EQ_INT(-EINVAL, result);
271
272   /* cleanup */
273   stub_rdt_teardown(rdt);
274
275   return 0;
276 }
277
278 int main(void) {
279   RUN_TEST(rdt_config_ngroups__one_process);
280   RUN_TEST(rdt_config_ngroups__two_groups);
281   RUN_TEST(rdt_config_ngroups__too_long_proc_name);
282   RUN_TEST(rdt_config_ngroups__duplicate_proc_name_between_groups);
283   RUN_TEST(rdt_config_ngroups__duplicate_proc_name_in_group);
284   RUN_TEST(rdt_config_ngroups__empty_group);
285   RUN_TEST(rdt_config_ngroups__empty_proc_name);
286   END_TEST;
287 }
288
289 #else
290 /***************************************************************************
291  * PQOS v1.2 mocks
292  */
293 int pqos_mon_reset(void) { return 0; }
294 int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; }
295 int pqos_mon_start(const unsigned num_cores, const unsigned *cores,
296                    const enum pqos_mon_event event, void *context,
297                    struct pqos_mon_data *group) {
298   return 0;
299 }
300 int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event,
301                        void *context, struct pqos_mon_data *group) {
302   return 0;
303 }
304 int pqos_mon_stop(struct pqos_mon_data *group) { return 0; }
305 int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) {
306   return 0;
307 }
308 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
309   return 0;
310 }
311 int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; }
312 int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) {
313   return 0;
314 }
315 int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; }
316 int pqos_alloc_assign(const unsigned technology, const unsigned *core_array,
317                       const unsigned core_num, unsigned *class_id) {
318   return 0;
319 }
320 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
321   return 0;
322 }
323 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
324                           const unsigned task_num, unsigned *class_id) {
325   return 0;
326 }
327 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
328   return 0;
329 }
330 int pqos_init(const struct pqos_config *config) { return 0; }
331 int pqos_fini(void) { return 0; }
332 int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type,
333                       const struct pqos_capability **cap_item) {
334   return 0;
335 }
336 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
337   return 0;
338 }
339
340 DEF_TEST(pqos12_test_stub) {
341   EXPECT_EQ_INT(0, 0);
342   return 0;
343 }
344
345 int main(void) {
346   RUN_TEST(pqos12_test_stub);
347   END_TEST;
348 }
349 #endif