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