Fix compile time issues
[collectd.git] / src / intel_rdt.c
index 7364820..62848db 100644 (file)
@@ -31,7 +31,7 @@
 #include "collectd.h"
 #include "utils/common/common.h"
 #include "utils/config_cores/config_cores.h"
 #include "collectd.h"
 #include "utils/common/common.h"
 #include "utils/config_cores/config_cores.h"
-#include "utils_proc_pids.h"
+#include "utils/proc_pids/proc_pids.h"
 #include <pqos.h>
 
 #define RDT_PLUGIN "intel_rdt"
 #include <pqos.h>
 
 #define RDT_PLUGIN "intel_rdt"
@@ -105,7 +105,7 @@ static void rdt_submit_derive(const char *cgroup, const char *type,
   vl.values_len = 1;
 
   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
   vl.values_len = 1;
 
   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
-  snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
+  ssnprintf(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));
   sstrncpy(vl.type, type, sizeof(vl.type));
   if (type_instance)
     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
@@ -121,7 +121,7 @@ static void rdt_submit_gauge(const char *cgroup, const char *type,
   vl.values_len = 1;
 
   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
   vl.values_len = 1;
 
   sstrncpy(vl.plugin, RDT_PLUGIN, sizeof(vl.plugin));
-  snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s", cgroup);
+  ssnprintf(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));
   sstrncpy(vl.type, type, sizeof(vl.type));
   if (type_instance)
     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
@@ -144,8 +144,8 @@ static void rdt_dump_cgroups(void) {
 
     memset(cores, 0, sizeof(cores));
     for (size_t j = 0; j < cgroup->num_cores; j++) {
 
     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]);
+      ssnprintf(cores + strlen(cores), sizeof(cores) - strlen(cores) - 1, " %d",
+                cgroup->cores[j]);
     }
 
     DEBUG(RDT_PLUGIN ":  group[%zu]:", i);
     }
 
     DEBUG(RDT_PLUGIN ":  group[%zu]:", i);
@@ -171,8 +171,8 @@ static void rdt_dump_ngroups(void) {
   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++)
   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]);
+      ssnprintf(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 ":  group[%d]:", (int)i);
     DEBUG(RDT_PLUGIN ":    description: %s", g_rdt->ngroups[i].desc);
@@ -247,10 +247,10 @@ static void rdt_dump_pids_data(void) {
   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) {
   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[j].curr;
+      pids_list_t *list = g_rdt->ngroups[i].proc_pids[j]->curr;
       for (size_t k = 0; k < list->size; k++)
       for (size_t k = 0; k < list->size; k++)
-        snprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
-                 list->pids[k]);
+        ssnprintf(pids + strlen(pids), sizeof(pids) - strlen(pids) - 1, " %u",
+                  list->pids[k]);
     }
     DEBUG(RDT_PLUGIN ":  [%s] %s", g_rdt->ngroups[i].desc, pids);
   }
     }
     DEBUG(RDT_PLUGIN ":  [%s] %s", g_rdt->ngroups[i].desc, pids);
   }
@@ -325,8 +325,12 @@ static int strlisttoarray(char *str_list, char ***names, size_t *names_num) {
       continue;
 
     if ((isdupstr((const char **)*names, *names_num, token))) {
       continue;
 
     if ((isdupstr((const char **)*names, *names_num, token))) {
-      ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'",
-            token, str_list);
+      if (str_list != NULL)
+        ERROR(RDT_PLUGIN ": Duplicated process name \'%s\' in group \'%s\'",
+              token, str_list);
+      else
+        ERROR(RDT_PLUGIN ": Duplicated process name \'%s\'", token);
+
       return -EINVAL;
     } else {
       if (0 != strarray_add(names, names_num, token)) {
       return -EINVAL;
     } else {
       if (0 != strarray_add(names, names_num, token)) {
@@ -475,6 +479,8 @@ static void rdt_free_ngroups(rdt_ctx_t *rdt) {
   }
   if (rdt->proc_pids)
     sfree(rdt->proc_pids);
   }
   if (rdt->proc_pids)
     sfree(rdt->proc_pids);
+
+  rdt->num_ngroups = 0;
 }
 
 /*
 }
 
 /*
@@ -526,7 +532,7 @@ static int rdt_config_ngroups(rdt_ctx_t *rdt, const oconfig_item_t *item) {
          name_idx++) {
       DEBUG(RDT_PLUGIN ":    checking process name [%zu]: %s", name_idx,
             rdt->ngroups[group_idx].names[name_idx]);
          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])) {
+      if (!proc_pids_is_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]);
         ERROR(RDT_PLUGIN ": Process name group '%s' contains invalid name '%s'",
               rdt->ngroups[group_idx].desc,
               rdt->ngroups[group_idx].names[name_idx]);
@@ -798,7 +804,7 @@ static int read_pids_data() {
 #endif /* COLLECT_DEBUG */
 
 groups_refresh:
 #endif /* COLLECT_DEBUG */
 
 groups_refresh:
-  ret = update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
+  ret = proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
   if (0 != ret) {
     ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
     return ret;
   if (0 != ret) {
     ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
     return ret;
@@ -839,8 +845,8 @@ static void rdt_init_pids_monitoring() {
      * PIDs statistics detection.
      */
     rdt_name_group_t *ng = &g_rdt->ngroups[group_idx];
      * 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);
+    int init_result =
+        proc_pids_init((const char **)ng->names, ng->num_names, &ng->proc_pids);
     if (0 != init_result) {
       ERROR(RDT_PLUGIN
             ": Initialization of proc_pids for group %zu failed. Error: %d",
     if (0 != init_result) {
       ERROR(RDT_PLUGIN
             ": Initialization of proc_pids for group %zu failed. Error: %d",
@@ -849,9 +855,9 @@ static void rdt_init_pids_monitoring() {
     }
 
     /* update global proc_pids table */
     }
 
     /* update global proc_pids table */
-    proc_pids_t **proc_pids = realloc(g_rdt->proc_pids,
-                                      (g_rdt->num_proc_pids + ng->num_names) *
-                                          sizeof(*g_rdt->proc_pids));
+    proc_pids_t **proc_pids =
+        realloc(g_rdt->proc_pids, (g_rdt->num_proc_pids + ng->num_names) *
+                                      sizeof(*g_rdt->proc_pids));
     if (NULL == proc_pids) {
       ERROR(RDT_PLUGIN ": Alloc error\n");
       continue;
     if (NULL == proc_pids) {
       ERROR(RDT_PLUGIN ": Alloc error\n");
       continue;
@@ -864,10 +870,12 @@ static void rdt_init_pids_monitoring() {
     g_rdt->num_proc_pids += ng->num_names;
   }
 
     g_rdt->num_proc_pids += ng->num_names;
   }
 
-  int update_result =
-      update_proc_pids(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
-  if (0 != update_result)
-    ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
+  if (g_rdt->num_ngroups > 0) {
+    int update_result =
+        proc_pids_update(RDT_PROC_PATH, g_rdt->proc_pids, g_rdt->num_proc_pids);
+    if (0 != update_result)
+      ERROR(RDT_PLUGIN ": Initial update of proc pids failed");
+  }
 
   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
     int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
 
   for (size_t group_idx = 0; group_idx < g_rdt->num_ngroups; group_idx++) {
     int refresh_result = rdt_refresh_ngroup(&(g_rdt->ngroups[group_idx]),
@@ -880,7 +888,7 @@ static void rdt_init_pids_monitoring() {
 #endif /* LIBPQOS2 */
 /*
  * NAME
 #endif /* LIBPQOS2 */
 /*
  * NAME
- *   rdt_free_ngroups
+ *   rdt_free_cgroups
  *
  * DESCRIPTION
  *   Function to deallocate memory allocated for core groups.
  *
  * DESCRIPTION
  *   Function to deallocate memory allocated for core groups.
@@ -890,6 +898,7 @@ static void rdt_free_cgroups(void) {
   for (int i = 0; i < RDT_MAX_CORES; i++) {
     sfree(g_rdt->pcgroups[i]);
   }
   for (int i = 0; i < RDT_MAX_CORES; i++) {
     sfree(g_rdt->pcgroups[i]);
   }
+  g_rdt->cores.num_cgroups = 0;
 }
 
 static int rdt_default_cgroups(void) {
 }
 
 static int rdt_default_cgroups(void) {
@@ -917,7 +926,7 @@ static int rdt_default_cgroups(void) {
     cgroup->num_cores = 1;
     cgroup->cores[0] = i;
 
     cgroup->num_cores = 1;
     cgroup->cores[0] = i;
 
-    snprintf(desc, sizeof(desc), "%d", g_rdt->pqos_cpu->cores[i].lcore);
+    ssnprintf(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");
     cgroup->desc = strdup(desc);
     if (cgroup->desc == NULL) {
       ERROR(RDT_PLUGIN ": Error allocating core group description");
@@ -1099,21 +1108,27 @@ static int rdt_config(oconfig_item_t *ci) {
       reports a failure in configuration and
       aborts
     */
       reports a failure in configuration and
       aborts
     */
-    return (0);
+    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) {
   }
 
   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) {
+      if (g_rdt->cores.num_cgroups > 0) {
+        ERROR(RDT_PLUGIN
+              ": Configuration parameter \"%s\" can be used only once.",
+              child->key);
         g_state = CONFIGURATION_ERROR;
         g_state = CONFIGURATION_ERROR;
+      } else if (rdt_config_cgroups(child) != 0)
+        g_state = CONFIGURATION_ERROR;
+
+      if (g_state == CONFIGURATION_ERROR)
         /* if we return -1 at this point collectd
            reports a failure in configuration and
            aborts
          */
         /* if we return -1 at this point collectd
            reports a failure in configuration and
            aborts
          */
-        return (0);
-      }
+        return 0;
 
 #if COLLECT_DEBUG
       rdt_dump_cgroups();
 
 #if COLLECT_DEBUG
       rdt_dump_cgroups();
@@ -1126,21 +1141,24 @@ static int rdt_config(oconfig_item_t *ci) {
                          "Resctrl monitoring is needed for PIDs monitoring.",
               child->key);
         g_state = CONFIGURATION_ERROR;
                          "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) {
+      else if (g_rdt->num_ngroups > 0) {
+        ERROR(RDT_PLUGIN
+              ": Configuration parameter \"%s\" can be used only once.",
+              child->key);
+        g_state = CONFIGURATION_ERROR;
+      }
+
+      else if (rdt_config_ngroups(g_rdt, child) != 0)
         g_state = CONFIGURATION_ERROR;
         g_state = CONFIGURATION_ERROR;
+
+      if (g_state == CONFIGURATION_ERROR)
         /* if we return -1 at this point collectd
            reports a failure in configuration and
            aborts
          */
         return 0;
         /* if we return -1 at this point collectd
            reports a failure in configuration and
            aborts
          */
         return 0;
-      }
 
 #if COLLECT_DEBUG
       rdt_dump_ngroups();
 
 #if COLLECT_DEBUG
       rdt_dump_ngroups();
@@ -1247,8 +1265,17 @@ static void rdt_init_cores_monitoring() {
 
 static int rdt_init(void) {
 
 
 static int rdt_init(void) {
 
-  if (g_state == CONFIGURATION_ERROR)
+  if (g_state == CONFIGURATION_ERROR) {
+    if (g_rdt != NULL) {
+      if (g_rdt->cores.num_cgroups > 0)
+        rdt_free_cgroups();
+#ifdef LIBPQOS2
+      if (g_rdt->num_ngroups > 0)
+        rdt_free_ngroups(g_rdt);
+#endif
+    }
     return -1;
     return -1;
+  }
 
   int rdt_preinint_result = rdt_preinit();
   if (rdt_preinint_result != 0)
 
   int rdt_preinint_result = rdt_preinit();
   if (rdt_preinint_result != 0)