1 #include "intel_rdt.c" /* sic */
5 /***************************************************************************
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) {
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) {
20 int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg) {
26 /***************************************************************************
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) {
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) {
41 int pqos_mon_add_pids(const unsigned num_pids, const pid_t *pids,
42 struct pqos_mon_data *group) {
45 int pqos_mon_remove_pids(const unsigned num_pids, const pid_t *pids,
46 struct pqos_mon_data *group) {
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) {
53 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
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) {
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) {
65 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
68 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
69 const unsigned task_num, unsigned *class_id) {
72 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
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) {
81 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
86 /***************************************************************************
92 * pids_list_get_element
95 * Gets list element at index position. Assumes list was created by
96 * pids_list_add_pid function.
100 * `index' Position of desired element relative to given list pointer.
103 * Pointer to element at index position.
104 * NULL if index exceeds list's length.
106 pids_list_t *pids_list_get_element(pids_list_t *list, const size_t index) {
109 while (list != NULL && current != index) {
118 * pids_list_find_element
121 * Gets index of element in the list matching
122 * given pid. Assumes PIDs are unique, stops searching
123 * on the first match.
127 * `pid' PID number to find
130 * Index of list element holding given PID.
132 int pids_list_find_element(pids_list_t *list, const pid_t pid) {
136 while (list != NULL) {
137 if (list->pid == pid) {
148 * pids_list_has_element
151 * Checks if the list contains given pid.
152 * Wrapper for pids_list_find_element function.
153 * Used to make tests easier to read.
157 * `pid' PID number to find
160 * 1 if list contains given PID
161 * 0 if list does not contain given PID
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;
172 * Frees memory allocated in the given list
177 void pids_list_free_all(pids_list_t *list) {
179 pids_list_t *previous = list;
185 typedef struct stub_proc_pid {
190 static const char *proc_fs = "/tmp/procfs_stub";
197 * Prepares testing environment by creating temporary
198 * PID/comm file structure.
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.
207 * -1 on base dir creation error.
208 * -2 on comm file creation error.
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) {
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);
224 FILE *fp = fopen(path, "w");
228 fwrite(proc_pids_array[i].comm, sizeof(char),
229 strlen(proc_pids_array[i].comm), fp);
237 * stub_procfs_teardown
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.
246 * system command result
248 int stub_procfs_teardown() {
250 sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd));
251 strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1);
255 /* Max PID value. More info:
256 * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html
258 #define MAX_PID 4194304
259 #define MAX_PID_STR "4194304"
261 /***************************************************************************
264 DEF_TEST(add_proc_pid_empty_list) {
266 proc_pids_t proc_pids_instance;
267 proc_pids_instance.pids = NULL;
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);
276 pids_list_free_all(proc_pids_instance.pids);
280 DEF_TEST(add_proc_pid_non_empty_list) {
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};
287 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) {
288 pids_list_add_pid(&proc_pids_instance.pids, pids[i]);
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);
297 pids_list_free_all(proc_pids_instance.pids);
301 DEF_TEST(get_pid_number_valid_dir) {
304 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
309 int pid_conversion = get_pid_number(&d, &pid);
311 EXPECT_EQ_INT(0, pid_conversion);
312 EXPECT_EQ_INT(MAX_PID, pid);
318 DEF_TEST(get_pid_number_invalid_dir_name) {
321 sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name));
326 int pid_conversion = get_pid_number(&d, &pid);
328 EXPECT_EQ_INT(-2, pid_conversion);
329 EXPECT_EQ_INT(0, pid);
335 DEF_TEST(read_proc_name_valid_name) {
337 stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}};
338 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
340 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
345 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
347 EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result);
348 EXPECT_EQ_STR(pp_stubs[0].comm, comm);
351 stub_procfs_teardown();
355 DEF_TEST(read_proc_name_invalid_name) {
358 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
363 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
365 EXPECT_EQ_INT(-1, read_result);
371 DEF_TEST(fetch_pids_for_procs_one_proc_many_pid) {
373 const char *proc_names[] = {"proc1"};
374 stub_proc_pid_t pp_stubs[] = {{"proc1", 1007},
379 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
380 proc_pids_t *output = NULL;
383 int result = fetch_pids_for_procs(proc_fs, proc_names,
384 STATIC_ARRAY_SIZE(proc_names), &output);
385 EXPECT_EQ_INT(0, result);
387 /* proc name check */
388 EXPECT_EQ_STR(proc_names[0], output[0].proccess_name);
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);
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);
401 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
402 pids_list_free_all(output[i].pids);
405 stub_procfs_teardown();
409 DEF_TEST(fetch_pids_for_procs_many_proc_many_pid) {
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;
421 int result = fetch_pids_for_procs(proc_fs, proc_names,
422 STATIC_ARRAY_SIZE(proc_names), &output);
423 EXPECT_EQ_INT(0, result);
425 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
427 /* proc name check */
428 EXPECT_EQ_STR(proc_names[i], output[i].proccess_name);
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),
436 /* check if proc struct has no incorrect pids */
437 EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid),
444 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
445 pids_list_free_all(output[i].pids);
448 stub_procfs_teardown();
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();
467 /***************************************************************************
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) {
477 int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event,
478 void *context, struct pqos_mon_data *group) {
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) {
485 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
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) {
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) {
497 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
500 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
501 const unsigned task_num, unsigned *class_id) {
504 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
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) {
513 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
517 DEF_TEST(pqos12_test_stub) {
523 RUN_TEST(pqos12_test_stub);