Merge remote-tracking branch 'origin/collectd-5.8'
[collectd.git] / src / intel_pmu.c
index a964a5f..d868c89 100644 (file)
@@ -192,7 +192,8 @@ static void pmu_dump_config(void) {
   DEBUG(PMU_PLUGIN ":   software_events   : %d", g_ctx.sw_events);
 
   for (size_t i = 0; i < g_ctx.hw_events_count; i++) {
-    DEBUG(PMU_PLUGIN ":   hardware_events[%zu]: %s", i, g_ctx.hw_events[i]);
+    DEBUG(PMU_PLUGIN ":   hardware_events[%" PRIsz "]: %s", i,
+          g_ctx.hw_events[i]);
   }
 }
 
@@ -204,6 +205,11 @@ static int pmu_config_hw_events(oconfig_item_t *ci) {
     return -EINVAL;
   }
 
+  if (g_ctx.hw_events) {
+    ERROR(PMU_PLUGIN ": Duplicate config for HardwareEvents.");
+    return -EINVAL;
+  }
+
   g_ctx.hw_events = calloc(ci->values_num, sizeof(char *));
   if (g_ctx.hw_events == NULL) {
     ERROR(PMU_PLUGIN ": Failed to allocate hw events.");
@@ -285,24 +291,24 @@ static void pmu_submit_counter(int cpu, char *event, counter_t value,
   plugin_dispatch_values(&vl);
 }
 
-meta_data_t *pmu_event_get_meta(struct event *e, int cpu) {
+meta_data_t *pmu_meta_data_create(const struct efd *efd) {
   meta_data_t *meta = NULL;
 
   /* create meta data only if value was scaled */
-  if (e->efd[cpu].val[1] != e->efd[cpu].val[2] && e->efd[cpu].val[2]) {
-    meta = meta_data_create();
-    if (meta == NULL) {
-      ERROR(PMU_PLUGIN ": meta_data_create failed.");
-      return NULL;
-    }
+  if (efd->val[1] == efd->val[2] || !efd->val[2]) {
+    return NULL;
+  }
 
-    meta_data_add_unsigned_int(meta, "intel_pmu:raw_count", e->efd[cpu].val[0]);
-    meta_data_add_unsigned_int(meta, "intel_pmu:time_enabled",
-                               e->efd[cpu].val[1]);
-    meta_data_add_unsigned_int(meta, "intel_pmu:time_running",
-                               e->efd[cpu].val[2]);
+  meta = meta_data_create();
+  if (meta == NULL) {
+    ERROR(PMU_PLUGIN ": meta_data_create failed.");
+    return NULL;
   }
 
+  meta_data_add_unsigned_int(meta, "intel_pmu:raw_count", efd->val[0]);
+  meta_data_add_unsigned_int(meta, "intel_pmu:time_enabled", efd->val[1]);
+  meta_data_add_unsigned_int(meta, "intel_pmu:time_running", efd->val[2]);
+
   return meta;
 }
 
@@ -329,15 +335,12 @@ static void pmu_dispatch_data(void) {
       all_value += value;
 
       /* get meta data with information about scaling */
-      meta_data_t *meta = pmu_event_get_meta(e, i);
+      meta_data_t *meta = pmu_meta_data_create(&e->efd[i]);
 
       /* dispatch per CPU value */
       pmu_submit_counter(i, e->event, value, meta);
 
-      if (meta) {
-        meta_data_destroy(meta);
-        meta = NULL;
-      }
+      meta_data_destroy(meta);
     }
 
     if (event_enabled > 0) {
@@ -404,12 +407,6 @@ static int pmu_add_hw_events(struct eventlist *el, char **e, size_t count) {
     char *s, *tmp;
     for (s = strtok_r(events, ",", &tmp); s; s = strtok_r(NULL, ",", &tmp)) {
 
-      /* Multiple events parsed in one entry */
-      if (group_events_count == 1) {
-        /* Mark previously added event as group leader */
-        el->eventlist_last->group_leader = 1;
-      }
-
       /* Allocate memory for event struct that contains array of efd structs
          for all cores */
       struct event *e =
@@ -419,19 +416,26 @@ static int pmu_add_hw_events(struct eventlist *el, char **e, size_t count) {
         return -ENOMEM;
       }
 
-      if (resolve_event(s, &e->attr) == 0) {
-        e->next = NULL;
-        if (!el->eventlist)
-          el->eventlist = e;
-        if (el->eventlist_last)
-          el->eventlist_last->next = e;
-        el->eventlist_last = e;
-        e->event = strdup(s);
-      } else {
-        DEBUG(PMU_PLUGIN ": Cannot resolve %s", s);
+      if (resolve_event(s, &e->attr) != 0) {
+        WARNING(PMU_PLUGIN ": Cannot resolve %s", s);
         sfree(e);
+        continue;
       }
 
+      /* Multiple events parsed in one entry */
+      if (group_events_count == 1) {
+        /* Mark previously added event as group leader */
+        el->eventlist_last->group_leader = 1;
+      }
+
+      e->next = NULL;
+      if (!el->eventlist)
+        el->eventlist = e;
+      if (el->eventlist_last)
+        el->eventlist_last->next = e;
+      el->eventlist_last = e;
+      e->event = strdup(s);
+
       group_events_count++;
     }