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 "suricata.h"
29 #include "counters.h"
30 #include "threadvars.h"
31 #include "tm-threads.h"
32 #include "conf.h"
33 #include "util-time.h"
34 #include "util-unittest.h"
35 #include "util-debug.h"
36 #include "util-byte.h"
37 #include "util-privs.h"
38 #include "util-signal.h"
39 #include "unix-manager.h"
40 #include "runmodes.h"
41 
42 #include "output.h"
43 #include "output-stats.h"
44 #include "output-json-stats.h"
45 
46 /* Time interval for syncing the local counters with the global ones */
47 #define STATS_WUT_TTS 3
48 
49 /* Time interval at which the mgmt thread o/p the stats */
50 #define STATS_MGMTT_TTS 8
51 
52 /**
53  * \brief Different kinds of qualifier that can be used to modify the behaviour
54  * of the counter to be registered
55  */
56 enum {
61 
63 };
64 
65 /**
66  * \brief per thread store of counters
67  */
68 typedef struct StatsThreadStore_ {
69  /** thread name used in output */
70  const char *name;
71 
73 
75  uint32_t size;
76 
79 
80 /**
81  * \brief Holds the output interface context for the counter api
82  */
83 typedef struct StatsGlobalContext_ {
84  /** list of thread stores: one per thread plus one global */
87  int sts_cnt;
88 
90 
93 
94 static void *stats_thread_data = NULL;
95 static StatsGlobalContext *stats_ctx = NULL;
96 static time_t stats_start_time;
97 /** refresh interval in seconds */
98 static uint32_t stats_tts = STATS_MGMTT_TTS;
99 /** is the stats counter enabled? */
100 static bool stats_enabled = true;
101 
102 /**< add decoder events as stats? enabled by default */
104 const char *stats_decoder_events_prefix = "decoder.event";
105 /**< add stream events as stats? disabled by default */
106 bool stats_stream_events = false;
107 
108 static int StatsOutput(ThreadVars *tv);
109 static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext *);
111 
112 /** stats table is filled each interval and passed to the
113  * loggers. Initialized at first use. */
114 static StatsTable stats_table = { NULL, NULL, 0, 0, 0, {0 , 0}};
115 static SCMutex stats_table_mutex = SCMUTEX_INITIALIZER;
116 static int stats_loggers_active = 1;
117 
118 static uint16_t counters_global_id = 0;
119 
120 bool StatsEnabled(void)
121 {
122  return stats_enabled;
123 }
124 
125 static void StatsPublicThreadContextInit(StatsPublicThreadContext *t)
126 {
127  SCMutexInit(&t->m, NULL);
128 }
129 
130 static void StatsPublicThreadContextCleanup(StatsPublicThreadContext *t)
131 {
132  SCMutexLock(&t->m);
134  t->head = NULL;
135  t->perf_flag = 0;
136  t->curr_id = 0;
137  SCMutexUnlock(&t->m);
138  SCMutexDestroy(&t->m);
139 }
140 
141 /**
142  * \brief Adds a value of type uint64_t to the local counter.
143  *
144  * \param id ID of the counter as set by the API
145  * \param pca Counter array that holds the local counter for this TM
146  * \param x Value to add to this local counter
147  */
148 void StatsAddUI64(ThreadVars *tv, uint16_t id, uint64_t x)
149 {
151 #if defined (UNITTESTS) || defined (FUZZ)
152  if (pca->initialized == 0)
153  return;
154 #endif
155 #ifdef DEBUG
156  BUG_ON ((id < 1) || (id > pca->size));
157 #endif
158  pca->head[id].value += x;
159  pca->head[id].updates++;
160  return;
161 }
162 
163 /**
164  * \brief Increments the local counter
165  *
166  * \param id Index of the counter in the counter array
167  * \param pca Counter array that holds the local counters for this TM
168  */
169 void StatsIncr(ThreadVars *tv, uint16_t id)
170 {
172 #if defined (UNITTESTS) || defined (FUZZ)
173  if (pca->initialized == 0)
174  return;
175 #endif
176 #ifdef DEBUG
177  BUG_ON ((id < 1) || (id > pca->size));
178 #endif
179  pca->head[id].value++;
180  pca->head[id].updates++;
181  return;
182 }
183 
184 /**
185  * \brief Decrements the local counter
186  *
187  * \param id Index of the counter in the counter array
188  * \param pca Counter array that holds the local counters for this TM
189  */
190 void StatsDecr(ThreadVars *tv, uint16_t id)
191 {
193 #if defined(UNITTESTS) || defined(FUZZ)
194  if (pca->initialized == 0)
195  return;
196 #endif
197 #ifdef DEBUG
198  BUG_ON((id < 1) || (id > pca->size));
199 #endif
200  pca->head[id].value--;
201  pca->head[id].updates++;
202  return;
203 }
204 
205 /**
206  * \brief Sets a value of type double to the local counter
207  *
208  * \param id Index of the local counter in the counter array
209  * \param pca Pointer to the StatsPrivateThreadContext
210  * \param x The value to set for the counter
211  */
212 void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
213 {
215 #if defined (UNITTESTS) || defined (FUZZ)
216  if (pca->initialized == 0)
217  return;
218 #endif
219 #ifdef DEBUG
220  BUG_ON ((id < 1) || (id > pca->size));
221 #endif
222 
223  if ((pca->head[id].pc->type == STATS_TYPE_MAXIMUM) && ((int64_t)x > pca->head[id].value)) {
224  pca->head[id].value = x;
225  } else if (pca->head[id].pc->type == STATS_TYPE_NORMAL) {
226  pca->head[id].value = x;
227  }
228 
229  pca->head[id].updates++;
230 
231  return;
232 }
233 
234 static ConfNode *GetConfig(void) {
235  ConfNode *stats = ConfGetNode("stats");
236  if (stats != NULL)
237  return stats;
238 
239  ConfNode *root = ConfGetNode("outputs");
240  ConfNode *node = NULL;
241  if (root != NULL) {
242  TAILQ_FOREACH(node, &root->head, next) {
243  if (strcmp(node->val, "stats") == 0) {
244  return node->head.tqh_first;
245  }
246  }
247  }
248  return NULL;
249 }
250 
251 /**
252  * \brief Initializes stats context
253  */
254 static void StatsInitCtxPreOutput(void)
255 {
256  SCEnter();
257  ConfNode *stats = GetConfig();
258  if (stats != NULL) {
259  const char *enabled = ConfNodeLookupChildValue(stats, "enabled");
260  if (enabled != NULL && ConfValIsFalse(enabled)) {
261  stats_enabled = false;
262  SCLogDebug("Stats module has been disabled");
263  SCReturn;
264  }
265  /* warn if we are using legacy config to enable stats */
266  ConfNode *gstats = ConfGetNode("stats");
267  if (gstats == NULL) {
268  SCLogWarning(SC_ERR_STATS_LOG_GENERIC, "global stats config is missing. "
269  "Stats enabled through legacy stats.log. "
270  "See %s/configuration/suricata-yaml.html#stats", GetDocURL());
271  }
272 
273  const char *interval = ConfNodeLookupChildValue(stats, "interval");
274  if (interval != NULL)
275  if (StringParseUint32(&stats_tts, 10, 0, interval) < 0) {
276  SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for "
277  "interval: \"%s\". Resetting to %d.", interval,
279  stats_tts = STATS_MGMTT_TTS;
280  }
281 
282  int b;
283  int ret = ConfGetChildValueBool(stats, "decoder-events", &b);
284  if (ret) {
285  stats_decoder_events = (b == 1);
286  }
287  ret = ConfGetChildValueBool(stats, "stream-events", &b);
288  if (ret) {
289  stats_stream_events = (b == 1);
290  }
291 
292  const char *prefix = NULL;
293  if (ConfGet("stats.decoder-events-prefix", &prefix) != 1) {
294  prefix = "decoder.event";
295  }
297  }
298  SCReturn;
299 }
300 
301 static void StatsInitCtxPostOutput(void)
302 {
303  SCEnter();
304  /* Store the engine start time */
305  time(&stats_start_time);
306 
307  /* init the lock used by StatsThreadStore */
308  if (SCMutexInit(&stats_ctx->sts_lock, NULL) != 0) {
309  FatalError(SC_ERR_FATAL, "error initializing sts mutex");
310  }
311 
312  if (stats_enabled && !OutputStatsLoggersRegistered()) {
313  stats_loggers_active = 0;
314 
315  /* if the unix command socket is enabled we do the background
316  * stats sync just in case someone runs 'dump-counters' */
317  if (!ConfUnixSocketIsEnable()) {
318  SCLogWarning(SC_WARN_NO_STATS_LOGGERS, "stats are enabled but no loggers are active");
319  stats_enabled = false;
320  SCReturn;
321  }
322  }
323 
324  SCReturn;
325 }
326 
327 /**
328  * \brief Releases the resources alloted to the output context of the
329  * Stats API
330  */
331 static void StatsReleaseCtx(void)
332 {
333  if (stats_ctx == NULL) {
334  SCLogDebug("Counter module has been disabled");
335  return;
336  }
337 
338  StatsThreadStore *sts = NULL;
339  StatsThreadStore *temp = NULL;
340  sts = stats_ctx->sts;
341 
342  while (sts != NULL) {
343  if (sts->head != NULL)
344  SCFree(sts->head);
345 
346  temp = sts->next;
347  SCFree(sts);
348  sts = temp;
349  }
350 
351  if (stats_ctx->counters_id_hash != NULL) {
352  HashTableFree(stats_ctx->counters_id_hash);
353  stats_ctx->counters_id_hash = NULL;
354  counters_global_id = 0;
355  }
356 
357  StatsPublicThreadContextCleanup(&stats_ctx->global_counter_ctx);
358  SCFree(stats_ctx);
359  stats_ctx = NULL;
360 
361  SCMutexLock(&stats_table_mutex);
362  /* free stats table */
363  if (stats_table.tstats != NULL) {
364  SCFree(stats_table.tstats);
365  stats_table.tstats = NULL;
366  }
367 
368  if (stats_table.stats != NULL) {
369  SCFree(stats_table.stats);
370  stats_table.stats = NULL;
371  }
372  memset(&stats_table, 0, sizeof(stats_table));
373  SCMutexUnlock(&stats_table_mutex);
374 
375  return;
376 }
377 
378 /**
379  * \brief management thread. This thread is responsible for writing the stats
380  *
381  * \param arg thread var
382  *
383  * \retval NULL This is the value that is always returned
384  */
385 static void *StatsMgmtThread(void *arg)
386 {
387  ThreadVars *tv_local = (ThreadVars *)arg;
388 
389  SCSetThreadName(tv_local->name);
390 
391  if (tv_local->thread_setup_flags != 0)
392  TmThreadSetupOptions(tv_local);
393 
394  /* Set the threads capability */
395  tv_local->cap_flags = 0;
396  SCDropCaps(tv_local);
397 
398  if (stats_ctx == NULL) {
399  SCLogError(SC_ERR_STATS_NOT_INIT, "Stats API not init"
400  "StatsInitCounterApi() has to be called first");
402  return NULL;
403  }
404 
406  BUG_ON(tm->ThreadInit == NULL);
407  int r = tm->ThreadInit(tv_local, NULL, &stats_thread_data);
408  if (r != 0 || stats_thread_data == NULL) {
409  SCLogError(SC_ERR_THREAD_INIT, "Stats API "
410  "ThreadInit failed");
412  return NULL;
413  }
414  SCLogDebug("stats_thread_data %p", &stats_thread_data);
415 
416  TmThreadsSetFlag(tv_local, THV_INIT_DONE);
417  while (1) {
418  if (TmThreadsCheckFlag(tv_local, THV_PAUSE)) {
419  TmThreadsSetFlag(tv_local, THV_PAUSED);
420  TmThreadTestThreadUnPaused(tv_local);
421  TmThreadsUnsetFlag(tv_local, THV_PAUSED);
422  }
423 
424  struct timeval cur_timev;
425  gettimeofday(&cur_timev, NULL);
426  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
427  cond_time.tv_sec += (stats_tts);
428 
429  /* wait for the set time, or until we are woken up by
430  * the shutdown procedure */
431  SCCtrlMutexLock(tv_local->ctrl_mutex);
432  SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
433  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
434 
435  SCMutexLock(&stats_table_mutex);
436  StatsOutput(tv_local);
437  SCMutexUnlock(&stats_table_mutex);
438 
439  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
440  break;
441  }
442  }
443 
445  TmThreadWaitForFlag(tv_local, THV_DEINIT);
446 
447  r = tm->ThreadDeinit(tv_local, stats_thread_data);
448  if (r != TM_ECODE_OK) {
449  SCLogError(SC_ERR_THREAD_DEINIT, "Stats Counter API "
450  "ThreadDeinit failed");
451  }
452 
453  TmThreadsSetFlag(tv_local, THV_CLOSED);
454  return NULL;
455 }
456 
457 /**
458  * \brief Wake up thread. This thread wakes up every TTS(time to sleep) seconds
459  * and sets the flag for every ThreadVars' StatsPublicThreadContext
460  *
461  * \param arg is NULL always
462  *
463  * \retval NULL This is the value that is always returned
464  */
465 static void *StatsWakeupThread(void *arg)
466 {
467  ThreadVars *tv_local = (ThreadVars *)arg;
468 
469  SCSetThreadName(tv_local->name);
470 
471  if (tv_local->thread_setup_flags != 0)
472  TmThreadSetupOptions(tv_local);
473 
474  /* Set the threads capability */
475  tv_local->cap_flags = 0;
476  SCDropCaps(tv_local);
477 
478  if (stats_ctx == NULL) {
479  SCLogError(SC_ERR_STATS_NOT_INIT, "Stats API not init"
480  "StatsInitCounterApi() has to be called first");
482  return NULL;
483  }
484 
485  TmThreadsSetFlag(tv_local, THV_INIT_DONE);
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(SC_ERR_MEM_ALLOC, "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(SC_ERR_MEM_ALLOC, "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) {
883  "Fatal error encountered in StatsInitCtx. Exiting...");
884  }
885  memset(stats_ctx, 0, sizeof(StatsGlobalContext));
886 
887  StatsPublicThreadContextInit(&stats_ctx->global_counter_ctx);
888 }
889 
891 {
892  StatsInitCtxPreOutput();
893 }
894 
896 {
897  StatsInitCtxPostOutput();
898 }
899 
900 
901 /**
902  * \brief Spawns the wakeup, and the management thread used by the stats api
903  *
904  * The threads use the condition variable in the thread vars to control
905  * their wait loops to make sure the main thread can quickly kill them.
906  */
908 {
909  SCEnter();
910 
911  if (!stats_enabled) {
912  SCReturn;
913  }
914 
915  ThreadVars *tv_wakeup = NULL;
916  ThreadVars *tv_mgmt = NULL;
917 
918  /* spawn the stats wakeup thread */
920  StatsWakeupThread, 1);
921  if (tv_wakeup == NULL) {
922  FatalError(SC_ERR_FATAL, "TmThreadCreateMgmtThread "
923  "failed");
924  }
925 
926  if (TmThreadSpawn(tv_wakeup) != 0) {
927  FatalError(SC_ERR_FATAL, "TmThreadSpawn failed for "
928  "StatsWakeupThread");
929  }
930 
931  /* spawn the stats mgmt thread */
933  StatsMgmtThread, 1);
934  if (tv_mgmt == NULL) {
935  FatalError(SC_ERR_FATAL, "TmThreadCreateMgmtThread failed");
936  }
937 
938  if (TmThreadSpawn(tv_mgmt) != 0) {
939  FatalError(SC_ERR_FATAL, "TmThreadSpawn failed for "
940  "StatsWakeupThread");
941  }
942 
943  SCReturn;
944 }
945 
946 /**
947  * \brief Registers a normal, unqualified counter
948  *
949  * \param name Name of the counter, to be registered
950  * \param tv Pointer to the ThreadVars instance for which the counter would
951  * be registered
952  *
953  * \retval id Counter id for the newly registered counter, or the already
954  * present counter
955  */
956 uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
957 {
958  uint16_t id = StatsRegisterQualifiedCounter(name,
961  STATS_TYPE_NORMAL, NULL);
962  return id;
963 }
964 
965 /**
966  * \brief Registers a counter, whose value holds the average of all the values
967  * assigned to it.
968  *
969  * \param name Name of the counter, to be registered
970  * \param tv Pointer to the ThreadVars instance for which the counter would
971  * be registered
972  *
973  * \retval id Counter id for the newly registered counter, or the already
974  * present counter
975  */
976 uint16_t StatsRegisterAvgCounter(const char *name, struct ThreadVars_ *tv)
977 {
978  uint16_t id = StatsRegisterQualifiedCounter(name,
981  STATS_TYPE_AVERAGE, NULL);
982  return id;
983 }
984 
985 /**
986  * \brief Registers a counter, whose value holds the maximum of all the values
987  * assigned to it.
988  *
989  * \param name Name of the counter, to be registered
990  * \param tv Pointer to the ThreadVars instance for which the counter would
991  * be registered
992  *
993  * \retval the counter id for the newly registered counter, or the already
994  * present counter
995  */
996 uint16_t StatsRegisterMaxCounter(const char *name, struct ThreadVars_ *tv)
997 {
998  uint16_t id = StatsRegisterQualifiedCounter(name,
1000  &tv->perf_public_ctx,
1001  STATS_TYPE_MAXIMUM, NULL);
1002  return id;
1003 }
1004 
1005 /**
1006  * \brief Registers a counter, which represents a global value
1007  *
1008  * \param name Name of the counter, to be registered
1009  * \param Func Function Pointer returning a uint64_t
1010  *
1011  * \retval id Counter id for the newly registered counter, or the already
1012  * present counter
1013  */
1014 uint16_t StatsRegisterGlobalCounter(const char *name, uint64_t (*Func)(void))
1015 {
1016 #if defined (UNITTESTS) || defined (FUZZ)
1017  if (stats_ctx == NULL)
1018  return 0;
1019 #else
1020  BUG_ON(stats_ctx == NULL);
1021 #endif
1022  uint16_t id = StatsRegisterQualifiedCounter(name, NULL,
1023  &(stats_ctx->global_counter_ctx),
1025  Func);
1026  return id;
1027 }
1028 
1029 typedef struct CountersIdType_ {
1030  uint16_t id;
1031  const char *string;
1033 
1034 static uint32_t CountersIdHashFunc(HashTable *ht, void *data, uint16_t datalen)
1035 {
1036  CountersIdType *t = (CountersIdType *)data;
1037  uint32_t hash = 0;
1038  int len = strlen(t->string);
1039 
1040  for (int i = 0; i < len; i++)
1041  hash += u8_tolower((unsigned char)t->string[i]);
1042 
1043  hash = hash % ht->array_size;
1044  return hash;
1045 }
1046 
1047 static char CountersIdHashCompareFunc(void *data1, uint16_t datalen1,
1048  void *data2, uint16_t datalen2)
1049 {
1050  CountersIdType *t1 = (CountersIdType *)data1;
1051  CountersIdType *t2 = (CountersIdType *)data2;
1052  int len1 = 0;
1053  int len2 = 0;
1054 
1055  if (t1 == NULL || t2 == NULL)
1056  return 0;
1057 
1058  if (t1->string == NULL || t2->string == NULL)
1059  return 0;
1060 
1061  len1 = strlen(t1->string);
1062  len2 = strlen(t2->string);
1063 
1064  if (len1 == len2 && memcmp(t1->string, t2->string, len1) == 0) {
1065  return 1;
1066  }
1067 
1068  return 0;
1069 }
1070 
1071 static void CountersIdHashFreeFunc(void *data)
1072 {
1073  SCFree(data);
1074 }
1075 
1076 
1077 /** \internal
1078  * \brief Adds a TM to the clubbed TM table. Multiple instances of the same TM
1079  * are stacked together in a PCTMI container.
1080  *
1081  * \param tm_name Name of the tm to be added to the table
1082  * \param pctx StatsPublicThreadContext associated with the TM tm_name
1083  *
1084  * \retval 1 on success, 0 on failure
1085  */
1086 static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext *pctx)
1087 {
1088  if (stats_ctx == NULL) {
1089  SCLogDebug("Counter module has been disabled");
1090  return 0;
1091  }
1092 
1093  if (thread_name == NULL || pctx == NULL) {
1094  SCLogDebug("supplied argument(s) to StatsThreadRegister NULL");
1095  return 0;
1096  }
1097 
1098  SCMutexLock(&stats_ctx->sts_lock);
1099  if (stats_ctx->counters_id_hash == NULL) {
1100  stats_ctx->counters_id_hash = HashTableInit(256, CountersIdHashFunc,
1101  CountersIdHashCompareFunc,
1102  CountersIdHashFreeFunc);
1103  BUG_ON(stats_ctx->counters_id_hash == NULL);
1104  }
1105  StatsCounter *pc = pctx->head;
1106  while (pc != NULL) {
1107  CountersIdType t = { 0, pc->name }, *id = NULL;
1108  id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t));
1109  if (id == NULL) {
1110  id = SCCalloc(1, sizeof(*id));
1111  BUG_ON(id == NULL);
1112  id->id = counters_global_id++;
1113  id->string = pc->name;
1114  BUG_ON(HashTableAdd(stats_ctx->counters_id_hash, id, sizeof(*id)) < 0);
1115  }
1116  pc->gid = id->id;
1117  pc = pc->next;
1118  }
1119 
1120 
1121  StatsThreadStore *temp = NULL;
1122  if ( (temp = SCMalloc(sizeof(StatsThreadStore))) == NULL) {
1123  SCMutexUnlock(&stats_ctx->sts_lock);
1124  return 0;
1125  }
1126  memset(temp, 0, sizeof(StatsThreadStore));
1127 
1128  temp->ctx = pctx;
1129  temp->name = thread_name;
1130 
1131  temp->next = stats_ctx->sts;
1132  stats_ctx->sts = temp;
1133  stats_ctx->sts_cnt++;
1134  SCLogDebug("stats_ctx->sts %p", stats_ctx->sts);
1135 
1136  SCMutexUnlock(&stats_ctx->sts_lock);
1137  return 1;
1138 }
1139 
1140 /** \internal
1141  * \brief Returns a counter array for counters in this id range(s_id - e_id)
1142  *
1143  * \param s_id Counter id of the first counter to be added to the array
1144  * \param e_id Counter id of the last counter to be added to the array
1145  * \param pctx Pointer to the tv's StatsPublicThreadContext
1146  *
1147  * \retval a counter-array in this(s_id-e_id) range for this TM instance
1148  */
1149 static int StatsGetCounterArrayRange(uint16_t s_id, uint16_t e_id,
1152 {
1153  StatsCounter *pc = NULL;
1154  uint32_t i = 0;
1155 
1156  if (pctx == NULL || pca == NULL) {
1157  SCLogDebug("pctx/pca is NULL");
1158  return -1;
1159  }
1160 
1161  if (s_id < 1 || e_id < 1 || s_id > e_id) {
1162  SCLogDebug("error with the counter ids");
1163  return -1;
1164  }
1165 
1166  if (e_id > pctx->curr_id) {
1167  SCLogDebug("end id is greater than the max id for this tv");
1168  return -1;
1169  }
1170 
1171  if ( (pca->head = SCMalloc(sizeof(StatsLocalCounter) * (e_id - s_id + 2))) == NULL) {
1172  return -1;
1173  }
1174  memset(pca->head, 0, sizeof(StatsLocalCounter) * (e_id - s_id + 2));
1175 
1176  pc = pctx->head;
1177  while (pc->id != s_id)
1178  pc = pc->next;
1179 
1180  i = 1;
1181  while ((pc != NULL) && (pc->id <= e_id)) {
1182  pca->head[i].pc = pc;
1183  pca->head[i].id = pc->id;
1184  pc = pc->next;
1185  i++;
1186  }
1187  pca->size = i - 1;
1188 
1189  pca->initialized = 1;
1190  return 0;
1191 }
1192 
1193 /** \internal
1194  * \brief Returns a counter array for all counters registered for this tm
1195  * instance
1196  *
1197  * \param pctx Pointer to the tv's StatsPublicThreadContext
1198  *
1199  * \retval pca Pointer to a counter-array for all counter of this tm instance
1200  * on success; NULL on failure
1201  */
1202 static int StatsGetAllCountersArray(StatsPublicThreadContext *pctx, StatsPrivateThreadContext *private)
1203 {
1204  if (pctx == NULL || private == NULL)
1205  return -1;
1206 
1207  return StatsGetCounterArrayRange(1, pctx->curr_id, pctx, private);
1208 }
1209 
1210 
1212 {
1213  StatsGetAllCountersArray(&(tv)->perf_public_ctx, &(tv)->perf_private_ctx);
1214 
1215  StatsThreadRegister(tv->printable_name ? tv->printable_name : tv->name,
1216  &(tv)->perf_public_ctx);
1217  return 0;
1218 }
1219 
1220 /**
1221  * \brief the private stats store with the public stats store
1222  *
1223  * \param pca Pointer to the StatsPrivateThreadContext
1224  * \param pctx Pointer the the tv's StatsPublicThreadContext
1225  *
1226  * \retval 1 on success
1227  * \retval -1 on error
1228  */
1230 {
1231 
1232  if (pca == NULL || pctx == NULL) {
1233  SCLogDebug("pca or pctx is NULL inside StatsUpdateCounterArray");
1234  return -1;
1235  }
1236 
1237  SCMutexLock(&pctx->m);
1238  StatsLocalCounter *pcae = pca->head;
1239  for (uint32_t i = 1; i <= pca->size; i++) {
1240  StatsCopyCounterValue(&pcae[i]);
1241  }
1242  SCMutexUnlock(&pctx->m);
1243 
1244  pctx->perf_flag = 0;
1245  return 1;
1246 }
1247 
1248 /**
1249  * \brief Get the value of the local copy of the counter that hold this id.
1250  *
1251  * \param tv threadvars
1252  * \param id The counter id.
1253  *
1254  * \retval 0 on success.
1255  * \retval -1 on error.
1256  */
1257 uint64_t StatsGetLocalCounterValue(ThreadVars *tv, uint16_t id)
1258 {
1260 #ifdef DEBUG
1261  BUG_ON ((id < 1) || (id > pca->size));
1262 #endif
1263  return pca->head[id].value;
1264 }
1265 
1266 /**
1267  * \brief Releases the resources alloted by the Stats API
1268  */
1270 {
1271  StatsLogSummary();
1272  StatsReleaseCtx();
1273 }
1274 
1275 /**
1276  * \brief Releases counters
1277  *
1278  * \param head Pointer to the head of the list of perf counters that have to
1279  * be freed
1280  */
1282 {
1283  StatsCounter *pc = NULL;
1284 
1285  while (head != NULL) {
1286  pc = head;
1287  head = head->next;
1288  StatsReleaseCounter(pc);
1289  }
1290 }
1291 
1292 /** \internal
1293  * \brief Releases the StatsPrivateThreadContext allocated by the user,
1294  * for storing and updating local counter values
1295  *
1296  * \param pca Pointer to the StatsPrivateThreadContext
1297  */
1298 static void StatsReleasePrivateThreadContext(StatsPrivateThreadContext *pca)
1299 {
1300  if (pca != NULL) {
1301  if (pca->head != NULL) {
1302  SCFree(pca->head);
1303  pca->head = NULL;
1304  pca->size = 0;
1305  }
1306  pca->initialized = 0;
1307  }
1308 }
1309 
1311 {
1312  StatsPublicThreadContextCleanup(&tv->perf_public_ctx);
1313  StatsReleasePrivateThreadContext(&tv->perf_private_ctx);
1314 }
1315 
1316 /*----------------------------------Unit_Tests--------------------------------*/
1317 
1318 #ifdef UNITTESTS
1319 /** \internal
1320  * \brief Registers a normal, unqualified counter
1321  *
1322  * \param name Name of the counter, to be registered
1323  * \param tm_name Name of the engine module under which the counter has to be
1324  * registered
1325  * \param type Datatype of this counter variable
1326  * \param pctx StatsPublicThreadContext corresponding to the tm_name key under which the
1327  * key has to be registered
1328  *
1329  * \retval id Counter id for the newly registered counter, or the already
1330  * present counter
1331  */
1332 static uint16_t RegisterCounter(const char *name, const char *tm_name,
1334 {
1335  uint16_t id = StatsRegisterQualifiedCounter(name, tm_name, pctx,
1336  STATS_TYPE_NORMAL, NULL);
1337  return id;
1338 }
1339 
1340 static int StatsTestCounterReg02(void)
1341 {
1343 
1344  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1345 
1346  return RegisterCounter(NULL, NULL, &pctx) == 0;
1347 }
1348 
1349 static int StatsTestCounterReg03(void)
1350 {
1352  int result;
1353 
1354  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1355 
1356  result = RegisterCounter("t1", "c1", &pctx);
1357 
1358  FAIL_IF_NOT(result);
1359 
1360  StatsReleaseCounters(pctx.head);
1361 
1362  PASS;
1363 }
1364 
1365 static int StatsTestCounterReg04(void)
1366 {
1368  int result;
1369 
1370  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1371 
1372  RegisterCounter("t1", "c1", &pctx);
1373  RegisterCounter("t2", "c2", &pctx);
1374  RegisterCounter("t3", "c3", &pctx);
1375 
1376  result = RegisterCounter("t1", "c1", &pctx);
1377 
1378  FAIL_IF_NOT(result);
1379 
1380  StatsReleaseCounters(pctx.head);
1381 
1382  PASS;
1383 }
1384 
1385 static int StatsTestGetCntArray05(void)
1386 {
1387  ThreadVars tv;
1388  int id;
1389 
1390  memset(&tv, 0, sizeof(ThreadVars));
1391 
1392  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1393  FAIL_IF(id != 1);
1394 
1395  int r = StatsGetAllCountersArray(NULL, &tv.perf_private_ctx);
1396  FAIL_IF_NOT(r == -1);
1397  PASS;
1398 }
1399 
1400 static int StatsTestGetCntArray06(void)
1401 {
1402  ThreadVars tv;
1403  int id;
1404 
1405  memset(&tv, 0, sizeof(ThreadVars));
1406 
1407  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1408  FAIL_IF(id != 1);
1409 
1410  int r = StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1411  FAIL_IF_NOT(r == 0);
1412 
1414  StatsReleasePrivateThreadContext(&tv.perf_private_ctx);
1415 
1416  PASS;
1417 }
1418 
1419 static int StatsTestCntArraySize07(void)
1420 {
1421  ThreadVars tv;
1422  StatsPrivateThreadContext *pca = NULL;
1423 
1424  memset(&tv, 0, sizeof(ThreadVars));
1425 
1426  //pca = (StatsPrivateThreadContext *)&tv.perf_private_ctx;
1427 
1428  RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1429  RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1430 
1431  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1432  pca = &tv.perf_private_ctx;
1433 
1434  StatsIncr(&tv, 1);
1435  StatsIncr(&tv, 2);
1436 
1437  FAIL_IF_NOT(pca->size == 2);
1438 
1440  StatsReleasePrivateThreadContext(pca);
1441 
1442  PASS;
1443 }
1444 
1445 static int StatsTestUpdateCounter08(void)
1446 {
1447  ThreadVars tv;
1448  StatsPrivateThreadContext *pca = NULL;
1449  int id;
1450 
1451  memset(&tv, 0, sizeof(ThreadVars));
1452 
1453  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1454 
1455  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1456  pca = &tv.perf_private_ctx;
1457 
1458  StatsIncr(&tv, id);
1459  StatsAddUI64(&tv, id, 100);
1460 
1461  FAIL_IF_NOT(pca->head[id].value == 101);
1462 
1464  StatsReleasePrivateThreadContext(pca);
1465 
1466  PASS;
1467 }
1468 
1469 static int StatsTestUpdateCounter09(void)
1470 {
1471  ThreadVars tv;
1472  StatsPrivateThreadContext *pca = NULL;
1473  uint16_t id1, id2;
1474 
1475  memset(&tv, 0, sizeof(ThreadVars));
1476 
1477  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1478  RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1479  RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1480  RegisterCounter("t4", "c4", &tv.perf_public_ctx);
1481  id2 = RegisterCounter("t5", "c5", &tv.perf_public_ctx);
1482 
1483  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1484  pca = &tv.perf_private_ctx;
1485 
1486  StatsIncr(&tv, id2);
1487  StatsAddUI64(&tv, id2, 100);
1488 
1489  FAIL_IF_NOT((pca->head[id1].value == 0) && (pca->head[id2].value == 101));
1490 
1492  StatsReleasePrivateThreadContext(pca);
1493 
1494  PASS;
1495 }
1496 
1497 static int StatsTestUpdateGlobalCounter10(void)
1498 {
1499  ThreadVars tv;
1500  StatsPrivateThreadContext *pca = NULL;
1501 
1502  int result = 1;
1503  uint16_t id1, id2, id3;
1504 
1505  memset(&tv, 0, sizeof(ThreadVars));
1506 
1507  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1508  id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1509  id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1510 
1511  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1512  pca = &tv.perf_private_ctx;
1513 
1514  StatsIncr(&tv, id1);
1515  StatsAddUI64(&tv, id2, 100);
1516  StatsIncr(&tv, id3);
1517  StatsAddUI64(&tv, id3, 100);
1518 
1520 
1521  result = (1 == tv.perf_public_ctx.head->value);
1522  result &= (100 == tv.perf_public_ctx.head->next->value);
1523  result &= (101 == tv.perf_public_ctx.head->next->next->value);
1524  FAIL_IF_NOT(result);
1525 
1527  StatsReleasePrivateThreadContext(pca);
1528 
1529  PASS;
1530 }
1531 
1532 static int StatsTestCounterValues11(void)
1533 {
1534  ThreadVars tv;
1535  StatsPrivateThreadContext *pca = NULL;
1536 
1537  int result = 1;
1538  uint16_t id1, id2, id3, id4;
1539 
1540  memset(&tv, 0, sizeof(ThreadVars));
1541 
1542  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1543  id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1544  id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1545  id4 = RegisterCounter("t4", "c4", &tv.perf_public_ctx);
1546 
1547  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1548  pca = &tv.perf_private_ctx;
1549 
1550  StatsIncr(&tv, id1);
1551  StatsAddUI64(&tv, id2, 256);
1552  StatsAddUI64(&tv, id3, 257);
1553  StatsAddUI64(&tv, id4, 16843024);
1554 
1556 
1557  result &= (1 == tv.perf_public_ctx.head->value);
1558  result &= (256 == tv.perf_public_ctx.head->next->value);
1559  result &= (257 == tv.perf_public_ctx.head->next->next->value);
1560  result &= (16843024 == tv.perf_public_ctx.head->next->next->next->value);
1561  FAIL_IF_NOT(result);
1562 
1564  StatsReleasePrivateThreadContext(pca);
1565 
1566  PASS;
1567 }
1568 
1569 #endif
1570 
1572 {
1573 #ifdef UNITTESTS
1574  UtRegisterTest("StatsTestCounterReg02", StatsTestCounterReg02);
1575  UtRegisterTest("StatsTestCounterReg03", StatsTestCounterReg03);
1576  UtRegisterTest("StatsTestCounterReg04", StatsTestCounterReg04);
1577  UtRegisterTest("StatsTestGetCntArray05", StatsTestGetCntArray05);
1578  UtRegisterTest("StatsTestGetCntArray06", StatsTestGetCntArray06);
1579  UtRegisterTest("StatsTestCntArraySize07", StatsTestCntArraySize07);
1580  UtRegisterTest("StatsTestUpdateCounter08", StatsTestUpdateCounter08);
1581  UtRegisterTest("StatsTestUpdateCounter09", StatsTestUpdateCounter09);
1582  UtRegisterTest("StatsTestUpdateGlobalCounter10",
1583  StatsTestUpdateGlobalCounter10);
1584  UtRegisterTest("StatsTestCounterValues11", StatsTestCounterValues11);
1585 #endif
1586 }
util-byte.h
tm-threads.h
FROM_TIMEVAL
#define FROM_TIMEVAL(timev)
intialize a 'struct timespec' from a 'struct timeval'.
Definition: util-time.h:34
thread_name_counter_wakeup
const char * thread_name_counter_wakeup
Definition: runmodes.c:74
StatsTable_::ntstats
uint32_t ntstats
Definition: output-stats.h:40
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:1635
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *tv)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:832
StatsThreadStore_::size
uint32_t size
Definition: counters.c:75
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:169
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
StatsThreadStore_
per thread store of counters
Definition: counters.c:68
StatsThreadStore_::head
StatsPublicThreadContext ** head
Definition: counters.c:74
ConfNode_::val
char * val
Definition: conf.h:34
StatsGlobalContext_
Holds the output interface context for the counter api.
Definition: counters.c:83
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:298
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:97
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:890
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:1758
StatsRegisterGlobalCounter
uint16_t StatsRegisterGlobalCounter(const char *name, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1014
STATS_MGMTT_TTS
#define STATS_MGMTT_TTS
Definition: counters.c:50
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
StatsCounter_::type
int type
Definition: counters.h:35
stats_stream_events
bool stats_stream_events
Definition: counters.c:106
PacketQueue_
simple fifo queue for packets with mutex and cond Calling the mutex or triggering the cond is respons...
Definition: packet-queue.h:47
TMM_STATSLOGGER
@ TMM_STATSLOGGER
Definition: tm-threads-common.h:63
THV_DEINIT
#define THV_DEINIT
Definition: threadvars.h:46
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
CountersIdType
struct CountersIdType_ CountersIdType
u8_tolower
#define u8_tolower(c)
Definition: suricata.h:178
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:34
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:308
StatsRecord_
Definition: output-stats.h:29
StatsCounter_::updates
uint64_t updates
Definition: counters.h:45
SC_WARN_NO_STATS_LOGGERS
@ SC_WARN_NO_STATS_LOGGERS
Definition: util-error.h:293
StatsSetUI64
void StatsSetUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Sets a value of type double to the local counter.
Definition: counters.c:212
StatsCounter_::name
const char * name
Definition: counters.h:52
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:253
StatsSetupPrivate
int StatsSetupPrivate(ThreadVars *tv)
Definition: counters.c:1211
StatsCounter_::Func
uint64_t(* Func)(void)
Definition: counters.h:49
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
HashTable_
Definition: util-hash.h:35
StatsCounter_::value
int64_t value
Definition: counters.h:44
tv_root
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:78
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:1031
SCDropCaps
#define SCDropCaps(...)
Definition: util-privs.h:37
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:33
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest.h
THV_PAUSE
#define THV_PAUSE
Definition: threadvars.h:39
STATS_TYPE_MAXIMUM
@ STATS_TYPE_MAXIMUM
Definition: counters.c:59
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:90
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
HashTableFree
void HashTableFree(HashTable *ht)
Definition: util-hash.c:79
ThreadVars_::cap_flags
uint8_t cap_flags
Definition: threadvars.h:81
ThreadVars_::perf_private_ctx
StatsPrivateThreadContext perf_private_ctx
Definition: threadvars.h:120
HashTable_::array_size
uint32_t array_size
Definition: util-hash.h:37
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
SC_ERR_STATS_NOT_INIT
@ SC_ERR_STATS_NOT_INIT
Definition: util-error.h:83
StatsThreadStore_::next
struct StatsThreadStore_ * next
Definition: counters.c:77
stats_decoder_events_prefix
const char * stats_decoder_events_prefix
Definition: counters.c:104
THV_RUNNING_DONE
#define THV_RUNNING_DONE
Definition: threadvars.h:47
TmThreadsUnsetFlag
void TmThreadsUnsetFlag(ThreadVars *tv, uint32_t flag)
Unset a thread flag.
Definition: tm-threads.c:105
util-signal.h
STATS_TYPE_FUNC
@ STATS_TYPE_FUNC
Definition: counters.c:60
SC_ERR_THREAD_INIT
@ SC_ERR_THREAD_INIT
Definition: util-error.h:79
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:330
StatsDecr
void StatsDecr(ThreadVars *tv, uint16_t id)
Decrements the local counter.
Definition: counters.c:190
counters.h
SC_ERR_STATS_LOG_GENERIC
@ SC_ERR_STATS_LOG_GENERIC
Definition: util-error.h:311
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:996
util-debug.h
CountersIdType_
Definition: counters.c:1029
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:55
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:895
TVT_MGMT
@ TVT_MGMT
Definition: tm-threads-common.h:90
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
ThreadVars_::perf_public_ctx
StatsPublicThreadContext perf_public_ctx
Definition: threadvars.h:126
StatsGlobalContext_::counters_id_hash
HashTable * counters_id_hash
Definition: counters.c:89
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:193
StatsRecord_::name
const char * name
Definition: output-stats.h:30
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
TmThreadTestThreadUnPaused
void TmThreadTestThreadUnPaused(ThreadVars *tv)
Tests if the thread represented in the arg has been unpaused or not.
Definition: tm-threads.c:1740
StatsThreadStore_::name
const char * name
Definition: counters.c:70
THV_KILL
#define THV_KILL
Definition: threadvars.h:41
STATS_TYPE_MAX
@ STATS_TYPE_MAX
Definition: counters.c:62
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:313
util-time.h
HashTableAdd
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:113
ThreadVars_::next
struct ThreadVars_ * next
Definition: threadvars.h:123
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:61
StatsLocalCounter_::pc
StatsCounter * pc
Definition: counters.h:81
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:281
StatsTable_
Definition: output-stats.h:36
tv_root_lock
SCMutex tv_root_lock
Definition: tm-threads.c:81
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:1257
StatsRecord_::value
int64_t value
Definition: output-stats.h:32
SCReturn
#define SCReturn
Definition: util-debug.h:302
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:210
CountersIdType_::id
uint16_t id
Definition: counters.c:1030
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
ThreadVars_::ctrl_cond
SCCtrlCondT * ctrl_cond
Definition: threadvars.h:131
conf.h
ThreadVars_::thread_group_name
char * thread_group_name
Definition: threadvars.h:67
StatsCounter_::id
uint16_t id
Definition: counters.h:38
ThreadVars_::thread_setup_flags
uint8_t thread_setup_flags
Definition: threadvars.h:69
TmEcode
TmEcode
Definition: tm-threads-common.h:81
StatsTable_::start_time
time_t start_time
Definition: output-stats.h:41
StatsLocalCounter_::id
uint16_t id
Definition: counters.h:84
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:96
SCCtrlCondTimedwait
#define SCCtrlCondTimedwait
Definition: threads-debug.h:385
runmodes.h
PacketQueue_::cond_q
SCCondT cond_q
Definition: packet-queue.h:55
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:1051
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
StatsLocalCounter_::value
int64_t value
Definition: counters.h:87
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
STATS_WUT_TTS
#define STATS_WUT_TTS
Definition: counters.c:47
Flow_::next
struct Flow_ * next
Definition: flow.h:407
StatsGlobalContext_::global_counter_ctx
StatsPublicThreadContext global_counter_ctx
Definition: counters.c:91
StatsTable_::stats
StatsRecord * stats
Definition: output-stats.h:37
TmModule_
Definition: tm-modules.h:43
StatsGlobalContext_::sts_lock
SCMutex sts_lock
Definition: counters.c:86
THV_PAUSED
#define THV_PAUSED
Definition: threadvars.h:40
THV_INIT_DONE
#define THV_INIT_DONE
Definition: threadvars.h:38
StatsPublicThreadContext_::m
SCMutex m
Definition: counters.h:72
StatsLocalCounter_
Storage for local counters, with a link to the public counter used for syncs.
Definition: counters.h:79
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
StatsGlobalContext_::sts_cnt
int sts_cnt
Definition: counters.c:87
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:72
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:120
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:907
StatsRecord_::tm_name
const char * tm_name
Definition: output-stats.h:31
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
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
STATS_TYPE_AVERAGE
@ STATS_TYPE_AVERAGE
Definition: counters.c:58
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
ConfGetChildValueBool
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
Definition: conf.c:485
StatsTable_::tstats
StatsRecord * tstats
Definition: output-stats.h:38
ThreadVars_::printable_name
char * printable_name
Definition: threadvars.h:66
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
StatsPublicThreadContext_::perf_flag
uint32_t perf_flag
Definition: counters.h:63
threadvars.h
ConfUnixSocketIsEnable
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:143
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:148
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
StatsCounter_::gid
uint16_t gid
Definition: counters.h:41
unix-manager.h
OutputStatsLoggersRegistered
int OutputStatsLoggersRegistered(void)
Definition: output-stats.c:206
head
Flow * head
Definition: flow-hash.h:1
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
SC_ERR_FATAL
@ SC_ERR_FATAL
Definition: util-error.h:203
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:103
StatsPrivateThreadContext_::size
uint32_t size
Definition: counters.h:101
StatsUpdateCounterArray
int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicThreadContext *pctx)
the private stats store with the public stats store
Definition: counters.c:1229
ConfValIsFalse
int ConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:546
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:34
StatsReleaseCounters
void StatsReleaseCounters(StatsCounter *head)
Releases counters.
Definition: counters.c:1281
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
suricata.h
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:66
GetDocURL
const char * GetDocURL(void)
Definition: suricata.c:1099
StatsGlobalContext_::sts
StatsThreadStore * sts
Definition: counters.c:85
StatsGlobalContext
struct StatsGlobalContext_ StatsGlobalContext
Holds the output interface context for the counter api.
STATS_TYPE_NORMAL
@ STATS_TYPE_NORMAL
Definition: counters.c:57
output-stats.h
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:976
ThreadVars_::ctrl_mutex
SCCtrlMutex * ctrl_mutex
Definition: threadvars.h:130
StatsThreadStore
struct StatsThreadStore_ StatsThreadStore
per thread store of counters
SC_ERR_THREAD_DEINIT
@ SC_ERR_THREAD_DEINIT
Definition: util-error.h:297
TVT_PPT
@ TVT_PPT
Definition: tm-threads-common.h:89
stats_decoder_events
bool stats_decoder_events
Definition: counters.c:103
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:89
StatsRegisterCounter
uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
Registers a normal, unqualified counter.
Definition: counters.c:956
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:69
THV_CLOSED
#define THV_CLOSED
Definition: threadvars.h:43
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
StatsReleaseResources
void StatsReleaseResources()
Releases the resources alloted by the Stats API.
Definition: counters.c:1269
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:120
StatsThreadCleanup
void StatsThreadCleanup(ThreadVars *tv)
Definition: counters.c:1310
StatsTable_::nstats
uint32_t nstats
Definition: output-stats.h:39
SCMutex
#define SCMutex
Definition: threads-debug.h:114
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:98
OutputStatsLog
TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
Definition: output-stats.c:87
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1571
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:798