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