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