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