Extract pids config to seperate file.
Extract pids config tests to seperate file.
Change-Id: I2978db56b15a126eced12f40baac631517303e04
Signed-off-by: Mateusz Starzyk <mateuszx.starzyk@intel.com>
test_utils_time \
test_utils_vl_lookup \
test_libcollectd_network_parse \
- test_utils_config_cores
+ test_utils_config_cores \
+ test_utils_proc_pids
TESTS = $(check_PROGRAMS)
src/testing.h
test_utils_config_cores_LDADD = libplugin_mock.la
+test_utils_proc_pids_SOURCES = \
+ src/utils_proc_pids_test.c \
+ src/testing.h
+test_utils_proc_pids_LDADD = libplugin_mock.la
+
libavltree_la_SOURCES = \
src/utils/avltree/avltree.c \
src/utils/avltree/avltree.h
pkglib_LTLIBRARIES += intel_rdt.la
intel_rdt_la_SOURCES = \
src/intel_rdt.c \
+ src/utils_proc_pids.c \
+ src/utils_proc_pids.h \
src/utils/config_cores/config_cores.h \
src/utils/config_cores/config_cores.c
intel_rdt_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBPQOS_CPPFLAGS)
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/utils_proc_pids.c \
src/daemon/configfile.c \
src/daemon/types_list.c
test_plugin_intel_rdt_CPPFLAGS = $(AM_CPPFLAGS)
#include "collectd.h"
#include "utils/common/common.h"
#include "utils/config_cores/config_cores.h"
+#include "utils_proc_pids.h"
#include <pqos.h>
#define RDT_PLUGIN "intel_rdt"
* 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_NAME_LEN 16
#define RDT_MAX_NAMES_GROUPS 64
-
#define RDT_PROC_PATH "/proc"
#endif /* LIBPQOS2 */
} rdt_config_status;
#ifdef LIBPQOS2
-/* Helper typedef for process name array
- * Extra 1 char is added for string null termination.
- */
-typedef char proc_comm_t[RDT_MAX_NAME_LEN + 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;
-
struct rdt_name_group_s {
char *desc;
size_t num_names;
static int g_interface = -1;
+static void rdt_submit_derive(const char *cgroup, const char *type,
+ const char *type_instance, derive_t value) {
+ value_list_t vl = VALUE_LIST_INIT;
+
+ vl.values = &(value_t){.derive = value};
+ vl.values_len = 1;
+
+ sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
+ snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
+ sstrncpy(vl.type, type, sizeof(vl.type));
+ if (type_instance)
+ sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+
+ plugin_dispatch_values(&vl);
+}
+
+static void rdt_submit_gauge(const char *cgroup, const char *type,
+ const char *type_instance, gauge_t value) {
+ value_list_t vl = VALUE_LIST_INIT;
+
+ vl.values = &(value_t){.gauge = value};
+ vl.values_len = 1;
+
+ sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
+ snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
+ sstrncpy(vl.type, type, sizeof(vl.type));
+ if (type_instance)
+ sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+
+ plugin_dispatch_values(&vl);
+}
+
+#if COLLECT_DEBUG
+static void rdt_dump_cgroups(void) {
+ char cores[RDT_MAX_CORES * 4];
+
+ if (g_rdt == NULL)
+ return;
+
+ DEBUG(RDT_PLUGIN ": Core Groups Dump");
+ DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->cores.num_cgroups);
+
+ for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
+ core_group_t *cgroup = g_rdt->cores.cgroups + i;
+
+ memset(cores, 0, sizeof(cores));
+ for (size_t j = 0; j < cgroup->num_cores; j++) {
+ snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
+ cgroup->cores[j]);
+ }
+
+ DEBUG(RDT_PLUGIN ": group[%zu]:", i);
+ DEBUG(RDT_PLUGIN ": description: %s", cgroup->desc);
+ DEBUG(RDT_PLUGIN ": cores: %s", cores);
+ DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->events[i]);
+ }
+
+ return;
+}
+
+#ifdef LIBPQOS2
+static void rdt_dump_ngroups(void) {
+
+ char names[DATA_MAX_NAME_LEN];
+
+ if (g_rdt == NULL)
+ return;
+
+ DEBUG(RDT_PLUGIN ": Process Names Groups Dump");
+ DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_ngroups);
+
+ for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
+ memset(names, 0, sizeof(names));
+ for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
+ snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
+ g_rdt->ngroups[i].names[j]);
+
+ DEBUG(RDT_PLUGIN ": group[%d]:", (int)i);
+ DEBUG(RDT_PLUGIN ": description: %s", g_rdt->ngroups[i].desc);
+ DEBUG(RDT_PLUGIN ": process names:%s", names);
+ DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->ngroups[i].events);
+ }
+
+ return;
+}
+#endif /* LIBPQOS2 */
+
+static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
+
+static inline double bytes_to_mb(const double bytes) {
+ return bytes / (1024.0 * 1024.0);
+}
+
+static void rdt_dump_cores_data(void) {
+/*
+ * CORE - monitored group of cores
+ * RMID - Resource Monitoring ID associated with the monitored group
+ * This is not available for monitoring with resource control
+ * LLC - last level cache occupancy
+ * MBL - local memory bandwidth
+ * MBR - remote memory bandwidth
+ */
+#ifdef LIBPQOS2
+ if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
+ DEBUG(RDT_PLUGIN ": CORE LLC[KB] MBL[MB] MBR[MB]");
+ } else {
+ DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]");
+ }
+#else
+ DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]");
+#endif /* LIBPQOS2 */
+
+ for (int i = 0; i < g_rdt->cores.num_cgroups; i++) {
+ const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
+
+ double llc = bytes_to_kb(pv->llc);
+ double mbr = bytes_to_mb(pv->mbm_remote_delta);
+ double mbl = bytes_to_mb(pv->mbm_local_delta);
+#ifdef LIBPQOS2
+ if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
+ DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f",
+ g_rdt->cores.cgroups[i].desc, llc, mbl, mbr);
+ } else {
+ DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
+ g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
+ llc, mbl, mbr);
+ }
+#else
+ DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
+ g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
+ llc, mbl, mbr);
+#endif /* LIBPQOS2 */
+ }
+}
+
+#ifdef LIBPQOS2
+static void rdt_dump_pids_data(void) {
+ /*
+ * NAME - monitored group of processes
+ * PIDs - list of PID numbers in the NAME group
+ * LLC - last level cache occupancy
+ * MBL - local memory bandwidth
+ * MBR - remote memory bandwidth
+ */
+
+ DEBUG(RDT_PLUGIN ": NAME PIDs");
+ char pids[DATA_MAX_NAME_LEN];
+ 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) {
+ snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
+ list->pid);
+ list = list->next;
+ }
+ }
+ DEBUG(RDT_PLUGIN ": [%s] %s", g_rdt->ngroups[i].desc, pids);
+ }
+
+ DEBUG(RDT_PLUGIN ": NAME LLC[KB] MBL[MB] MBR[MB]");
+ for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
+
+ const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
+
+ double llc = bytes_to_kb(pv->llc);
+ double mbr = bytes_to_mb(pv->mbm_remote_delta);
+ double mbl = bytes_to_mb(pv->mbm_local_delta);
+
+ DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc,
+ llc, mbl, mbr);
+ }
+}
+#endif /* LIBPQOS2 */
+#endif /* COLLECT_DEBUG */
+
#ifdef LIBPQOS2
static int isdupstr(const char *names[], const size_t size, const char *name) {
for (size_t i = 0; i < size; i++)
- if (strncmp(names[i], name, (size_t)RDT_MAX_NAME_LEN) == 0)
+ if (strncmp(names[i], name, (size_t)MAX_PROC_NAME_LEN) == 0)
return 1;
return 0;
for (size_t i = 0; i < sz_a; i++) {
for (size_t j = 0; j < sz_b; j++)
- if (strncmp(tab_a[i], tab_b[j], (size_t)RDT_MAX_NAME_LEN) == 0)
+ if (strncmp(tab_a[i], tab_b[j], (size_t)MAX_PROC_NAME_LEN) == 0)
found++;
}
/* if no names are the same */
return index;
}
-#endif /* LIBPQOS2 */
-
-#if COLLECT_DEBUG
-static void rdt_dump_cgroups(void) {
- char cores[RDT_MAX_CORES * 4];
-
- if (g_rdt == NULL)
- return;
- DEBUG(RDT_PLUGIN ": Core Groups Dump");
- DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->cores.num_cgroups);
+static void rdt_free_ngroups(rdt_ctx_t *rdt) {
+ for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
+ if (rdt->ngroups[i].desc)
+ DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...",
+ rdt->ngroups[i].desc);
+ sfree(rdt->ngroups[i].desc);
+ strarray_free(rdt->ngroups[i].names, rdt->ngroups[i].num_names);
- for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
- core_group_t *cgroup = g_rdt->cores.cgroups + i;
+ 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);
+ }
- memset(cores, 0, sizeof(cores));
- for (size_t j = 0; j < cgroup->num_cores; j++) {
- snprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
- cgroup->cores[j]);
+ sfree(rdt->ngroups[i].proc_pids_array);
}
-
- DEBUG(RDT_PLUGIN ": group[%zu]:", i);
- DEBUG(RDT_PLUGIN ": description: %s", cgroup->desc);
- DEBUG(RDT_PLUGIN ": cores: %s", cores);
- DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->events[i]);
+ rdt->ngroups[i].num_names = 0;
+ sfree(rdt->pngroups[i]);
}
-
- return;
}
-#ifdef LIBPQOS2
-static void rdt_dump_ngroups(void) {
-
- char names[DATA_MAX_NAME_LEN];
-
- if (g_rdt == NULL)
- return;
-
- DEBUG(RDT_PLUGIN ": Process Names Groups Dump");
- DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_ngroups);
+static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
+ int n = 0;
+ enum pqos_mon_event events = 0;
- for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
- memset(names, 0, sizeof(names));
- for (size_t j = 0; j < g_rdt->ngroups[i].num_names; j++)
- snprintf(names + strlen(names), sizeof(names) - strlen(names) - 1, " %s",
- g_rdt->ngroups[i].names[j]);
+ if (item == NULL) {
+ DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
+ return -EINVAL;
+ }
- DEBUG(RDT_PLUGIN ": group[%d]:", (int)i);
- DEBUG(RDT_PLUGIN ": description: %s", g_rdt->ngroups[i].desc);
- DEBUG(RDT_PLUGIN ": process names:%s", names);
- DEBUG(RDT_PLUGIN ": events: 0x%X", g_rdt->ngroups[i].events);
+ DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
+ for (int j = 0; j < item->values_num; j++) {
+ if (item->values[j].type != OCONFIG_TYPE_STRING) {
+ ERROR(RDT_PLUGIN
+ ": given process names group value is not a string [idx=%d]",
+ j);
+ return -EINVAL;
+ }
+ DEBUG(RDT_PLUGIN ": [%d]: %s", j, item->values[j].value.string);
}
- return;
-}
-#endif /* LIBPQOS2 */
-
-static inline double bytes_to_kb(const double bytes) { return bytes / 1024.0; }
-
-static inline double bytes_to_mb(const double bytes) {
- return bytes / (1024.0 * 1024.0);
-}
-
-static void rdt_dump_cores_data(void) {
-/*
- * CORE - monitored group of cores
- * RMID - Resource Monitoring ID associated with the monitored group
- * This is not available for monitoring with resource control
- * LLC - last level cache occupancy
- * MBL - local memory bandwidth
- * MBR - remote memory bandwidth
- */
-#ifdef LIBPQOS2
- if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
- DEBUG(RDT_PLUGIN ": CORE LLC[KB] MBL[MB] MBR[MB]");
- } else {
- DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]");
- }
-#else
- DEBUG(RDT_PLUGIN ": CORE RMID LLC[KB] MBL[MB] MBR[MB]");
-#endif /* LIBPQOS2 */
-
- for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) {
- const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values;
-
- double llc = bytes_to_kb(pv->llc);
- double mbr = bytes_to_mb(pv->mbm_remote_delta);
- double mbl = bytes_to_mb(pv->mbm_local_delta);
-#ifdef LIBPQOS2
- if (g_interface == PQOS_INTER_OS_RESCTRL_MON) {
- DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f",
- g_rdt->cores.cgroups[i].desc, llc, mbl, mbr);
- } else {
- DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
- g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
- llc, mbl, mbr);
- }
-#else
- DEBUG(RDT_PLUGIN ": [%s] %8u %10.1f %10.1f %10.1f",
- g_rdt->cores.cgroups[i].desc, g_rdt->pcgroups[i]->poll_ctx[0].rmid,
- llc, mbl, mbr);
-#endif /* LIBPQOS2 */
- }
-}
-
-#ifdef LIBPQOS2
-static void rdt_dump_pids_data(void) {
- /*
- * NAME - monitored group of processes
- * PIDs - list of PID numbers in the NAME group
- * LLC - last level cache occupancy
- * MBL - local memory bandwidth
- * MBR - remote memory bandwidth
- */
-
- DEBUG(RDT_PLUGIN ": NAME PIDs");
- char pids[DATA_MAX_NAME_LEN];
- 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) {
- snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
- list->pid);
- list = list->next;
- }
- }
- DEBUG(RDT_PLUGIN ": [%s] %s", g_rdt->ngroups[i].desc, pids);
- }
-
- DEBUG(RDT_PLUGIN ": NAME LLC[KB] MBL[MB] MBR[MB]");
- for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
-
- const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
-
- double llc = bytes_to_kb(pv->llc);
- double mbr = bytes_to_mb(pv->mbm_remote_delta);
- double mbl = bytes_to_mb(pv->mbm_local_delta);
-
- DEBUG(RDT_PLUGIN ": [%s] %10.1f %10.1f %10.1f", g_rdt->ngroups[i].desc,
- llc, mbl, mbr);
- }
-}
-#endif /* LIBPQOS2 */
-#endif /* COLLECT_DEBUG */
-
-static void rdt_free_cgroups(void) {
- config_cores_cleanup(&g_rdt->cores);
- for (int i = 0; i < RDT_MAX_CORES; i++) {
- sfree(g_rdt->pcgroups[i]);
- }
-}
-
-#ifdef LIBPQOS2
-static int 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);
- }
- return 0;
-}
-
-static void rdt_free_ngroups(rdt_ctx_t *rdt) {
- for (int i = 0; i < RDT_MAX_NAMES_GROUPS; i++) {
- if (rdt->ngroups[i].desc)
- DEBUG(RDT_PLUGIN ": Freeing pids \'%s\' group\'s data...",
- 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);
- }
-
- sfree(rdt->ngroups[i].proc_pids_array);
- }
-
- rdt->ngroups[i].num_names = 0;
- sfree(rdt->pngroups[i]);
- }
-}
-#endif /* LIBPQOS2 */
-
-static int rdt_default_cgroups(void) {
- unsigned num_cores = g_rdt->pqos_cpu->num_cores;
-
- g_rdt->cores.cgroups = calloc(num_cores, sizeof(*g_rdt->cores.cgroups));
- if (g_rdt->cores.cgroups == NULL) {
- ERROR(RDT_PLUGIN ": Error allocating core groups array");
- return -ENOMEM;
- }
- g_rdt->cores.num_cgroups = num_cores;
-
- /* configure each core in separate group */
- for (unsigned i = 0; i < num_cores; i++) {
- core_group_t *cgroup = g_rdt->cores.cgroups + i;
- char desc[DATA_MAX_NAME_LEN];
-
- /* set core group info */
- cgroup->cores = calloc(1, sizeof(*cgroup->cores));
- if (cgroup->cores == NULL) {
- ERROR(RDT_PLUGIN ": Error allocating cores array");
- rdt_free_cgroups();
- return -ENOMEM;
- }
- cgroup->num_cores = 1;
- cgroup->cores[0] = i;
-
- snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
- cgroup->desc = strdup(desc);
- if (cgroup->desc == NULL) {
- ERROR(RDT_PLUGIN ": Error allocating core group description");
- rdt_free_cgroups();
- return -ENOMEM;
- }
- }
-
- return num_cores;
-}
-
-static int rdt_is_core_id_valid(unsigned int core_id) {
-
- for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
- if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
- return 1;
-
- return 0;
-}
-
-#ifdef LIBPQOS2
-static int rdt_is_proc_name_valid(const char *name) {
-
- if (name != NULL) {
- unsigned len = strlen(name);
- if (len > 0 && len <= RDT_MAX_NAME_LEN)
- return 1;
- else {
- DEBUG(RDT_PLUGIN
- ": Process name \'%s\' is too long. Max supported len is %d chars.",
- name, RDT_MAX_NAME_LEN);
- }
- }
-
- return 0;
-}
-#endif /* LIBPQOS2 */
-
-static int rdt_config_cgroups(oconfig_item_t *item) {
- size_t n = 0;
- enum pqos_mon_event events = 0;
-
- if (config_cores_parse(item, &g_rdt->cores) < 0) {
- rdt_free_cgroups();
- ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
+ n = oconfig_to_ngroups(item, rdt->ngroups, RDT_MAX_NAMES_GROUPS);
+ if (n < 0) {
+ rdt_free_ngroups(rdt);
+ ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
return -EINVAL;
}
- n = g_rdt->cores.num_cgroups;
-
- /* validate configured core id values */
- for (size_t group_idx = 0; group_idx < n; group_idx++) {
- core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
- for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
- if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
- ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
- cgroup->desc, cgroup->cores[core_idx]);
- rdt_free_cgroups();
- return -EINVAL;
- }
- }
- }
-
- if (n == 0) {
- /* create default core groups if "Cores" config option is empty */
- int ret = rdt_default_cgroups();
- if (ret < 0) {
- rdt_free_cgroups();
- ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
- return ret;
- }
- n = (size_t)ret;
- INFO(RDT_PLUGIN
- ": No core groups configured. Default core groups created.");
- }
-
- /* Get all available events on this platform */
- for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
- events |= g_rdt->cap_mon->u.mon->events[i].type;
-
- events &= ~(PQOS_PERF_EVENT_LLC_MISS);
-
- DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
- g_rdt->pqos_cpu->num_cores);
- DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
-
- g_rdt->cores.num_cgroups = n;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < i; j++) {
- int found = 0;
- found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
- &g_rdt->cores.cgroups[i]);
- if (found != 0) {
- rdt_free_cgroups();
- ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
- return -EINVAL;
- }
- }
-
- g_rdt->events[i] = events;
- g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
- if (g_rdt->pcgroups[i] == NULL) {
- rdt_free_cgroups();
- ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
- return -ENOMEM;
- }
- }
-
- return 0;
-}
-
-#ifdef LIBPQOS2
-static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
- int n = 0;
- enum pqos_mon_event events = 0;
-
- if (item == NULL) {
- DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument.");
- return -EINVAL;
- }
-
- DEBUG(RDT_PLUGIN ": Process names groups [%d]:", item->values_num);
- for (int j = 0; j < item->values_num; j++) {
- if (item->values[j].type != OCONFIG_TYPE_STRING) {
- ERROR(RDT_PLUGIN
- ": given process names group value is not a string [idx=%d]",
- j);
- return -EINVAL;
- }
- DEBUG(RDT_PLUGIN ": [%d]: %s", j, item->values[j].value.string);
- }
-
- n = oconfig_to_ngroups(item, rdt->ngroups, RDT_MAX_NAMES_GROUPS);
- if (n < 0) {
- rdt_free_ngroups(rdt);
- ERROR(RDT_PLUGIN ": Error parsing process name groups configuration.");
- return -EINVAL;
- }
-
- /* validate configured process name values */
- for (int group_idx = 0; group_idx < n; group_idx++) {
- DEBUG(RDT_PLUGIN ": checking group [%d]: %s", group_idx,
- rdt->ngroups[group_idx].desc);
- for (size_t name_idx = 0; name_idx < rdt->ngroups[group_idx].num_names;
- name_idx++) {
- DEBUG(RDT_PLUGIN ": checking process name [%zu]: %s", name_idx,
- rdt->ngroups[group_idx].names[name_idx]);
- if (!rdt_is_proc_name_valid(rdt->ngroups[group_idx].names[name_idx])) {
- ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
- rdt->ngroups[group_idx].desc,
- rdt->ngroups[group_idx].names[name_idx]);
- rdt_free_ngroups(rdt);
- return -EINVAL;
- }
- }
- }
-
- if (n == 0) {
- ERROR(RDT_PLUGIN ": Empty process name groups configured.");
- return -EINVAL;
- }
-
- /* Get all available events on this platform */
- for (unsigned i = 0; i < rdt->cap_mon->u.mon->num_events; i++)
- events |= rdt->cap_mon->u.mon->events[i].type;
-
- events &= ~(PQOS_PERF_EVENT_LLC_MISS);
-
- DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
-
- rdt->num_ngroups = n;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < i; j++) {
- int found = ngroup_cmp(&rdt->ngroups[j], &rdt->ngroups[i]);
- if (found != 0) {
- rdt_free_ngroups(rdt);
- ERROR(RDT_PLUGIN
- ": Cannot monitor same process name in different groups.");
- return -EINVAL;
- }
- }
-
- rdt->ngroups[i].events = events;
- rdt->pngroups[i] = calloc(1, sizeof(*rdt->pngroups[i]));
- if (rdt->pngroups[i] == NULL) {
- rdt_free_ngroups(rdt);
- ERROR(RDT_PLUGIN
- ": Failed to allocate memory for process name monitoring data.");
- return -ENOMEM;
- }
- }
-
- return 0;
-}
-
-/*
- * 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) {
- assert(list);
-
- 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
- * pids_list_contains_pid
- *
- * DESCRIPTION
- * Tests if pids list contains specific pid.
- *
- * PARAMETERS
- * `list' Head of pids_list.
- * `pid' Pid to be searched for.
- *
- * RETURN VALUE
- * If PID found in list, returns 1,
- * Otherwise returns 0.
- */
-static int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
- assert(list);
-
- pids_list_t *current = list;
- while (current != NULL) {
- if (current->pid == pid)
- return 1;
- current = current->next;
- }
- return 0;
-}
-
-/*
- * NAME
- * pids_list_add_pids_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.
- *
- * RETURN VALUE
- * On success, returns 0.
- * -1 on memory allocation error.
- */
-static int pids_list_add_pids_list(pids_list_t **dst, pids_list_t *src,
- size_t *dst_num) {
- assert(dst);
- assert(src);
- assert(dst_num);
-
- pids_list_t *current = src;
- int ret;
-
- while (current != NULL) {
- ret = pids_list_add_pid(dst, current->pid);
- if (0 != ret)
- return ret;
-
- ++(*dst_num);
- current = current->next;
- }
-
- 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);
- if (path == NULL)
- return -1;
- 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);
- name[out_size - 1] = '\0';
- 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. -1 on 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 -1; /* conversion failed, not proc-pid */
- }
- /* all checks passed, marking as success */
- return 0;
-}
-
-/*
- * 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
- */
-static 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;
- }
-}
-
-/*
- * NAME
- * initialize_proc_pids
- *
- * DESCRIPTION
- * Helper function to properly initialize array of proc_pids.
- * Allocates memory for proc_pids structs.
- *
- * PARAMETERS
- * `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.
- * RETURN VALUE
- * 0 on success. Negative number on error:
- * -1: allocation error
- */
-static int initialize_proc_pids(const char **procs_names_array,
- const size_t procs_names_array_size,
- proc_pids_t **proc_pids_array) {
-
- assert(proc_pids_array);
- assert(NULL == *proc_pids_array);
-
- /* Copy procs names to output array. Initialize pids list with NULL value. */
- *proc_pids_array = calloc(procs_names_array_size, sizeof(**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;
- }
-
- 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_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.
- *
- * RETURN VALUE
- * 0 on success. -1 on error.
- */
-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);
-
- 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;
- }
-
- int init_result = initialize_proc_pids(
- procs_names_array, procs_names_array_size, proc_pids_array);
- if (0 != init_result)
- return -1;
-
- /* 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);
- sfree(*proc_pids_array);
- return -1;
- }
- return 0;
-}
-#endif /* LIBPQOS2 */
-
-static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
- DEBUG(RDT_PLUGIN ": %s", msg);
-}
-
-static int rdt_preinit(void) {
- int ret;
-
- if (g_rdt != NULL) {
- /* already initialized if config callback was called before init callback */
- return 0;
- }
-
- g_rdt = calloc(1, sizeof(*g_rdt));
- if (g_rdt == NULL) {
- ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
- return -ENOMEM;
- }
-
- struct pqos_config pqos = {.fd_log = -1,
- .callback_log = rdt_pqos_log,
- .context_log = NULL,
- .verbose = 0,
-#ifdef LIBPQOS2
- .interface = PQOS_INTER_OS_RESCTRL_MON};
- DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface");
-#else
- .interface = PQOS_INTER_MSR};
- DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
-#endif
-
- ret = pqos_init(&pqos);
- DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
-
-#ifdef LIBPQOS2
- if (ret == PQOS_RETVAL_INTER) {
- pqos.interface = PQOS_INTER_MSR;
- DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
- ret = pqos_init(&pqos);
- DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
- }
-#endif
-
- if (ret != PQOS_RETVAL_OK) {
- ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
- goto rdt_preinit_error1;
- }
-
- g_interface = pqos.interface;
-
- ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
- if (ret != PQOS_RETVAL_OK) {
- ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
- goto rdt_preinit_error2;
- }
-
- ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
- if (ret == PQOS_RETVAL_PARAM) {
- ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
- goto rdt_preinit_error2;
- }
-
- if (g_rdt->cap_mon == NULL) {
- ERROR(
- RDT_PLUGIN
- ": Monitoring capability not detected. Nothing to do for the plugin.");
- goto rdt_preinit_error2;
- }
-
- /* Reset pqos monitoring groups registers */
- pqos_mon_reset();
-
- return 0;
-
-rdt_preinit_error2:
- pqos_fini();
-
-rdt_preinit_error1:
- sfree(g_rdt);
-
- return -1;
-}
-
-static int rdt_config(oconfig_item_t *ci) {
- if (rdt_preinit() != 0) {
- g_state = CONFIGURATION_ERROR;
- /* if we return -1 at this point collectd
- reports a failure in configuration and
- aborts
- */
- return (0);
- }
-
- for (int i = 0; i < ci->children_num; i++) {
- oconfig_item_t *child = ci->children + i;
-
- if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
- if (rdt_config_cgroups(child) != 0) {
- g_state = CONFIGURATION_ERROR;
- /* if we return -1 at this point collectd
- reports a failure in configuration and
- aborts
- */
- return (0);
- }
-
-#if COLLECT_DEBUG
- rdt_dump_cgroups();
-#endif /* COLLECT_DEBUG */
- } else if (strncasecmp("Processes", child->key,
- (size_t)strlen("Processes")) == 0) {
-#ifdef LIBPQOS2
- if (g_interface != PQOS_INTER_OS_RESCTRL_MON) {
- ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. "
- "Resctrl monitoring is needed for PIDs monitoring.",
- child->key);
- g_state = CONFIGURATION_ERROR;
- /* if we return -1 at this point collectd
- reports a failure in configuration and
- aborts
- */
- return 0;
- }
-
- if (rdt_config_ngroups(g_rdt, child) != 0) {
- g_state = CONFIGURATION_ERROR;
- /* if we return -1 at this point collectd
- reports a failure in configuration and
- aborts
- */
- return 0;
- }
-
-#if COLLECT_DEBUG
- rdt_dump_ngroups();
-#endif /* COLLECT_DEBUG */
-#else /* !LIBPQOS2 */
- ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
- "recompile collectd with libpqos version 2.0 or newer.",
- child->key);
-#endif /* LIBPQOS2 */
- } else {
- ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
- }
- }
-
- return 0;
-}
-
-static void rdt_submit_derive(const char *cgroup, const char *type,
- const char *type_instance, derive_t value) {
- value_list_t vl = VALUE_LIST_INIT;
-
- vl.values = &(value_t){.derive = value};
- vl.values_len = 1;
-
- sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
- snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
- sstrncpy(vl.type, type, sizeof(vl.type));
- if (type_instance)
- sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
-
- plugin_dispatch_values(&vl);
-}
-
-static void rdt_submit_gauge(const char *cgroup, const char *type,
- const char *type_instance, gauge_t value) {
- value_list_t vl = VALUE_LIST_INIT;
- vl.values = &(value_t){.gauge = value};
- vl.values_len = 1;
+ /* validate configured process name values */
+ for (int group_idx = 0; group_idx < n; group_idx++) {
+ DEBUG(RDT_PLUGIN ": checking group [%d]: %s", group_idx,
+ rdt->ngroups[group_idx].desc);
+ for (size_t name_idx = 0; name_idx < rdt->ngroups[group_idx].num_names;
+ name_idx++) {
+ DEBUG(RDT_PLUGIN ": checking process name [%zu]: %s", name_idx,
+ rdt->ngroups[group_idx].names[name_idx]);
+ if (!is_proc_name_valid(rdt->ngroups[group_idx].names[name_idx])) {
+ ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
+ rdt->ngroups[group_idx].desc,
+ rdt->ngroups[group_idx].names[name_idx]);
+ rdt_free_ngroups(rdt);
+ return -EINVAL;
+ }
+ }
+ }
- sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
- snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
- sstrncpy(vl.type, type, sizeof(vl.type));
- if (type_instance)
- sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
+ if (n == 0) {
+ ERROR(RDT_PLUGIN ": Empty process name groups configured.");
+ return -EINVAL;
+ }
- plugin_dispatch_values(&vl);
-}
+ /* Get all available events on this platform */
+ for (unsigned i = 0; i < rdt->cap_mon->u.mon->num_events; i++)
+ events |= rdt->cap_mon->u.mon->events[i].type;
-#ifdef LIBPQOS2
-static int rdt_pid_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);
- assert(added);
- assert(removed);
-
- if (NULL == prev) {
- /* append all PIDs from curr to added*/
- return pids_list_add_pids_list(added, curr, added_num);
- } else if (NULL == curr) {
- /* append all PIDs from prev to removed*/
- return pids_list_add_pids_list(removed, prev, removed_num);
- }
+ events &= ~(PQOS_PERF_EVENT_LLC_MISS);
+
+ DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
- pids_list_t *item = prev;
- while (item != NULL) {
- if (0 == pids_list_contains_pid(curr, item->pid)) {
- pids_list_add_pid(removed, item->pid);
- ++(*removed_num);
+ rdt->num_ngroups = n;
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < i; j++) {
+ int found = ngroup_cmp(&rdt->ngroups[j], &rdt->ngroups[i]);
+ if (found != 0) {
+ rdt_free_ngroups(rdt);
+ ERROR(RDT_PLUGIN
+ ": Cannot monitor same process name in different groups.");
+ return -EINVAL;
+ }
}
- item = item->next;
- }
- item = curr;
- while (item != NULL) {
- if (0 == pids_list_contains_pid(prev, item->pid)) {
- pids_list_add_pid(added, item->pid);
- ++(*added_num);
+ rdt->ngroups[i].events = events;
+ rdt->pngroups[i] = calloc(1, sizeof(*rdt->pngroups[i]));
+ if (rdt->pngroups[i] == NULL) {
+ rdt_free_ngroups(rdt);
+ ERROR(RDT_PLUGIN
+ ": Failed to allocate memory for process name monitoring data.");
+ return -ENOMEM;
}
- item = item->next;
}
return 0;
if (NULL == proc_pids_array_prev[i].pids &&
NULL == proc_pids_array_curr[i].pids)
continue;
- int diff_result = rdt_pid_list_diff(
+ 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);
if (0 != diff_result) {
if (proc_pids_array_curr[i].pids)
pids_list_free(proc_pids_array_curr[i].pids);
- sfree(proc_pids_array_curr);
+ 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);
+
+ return result;
+}
+
+static int read_pids_data() {
+
+ if (0 == g_rdt->num_ngroups) {
+ DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped");
+ return 0;
+ }
+
+ DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups");
+ struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0};
+ size_t active_group_idx = 0;
+ for (size_t pngroups_idx = 0;
+ pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx)
+ if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count)
+ active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx];
+
+ int ret = 0;
+
+ if (0 == active_group_idx) {
+ DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped");
+ goto groups_refresh;
+ }
+
+ DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling");
+
+ int poll_result = pqos_mon_poll(active_groups, active_group_idx);
+ if (poll_result != PQOS_RETVAL_OK) {
+ ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for "
+ "pids. Error [%d].",
+ poll_result);
+ ret = -poll_result;
+ goto groups_refresh;
+ }
+
+ for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
+ enum pqos_mon_event mbm_events =
+ (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
+ PQOS_MON_EVENT_RMEM_BW);
+
+ if (g_rdt->pngroups[i] == NULL ||
+ g_rdt->ngroups[i].monitored_pids_count == 0)
+ continue;
+
+ const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
+
+ /* Submit only monitored events data */
+
+ if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
+ rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc);
+
+ if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC)
+ rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc);
+
+ if (g_rdt->ngroups[i].events & mbm_events) {
+ rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local",
+ pv->mbm_local_delta);
+ rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote",
+ pv->mbm_remote_delta);
+ }
+ }
+
+#if COLLECT_DEBUG
+ rdt_dump_pids_data();
+#endif /* COLLECT_DEBUG */
+
+groups_refresh:
+ 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]);
+
+ if (0 != refresh_result) {
+ ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d",
+ i, refresh_result);
+ if (0 == ret) {
+ /* refresh error will be escalated only if there were no
+ * errors before.
+ */
+ ret = refresh_result;
+ }
+ }
+ }
+
+ assert(ret <= 0);
+ return ret;
+}
+
+static void rdt_init_pids_monitoring() {
+ for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
+ /*
+ * Each group must have not-null proc_pids array.
+ * Initial refresh is not mandatory for proper
+ * PIDs statistics detection.
+ */
+ 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);
+ if (0 != init_result) {
+ ERROR(RDT_PLUGIN
+ ": Initialization of proc_pids for group %zu failed. Error: %d",
+ group_idx, init_result);
+ continue;
+ }
+
+ int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
+ g_rdt->pngroups[group_idx]);
+ if (0 != refresh_result)
+ ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d",
+ group_idx, refresh_result);
+ }
+}
+#endif /* LIBPQOS2 */
+
+static void rdt_free_cgroups(void) {
+ config_cores_cleanup(&g_rdt->cores);
+ for (int i = 0; i < RDT_MAX_CORES; i++) {
+ sfree(g_rdt->pcgroups[i]);
+ }
+}
+
+static int rdt_default_cgroups(void) {
+ unsigned num_cores = g_rdt->pqos_cpu->num_cores;
+
+ g_rdt->cores.cgroups = calloc(num_cores, sizeof(*(g_rdt->cores.cgroups)));
+ if (g_rdt->cores.cgroups == NULL) {
+ ERROR(RDT_PLUGIN ": Error allocating core groups array");
+ return -ENOMEM;
+ }
+ g_rdt->cores.num_cgroups = num_cores;
+
+ /* configure each core in separate group */
+ for (unsigned i = 0; i < num_cores; i++) {
+ core_group_t *cgroup = g_rdt->cores.cgroups + i;
+ char desc[DATA_MAX_NAME_LEN];
+
+ /* set core group info */
+ cgroup->cores = calloc(1, sizeof(*cgroup->cores));
+ if (cgroup->cores == NULL) {
+ ERROR(RDT_PLUGIN ": Error allocating cores array");
+ rdt_free_cgroups();
+ return -ENOMEM;
+ }
+ cgroup->num_cores = 1;
+ cgroup->cores[0] = i;
+
+ snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
+ cgroup->desc = strdup(desc);
+ if (cgroup->desc == NULL) {
+ ERROR(RDT_PLUGIN ": Error allocating core group description");
+ rdt_free_cgroups();
+ return -ENOMEM;
+ }
+ }
+
+ return num_cores;
+}
+
+static int rdt_is_core_id_valid(unsigned int core_id) {
+
+ for (unsigned int i = 0; i < g_rdt->pqos_cpu->num_cores; i++)
+ if (core_id == g_rdt->pqos_cpu->cores[i].lcore)
+ return 1;
+
+ return 0;
+}
+
+static int rdt_config_cgroups(oconfig_item_t *item) {
+ size_t n = 0;
+ enum pqos_mon_event events = 0;
+
+ if (config_cores_parse(item, &g_rdt->cores) < 0) {
+ rdt_free_cgroups();
+ ERROR(RDT_PLUGIN ": Error parsing core groups configuration.");
+ return -EINVAL;
+ }
+ n = g_rdt->cores.num_cgroups;
+
+ /* validate configured core id values */
+ for (size_t group_idx = 0; group_idx < n; group_idx++) {
+ core_group_t *cgroup = g_rdt->cores.cgroups + group_idx;
+ for (size_t core_idx = 0; core_idx < cgroup->num_cores; core_idx++) {
+ if (!rdt_is_core_id_valid(cgroup->cores[core_idx])) {
+ ERROR(RDT_PLUGIN ": Core group '%s' contains invalid core id '%u'",
+ cgroup->desc, cgroup->cores[core_idx]);
+ rdt_free_cgroups();
+ return -EINVAL;
+ }
+ }
+ }
+
+ if (n == 0) {
+ /* create default core groups if "Cores" config option is empty */
+ int ret = rdt_default_cgroups();
+ if (ret < 0) {
+ rdt_free_cgroups();
+ ERROR(RDT_PLUGIN ": Error creating default core groups configuration.");
+ return ret;
+ }
+ n = (size_t)ret;
+ INFO(RDT_PLUGIN
+ ": No core groups configured. Default core groups created.");
}
- if (new_pids)
- pids_list_free(new_pids);
+ /* Get all available events on this platform */
+ for (unsigned int i = 0; i < g_rdt->cap_mon->u.mon->num_events; i++)
+ events |= g_rdt->cap_mon->u.mon->events[i].type;
- if (new_pids_array)
- free(new_pids_array);
+ events &= ~(PQOS_PERF_EVENT_LLC_MISS);
- if (lost_pids)
- pids_list_free(lost_pids);
+ DEBUG(RDT_PLUGIN ": Number of cores in the system: %u",
+ g_rdt->pqos_cpu->num_cores);
+ DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events);
- if (lost_pids_array)
- free(lost_pids_array);
+ g_rdt->cores.num_cgroups = n;
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < i; j++) {
+ int found = 0;
+ found = config_cores_cmp_cgroups(&g_rdt->cores.cgroups[j],
+ &g_rdt->cores.cgroups[i]);
+ if (found != 0) {
+ rdt_free_cgroups();
+ ERROR(RDT_PLUGIN ": Cannot monitor same cores in different groups.");
+ return -EINVAL;
+ }
+ }
- return result;
+ g_rdt->events[i] = events;
+ g_rdt->pcgroups[i] = calloc(1, sizeof(*g_rdt->pcgroups[i]));
+ if (g_rdt->pcgroups[i] == NULL) {
+ rdt_free_cgroups();
+ ERROR(RDT_PLUGIN ": Failed to allocate memory for monitoring data.");
+ return -ENOMEM;
+ }
+ }
+
+ return 0;
}
-static int read_pids_data() {
+static void rdt_pqos_log(void *context, const size_t size, const char *msg) {
+ DEBUG(RDT_PLUGIN ": %s", msg);
+}
- if (0 == g_rdt->num_ngroups) {
- DEBUG(RDT_PLUGIN ": read_pids_data: not configured - PIDs read skipped");
+static int rdt_preinit(void) {
+ int ret;
+
+ if (g_rdt != NULL) {
+ /* already initialized if config callback was called before init callback */
return 0;
}
- DEBUG(RDT_PLUGIN ": read_pids_data: Scanning active groups");
- struct pqos_mon_data *active_groups[RDT_MAX_NAMES_GROUPS] = {0};
- size_t active_group_idx = 0;
- for (size_t pngroups_idx = 0;
- pngroups_idx < STATIC_ARRAY_SIZE(g_rdt->pngroups); ++pngroups_idx)
- if (0 != g_rdt->ngroups[pngroups_idx].monitored_pids_count)
- active_groups[active_group_idx++] = g_rdt->pngroups[pngroups_idx];
+ g_rdt = calloc(1, sizeof(*g_rdt));
+ if (g_rdt == NULL) {
+ ERROR(RDT_PLUGIN ": Failed to allocate memory for rdt context.");
+ return -ENOMEM;
+ }
- int ret = 0;
+ struct pqos_config pqos = {.fd_log = -1,
+ .callback_log = rdt_pqos_log,
+ .context_log = NULL,
+ .verbose = 0,
+#ifdef LIBPQOS2
+ .interface = PQOS_INTER_OS_RESCTRL_MON};
+ DEBUG(RDT_PLUGIN ": Initializing PQoS with RESCTRL interface");
+#else
+ .interface = PQOS_INTER_MSR};
+ DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
+#endif
- if (0 == active_group_idx) {
- DEBUG(RDT_PLUGIN ": read_pids_data: no active groups - PIDs read skipped");
- goto groups_refresh;
+ ret = pqos_init(&pqos);
+ DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
+
+#ifdef LIBPQOS2
+ if (ret == PQOS_RETVAL_INTER) {
+ pqos.interface = PQOS_INTER_MSR;
+ DEBUG(RDT_PLUGIN ": Initializing PQoS with MSR interface");
+ ret = pqos_init(&pqos);
+ DEBUG(RDT_PLUGIN ": PQoS initialization result: [%d]", ret);
}
+#endif
- DEBUG(RDT_PLUGIN ": read_pids_data: PIDs data polling");
+ if (ret != PQOS_RETVAL_OK) {
+ ERROR(RDT_PLUGIN ": Error initializing PQoS library!");
+ goto rdt_preinit_error1;
+ }
- int poll_result = pqos_mon_poll(active_groups, active_group_idx);
- if (poll_result != PQOS_RETVAL_OK) {
- ERROR(RDT_PLUGIN ": read_pids_data: Failed to poll monitoring data for "
- "pids. Error [%d].",
- poll_result);
- ret = -poll_result;
- goto groups_refresh;
+ g_interface = pqos.interface;
+
+ ret = pqos_cap_get(&g_rdt->pqos_cap, &g_rdt->pqos_cpu);
+ if (ret != PQOS_RETVAL_OK) {
+ ERROR(RDT_PLUGIN ": Error retrieving PQoS capabilities.");
+ goto rdt_preinit_error2;
}
- for (size_t i = 0; i < g_rdt->num_ngroups; i++) {
- enum pqos_mon_event mbm_events =
- (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW |
- PQOS_MON_EVENT_RMEM_BW);
+ ret = pqos_cap_get_type(g_rdt->pqos_cap, PQOS_CAP_TYPE_MON, &g_rdt->cap_mon);
+ if (ret == PQOS_RETVAL_PARAM) {
+ ERROR(RDT_PLUGIN ": Error retrieving monitoring capabilities.");
+ goto rdt_preinit_error2;
+ }
- if (g_rdt->pngroups[i] == NULL ||
- g_rdt->ngroups[i].monitored_pids_count == 0)
- continue;
+ if (g_rdt->cap_mon == NULL) {
+ ERROR(
+ RDT_PLUGIN
+ ": Monitoring capability not detected. Nothing to do for the plugin.");
+ goto rdt_preinit_error2;
+ }
- const struct pqos_event_values *pv = &g_rdt->pngroups[i]->values;
+ /* Reset pqos monitoring groups registers */
+ pqos_mon_reset();
- /* Submit only monitored events data */
+ return 0;
- if (g_rdt->ngroups[i].events & PQOS_MON_EVENT_L3_OCCUP)
- rdt_submit_gauge(g_rdt->ngroups[i].desc, "bytes", "llc", pv->llc);
+rdt_preinit_error2:
+ pqos_fini();
- if (g_rdt->ngroups[i].events & PQOS_PERF_EVENT_IPC)
- rdt_submit_gauge(g_rdt->ngroups[i].desc, "ipc", NULL, pv->ipc);
+rdt_preinit_error1:
+ sfree(g_rdt);
- if (g_rdt->ngroups[i].events & mbm_events) {
- rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "local",
- pv->mbm_local_delta);
- rdt_submit_derive(g_rdt->ngroups[i].desc, "memory_bandwidth", "remote",
- pv->mbm_remote_delta);
- }
+ return -1;
+}
+
+static int rdt_config(oconfig_item_t *ci) {
+ if (rdt_preinit() != 0) {
+ g_state = CONFIGURATION_ERROR;
+ /* if we return -1 at this point collectd
+ reports a failure in configuration and
+ aborts
+ */
+ return (0);
}
-#if COLLECT_DEBUG
- rdt_dump_pids_data();
-#endif /* COLLECT_DEBUG */
+ for (int i = 0; i < ci->children_num; i++) {
+ oconfig_item_t *child = ci->children + i;
-groups_refresh:
- 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]);
+ if (strncasecmp("Cores", child->key, (size_t)strlen("Cores")) == 0) {
+ if (rdt_config_cgroups(child) != 0) {
+ g_state = CONFIGURATION_ERROR;
+ /* if we return -1 at this point collectd
+ reports a failure in configuration and
+ aborts
+ */
+ return (0);
+ }
- if (0 != refresh_result) {
- ERROR(RDT_PLUGIN ": read_pids_data: NGroup %zu refresh failed. Error: %d",
- i, refresh_result);
- if (0 == ret) {
- /* refresh error will be escalated only if there were no
- * errors before.
+#if COLLECT_DEBUG
+ rdt_dump_cgroups();
+#endif /* COLLECT_DEBUG */
+ } else if (strncasecmp("Processes", child->key,
+ (size_t)strlen("Processes")) == 0) {
+#ifdef LIBPQOS2
+ if (g_interface != PQOS_INTER_OS_RESCTRL_MON) {
+ ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported. "
+ "Resctrl monitoring is needed for PIDs monitoring.",
+ child->key);
+ g_state = CONFIGURATION_ERROR;
+ /* if we return -1 at this point collectd
+ reports a failure in configuration and
+ aborts
*/
- ret = refresh_result;
+ return 0;
}
- }
- }
- assert(ret <= 0);
- return ret;
-}
+ if (rdt_config_ngroups(g_rdt, child) != 0) {
+ g_state = CONFIGURATION_ERROR;
+ /* if we return -1 at this point collectd
+ reports a failure in configuration and
+ aborts
+ */
+ return 0;
+ }
-static void rdt_init_pids_monitoring() {
- for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
- /*
- * Each group must have not-null proc_pids array.
- * Initial refresh is not mandatory for proper
- * PIDs statistics detection.
- */
- 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);
- if (0 != init_result) {
- ERROR(RDT_PLUGIN
- ": Initialization of proc_pids for group %zu failed. Error: %d",
- group_idx, init_result);
- continue;
+#if COLLECT_DEBUG
+ rdt_dump_ngroups();
+#endif /* COLLECT_DEBUG */
+#else /* !LIBPQOS2 */
+ ERROR(RDT_PLUGIN ": Configuration parameter \"%s\" not supported, please "
+ "recompile collectd with libpqos version 2.0 or newer.",
+ child->key);
+#endif /* LIBPQOS2 */
+ } else {
+ ERROR(RDT_PLUGIN ": Unknown configuration parameter \"%s\".", child->key);
}
-
- int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
- g_rdt->pngroups[group_idx]);
- if (0 != refresh_result)
- ERROR(RDT_PLUGIN ": Initial refresh of group %zu failed. Error: %d",
- group_idx, refresh_result);
}
+
+ return 0;
}
-#endif /* LIBPQOS2 */
static int read_cores_data() {
ret = pqos_fini();
if (ret != PQOS_RETVAL_OK)
ERROR(RDT_PLUGIN ": Error shutting down PQoS library.");
-
rdt_free_cgroups();
#ifdef LIBPQOS2
rdt_free_ngroups(g_rdt);
#include "intel_rdt.c" /* sic */
#include "testing.h"
-#include <sys/stat.h>
/***************************************************************************
* PQOS mocks
int pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu) {
return 0;
}
+
/***************************************************************************
* 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;
-} 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"
-
rdt_ctx_t *stub_rdt_setup() {
rdt_ctx_t *rdt = calloc(1, sizeof(*rdt));
/***************************************************************************
* tests
*/
-DEF_TEST(initialize_proc_pids__on_nullptr) {
- /* 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;
-
- /* 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);
-
- /* cleanup */
- free(proc_pids_array);
- return 0;
-}
-
-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(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(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);
- return 0;
-}
-
-DEF_TEST(pids_list_add_pids_list__non_empty_lists) {
- /* 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;
- 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]);
- }
-
- /* check */
- int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
- EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2), increase);
-
- 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]));
- EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_2[i]));
- }
-
- /* setup */
- pids_list_free(pids_list_1);
- pids_list_free(pids_list_2);
- return 0;
-}
-
-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;
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(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);
- EXPECT_EQ_INT(0, result);
- EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), increase);
-
- 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]));
-
- /* setup */
- pids_list_free(pids_list_1);
- pids_list_free(pids_list_2);
- 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(-1, 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_contains_pid(output[0].pids, 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);
- }
-
- /* cleanup */
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
- pids_list_free(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_contains_pid(output[i].pids, 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);
- }
- }
-
- /* cleanup */
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
- pids_list_free(output[i].pids);
- free(output);
- stub_procfs_teardown();
- return 0;
-}
-
DEF_TEST(rdt_config_ngroups__one_process) {
/* setup */
rdt_ctx_t *rdt = stub_rdt_setup();
return 0;
}
-DEF_TEST(rdt_pid_list_diff__all_changed) {
- /* 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;
-
- /* check */
- int result = rdt_pid_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
- 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);
-
- 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]));
- EXPECT_EQ_INT(1, pids_list_contains_pid(lost_pids, pids_array_before[i]));
- }
-
- /* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
- pids_list_free(new_pids);
- pids_list_free(lost_pids);
-
- return 0;
-}
-
-DEF_TEST(rdt_pid_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;
-
- /* check */
- int result = rdt_pid_list_diff(pids_list_before, pids_list_before, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
- 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);
-
- /* cleanup */
- pids_list_free(pids_list_before);
-
- return 0;
-}
-
-DEF_TEST(rdt_pid_list_diff__one_added) {
- /* setup */
- 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;
-
- /* check */
- int result = rdt_pid_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
- 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);
-
- /* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
- pids_list_free(new_pids);
-
- return 0;
-}
-
-DEF_TEST(rdt_pid_list_diff__one_removed) {
- /* setup */
- 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]);
-
- pids_list_t *new_pids = NULL;
- size_t new_pids_count = 0;
- pids_list_t *lost_pids = NULL;
- size_t lost_pids_count = 0;
-
- /* check */
- int result = rdt_pid_list_diff(pids_list_before, pids_list_after, &new_pids,
- &new_pids_count, &lost_pids, &lost_pids_count);
- 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);
-
- /* cleanup */
- pids_list_free(pids_list_before);
- pids_list_free(pids_list_after);
- pids_list_free(lost_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(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(rdt_config_ngroups__one_process);
RUN_TEST(rdt_config_ngroups__two_groups);
RUN_TEST(rdt_config_ngroups__too_long_proc_name);
RUN_TEST(rdt_config_ngroups__duplicate_proc_name_in_group);
RUN_TEST(rdt_config_ngroups__empty_group);
RUN_TEST(rdt_config_ngroups__empty_proc_name);
- RUN_TEST(rdt_pid_list_diff__all_changed);
- RUN_TEST(rdt_pid_list_diff__nothing_changed);
- RUN_TEST(rdt_pid_list_diff__one_added);
- RUN_TEST(rdt_pid_list_diff__one_removed);
- stub_procfs_teardown();
END_TEST;
}
--- /dev/null
+#include "collectd.h"
+#include "utils/common/common.h"
+#include "utils_proc_pids.h"
+
+#define UTIL_NAME "utils_proc_pids"
+
+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);
+ }
+}
+
+int is_proc_name_valid(const char *name) {
+
+ if (name != NULL) {
+ unsigned len = strlen(name);
+ if (len > 0 && len <= MAX_PROC_NAME_LEN)
+ return 1;
+ else {
+ DEBUG(UTIL_NAME
+ ": Process name \'%s\' is too long. Max supported len is %d chars.",
+ name, MAX_PROC_NAME_LEN);
+ }
+ }
+
+ return 0;
+}
+
+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 (new_element == NULL) {
+ ERROR(UTIL_NAME ": 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;
+}
+
+int pids_list_contains_pid(pids_list_t *list, const pid_t pid) {
+ assert(list);
+
+ 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) {
+ assert(dst);
+ assert(src);
+ assert(dst_num);
+
+ pids_list_t *current = src;
+ int ret;
+
+ while (current != NULL) {
+ ret = pids_list_add_pid(dst, current->pid);
+ if (0 != ret)
+ return ret;
+
+ ++(*dst_num);
+ current = current->next;
+ }
+
+ 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);
+ 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);
+ if (path == NULL)
+ return -1;
+ FILE *f = fopen(path, "r");
+ if (f == NULL) {
+ ERROR(UTIL_NAME ": Failed to open comm file, error: %d\n", errno);
+ sfree(path);
+ return -1;
+ }
+ size_t read_length = fread(name, sizeof(char), out_size, f);
+ name[out_size - 1] = '\0';
+ fclose(f);
+ sfree(path);
+ /* strip new line ending */
+ char *newline = strchr(name, '\n');
+ if (newline) {
+ *newline = '\0';
+ }
+
+ return read_length;
+}
+
+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 -1; /* conversion failed, not proc-pid */
+ }
+ /* all checks passed, marking as success */
+ 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) {
+
+ assert(proc_pids_array);
+ assert(NULL == *proc_pids_array);
+
+ /* Copy procs names to output array. Initialize pids list with NULL value. */
+ (*proc_pids_array) =
+ calloc(procs_names_array_size, sizeof(**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;
+ }
+
+ return 0;
+}
+
+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);
+
+ DIR *proc_dir = opendir(procfs_path);
+ if (proc_dir == NULL) {
+ ERROR(UTIL_NAME ": Could not open %s directory, error: %d", procfs_path,
+ errno);
+ return -1;
+ }
+
+ int init_result = initialize_proc_pids(
+ procs_names_array, procs_names_array_size, proc_pids_array);
+ if (0 != init_result)
+ return -1;
+
+ /* 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(UTIL_NAME ": 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(UTIL_NAME ": failed to close %s directory, error: %d", procfs_path,
+ errno);
+ sfree((*proc_pids_array));
+ return -1;
+ }
+ return 0;
+}
+
+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);
+ assert(added);
+ assert(removed);
+
+ if (NULL == prev) {
+ /* append all PIDs from curr to added*/
+ return pids_list_add_pids_list(added, curr, added_num);
+ } else if (NULL == curr) {
+ /* append all PIDs from prev to removed*/
+ return pids_list_add_pids_list(removed, prev, removed_num);
+ }
+
+ 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);
+ 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);
+ if (add_result < 0)
+ return add_result;
+ ++(*added_num);
+ }
+ item = item->next;
+ }
+
+ return 0;
+}
--- /dev/null
+/**
+ * collectd - src/utils_config_pids.h
+ *
+ * Copyright(c) 2018 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>
+ **/
+
+#include <dirent.h>
+#include <sys/types.h>
+
+/*
+ * Process name inside comm file is limited to 16 chars.
+ * More info here: http://man7.org/linux/man-pages/man5/proc.5.html
+ */
+#define MAX_PROC_NAME_LEN 16
+
+/* Helper typedef for process name array
+ * Extra 1 char is added for string null termination.
+ */
+typedef char proc_comm_t[MAX_PROC_NAME_LEN + 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_free
+ *
+ * DESCRIPTION
+ * Free all elements of given pids list
+ *
+ * PARAMETERS
+ * `list' Head of target pids_list.
+ */
+void pids_list_free(pids_list_t *list);
+
+/*
+ * NAME
+ * is_proc_name_valid
+ *
+ * DESCRIPTION
+ * Checks if given string as valid process name.
+ *
+ * PARAMETERS
+ * `name' null-terminated char array
+ *
+ * RETURN VALUE
+ * If given name is a valid process name, returns 1,
+ * Otherwise returns 0.
+ */
+int is_proc_name_valid(const char *name);
+
+/*
+ * 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.
+ */
+int pids_list_add_pid(pids_list_t **list, const pid_t pid);
+
+/*
+ * NAME
+ * pids_list_contains_pid
+ *
+ * DESCRIPTION
+ * Tests if pids list contains specific pid.
+ *
+ * PARAMETERS
+ * `list' Head of pids_list.
+ * `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
+ * pids_list_add_pids_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.
+ *
+ * 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);
+/*
+ * 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
+ */
+int read_proc_name(const char *procfs_path, const struct dirent *pid_entry,
+ char *name, const size_t out_size);
+
+/*
+ * 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. -1 on error.
+ */
+int get_pid_number(struct dirent *entry, pid_t *pid);
+
+/*
+ * 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
+ * Helper function to properly initialize array of proc_pids.
+ * Allocates memory for proc_pids structs.
+ *
+ * PARAMETERS
+ * `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.
+ * RETURN VALUE
+ * 0 on success. Negative number on error:
+ * -1: allocation error
+ */
+int initialize_proc_pids(const char **procs_names_array,
+ const size_t procs_names_array_size,
+ proc_pids_t **proc_pids_array);
+
+/*
+ * 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_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.
+ *
+ * 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);
+
+/*
+ * NAME
+ * pids_list_diff
+ *
+ * DESCRIPTION
+ * 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
+ * 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);
--- /dev/null
+#include "testing.h"
+#include "utils_proc_pids.c" /* sic */
+#include <sys/stat.h>
+
+/***************************************************************************
+ * 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;
+} 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(initialize_proc_pids__on_nullptr) {
+ /* 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;
+
+ /* 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);
+
+ /* cleanup */
+ free(proc_pids_array);
+ return 0;
+}
+
+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(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(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);
+ return 0;
+}
+
+DEF_TEST(pids_list_add_pids_list__non_empty_lists) {
+ /* 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;
+ 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]);
+ }
+
+ /* check */
+ int result = pids_list_add_pids_list(&pids_list_1, pids_list_2, &increase);
+ EXPECT_EQ_INT(0, result);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array_2), increase);
+
+ 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]));
+ EXPECT_EQ_INT(1, pids_list_contains_pid(pids_list_1, pids_array_2[i]));
+ }
+
+ /* setup */
+ pids_list_free(pids_list_1);
+ pids_list_free(pids_list_2);
+ return 0;
+}
+
+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;
+ for (size_t i = 0; i < STATIC_ARRAY_SIZE(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);
+ EXPECT_EQ_INT(0, result);
+ EXPECT_EQ_INT(STATIC_ARRAY_SIZE(pids_array), increase);
+
+ 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]));
+
+ /* setup */
+ pids_list_free(pids_list_1);
+ pids_list_free(pids_list_2);
+ 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(-1, 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_contains_pid(output[0].pids, 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);
+ }
+
+ /* cleanup */
+ for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
+ pids_list_free(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_contains_pid(output[i].pids, 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);
+ }
+ }
+
+ /* cleanup */
+ for (size_t i = 0; i < STATIC_ARRAY_SIZE(proc_names); ++i)
+ pids_list_free(output[i].pids);
+ free(output);
+ stub_procfs_teardown();
+ return 0;
+}
+
+DEF_TEST(pids_list_diff__all_changed) {
+ /* 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;
+
+ /* check */
+ int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
+ &new_pids_count, &lost_pids, &lost_pids_count);
+ 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);
+
+ 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]));
+ EXPECT_EQ_INT(1, pids_list_contains_pid(lost_pids, pids_array_before[i]));
+ }
+
+ /* cleanup */
+ pids_list_free(pids_list_before);
+ pids_list_free(pids_list_after);
+ pids_list_free(new_pids);
+ pids_list_free(lost_pids);
+
+ return 0;
+}
+
+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;
+
+ /* check */
+ int result = pids_list_diff(pids_list_before, pids_list_before, &new_pids,
+ &new_pids_count, &lost_pids, &lost_pids_count);
+ 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);
+
+ /* cleanup */
+ pids_list_free(pids_list_before);
+
+ return 0;
+}
+
+DEF_TEST(pids_list_diff__one_added) {
+ /* setup */
+ 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;
+
+ /* check */
+ int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
+ &new_pids_count, &lost_pids, &lost_pids_count);
+ 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);
+
+ /* cleanup */
+ pids_list_free(pids_list_before);
+ pids_list_free(pids_list_after);
+ pids_list_free(new_pids);
+
+ return 0;
+}
+
+DEF_TEST(pids_list_diff__one_removed) {
+ /* setup */
+ 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]);
+
+ pids_list_t *new_pids = NULL;
+ size_t new_pids_count = 0;
+ pids_list_t *lost_pids = NULL;
+ size_t lost_pids_count = 0;
+
+ /* check */
+ int result = pids_list_diff(pids_list_before, pids_list_after, &new_pids,
+ &new_pids_count, &lost_pids, &lost_pids_count);
+ 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);
+
+ /* cleanup */
+ pids_list_free(pids_list_before);
+ pids_list_free(pids_list_after);
+ pids_list_free(lost_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(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(pids_list_diff__all_changed);
+ RUN_TEST(pids_list_diff__nothing_changed);
+ RUN_TEST(pids_list_diff__one_added);
+ RUN_TEST(pids_list_diff__one_removed);
+ stub_procfs_teardown();
+ END_TEST;
+}