]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
counters: add StatsDecr
authorVictor Julien <vjulien@oisf.net>
Fri, 26 Nov 2021 20:01:08 +0000 (21:01 +0100)
committerVictor Julien <vjulien@oisf.net>
Mon, 13 Jun 2022 10:58:20 +0000 (12:58 +0200)
src/counters.c
src/counters.h
src/log-stats.c
src/output-stats.h

index 46fc79caa21fc6e6a9f475da5e67b999013e3c90..6960fe06e93663e49bc8c6c965b7212d36380202 100644 (file)
@@ -181,6 +181,27 @@ void StatsIncr(ThreadVars *tv, uint16_t id)
     return;
 }
 
+/**
+ * \brief Decrements the local counter
+ *
+ * \param id  Index of the counter in the counter array
+ * \param pca Counter array that holds the local counters for this TM
+ */
+void StatsDecr(ThreadVars *tv, uint16_t id)
+{
+    StatsPrivateThreadContext *pca = &tv->perf_private_ctx;
+#if defined(UNITTESTS) || defined(FUZZ)
+    if (pca->initialized == 0)
+        return;
+#endif
+#ifdef DEBUG
+    BUG_ON((id < 1) || (id > pca->size));
+#endif
+    pca->head[id].value--;
+    pca->head[id].updates++;
+    return;
+}
+
 /**
  * \brief Sets a value of type double to the local counter
  *
@@ -199,8 +220,7 @@ void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
     BUG_ON ((id < 1) || (id > pca->size));
 #endif
 
-    if ((pca->head[id].pc->type == STATS_TYPE_MAXIMUM) &&
-            (x > pca->head[id].value)) {
+    if ((pca->head[id].pc->type == STATS_TYPE_MAXIMUM) && ((int64_t)x > pca->head[id].value)) {
         pca->head[id].value = x;
     } else if (pca->head[id].pc->type == STATS_TYPE_NORMAL) {
         pca->head[id].value = x;
@@ -666,7 +686,7 @@ static int StatsOutput(ThreadVars *tv)
      *  especially needed for the average counters */
     struct CountersMergeTable {
         int type;
-        uint64_t value;
+        int64_t value;
         uint64_t updates;
     } merge_table[max_id];
     memset(&merge_table, 0x00,
index d59cdbc5819dd9a28d834afa77d1f781fdb4e6aa..f6b6bc07a7c6a63ef2ae45576a8646f029f10510 100644 (file)
@@ -41,7 +41,7 @@ typedef struct StatsCounter_ {
     uint16_t gid;
 
     /* counter value(s): copies from the 'private' counter */
-    uint64_t value;     /**< sum of updates/increments, or 'set' value */
+    int64_t value;      /**< sum of updates/increments, or 'set' value */
     uint64_t updates;   /**< number of updates (for avg) */
 
     /* when using type STATS_TYPE_Q_FUNC this function is called once
@@ -84,7 +84,7 @@ typedef struct StatsLocalCounter_ {
     uint16_t id;
 
     /* total value of the adds/increments, or exact value in case of 'set' */
-    uint64_t value;
+    int64_t value;
 
     /* no of times the local counter has been updated */
     uint64_t updates;
@@ -124,6 +124,7 @@ uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
 void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t);
 void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t);
 void StatsIncr(struct ThreadVars_ *, uint16_t);
+void StatsDecr(struct ThreadVars_ *, uint16_t);
 
 /* utility functions */
 int StatsUpdateCounterArray(StatsPrivateThreadContext *, StatsPublicThreadContext *);
index cb257a6f190a9e5f189fc2d63b7bb26e322f20e5..ac8ea13a5aa5613f5098b72ee2df7d70a6bdaa21 100644 (file)
@@ -142,7 +142,7 @@ static int LogStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *s
                     continue;
 
                 char line[256];
-                size_t len = snprintf(line, sizeof(line), "%-45s | %-25s | %-" PRIu64 "\n",
+                size_t len = snprintf(line, sizeof(line), "%-45s | %-25s | %-" PRIi64 "\n",
                         st->tstats[u].name, st->tstats[u].tm_name, st->tstats[u].value);
 
                 /* since we can have many threads, the buffer might not be big enough.
index eba72f36e4101815ea6d5ed67f5a762bf80e8ae4..e5cd429b407cda65c4c46faa841c5a734bdfd12d 100644 (file)
@@ -29,8 +29,8 @@
 typedef struct StatsRecord_ {
     const char *name;
     const char *tm_name;
-    uint64_t value;         /**< total value */
-    uint64_t pvalue;        /**< prev value (may be higher for memuse counters) */
+    int64_t value;  /**< total value */
+    int64_t pvalue; /**< prev value (may be higher for memuse counters) */
 } StatsRecord;
 
 typedef struct StatsTable_ {