From 62be067773ebc9d9d958c9707332bfeaecedac8a Mon Sep 17 00:00:00 2001 From: "Starzyk, MateuszX" Date: Mon, 19 Feb 2018 14:52:38 +0000 Subject: [PATCH] intel_rdt: adds pids extraction for given processes list adds intel_rdt_tests file with unit-tests of newly introduced functions. Change-Id: I0e26322caa19f57b7d660807646bd437586cd80d Signed-off-by: Starzyk, MateuszX --- Makefile.am | 12 ++ src/intel_rdt.c | 227 ++++++++++++++++++++++- src/intel_rdt_test.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 752 insertions(+), 1 deletion(-) create mode 100644 src/intel_rdt_test.c diff --git a/Makefile.am b/Makefile.am index 485869e8..81b177b2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1070,6 +1070,18 @@ intel_rdt_la_SOURCES = \ intel_rdt_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBPQOS_CPPFLAGS) intel_rdt_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(BUILD_WITH_LIBPQOS_LDFLAGS) intel_rdt_la_LIBADD = $(BUILD_WITH_LIBPQOS_LIBS) + +test_plugin_intel_rdt_SOURCES = \ + src/intel_rdt_test.c \ + src/utils/config_cores/config_cores.h \ + src/utils/config_cores/config_cores.c \ + src/daemon/configfile.c \ + src/daemon/types_list.c +test_plugin_intel_rdt_CPPFLAGS = $(AM_CPPFLAGS) +test_plugin_intel_rdt_LDFLAGS = $(PLUGIN_LDFLAGS) +test_plugin_intel_rdt_LDADD = liboconfig.la libplugin_mock.la +check_PROGRAMS += test_plugin_intel_rdt +TESTS += test_plugin_intel_rdt endif if BUILD_PLUGIN_INTERFACE diff --git a/src/intel_rdt.c b/src/intel_rdt.c index 193aa77b..446dc655 100644 --- a/src/intel_rdt.c +++ b/src/intel_rdt.c @@ -23,12 +23,12 @@ * * Authors: * Serhiy Pshyk + * Starzyk, Mateusz **/ #include "collectd.h" #include "utils/common/common.h" #include "utils/config_cores/config_cores.h" - #include #define RDT_PLUGIN "intel_rdt" @@ -36,6 +36,11 @@ #define RDT_MAX_SOCKETS 8 #define RDT_MAX_SOCKET_CORES 64 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS) +/* + * Process name inside comm file is limited to 16 chars. + * More info here: http://man7.org/linux/man-pages/man5/proc.5.html + */ +#define RDT_MAX_PROC_COMM_LENGTH 16 typedef enum { UNKNOWN = 0, @@ -238,6 +243,226 @@ static int rdt_config_cgroups(oconfig_item_t *item) { return 0; } +/* Helper typedef for process name array + * Extra 1 char is added for string null termination. + */ +typedef char proc_comm_t[RDT_MAX_PROC_COMM_LENGTH + 1]; + +/* Linked one-way list of pids. */ +typedef struct pids_list_s { + pid_t pid; + struct pids_list_s *next; +} pids_list_t; + +/* Holds process name and list of pids assigned to that name */ +typedef struct proc_pids_s { + proc_comm_t proccess_name; + pids_list_t *pids; +} proc_pids_t; + +/* + * NAME + * pids_list_add_pid + * + * DESCRIPTION + * Adds pid at the end of the pids list. + * Allocates memory for new pid element, it is up to user to free it. + * + * PARAMETERS + * `list' Head of target pids_list. + * `pid' Pid to be added. + * + * RETURN VALUE + * On success, returns 0. + * -1 on memory allocation error. + */ +static int pids_list_add_pid(pids_list_t **list, const pid_t pid) { + pids_list_t *new_element = calloc(1, sizeof(*new_element)); + + if (new_element == NULL) { + ERROR(RDT_PLUGIN ": Alloc error\n"); + return -1; + } + new_element->pid = pid; + new_element->next = NULL; + + pids_list_t **current = list; + while (*current != NULL) { + current = &((*current)->next); + } + *current = new_element; + return 0; +} + +/* + * NAME + * read_proc_name + * + * DESCRIPTION + * Reads process name from given pid directory. + * Strips new-line character (\n). + * + * PARAMETERS + * `procfs_path` Path to systems proc directory (e.g. /proc) + * `pid_entry' Dirent for PID directory + * `name' Output buffer for process name, recommended proc_comm. + * `out_size' Output buffer size, recommended sizeof(proc_comm) + * + * RETURN VALUE + * On success, the number of read bytes (includes stripped \n). + * -1 on file open error + */ +static int read_proc_name(const char *procfs_path, + const struct dirent *pid_entry, char *name, + const size_t out_size) { + assert(procfs_path); + assert(pid_entry); + assert(name); + assert(out_size); + memset(name, 0, out_size); + + const char *comm_file_name = "comm"; + + char *path = ssnprintf_alloc("%s/%s/%s", procfs_path, pid_entry->d_name, + comm_file_name); + + FILE *f = fopen(path, "r"); + if (f == NULL) { + ERROR(RDT_PLUGIN ": Failed to open comm file, error: %d\n", errno); + sfree(path); + return -1; + } + size_t read_length = fread(name, sizeof(char), out_size, f); + fclose(f); + sfree(path); + /* strip new line ending */ + char *newline = strchr(name, '\n'); + if (newline) { + *newline = '\0'; + } + + return read_length; +} + +/* + * NAME + * get_pid_number + * + * DESCRIPTION + * Gets pid number for given /proc/pid directory entry or + * returns error if input directory does not hold PID information. + * + * PARAMETERS + * `entry' Dirent for PID directory + * `pid' PID number to be filled + * + * RETURN VALUE + * 0 on success. Negative number on error: + * -1: given entry is not a directory + * -2: PID conversion error + */ +static int get_pid_number(struct dirent *entry, pid_t *pid) { + char *tmp_end; /* used for strtoul error check*/ + + if (pid == NULL || entry == NULL) + return -1; + + if (entry->d_type != DT_DIR) + return -1; + + /* trying to get pid number from directory name*/ + *pid = strtoul(entry->d_name, &tmp_end, 10); + if (*tmp_end != '\0') { + return -2; /* conversion failed, not proc-pid */ + } + /* all checks passed, marking as success */ + return 0; +} + +/* + * NAME + * fetch_pids_for_procs + * + * DESCRIPTION + * Finds PIDs matching given process's names. + * Searches all PID directories in /proc fs and + * allocates memory for proc_pids structs, it is up to user to free it. + * Output array will have same element count as input array. + * + * PARAMETERS + * `procfs_path' Path to systems proc directory (e.g. /proc) + * `procs' Array of null-terminated strings with + * process' names to search for + * `procs_size' procs array element count + * `proc_pids_array' Address of pointer, under which new + * array of proc_pids will be allocated. Must be NULL. + * + * RETURN VALUE + * 0 on success. Negative number on error: + * -1: could not open /proc dir + */ +__attribute__((unused)) /* TODO: remove this attribute when PID monitoring is + implemented */ +static int +fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array, + const size_t procs_names_array_size, + proc_pids_t **proc_pids_array) { + assert(procfs_path); + assert(procs_names_array); + assert(procs_names_array_size); + assert(proc_pids_array); + assert(NULL == *proc_pids_array); + + DIR *proc_dir = opendir(procfs_path); + if (proc_dir == NULL) { + ERROR(RDT_PLUGIN ": Could not open %s directory, error: %d", procfs_path, + errno); + return -1; + } + + /* Copy procs names to output array. Initialize pids list with NULL value. */ + (*proc_pids_array) = + calloc(procs_names_array_size, sizeof(**proc_pids_array)); + for (size_t i = 0; i < procs_names_array_size; ++i) { + sstrncpy((*proc_pids_array)[i].proccess_name, procs_names_array[i], + STATIC_ARRAY_SIZE((*proc_pids_array)[i].proccess_name)); + (*proc_pids_array)[i].pids = NULL; + } + + /* Go through procfs and find PIDS and their comms */ + struct dirent *entry; + while ((entry = readdir(proc_dir)) != NULL) { + + pid_t pid; + int pid_conversion = get_pid_number(entry, &pid); + if (pid_conversion < 0) + continue; + + proc_comm_t comm; + int read_result = + read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t)); + if (read_result <= 0) { + ERROR(RDT_PLUGIN ": Comm file skipped. Read result: %d", read_result); + continue; + } + + /* Try to find comm in input procs array (proc_pids_array has same names) */ + for (size_t i = 0; i < procs_names_array_size; ++i) { + if (0 == strncmp(comm, (*proc_pids_array)[i].proccess_name, + STATIC_ARRAY_SIZE(comm))) + pids_list_add_pid(&((*proc_pids_array)[i].pids), pid); + } + } + + int close_result = closedir(proc_dir); + if (0 != close_result) { + ERROR(RDT_PLUGIN ": failed to close %s directory, error: %d", procfs_path, + errno); + return -1; + } + return 0; +} + static void rdt_pqos_log(void *context, const size_t size, const char *msg) { DEBUG(RDT_PLUGIN ": %s", msg); } diff --git a/src/intel_rdt_test.c b/src/intel_rdt_test.c new file mode 100644 index 00000000..5d88fa0e --- /dev/null +++ b/src/intel_rdt_test.c @@ -0,0 +1,514 @@ +#include "intel_rdt.c" /* sic */ +#include "testing.h" +#include + +/*************************************************************************** + * PQOS mocks + */ +#if PQOS_VERSION >= 30000 +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg, + const enum pqos_cdp_config l2_cdp_cfg, + const enum pqos_mba_config mba_cfg) { + return 0; +} +#elif PQOS_VERSION >= 2000 +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg, + const enum pqos_cdp_config l2_cdp_cfg) { + return 0; +} +#else +int pqos_alloc_reset(const enum pqos_cdp_config l3_cdp_cfg) { + return 0; +} +#endif + +#ifdef LIBPQOS2 +/*************************************************************************** + * PQOS v2.0 mocks + */ +int pqos_mon_reset(void) { return 0; } +int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; } +int pqos_mon_start(const unsigned num_cores, const unsigned *cores, + const enum pqos_mon_event event, void *context, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_start_pids(const unsigned num_pids, const pid_t *pids, + const enum pqos_mon_event event, void *context, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_add_pids(const unsigned num_pids, const pid_t *pids, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_remove_pids(const unsigned num_pids, const pid_t *pids, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_stop(struct pqos_mon_data *group) { return 0; } +int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) { + return 0; +} +int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; } +int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; } +int pqos_alloc_assign(const unsigned technology, const unsigned *core_array, + const unsigned core_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) { + return 0; +} +int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array, + const unsigned task_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) { + return 0; +} +int pqos_init(const struct pqos_config *config) { return 0; } +int pqos_fini(void) { return 0; } +int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type, + const struct pqos_capability **cap_item) { + return 0; +} +int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) { + return 0; +} +#else +/*************************************************************************** + * PQOS v1.2 mocks + */ +int pqos_mon_reset(void) { return 0; } +int pqos_mon_assoc_get(const unsigned lcore, pqos_rmid_t *rmid) { return 0; } +int pqos_mon_start(const unsigned num_cores, const unsigned *cores, + const enum pqos_mon_event event, void *context, + struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_start_pid(const pid_t pids, const enum pqos_mon_event event, + void *context, struct pqos_mon_data *group) { + return 0; +} +int pqos_mon_stop(struct pqos_mon_data *group) { return 0; } +int pqos_mon_poll(struct pqos_mon_data **groups, const unsigned num_groups) { + return 0; +} +int pqos_alloc_assoc_set(const unsigned lcore, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get(const unsigned lcore, unsigned *class_id) { return 0; } +int pqos_alloc_assoc_set_pid(const pid_t task, const unsigned class_id) { + return 0; +} +int pqos_alloc_assoc_get_pid(const pid_t task, unsigned *class_id) { return 0; } +int pqos_alloc_assign(const unsigned technology, const unsigned *core_array, + const unsigned core_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release(const unsigned *core_array, const unsigned core_num) { + return 0; +} +int pqos_alloc_assign_pid(const unsigned technology, const pid_t *task_array, + const unsigned task_num, unsigned *class_id) { + return 0; +} +int pqos_alloc_release_pid(const pid_t *task_array, const unsigned task_num) { + return 0; +} +int pqos_init(const struct pqos_config *config) { return 0; } +int pqos_fini(void) { return 0; } +int pqos_cap_get_type(const struct pqos_cap *cap, const enum pqos_cap_type type, + const struct pqos_capability **cap_item) { + return 0; +} +int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) { + return 0; +} +#endif /* LIBPQOS2 */ + +/*************************************************************************** + * helper functions + */ + +/* + * NAME + * pids_list_get_element + * + * DESCRIPTION + * Gets list element at index position. Assumes list was created by + * pids_list_add_pid function. + * + * PARAMETERS + * `list' Pids list + * `index' Position of desired element relative to given list pointer. + * + * RETURN VALUE + * Pointer to element at index position. + * NULL if index exceeds list's length. + */ +pids_list_t *pids_list_get_element(pids_list_t *list, const size_t index) { + assert(list); + size_t current = 0; + while (list != NULL && current != index) { + list = list->next; + current++; + } + return list; +} + +/* + * NAME + * pids_list_find_element + * + * DESCRIPTION + * Gets index of element in the list matching + * given pid. Assumes PIDs are unique, stops searching + * on the first match. + * + * PARAMETERS + * `list' Pids list + * `pid' PID number to find + * + * RETURN VALUE + * Index of list element holding given PID. + */ +int pids_list_find_element(pids_list_t *list, const pid_t pid) { + assert(list); + int result = -1; + size_t current = 0; + while (list != NULL) { + if (list->pid == pid) { + result = current; + break; + } + list = list->next; + } + return result; +} + +/* + * NAME + * pids_list_has_element + * + * DESCRIPTION + * Checks if the list contains given pid. + * Wrapper for pids_list_find_element function. + * Used to make tests easier to read. + * + * PARAMETERS + * `list' Pids list + * `pid' PID number to find + * + * RETURN VALUE + * 1 if list contains given PID + * 0 if list does not contain given PID + */ +int pids_list_has_element(pids_list_t *list, const pid_t pid) { + return pids_list_find_element(list, pid) >= 0 ? 1 : 0; +} + +/* + * NAME + * pids_list_free_all + * + * DESCRIPTION + * Frees memory allocated in the given list + * + * PARAMETERS + * `list' Pids list + */ +void pids_list_free_all(pids_list_t *list) { + while (list) { + pids_list_t *previous = list; + list = list->next; + free(previous); + } +} + +typedef struct stub_proc_pid { + proc_comm_t comm; + pid_t pid; +} stub_proc_pid_t; + +static const char *proc_fs = "/tmp/procfs_stub"; + +/* + * NAME + * stub_procfs_setup + * + * DESCRIPTION + * Prepares testing environment by creating temporary + * PID/comm file structure. + * + * PARAMETERS + * `proc_pids_array' Array of stub_proc_pid_t structs. Represents + * which PIDs should hold given process name. + * `proc_pids_array_length' Element count of input array. + * + * RETURN VALUE + * 0 on success. + * -1 on base dir creation error. + * -2 on comm file creation error. + */ +int stub_procfs_setup(const stub_proc_pid_t *proc_pids_array, + const size_t proc_pids_array_length) { + if (mkdir(proc_fs, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { + return -1; + } + char path[256]; + + for (size_t i = 0; i < proc_pids_array_length; ++i) { + memset(path, 0, sizeof(path)); + snprintf(path, STATIC_ARRAY_SIZE(path), "%s/%d", proc_fs, + proc_pids_array[i].pid); + mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + strncat(path, "/comm", STATIC_ARRAY_SIZE(path) - strlen(path) - 1); + + FILE *fp = fopen(path, "w"); + if (!fp) { + return -2; + } + fwrite(proc_pids_array[i].comm, sizeof(char), + strlen(proc_pids_array[i].comm), fp); + fclose(fp); + } + return 0; +} + +/* + * NAME + * stub_procfs_teardown + * + * DESCRIPTION + * Clears testing environment: removes stub proc files. + * NOTE - This function could be implemented by usage of nftw, but this + * would require #define _XOPEN_SOURCE 500, which + * messes up intel_rdt includes. + * + * RETURN VALUE + * system command result + */ +int stub_procfs_teardown() { + char cmd[256]; + sstrncpy(cmd, "rm -rf ", STATIC_ARRAY_SIZE(cmd)); + strncat(cmd, proc_fs, STATIC_ARRAY_SIZE(cmd) - strlen(cmd) - 1); + return system(cmd); +} + +/* Max PID value. More info: + * http://web.archive.org/web/20111209081734/http://research.cs.wisc.edu/condor/condorg/linux_scalability.html + */ +#define MAX_PID 4194304 +#define MAX_PID_STR "4194304" + +/*************************************************************************** + * tests + */ +DEF_TEST(add_proc_pid_empty_list) { + /* setup */ + proc_pids_t proc_pids_instance; + proc_pids_instance.pids = NULL; + pid_t pid = 1234; + + /* check */ + pids_list_add_pid(&proc_pids_instance.pids, pid); + pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, 0); + EXPECT_EQ_INT(pid, added->pid); + + /* cleanup */ + pids_list_free_all(proc_pids_instance.pids); + return 0; +} + +DEF_TEST(add_proc_pid_non_empty_list) { + /* setup */ + proc_pids_t proc_pids_instance; + proc_pids_instance.pids = NULL; + pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007}; + + /* check */ + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) { + pids_list_add_pid(&proc_pids_instance.pids, pids[i]); + } + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i) { + pids_list_t *added = pids_list_get_element(proc_pids_instance.pids, i); + EXPECT_EQ_INT(pids[i], added->pid); + } + + /* cleanup */ + pids_list_free_all(proc_pids_instance.pids); + return 0; +} + +DEF_TEST(get_pid_number_valid_dir) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + pid_t pid = 0; + + /* check */ + int pid_conversion = get_pid_number(&d, &pid); + + EXPECT_EQ_INT(0, pid_conversion); + EXPECT_EQ_INT(MAX_PID, pid); + + /* cleanup */ + return 0; +} + +DEF_TEST(get_pid_number_invalid_dir_name) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, "invalid", STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + pid_t pid = 0; + + /* check */ + int pid_conversion = get_pid_number(&d, &pid); + + EXPECT_EQ_INT(-2, pid_conversion); + EXPECT_EQ_INT(0, pid); + + /* cleanup */ + return 0; +} + +DEF_TEST(read_proc_name_valid_name) { + /* setup */ + stub_proc_pid_t pp_stubs[] = {{"proc1", MAX_PID}}; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + + /* check */ + proc_comm_t comm; + int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm)); + + EXPECT_EQ_INT(strlen(pp_stubs[0].comm), read_result); + EXPECT_EQ_STR(pp_stubs[0].comm, comm); + + /* cleanup */ + stub_procfs_teardown(); + return 0; +} + +DEF_TEST(read_proc_name_invalid_name) { + /* setup */ + struct dirent d; + sstrncpy(d.d_name, MAX_PID_STR, STATIC_ARRAY_SIZE(d.d_name)); + d.d_type = DT_DIR; + + /* check */ + proc_comm_t comm; + int read_result = read_proc_name(proc_fs, &d, comm, STATIC_ARRAY_SIZE(comm)); + + EXPECT_EQ_INT(-1, read_result); + + /* cleanup */ + return 0; +} + +DEF_TEST(fetch_pids_for_procs_one_proc_many_pid) { + /* setup */ + const char *proc_names[] = {"proc1"}; + stub_proc_pid_t pp_stubs[] = {{"proc1", 1007}, + {"proc1", 1008}, + {"proc1", 1009}, + {"proc2", 1010}, + {"proc3", 1011}}; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + proc_pids_t *output = NULL; + + /* check */ + int result = fetch_pids_for_procs(proc_fs, proc_names, + STATIC_ARRAY_SIZE(proc_names), &output); + EXPECT_EQ_INT(0, result); + + /* proc name check */ + EXPECT_EQ_STR(proc_names[0], output[0].proccess_name); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(pp_stubs); ++i) { + if (0 == strcmp(pp_stubs[i].comm, proc_names[0])) { + /* check if proc struct has correct pids */ + EXPECT_EQ_INT(pids_list_has_element(output[0].pids, pp_stubs[i].pid), 1); + } else { + /* check if proc struct has no incorrect pids */ + EXPECT_EQ_INT(pids_list_has_element(output[0].pids, pp_stubs[i].pid), 0); + } + } + + /* cleanup */ + for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) { + pids_list_free_all(output[i].pids); + } + free(output); + stub_procfs_teardown(); + return 0; +} + +DEF_TEST(fetch_pids_for_procs_many_proc_many_pid) { + /* setup */ + const char *proc_names[] = {"proc1", "proc2", "proc3"}; + stub_proc_pid_t pp_stubs[] = { + {"proc1", 1007}, {"proc1", 1008}, {"proc1", 1009}, {"proc2", 2007}, + {"proc2", 2008}, {"proc2", 2009}, {"proc3", 3007}, {"proc3", 3008}, + {"proc3", 3009}, {"proc4", 4007}, {"proc4", 4008}, {"proc4", 4009}, + {"proc5", 5007}, {"proc5", 5008}, {"proc5", 5009}}; + stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs)); + proc_pids_t *output = NULL; + + /* check */ + int result = fetch_pids_for_procs(proc_fs, proc_names, + STATIC_ARRAY_SIZE(proc_names), &output); + EXPECT_EQ_INT(0, result); + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) { + + /* proc name check */ + EXPECT_EQ_STR(proc_names[i], output[i].proccess_name); + + for (size_t j = 0; j < STATIC_ARRAY_SIZE(pp_stubs); ++j) { + if (0 == strcmp(pp_stubs[j].comm, proc_names[i])) { + /* check if proc struct has correct pids */ + EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid), + 1); + } else { + /* check if proc struct has no incorrect pids */ + EXPECT_EQ_INT(pids_list_has_element(output[i].pids, pp_stubs[j].pid), + 0); + } + } + } + + /* cleanup */ + for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i) { + pids_list_free_all(output[i].pids); + } + free(output); + stub_procfs_teardown(); + return 0; +} + +int main(void) { + stub_procfs_teardown(); + RUN_TEST(add_proc_pid_empty_list); + RUN_TEST(add_proc_pid_non_empty_list); + RUN_TEST(get_pid_number_valid_dir); + RUN_TEST(get_pid_number_invalid_dir_name); + RUN_TEST(read_proc_name_valid_name); + RUN_TEST(read_proc_name_invalid_name); + RUN_TEST(fetch_pids_for_procs_one_proc_many_pid); + RUN_TEST(fetch_pids_for_procs_many_proc_many_pid); + stub_procfs_teardown(); + END_TEST; +} -- 2.11.0