X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fintel_rdt.c;h=e509b875f28a560e9482578da342a874879c53a3;hb=7f6e8b79ab4bf40d08c8955e1feb67416f2dc358;hp=446dc6559d6d722b3bef7d7fd5e9ac60f2864267;hpb=62be067773ebc9d9d958c9707332bfeaecedac8a;p=collectd.git diff --git a/src/intel_rdt.c b/src/intel_rdt.c index 446dc655..e509b875 100644 --- a/src/intel_rdt.c +++ b/src/intel_rdt.c @@ -24,6 +24,7 @@ * Authors: * Serhiy Pshyk * Starzyk, Mateusz + * Wojciech Andralojc **/ #include "collectd.h" @@ -33,25 +34,72 @@ #define RDT_PLUGIN "intel_rdt" +/* libpqos v2.0 or newer is required for process monitoring*/ +#undef LIBPQOS2 +#if defined(PQOS_VERSION) && PQOS_VERSION >= 20000 +#define LIBPQOS2 +#endif + +#define RDT_PLUGIN "intel_rdt" + #define RDT_MAX_SOCKETS 8 #define RDT_MAX_SOCKET_CORES 64 #define RDT_MAX_CORES (RDT_MAX_SOCKET_CORES * RDT_MAX_SOCKETS) + +#ifdef LIBPQOS2 /* * Process name inside comm file is limited to 16 chars. * More info here: http://man7.org/linux/man-pages/man5/proc.5.html */ -#define RDT_MAX_PROC_COMM_LENGTH 16 +#define RDT_MAX_NAME_LEN 16 +#define RDT_MAX_NAMES_GROUPS 64 + +#define RDT_PROC_PATH "/proc" +#endif /* LIBPQOS2 */ typedef enum { UNKNOWN = 0, CONFIGURATION_ERROR, } 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; + char **names; + proc_pids_t *proc_pids_array; + size_t monitored_pids_count; + enum pqos_mon_event events; +}; +typedef struct rdt_name_group_s rdt_name_group_t; +#endif /* LIBPQOS2 */ + struct rdt_ctx_s { core_groups_list_t cores; enum pqos_mon_event events[RDT_MAX_CORES]; - struct pqos_mon_data *pgroups[RDT_MAX_CORES]; - size_t num_groups; + struct pqos_mon_data *pcgroups[RDT_MAX_CORES]; +#ifdef LIBPQOS2 + rdt_name_group_t ngroups[RDT_MAX_NAMES_GROUPS]; + struct pqos_mon_data *pngroups[RDT_MAX_NAMES_GROUPS]; + size_t num_ngroups; +#endif /* LIBPQOS2 */ const struct pqos_cpuinfo *pqos_cpu; const struct pqos_cap *pqos_cap; const struct pqos_capability *cap_mon; @@ -62,6 +110,187 @@ static rdt_ctx_t *g_rdt; static rdt_config_status g_state = UNKNOWN; +static int g_interface = -1; + +#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) + return 1; + + return 0; +} + +/* + * NAME + * strlisttoarray + * + * DESCRIPTION + * Converts string representing list of strings into array of strings. + * Allowed format is: + * name,name1,name2,name3 + * + * PARAMETERS + * `str_list' String representing list of strings. + * `names' Array to put extracted strings into. + * `names_num' Variable to put number of extracted strings. + * + * RETURN VALUE + * Number of elements placed into names. + */ +static int strlisttoarray(char *str_list, char ***names, size_t *names_num) { + char *saveptr = NULL; + + if (str_list == NULL || names == NULL) + return -EINVAL; + + if (strstr(str_list, ",,")) { + /* strtok ignores empty words between separators. + * This condition handles that by rejecting strings + * with consecutive seprators */ + ERROR(RDT_PLUGIN ": Empty process name"); + return -EINVAL; + } + + for (;;) { + char *token = strtok_r(str_list, ",", &saveptr); + if (token == NULL) + break; + + str_list = NULL; + + while (isspace(*token)) + token++; + + if (*token == '\0') + continue; + + if ((isdupstr((const char **)*names, *names_num, token))) { + ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'", + token, str_list); + return -EINVAL; + } else { + if (0 != strarray_add(names, names_num, token)) { + ERROR(RDT_PLUGIN ": Error allocating process name string"); + return -ENOMEM; + } + } + } + + return 0; +} + +/* + * NAME + * ngroup_cmp + * + * DESCRIPTION + * Function to compare names in two name groups. + * + * PARAMETERS + * `ng_a' Pointer to name group a. + * `ng_b' Pointer to name group b. + * + * RETURN VALUE + * 1 if both groups contain the same names + * 0 if none of their names match + * -1 if some but not all names match + */ +static int ngroup_cmp(const rdt_name_group_t *ng_a, + const rdt_name_group_t *ng_b) { + unsigned found = 0; + + assert(ng_a != NULL); + assert(ng_b != NULL); + + const size_t sz_a = (unsigned)ng_a->num_names; + const size_t sz_b = (unsigned)ng_b->num_names; + const char **tab_a = (const char **)ng_a->names; + const char **tab_b = (const char **)ng_b->names; + + 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) + found++; + } + /* if no names are the same */ + if (!found) + return 0; + /* if group contains same names */ + if (sz_a == sz_b && sz_b == (size_t)found) + return 1; + /* if not all names are the same */ + return -1; +} + +/* + * NAME + * oconfig_to_ngroups + * + * DESCRIPTION + * Function to set the descriptions and names for each process names group. + * Takes a config option containing list of strings that are used to set + * process group values. + * + * PARAMETERS + * `item' Config option containing process names groups. + * `groups' Table of process name groups to set values in. + * `max_groups' Maximum number of process name groups allowed. + * + * RETURN VALUE + * On success, the number of name groups set up. On error, appropriate + * negative error value. + */ +static int oconfig_to_ngroups(const oconfig_item_t *item, + rdt_name_group_t *groups, + const size_t max_groups) { + int index = 0; + + assert(groups != NULL); + assert(max_groups > 0); + assert(item != NULL); + + for (int j = 0; j < item->values_num; j++) { + int ret; + char value[DATA_MAX_NAME_LEN]; + + if ((item->values[j].value.string == NULL) || + (strlen(item->values[j].value.string) == 0)) { + ERROR(RDT_PLUGIN ": Error - empty group"); + return -EINVAL; + } + + sstrncpy(value, item->values[j].value.string, sizeof(value)); + + ret = strlisttoarray(value, &groups[index].names, &groups[index].num_names); + if (ret != 0 || groups[index].num_names == 0) { + ERROR(RDT_PLUGIN ": Error parsing process names group (%s)", + item->values[j].value.string); + return -EINVAL; + } + + /* set group description info */ + groups[index].desc = sstrdup(item->values[j].value.string); + if (groups[index].desc == NULL) { + ERROR(RDT_PLUGIN ": Error allocating name group description"); + return -ENOMEM; + } + + groups[index].proc_pids_array = NULL; + groups[index].monitored_pids_count = 0; + + index++; + + if (index >= (const int)max_groups) { + WARNING(RDT_PLUGIN ": Too many process names groups configured"); + return index; + } + } + + return index; +} +#endif /* LIBPQOS2 */ + #if COLLECT_DEBUG static void rdt_dump_cgroups(void) { char cores[RDT_MAX_CORES * 4]; @@ -70,9 +299,9 @@ static void rdt_dump_cgroups(void) { return; DEBUG(RDT_PLUGIN ": Core Groups Dump"); - DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->num_groups); + DEBUG(RDT_PLUGIN ": groups count: %" PRIsz, g_rdt->cores.num_cgroups); - for (size_t i = 0; i < g_rdt->num_groups; i++) { + 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)); @@ -90,40 +319,166 @@ static void rdt_dump_cgroups(void) { 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_data(void) { +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) { /* - * CORE - monitored group of cores - * RMID - Resource Monitoring ID associated with the monitored group + * 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(" CORE RMID LLC[KB] MBL[MB] MBR[MB]"); - for (size_t i = 0; i < g_rdt->num_groups; i++) { - const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values; + + 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(" [%s] %8u %10.1f %10.1f %10.1f", g_rdt->cores.cgroups[i].desc, - g_rdt->pgroups[i]->poll_ctx[0].rmid, llc, mbl, mbr); + 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->pgroups[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; @@ -171,6 +526,24 @@ static int rdt_is_core_id_valid(unsigned int core_id) { 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; @@ -218,9 +591,9 @@ static int rdt_config_cgroups(oconfig_item_t *item) { g_rdt->pqos_cpu->num_cores); DEBUG(RDT_PLUGIN ": Available events to monitor: %#x", events); - g_rdt->num_groups = n; - for (size_t i = 0; i < n; i++) { - for (size_t j = 0; j < i; j++) { + 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]); @@ -232,8 +605,8 @@ static int rdt_config_cgroups(oconfig_item_t *item) { } g_rdt->events[i] = events; - g_rdt->pgroups[i] = calloc(1, sizeof(*g_rdt->pgroups[i])); - if (g_rdt->pgroups[i] == NULL) { + 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; @@ -243,22 +616,89 @@ static int rdt_config_cgroups(oconfig_item_t *item) { return 0; } -/* Helper typedef for process name array - * Extra 1 char is added for string null termination. - */ -typedef char proc_comm_t[RDT_MAX_PROC_COMM_LENGTH + 1]; +#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; -/* Linked one-way list of pids. */ -typedef struct pids_list_s { - pid_t pid; - struct pids_list_s *next; -} pids_list_t; + if (item == NULL) { + DEBUG(RDT_PLUGIN ": ngroups_config: Invalid argument."); + return -EINVAL; + } -/* 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; + 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 @@ -277,6 +717,8 @@ typedef struct proc_pids_s { * -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) { @@ -296,6 +738,72 @@ static 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. + */ +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 @@ -357,9 +865,7 @@ static int read_proc_name(const char *procfs_path, * `pid' PID number to be filled * * RETURN VALUE - * 0 on success. Negative number on error: - * -1: given entry is not a directory - * -2: PID conversion error + * 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*/ @@ -373,7 +879,7 @@ static int get_pid_number(struct dirent *entry, pid_t *pid) { /* trying to get pid number from directory name*/ *pid = strtoul(entry->d_name, &tmp_end, 10); if (*tmp_end != '\0') { - return -2; /* conversion failed, not proc-pid */ + return -1; /* conversion failed, not proc-pid */ } /* all checks passed, marking as success */ return 0; @@ -381,6 +887,76 @@ static 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 + */ +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 @@ -390,28 +966,24 @@ static int get_pid_number(struct dirent *entry, pid_t *pid) { * Output array will have same element count as input array. * * PARAMETERS - * `procfs_path' Path to systems proc directory (e.g. /proc) - * `procs' Array of null-terminated strings with - * process' names to search for - * `procs_size' procs array element count - * `proc_pids_array' Address of pointer, under which new - * array of proc_pids will be allocated. Must be NULL. + * `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. Negative number on error: - * -1: could not open /proc dir + * 0 on success. -1 on error. */ -__attribute__((unused)) /* TODO: remove this attribute when PID monitoring is - implemented */ -static int -fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array, - const size_t procs_names_array_size, - proc_pids_t **proc_pids_array) { +static int fetch_pids_for_procs(const char *procfs_path, + const char **procs_names_array, + const size_t procs_names_array_size, + proc_pids_t **proc_pids_array) { assert(procfs_path); assert(procs_names_array); assert(procs_names_array_size); - assert(proc_pids_array); - assert(NULL == *proc_pids_array); DIR *proc_dir = opendir(procfs_path); if (proc_dir == NULL) { @@ -420,14 +992,10 @@ fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array, return -1; } - /* Copy procs names to output array. Initialize pids list with NULL value. */ - (*proc_pids_array) = - calloc(procs_names_array_size, sizeof(**proc_pids_array)); - for (size_t i = 0; i < procs_names_array_size; ++i) { - sstrncpy((*proc_pids_array)[i].proccess_name, procs_names_array[i], - STATIC_ARRAY_SIZE((*proc_pids_array)[i].proccess_name)); - (*proc_pids_array)[i].pids = NULL; - } + 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; @@ -458,10 +1026,12 @@ fetch_pids_for_procs(const char *procfs_path, const char **procs_names_array, 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); @@ -484,14 +1054,34 @@ static int rdt_preinit(void) { struct pqos_config pqos = {.fd_log = -1, .callback_log = rdt_pqos_log, .context_log = NULL, - .verbose = 0}; + .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."); @@ -520,7 +1110,6 @@ rdt_preinit_error2: pqos_fini(); rdt_preinit_error1: - sfree(g_rdt); return -1; @@ -539,7 +1128,7 @@ static int rdt_config(oconfig_item_t *ci) { for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; - if (strcasecmp("Cores", child->key) == 0) { + 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 @@ -552,6 +1141,38 @@ static int rdt_config(oconfig_item_t *ci) { #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); } @@ -592,32 +1213,366 @@ static void rdt_submit_gauge(const char *cgroup, const char *type, plugin_dispatch_values(&vl); } -static int rdt_read(__attribute__((unused)) user_data_t *ud) { - int ret; +#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); + } - if (g_rdt == NULL) { - ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized."); - return -EINVAL; + 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); + } + item = item->next; } - ret = pqos_mon_poll(&g_rdt->pgroups[0], (unsigned)g_rdt->num_groups); - if (ret != PQOS_RETVAL_OK) { - ERROR(RDT_PLUGIN ": Failed to poll monitoring data."); + item = curr; + while (item != NULL) { + if (0 == pids_list_contains_pid(prev, item->pid)) { + pids_list_add_pid(added, item->pid); + ++(*added_num); + } + item = item->next; + } + + return 0; +} + +static int rdt_refresh_ngroup(rdt_name_group_t *ngroup, + struct pqos_mon_data *group_mon_data) { + + int result = 0; + + if (NULL == ngroup) + return -1; + + if (NULL == ngroup->proc_pids_array) { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\' uninitialized process pids array.", + ngroup->desc); + return -1; } + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group.", + ngroup->desc); + + proc_pids_t *proc_pids_array_prev = ngroup->proc_pids_array; + proc_pids_t *proc_pids_array_curr = NULL; + + int fetch_result = + fetch_pids_for_procs(RDT_PROC_PATH, (const char **)ngroup->names, + ngroup->num_names, &proc_pids_array_curr); + + if (0 != fetch_result) { + ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' failed to fetch PIDs.", + ngroup->desc); + return fetch_result; + } + + pids_list_t *new_pids = NULL; + size_t new_pids_count = 0; + + pids_list_t *lost_pids = NULL; + size_t lost_pids_count = 0; + + for (size_t i = 0; i < ngroup->num_names; ++i) { + if (NULL == proc_pids_array_prev[i].pids && + NULL == proc_pids_array_curr[i].pids) + continue; + int diff_result = rdt_pid_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) { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] during PID diff.", + ngroup->desc, diff_result); + result = -1; + goto cleanup; + } + } + + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' process names group, added: " + "%u, removed: %u.", + ngroup->desc, (unsigned)new_pids_count, (unsigned)lost_pids_count); + + if (new_pids_count != 0 || lost_pids_count != 0) { + + if (new_pids) { + pid_t new_pids_array[new_pids_count]; + pids_list_to_array(new_pids_array, new_pids, + STATIC_ARRAY_SIZE(new_pids_array)); + + /* no pids are monitored for this group yet: start monitoring */ + if (0 == ngroup->monitored_pids_count) { + + int start_result = + pqos_mon_start_pids(new_pids_count, new_pids_array, ngroup->events, + (void *)ngroup->desc, group_mon_data); + if (PQOS_RETVAL_OK == start_result) { + ngroup->monitored_pids_count = new_pids_count; + } else { + ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while " + "STARTING pids monitoring", + ngroup->desc, start_result); + result = -1; + goto pqos_error_recovery; + } + + } else { + + int add_result = + pqos_mon_add_pids(new_pids_count, new_pids_array, group_mon_data); + if (PQOS_RETVAL_OK == add_result) + ngroup->monitored_pids_count += new_pids_count; + else { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] while ADDING pids.", + ngroup->desc, add_result); + result = -1; + goto pqos_error_recovery; + } + } + } + + if (lost_pids) { + pid_t lost_pids_array[lost_pids_count]; + pids_list_to_array(lost_pids_array, lost_pids, + STATIC_ARRAY_SIZE(lost_pids_array)); + + if (lost_pids_count == ngroup->monitored_pids_count) { + /* all pids for this group are lost: stop monitoring */ + int stop_result = pqos_mon_stop(group_mon_data); + if (PQOS_RETVAL_OK != stop_result) { + ERROR(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\'. Error [%d] while " + "STOPPING monitoring", + ngroup->desc, stop_result); + result = -1; + goto pqos_error_recovery; + } + ngroup->monitored_pids_count = 0; + } else { + assert(lost_pids_count < ngroup->monitored_pids_count); + int remove_result = pqos_mon_remove_pids( + lost_pids_count, lost_pids_array, group_mon_data); + if (PQOS_RETVAL_OK == remove_result) { + ngroup->monitored_pids_count -= lost_pids_count; + } else { + ERROR(RDT_PLUGIN + ": rdt_refresh_ngroup: \'%s\'. Error [%d] while REMOVING pids.", + ngroup->desc, remove_result); + result = -1; + goto pqos_error_recovery; + } + } + } + + ngroup->proc_pids_array = proc_pids_array_curr; + } + + goto cleanup; + +pqos_error_recovery: + /* Why? + * Resources might be temporary unavailable. + * + * How? + * Collectd will halt the reading thread for this + * plugin if it returns an error. + * Consecutive errors will be increasing the read period + * up to 1 day interval. + * On pqos error stop monitoring current group + * and reset the proc_pids array + * monitoring will be restarted on next collectd read cycle + */ + DEBUG(RDT_PLUGIN ": rdt_refresh_ngroup: \'%s\' group RESET after error.", + ngroup->desc); + pqos_mon_stop(group_mon_data); + for (size_t i = 0; i < ngroup->num_names; ++i) { + if (ngroup->proc_pids_array[i].pids) + pids_list_free(ngroup->proc_pids_array[i].pids); + } + sfree(ngroup->proc_pids_array); + + initialize_proc_pids((const char **)ngroup->names, ngroup->num_names, + &ngroup->proc_pids_array); + ngroup->monitored_pids_count = 0; + +cleanup: + if (ngroup->proc_pids_array == proc_pids_array_curr) { + assert(proc_pids_array_curr); + /* new list was successfully saved, free the old one */ + for (size_t i = 0; i < ngroup->num_names; ++i) + if (proc_pids_array_prev[i].pids) + pids_list_free(proc_pids_array_prev[i].pids); + + sfree(proc_pids_array_prev); + + } else { + /* new list was not saved. Free the new list, keep the old one*/ + for (size_t i = 0; i < ngroup->num_names; ++i) + if (proc_pids_array_curr[i].pids) + pids_list_free(proc_pids_array_curr[i].pids); + + sfree(proc_pids_array_curr); + } + + if (new_pids) + pids_list_free(new_pids); + + if (lost_pids) + pids_list_free(lost_pids); + + 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_data(); + rdt_dump_pids_data(); #endif /* COLLECT_DEBUG */ - for (size_t i = 0; i < g_rdt->num_groups; i++) { - core_group_t *cgroup = g_rdt->cores.cgroups + 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 (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 int read_cores_data() { + + if (0 == g_rdt->cores.num_cgroups) { + DEBUG(RDT_PLUGIN ": read_cores_data: not configured - Cores read skipped"); + return 0; + } + DEBUG(RDT_PLUGIN ": read_cores_data: Cores data poll"); + + int ret = + pqos_mon_poll(&g_rdt->pcgroups[0], (unsigned)g_rdt->cores.num_cgroups); + if (ret != PQOS_RETVAL_OK) { + ERROR(RDT_PLUGIN ": read_cores_data: Failed to poll monitoring data for " + "cores. Error [%d].", + ret); + return -1; + } + + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { + core_group_t *cgroup = g_rdt->cores.cgroups + i; enum pqos_mon_event mbm_events = (PQOS_MON_EVENT_LMEM_BW | PQOS_MON_EVENT_TMEM_BW | PQOS_MON_EVENT_RMEM_BW); - const struct pqos_event_values *pv = &g_rdt->pgroups[i]->values; + const struct pqos_event_values *pv = &g_rdt->pcgroups[i]->values; /* Submit only monitored events data */ @@ -635,30 +1590,65 @@ static int rdt_read(__attribute__((unused)) user_data_t *ud) { } } +#if COLLECT_DEBUG + rdt_dump_cores_data(); +#endif /* COLLECT_DEBUG */ + return 0; } -static int rdt_init(void) { - int ret; +static int rdt_read(__attribute__((unused)) user_data_t *ud) { - if (g_state == CONFIGURATION_ERROR) - return -1; + if (g_rdt == NULL) { + ERROR(RDT_PLUGIN ": rdt_read: plugin not initialized."); + return -EINVAL; + } + + int cores_read_result = read_cores_data(); + +#ifdef LIBPQOS2 + int pids_read_result = read_pids_data(); +#endif /* LIBPQOS2 */ - ret = rdt_preinit(); - if (ret != 0) - return ret; + if (0 != cores_read_result) + return cores_read_result; - /* Start monitoring */ - for (size_t i = 0; i < g_rdt->num_groups; i++) { +#ifdef LIBPQOS2 + if (0 != pids_read_result) + return pids_read_result; +#endif /* LIBPQOS2 */ + + return 0; +} + +static void rdt_init_cores_monitoring() { + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { core_group_t *cg = g_rdt->cores.cgroups + i; - ret = pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i], - (void *)cg->desc, g_rdt->pgroups[i]); + int mon_start_result = + pqos_mon_start(cg->num_cores, cg->cores, g_rdt->events[i], + (void *)cg->desc, g_rdt->pcgroups[i]); - if (ret != PQOS_RETVAL_OK) - ERROR(RDT_PLUGIN ": Error starting monitoring group %s (pqos status=%d)", - cg->desc, ret); + if (mon_start_result != PQOS_RETVAL_OK) + ERROR(RDT_PLUGIN + ": Error starting cores monitoring group %s (pqos status=%d)", + cg->desc, mon_start_result); } +} + +static int rdt_init(void) { + + if (g_state == CONFIGURATION_ERROR) + return -1; + + int rdt_preinint_result = rdt_preinit(); + if (rdt_preinint_result != 0) + return rdt_preinint_result; + + rdt_init_cores_monitoring(); +#ifdef LIBPQOS2 + rdt_init_pids_monitoring(); +#endif /* LIBPQOS2 */ return 0; } @@ -671,16 +1661,25 @@ static int rdt_shutdown(void) { if (g_rdt == NULL) return 0; - /* Stop monitoring */ - for (size_t i = 0; i < g_rdt->num_groups; i++) { - pqos_mon_stop(g_rdt->pgroups[i]); + /* Stop monitoring cores */ + for (size_t i = 0; i < g_rdt->cores.num_cgroups; i++) { + pqos_mon_stop(g_rdt->pcgroups[i]); } +/* Stop pids monitoring */ +#ifdef LIBPQOS2 + for (size_t i = 0; i < g_rdt->num_ngroups; i++) + pqos_mon_stop(g_rdt->pngroups[i]); +#endif + 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); +#endif /* LIBPQOS2 */ sfree(g_rdt); return 0;