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) {
85 /***************************************************************************
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) {
95 int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event,
96 void *context, struct pqos_mon_data *group) {
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) {
103 int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) {
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) {
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) {
115 int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) {
118 int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array,
119 const unsigned task_num, unsigned *class_id) {
122 int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) {
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) {
131 int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
134 #endif /* LIBPQOS2 */
136 /***************************************************************************
142 * pids_list_get_element
145 * Gets list element at index position. Assumes list was created by
146 * pids_list_add_pid function.
150 * `index' Position of desired element relative to given list pointer.
153 * Pointer to element at index position.
154 * NULL if index exceeds list's length.
156 pids_list_t *pids_list_get_element(pids_list_t *list, const size_t index) {
159 while (list != NULL && current != index) {
168 * pids_list_find_element
171 * Gets index of element in the list matching
172 * given pid. Assumes PIDs are unique, stops searching
173 * on the first match.
177 * `pid' PID number to find
180 * Index of list element holding given PID.
182 int pids_list_find_element(pids_list_t *list, const pid_t pid) {
186 while (list != NULL) {
187 if (list->pid == pid) {
198 * pids_list_has_element
201 * Checks if the list contains given pid.
202 * Wrapper for pids_list_find_element function.
203 * Used to make tests easier to read.
207 * `pid' PID number to find
210 * 1 if list contains given PID
211 * 0 if list does not contain given PID
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;
222 * Frees memory allocated in the given list
227 void pids_list_free_all(pids_list_t *list) {
229 pids_list_t *previous = list;
235 typedef struct stub_proc_pid {
240 static const char *proc_fs = "/tmp/procfs_stub";
247 * Prepares testing environment by creating temporary
248 * PID/comm file structure.
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.
257 * -1 on base dir creation error.
258 * -2 on comm file creation error.
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) {
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);
274 FILE *fp = fopen(path, "w");
278 fwrite(proc_pids_array[i].comm, sizeof(char),
279 strlen(proc_pids_array[i].comm), fp);
287 * stub_procfs_teardown
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.
296 * system command result
298 int stub_procfs_teardown() {
300 sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd));
301 strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1);
305 /* Max PID value. More info:
306 * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html
308 #define MAX_PID 4194304
309 #define MAX_PID_STR "4194304"
311 /***************************************************************************
314 DEF_TEST(add_proc_pid_empty_list) {
316 proc_pids_t proc_pids_instance;
317 proc_pids_instance.pids = NULL;
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);
326 pids_list_free_all(proc_pids_instance.pids);
330 DEF_TEST(add_proc_pid_non_empty_list) {
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};
337 for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) {
338 pids_list_add_pid(&proc_pids_instance.pids, pids[i]);
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);
347 pids_list_free_all(proc_pids_instance.pids);
351 DEF_TEST(get_pid_number_valid_dir) {
354 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
359 int pid_conversion = get_pid_number(&d, &pid);
361 EXPECT_EQ_INT(0, pid_conversion);
362 EXPECT_EQ_INT(MAX_PID, pid);
368 DEF_TEST(get_pid_number_invalid_dir_name) {
371 sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name));
376 int pid_conversion = get_pid_number(&d, &pid);
378 EXPECT_EQ_INT(-2, pid_conversion);
379 EXPECT_EQ_INT(0, pid);
385 DEF_TEST(read_proc_name_valid_name) {
387 stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}};
388 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
390 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
395 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
397 EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result);
398 EXPECT_EQ_STR(pp_stubs[0].comm, comm);
401 stub_procfs_teardown();
405 DEF_TEST(read_proc_name_invalid_name) {
408 sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name));
413 int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm));
415 EXPECT_EQ_INT(-1, read_result);
421 DEF_TEST(fetch_pids_for_procs_one_proc_many_pid) {
423 const char *proc_names[] = {"proc1"};
424 stub_proc_pid_t pp_stubs[] = {{"proc1", 1007},
429 stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
430 proc_pids_t *output = NULL;
433 int result = fetch_pids_for_procs(proc_fs, proc_names,
434 STATIC_ARRAY_SIZE(proc_names), &output);
435 EXPECT_EQ_INT(0, result);
437 /* proc name check */
438 EXPECT_EQ_STR(proc_names[0], output[0].proccess_name);
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);
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);
451 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
452 pids_list_free_all(output[i].pids);
455 stub_procfs_teardown();
459 DEF_TEST(fetch_pids_for_procs_many_proc_many_pid) {
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;
471 int result = fetch_pids_for_procs(proc_fs, proc_names,
472 STATIC_ARRAY_SIZE(proc_names), &output);
473 EXPECT_EQ_INT(0, result);
475 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
477 /* proc name check */
478 EXPECT_EQ_STR(proc_names[i], output[i].proccess_name);
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),
486 /* check if proc struct has no incorrect pids */
487 EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid),
494 for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) {
495 pids_list_free_all(output[i].pids);
498 stub_procfs_teardown();
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();