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