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(SC_ERR_STATS_LOG_GENERIC, "global stats config is missing. "
267  "Stats enabled through legacy stats.log. "
268  "See %s/configuration/suricata-yaml.html#stats", GetDocURL());
269  }
270 
271  const char *interval = ConfNodeLookupChildValue(stats, "interval");
272  if (interval != NULL)
273  if (StringParseUint32(&stats_tts, 10, 0, interval) < 0) {
274  SCLogWarning(SC_ERR_INVALID_VALUE, "Invalid value for "
275  "interval: \"%s\". Resetting to %d.", interval,
277  stats_tts = STATS_MGMTT_TTS;
278  }
279 
280  int b;
281  int ret = ConfGetChildValueBool(stats, "decoder-events", &b);
282  if (ret) {
283  stats_decoder_events = (b == 1);
284  }
285  ret = ConfGetChildValueBool(stats, "stream-events", &b);
286  if (ret) {
287  stats_stream_events = (b == 1);
288  }
289 
290  const char *prefix = NULL;
291  if (ConfGet("stats.decoder-events-prefix", &prefix) != 1) {
292  prefix = "decoder.event";
293  }
295  }
296  SCReturn;
297 }
298 
299 static void StatsInitCtxPostOutput(void)
300 {
301  SCEnter();
302  /* Store the engine start time */
303  time(&stats_start_time);
304 
305  /* init the lock used by StatsThreadStore */
306  if (SCMutexInit(&stats_ctx->sts_lock, NULL) != 0) {
307  FatalError(SC_ERR_FATAL, "error initializing sts mutex");
308  }
309 
310  if (stats_enabled && !OutputStatsLoggersRegistered()) {
311  stats_loggers_active = 0;
312 
313  /* if the unix command socket is enabled we do the background
314  * stats sync just in case someone runs 'dump-counters' */
315  if (!ConfUnixSocketIsEnable()) {
316  SCLogWarning(SC_WARN_NO_STATS_LOGGERS, "stats are enabled but no loggers are active");
317  stats_enabled = false;
318  SCReturn;
319  }
320  }
321 
322  SCReturn;
323 }
324 
325 /**
326  * \brief Releases the resources alloted to the output context of the
327  * Stats API
328  */
329 static void StatsReleaseCtx(void)
330 {
331  if (stats_ctx == NULL) {
332  SCLogDebug("Counter module has been disabled");
333  return;
334  }
335 
336  StatsThreadStore *sts = NULL;
337  StatsThreadStore *temp = NULL;
338  sts = stats_ctx->sts;
339 
340  while (sts != NULL) {
341  if (sts->head != NULL)
342  SCFree(sts->head);
343 
344  temp = sts->next;
345  SCFree(sts);
346  sts = temp;
347  }
348 
349  if (stats_ctx->counters_id_hash != NULL) {
350  HashTableFree(stats_ctx->counters_id_hash);
351  stats_ctx->counters_id_hash = NULL;
352  counters_global_id = 0;
353  }
354 
355  StatsPublicThreadContextCleanup(&stats_ctx->global_counter_ctx);
356  SCFree(stats_ctx);
357  stats_ctx = NULL;
358 
359  SCMutexLock(&stats_table_mutex);
360  /* free stats table */
361  if (stats_table.tstats != NULL) {
362  SCFree(stats_table.tstats);
363  stats_table.tstats = NULL;
364  }
365 
366  if (stats_table.stats != NULL) {
367  SCFree(stats_table.stats);
368  stats_table.stats = NULL;
369  }
370  memset(&stats_table, 0, sizeof(stats_table));
371  SCMutexUnlock(&stats_table_mutex);
372 
373  return;
374 }
375 
376 /**
377  * \brief management thread. This thread is responsible for writing the stats
378  *
379  * \param arg thread var
380  *
381  * \retval NULL This is the value that is always returned
382  */
383 static void *StatsMgmtThread(void *arg)
384 {
385  ThreadVars *tv_local = (ThreadVars *)arg;
386 
387  SCSetThreadName(tv_local->name);
388 
389  if (tv_local->thread_setup_flags != 0)
390  TmThreadSetupOptions(tv_local);
391 
392  /* Set the threads capability */
393  tv_local->cap_flags = 0;
394  SCDropCaps(tv_local);
395 
396  if (stats_ctx == NULL) {
397  SCLogError(SC_ERR_STATS_NOT_INIT, "Stats API not init"
398  "StatsInitCounterApi() has to be called first");
400  return NULL;
401  }
402 
404  BUG_ON(tm->ThreadInit == NULL);
405  int r = tm->ThreadInit(tv_local, NULL, &stats_thread_data);
406  if (r != 0 || stats_thread_data == NULL) {
407  SCLogError(SC_ERR_THREAD_INIT, "Stats API "
408  "ThreadInit failed");
410  return NULL;
411  }
412  SCLogDebug("stats_thread_data %p", &stats_thread_data);
413 
415  while (1) {
416  if (TmThreadsCheckFlag(tv_local, THV_PAUSE)) {
417  TmThreadsSetFlag(tv_local, THV_PAUSED);
418  TmThreadTestThreadUnPaused(tv_local);
419  TmThreadsUnsetFlag(tv_local, THV_PAUSED);
420  }
421 
422  struct timeval cur_timev;
423  gettimeofday(&cur_timev, NULL);
424  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
425  cond_time.tv_sec += (stats_tts);
426 
427  /* wait for the set time, or until we are woken up by
428  * the shutdown procedure */
429  SCCtrlMutexLock(tv_local->ctrl_mutex);
430  SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
431  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
432 
433  SCMutexLock(&stats_table_mutex);
434  StatsOutput(tv_local);
435  SCMutexUnlock(&stats_table_mutex);
436 
437  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
438  break;
439  }
440  }
441 
443  TmThreadWaitForFlag(tv_local, THV_DEINIT);
444 
445  r = tm->ThreadDeinit(tv_local, stats_thread_data);
446  if (r != TM_ECODE_OK) {
447  SCLogError(SC_ERR_THREAD_DEINIT, "Stats Counter API "
448  "ThreadDeinit failed");
449  }
450 
451  TmThreadsSetFlag(tv_local, THV_CLOSED);
452  return NULL;
453 }
454 
455 /**
456  * \brief Wake up thread. This thread wakes up every TTS(time to sleep) seconds
457  * and sets the flag for every ThreadVars' StatsPublicThreadContext
458  *
459  * \param arg is NULL always
460  *
461  * \retval NULL This is the value that is always returned
462  */
463 static void *StatsWakeupThread(void *arg)
464 {
465  ThreadVars *tv_local = (ThreadVars *)arg;
466 
467  SCSetThreadName(tv_local->name);
468 
469  if (tv_local->thread_setup_flags != 0)
470  TmThreadSetupOptions(tv_local);
471 
472  /* Set the threads capability */
473  tv_local->cap_flags = 0;
474  SCDropCaps(tv_local);
475 
476  if (stats_ctx == NULL) {
477  SCLogError(SC_ERR_STATS_NOT_INIT, "Stats API not init"
478  "StatsInitCounterApi() has to be called first");
480  return NULL;
481  }
482 
484 
485  while (1) {
486  if (TmThreadsCheckFlag(tv_local, THV_PAUSE)) {
487  TmThreadsSetFlag(tv_local, THV_PAUSED);
488  TmThreadTestThreadUnPaused(tv_local);
489  TmThreadsUnsetFlag(tv_local, THV_PAUSED);
490  }
491 
492  struct timeval cur_timev;
493  gettimeofday(&cur_timev, NULL);
494  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
495  cond_time.tv_sec += STATS_WUT_TTS;
496 
497  /* wait for the set time, or until we are woken up by
498  * the shutdown procedure */
499  SCCtrlMutexLock(tv_local->ctrl_mutex);
500  SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
501  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
502 
505  while (tv != NULL) {
506  if (tv->perf_public_ctx.head == NULL) {
507  tv = tv->next;
508  continue;
509  }
510 
511  /* assuming the assignment of an int to be atomic, and even if it's
512  * not, it should be okay */
514 
515  if (tv->inq != NULL) {
516  PacketQueue *q = tv->inq->pq;
517  SCCondSignal(&q->cond_q);
518  }
519 
520  tv = tv->next;
521  }
522 
523  /* mgt threads for flow manager */
524  tv = tv_root[TVT_MGMT];
525  while (tv != NULL) {
526  if (tv->perf_public_ctx.head == NULL) {
527  tv = tv->next;
528  continue;
529  }
530 
531  /* assuming the assignment of an int to be atomic, and even if it's
532  * not, it should be okay */
534 
535  tv = tv->next;
536  }
538 
539  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
540  break;
541  }
542  }
543 
545  TmThreadWaitForFlag(tv_local, THV_DEINIT);
546  TmThreadsSetFlag(tv_local, THV_CLOSED);
547  return NULL;
548 }
549 
550 /**
551  * \brief Releases a counter
552  *
553  * \param pc Pointer to the StatsCounter to be freed
554  */
555 static void StatsReleaseCounter(StatsCounter *pc)
556 {
557  if (pc != NULL) {
558  SCFree(pc);
559  }
560 
561  return;
562 }
563 
564 /**
565  * \brief Registers a counter.
566  *
567  * \param name Name of the counter, to be registered
568  * \param tm_name Thread module to which this counter belongs
569  * \param pctx StatsPublicThreadContext for this tm-tv instance
570  * \param type_q Qualifier describing the type of counter to be registered
571  *
572  * \retval the counter id for the newly registered counter, or the already
573  * present counter on success
574  * \retval 0 on failure
575  */
576 static uint16_t StatsRegisterQualifiedCounter(const char *name, const char *tm_name,
578  int type_q, uint64_t (*Func)(void))
579 {
580  StatsCounter **head = &pctx->head;
581  StatsCounter *temp = NULL;
582  StatsCounter *prev = NULL;
583  StatsCounter *pc = NULL;
584 
585  if (name == NULL || pctx == NULL) {
586  SCLogDebug("Counter name, StatsPublicThreadContext NULL");
587  return 0;
588  }
589 
590  temp = prev = *head;
591  while (temp != NULL) {
592  prev = temp;
593 
594  if (strcmp(name, temp->name) == 0) {
595  break;
596  }
597 
598  temp = temp->next;
599  }
600 
601  /* We already have a counter registered by this name */
602  if (temp != NULL)
603  return(temp->id);
604 
605  /* if we reach this point we don't have a counter registered by this name */
606  if ( (pc = SCMalloc(sizeof(StatsCounter))) == NULL)
607  return 0;
608  memset(pc, 0, sizeof(StatsCounter));
609 
610  /* assign a unique id to this StatsCounter. The id is local to this
611  * thread context. Please note that the id start from 1, and not 0 */
612  pc->id = ++(pctx->curr_id);
613  pc->name = name;
614  pc->type = type_q;
615  pc->Func = Func;
616 
617  /* we now add the counter to the list */
618  if (prev == NULL)
619  *head = pc;
620  else
621  prev->next = pc;
622 
623  return pc->id;
624 }
625 
626 /**
627  * \brief Copies the StatsCounter value from the local counter present in the
628  * StatsPrivateThreadContext to its corresponding global counterpart. Used
629  * internally by StatsUpdateCounterArray()
630  *
631  * \param pcae Pointer to the StatsPrivateThreadContext which holds the local
632  * versions of the counters
633  */
634 static void StatsCopyCounterValue(StatsLocalCounter *pcae)
635 {
636  StatsCounter *pc = pcae->pc;
637 
638  pc->value = pcae->value;
639  pc->updates = pcae->updates;
640  return;
641 }
642 
643 /**
644  * \brief The output interface for the Stats API
645  */
646 static int StatsOutput(ThreadVars *tv)
647 {
648  const StatsThreadStore *sts = NULL;
649  const StatsCounter *pc = NULL;
650  void *td = stats_thread_data;
651 
652  if (counters_global_id == 0)
653  return -1;
654 
655  if (stats_table.nstats == 0) {
656  StatsThreadRegister("Global", &stats_ctx->global_counter_ctx);
657 
658  uint32_t nstats = counters_global_id;
659 
660  stats_table.nstats = nstats;
661  stats_table.stats = SCCalloc(stats_table.nstats, sizeof(StatsRecord));
662  if (stats_table.stats == NULL) {
663  stats_table.nstats = 0;
664  SCLogError(SC_ERR_MEM_ALLOC, "could not alloc memory for stats");
665  return -1;
666  }
667 
668  stats_table.ntstats = stats_ctx->sts_cnt;
669  uint32_t array_size = stats_table.nstats * sizeof(StatsRecord);
670  stats_table.tstats = SCCalloc(stats_table.ntstats, array_size);
671  if (stats_table.tstats == NULL) {
672  stats_table.ntstats = 0;
673  SCLogError(SC_ERR_MEM_ALLOC, "could not alloc memory for stats");
674  return -1;
675  }
676 
677  stats_table.start_time = stats_start_time;
678  }
679 
680  const uint16_t max_id = counters_global_id;
681  if (max_id == 0)
682  return -1;
683 
684  /** temporary local table to merge the per thread counters,
685  * especially needed for the average counters */
686  struct CountersMergeTable {
687  int type;
688  int64_t value;
689  uint64_t updates;
690  } merge_table[max_id];
691  memset(&merge_table, 0x00,
692  max_id * sizeof(struct CountersMergeTable));
693 
694  int thread = stats_ctx->sts_cnt - 1;
695  StatsRecord *table = stats_table.stats;
696 
697  /* Loop through the thread counter stores. The global counters
698  * are in a separate store inside this list. */
699  sts = stats_ctx->sts;
700  SCLogDebug("sts %p", sts);
701  while (sts != NULL) {
702  BUG_ON(thread < 0);
703 
704  SCLogDebug("Thread %d %s ctx %p", thread, sts->name, sts->ctx);
705 
706  /* temporary table for quickly storing the counters for this
707  * thread store, so that we can post process them outside
708  * of the thread store lock */
709  struct CountersMergeTable thread_table[max_id];
710  memset(&thread_table, 0x00,
711  max_id * sizeof(struct CountersMergeTable));
712 
713  SCMutexLock(&sts->ctx->m);
714  pc = sts->ctx->head;
715  while (pc != NULL) {
716  SCLogDebug("Counter %s (%u:%u) value %"PRIu64,
717  pc->name, pc->id, pc->gid, pc->value);
718 
719  thread_table[pc->gid].type = pc->type;
720  switch (pc->type) {
721  case STATS_TYPE_FUNC:
722  if (pc->Func != NULL)
723  thread_table[pc->gid].value = pc->Func();
724  break;
725  case STATS_TYPE_AVERAGE:
726  default:
727  thread_table[pc->gid].value = pc->value;
728  break;
729  }
730  thread_table[pc->gid].updates = pc->updates;
731  table[pc->gid].name = pc->name;
732 
733  pc = pc->next;
734  }
735  SCMutexUnlock(&sts->ctx->m);
736 
737  /* update merge table */
738  for (uint16_t c = 0; c < max_id; c++) {
739  struct CountersMergeTable *e = &thread_table[c];
740  /* thread only sets type if it has a counter
741  * of this type. */
742  if (e->type == 0)
743  continue;
744 
745  switch (e->type) {
746  case STATS_TYPE_MAXIMUM:
747  if (e->value > merge_table[c].value)
748  merge_table[c].value = e->value;
749  break;
750  case STATS_TYPE_FUNC:
751  merge_table[c].value = e->value;
752  break;
753  case STATS_TYPE_AVERAGE:
754  default:
755  merge_table[c].value += e->value;
756  break;
757  }
758  merge_table[c].updates += e->updates;
759  merge_table[c].type = e->type;
760  }
761 
762  /* update per thread stats table */
763  for (uint16_t c = 0; c < max_id; c++) {
764  struct CountersMergeTable *e = &thread_table[c];
765  /* thread only sets type if it has a counter
766  * of this type. */
767  if (e->type == 0)
768  continue;
769 
770  uint32_t offset = (thread * stats_table.nstats) + c;
771  StatsRecord *r = &stats_table.tstats[offset];
772  /* xfer previous value to pvalue and reset value */
773  r->pvalue = r->value;
774  r->value = 0;
775  r->name = table[c].name;
776  r->tm_name = sts->name;
777 
778  switch (e->type) {
779  case STATS_TYPE_AVERAGE:
780  if (e->value > 0 && e->updates > 0) {
781  r->value = (uint64_t)(e->value / e->updates);
782  }
783  break;
784  default:
785  r->value = e->value;
786  break;
787  }
788  }
789 
790  sts = sts->next;
791  thread--;
792  }
793 
794  /* transfer 'merge table' to final stats table */
795  for (uint16_t x = 0; x < max_id; x++) {
796  /* xfer previous value to pvalue and reset value */
797  table[x].pvalue = table[x].value;
798  table[x].value = 0;
799  table[x].tm_name = "Total";
800 
801  struct CountersMergeTable *m = &merge_table[x];
802  switch (m->type) {
803  case STATS_TYPE_MAXIMUM:
804  if (m->value > table[x].value)
805  table[x].value = m->value;
806  break;
807  case STATS_TYPE_AVERAGE:
808  if (m->value > 0 && m->updates > 0) {
809  table[x].value = (uint64_t)(m->value / m->updates);
810  }
811  break;
812  default:
813  table[x].value += m->value;
814  break;
815  }
816  }
817 
818  /* invoke logger(s) */
819  if (stats_loggers_active) {
820  OutputStatsLog(tv, td, &stats_table);
821  }
822  return 1;
823 }
824 
825 #ifdef BUILD_UNIX_SOCKET
826 /** \brief callback for getting stats into unix socket
827  */
828 TmEcode StatsOutputCounterSocket(json_t *cmd,
829  json_t *answer, void *data)
830 {
831  json_t *message = NULL;
832  TmEcode r = TM_ECODE_OK;
833 
834  if (!stats_enabled) {
835  r = TM_ECODE_FAILED;
836  message = json_string("stats are disabled in the config");
837  } else {
838  SCMutexLock(&stats_table_mutex);
839  if (stats_table.start_time == 0) {
840  r = TM_ECODE_FAILED;
841  message = json_string("stats not yet synchronized");
842  } else {
843  message = StatsToJSON(&stats_table, JSON_STATS_TOTALS|JSON_STATS_THREADS);
844  }
845  SCMutexUnlock(&stats_table_mutex);
846  }
847  json_object_set_new(answer, "message", message);
848  return r;
849 }
850 #endif /* BUILD_UNIX_SOCKET */
851 
852 static void StatsLogSummary(void)
853 {
854  if (!stats_enabled) {
855  return;
856  }
857  uint64_t alerts = 0;
858  SCMutexLock(&stats_table_mutex);
859  if (stats_table.start_time != 0) {
860  const StatsTable *st = &stats_table;
861  for (uint32_t u = 0; u < st->nstats; u++) {
862  const char *name = st->stats[u].name;
863  if (name == NULL || strcmp(name, "detect.alert") != 0)
864  continue;
865  alerts = st->stats[u].value;
866  break;
867  }
868  }
869  SCMutexUnlock(&stats_table_mutex);
870  SCLogInfo("Alerts: %"PRIu64, alerts);
871 }
872 
873 /**
874  * \brief Initializes the perf counter api. Things are hard coded currently.
875  * More work to be done when we implement multiple interfaces
876  */
877 void StatsInit(void)
878 {
879  BUG_ON(stats_ctx != NULL);
880  if ( (stats_ctx = SCMalloc(sizeof(StatsGlobalContext))) == NULL) {
882  "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(SC_ERR_FATAL, "TmThreadCreateMgmtThread "
922  "failed");
923  }
924 
925  if (TmThreadSpawn(tv_wakeup) != 0) {
926  FatalError(SC_ERR_FATAL, "TmThreadSpawn failed for "
927  "StatsWakeupThread");
928  }
929 
930  /* spawn the stats mgmt thread */
932  StatsMgmtThread, 1);
933  if (tv_mgmt == NULL) {
934  FatalError(SC_ERR_FATAL, "TmThreadCreateMgmtThread failed");
935  }
936 
937  if (TmThreadSpawn(tv_mgmt) != 0) {
938  FatalError(SC_ERR_FATAL, "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)
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: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:1639
TmThreadSetupOptions
TmEcode TmThreadSetupOptions(ThreadVars *tv)
Set the thread options (cpu affinitythread). Priority should be already set by pthread_create.
Definition: tm-threads.c:837
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
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
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:296
TmThreadsSetFlag
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
Definition: tm-threads.c:98
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:1762
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:176
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:301
util-hash.h
StatsRecord_
Definition: output-stats.h:31
StatsCounter_::updates
uint64_t updates
Definition: counters.h:47
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: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:79
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:877
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
SC_ERR_STATS_NOT_INIT
@ SC_ERR_STATS_NOT_INIT
Definition: util-error.h:83
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:106
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:331
StatsDecr
void StatsDecr(ThreadVars *tv, uint16_t id)
Decrements the local counter.
Definition: counters.c:188
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: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:298
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:1744
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
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:82
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:300
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: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:1055
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:215
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:407
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
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255
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(x,...)
Definition: util-debug.h:530
ConfGetChildValueBool
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
Definition: conf.c:486
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
head
Flow * head
Definition: flow-hash.h:1
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:242
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: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:547
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
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
suricata.h
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:68
GetDocURL
const char * GetDocURL(void)
Definition: suricata.c:1102
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
SC_ERR_THREAD_DEINIT
@ SC_ERR_THREAD_DEINIT
Definition: util-error.h:297
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:90
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
StatsReleaseResources
void StatsReleaseResources()
Releases the resources alloted by the Stats API.
Definition: counters.c:1268
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:799