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