suricata
counters.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2024 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Engine stats API
25  */
26 
27 #include "suricata-common.h"
28 #include "counters.h"
29 
30 #include "suricata.h"
31 #include "threadvars.h"
32 
33 #include "output.h"
34 #include "output-json-stats.h"
35 
36 #include "util-byte.h"
37 #include "util-conf.h"
38 #include "util-hash.h"
39 #include "util-time.h"
40 
41 #include "tm-threads.h"
42 #include "util-privs.h"
43 
44 /* Time interval for syncing the local counters with the global ones */
45 #define STATS_WUT_TTS 3
46 
47 /* Time interval at which the mgmt thread o/p the stats */
48 #define STATS_MGMTT_TTS 8
49 
50 /**
51  * \brief Different kinds of qualifier that can be used to modify the behaviour
52  * of the counter to be registered
53  */
54 enum {
59 
61 };
62 
63 /**
64  * \brief per thread store of counters
65  */
66 typedef struct StatsThreadStore_ {
67  /** thread name used in output */
68  const char *name;
69 
71 
74 
75 /**
76  * \brief Holds the output interface context for the counter api
77  */
78 typedef struct StatsGlobalContext_ {
79  /** list of thread stores: one per thread plus one global */
82  int sts_cnt;
83 
85 
88 
89 static void *stats_thread_data = NULL;
90 static StatsGlobalContext *stats_ctx = NULL;
91 static time_t stats_start_time;
92 /** refresh interval in seconds */
93 static uint32_t stats_tts = STATS_MGMTT_TTS;
94 /** is the stats counter enabled? */
95 static bool stats_enabled = true;
96 
97 /**< add decoder events as stats? enabled by default */
99 const char *stats_decoder_events_prefix = "decoder.event";
100 /**< add stream events as stats? disabled by default */
101 bool stats_stream_events = false;
102 
103 static int StatsOutput(ThreadVars *tv);
104 static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext *);
105 static void StatsReleaseCounters(StatsCounter *head);
106 static int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicThreadContext *pctx);
107 
108 /** stats table is filled each interval and passed to the
109  * loggers. Initialized at first use. */
110 static StatsTable stats_table = { NULL, NULL, 0, 0, 0, {0 , 0}};
111 static SCMutex stats_table_mutex = SCMUTEX_INITIALIZER;
112 static int stats_loggers_active = 1;
113 
114 static uint16_t counters_global_id = 0;
115 
116 bool StatsEnabled(void)
117 {
118  return stats_enabled;
119 }
120 
121 static void StatsPublicThreadContextInit(StatsPublicThreadContext *t)
122 {
123  SCSpinInit(&t->lock, 0);
124 }
125 
126 static void StatsPublicThreadContextCleanup(StatsPublicThreadContext *t)
127 {
128  SCSpinLock(&t->lock);
130  SCFree(t->pc_array);
131  StatsReleaseCounters(t->head);
132  t->head = NULL;
133  SC_ATOMIC_SET(t->sync_now, false);
134  t->curr_id = 0;
135  SCSpinUnlock(&t->lock);
136  SCSpinDestroy(&t->lock);
137 }
138 
139 /**
140  * \brief Adds a value of type uint64_t to the local counter.
141  *
142  * \param id ID of the counter as set by the API
143  * \param pca Counter array that holds the local counter for this TM
144  * \param x Value to add to this local counter
145  */
147 {
148  StatsPrivateThreadContext *pca = &stats->priv;
149 #if defined (UNITTESTS) || defined (FUZZ)
150  if (pca->initialized == 0)
151  return;
152 #endif
153 #ifdef DEBUG
154  BUG_ON((id.id < 1) || (id.id > pca->size));
155 #endif
156  pca->head[id.id].v += x;
157 }
158 
159 /**
160  * \brief Increments the local counter
161  *
162  * \param id Index of the counter in the counter array
163  * \param pca Counter array that holds the local counters for this TM
164  */
166 {
167  StatsPrivateThreadContext *pca = &stats->priv;
168 #if defined (UNITTESTS) || defined (FUZZ)
169  if (pca->initialized == 0)
170  return;
171 #endif
172 #ifdef DEBUG
173  BUG_ON((id.id < 1) || (id.id > pca->size));
174 #endif
175  pca->head[id.id].v++;
176 }
177 
178 /**
179  * \brief Decrements the local counter
180  *
181  * \param stats per thread counter structure
182  * \param id Index of the counter in the counter array
183  */
185 {
186  StatsPrivateThreadContext *pca = &stats->priv;
187 #if defined(UNITTESTS) || defined(FUZZ)
188  if (pca->initialized == 0)
189  return;
190 #endif
191 #ifdef DEBUG
192  BUG_ON((id.id < 1) || (id.id > pca->size));
193 #endif
194  pca->head[id.id].v--;
195 }
196 
197 /**
198  * \brief set, so overwrite, the value of the local counter
199  *
200  * \param stats per thread counter structure
201  * \param id Index of the local counter in the counter array
202  * \param x The value to set for the counter
203  */
205 {
206  StatsPrivateThreadContext *pca = &stats->priv;
207 #if defined (UNITTESTS) || defined (FUZZ)
208  if (pca->initialized == 0)
209  return;
210 #endif
211 #ifdef DEBUG
212  BUG_ON((id.id < 1) || (id.id > pca->size));
213 #endif
214  pca->head[id.id].v = x;
215 }
216 
217 /**
218  * \brief update the value of the localmax counter
219  *
220  * \param stats per thread counter structure
221  * \param id Index of the local counter in the counter array
222  * \param x The value to set for the counter
223  */
225 {
226  StatsPrivateThreadContext *pca = &stats->priv;
227 #if defined(UNITTESTS) || defined(FUZZ)
228  if (pca->initialized == 0)
229  return;
230 #endif
231 #ifdef DEBUG
232  BUG_ON((id.id < 1) || (id.id > pca->size));
233 #endif
234 
235  if ((int64_t)x > pca->head[id.id].v) {
236  pca->head[id.id].v = x;
237  }
238 }
239 
241 {
242  StatsPrivateThreadContext *pca = &stats->priv;
243 #if defined(UNITTESTS) || defined(FUZZ)
244  if (pca->initialized == 0)
245  return;
246 #endif
247 #ifdef DEBUG
248  BUG_ON((id.id < 1) || (id.id > pca->size));
249 #endif
250 
251  pca->head[id.id].v += x;
252  pca->head[id.id + 1].v++;
253 }
254 
255 static SCConfNode *GetConfig(void)
256 {
257  SCConfNode *stats = SCConfGetNode("stats");
258  if (stats != NULL)
259  return stats;
260 
261  SCConfNode *root = SCConfGetNode("outputs");
262  SCConfNode *node = NULL;
263  if (root != NULL) {
264  TAILQ_FOREACH(node, &root->head, next) {
265  if (strcmp(node->val, "stats") == 0) {
266  return node->head.tqh_first;
267  }
268  }
269  }
270  return NULL;
271 }
272 
273 /**
274  * \brief Initializes stats context
275  */
276 static void StatsInitCtxPreOutput(void)
277 {
278  SCEnter();
279  SCConfNode *stats = GetConfig();
280  if (stats != NULL) {
281  const char *enabled = SCConfNodeLookupChildValue(stats, "enabled");
282  if (enabled != NULL && SCConfValIsFalse(enabled)) {
283  stats_enabled = false;
284  SCLogDebug("Stats module has been disabled");
285  SCReturn;
286  }
287  /* warn if we are using legacy config to enable stats */
288  SCConfNode *gstats = SCConfGetNode("stats");
289  if (gstats == NULL) {
290  SCLogWarning("global stats config is missing. "
291  "Stats enabled through legacy stats.log. "
292  "See %s/configuration/suricata-yaml.html#stats",
293  GetDocURL());
294  }
295 
296  const char *interval = SCConfNodeLookupChildValue(stats, "interval");
297  if (interval != NULL)
298  if (StringParseUint32(&stats_tts, 10, 0, interval) < 0) {
299  SCLogWarning("Invalid value for "
300  "interval: \"%s\". Resetting to %d.",
301  interval, STATS_MGMTT_TTS);
302  stats_tts = STATS_MGMTT_TTS;
303  }
304 
305  int b;
306  int ret = SCConfGetChildValueBool(stats, "decoder-events", &b);
307  if (ret) {
308  stats_decoder_events = (b == 1);
309  }
310  ret = SCConfGetChildValueBool(stats, "stream-events", &b);
311  if (ret) {
312  stats_stream_events = (b == 1);
313  }
314 
315  const char *prefix = NULL;
316  if (SCConfGet("stats.decoder-events-prefix", &prefix) != 1) {
317  prefix = "decoder.event";
318  }
320  }
321  SCReturn;
322 }
323 
324 static void StatsInitCtxPostOutput(void)
325 {
326  SCEnter();
327  /* Store the engine start time */
328  time(&stats_start_time);
329 
330  /* init the lock used by StatsThreadStore */
331  if (SCMutexInit(&stats_ctx->sts_lock, NULL) != 0) {
332  FatalError("error initializing sts mutex");
333  }
334 
335  if (stats_enabled && !OutputStatsLoggersRegistered()) {
336  stats_loggers_active = 0;
337 
338  /* if the unix command socket is enabled we do the background
339  * stats sync just in case someone runs 'dump-counters' */
340  if (!ConfUnixSocketIsEnable()) {
341  SCLogWarning("stats are enabled but no loggers are active");
342  stats_enabled = false;
343  SCReturn;
344  }
345  }
346 
347  SCReturn;
348 }
349 
350 /**
351  * \brief Releases the resources allotted to the output context of the
352  * Stats API
353  */
354 static void StatsReleaseCtx(void)
355 {
356  if (stats_ctx == NULL) {
357  SCLogDebug("Counter module has been disabled");
358  return;
359  }
360 
361  StatsThreadStore *sts = NULL;
362  StatsThreadStore *temp = NULL;
363  sts = stats_ctx->sts;
364 
365  while (sts != NULL) {
366  temp = sts->next;
367  SCFree(sts);
368  sts = temp;
369  }
370 
371  if (stats_ctx->counters_id_hash != NULL) {
372  HashTableFree(stats_ctx->counters_id_hash);
373  stats_ctx->counters_id_hash = NULL;
374  counters_global_id = 0;
375  }
376 
377  StatsPublicThreadContextCleanup(&stats_ctx->global_counter_ctx);
378  SCFree(stats_ctx);
379  stats_ctx = NULL;
380 
381  SCMutexLock(&stats_table_mutex);
382  /* free stats table */
383  if (stats_table.tstats != NULL) {
384  SCFree(stats_table.tstats);
385  stats_table.tstats = NULL;
386  }
387 
388  if (stats_table.stats != NULL) {
389  SCFree(stats_table.stats);
390  stats_table.stats = NULL;
391  }
392  memset(&stats_table, 0, sizeof(stats_table));
393  SCMutexUnlock(&stats_table_mutex);
394 }
395 
396 /**
397  * \brief management thread. This thread is responsible for writing the stats
398  *
399  * \param arg thread var
400  *
401  * \retval NULL This is the value that is always returned
402  */
403 static void *StatsMgmtThread(void *arg)
404 {
405  ThreadVars *tv_local = (ThreadVars *)arg;
406 
407  SCSetThreadName(tv_local->name);
408 
409  if (tv_local->thread_setup_flags != 0)
410  TmThreadSetupOptions(tv_local);
411 
412  /* Set the threads capability */
413  tv_local->cap_flags = 0;
414  SCDropCaps(tv_local);
415 
416  if (stats_ctx == NULL) {
417  SCLogError("Stats API not init"
418  "StatsInitCounterApi() has to be called first");
420  return NULL;
421  }
422 
424  BUG_ON(tm->ThreadInit == NULL);
425  int r = tm->ThreadInit(tv_local, NULL, &stats_thread_data);
426  if (r != 0 || stats_thread_data == NULL) {
427  SCLogError("Stats API "
428  "ThreadInit failed");
430  return NULL;
431  }
432  SCLogDebug("stats_thread_data %p", &stats_thread_data);
433 
435  bool run = TmThreadsWaitForUnpause(tv_local);
436  while (run) {
437  struct timeval cur_timev;
438  gettimeofday(&cur_timev, NULL);
439  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
440  cond_time.tv_sec += (stats_tts);
441 
442  /* wait for the set time, or until we are woken up by
443  * the shutdown procedure */
444  SCCtrlMutexLock(tv_local->ctrl_mutex);
445  while (1) {
446  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
447  break;
448  }
449  int rc = SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
450  if (rc == ETIMEDOUT || rc < 0) {
451  break;
452  }
453  }
454  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
455 
456  SCMutexLock(&stats_table_mutex);
457  StatsOutput(tv_local);
458  SCMutexUnlock(&stats_table_mutex);
459 
460  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
461  break;
462  }
463  }
464 
466  TmThreadWaitForFlag(tv_local, THV_DEINIT);
467 
468  r = tm->ThreadDeinit(tv_local, stats_thread_data);
469  if (r != TM_ECODE_OK) {
470  SCLogError("Stats Counter API "
471  "ThreadDeinit failed");
472  }
473 
474  TmThreadsSetFlag(tv_local, THV_CLOSED);
475  return NULL;
476 }
477 
479 {
480  StatsUpdateCounterArray(&stats->priv, &stats->pub);
481 }
482 
484 {
485  if (SC_ATOMIC_GET(stats->pub.sync_now)) {
486  StatsUpdateCounterArray(&stats->priv, &stats->pub);
487  }
488 }
489 
490 /**
491  * \brief Wake up thread. This thread wakes up every TTS(time to sleep) seconds
492  * and sets the flag for every ThreadVars' StatsPublicThreadContext
493  *
494  * \param arg is NULL always
495  *
496  * \retval NULL This is the value that is always returned
497  */
498 static void *StatsWakeupThread(void *arg)
499 {
500  ThreadVars *tv_local = (ThreadVars *)arg;
501 
502  SCSetThreadName(tv_local->name);
503 
504  if (tv_local->thread_setup_flags != 0)
505  TmThreadSetupOptions(tv_local);
506 
507  /* Set the threads capability */
508  tv_local->cap_flags = 0;
509  SCDropCaps(tv_local);
510 
511  if (stats_ctx == NULL) {
512  SCLogError("Stats API not init"
513  "StatsInitCounterApi() has to be called first");
515  return NULL;
516  }
517 
519  bool run = TmThreadsWaitForUnpause(tv_local);
520 
521  while (run) {
522  struct timeval cur_timev;
523  gettimeofday(&cur_timev, NULL);
524  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
525  cond_time.tv_sec += STATS_WUT_TTS;
526 
527  /* wait for the set time, or until we are woken up by
528  * the shutdown procedure */
529  SCCtrlMutexLock(tv_local->ctrl_mutex);
530  while (1) {
531  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
532  break;
533  }
534  int rc = SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
535  if (rc == ETIMEDOUT || rc < 0) {
536  break;
537  }
538  }
539  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
540 
543  while (tv != NULL) {
544  if (tv->stats.pub.head == NULL) {
545  tv = tv->next;
546  continue;
547  }
548 
549  SC_ATOMIC_SET(tv->stats.pub.sync_now, true);
550 
551  if (tv->inq != NULL) {
552  PacketQueue *q = tv->inq->pq;
553  SCMutexLock(&q->mutex_q);
554  SCCondSignal(&q->cond_q);
555  SCMutexUnlock(&q->mutex_q);
556  }
557 
558  tv = tv->next;
559  }
560 
561  /* mgt threads for flow manager */
562  tv = tv_root[TVT_MGMT];
563  while (tv != NULL) {
564  if (tv->stats.pub.head == NULL) {
565  tv = tv->next;
566  continue;
567  }
568 
569  SC_ATOMIC_SET(tv->stats.pub.sync_now, true);
570 
571  tv = tv->next;
572  }
574 
575  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
576  break;
577  }
578  }
579 
581  TmThreadWaitForFlag(tv_local, THV_DEINIT);
582  TmThreadsSetFlag(tv_local, THV_CLOSED);
583  return NULL;
584 }
585 
586 /**
587  * \brief Releases a counter
588  *
589  * \param pc Pointer to the StatsCounter to be freed
590  */
591 static void StatsReleaseCounter(StatsCounter *pc)
592 {
593  if (pc != NULL) {
594  SCFree(pc);
595  }
596 }
597 
598 /**
599  * \brief Registers a counter.
600  *
601  * \param name Name of the counter, to be registered
602  * \param pctx StatsPublicThreadContext for this tm-tv instance
603  * \param type_q Qualifier describing the type of counter to be registered
604  *
605  * \retval the counter id for the newly registered counter, or the already
606  * present counter on success
607  * \retval 0 on failure
608  */
609 static uint16_t StatsRegisterQualifiedCounter(
610  const char *name, StatsPublicThreadContext *pctx, int type_q, uint64_t (*Func)(void))
611 {
612  StatsCounter **head = &pctx->head;
613  StatsCounter *temp = NULL;
614  StatsCounter *prev = NULL;
615  StatsCounter *pc = NULL;
616 
617  if (name == NULL || pctx == NULL) {
618  SCLogDebug("Counter name, StatsPublicThreadContext NULL");
619  return 0;
620  }
621 
622  temp = prev = *head;
623  while (temp != NULL) {
624  prev = temp;
625 
626  if (strcmp(name, temp->name) == 0) {
627  break;
628  }
629 
630  temp = temp->next;
631  }
632 
633  /* We already have a counter registered by this name */
634  if (temp != NULL)
635  return(temp->id);
636 
637  /* if we reach this point we don't have a counter registered by this name */
638  if ((pc = SCCalloc(1, sizeof(StatsCounter))) == NULL)
639  return 0;
640 
641  /* assign a unique id to this StatsCounter. The id is local to this
642  * thread context. Please note that the id start from 1, and not 0 */
643  pc->id = ++(pctx->curr_id);
644 
645  /* for AVG counters we use 2 indices into the tables: one for values,
646  * the other to track updates. */
647  if (type_q == STATS_TYPE_AVERAGE)
648  ++(pctx->curr_id);
649  pc->name = name;
650 
651  /* Precalculate the short name */
652  if (strrchr(name, '.') != NULL) {
653  pc->short_name = &name[strrchr(name, '.') - name + 1];
654  }
655 
656  pc->type = type_q;
657  pc->Func = Func;
658 
659  /* we now add the counter to the list */
660  if (prev == NULL)
661  *head = pc;
662  else
663  prev->next = pc;
664 
665  return pc->id;
666 }
667 
668 /**
669  * \brief The output interface for the Stats API
670  */
671 static int StatsOutput(ThreadVars *tv)
672 {
673  const StatsThreadStore *sts = NULL;
674  void *td = stats_thread_data;
675 
676  if (counters_global_id == 0)
677  return -1;
678 
679  if (stats_table.nstats == 0) {
680  StatsThreadRegister("Global", &stats_ctx->global_counter_ctx);
681 
682  uint32_t nstats = counters_global_id;
683 
684  stats_table.nstats = nstats;
685  stats_table.stats = SCCalloc(stats_table.nstats, sizeof(StatsRecord));
686  if (stats_table.stats == NULL) {
687  stats_table.nstats = 0;
688  SCLogError("could not alloc memory for stats");
689  return -1;
690  }
691 
692  stats_table.ntstats = stats_ctx->sts_cnt;
693  uint32_t array_size = stats_table.nstats * sizeof(StatsRecord);
694  stats_table.tstats = SCCalloc(stats_table.ntstats, array_size);
695  if (stats_table.tstats == NULL) {
696  stats_table.ntstats = 0;
697  SCLogError("could not alloc memory for stats");
698  return -1;
699  }
700 
701  stats_table.start_time = stats_start_time;
702  }
703 
704  const uint16_t max_id = counters_global_id;
705  if (max_id == 0)
706  return -1;
707 
708  /** temporary local table to merge the per thread counters,
709  * especially needed for the average counters */
710  struct CountersMergeTable {
711  int type;
712  int64_t value;
713  uint64_t updates;
714  } merge_table[max_id];
715  memset(&merge_table, 0x00,
716  max_id * sizeof(struct CountersMergeTable));
717 
718  int thread = stats_ctx->sts_cnt - 1;
719  StatsRecord *table = stats_table.stats;
720 
721  /* Loop through the thread counter stores. The global counters
722  * are in a separate store inside this list. */
723  sts = stats_ctx->sts;
724  SCLogDebug("sts %p", sts);
725  while (sts != NULL) {
726  DEBUG_VALIDATE_BUG_ON(thread < 0);
727 
728  SCLogDebug("Thread %d %s ctx %p", thread, sts->name, sts->ctx);
729 
730  /* temporary table for quickly storing the counters for this
731  * thread store, so that we can post process them outside
732  * of the thread store lock */
733  struct CountersMergeTable thread_table[max_id];
734  memset(&thread_table, 0x00,
735  max_id * sizeof(struct CountersMergeTable));
736 
737  StatsLocalCounter thread_table_from_private[max_id];
738  memset(&thread_table_from_private, 0x00, max_id * sizeof(StatsLocalCounter));
739 
740  /* copy private table to a local variable to loop it w/o lock */
741  bool skip = false;
742  SCSpinLock(&sts->ctx->lock);
743  uint16_t table_size = sts->ctx->curr_id + 1;
744  if (sts->ctx->copy_of_private == NULL) {
745  skip = true;
746  } else {
747  memcpy(&thread_table_from_private, sts->ctx->copy_of_private,
748  table_size * sizeof(StatsLocalCounter));
749  }
750  SCSpinUnlock(&sts->ctx->lock);
751  if (skip)
752  goto next;
753 
754  /* loop counters and handle them. This includes the global counters, which
755  * access the StatsCounters but don't modify them. */
756  for (uint16_t i = 1; i < table_size; i++) {
757  const StatsCounter *pc = sts->ctx->pc_array[i];
758  thread_table[pc->gid].type = pc->type;
759 
760  table[pc->gid].name = pc->name;
761  table[pc->gid].short_name = pc->short_name;
762 
763  switch (pc->type) {
764  case STATS_TYPE_FUNC:
765  if (pc->Func != NULL)
766  thread_table[pc->gid].value = pc->Func();
767  break;
768  case STATS_TYPE_AVERAGE:
769  thread_table[pc->gid].value = thread_table_from_private[i].v;
770  thread_table[pc->gid].updates = thread_table_from_private[i + 1].v;
771  /* skip updates row */
772  i++;
773  break;
774  default:
775  SCLogDebug("Counter %s (%u:%u) value %" PRIu64, pc->name, pc->id, pc->gid,
776  thread_table_from_private[i].v);
777 
778  thread_table[pc->gid].value = thread_table_from_private[i].v;
779  break;
780  }
781  }
782 
783  /* update merge table */
784  for (uint16_t c = 0; c < max_id; c++) {
785  struct CountersMergeTable *e = &thread_table[c];
786  /* thread only sets type if it has a counter
787  * of this type. */
788  if (e->type == 0)
789  continue;
790 
791  switch (e->type) {
792  case STATS_TYPE_MAXIMUM:
793  if (e->value > merge_table[c].value)
794  merge_table[c].value = e->value;
795  break;
796  case STATS_TYPE_FUNC:
797  merge_table[c].value = e->value;
798  break;
799  case STATS_TYPE_AVERAGE:
800  default:
801  merge_table[c].value += e->value;
802  break;
803  }
804  merge_table[c].updates += e->updates;
805  merge_table[c].type = e->type;
806  }
807 
808  /* update per thread stats table */
809  for (uint16_t c = 0; c < max_id; c++) {
810  struct CountersMergeTable *e = &thread_table[c];
811  /* thread only sets type if it has a counter
812  * of this type. */
813  if (e->type == 0)
814  continue;
815 
816  uint32_t offset = (thread * stats_table.nstats) + c;
817  StatsRecord *r = &stats_table.tstats[offset];
818  /* xfer previous value to pvalue and reset value */
819  r->pvalue = r->value;
820  r->value = 0;
821  r->name = table[c].name;
822  r->short_name = table[c].short_name;
823  r->tm_name = sts->name;
824 
825  switch (e->type) {
826  case STATS_TYPE_AVERAGE:
827  if (e->value > 0 && e->updates > 0) {
828  r->value = (uint64_t)(e->value / e->updates);
829  }
830  break;
831  default:
832  r->value = e->value;
833  break;
834  }
835  }
836 
837  next:
838  sts = sts->next;
839  thread--;
840  }
841 
842  /* transfer 'merge table' to final stats table */
843  for (uint16_t x = 0; x < max_id; x++) {
844  /* xfer previous value to pvalue and reset value */
845  table[x].pvalue = table[x].value;
846  table[x].value = 0;
847  table[x].tm_name = "Total";
848 
849  struct CountersMergeTable *m = &merge_table[x];
850  switch (m->type) {
851  case STATS_TYPE_MAXIMUM:
852  if (m->value > table[x].value)
853  table[x].value = m->value;
854  break;
855  case STATS_TYPE_AVERAGE:
856  if (m->value > 0 && m->updates > 0) {
857  table[x].value = (uint64_t)(m->value / m->updates);
858  }
859  break;
860  default:
861  table[x].value += m->value;
862  break;
863  }
864  }
865 
866  /* invoke logger(s) */
867  if (stats_loggers_active) {
868  OutputStatsLog(tv, td, &stats_table);
869  }
870  return 1;
871 }
872 
873 #ifdef BUILD_UNIX_SOCKET
874 /** \brief callback for getting stats into unix socket
875  */
876 TmEcode StatsOutputCounterSocket(json_t *cmd,
877  json_t *answer, void *data)
878 {
879  json_t *message = NULL;
880  TmEcode r = TM_ECODE_OK;
881 
882  if (!stats_enabled) {
883  r = TM_ECODE_FAILED;
884  message = json_string("stats are disabled in the config");
885  } else {
886  SCMutexLock(&stats_table_mutex);
887  if (stats_table.start_time == 0) {
888  r = TM_ECODE_FAILED;
889  message = json_string("stats not yet synchronized");
890  } else {
891  message = StatsToJSON(&stats_table, JSON_STATS_TOTALS|JSON_STATS_THREADS);
892  }
893  SCMutexUnlock(&stats_table_mutex);
894  }
895  json_object_set_new(answer, "message", message);
896  return r;
897 }
898 #endif /* BUILD_UNIX_SOCKET */
899 
900 static void StatsLogSummary(void)
901 {
902  if (!stats_enabled) {
903  return;
904  }
905  uint64_t alerts = 0;
906  SCMutexLock(&stats_table_mutex);
907  if (stats_table.start_time != 0) {
908  const StatsTable *st = &stats_table;
909  for (uint32_t u = 0; u < st->nstats; u++) {
910  const char *name = st->stats[u].name;
911  if (name == NULL || strcmp(name, "detect.alert") != 0)
912  continue;
913  alerts = st->stats[u].value;
914  break;
915  }
916  }
917  SCMutexUnlock(&stats_table_mutex);
918  SCLogInfo("Alerts: %"PRIu64, alerts);
919 }
920 
921 /**
922  * \brief Initializes the perf counter api. Things are hard coded currently.
923  * More work to be done when we implement multiple interfaces
924  */
925 void StatsInit(void)
926 {
927  BUG_ON(stats_ctx != NULL);
928  if ((stats_ctx = SCCalloc(1, sizeof(StatsGlobalContext))) == NULL) {
929  FatalError("Fatal error encountered in StatsInitCtx. Exiting...");
930  }
931 
932  StatsPublicThreadContextInit(&stats_ctx->global_counter_ctx);
933 }
934 
936 {
937  StatsInitCtxPreOutput();
938 }
939 
941 {
942  StatsInitCtxPostOutput();
943 }
944 
945 
946 /**
947  * \brief Spawns the wakeup, and the management thread used by the stats api
948  *
949  * The threads use the condition variable in the thread vars to control
950  * their wait loops to make sure the main thread can quickly kill them.
951  */
953 {
954  SCEnter();
955 
956  if (!stats_enabled) {
957  SCReturn;
958  }
959 
960  ThreadVars *tv_wakeup = NULL;
961  ThreadVars *tv_mgmt = NULL;
962 
963  /* spawn the stats wakeup thread */
965  StatsWakeupThread, 1);
966  if (tv_wakeup == NULL) {
967  FatalError("TmThreadCreateMgmtThread "
968  "failed");
969  }
970 
971  if (TmThreadSpawn(tv_wakeup) != 0) {
972  FatalError("TmThreadSpawn failed for "
973  "StatsWakeupThread");
974  }
975 
976  /* spawn the stats mgmt thread */
978  StatsMgmtThread, 1);
979  if (tv_mgmt == NULL) {
980  FatalError("TmThreadCreateMgmtThread failed");
981  }
982 
983  if (TmThreadSpawn(tv_mgmt) != 0) {
984  FatalError("TmThreadSpawn failed for "
985  "StatsWakeupThread");
986  }
987 
988  SCReturn;
989 }
990 
991 /**
992  * \brief Registers a normal, unqualified counter
993  *
994  * \param name Name of the counter, to be registered
995  * \param tv Pointer to the ThreadVars instance for which the counter would
996  * be registered
997  *
998  * \retval id Counter id for the newly registered counter, or the already
999  * present counter
1000  */
1002 {
1003  uint16_t id = StatsRegisterQualifiedCounter(name, &stats->pub, STATS_TYPE_NORMAL, NULL);
1004  StatsCounterId s = { .id = id };
1005  return s;
1006 }
1007 
1008 /**
1009  * \brief Registers a counter, whose value holds the average of all the values
1010  * assigned to it.
1011  *
1012  * \param name Name of the counter, to be registered
1013  * \param tv Pointer to the ThreadVars instance for which the counter would
1014  * be registered
1015  *
1016  * \retval id Counter id for the newly registered counter, or the already
1017  * present counter
1018  */
1020 {
1021  uint16_t id = StatsRegisterQualifiedCounter(name, &stats->pub, STATS_TYPE_AVERAGE, NULL);
1022  StatsCounterAvgId s = { .id = id };
1023  return s;
1024 }
1025 
1026 /**
1027  * \brief Registers a counter, whose value holds the maximum of all the values
1028  * assigned to it.
1029  *
1030  * \param name Name of the counter, to be registered
1031  * \param tv Pointer to the ThreadVars instance for which the counter would
1032  * be registered
1033  *
1034  * \retval the counter id for the newly registered counter, or the already
1035  * present counter
1036  */
1038 {
1039  uint16_t id = StatsRegisterQualifiedCounter(name, &stats->pub, STATS_TYPE_MAXIMUM, NULL);
1040  StatsCounterMaxId s = { .id = id };
1041  return s;
1042 }
1043 
1044 /**
1045  * \brief Registers a counter, which represents a global value
1046  *
1047  * \param name Name of the counter, to be registered
1048  * \param Func Function Pointer returning a uint64_t
1049  *
1050  * \retval id Counter id for the newly registered counter, or the already
1051  * present counter
1052  */
1053 StatsCounterGlobalId StatsRegisterGlobalCounter(const char *name, uint64_t (*Func)(void))
1054 {
1055  StatsCounterGlobalId s = { .id = 0 };
1056 #if defined (UNITTESTS) || defined (FUZZ)
1057  if (stats_ctx == NULL)
1058  return s;
1059 #else
1060  BUG_ON(stats_ctx == NULL);
1061 #endif
1062  uint16_t id = StatsRegisterQualifiedCounter(
1063  name, &(stats_ctx->global_counter_ctx), STATS_TYPE_FUNC, Func);
1064  s.id = id;
1065  return s;
1066 }
1067 
1068 typedef struct CountersIdType_ {
1069  uint16_t id;
1070  const char *string;
1072 
1073 static uint32_t CountersIdHashFunc(HashTable *ht, void *data, uint16_t datalen)
1074 {
1075  CountersIdType *t = (CountersIdType *)data;
1076  uint32_t hash = 0;
1077  size_t len = strlen(t->string);
1078 
1079  for (size_t i = 0; i < len; i++)
1080  hash += u8_tolower((unsigned char)t->string[i]);
1081 
1082  hash = hash % ht->array_size;
1083  return hash;
1084 }
1085 
1086 static char CountersIdHashCompareFunc(void *data1, uint16_t datalen1,
1087  void *data2, uint16_t datalen2)
1088 {
1089  CountersIdType *t1 = (CountersIdType *)data1;
1090  CountersIdType *t2 = (CountersIdType *)data2;
1091  size_t len1 = 0;
1092  size_t len2 = 0;
1093 
1094  if (t1 == NULL || t2 == NULL)
1095  return 0;
1096 
1097  if (t1->string == NULL || t2->string == NULL)
1098  return 0;
1099 
1100  len1 = strlen(t1->string);
1101  len2 = strlen(t2->string);
1102 
1103  if (len1 == len2 && memcmp(t1->string, t2->string, len1) == 0) {
1104  return 1;
1105  }
1106 
1107  return 0;
1108 }
1109 
1110 static void CountersIdHashFreeFunc(void *data)
1111 {
1112  SCFree(data);
1113 }
1114 
1115 static int StatsThreadSetupPublic(StatsPublicThreadContext *pctx)
1116 {
1117  size_t array_size = pctx->curr_id + 1;
1118  pctx->pc_array = SCCalloc(array_size, sizeof(StatsCounter *));
1119  if (pctx->pc_array == NULL) {
1120  return -1;
1121  }
1122  for (StatsCounter *pc = pctx->head; pc != NULL; pc = pc->next) {
1123  SCLogDebug("pc %s gid %u id %u", pc->name, pc->gid, pc->id);
1124  BUG_ON(pctx->pc_array[pc->id] != NULL);
1125  pctx->pc_array[pc->id] = pc;
1126  }
1127 
1128  SCLogDebug("array_size %u memory %" PRIu64, (uint32_t)array_size,
1129  (uint64_t)(array_size * sizeof(StatsLocalCounter)));
1130  pctx->copy_of_private = SCCalloc(array_size, sizeof(StatsLocalCounter));
1131  if (pctx->copy_of_private == NULL) {
1132  return -1;
1133  }
1134  return 0;
1135 }
1136 
1137 /** \internal
1138  * \brief Adds a TM to the clubbed TM table. Multiple instances of the same TM
1139  * are stacked together in a PCTMI container.
1140  *
1141  * \param tm_name Name of the tm to be added to the table
1142  * \param pctx StatsPublicThreadContext associated with the TM tm_name
1143  *
1144  * \retval 1 on success, 0 on failure
1145  */
1146 static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext *pctx)
1147 {
1148  if (stats_ctx == NULL) {
1149  return 1;
1150  }
1151 
1152  if (thread_name == NULL || pctx == NULL) {
1153  SCLogDebug("supplied argument(s) to StatsThreadRegister NULL");
1154  return 0;
1155  }
1156 
1157  SCMutexLock(&stats_ctx->sts_lock);
1158  SCLogDebug("thread %s", thread_name);
1159  if (stats_ctx->counters_id_hash == NULL) {
1160  stats_ctx->counters_id_hash = HashTableInit(256, CountersIdHashFunc,
1161  CountersIdHashCompareFunc,
1162  CountersIdHashFreeFunc);
1163  if (stats_ctx->counters_id_hash == NULL) {
1164  SCMutexUnlock(&stats_ctx->sts_lock);
1165  return 0;
1166  }
1167  }
1168  StatsCounter *pc = pctx->head;
1169  while (pc != NULL) {
1170  CountersIdType t = { 0, pc->name }, *id = NULL;
1171  id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t));
1172  if (id == NULL) {
1173  id = SCCalloc(1, sizeof(*id));
1174  BUG_ON(id == NULL);
1175  id->id = counters_global_id++;
1176  id->string = pc->name;
1177  int r = HashTableAdd(stats_ctx->counters_id_hash, id, sizeof(*id));
1178  DEBUG_VALIDATE_BUG_ON(r < 0);
1179  if (r < 0) {
1180  SCMutexUnlock(&stats_ctx->sts_lock);
1181  return 0;
1182  }
1183  }
1184  pc->gid = id->id;
1185  pc = pc->next;
1186  }
1187 
1188  if (StatsThreadSetupPublic(pctx) != 0) {
1189  SCLogDebug("failed to setup StatsThreadSetupPublic");
1190  SCMutexUnlock(&stats_ctx->sts_lock);
1191  return 0;
1192  }
1193 
1194  StatsThreadStore *temp = NULL;
1195  if ((temp = SCCalloc(1, sizeof(StatsThreadStore))) == NULL) {
1196  SCMutexUnlock(&stats_ctx->sts_lock);
1197  return 0;
1198  }
1199 
1200  temp->ctx = pctx;
1201  temp->name = thread_name;
1202 
1203  temp->next = stats_ctx->sts;
1204  stats_ctx->sts = temp;
1205  stats_ctx->sts_cnt++;
1206  SCLogDebug("stats_ctx->sts %p", stats_ctx->sts);
1207 
1208  SCMutexUnlock(&stats_ctx->sts_lock);
1209  return 1;
1210 }
1211 
1212 /** \internal
1213  * \brief Returns a counter array for all counters registered for this tm
1214  * instance
1215  *
1216  * \param pctx Pointer to the tv's StatsPublicThreadContext
1217  *
1218  * \retval pca Pointer to a counter-array for all counter of this tm instance
1219  * on success; NULL on failure
1220  */
1221 static int StatsGetAllCountersArray(
1223 {
1224  if (pctx == NULL || private == NULL)
1225  return -1;
1226 
1227  private->size = pctx->curr_id + 1;
1228 
1229  private->head = SCCalloc(private->size, sizeof(StatsLocalCounter));
1230  if (private->head == NULL) {
1231  return -1;
1232  }
1233 
1234  private->initialized = 1;
1235  return 0;
1236 }
1237 
1238 int StatsSetupPrivate(StatsThreadContext *stats, const char *thread_name)
1239 {
1240  int r = StatsGetAllCountersArray(&stats->pub, &stats->priv);
1241  if (r < 0) {
1242  return -1;
1243  }
1244 
1245  r = StatsThreadRegister(thread_name, &stats->pub);
1246  if (r != 1) {
1247  return -2;
1248  }
1249  return 0;
1250 }
1251 
1252 static void StatsThreadInitPublic(StatsPublicThreadContext *pctx)
1253 {
1254  memset(pctx, 0x00, sizeof(*pctx));
1255  SCSpinInit(&pctx->lock, 0);
1256 }
1257 
1259 {
1260  StatsThreadInitPublic(&stats->pub);
1261 }
1262 
1263 /**
1264  * \brief the private stats store with the public stats store
1265  *
1266  * \param pca Pointer to the StatsPrivateThreadContext
1267  * \param pctx Pointer the tv's StatsPublicThreadContext
1268  *
1269  * \retval 1 on success
1270  * \retval -1 on error
1271  */
1272 static int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicThreadContext *pctx)
1273 {
1274 
1275  if (pca == NULL || pctx == NULL) {
1276  SCLogDebug("pca or pctx is NULL inside StatsUpdateCounterArray");
1277  return -1;
1278  }
1279 
1280  if (pca->size > 0 && pctx->copy_of_private != NULL) {
1281  /* copy the whole table under lock to the public section
1282  * and release the lock. The stats thread will copy it from
1283  * there. */
1284  SCSpinLock(&pctx->lock);
1285  memcpy(pctx->copy_of_private, pca->head, pca->size * sizeof(StatsLocalCounter));
1286  SCSpinUnlock(&pctx->lock);
1287  }
1288  SC_ATOMIC_SET(pctx->sync_now, false);
1289  return 1;
1290 }
1291 
1292 /**
1293  * \brief Get the value of the local copy of the counter that hold this id.
1294  *
1295  * \param tv threadvars
1296  * \param id The counter id.
1297  *
1298  * \retval 0 on success.
1299  * \retval -1 on error.
1300  */
1302 {
1303  StatsPrivateThreadContext *pca = &stats->priv;
1304 #ifdef DEBUG
1305  BUG_ON((id.id < 1) || (id.id > pca->size));
1306 #endif
1307  return pca->head[id.id].v;
1308 }
1309 
1310 /**
1311  * \brief Releases the resources allotted by the Stats API
1312  */
1314 {
1315  StatsLogSummary();
1316  StatsReleaseCtx();
1317 }
1318 
1319 /**
1320  * \brief Releases counters
1321  *
1322  * \param head Pointer to the head of the list of perf counters that have to
1323  * be freed
1324  */
1325 static void StatsReleaseCounters(StatsCounter *head)
1326 {
1327  StatsCounter *pc = NULL;
1328 
1329  while (head != NULL) {
1330  pc = head;
1331  head = head->next;
1332  StatsReleaseCounter(pc);
1333  }
1334 }
1335 
1336 /** \internal
1337  * \brief Releases the StatsPrivateThreadContext allocated by the user,
1338  * for storing and updating local counter values
1339  *
1340  * \param pca Pointer to the StatsPrivateThreadContext
1341  */
1342 static void StatsReleasePrivateThreadContext(StatsPrivateThreadContext *pca)
1343 {
1344  if (pca != NULL) {
1345  if (pca->head != NULL) {
1346  SCFree(pca->head);
1347  pca->head = NULL;
1348  pca->size = 0;
1349  }
1350  pca->initialized = 0;
1351  }
1352 }
1353 
1355 {
1356  StatsPublicThreadContextCleanup(&stats->pub);
1357  StatsReleasePrivateThreadContext(&stats->priv);
1358 }
1359 
1360 /*----------------------------------Unit_Tests--------------------------------*/
1361 
1362 #ifdef UNITTESTS
1363 /** \internal
1364  * \brief Registers a normal, unqualified counter
1365  *
1366  * \param name Name of the counter, to be registered
1367  * \param type Datatype of this counter variable
1368  * \param pctx StatsPublicThreadContext corresponding to the tm_name key under which the
1369  * key has to be registered
1370  *
1371  * \retval id Counter id for the newly registered counter, or the already
1372  * present counter
1373  */
1374 static StatsCounterId RegisterCounter(
1375  const char *name, const char *tm_name, StatsPublicThreadContext *pctx)
1376 {
1377  uint16_t id = StatsRegisterQualifiedCounter(name, pctx, STATS_TYPE_NORMAL, NULL);
1378  StatsCounterId s = { .id = id };
1379  return s;
1380 }
1381 
1382 static int StatsTestCounterReg02(void)
1383 {
1385  StatsThreadInitPublic(&pctx);
1386 
1387  StatsCounterId id = RegisterCounter(NULL, NULL, &pctx);
1388  FAIL_IF_NOT(id.id == 0);
1389  PASS;
1390 }
1391 
1392 static int StatsTestCounterReg03(void)
1393 {
1395  StatsThreadInitPublic(&pctx);
1396 
1397  StatsCounterId id = RegisterCounter("t1", "c1", &pctx);
1398  FAIL_IF_NOT(id.id == 1);
1399 
1400  StatsReleaseCounters(pctx.head);
1401  PASS;
1402 }
1403 
1404 static int StatsTestCounterReg04(void)
1405 {
1407  StatsThreadInitPublic(&pctx);
1408 
1409  StatsCounterId c1 = RegisterCounter("t1", "c1", &pctx);
1410  FAIL_IF_NOT(c1.id == 1);
1411  StatsCounterId c2 = RegisterCounter("t2", "c2", &pctx);
1412  FAIL_IF_NOT(c2.id == 2);
1413  StatsCounterId c3 = RegisterCounter("t3", "c3", &pctx);
1414  FAIL_IF_NOT(c3.id == 3);
1415  StatsCounterId id = RegisterCounter("t1", "c1", &pctx);
1416  FAIL_IF_NOT(id.id == 1);
1417 
1418  StatsReleaseCounters(pctx.head);
1419  PASS;
1420 }
1421 
1422 static int StatsTestGetCntArray05(void)
1423 {
1424  ThreadVars tv;
1425  memset(&tv, 0, sizeof(ThreadVars));
1427  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1428  FAIL_IF(c1.id != 1);
1429  int r = StatsGetAllCountersArray(NULL, &tv.stats.priv);
1430  FAIL_IF_NOT(r == -1);
1432  PASS;
1433 }
1434 
1435 static int StatsTestGetCntArray06(void)
1436 {
1437  ThreadVars tv;
1438  memset(&tv, 0, sizeof(ThreadVars));
1440  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1441  FAIL_IF(c1.id != 1);
1442  StatsThreadSetupPublic(&tv.stats.pub);
1443  int r = StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1444  FAIL_IF_NOT(r == 0);
1446  PASS;
1447 }
1448 
1449 static int StatsTestCntArraySize07(void)
1450 {
1451  ThreadVars tv;
1452  memset(&tv, 0, sizeof(ThreadVars));
1454  StatsPrivateThreadContext *pca = NULL;
1455 
1456  StatsCounterId id1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1457  StatsCounterId id2 = RegisterCounter("t2", "c2", &tv.stats.pub);
1458 
1459  StatsThreadSetupPublic(&tv.stats.pub);
1460  StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1461  pca = &tv.stats.priv;
1462 
1463  StatsCounterIncr(&tv.stats, id1);
1464  StatsCounterIncr(&tv.stats, id2);
1465 
1466  FAIL_IF_NOT(pca->size == 3);
1467 
1469  PASS;
1470 }
1471 
1472 static int StatsTestUpdateCounter08(void)
1473 {
1474  ThreadVars tv;
1475  memset(&tv, 0, sizeof(ThreadVars));
1477  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1478  StatsThreadSetupPublic(&tv.stats.pub);
1479  StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1481 
1482  StatsCounterIncr(&tv.stats, c1);
1483  StatsCounterAddI64(&tv.stats, c1, 100);
1484  FAIL_IF_NOT(pca->head[c1.id].v == 101);
1485 
1487  PASS;
1488 }
1489 
1490 static int StatsTestUpdateCounter09(void)
1491 {
1492  ThreadVars tv;
1493  memset(&tv, 0, sizeof(ThreadVars));
1495 
1496  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1497  RegisterCounter("t2", "c2", &tv.stats.pub);
1498  RegisterCounter("t3", "c3", &tv.stats.pub);
1499  RegisterCounter("t4", "c4", &tv.stats.pub);
1500  StatsCounterId c5 = RegisterCounter("t5", "c5", &tv.stats.pub);
1501 
1502  StatsThreadSetupPublic(&tv.stats.pub);
1503  StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1505 
1506  StatsCounterIncr(&tv.stats, c5);
1507  StatsCounterAddI64(&tv.stats, c5, 100);
1508 
1509  FAIL_IF_NOT(pca->head[c1.id].v == 0);
1510  FAIL_IF_NOT(pca->head[c5.id].v == 101);
1511 
1513  PASS;
1514 }
1515 
1516 static int StatsTestUpdateGlobalCounter10(void)
1517 {
1518  ThreadVars tv;
1519  memset(&tv, 0, sizeof(ThreadVars));
1521 
1522  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1523  StatsCounterId c2 = RegisterCounter("t2", "c2", &tv.stats.pub);
1524  StatsCounterId c3 = RegisterCounter("t3", "c3", &tv.stats.pub);
1525 
1526  StatsThreadSetupPublic(&tv.stats.pub);
1527  StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1529 
1530  StatsCounterIncr(&tv.stats, c1);
1531  StatsCounterAddI64(&tv.stats, c2, 100);
1532  StatsCounterIncr(&tv.stats, c3);
1533  StatsCounterAddI64(&tv.stats, c3, 100);
1534 
1535  StatsUpdateCounterArray(pca, &tv.stats.pub);
1536 
1538  FAIL_IF_NOT(100 == tv.stats.pub.copy_of_private[c2.id].v);
1539  FAIL_IF_NOT(101 == tv.stats.pub.copy_of_private[c3.id].v);
1540 
1542  PASS;
1543 }
1544 
1545 static int StatsTestCounterValues11(void)
1546 {
1547  ThreadVars tv;
1548  memset(&tv, 0, sizeof(ThreadVars));
1550 
1551  StatsCounterId c1 = RegisterCounter("t1", "c1", &tv.stats.pub);
1552  StatsCounterId c2 = RegisterCounter("t2", "c2", &tv.stats.pub);
1553  StatsCounterId c3 = RegisterCounter("t3", "c3", &tv.stats.pub);
1554  StatsCounterId c4 = RegisterCounter("t4", "c4", &tv.stats.pub);
1555 
1556  StatsThreadSetupPublic(&tv.stats.pub);
1557  StatsGetAllCountersArray(&tv.stats.pub, &tv.stats.priv);
1559 
1560  StatsCounterIncr(&tv.stats, c1);
1561  StatsCounterAddI64(&tv.stats, c2, 256);
1562  StatsCounterAddI64(&tv.stats, c3, 257);
1563  StatsCounterAddI64(&tv.stats, c4, 16843024);
1564 
1565  StatsUpdateCounterArray(pca, &tv.stats.pub);
1566 
1568  FAIL_IF_NOT(256 == tv.stats.pub.copy_of_private[c2.id].v);
1569  FAIL_IF_NOT(257 == tv.stats.pub.copy_of_private[c3.id].v);
1570  FAIL_IF_NOT(16843024 == tv.stats.pub.copy_of_private[c4.id].v);
1571 
1573  PASS;
1574 }
1575 
1576 #endif
1577 
1579 {
1580 #ifdef UNITTESTS
1581  UtRegisterTest("StatsTestCounterReg02", StatsTestCounterReg02);
1582  UtRegisterTest("StatsTestCounterReg03", StatsTestCounterReg03);
1583  UtRegisterTest("StatsTestCounterReg04", StatsTestCounterReg04);
1584  UtRegisterTest("StatsTestGetCntArray05", StatsTestGetCntArray05);
1585  UtRegisterTest("StatsTestGetCntArray06", StatsTestGetCntArray06);
1586  UtRegisterTest("StatsTestCntArraySize07", StatsTestCntArraySize07);
1587  UtRegisterTest("StatsTestUpdateCounter08", StatsTestUpdateCounter08);
1588  UtRegisterTest("StatsTestUpdateCounter09", StatsTestUpdateCounter09);
1589  UtRegisterTest("StatsTestUpdateGlobalCounter10",
1590  StatsTestUpdateGlobalCounter10);
1591  UtRegisterTest("StatsTestCounterValues11", StatsTestCounterValues11);
1592 #endif
1593 }
StatsPublicThreadContext_::lock
SCSpinlock lock
Definition: counters.h:100
util-byte.h
tm-threads.h
FROM_TIMEVAL
#define FROM_TIMEVAL(timev)
initialize a 'struct timespec' from a 'struct timeval'.
Definition: util-time.h:124
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition: counters.c:1313
StatsRegisterGlobalCounter
StatsCounterGlobalId StatsRegisterGlobalCounter(const char *name, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1053
thread_name_counter_wakeup
const char * thread_name_counter_wakeup
Definition: runmodes.c:76
StatsTable_::ntstats
uint32_t ntstats
Definition: output-stats.h:43
len
uint8_t len
Definition: app-layer-dnp3.h:2
StatsCounterMaxUpdateI64
void StatsCounterMaxUpdateI64(StatsThreadContext *stats, StatsCounterMaxId id, int64_t x)
update the value of the localmax counter
Definition: counters.c:224
TmThreadSpawn
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
Definition: tm-threads.c:1702
SCSpinDestroy
#define SCSpinDestroy
Definition: threads-debug.h:240
STATS_TYPE_MAXIMUM
@ STATS_TYPE_MAXIMUM
Definition: counters.c:57
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *tv)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:863
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *stats)
Definition: counters.c:483
StatsThreadStore_
per thread store of counters
Definition: counters.c:66
StatsGlobalContext_
Holds the output interface context for the counter api.
Definition: counters.c:78
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
TVT_PPT
@ TVT_PPT
Definition: tm-threads-common.h:88
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
StatsRecord
struct StatsRecord_ StatsRecord
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
StatsRegisterCounter
StatsCounterId StatsRegisterCounter(const char *name, StatsThreadContext *stats)
Registers a normal, unqualified counter.
Definition: counters.c:1001
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:101
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:935
TmThreadWaitForFlag
void TmThreadWaitForFlag(ThreadVars *tv, uint32_t flags)
Waits till the specified flag(s) is(are) set. We don't bother if the kill flag has been set or not on...
Definition: tm-threads.c:1820
STATS_MGMTT_TTS
#define STATS_MGMTT_TTS
Definition: counters.c:48
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
StatsCounter_::type
int type
Definition: counters.h:50
stats_stream_events
bool stats_stream_events
Definition: counters.c:101
name
const char * name
Definition: detect-engine-proto.c:48
PacketQueue_
simple fifo queue for packets with mutex and cond Calling the mutex or triggering the cond is respons...
Definition: packet-queue.h:49
TMM_STATSLOGGER
@ TMM_STATSLOGGER
Definition: tm-threads-common.h:61
THV_DEINIT
#define THV_DEINIT
Definition: threadvars.h:45
CountersIdType
struct CountersIdType_ CountersIdType
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:49
thread_name_counter_stats
const char * thread_name_counter_stats
Definition: runmodes.c:75
Tmq_::pq
PacketQueue * pq
Definition: tm-queues.h:35
SCSetThreadName
#define SCSetThreadName(n)
Definition: threads.h:305
util-hash.h
StatsRecord_
Definition: output-stats.h:31
StatsPublicThreadContext_::copy_of_private
StatsLocalCounter * copy_of_private
Definition: counters.h:97
SCConfGet
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:350
THV_RUNNING
#define THV_RUNNING
Definition: threadvars.h:55
StatsCounter_::name
const char * name
Definition: counters.h:63
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
SCConfGetChildValueBool
int SCConfGetChildValueBool(const SCConfNode *base, const char *name, int *val)
Definition: conf.c:515
StatsCounter_::Func
uint64_t(* Func)(void)
Definition: counters.h:60
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
HashTable_
Definition: util-hash.h:35
u8_tolower
#define u8_tolower(c)
Definition: suricata-common.h:453
tv_root
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:82
SCSpinLock
#define SCSpinLock
Definition: threads-debug.h:236
SCConfValIsFalse
int SCConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:576
StatsInit
void StatsInit(void)
Initializes the perf counter api. Things are hard coded currently. More work to be done when we imple...
Definition: counters.c:925
util-privs.h
TVT_MGMT
@ TVT_MGMT
Definition: tm-threads-common.h:89
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
CountersIdType_::string
const char * string
Definition: counters.c:1070
SCDropCaps
#define SCDropCaps(...)
Definition: util-privs.h:89
StatsCounterId
Definition: counters.h:30
m
SCMutex m
Definition: flow-hash.h:6
SCConfNodeLookupChildValue
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:824
JSON_STATS_TOTALS
#define JSON_STATS_TOTALS
Definition: output-json-stats.h:29
StatsRecord_::pvalue
int64_t pvalue
Definition: output-stats.h:36
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
StatsRegisterAvgCounter
StatsCounterAvgId StatsRegisterAvgCounter(const char *name, StatsThreadContext *stats)
Registers a counter, whose value holds the average of all the values assigned to it.
Definition: counters.c:1019
StatsCounterDecr
void StatsCounterDecr(StatsThreadContext *stats, StatsCounterId id)
Decrements the local counter.
Definition: counters.c:184
STATS_TYPE_FUNC
@ STATS_TYPE_FUNC
Definition: counters.c:58
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
HashTableFree
void HashTableFree(HashTable *ht)
Free a HashTable and all its contents.
Definition: util-hash.c:100
ThreadVars_::cap_flags
uint8_t cap_flags
Definition: threadvars.h:80
HashTable_::array_size
uint32_t array_size
Definition: util-hash.h:37
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:53
PacketQueue_::mutex_q
SCMutex mutex_q
Definition: packet-queue.h:56
StatsThreadStore_::next
struct StatsThreadStore_ * next
Definition: counters.c:72
stats_decoder_events_prefix
const char * stats_decoder_events_prefix
Definition: counters.c:99
THV_RUNNING_DONE
#define THV_RUNNING_DONE
Definition: threadvars.h:46
StatsCounterAvgAddI64
void StatsCounterAvgAddI64(StatsThreadContext *stats, StatsCounterAvgId id, int64_t x)
Definition: counters.c:240
counters.h
STATS_TYPE_MAX
@ STATS_TYPE_MAX
Definition: counters.c:60
StatsCounter_::short_name
const char * short_name
Definition: counters.h:64
CountersIdType_
Definition: counters.c:1068
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
StatsCounter_::next
struct StatsCounter_ * next
Definition: counters.h:67
StatsCounterMaxId
Definition: counters.h:38
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:940
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
StatsGlobalContext_::counters_id_hash
HashTable * counters_id_hash
Definition: counters.c:84
SCEnter
#define SCEnter(...)
Definition: util-debug.h:281
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:182
StatsRecord_::name
const char * name
Definition: output-stats.h:32
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
StatsThreadStore_::name
const char * name
Definition: counters.c:68
THV_KILL
#define THV_KILL
Definition: threadvars.h:40
StatsThreadContext_::pub
StatsPublicThreadContext pub
Definition: counters.h:117
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *stats, StatsCounterId id)
Increments the local counter.
Definition: counters.c:165
SCSpinUnlock
#define SCSpinUnlock
Definition: threads-debug.h:238
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:269
util-time.h
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:259
HashTableAdd
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:120
STATS_TYPE_AVERAGE
@ STATS_TYPE_AVERAGE
Definition: counters.c:56
ThreadVars_::next
struct ThreadVars_ * next
Definition: threadvars.h:124
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:81
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
StatsTable_
Definition: output-stats.h:39
tv_root_lock
SCMutex tv_root_lock
Definition: tm-threads.c:85
StatsRecord_::value
int64_t value
Definition: output-stats.h:35
SCReturn
#define SCReturn
Definition: util-debug.h:283
SCCtrlMutexLock
#define SCCtrlMutexLock(mut)
Definition: threads-debug.h:377
StatsToJSON
json_t * StatsToJSON(const StatsTable *st, uint8_t flags)
turn StatsTable into a json object
Definition: output-json-stats.c:211
CountersIdType_::id
uint16_t id
Definition: counters.c:1069
type
uint16_t type
Definition: decode-vlan.c:106
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
ThreadVars_::ctrl_cond
SCCtrlCondT * ctrl_cond
Definition: threadvars.h:129
StatsCounterGlobalId::id
uint16_t id
Definition: counters.h:43
StatsCounter_::id
uint16_t id
Definition: counters.h:53
ThreadVars_::thread_setup_flags
uint8_t thread_setup_flags
Definition: threadvars.h:68
TmEcode
TmEcode
Definition: tm-threads-common.h:80
StatsTable_::start_time
time_t start_time
Definition: output-stats.h:44
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:106
SCCtrlCondTimedwait
#define SCCtrlCondTimedwait
Definition: threads-debug.h:386
StatsSetupPrivate
int StatsSetupPrivate(StatsThreadContext *stats, const char *thread_name)
Definition: counters.c:1238
PacketQueue_::cond_q
SCCondT cond_q
Definition: packet-queue.h:57
TmThreadCreateMgmtThread
ThreadVars * TmThreadCreateMgmtThread(const char *name, void *(fn_p)(void *), int mucond)
Creates and returns the TV instance for a Management thread(MGMT). This function supports only custom...
Definition: tm-threads.c:1097
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:229
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
STATS_WUT_TTS
#define STATS_WUT_TTS
Definition: counters.c:45
Flow_::next
struct Flow_ * next
Definition: flow.h:388
StatsGlobalContext_::global_counter_ctx
StatsPublicThreadContext global_counter_ctx
Definition: counters.c:86
StatsTable_::stats
StatsRecord * stats
Definition: output-stats.h:40
TmModule_
Definition: tm-modules.h:47
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1258
StatsCounterGlobalId
Definition: counters.h:42
StatsGlobalContext_::sts_lock
SCMutex sts_lock
Definition: counters.c:81
THV_INIT_DONE
#define THV_INIT_DONE
Definition: threadvars.h:37
StatsLocalCounter_
counter type for local (private) increments. For AVG counters we use 2 to track values and updates.
Definition: counters.h:74
SCCtrlMutexUnlock
#define SCCtrlMutexUnlock(mut)
Definition: threads-debug.h:379
SCCondSignal
#define SCCondSignal
Definition: threads-debug.h:140
ThreadVars_::inq
Tmq * inq
Definition: threadvars.h:89
util-conf.h
StatsGlobalContext_::sts_cnt
int sts_cnt
Definition: counters.c:82
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
output-json-stats.h
TmThreadsWaitForUnpause
bool TmThreadsWaitForUnpause(ThreadVars *tv)
Wait for a thread to become unpaused.
Definition: tm-threads.c:363
StatsCounterMaxId::id
uint16_t id
Definition: counters.h:39
StatsPublicThreadContext_::pc_array
const StatsCounter ** pc_array
Definition: counters.h:95
StatsThreadStore_::ctx
StatsPublicThreadContext * ctx
Definition: counters.c:70
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:116
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:952
StatsRecord_::tm_name
const char * tm_name
Definition: output-stats.h:34
StatsSyncCounters
void StatsSyncCounters(StatsThreadContext *stats)
Definition: counters.c:478
StatsCounterGetLocalValue
int64_t StatsCounterGetLocalValue(StatsThreadContext *stats, StatsCounterId id)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1301
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:51
JSON_STATS_THREADS
#define JSON_STATS_THREADS
Definition: output-json-stats.h:30
StatsCounterAvgId::id
uint16_t id
Definition: counters.h:35
StatsThreadContext_
Definition: counters.h:116
FatalError
#define FatalError(...)
Definition: util-debug.h:514
StatsTable_::tstats
StatsRecord * tstats
Definition: output-stats.h:41
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
threadvars.h
ConfUnixSocketIsEnable
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:136
SCSpinInit
#define SCSpinInit
Definition: threads-debug.h:239
StatsCounterSetI64
void StatsCounterSetI64(StatsThreadContext *stats, StatsCounterId id, int64_t x)
set, so overwrite, the value of the local counter
Definition: counters.c:204
StatsCounter_::gid
uint16_t gid
Definition: counters.h:56
SCConfGetNode
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition: conf.c:181
OutputStatsLoggersRegistered
int OutputStatsLoggersRegistered(void)
Definition: output-stats.c:177
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:271
head
Flow * head
Definition: flow-hash.h:1
StatsCounterAvgId
Definition: counters.h:34
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:113
StatsPrivateThreadContext_::size
uint32_t size
Definition: counters.h:111
StatsCounterId::id
uint16_t id
Definition: counters.h:31
StatsRegisterMaxCounter
StatsCounterMaxId StatsRegisterMaxCounter(const char *name, StatsThreadContext *stats)
Registers a counter, whose value holds the maximum of all the values assigned to it.
Definition: counters.c:1037
StatsRecord_::short_name
const char * short_name
Definition: output-stats.h:33
HashTableInit
HashTable * HashTableInit(uint32_t size, uint32_t(*Hash)(struct HashTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hash.c:35
STATS_TYPE_NORMAL
@ STATS_TYPE_NORMAL
Definition: counters.c:55
suricata.h
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:86
GetDocURL
const char * GetDocURL(void)
Definition: suricata.c:1169
StatsGlobalContext_::sts
StatsThreadStore * sts
Definition: counters.c:80
StatsGlobalContext
struct StatsGlobalContext_ StatsGlobalContext
Holds the output interface context for the counter api.
id
uint32_t id
Definition: detect-flowbits.c:944
ThreadVars_::ctrl_mutex
SCCtrlMutex * ctrl_mutex
Definition: threadvars.h:128
StatsThreadStore
struct StatsThreadStore_ StatsThreadStore
per thread store of counters
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
stats_decoder_events
bool stats_decoder_events
Definition: counters.c:98
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:93
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:89
THV_CLOSED
#define THV_CLOSED
Definition: threadvars.h:42
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
SCConfNode_
Definition: conf.h:37
StatsCounterAddI64
void StatsCounterAddI64(StatsThreadContext *stats, StatsCounterId id, int64_t x)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:146
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1354
SCConfNode_::val
char * val
Definition: conf.h:39
StatsTable_::nstats
uint32_t nstats
Definition: output-stats.h:42
SCMutex
#define SCMutex
Definition: threads-debug.h:114
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:108
OutputStatsLog
TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
Definition: output-stats.c:77
StatsLocalCounter_::v
int64_t v
Definition: counters.h:75
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1578
StatsThreadContext_::priv
StatsPrivateThreadContext priv
Definition: counters.h:118
output.h