/**
* collectd - src/intel_rdt.c
*
- * Copyright(c) 2016-2018 Intel Corporation. All rights reserved.
+ * Copyright(c) 2016-2019 Intel Corporation. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* Serhiy Pshyk <serhiyx.pshyk@intel.com>
* Starzyk, Mateusz <mateuszx.starzyk@intel.com>
* Wojciech Andralojc <wojciechx.andralojc@intel.com>
+ * Michał Aleksiński <michalx.aleksinski@intel.com>
**/
#include "collectd.h"
char *desc;
size_t num_names;
char **names;
- proc_pids_t *proc_pids_array;
+ proc_pids_t **proc_pids;
size_t monitored_pids_count;
enum pqos_mon_event events;
};
rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS];
struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS];
size_t num_ngroups;
+ proc_pids_t **proc_pids;
+ size_t num_proc_pids;
#endif /* LIBPQOS2 */
const struct pqos_cpuinfo *pqos_cpu;
const struct pqos_cap *pqos_cap;
for (size_t i = 0; i < g_rdt->num_ngroups; ++i) {
memset(pids, 0, sizeof(pids));
for (size_t j = 0; j < g_rdt->ngroups[i].num_names; ++j) {
- pids_list_t *list = g_rdt->ngroups[i].proc_pids_array[j].pids;
- while (list != NULL) {
+ pids_list_t *list = g_rdt->ngroups[i].proc_pids[j].curr;
+ for (size_t k = 0; k < list->size; k++)
snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
- list->pid);
- list = list->next;
- }
+ list->pids[k]);
}
DEBUG(RDT_PLUGIN ": [%s] %s", g_rdt->ngroups[i].desc, pids);
}
return -ENOMEM;
}
- groups[index].proc_pids_array = NULL;
+ groups[index].proc_pids = NULL;
groups[index].monitored_pids_count = 0;
index++;
return index;
}
+/*
+ * NAME
+ * rdt_free_ngroups
+ *
+ * DESCRIPTION
+ * Function to deallocate memory allocated for name groups.
+ *
+ * PARAMETERS
+ * `rdt' Pointer to rdt context
+ */
static void rdt_free_ngroups(rdt_ctx_t *rdt) {
for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
if (rdt->ngroups[i].desc)
sfree(rdt->ngroups[i].desc);
strarray_free(rdt->ngroups[i].names, rdt->ngroups[i].num_names);
- if (rdt->ngroups[i].proc_pids_array) {
- for (size_t j = 0; j < rdt->ngroups[i].num_names; ++j) {
- if (NULL == rdt->ngroups[i].proc_pids_array[j].pids)
- continue;
- pids_list_free(rdt->ngroups[i].proc_pids_array[j].pids);
- }
+ if (rdt->ngroups[i].proc_pids)
+ proc_pids_free(rdt->ngroups[i].proc_pids, rdt->ngroups[i].num_names);
- sfree(rdt->ngroups[i].proc_pids_array);
- }
rdt->ngroups[i].num_names = 0;
sfree(rdt->pngroups[i]);
}
+ if (rdt->proc_pids)
+ sfree(rdt->proc_pids);
}
+/*
+ * NAME
+ * rdt_config_ngroups
+ *
+ * DESCRIPTION
+ * Reads name groups configuration.
+ *
+ * PARAMETERS
+ * `rdt` Pointer to rdt context
+ * `item' Config option containing process names groups.
+ *
+ * RETURN VALUE
+ * 0 on success. Negative number on error.
+ */
static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
int n = 0;
enum pqos_mon_event events = 0;
return 0;
}
+/*
+ * NAME
+ * rdt_refresh_ngroup
+ *
+ * DESCRIPTION
+ * Refresh pids monitored by name group.
+ *
+ * PARAMETERS
+ * `ngroup` Pointer to name group.
+ * `group_mon_data' PQoS monitoring context.
+ *
+ * RETURN VALUE
+ * 0 on success. Negative number on error.
+ */
static int rdt_refresh_ngroup(rdt_name_group_t *ngroup,
struct pqos_mon_data *group_mon_data) {
if (NULL == ngroup)
return -1;
- if (NULL == ngroup->proc_pids_array) {
+ if (NULL == ngroup->proc_pids) {
ERROR(RDT_PLUGIN
": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.",
ngroup->desc);
DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.",
ngroup->desc);
- proc_pids_t *proc_pids_array_prev = ngroup->proc_pids_array;
- proc_pids_t *proc_pids_array_curr = NULL;
-
- int fetch_result =
- fetch_pids_for_procs(RDT_PROC_PATH, (const char **)ngroup->names,
- ngroup->num_names, &proc_pids_array_curr);
-
- if (0 != fetch_result) {
- ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' failed to fetch PIDs.",
- ngroup->desc);
- return fetch_result;
- }
-
- pids_list_t *new_pids = NULL;
- pid_t *new_pids_array = NULL;
- size_t new_pids_count = 0;
+ proc_pids_t **proc_pids = ngroup->proc_pids;
+ pids_list_t added_pids;
+ pids_list_t removed_pids;
- pids_list_t *lost_pids = NULL;
- pid_t *lost_pids_array = NULL;
- size_t lost_pids_count = 0;
+ memset(&added_pids, 0, sizeof(added_pids));
+ memset(&removed_pids, 0, sizeof(removed_pids));
for (size_t i = 0; i < ngroup->num_names; ++i) {
- if (NULL == proc_pids_array_prev[i].pids &&
- NULL == proc_pids_array_curr[i].pids)
- continue;
- int diff_result = pids_list_diff(
- proc_pids_array_prev[i].pids, proc_pids_array_curr[i].pids, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
+ int diff_result = pids_list_diff(proc_pids[i], &added_pids, &removed_pids);
if (0 != diff_result) {
ERROR(RDT_PLUGIN
": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.",
DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: "
"%u, removed: %u.",
- ngroup->desc, (unsigned)new_pids_count, (unsigned)lost_pids_count);
-
- if (new_pids && new_pids_count > 0) {
- new_pids_array = malloc(new_pids_count * sizeof(pid_t));
- if (new_pids_array == NULL) {
- ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Memory "
- "allocation failed",
- ngroup->desc);
- result = -1;
- goto cleanup;
- }
- pids_list_to_array(new_pids_array, new_pids, new_pids_count);
+ ngroup->desc, (unsigned)added_pids.size, (unsigned)removed_pids.size);
+
+ if (added_pids.size > 0) {
/* no pids are monitored for this group yet: start monitoring */
if (0 == ngroup->monitored_pids_count) {
int start_result =
- pqos_mon_start_pids(new_pids_count, new_pids_array, ngroup->events,
+ pqos_mon_start_pids(added_pids.size, added_pids.pids, ngroup->events,
(void *)ngroup->desc, group_mon_data);
if (PQOS_RETVAL_OK == start_result) {
- ngroup->monitored_pids_count = new_pids_count;
+ ngroup->monitored_pids_count = added_pids.size;
} else {
ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while "
"STARTING pids monitoring",
} else {
int add_result =
- pqos_mon_add_pids(new_pids_count, new_pids_array, group_mon_data);
+ pqos_mon_add_pids(added_pids.size, added_pids.pids, group_mon_data);
if (PQOS_RETVAL_OK == add_result)
- ngroup->monitored_pids_count += new_pids_count;
+ ngroup->monitored_pids_count += added_pids.size;
else {
ERROR(RDT_PLUGIN
": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.",
}
}
- if (lost_pids && lost_pids_count > 0) {
- lost_pids_array = malloc(lost_pids_count * sizeof(pid_t));
- if (lost_pids_array == NULL) {
- ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Memory "
- "allocation failed",
- ngroup->desc);
- result = -1;
- goto cleanup;
- }
- pids_list_to_array(lost_pids_array, lost_pids, lost_pids_count);
+ if (removed_pids.size > 0) {
- if (lost_pids_count == ngroup->monitored_pids_count) {
+ /* all pids are removed: stop monitoring */
+ if (removed_pids.size == ngroup->monitored_pids_count) {
/* all pids for this group are lost: stop monitoring */
int stop_result = pqos_mon_stop(group_mon_data);
if (PQOS_RETVAL_OK != stop_result) {
}
ngroup->monitored_pids_count = 0;
} else {
- assert(lost_pids_count < ngroup->monitored_pids_count);
- int remove_result = pqos_mon_remove_pids(lost_pids_count, lost_pids_array,
- group_mon_data);
+ int remove_result = pqos_mon_remove_pids(
+ removed_pids.size, removed_pids.pids, group_mon_data);
if (PQOS_RETVAL_OK == remove_result) {
- ngroup->monitored_pids_count -= lost_pids_count;
+ ngroup->monitored_pids_count -= removed_pids.size;
} else {
ERROR(RDT_PLUGIN
": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.",
}
}
- if (new_pids_count > 0 || lost_pids_count > 0)
- ngroup->proc_pids_array = proc_pids_array_curr;
-
goto cleanup;
pqos_error_recovery:
DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.",
ngroup->desc);
pqos_mon_stop(group_mon_data);
- for (size_t i = 0; i < ngroup->num_names; ++i) {
- if (ngroup->proc_pids_array[i].pids)
- pids_list_free(ngroup->proc_pids_array[i].pids);
- }
- sfree(ngroup->proc_pids_array);
+ for (size_t i = 0; i < ngroup->num_names; ++i)
+ if (ngroup->proc_pids[i]->curr)
+ ngroup->proc_pids[i]->curr->size = 0;
- initialize_proc_pids((const char **)ngroup->names, ngroup->num_names,
- &ngroup->proc_pids_array);
ngroup->monitored_pids_count = 0;
cleanup:
- if (ngroup->proc_pids_array == proc_pids_array_curr) {
- assert(proc_pids_array_curr);
- /* new list was successfully saved, free the old one */
- for (size_t i = 0; i < ngroup->num_names; ++i)
- if (proc_pids_array_prev[i].pids)
- pids_list_free(proc_pids_array_prev[i].pids);
-
- sfree(proc_pids_array_prev);
-
- } else {
- /* new list was not saved. Free the new list, keep the old one*/
- for (size_t i = 0; i < ngroup->num_names; ++i)
- if (proc_pids_array_curr[i].pids)
- pids_list_free(proc_pids_array_curr[i].pids);
-
- sfree(proc_pids_array_curr);
- }
-
- if (new_pids)
- pids_list_free(new_pids);
-
- if (new_pids_array)
- free(new_pids_array);
-
- if (lost_pids)
- pids_list_free(lost_pids);
-
- if (lost_pids_array)
- free(lost_pids_array);
+ pids_list_clear(&added_pids);
+ pids_list_clear(&removed_pids);
return result;
}
+/*
+ * NAME
+ * read_pids_data
+ *
+ * DESCRIPTION
+ * Poll monitoring statistics for name groups
+ *
+ * RETURN VALUE
+ * 0 on success. Negative number on error.
+ */
static int read_pids_data() {
if (0 == g_rdt->num_ngroups) {
#endif /* COLLECT_DEBUG */
groups_refresh:
+ ret = update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
+ if (0 != ret) {
+ ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
+ return ret;
+ }
+
for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
int refresh_result =
rdt_refresh_ngroup(&(g_rdt->ngroups[i]), g_rdt->pngroups[i]);
return ret;
}
+/*
+ * NAME
+ * rdt_init_pids_monitoring
+ *
+ * DESCRIPTION
+ * Initialize pids monitoring for all name groups
+ */
static void rdt_init_pids_monitoring() {
for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
/*
*/
rdt_name_group_t *ng = &g_rdt->ngroups[group_idx];
int init_result = initialize_proc_pids((const char **)ng->names,
- ng->num_names, &ng->proc_pids_array);
+ ng->num_names, &ng->proc_pids);
if (0 != init_result) {
ERROR(RDT_PLUGIN
": Initialization of proc_pids for group %zu failed. Error: %d",
continue;
}
+ /* update global proc_pids table */
+ proc_pids_t **proc_pids = realloc(g_rdt->proc_pids,
+ (g_rdt->num_proc_pids + ng->num_names) *
+ sizeof(*g_rdt->proc_pids));
+ if (NULL == proc_pids) {
+ ERROR(RDT_PLUGIN ": Alloc error\n");
+ continue;
+ }
+
+ for (size_t i = 0; i < ng->num_names; i++)
+ proc_pids[g_rdt->num_proc_pids + i] = ng->proc_pids[i];
+
+ g_rdt->proc_pids = proc_pids;
+ g_rdt->num_proc_pids += ng->num_names;
+ }
+
+ int update_result =
+ update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
+ if (0 != update_result)
+ ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
+
+ for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
g_rdt->pngroups[group_idx]);
if (0 != refresh_result)
}
}
#endif /* LIBPQOS2 */
-
+/*
+ * NAME
+ * rdt_free_ngroups
+ *
+ * DESCRIPTION
+ * Function to deallocate memory allocated for core groups.
+ */
static void rdt_free_cgroups(void) {
config_cores_cleanup(&g_rdt->cores);
for (int i = 0; i < RDT_MAX_CORES; i++) {
+/**
+ * collectd - src/utils_config_pids.c
+ *
+ * Copyright(c) 2018-2019 Intel Corporation. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ * Starzyk, Mateusz <mateuszx.starzyk@intel.com>
+ * Wojciech Andralojc <wojciechx.andralojc@intel.com>
+ * Michał Aleksiński <michalx.aleksinski@intel.com>
+ **/
+
#include "collectd.h"
#include "utils/common/common.h"
#include "utils_proc_pids.h"
void pids_list_free(pids_list_t *list) {
assert(list);
- pids_list_t *current = list;
- while (current != NULL) {
- pids_list_t *previous = current;
- current = current->next;
- sfree(previous);
- }
+ sfree(list->pids);
+ sfree(list);
}
int is_proc_name_valid(const char *name) {
return 0;
}
-int pids_list_add_pid(pids_list_t **list, const pid_t pid) {
+int pids_list_add_pid(pids_list_t *list, const pid_t pid) {
assert(list);
- pids_list_t *new_element = calloc(1, sizeof(*new_element));
+ if (list->allocated == list->size) {
+ size_t new_allocated = list->allocated + 1 + list->allocated / 10;
+ pid_t *new_pids = realloc(list->pids, sizeof(pid_t) * new_allocated);
- if (new_element == NULL) {
- ERROR(UTIL_NAME ": Alloc error\n");
- return -1;
- }
- new_element->pid = pid;
- new_element->next = NULL;
+ if (NULL == new_pids) {
+ ERROR(UTIL_NAME ": Alloc error\n");
+ return -1;
+ }
- pids_list_t **current = list;
- while (*current != NULL) {
- current = &((*current)->next);
+ list->pids = new_pids;
+ list->allocated = new_allocated;
}
- *current = new_element;
- return 0;
-}
-int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
- assert(list);
+ list->pids[list->size] = pid;
+ list->size++;
- pids_list_t *current = list;
- while (current != NULL) {
- if (current->pid == pid)
- return 1;
- current = current->next;
- }
return 0;
}
-int pids_list_add_pids_list(pids_list_t **dst, pids_list_t *src,
- size_t *dst_num) {
+int pids_list_add_list(pids_list_t *dst, pids_list_t *src) {
assert(dst);
assert(src);
- assert(dst_num);
- pids_list_t *current = src;
- int ret;
+ if (dst->allocated < dst->size + src->size) {
+ pid_t *new_pids =
+ realloc(dst->pids, sizeof(pid_t) * (dst->size + src->size));
- while (current != NULL) {
- ret = pids_list_add_pid(dst, current->pid);
- if (0 != ret)
- return ret;
+ if (NULL == new_pids) {
+ ERROR(UTIL_NAME ": Alloc error\n");
+ return -1;
+ }
- ++(*dst_num);
- current = current->next;
+ dst->allocated = dst->size + src->size;
+ dst->pids = new_pids;
}
+ memcpy(dst->pids + dst->size, src->pids, src->size * sizeof(*(src->pids)));
+ dst->size += src->size;
+
+ return 0;
+}
+
+int pids_list_clear(pids_list_t *list) {
+ assert(list);
+
+ if (list->pids != NULL)
+ sfree(list->pids);
+
+ list->size = 0;
+ list->allocated = 0;
+
+ return 0;
+}
+
+int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
+ assert(list);
+
+ for (int i = 0; i < list->size; i++)
+ if (list->pids[i] == pid)
+ return 1;
+
return 0;
}
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);
return 0;
}
-void pids_list_to_array(pid_t *array, pids_list_t *list,
- const size_t array_length) {
-
- assert(list);
- assert(array);
- assert(array_length > 0);
-
- size_t current = 0;
-
- while (list != NULL && current < array_length) {
- array[current] = list->pid;
- list = list->next;
- ++current;
- }
-}
-
int initialize_proc_pids(const char **procs_names_array,
const size_t procs_names_array_size,
- proc_pids_t **proc_pids_array) {
+ proc_pids_t **proc_pids[]) {
- assert(proc_pids_array);
- assert(NULL == *proc_pids_array);
+ proc_pids_t **proc_pids_array;
+ assert(proc_pids);
+ assert(NULL == *proc_pids);
/* Copy procs names to output array. Initialize pids list with NULL value. */
- (*proc_pids_array) =
- calloc(procs_names_array_size, sizeof(**proc_pids_array));
+ proc_pids_array = calloc(procs_names_array_size, sizeof(*proc_pids_array));
- if (NULL == (*proc_pids_array))
+ if (NULL == proc_pids_array)
return -1;
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;
+ proc_pids_array[i] = calloc(1, sizeof(**proc_pids_array));
+ if (NULL == proc_pids_array[i])
+ goto initialize_proc_pids_error;
+
+ sstrncpy(proc_pids_array[i]->process_name, procs_names_array[i],
+ STATIC_ARRAY_SIZE(proc_pids_array[i]->process_name));
+ proc_pids_array[i]->prev = NULL;
+ proc_pids_array[i]->curr = NULL;
}
+ *proc_pids = proc_pids_array;
+
return 0;
+initialize_proc_pids_error:
+ if (NULL != proc_pids_array) {
+ for (size_t i = 0; i < procs_names_array_size; ++i) {
+ free(proc_pids_array[i]);
+ }
+ free(proc_pids_array);
+ }
+ return -1;
}
-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) {
+static void swap_proc_pids(proc_pids_t **proc_pids, size_t proc_pids_num) {
+ for (size_t i = 0; i < proc_pids_num; i++) {
+ pids_list_t *swap = proc_pids[i]->prev;
+ proc_pids[i]->prev = proc_pids[i]->curr;
+ proc_pids[i]->curr = swap;
+ }
+}
+
+int update_proc_pids(const char *procfs_path, proc_pids_t **proc_pids,
+ size_t proc_pids_num) {
assert(procfs_path);
- assert(procs_names_array);
- assert(procs_names_array_size);
+ assert(proc_pids);
DIR *proc_dir = opendir(procfs_path);
if (proc_dir == NULL) {
return -1;
}
- int init_result = initialize_proc_pids(
- procs_names_array, procs_names_array_size, proc_pids_array);
- if (0 != init_result) {
- closedir(proc_dir);
- return -1;
+ swap_proc_pids(proc_pids, proc_pids_num);
+
+ for (size_t i = 0; i < proc_pids_num; i++) {
+ if (NULL == proc_pids[i]->curr)
+ proc_pids[i]->curr = calloc(1, sizeof(*(proc_pids[i]->curr)));
+
+ if (NULL == proc_pids[i]->curr) {
+ ERROR(UTIL_NAME ": Alloc error\n");
+ goto update_error;
+ }
+
+ proc_pids[i]->curr->size = 0;
}
/* 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)
proc_comm_t comm;
int read_result =
read_proc_name(procfs_path, entry, comm, sizeof(proc_comm_t));
- if (read_result <= 0) {
- ERROR(UTIL_NAME ": Comm file skipped. Read result: %d", read_result);
+ if (read_result <= 0)
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);
+ /* Try to find comm in input procs array */
+ for (size_t i = 0; i < proc_pids_num; ++i) {
+ if (0 ==
+ strncmp(comm, proc_pids[i]->process_name, STATIC_ARRAY_SIZE(comm)))
+ pids_list_add_pid(proc_pids[i]->curr, pid);
}
}
int close_result = closedir(proc_dir);
if (0 != close_result) {
- ERROR(UTIL_NAME ": failed to close %s directory, error: %d", procfs_path,
- errno);
- sfree((*proc_pids_array));
- return -1;
+ ERROR(UTIL_NAME ": failed to close /proc directory, error: %d", errno);
+ goto update_error;
}
return 0;
+
+update_error:
+ swap_proc_pids(proc_pids, proc_pids_num);
+ return -1;
}
-int pids_list_diff(pids_list_t *prev, pids_list_t *curr, pids_list_t **added,
- size_t *added_num, pids_list_t **removed,
- size_t *removed_num) {
- assert(prev || curr);
+int pids_list_diff(proc_pids_t *proc, pids_list_t *added,
+ pids_list_t *removed) {
+ assert(proc);
assert(added);
assert(removed);
- if (NULL == prev) {
+ added->size = 0;
+ removed->size = 0;
+
+ if (NULL == proc->prev || 0 == proc->prev->size) {
/* append all PIDs from curr to added*/
- return pids_list_add_pids_list(added, curr, added_num);
- } else if (NULL == curr) {
+ return pids_list_add_list(added, proc->curr);
+ } else if (NULL == proc->curr || 0 == proc->curr->size) {
/* append all PIDs from prev to removed*/
- return pids_list_add_pids_list(removed, prev, removed_num);
+ return pids_list_add_list(removed, proc->prev);
}
- pids_list_t *item = prev;
- while (item != NULL) {
- if (0 == pids_list_contains_pid(curr, item->pid)) {
- int add_result = pids_list_add_pid(removed, item->pid);
+ for (int i = 0; i < proc->prev->size; i++)
+ if (0 == pids_list_contains_pid(proc->curr, proc->prev->pids[i])) {
+ int add_result = pids_list_add_pid(removed, proc->prev->pids[i]);
if (add_result < 0)
return add_result;
- ++(*removed_num);
}
- item = item->next;
- }
- item = curr;
- while (item != NULL) {
- if (0 == pids_list_contains_pid(prev, item->pid)) {
- int add_result = pids_list_add_pid(added, item->pid);
+ for (int i = 0; i < proc->curr->size; i++)
+ if (0 == pids_list_contains_pid(proc->prev, proc->curr->pids[i])) {
+ int add_result = pids_list_add_pid(added, proc->curr->pids[i]);
if (add_result < 0)
return add_result;
- ++(*added_num);
}
- item = item->next;
+
+ return 0;
+}
+
+int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num) {
+ for (size_t i = 0; i < proc_pids_num; i++) {
+ if (NULL != proc_pids[i]->curr)
+ pids_list_free(proc_pids[i]->curr);
+ if (NULL != proc_pids[i]->prev)
+ pids_list_free(proc_pids[i]->prev);
+ sfree(proc_pids[i]);
}
+ sfree(proc_pids);
return 0;
}
/**
* collectd - src/utils_config_pids.h
*
- * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ * Copyright(c) 2018-2019 Intel Corporation. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* Authors:
* Starzyk, Mateusz <mateuszx.starzyk@intel.com>
* Wojciech Andralojc <wojciechx.andralojc@intel.com>
+ * Michał Aleksiński <michalx.aleksinski@intel.com>
**/
#include <dirent.h>
*/
typedef char proc_comm_t[MAX_PROC_NAME_LEN + 1];
-/* Linked one-way list of pids. */
+/* List of pids. */
typedef struct pids_list_s {
- pid_t pid;
- struct pids_list_s *next;
+ pid_t *pids;
+ size_t size;
+ size_t allocated;
} 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_comm_t process_name;
+ pids_list_t *prev;
+ pids_list_t *curr;
} proc_pids_t;
/*
* 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.
+ * Adds pid at the end of the pids array.
+ * Reallocates memory for new pid element, it is up to user to free it.
*
* PARAMETERS
- * `list' Head of target pids_list.
+ * `list' Target pids_list.
* `pid' Pid to be added.
*
* RETURN VALUE
* On success, returns 0.
* -1 on memory allocation error.
*/
-int pids_list_add_pid(pids_list_t **list, const pid_t pid);
+int pids_list_add_pid(pids_list_t *list, const pid_t pid);
/*
* NAME
- * pids_list_contains_pid
+ * pids_list_clear
*
* DESCRIPTION
- * Tests if pids list contains specific pid.
+ * Remove all pids from the list
*
* PARAMETERS
- * `list' Head of pids_list.
- * `pid' Pid to be searched for.
+ * `list' Target pids_list.
*
* RETURN VALUE
- * If PID found in list, returns 1,
- * Otherwise returns 0.
+ * On success, return 0
*/
-int pids_list_contains_pid(pids_list_t *list, const pid_t pid);
+int pids_list_clear(pids_list_t *list);
/*
* NAME
- * pids_list_add_pids_list
+ * pids_list_add_list
*
* DESCRIPTION
* Adds pids list at the end of the pids list.
* Allocates memory for new pid elements, it is up to user to free it.
- * Increases dst_num by a number of added PIDs.
*
* PARAMETERS
- * `dst' Head of target PIDs list.
- * `src' Head of source PIDs list.
- * `dst_num' Variable to be increased by a number of appended PIDs.
+ * `dst' Target PIDs list.
+ * `src' Source PIDs list.
*
* RETURN VALUE
* On success, returns 0.
* -1 on memory allocation error.
*/
-int pids_list_add_pids_list(pids_list_t **dst, pids_list_t *src,
- size_t *dst_num);
+#
+int pids_list_add_list(pids_list_t *dst, pids_list_t *src);
+
+/*
+ * NAME
+ * pids_list_contains_pid
+ *
+ * DESCRIPTION
+ * Tests if pids list contains specific pid.
+ *
+ * PARAMETERS
+ * `list' pids_list to check.
+ * `pid' Pid to be searched for.
+ *
+ * RETURN VALUE
+ * If PID found in list, returns 1,
+ * Otherwise returns 0.
+ */
+int pids_list_contains_pid(pids_list_t *list, const pid_t pid);
+
/*
* NAME
* read_proc_name
* Strips new-line character (\n).
*
* PARAMETERS
- * `procfs_path` Path to systems proc directory (e.g. /proc)
+ * `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)
/*
* NAME
- * pids_list_to_array
- *
- * DESCRIPTION
- * Copies element from list to array. Assumes the space for the array is
- * allocated.
- *
- * PARAMETERS
- * `array' First element of target array
- * `list' Head of the list
- * `array_length' Length (element count) of the target array
- */
-void pids_list_to_array(pid_t *array, pids_list_t *list,
- const size_t array_length);
-
-/*
- * NAME
* initialize_proc_pids
*
* DESCRIPTION
* `procs_names_array' Array of null-terminated strings with
* process' names to be copied to new array
* `procs_names_array_size' procs_names_array element count
- * `proc_pids_array' Address of pointer, under which new
+ * `proc_pids' Address of pointer, under which new
* array of proc_pids will be allocated.
* Must be NULL.
* RETURN VALUE
*/
int initialize_proc_pids(const char **procs_names_array,
const size_t procs_names_array_size,
- proc_pids_t **proc_pids_array);
+ proc_pids_t **proc_pids[]);
/*
* NAME
- * fetch_pids_for_procs
+ * update_proc_pids
*
* 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.
+ * Updates PIDs matching processes's names.
+ * Searches all PID directories in /proc fs and updates current pids_list.
*
* PARAMETERS
- * `procfs_path' Path to systems proc directory (e.g. /proc)
- * `procs_names_array' Array of null-terminated strings with
- * process' names to be copied to new array
- * `procs_names_array_size' procs_names_array element count
- * `proc_pids_array' Address of pointer, under which new
- * array of proc_pids will be allocated.
- * Must be NULL.
+ * `procfs_path' Path to systems proc directory (e.g. /proc)
+ * `proc_pids' Array of proc_pids pointers to be updated.
+ * `proc_pids_num' proc_pids element count
*
* RETURN VALUE
* 0 on success. -1 on error.
*/
-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);
+int update_proc_pids(const char *procfs_path, proc_pids_t *proc_pids[],
+ size_t proc_pids_num);
/*
* NAME
* Searches for differences in two given lists
*
* PARAMETERS
- * `prev' List of pids before changes
- * `curr' List of pids after changes
- * `added' Result array storing new pids which appeared in `curr'
- * `added_num' `added_num' array length
- * `removed' Result array storing pids which disappeared in `prev'
- * `removed_num' `removed' array length
+ * `proc' List of pids
+ * `added' New pids which appeared
+ * `removed' Result array storing pids which disappeared
* RETURN VALUE
* 0 on success. Negative number on error.
*/
-int pids_list_diff(pids_list_t *prev, pids_list_t *curr, pids_list_t **added,
- size_t *added_num, pids_list_t **removed,
- size_t *removed_num);
+int pids_list_diff(proc_pids_t *proc, pids_list_t *added, pids_list_t *removed);
+
+/*
+ * NAME
+ * proc_pids_free
+ *
+ * DESCRIPTION
+ * Releses memory allocatd for proc_pids
+ *
+ * PARAMETERS
+ * `proc_pids' Array of proc_pids
+ * `proc_pids_num' proc_pids element count
+ *
+ * RETURN VALUE
+ * 0 on success. -1 on error.
+ */
+int proc_pids_free(proc_pids_t *proc_pids[], size_t proc_pids_num);
* 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;
-}
-
typedef struct stub_proc_pid {
proc_comm_t comm;
pid_t pid;
/* setup */
const char *procs_names_array[] = {"proc1", "proc2", "proc3"};
const size_t procs_names_array_size = STATIC_ARRAY_SIZE(procs_names_array);
- proc_pids_t *proc_pids_array = NULL;
+ proc_pids_t **proc_pids_array = NULL;
/* check */
int result = initialize_proc_pids(procs_names_array, procs_names_array_size,
&proc_pids_array);
EXPECT_EQ_INT(0, result);
for (size_t i = 0; i < procs_names_array_size; ++i)
- EXPECT_EQ_STR(procs_names_array[i], proc_pids_array[i].proccess_name);
+ EXPECT_EQ_STR(procs_names_array[i], proc_pids_array[i]->process_name);
/* cleanup */
- free(proc_pids_array);
+ proc_pids_free(proc_pids_array, procs_names_array_size);
return 0;
}
-DEF_TEST(add_proc_pid__empty_list) {
+DEF_TEST(pid_list_add_pid__empty_list) {
/* setup */
- proc_pids_t proc_pids_instance;
- proc_pids_instance.pids = NULL;
+ pids_list_t *proc_pids_instance = calloc(1, sizeof(*proc_pids_instance));
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);
+ pids_list_add_pid(proc_pids_instance, pid);
+ EXPECT_EQ_INT(pid, proc_pids_instance->pids[0]);
/* cleanup */
- pids_list_free(proc_pids_instance.pids);
+ pids_list_free(proc_pids_instance);
return 0;
}
-DEF_TEST(add_proc_pid__non_empty_list) {
+DEF_TEST(pid_list_add_pid__non_empty_list) {
/* setup */
- proc_pids_t proc_pids_instance;
- proc_pids_instance.pids = NULL;
+ pids_list_t *proc_pids_instance = calloc(1, sizeof(*proc_pids_instance));
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]);
+ pids_list_add_pid(proc_pids_instance, 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);
+ EXPECT_EQ_INT(pids[i], proc_pids_instance->pids[i]);
}
/* cleanup */
- pids_list_free(proc_pids_instance.pids);
- return 0;
-}
-
-DEF_TEST(pids_list_to_array__non_empty_list) {
- /* setup */
- pid_t pids[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
- pids_list_t *pids_list = NULL;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i)
- pids_list_add_pid(&pids_list, pids[i]);
-
- /* check */
- pid_t target_array[STATIC_ARRAY_SIZE(pids)];
- pids_list_to_array(target_array, pids_list, STATIC_ARRAY_SIZE(target_array));
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids); ++i)
- EXPECT_EQ_INT(pids[i], target_array[i]);
-
- /* cleanup */
- pids_list_free(pids_list);
+ pids_list_free(proc_pids_instance);
return 0;
}
/* setup */
pid_t pids_array_1[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
pid_t pids_array_2[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
- pids_list_t *pids_list_1 = NULL;
- pids_list_t *pids_list_2 = NULL;
- size_t increase = 0;
+ pids_list_t *pids_list_1 = calloc(1, sizeof(*pids_list_1));
+ pids_list_t *pids_list_2 = calloc(1, sizeof(*pids_list_2));
for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) {
- pids_list_add_pid(&pids_list_1, pids_array_1[i]);
- pids_list_add_pid(&pids_list_2, pids_array_2[i]);
+ pids_list_add_pid(pids_list_1, pids_array_1[i]);
+ pids_list_add_pid(pids_list_2, pids_array_2[i]);
}
/* check */
- int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
+ int result = pids_list_add_list(pids_list_1, pids_list_2);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2), increase);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2) +
+ STATIC_ARRAY_SIZE(pids_array_1),
+ pids_list_1->size);
for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_1); ++i) {
EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_1[i]));
DEF_TEST(pids_list_add_pids_list__add_to_empty) {
/* setup */
pid_t pids_array[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
- pids_list_t *pids_list_1 = NULL;
- pids_list_t *pids_list_2 = NULL;
- size_t increase = 0;
+ pids_list_t *pids_list_1 = calloc(1, sizeof(*pids_list_1));
+ pids_list_t *pids_list_2 = calloc(1, sizeof(*pids_list_2));
for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i)
- pids_list_add_pid(&pids_list_2, pids_array[i]);
+ pids_list_add_pid(pids_list_2, pids_array[i]);
/* check */
- int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
+ int result = pids_list_add_list(pids_list_1, pids_list_2);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), increase);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), pids_list_1->size);
for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array); ++i)
EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array[i]));
return 0;
}
-DEF_TEST(fetch_pids_for_procs__one_proc_many_pid) {
+DEF_TEST(update_proc_pids__one_proc_many_pid) {
/* setup */
const char *proc_names[] = {"proc1"};
stub_proc_pid_t pp_stubs[] = {{"proc1", 1007},
{"proc1", 1009},
{"proc2", 1010},
{"proc3", 1011}};
+ proc_pids_t **proc_pids = NULL;
+ int result;
stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
- proc_pids_t *output = NULL;
+
+ result = initialize_proc_pids(proc_names, STATIC_ARRAY_SIZE(proc_names),
+ &proc_pids);
+ EXPECT_EQ_INT(0, result);
/* check */
- int result = fetch_pids_for_procs(proc_fs, proc_names,
- STATIC_ARRAY_SIZE(proc_names), &output);
+ result = update_proc_pids(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names));
EXPECT_EQ_INT(0, result);
/* proc name check */
- EXPECT_EQ_STR(proc_names[0], output[0].proccess_name);
+ EXPECT_EQ_STR(proc_names[0], proc_pids[0]->process_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_contains_pid(output[0].pids, pp_stubs[i].pid), 1);
+ EXPECT_EQ_INT(pids_list_contains_pid(proc_pids[0]->curr, pp_stubs[i].pid),
+ 1);
else
/* check if proc struct has no incorrect pids */
- EXPECT_EQ_INT(pids_list_contains_pid(output[0].pids, pp_stubs[i].pid), 0);
+ EXPECT_EQ_INT(pids_list_contains_pid(proc_pids[0]->curr, pp_stubs[i].pid),
+ 0);
}
/* cleanup */
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
- pids_list_free(output[i].pids);
- free(output);
+ proc_pids_free(proc_pids, STATIC_ARRAY_SIZE(proc_names));
stub_procfs_teardown();
return 0;
}
-DEF_TEST(fetch_pids_for_procs__many_proc_many_pid) {
+DEF_TEST(update_proc_pids__many_proc_many_pid) {
/* setup */
const char *proc_names[] = {"proc1", "proc2", "proc3"};
stub_proc_pid_t pp_stubs[] = {
{"proc2", 2008}, {"proc2", 2009}, {"proc3", 3007}, {"proc3", 3008},
{"proc3", 3009}, {"proc4", 4007}, {"proc4", 4008}, {"proc4", 4009},
{"proc5", 5007}, {"proc5", 5008}, {"proc5", 5009}};
+ proc_pids_t **proc_pids = NULL;
+ int result;
stub_procfs_setup(pp_stubs, STATIC_ARRAY_SIZE(pp_stubs));
- proc_pids_t *output = NULL;
+
+ result = initialize_proc_pids(proc_names, STATIC_ARRAY_SIZE(proc_names),
+ &proc_pids);
+ EXPECT_EQ_INT(0, result);
/* check */
- int result = fetch_pids_for_procs(proc_fs, proc_names,
- STATIC_ARRAY_SIZE(proc_names), &output);
+ result = update_proc_pids(proc_fs, proc_pids, STATIC_ARRAY_SIZE(proc_names));
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);
+ EXPECT_EQ_STR(proc_names[i], proc_pids[i]->process_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_contains_pid(output[i].pids, pp_stubs[j].pid),
- 1);
+ EXPECT_EQ_INT(
+ pids_list_contains_pid(proc_pids[i]->curr, pp_stubs[j].pid), 1);
else
/* check if proc struct has no incorrect pids */
- EXPECT_EQ_INT(pids_list_contains_pid(output[i].pids, pp_stubs[j].pid),
- 0);
+ EXPECT_EQ_INT(
+ pids_list_contains_pid(proc_pids[i]->curr, pp_stubs[j].pid), 0);
}
}
/* cleanup */
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
- pids_list_free(output[i].pids);
- free(output);
+ proc_pids_free(proc_pids, STATIC_ARRAY_SIZE(proc_names));
stub_procfs_teardown();
return 0;
}
/* setup */
pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
pid_t pids_array_after[] = {2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007};
- pids_list_t *pids_list_before = NULL;
- pids_list_t *pids_list_after = NULL;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i) {
- pids_list_add_pid(&pids_list_before, pids_array_before[i]);
- pids_list_add_pid(&pids_list_after, pids_array_after[i]);
- }
-
- pids_list_t *new_pids = NULL;
- size_t new_pids_count = 0;
- pids_list_t *lost_pids = NULL;
- size_t lost_pids_count = 0;
+ proc_pids_t proc_pids;
+ pids_list_t curr;
+ pids_list_t prev;
+
+ prev.pids = pids_array_before;
+ prev.size = STATIC_ARRAY_SIZE(pids_array_before);
+ prev.allocated = prev.size;
+ curr.pids = pids_array_after;
+ curr.size = STATIC_ARRAY_SIZE(pids_array_after);
+ curr.allocated = curr.size;
+ proc_pids.curr = &curr;
+ proc_pids.prev = &prev;
+
+ pids_list_t *new_pids = calloc(1, sizeof(*new_pids));
+ pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids));
/* check */
- int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
+ int result = pids_list_diff(&proc_pids, new_pids, lost_pids);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_before), lost_pids_count);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_after), new_pids_count);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_before), lost_pids->size);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_after), new_pids->size);
for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i) {
EXPECT_EQ_INT(1, pids_list_contains_pid(new_pids, pids_array_after[i]));
}
/* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
pids_list_free(new_pids);
pids_list_free(lost_pids);
DEF_TEST(pids_list_diff__nothing_changed) {
/* setup */
pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
- pids_list_t *pids_list_before = NULL;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i) {
- pids_list_add_pid(&pids_list_before, pids_array_before[i]);
- }
-
- pids_list_t *new_pids = NULL;
- size_t new_pids_count = 0;
- pids_list_t *lost_pids = NULL;
- size_t lost_pids_count = 0;
+ proc_pids_t proc_pids;
+ pids_list_t curr;
+ pids_list_t prev;
+
+ prev.pids = pids_array_before;
+ prev.size = STATIC_ARRAY_SIZE(pids_array_before);
+ prev.allocated = prev.size;
+ curr.pids = pids_array_before;
+ curr.size = STATIC_ARRAY_SIZE(pids_array_before);
+ curr.allocated = curr.size;
+ proc_pids.curr = &curr;
+ proc_pids.prev = &prev;
+
+ pids_list_t *new_pids = calloc(1, sizeof(*new_pids));
+ pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids));
/* check */
- int result = pids_list_diff(pids_list_before, pids_list_before, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
+ int result = pids_list_diff(&proc_pids, new_pids, lost_pids);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(0, lost_pids_count);
- EXPECT_EQ_INT(0, new_pids_count);
- OK(NULL == new_pids);
- OK(NULL == lost_pids);
+ EXPECT_EQ_INT(0, lost_pids->size);
+ EXPECT_EQ_INT(0, new_pids->size);
/* cleanup */
- pids_list_free(pids_list_before);
+ pids_list_free(lost_pids);
+ pids_list_free(new_pids);
return 0;
}
pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004,
1005, 1006, 1007, 1008};
- pids_list_t *pids_list_before = NULL;
- pids_list_t *pids_list_after = NULL;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i)
- pids_list_add_pid(&pids_list_before, pids_array_before[i]);
-
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i)
- pids_list_add_pid(&pids_list_after, pids_array_after[i]);
-
- pids_list_t *new_pids = NULL;
- size_t new_pids_count = 0;
- pids_list_t *lost_pids = NULL;
- size_t lost_pids_count = 0;
+ proc_pids_t proc_pids;
+ pids_list_t curr;
+ pids_list_t prev;
+
+ prev.pids = pids_array_before;
+ prev.size = STATIC_ARRAY_SIZE(pids_array_before);
+ prev.allocated = prev.size;
+ curr.pids = pids_array_after;
+ curr.size = STATIC_ARRAY_SIZE(pids_array_after);
+ curr.allocated = curr.size;
+ proc_pids.curr = &curr;
+ proc_pids.prev = &prev;
+
+ pids_list_t *new_pids = calloc(1, sizeof(*new_pids));
+ pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids));
/* check */
- int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
+ int result = pids_list_diff(&proc_pids, new_pids, lost_pids);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(0, lost_pids_count);
- EXPECT_EQ_INT(1, new_pids_count);
- EXPECT_EQ_INT(1008, new_pids->pid);
+ EXPECT_EQ_INT(0, lost_pids->size);
+ EXPECT_EQ_INT(1, new_pids->size);
+ EXPECT_EQ_INT(1008, new_pids->pids[0]);
/* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
+ pids_list_free(lost_pids);
pids_list_free(new_pids);
return 0;
pid_t pids_array_before[] = {1000, 1001, 1002, 1003, 1004,
1005, 1006, 1007, 1008};
pid_t pids_array_after[] = {1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007};
- pids_list_t *pids_list_before = NULL;
- pids_list_t *pids_list_after = NULL;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_before); ++i)
- pids_list_add_pid(&pids_list_before, pids_array_before[i]);
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(pids_array_after); ++i)
- pids_list_add_pid(&pids_list_after, pids_array_after[i]);
+ proc_pids_t proc_pids;
+ pids_list_t curr;
+ pids_list_t prev;
- pids_list_t *new_pids = NULL;
- size_t new_pids_count = 0;
- pids_list_t *lost_pids = NULL;
- size_t lost_pids_count = 0;
+ prev.pids = pids_array_before;
+ prev.size = STATIC_ARRAY_SIZE(pids_array_before);
+ prev.allocated = prev.size;
+ curr.pids = pids_array_after;
+ curr.size = STATIC_ARRAY_SIZE(pids_array_after);
+ curr.allocated = curr.size;
+ proc_pids.curr = &curr;
+ proc_pids.prev = &prev;
+
+ pids_list_t *new_pids = calloc(1, sizeof(*new_pids));
+ pids_list_t *lost_pids = calloc(1, sizeof(*lost_pids));
/* check */
- int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
+ int result = pids_list_diff(&proc_pids, new_pids, lost_pids);
EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(1, lost_pids_count);
- EXPECT_EQ_INT(0, new_pids_count);
- EXPECT_EQ_INT(1008, lost_pids->pid);
+ EXPECT_EQ_INT(0, new_pids->size);
+ EXPECT_EQ_INT(1, lost_pids->size);
+ EXPECT_EQ_INT(1008, lost_pids->pids[0]);
/* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
pids_list_free(lost_pids);
+ pids_list_free(new_pids);
return 0;
}
int main(void) {
stub_procfs_teardown();
RUN_TEST(initialize_proc_pids__on_nullptr);
- RUN_TEST(add_proc_pid__empty_list);
- RUN_TEST(add_proc_pid__non_empty_list);
- RUN_TEST(pids_list_to_array__non_empty_list);
+ RUN_TEST(pid_list_add_pid__empty_list);
+ RUN_TEST(pid_list_add_pid__non_empty_list);
RUN_TEST(pids_list_add_pids_list__non_empty_lists);
RUN_TEST(pids_list_add_pids_list__add_to_empty);
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);
+ RUN_TEST(update_proc_pids__one_proc_many_pid);
+ RUN_TEST(update_proc_pids__many_proc_many_pid);
RUN_TEST(pids_list_diff__all_changed);
RUN_TEST(pids_list_diff__nothing_changed);
RUN_TEST(pids_list_diff__one_added);