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