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