]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Change the signature of getters for time to return status of operation
authorBarbara Kaczorowska <bkjg@google.com>
Wed, 19 Aug 2020 06:54:21 +0000 (06:54 +0000)
committerBarbara Kaczorowska <bkjg@google.com>
Wed, 19 Aug 2020 07:43:20 +0000 (07:43 +0000)
src/daemon/utils_cache.c
src/daemon/utils_cache.h
src/daemon/utils_cache_test.c

index 406e21030c9d30b46c131297b67d9688a144c308..bc9e5e20441f69a308eebe372730bc77fc5f7d1c 100644 (file)
@@ -85,56 +85,6 @@ struct uc_iter_s {
 static c_avl_tree_t *cache_tree;
 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
 
-/* TODO(bkjg): change to return status and return time in pointer given as
- * argument */
-cdtime_t uc_get_last_time(char *name) {
-  cache_entry_t *ce = NULL;
-
-  pthread_mutex_lock(&cache_lock);
-
-  if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
-    assert(ce != NULL);
-
-    /* remove missing values from getval */
-    if (ce->state == STATE_MISSING) {
-      pthread_mutex_unlock(&cache_lock);
-      return UINT64_MAX;
-    } else {
-      pthread_mutex_unlock(&cache_lock);
-      return ce->last_time;
-    }
-  } else {
-    DEBUG("utils_cache: uc_get_time_of_last_update: No such value: %s", name);
-    pthread_mutex_unlock(&cache_lock);
-    return UINT64_MAX;
-  }
-}
-
-/* TODO(bkjg): change to return status and return time in pointer given as
- * argument */
-cdtime_t uc_get_last_update(char *name) {
-  cache_entry_t *ce = NULL;
-
-  pthread_mutex_lock(&cache_lock);
-
-  if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
-    assert(ce != NULL);
-
-    /* remove missing values from getval */
-    if (ce->state == STATE_MISSING) {
-      pthread_mutex_unlock(&cache_lock);
-      return UINT64_MAX;
-    } else {
-      pthread_mutex_unlock(&cache_lock);
-      return ce->last_update;
-    }
-  } else {
-    DEBUG("utils_cache: uc_get_time_of_last_update: No such value: %s", name);
-    pthread_mutex_unlock(&cache_lock);
-    return UINT64_MAX;
-  }
-}
-
 static int cache_compare(const cache_entry_t *a, const cache_entry_t *b) {
 #if COLLECT_DEBUG
   assert((a != NULL) && (b != NULL));
@@ -964,6 +914,55 @@ int uc_inc_hits(metric_t const *m, int step) {
   return ret;
 } /* int uc_inc_hits */
 
+int uc_get_last_time(char *name, cdtime_t *ret_value) {
+  cache_entry_t *ce = NULL;
+
+  pthread_mutex_lock(&cache_lock);
+
+  if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
+    assert(ce != NULL);
+
+    /* remove missing values from getval */
+    if (ce->state == STATE_MISSING) {
+      pthread_mutex_unlock(&cache_lock);
+      return -1;
+    } else {
+      *ret_value = ce->last_time;
+    }
+  } else {
+    DEBUG("utils_cache: uc_get_time_of_last_time: No such value: %s", name);
+    pthread_mutex_unlock(&cache_lock);
+    return -1;
+  }
+      pthread_mutex_unlock(&cache_lock);
+  return 0;
+}
+
+int uc_get_last_update(char *name, cdtime_t *ret_value) {
+  cache_entry_t *ce = NULL;
+
+  pthread_mutex_lock(&cache_lock);
+
+  if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
+    assert(ce != NULL);
+
+    /* remove missing values from getval */
+    if (ce->state == STATE_MISSING) {
+      pthread_mutex_unlock(&cache_lock);
+      return -1;
+    } else {
+      *ret_value = ce->last_update;
+    }
+  } else {
+    DEBUG("utils_cache: uc_get_time_of_last_update: No such value: %s", name);
+    pthread_mutex_unlock(&cache_lock);
+    return -1;
+  }
+
+  pthread_mutex_unlock(&cache_lock);
+  return 0;
+}
+
 /*
  * Iterator interface
  */
index eb97af82a0584d08884f8bec112155a884fbea22..5f69a8ab06f282c7b825caa3d0cb820949e1504a 100644 (file)
@@ -39,9 +39,6 @@
 #define STATE_ERROR 3
 #define STATE_MISSING 15
 
-cdtime_t uc_get_last_time(char *name);
-cdtime_t uc_get_last_update(char *name);
-
 int uc_init(void);
 int uc_check_timeout(void);
 int uc_update(metric_family_t const *fam);
@@ -75,6 +72,10 @@ int uc_get_history(metric_t const *m, gauge_t *ret_history, size_t num_steps);
 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
                            size_t num_steps);
 
+/* functions for tests purposes */
+int uc_get_last_time(char *name, cdtime_t *ret_value);
+int uc_get_last_update(char *name, cdtime_t *ret_value);
+
 /*
  * Iterator interface
  */
index 561bd766e338b9de7097b5a6f262800285790dd6..12e375eaa2dc44d24d42a06498614d5fcfba661e 100644 (file)
@@ -33,7 +33,7 @@
 
 static metric_family_t *create_metric_family_for_test1(char *name,
                                                        size_t num_metrics,
-                                                       gauge_t *gauges) {
+                                                       const gauge_t *gauges) {
   metric_family_t *fam = calloc(1, sizeof(metric_family_t));
   fam->name = name;
   fam->type = METRIC_TYPE_GAUGE;
@@ -57,8 +57,8 @@ static metric_family_t *create_metric_family_for_test1(char *name,
 static metric_family_t *create_metric_family_for_test2(char *name,
                                                        double *want_ret_value,
                                                        size_t num_metrics,
-                                                       counter_t *counters,
-                                                       uint64_t *times) {
+                                                       const counter_t *counters,
+                                                       const uint64_t *times) {
   metric_family_t *fam = calloc(1, sizeof(metric_family_t));
   fam->name = name;
   fam->type = METRIC_TYPE_COUNTER;
@@ -86,7 +86,7 @@ static metric_family_t *create_metric_family_for_test2(char *name,
 
 static metric_family_t *create_metric_family_for_test3(char *name,
                                                        size_t num_metrics,
-                                                       gauge_t *gauges) {
+                                                       const gauge_t *gauges) {
   metric_family_t *fam = calloc(1, sizeof(metric_family_t));
   fam->name = name;
   fam->type = METRIC_TYPE_UNTYPED;
@@ -291,11 +291,12 @@ DEF_TEST(uc_update) {
       }
     }
 
+    cdtime_t time;
     EXPECT_EQ_INT(cases[i].want_get, uc_update(cases[i].fam));
-    EXPECT_EQ_UINT64(
-        cases[i].fam->metric.ptr[cases[i].fam->metric.num - 1].time,
-        uc_get_last_time(cases[i].fam->name));
-    EXPECT_EQ_UINT64(cdtime(), uc_get_last_update(cases[i].fam->name));
+    CHECK_ZERO(uc_get_last_time(cases[i].fam->name, &time));
+    EXPECT_EQ_UINT64( cases[i].fam->metric.ptr[cases[i].fam->metric.num - 1].time, time);
+    CHECK_ZERO(uc_get_last_update(cases[i].fam->name, &time));
+    EXPECT_EQ_UINT64(cdtime(), time);
 
     CHECK_ZERO(metric_family_metric_reset(cases[i].fam));
     free(cases[i].fam);