intel_rdt: Backwards compatibility, libpqos v1
[collectd.git] / src / intel_rdt_test.c
1 #include "intel_rdt.c" /* sic */
2 #include "testing.h"
3 #include <sys/stat.h>
4
5 /***************************************************************************
6  * PQOS mocks
7  */
8 #if PQOS_VERSION >= 30000
9 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg,
10                      const enum pqos_cdp_config l2_cdp_cfg,
11                      const enum pqos_mba_config mba_cfg) {
12   return 0;
13 }
14 #elif PQOS_VERSION >= 2000
15 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg,
16                      const enum pqos_cdp_config l2_cdp_cfg) {
17   return 0;
18 }
19 #else
20 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg) {
21   return 0;
22 }
23 #endif
24
25 #ifdef LIBPQOS2
26 /***************************************************************************
27  * PQOS v2.0 mocks
28  */
29 int pqos_mon_reset(void) { return 0; }
30 int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; }
31 int pqos_mon_start(const unsigned num_cores, const unsigned *cores,
32                    const enum pqos_mon_event event, void *context,
33                    struct pqos_mon_data *group) {
34   return 0;
35 }
36 int pqos_mon_start_pids(const unsigned num_pids, const pid_t *pids,
37                         const enum pqos_mon_event event, void *context,
38                         struct pqos_mon_data *group) {
39   return 0;
40 }
41 int pqos_mon_add_pids(const unsigned num_pids, const pid_t *pids,
42                       struct pqos_mon_data *group) {
43   return 0;
44 }
45 int pqos_mon_remove_pids(const unsigned num_pids, const pid_t *pids,
46                          struct pqos_mon_data *group) {
47   return 0;
48 }
49 int pqos_mon_stop(struct pqos_mon_data *group) { return 0; }
50 int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) {
51   return 0;
52 }
53 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
54   return 0;
55 }
56 int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; }
57 int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) {
58   return 0;
59 }
60 int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; }
61 int pqos_alloc_assign(const unsigned technology, const unsigned *core_array,
62                       const unsigned core_num, unsigned *class_id) {
63   return 0;
64 }
65 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
66   return 0;
67 }
68 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
69                           const unsigned task_num, unsigned *class_id) {
70   return 0;
71 }
72 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
73   return 0;
74 }
75 int pqos_init(const struct pqos_config *config) { return 0; }
76 int pqos_fini(void) { return 0; }
77 int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type,
78                       const struct pqos_capability **cap_item) {
79   return 0;
80 }
81 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
82   return 0;
83 }
84 #endif /* LIBPQOS2 */
85
86 /***************************************************************************
87  * helper functions
88  */
89
90 /*
91  * NAME
92  *   pids_list_get_element
93  *
94  * DESCRIPTION
95  *   Gets list element at index position. Assumes list was created by
96  *   pids_list_add_pid function.
97  *
98  * PARAMETERS
99  *   `list'      Pids list
100  *   `index'     Position of desired element relative to given list pointer.
101  *
102  * RETURN VALUE
103  *   Pointer to element at index position.
104  *   NULL if index exceeds list's length.
105  */
106 pids_list_t *pids_list_get_element(pids_list_t *list, const size_t index) {
107   assert(list);
108   size_t current = 0;
109   while (list != NULL && current != index) {
110     list = list->next;
111     current++;
112   }
113   return list;
114 }
115
116 /*
117  * NAME
118  *   pids_list_find_element
119  *
120  * DESCRIPTION
121  *   Gets index of element in the list matching
122  *   given pid. Assumes PIDs are unique, stops searching
123  *   on the first match.
124  *
125  * PARAMETERS
126  *   `list'     Pids list
127  *   `pid'      PID number to find
128  *
129  * RETURN VALUE
130  *   Index of list element holding given PID.
131  */
132 int pids_list_find_element(pids_list_t *list, const pid_t pid) {
133   assert(list);
134   int result = -1;
135   size_t current = 0;
136   while (list != NULL) {
137     if (list->pid == pid) {
138       result = current;
139       break;
140     }
141     list = list->next;
142   }
143   return result;
144 }
145
146 /*
147  * NAME
148  *   pids_list_has_element
149  *
150  * DESCRIPTION
151  *  Checks if the list contains given pid.
152  *  Wrapper for pids_list_find_element function.
153  *  Used to make tests easier to read.
154  *
155  * PARAMETERS
156  *   `list'     Pids list
157  *   `pid'      PID number to find
158  *
159  * RETURN VALUE
160  *   1 if list contains given PID
161  *   0 if list does not contain given PID
162  */
163 int pids_list_has_element(pids_list_t *list, const pid_t pid) {
164   return pids_list_find_element(list, pid) >= 0 ? 1 : 0;
165 }
166
167 /*
168  * NAME
169  *   pids_list_free_all
170  *
171  * DESCRIPTION
172  *   Frees memory allocated in the given list
173  *
174  * PARAMETERS
175  *   `list'     Pids list
176  */
177 void pids_list_free_all(pids_list_t *list) {
178   while (list) {
179     pids_list_t *previous = list;
180     list = list->next;
181     free(previous);
182   }
183 }
184
185 typedef struct stub_proc_pid {
186   proc_comm_t comm;
187   pid_t pid;
188 } stub_proc_pid_t;
189
190 static const char *proc_fs = "/tmp/procfs_stub";
191
192 /*
193  * NAME
194  *   stub_procfs_setup
195  *
196  * DESCRIPTION
197  *   Prepares testing environment by creating temporary
198  *   PID/comm file structure.
199  *
200  * PARAMETERS
201  *   `proc_pids_array'          Array of stub_proc_pid_t structs. Represents
202  *                              which PIDs should hold given process name.
203  *   `proc_pids_array_length'   Element count of input array.
204  *
205  * RETURN VALUE
206  *   0 on success.
207  *   -1 on base dir creation error.
208  *   -2 on comm file creation error.
209  */
210 int stub_procfs_setup(const stub_proc_pid_t *proc_pids_array,
211                       const size_t proc_pids_array_length) {
212   if (mkdir(proc_fs, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
213     return -1;
214   }
215   char path[256];
216
217   for (size_t i = 0; i < proc_pids_array_length; ++i) {
218     memset(path, 0, sizeof(path));
219     snprintf(path, STATIC_ARRAY_SIZE(path), "%s/%d", proc_fs,
220              proc_pids_array[i].pid);
221     mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
222     strncat(path, "/comm", STATIC_ARRAY_SIZE(path) - strlen(path) - 1);
223
224     FILE *fp = fopen(path, "w");
225     if (!fp) {
226       return -2;
227     }
228     fwrite(proc_pids_array[i].comm, sizeof(char),
229            strlen(proc_pids_array[i].comm), fp);
230     fclose(fp);
231   }
232   return 0;
233 }
234
235 /*
236  * NAME
237  *   stub_procfs_teardown
238  *
239  * DESCRIPTION
240  *   Clears testing environment: removes stub proc files.
241  *   NOTE - This function could be implemented by usage of nftw, but this
242  *   would require #define _XOPEN_SOURCE 500, which
243  *   messes up intel_rdt includes.
244  *
245  * RETURN VALUE
246  *   system command result
247  */
248 int stub_procfs_teardown() {
249   char cmd[256];
250   sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd));
251   strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1);
252   return system(cmd);
253 }
254
255 /* Max PID value. More info:
256  * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html
257  */
258 #define MAX_PID 4194304
259 #define MAX_PID_STR "4194304"
260
261 /***************************************************************************
262  * tests
263  */
264 DEF_TEST(add_proc_pid_empty_list) {
265   /* setup */
266   proc_pids_t proc_pids_instance;
267   proc_pids_instance.pids = NULL;
268   pid_t pid = 1234;
269
270   /* check */
271   pids_list_add_pid(&proc_pids_instance.pids, pid);
272   pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, 0);
273   EXPECT_EQ_INT(pid, added->pid);
274
275   /* cleanup */
276   pids_list_free_all(proc_pids_instance.pids);
277   return 0;
278 }
279
280 DEF_TEST(add_proc_pid_non_empty_list) {
281   /* setup */
282   proc_pids_t proc_pids_instance;
283   proc_pids_instance.pids = NULL;
284   pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
285
286   /* check */
287   for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) {
288     pids_list_add_pid(&proc_pids_instance.pids, pids[i]);
289   }
290
291   for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) {
292     pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, i);
293     EXPECT_EQ_INT(pids[i], added->pid);
294   }
295
296   /* cleanup */
297   pids_list_free_all(proc_pids_instance.pids);
298   return 0;
299 }
300
301 DEF_TEST(get_pid_number_valid_dir) {
302   /* setup */
303   struct dirent d;
304   sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
305   d.d_type = DT_DIR;
306   pid_t pid = 0;
307
308   /* check */
309   int pid_conversion = get_pid_number(&d, &pid);
310
311   EXPECT_EQ_INT(0, pid_conversion);
312   EXPECT_EQ_INT(MAX_PID, pid);
313
314   /* cleanup */
315   return 0;
316 }
317
318 DEF_TEST(get_pid_number_invalid_dir_name) {
319   /* setup */
320   struct dirent d;
321   sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name));
322   d.d_type = DT_DIR;
323   pid_t pid = 0;
324
325   /* check */
326   int pid_conversion = get_pid_number(&d, &pid);
327
328   EXPECT_EQ_INT(-2, pid_conversion);
329   EXPECT_EQ_INT(0, pid);
330
331   /* cleanup */
332   return 0;
333 }
334
335 DEF_TEST(read_proc_name_valid_name) {
336   /* setup */
337   stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}};
338   stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
339   struct dirent d;
340   sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
341   d.d_type = DT_DIR;
342
343   /* check */
344   proc_comm_t comm;
345   int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
346
347   EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result);
348   EXPECT_EQ_STR(pp_stubs[0].comm, comm);
349
350   /* cleanup */
351   stub_procfs_teardown();
352   return 0;
353 }
354
355 DEF_TEST(read_proc_name_invalid_name) {
356   /* setup */
357   struct dirent d;
358   sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
359   d.d_type = DT_DIR;
360
361   /* check */
362   proc_comm_t comm;
363   int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
364
365   EXPECT_EQ_INT(-1, read_result);
366
367   /* cleanup */
368   return 0;
369 }
370
371 DEF_TEST(fetch_pids_for_procs_one_proc_many_pid) {
372   /* setup */
373   const char *proc_names[] = {"proc1"};
374   stub_proc_pid_t pp_stubs[] = {{"proc1", 1007},
375                                 {"proc1", 1008},
376                                 {"proc1", 1009},
377                                 {"proc2", 1010},
378                                 {"proc3", 1011}};
379   stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
380   proc_pids_t *output = NULL;
381
382   /* check */
383   int result = fetch_pids_for_procs(proc_fs, proc_names,
384                                     STATIC_ARRAY_SIZE(proc_names), &output);
385   EXPECT_EQ_INT(0, result);
386
387   /* proc name check */
388   EXPECT_EQ_STR(proc_names[0], output[0].proccess_name);
389
390   for (size_t i = 0; i < STATIC_ARRAY_SIZE(pp_stubs); ++i) {
391     if (0 == strcmp(pp_stubs[i].comm, proc_names[0])) {
392       /* check if proc struct has correct pids */
393       EXPECT_EQ_INT(pids_list_has_element(output[0].pids, pp_stubs[i].pid), 1);
394     } else {
395       /* check if proc struct has no incorrect pids */
396       EXPECT_EQ_INT(pids_list_has_element(output[0].pids, pp_stubs[i].pid), 0);
397     }
398   }
399
400   /* cleanup */
401   for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
402     pids_list_free_all(output[i].pids);
403   }
404   free(output);
405   stub_procfs_teardown();
406   return 0;
407 }
408
409 DEF_TEST(fetch_pids_for_procs_many_proc_many_pid) {
410   /* setup */
411   const char *proc_names[] = {"proc1", "proc2", "proc3"};
412   stub_proc_pid_t pp_stubs[] = {
413       {"proc1", 1007}, {"proc1", 1008}, {"proc1", 1009}, {"proc2", 2007},
414       {"proc2", 2008}, {"proc2", 2009}, {"proc3", 3007}, {"proc3", 3008},
415       {"proc3", 3009}, {"proc4", 4007}, {"proc4", 4008}, {"proc4", 4009},
416       {"proc5", 5007}, {"proc5", 5008}, {"proc5", 5009}};
417   stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
418   proc_pids_t *output = NULL;
419
420   /* check */
421   int result = fetch_pids_for_procs(proc_fs, proc_names,
422                                     STATIC_ARRAY_SIZE(proc_names), &output);
423   EXPECT_EQ_INT(0, result);
424
425   for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
426
427     /* proc name check */
428     EXPECT_EQ_STR(proc_names[i], output[i].proccess_name);
429
430     for (size_t j = 0; j < STATIC_ARRAY_SIZE(pp_stubs); ++j) {
431       if (0 == strcmp(pp_stubs[j].comm, proc_names[i])) {
432         /* check if proc struct has correct pids */
433         EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid),
434                       1);
435       } else {
436         /* check if proc struct has no incorrect pids */
437         EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid),
438                       0);
439       }
440     }
441   }
442
443   /* cleanup */
444   for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
445     pids_list_free_all(output[i].pids);
446   }
447   free(output);
448   stub_procfs_teardown();
449   return 0;
450 }
451
452 int main(void) {
453   stub_procfs_teardown();
454   RUN_TEST(add_proc_pid_empty_list);
455   RUN_TEST(add_proc_pid_non_empty_list);
456   RUN_TEST(get_pid_number_valid_dir);
457   RUN_TEST(get_pid_number_invalid_dir_name);
458   RUN_TEST(read_proc_name_valid_name);
459   RUN_TEST(read_proc_name_invalid_name);
460   RUN_TEST(fetch_pids_for_procs_one_proc_many_pid);
461   RUN_TEST(fetch_pids_for_procs_many_proc_many_pid);
462   stub_procfs_teardown();
463   END_TEST;
464 }
465
466 #else
467 /***************************************************************************
468  * PQOS v1.2 mocks
469  */
470 int pqos_mon_reset(void) { return 0; }
471 int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; }
472 int pqos_mon_start(const unsigned num_cores, const unsigned *cores,
473                    const enum pqos_mon_event event, void *context,
474                    struct pqos_mon_data *group) {
475   return 0;
476 }
477 int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event,
478                        void *context, struct pqos_mon_data *group) {
479   return 0;
480 }
481 int pqos_mon_stop(struct pqos_mon_data *group) { return 0; }
482 int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) {
483   return 0;
484 }
485 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
486   return 0;
487 }
488 int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; }
489 int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) {
490   return 0;
491 }
492 int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; }
493 int pqos_alloc_assign(const unsigned technology, const unsigned *core_array,
494                       const unsigned core_num, unsigned *class_id) {
495   return 0;
496 }
497 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
498   return 0;
499 }
500 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
501                           const unsigned task_num, unsigned *class_id) {
502   return 0;
503 }
504 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
505   return 0;
506 }
507 int pqos_init(const struct pqos_config *config) { return 0; }
508 int pqos_fini(void) { return 0; }
509 int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type,
510                       const struct pqos_capability **cap_item) {
511   return 0;
512 }
513 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
514   return 0;
515 }
516
517 DEF_TEST(pqos12_test_stub) {
518   EXPECT_EQ_INT(0, 0);
519   return 0;
520 }
521
522 int main(void) {
523   RUN_TEST(pqos12_test_stub);
524   END_TEST;
525 }
526 #endif