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  uint8_t run = 1;
360 
361  /* Set the thread name */
362  if (SCSetThreadName(tv_local->name) < 0) {
363  SCLogWarning(SC_ERR_THREAD_INIT, "Unable to set thread name");
364  }
365 
366  if (tv_local->thread_setup_flags != 0)
367  TmThreadSetupOptions(tv_local);
368 
369  /* Set the threads capability */
370  tv_local->cap_flags = 0;
371 
372  SCDropCaps(tv_local);
373 
374  if (stats_ctx == NULL) {
375  SCLogError(SC_ERR_STATS_NOT_INIT, "Stats API not init"
376  "StatsInitCounterApi() has to be called first");
378  return NULL;
379  }
380 
382  BUG_ON(tm->ThreadInit == NULL);
383  int r = tm->ThreadInit(tv_local, NULL, &stats_thread_data);
384  if (r != 0 || stats_thread_data == NULL) {
385  SCLogError(SC_ERR_THREAD_INIT, "Stats API "
386  "ThreadInit failed");
388  return NULL;
389  }
390  SCLogDebug("stats_thread_data %p", &stats_thread_data);
391 
392  TmThreadsSetFlag(tv_local, THV_INIT_DONE);
393  while (run) {
394  if (TmThreadsCheckFlag(tv_local, THV_PAUSE)) {
395  TmThreadsSetFlag(tv_local, THV_PAUSED);
396  TmThreadTestThreadUnPaused(tv_local);
397  TmThreadsUnsetFlag(tv_local, THV_PAUSED);
398  }
399 
400  struct timeval cur_timev;
401  gettimeofday(&cur_timev, NULL);
402 
403  struct timespec cond_time = FROM_TIMEVAL(cur_timev);
404  cond_time.tv_sec += (stats_tts);
405 
406  /* wait for the set time, or until we are woken up by
407  * the shutdown procedure */
408  SCCtrlMutexLock(tv_local->ctrl_mutex);
409  SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
410  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
411 
412  SCMutexLock(&stats_table_mutex);
413  StatsOutput(tv_local);
414  SCMutexUnlock(&stats_table_mutex);
415 
416  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
417  run = 0;
418  }
419  }
420 
422  TmThreadWaitForFlag(tv_local, THV_DEINIT);
423 
424  r = tm->ThreadDeinit(tv_local, stats_thread_data);
425  if (r != TM_ECODE_OK) {
426  SCLogError(SC_ERR_THREAD_DEINIT, "Stats Counter API "
427  "ThreadDeinit failed");
428  }
429 
430  TmThreadsSetFlag(tv_local, THV_CLOSED);
431  return NULL;
432 }
433 
434 /**
435  * \brief Wake up thread. This thread wakes up every TTS(time to sleep) seconds
436  * and sets the flag for every ThreadVars' StatsPublicThreadContext
437  *
438  * \param arg is NULL always
439  *
440  * \retval NULL This is the value that is always returned
441  */
442 static void *StatsWakeupThread(void *arg)
443 {
444  ThreadVars *tv_local = (ThreadVars *)arg;
445  uint8_t run = 1;
446  ThreadVars *tv = NULL;
447  PacketQueue *q = NULL;
448  struct timespec cond_time;
449 
450  /* Set the thread name */
451  if (SCSetThreadName(tv_local->name) < 0) {
452  SCLogWarning(SC_ERR_THREAD_INIT, "Unable to set thread name");
453  }
454 
455  if (tv_local->thread_setup_flags != 0)
456  TmThreadSetupOptions(tv_local);
457 
458  /* Set the threads capability */
459  tv_local->cap_flags = 0;
460 
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 (run) {
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  cond_time.tv_sec = time(NULL) + STATS_WUT_TTS;
479  cond_time.tv_nsec = 0;
480 
481  /* wait for the set time, or until we are woken up by
482  * the shutdown procedure */
483  SCCtrlMutexLock(tv_local->ctrl_mutex);
484  SCCtrlCondTimedwait(tv_local->ctrl_cond, tv_local->ctrl_mutex, &cond_time);
485  SCCtrlMutexUnlock(tv_local->ctrl_mutex);
486 
487  tv = tv_root[TVT_PPT];
488  while (tv != NULL) {
489  if (tv->perf_public_ctx.head == NULL) {
490  tv = tv->next;
491  continue;
492  }
493 
494  /* assuming the assignment of an int to be atomic, and even if it's
495  * not, it should be okay */
496  tv->perf_public_ctx.perf_flag = 1;
497 
498  if (tv->inq != NULL) {
499  q = &trans_q[tv->inq->id];
500  SCCondSignal(&q->cond_q);
501  }
502 
503  tv = tv->next;
504  }
505 
506  /* mgt threads for flow manager */
507  tv = tv_root[TVT_MGMT];
508  while (tv != NULL) {
509  if (tv->perf_public_ctx.head == NULL) {
510  tv = tv->next;
511  continue;
512  }
513 
514  /* assuming the assignment of an int to be atomic, and even if it's
515  * not, it should be okay */
516  tv->perf_public_ctx.perf_flag = 1;
517 
518  tv = tv->next;
519  }
520 
521  if (TmThreadsCheckFlag(tv_local, THV_KILL)) {
522  run = 0;
523  }
524  }
525 
527  TmThreadWaitForFlag(tv_local, THV_DEINIT);
528 
529  TmThreadsSetFlag(tv_local, THV_CLOSED);
530  return NULL;
531 }
532 
533 /**
534  * \brief Releases a counter
535  *
536  * \param pc Pointer to the StatsCounter to be freed
537  */
538 static void StatsReleaseCounter(StatsCounter *pc)
539 {
540  if (pc != NULL) {
541  SCFree(pc);
542  }
543 
544  return;
545 }
546 
547 /**
548  * \brief Registers a counter.
549  *
550  * \param name Name of the counter, to be registered
551  * \param tm_name Thread module to which this counter belongs
552  * \param pctx StatsPublicThreadContext for this tm-tv instance
553  * \param type_q Qualifier describing the type of counter to be registered
554  *
555  * \retval the counter id for the newly registered counter, or the already
556  * present counter on success
557  * \retval 0 on failure
558  */
559 static uint16_t StatsRegisterQualifiedCounter(const char *name, const char *tm_name,
561  int type_q, uint64_t (*Func)(void))
562 {
563  StatsCounter **head = &pctx->head;
564  StatsCounter *temp = NULL;
565  StatsCounter *prev = NULL;
566  StatsCounter *pc = NULL;
567 
568  if (name == NULL || pctx == NULL) {
569  SCLogDebug("Counter name, StatsPublicThreadContext NULL");
570  return 0;
571  }
572 
573  temp = prev = *head;
574  while (temp != NULL) {
575  prev = temp;
576 
577  if (strcmp(name, temp->name) == 0) {
578  break;
579  }
580 
581  temp = temp->next;
582  }
583 
584  /* We already have a counter registered by this name */
585  if (temp != NULL)
586  return(temp->id);
587 
588  /* if we reach this point we don't have a counter registered by this name */
589  if ( (pc = SCMalloc(sizeof(StatsCounter))) == NULL)
590  return 0;
591  memset(pc, 0, sizeof(StatsCounter));
592 
593  /* assign a unique id to this StatsCounter. The id is local to this
594  * thread context. Please note that the id start from 1, and not 0 */
595  pc->id = ++(pctx->curr_id);
596  pc->name = name;
597  pc->type = type_q;
598  pc->Func = Func;
599 
600  /* we now add the counter to the list */
601  if (prev == NULL)
602  *head = pc;
603  else
604  prev->next = pc;
605 
606  return pc->id;
607 }
608 
609 /**
610  * \brief Copies the StatsCounter value from the local counter present in the
611  * StatsPrivateThreadContext to its corresponding global counterpart. Used
612  * internally by StatsUpdateCounterArray()
613  *
614  * \param pcae Pointer to the StatsPrivateThreadContext which holds the local
615  * versions of the counters
616  */
617 static void StatsCopyCounterValue(StatsLocalCounter *pcae)
618 {
619  StatsCounter *pc = pcae->pc;
620 
621  pc->value = pcae->value;
622  pc->updates = pcae->updates;
623  return;
624 }
625 
626 /**
627  * \brief The output interface for the Stats API
628  */
629 static int StatsOutput(ThreadVars *tv)
630 {
631  const StatsThreadStore *sts = NULL;
632  const StatsCounter *pc = NULL;
633  void *td = stats_thread_data;
634 
635  if (counters_global_id == 0)
636  return -1;
637 
638  if (stats_table.nstats == 0) {
639  StatsThreadRegister("Global", &stats_ctx->global_counter_ctx);
640 
641  uint32_t nstats = counters_global_id;
642 
643  stats_table.nstats = nstats;
644  stats_table.stats = SCCalloc(stats_table.nstats, sizeof(StatsRecord));
645  if (stats_table.stats == NULL) {
646  stats_table.nstats = 0;
647  SCLogError(SC_ERR_MEM_ALLOC, "could not alloc memory for stats");
648  return -1;
649  }
650 
651  stats_table.ntstats = stats_ctx->sts_cnt;
652  uint32_t array_size = stats_table.nstats * sizeof(StatsRecord);
653  stats_table.tstats = SCCalloc(stats_table.ntstats, array_size);
654  if (stats_table.tstats == NULL) {
655  stats_table.ntstats = 0;
656  SCLogError(SC_ERR_MEM_ALLOC, "could not alloc memory for stats");
657  return -1;
658  }
659 
660  stats_table.start_time = stats_start_time;
661  }
662 
663  const uint16_t max_id = counters_global_id;
664  if (max_id == 0)
665  return -1;
666 
667  /** temporary local table to merge the per thread counters,
668  * especially needed for the average counters */
669  struct CountersMergeTable {
670  int type;
671  uint64_t value;
672  uint64_t updates;
673  } merge_table[max_id];
674  memset(&merge_table, 0x00,
675  max_id * sizeof(struct CountersMergeTable));
676 
677  int thread = stats_ctx->sts_cnt - 1;
678  StatsRecord *table = stats_table.stats;
679 
680  /* Loop through the thread counter stores. The global counters
681  * are in a separate store inside this list. */
682  sts = stats_ctx->sts;
683  SCLogDebug("sts %p", sts);
684  while (sts != NULL) {
685  BUG_ON(thread < 0);
686 
687  SCLogDebug("Thread %d %s ctx %p", thread, sts->name, sts->ctx);
688 
689  /* temporay table for quickly storing the counters for this
690  * thread store, so that we can post process them outside
691  * of the thread store lock */
692  struct CountersMergeTable thread_table[max_id];
693  memset(&thread_table, 0x00,
694  max_id * sizeof(struct CountersMergeTable));
695 
696  SCMutexLock(&sts->ctx->m);
697  pc = sts->ctx->head;
698  while (pc != NULL) {
699  SCLogDebug("Counter %s (%u:%u) value %"PRIu64,
700  pc->name, pc->id, pc->gid, pc->value);
701 
702  thread_table[pc->gid].type = pc->type;
703  switch (pc->type) {
704  case STATS_TYPE_FUNC:
705  if (pc->Func != NULL)
706  thread_table[pc->gid].value = pc->Func();
707  break;
708  case STATS_TYPE_AVERAGE:
709  default:
710  thread_table[pc->gid].value = pc->value;
711  break;
712  }
713  thread_table[pc->gid].updates = pc->updates;
714  table[pc->gid].name = pc->name;
715 
716  pc = pc->next;
717  }
718  SCMutexUnlock(&sts->ctx->m);
719 
720  /* update merge table */
721  uint16_t c;
722  for (c = 0; c < max_id; c++) {
723  struct CountersMergeTable *e = &thread_table[c];
724  /* thread only sets type if it has a counter
725  * of this type. */
726  if (e->type == 0)
727  continue;
728 
729  switch (e->type) {
730  case STATS_TYPE_MAXIMUM:
731  if (e->value > merge_table[c].value)
732  merge_table[c].value = e->value;
733  break;
734  case STATS_TYPE_FUNC:
735  merge_table[c].value = e->value;
736  break;
737  case STATS_TYPE_AVERAGE:
738  default:
739  merge_table[c].value += e->value;
740  break;
741  }
742  merge_table[c].updates += e->updates;
743  merge_table[c].type = e->type;
744  }
745 
746  /* update per thread stats table */
747  for (c = 0; c < max_id; c++) {
748  struct CountersMergeTable *e = &thread_table[c];
749  /* thread only sets type if it has a counter
750  * of this type. */
751  if (e->type == 0)
752  continue;
753 
754  uint32_t offset = (thread * stats_table.nstats) + c;
755  StatsRecord *r = &stats_table.tstats[offset];
756  /* xfer previous value to pvalue and reset value */
757  r->pvalue = r->value;
758  r->value = 0;
759  r->name = table[c].name;
760  r->tm_name = sts->name;
761 
762  switch (e->type) {
763  case STATS_TYPE_AVERAGE:
764  if (e->value > 0 && e->updates > 0) {
765  r->value = (uint64_t)(e->value / e->updates);
766  }
767  break;
768  default:
769  r->value = e->value;
770  break;
771  }
772  }
773 
774  sts = sts->next;
775  thread--;
776  }
777 
778  /* transfer 'merge table' to final stats table */
779  uint16_t x;
780  for (x = 0; x < max_id; x++) {
781  /* xfer previous value to pvalue and reset value */
782  table[x].pvalue = table[x].value;
783  table[x].value = 0;
784  table[x].tm_name = "Total";
785 
786  struct CountersMergeTable *m = &merge_table[x];
787  switch (m->type) {
788  case STATS_TYPE_MAXIMUM:
789  if (m->value > table[x].value)
790  table[x].value = m->value;
791  break;
792  case STATS_TYPE_AVERAGE:
793  if (m->value > 0 && m->updates > 0) {
794  table[x].value = (uint64_t)(m->value / m->updates);
795  }
796  break;
797  default:
798  table[x].value += m->value;
799  break;
800  }
801  }
802 
803  /* invoke logger(s) */
804  if (stats_loggers_active) {
805  OutputStatsLog(tv, td, &stats_table);
806  }
807  return 1;
808 }
809 
810 #ifdef BUILD_UNIX_SOCKET
811 /** \brief callback for getting stats into unix socket
812  */
813 TmEcode StatsOutputCounterSocket(json_t *cmd,
814  json_t *answer, void *data)
815 {
816  json_t *message = NULL;
817  TmEcode r = TM_ECODE_OK;
818 
819  SCMutexLock(&stats_table_mutex);
820  if (stats_table.start_time == 0) {
821  r = TM_ECODE_FAILED;
822  message = json_string("stats not yet synchronized");
823  } else {
824  message = StatsToJSON(&stats_table, JSON_STATS_TOTALS|JSON_STATS_THREADS);
825  }
826  SCMutexUnlock(&stats_table_mutex);
827 
828  json_object_set_new(answer, "message", message);
829  return r;
830 }
831 #endif /* BUILD_UNIX_SOCKET */
832 
833 static void StatsLogSummary(void)
834 {
835  uint64_t alerts = 0;
836  SCMutexLock(&stats_table_mutex);
837  if (stats_table.start_time != 0) {
838  const StatsTable *st = &stats_table;
839  uint32_t u = 0;
840  for (u = 0; u < st->nstats; u++) {
841  const char *name = st->stats[u].name;
842  if (name == NULL || strcmp(name, "detect.alert") != 0)
843  continue;
844  alerts = st->stats[u].value;
845  break;
846  }
847  }
848  SCMutexUnlock(&stats_table_mutex);
849  SCLogInfo("Alerts: %"PRIu64, alerts);
850 }
851 
852 /**
853  * \brief Initializes the perf counter api. Things are hard coded currently.
854  * More work to be done when we implement multiple interfaces
855  */
856 void StatsInit(void)
857 {
858  BUG_ON(stats_ctx != NULL);
859  if ( (stats_ctx = SCMalloc(sizeof(StatsGlobalContext))) == NULL) {
860  SCLogError(SC_ERR_FATAL, "Fatal error encountered in StatsInitCtx. Exiting...");
861  exit(EXIT_FAILURE);
862  }
863  memset(stats_ctx, 0, sizeof(StatsGlobalContext));
864 
865  StatsPublicThreadContextInit(&stats_ctx->global_counter_ctx);
866 }
867 
869 {
870  StatsInitCtxPreOutput();
871 }
872 
874 {
875  StatsInitCtxPostOutput();
876 }
877 
878 
879 /**
880  * \brief Spawns the wakeup, and the management thread used by the stats api
881  *
882  * The threads use the condition variable in the thread vars to control
883  * their wait loops to make sure the main thread can quickly kill them.
884  */
886 {
887  SCEnter();
888 
889  if (!stats_enabled) {
890  SCReturn;
891  }
892 
893  ThreadVars *tv_wakeup = NULL;
894  ThreadVars *tv_mgmt = NULL;
895 
896  /* spawn the stats wakeup thread */
898  StatsWakeupThread, 1);
899  if (tv_wakeup == NULL) {
900  SCLogError(SC_ERR_THREAD_CREATE, "TmThreadCreateMgmtThread "
901  "failed");
902  exit(EXIT_FAILURE);
903  }
904 
905  if (TmThreadSpawn(tv_wakeup) != 0) {
906  SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed for "
907  "StatsWakeupThread");
908  exit(EXIT_FAILURE);
909  }
910 
911  /* spawn the stats mgmt thread */
913  StatsMgmtThread, 1);
914  if (tv_mgmt == NULL) {
916  "TmThreadCreateMgmtThread failed");
917  exit(EXIT_FAILURE);
918  }
919 
920  if (TmThreadSpawn(tv_mgmt) != 0) {
921  SCLogError(SC_ERR_THREAD_SPAWN, "TmThreadSpawn failed for "
922  "StatsWakeupThread");
923  exit(EXIT_FAILURE);
924  }
925 
926  SCReturn;
927 }
928 
929 /**
930  * \brief Registers a normal, unqualified counter
931  *
932  * \param name Name of the counter, to be registered
933  * \param tv Pointer to the ThreadVars instance for which the counter would
934  * be registered
935  *
936  * \retval id Counter id for the newly registered counter, or the already
937  * present counter
938  */
939 uint16_t StatsRegisterCounter(const char *name, struct ThreadVars_ *tv)
940 {
941  uint16_t id = StatsRegisterQualifiedCounter(name,
942  (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->printable_name,
943  &tv->perf_public_ctx,
944  STATS_TYPE_NORMAL, NULL);
945 
946  return id;
947 }
948 
949 /**
950  * \brief Registers a counter, whose value holds the average of all the values
951  * assigned to it.
952  *
953  * \param name Name of the counter, to be registered
954  * \param tv Pointer to the ThreadVars instance for which the counter would
955  * be registered
956  *
957  * \retval id Counter id for the newly registered counter, or the already
958  * present counter
959  */
960 uint16_t StatsRegisterAvgCounter(const char *name, struct ThreadVars_ *tv)
961 {
962  uint16_t id = StatsRegisterQualifiedCounter(name,
963  (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->printable_name,
964  &tv->perf_public_ctx,
965  STATS_TYPE_AVERAGE, NULL);
966 
967  return id;
968 }
969 
970 /**
971  * \brief Registers a counter, whose value holds the maximum of all the values
972  * assigned to it.
973  *
974  * \param name Name of the counter, to be registered
975  * \param tv Pointer to the ThreadVars instance for which the counter would
976  * be registered
977  *
978  * \retval the counter id for the newly registered counter, or the already
979  * present counter
980  */
981 uint16_t StatsRegisterMaxCounter(const char *name, struct ThreadVars_ *tv)
982 {
983  uint16_t id = StatsRegisterQualifiedCounter(name,
984  (tv->thread_group_name != NULL) ? tv->thread_group_name : tv->printable_name,
985  &tv->perf_public_ctx,
986  STATS_TYPE_MAXIMUM, NULL);
987 
988  return id;
989 }
990 
991 /**
992  * \brief Registers a counter, which represents a global value
993  *
994  * \param name Name of the counter, to be registered
995  * \param Func Function Pointer returning a uint64_t
996  *
997  * \retval id Counter id for the newly registered counter, or the already
998  * present counter
999  */
1000 uint16_t StatsRegisterGlobalCounter(const char *name, uint64_t (*Func)(void))
1001 {
1002 #ifdef UNITTESTS
1003  if (stats_ctx == NULL)
1004  return 0;
1005 #else
1006  BUG_ON(stats_ctx == NULL);
1007 #endif
1008  uint16_t id = StatsRegisterQualifiedCounter(name, NULL,
1009  &(stats_ctx->global_counter_ctx),
1011  Func);
1012  return id;
1013 }
1014 
1015 typedef struct CountersIdType_ {
1016  uint16_t id;
1017  const char *string;
1018 } CountersIdType;
1019 
1020 static uint32_t CountersIdHashFunc(HashTable *ht, void *data, uint16_t datalen)
1021 {
1022  CountersIdType *t = (CountersIdType *)data;
1023  uint32_t hash = 0;
1024  int i = 0;
1025 
1026  int len = strlen(t->string);
1027 
1028  for (i = 0; i < len; i++)
1029  hash += tolower((unsigned char)t->string[i]);
1030 
1031  hash = hash % ht->array_size;
1032 
1033  return hash;
1034 }
1035 
1036 static char CountersIdHashCompareFunc(void *data1, uint16_t datalen1,
1037  void *data2, uint16_t datalen2)
1038 {
1039  CountersIdType *t1 = (CountersIdType *)data1;
1040  CountersIdType *t2 = (CountersIdType *)data2;
1041  int len1 = 0;
1042  int len2 = 0;
1043 
1044  if (t1 == NULL || t2 == NULL)
1045  return 0;
1046 
1047  if (t1->string == NULL || t2->string == NULL)
1048  return 0;
1049 
1050  len1 = strlen(t1->string);
1051  len2 = strlen(t2->string);
1052 
1053  if (len1 == len2 && memcmp(t1->string, t2->string, len1) == 0) {
1054  return 1;
1055  }
1056 
1057  return 0;
1058 }
1059 
1060 static void CountersIdHashFreeFunc(void *data)
1061 {
1062  SCFree(data);
1063 }
1064 
1065 
1066 /** \internal
1067  * \brief Adds a TM to the clubbed TM table. Multiple instances of the same TM
1068  * are stacked together in a PCTMI container.
1069  *
1070  * \param tm_name Name of the tm to be added to the table
1071  * \param pctx StatsPublicThreadContext associated with the TM tm_name
1072  *
1073  * \retval 1 on success, 0 on failure
1074  */
1075 static int StatsThreadRegister(const char *thread_name, StatsPublicThreadContext *pctx)
1076 {
1077  if (stats_ctx == NULL) {
1078  SCLogDebug("Counter module has been disabled");
1079  return 0;
1080  }
1081 
1082  StatsThreadStore *temp = NULL;
1083 
1084  if (thread_name == NULL || pctx == NULL) {
1085  SCLogDebug("supplied argument(s) to StatsThreadRegister NULL");
1086  return 0;
1087  }
1088 
1089  SCMutexLock(&stats_ctx->sts_lock);
1090  if (stats_ctx->counters_id_hash == NULL) {
1091  stats_ctx->counters_id_hash = HashTableInit(256, CountersIdHashFunc,
1092  CountersIdHashCompareFunc,
1093  CountersIdHashFreeFunc);
1094  BUG_ON(stats_ctx->counters_id_hash == NULL);
1095  }
1096  StatsCounter *pc = pctx->head;
1097  while (pc != NULL) {
1098  CountersIdType t = { 0, pc->name }, *id = NULL;
1099  id = HashTableLookup(stats_ctx->counters_id_hash, &t, sizeof(t));
1100  if (id == NULL) {
1101  id = SCCalloc(1, sizeof(*id));
1102  BUG_ON(id == NULL);
1103  id->id = counters_global_id++;
1104  id->string = pc->name;
1105  BUG_ON(HashTableAdd(stats_ctx->counters_id_hash, id, sizeof(*id)) < 0);
1106  }
1107  pc->gid = id->id;
1108  pc = pc->next;
1109  }
1110 
1111 
1112  if ( (temp = SCMalloc(sizeof(StatsThreadStore))) == NULL) {
1113  SCMutexUnlock(&stats_ctx->sts_lock);
1114  return 0;
1115  }
1116  memset(temp, 0, sizeof(StatsThreadStore));
1117 
1118  temp->ctx = pctx;
1119  temp->name = thread_name;
1120 
1121  temp->next = stats_ctx->sts;
1122  stats_ctx->sts = temp;
1123  stats_ctx->sts_cnt++;
1124  SCLogDebug("stats_ctx->sts %p", stats_ctx->sts);
1125 
1126  SCMutexUnlock(&stats_ctx->sts_lock);
1127  return 1;
1128 }
1129 
1130 /** \internal
1131  * \brief Returns a counter array for counters in this id range(s_id - e_id)
1132  *
1133  * \param s_id Counter id of the first counter to be added to the array
1134  * \param e_id Counter id of the last counter to be added to the array
1135  * \param pctx Pointer to the tv's StatsPublicThreadContext
1136  *
1137  * \retval a counter-array in this(s_id-e_id) range for this TM instance
1138  */
1139 static int StatsGetCounterArrayRange(uint16_t s_id, uint16_t e_id,
1142 {
1143  StatsCounter *pc = NULL;
1144  uint32_t i = 0;
1145 
1146  if (pctx == NULL || pca == NULL) {
1147  SCLogDebug("pctx/pca is NULL");
1148  return -1;
1149  }
1150 
1151  if (s_id < 1 || e_id < 1 || s_id > e_id) {
1152  SCLogDebug("error with the counter ids");
1153  return -1;
1154  }
1155 
1156  if (e_id > pctx->curr_id) {
1157  SCLogDebug("end id is greater than the max id for this tv");
1158  return -1;
1159  }
1160 
1161  if ( (pca->head = SCMalloc(sizeof(StatsLocalCounter) * (e_id - s_id + 2))) == NULL) {
1162  return -1;
1163  }
1164  memset(pca->head, 0, sizeof(StatsLocalCounter) * (e_id - s_id + 2));
1165 
1166  pc = pctx->head;
1167  while (pc->id != s_id)
1168  pc = pc->next;
1169 
1170  i = 1;
1171  while ((pc != NULL) && (pc->id <= e_id)) {
1172  pca->head[i].pc = pc;
1173  pca->head[i].id = pc->id;
1174  pc = pc->next;
1175  i++;
1176  }
1177  pca->size = i - 1;
1178 
1179  pca->initialized = 1;
1180  return 0;
1181 }
1182 
1183 /** \internal
1184  * \brief Returns a counter array for all counters registered for this tm
1185  * instance
1186  *
1187  * \param pctx Pointer to the tv's StatsPublicThreadContext
1188  *
1189  * \retval pca Pointer to a counter-array for all counter of this tm instance
1190  * on success; NULL on failure
1191  */
1192 static int StatsGetAllCountersArray(StatsPublicThreadContext *pctx, StatsPrivateThreadContext *private)
1193 {
1194  if (pctx == NULL || private == NULL)
1195  return -1;
1196 
1197  return StatsGetCounterArrayRange(1, pctx->curr_id, pctx, private);
1198 }
1199 
1200 
1202 {
1203  StatsGetAllCountersArray(&(tv)->perf_public_ctx, &(tv)->perf_private_ctx);
1204 
1205  StatsThreadRegister(tv->printable_name ? tv->printable_name : tv->name,
1206  &(tv)->perf_public_ctx);
1207  return 0;
1208 }
1209 
1210 /**
1211  * \brief Syncs the counter array with the global counter variables
1212  *
1213  * \param pca Pointer to the StatsPrivateThreadContext
1214  * \param pctx Pointer the the tv's StatsPublicThreadContext
1215  *
1216  * \retval 0 on success
1217  * \retval -1 on error
1218  */
1220 {
1221  StatsLocalCounter *pcae = NULL;
1222  uint32_t i = 0;
1223 
1224  if (pca == NULL || pctx == NULL) {
1225  SCLogDebug("pca or pctx is NULL inside StatsUpdateCounterArray");
1226  return -1;
1227  }
1228 
1229  pcae = pca->head;
1230 
1231  SCMutexLock(&pctx->m);
1232  for (i = 1; i <= pca->size; i++) {
1233  StatsCopyCounterValue(&pcae[i]);
1234  }
1235  SCMutexUnlock(&pctx->m);
1236 
1237  pctx->perf_flag = 0;
1238 
1239  return 1;
1240 }
1241 
1242 /**
1243  * \brief Get the value of the local copy of the counter that hold this id.
1244  *
1245  * \param tv threadvars
1246  * \param id The counter id.
1247  *
1248  * \retval 0 on success.
1249  * \retval -1 on error.
1250  */
1251 uint64_t StatsGetLocalCounterValue(ThreadVars *tv, uint16_t id)
1252 {
1254 #ifdef DEBUG
1255  BUG_ON ((id < 1) || (id > pca->size));
1256 #endif
1257  return pca->head[id].value;
1258 }
1259 
1260 /**
1261  * \brief Releases the resources alloted by the Stats API
1262  */
1264 {
1265  StatsLogSummary();
1266  StatsReleaseCtx();
1267 
1268  return;
1269 }
1270 
1271 /**
1272  * \brief Releases counters
1273  *
1274  * \param head Pointer to the head of the list of perf counters that have to
1275  * be freed
1276  */
1278 {
1279  StatsCounter *pc = NULL;
1280 
1281  while (head != NULL) {
1282  pc = head;
1283  head = head->next;
1284  StatsReleaseCounter(pc);
1285  }
1286 
1287  return;
1288 }
1289 
1290 /** \internal
1291  * \brief Releases the StatsPrivateThreadContext allocated by the user,
1292  * for storing and updating local counter values
1293  *
1294  * \param pca Pointer to the StatsPrivateThreadContext
1295  */
1296 static void StatsReleasePrivateThreadContext(StatsPrivateThreadContext *pca)
1297 {
1298  if (pca != NULL) {
1299  if (pca->head != NULL) {
1300  SCFree(pca->head);
1301  pca->head = NULL;
1302  pca->size = 0;
1303  }
1304  pca->initialized = 0;
1305  }
1306 
1307  return;
1308 }
1309 
1311 {
1312  StatsPublicThreadContextCleanup(&tv->perf_public_ctx);
1313  StatsReleasePrivateThreadContext(&tv->perf_private_ctx);
1314 }
1315 
1316 /*----------------------------------Unit_Tests--------------------------------*/
1317 
1318 #ifdef UNITTESTS
1319 /** \internal
1320  * \brief Registers a normal, unqualified counter
1321  *
1322  * \param name Name of the counter, to be registered
1323  * \param tm_name Name of the engine module under which the counter has to be
1324  * registered
1325  * \param type Datatype of this counter variable
1326  * \param pctx StatsPublicThreadContext corresponding to the tm_name key under which the
1327  * key has to be registered
1328  *
1329  * \retval id Counter id for the newly registered counter, or the already
1330  * present counter
1331  */
1332 static uint16_t RegisterCounter(const char *name, const char *tm_name,
1334 {
1335  uint16_t id = StatsRegisterQualifiedCounter(name, tm_name, pctx,
1336  STATS_TYPE_NORMAL, NULL);
1337  return id;
1338 }
1339 
1340 static int StatsTestCounterReg02(void)
1341 {
1343 
1344  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1345 
1346  return RegisterCounter(NULL, NULL, &pctx) == 0;
1347 }
1348 
1349 static int StatsTestCounterReg03(void)
1350 {
1352  int result;
1353 
1354  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1355 
1356  result = RegisterCounter("t1", "c1", &pctx);
1357 
1358  StatsReleaseCounters(pctx.head);
1359 
1360  return result;
1361 }
1362 
1363 static int StatsTestCounterReg04(void)
1364 {
1366  int result;
1367 
1368  memset(&pctx, 0, sizeof(StatsPublicThreadContext));
1369 
1370  RegisterCounter("t1", "c1", &pctx);
1371  RegisterCounter("t2", "c2", &pctx);
1372  RegisterCounter("t3", "c3", &pctx);
1373 
1374  result = RegisterCounter("t1", "c1", &pctx);
1375 
1376  StatsReleaseCounters(pctx.head);
1377 
1378  return result;
1379 }
1380 
1381 static int StatsTestGetCntArray05(void)
1382 {
1383  ThreadVars tv;
1384  int id;
1385 
1386  memset(&tv, 0, sizeof(ThreadVars));
1387 
1388  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1389  if (id != 1) {
1390  printf("id %d: ", id);
1391  return 0;
1392  }
1393 
1394  int r = StatsGetAllCountersArray(NULL, &tv.perf_private_ctx);
1395  return (r == -1) ? 1 : 0;
1396 }
1397 
1398 static int StatsTestGetCntArray06(void)
1399 {
1400  ThreadVars tv;
1401  int id;
1402  int result;
1403 
1404  memset(&tv, 0, sizeof(ThreadVars));
1405 
1406  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1407  if (id != 1)
1408  return 0;
1409 
1410  int r = StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1411 
1412  result = (r == 0) ? 1 : 0;
1413 
1415  StatsReleasePrivateThreadContext(&tv.perf_private_ctx);
1416 
1417  return result;
1418 }
1419 
1420 static int StatsTestCntArraySize07(void)
1421 {
1422  ThreadVars tv;
1423  StatsPrivateThreadContext *pca = NULL;
1424  int result;
1425 
1426  memset(&tv, 0, sizeof(ThreadVars));
1427 
1428  //pca = (StatsPrivateThreadContext *)&tv.perf_private_ctx;
1429 
1430  RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1431  RegisterCounter("t2", "c2", &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, 1);
1437  StatsIncr(&tv, 2);
1438 
1439  result = pca->size;
1440 
1442  StatsReleasePrivateThreadContext(pca);
1443 
1444  PASS_IF(result == 2);
1445 }
1446 
1447 static int StatsTestUpdateCounter08(void)
1448 {
1449  ThreadVars tv;
1450  StatsPrivateThreadContext *pca = NULL;
1451  int id;
1452  int result;
1453 
1454  memset(&tv, 0, sizeof(ThreadVars));
1455 
1456  id = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1457 
1458  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1459  pca = &tv.perf_private_ctx;
1460 
1461  StatsIncr(&tv, id);
1462  StatsAddUI64(&tv, id, 100);
1463 
1464  result = pca->head[id].value;
1465 
1467  StatsReleasePrivateThreadContext(pca);
1468 
1469  return result == 101;
1470 }
1471 
1472 static int StatsTestUpdateCounter09(void)
1473 {
1474  ThreadVars tv;
1475  StatsPrivateThreadContext *pca = NULL;
1476  uint16_t id1, id2;
1477  int result;
1478 
1479  memset(&tv, 0, sizeof(ThreadVars));
1480 
1481  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1482  RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1483  RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1484  RegisterCounter("t4", "c4", &tv.perf_public_ctx);
1485  id2 = RegisterCounter("t5", "c5", &tv.perf_public_ctx);
1486 
1487  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1488  pca = &tv.perf_private_ctx;
1489 
1490  StatsIncr(&tv, id2);
1491  StatsAddUI64(&tv, id2, 100);
1492 
1493  result = (pca->head[id1].value == 0) && (pca->head[id2].value == 101);
1494 
1496  StatsReleasePrivateThreadContext(pca);
1497 
1498  return result;
1499 }
1500 
1501 static int StatsTestUpdateGlobalCounter10(void)
1502 {
1503  ThreadVars tv;
1504  StatsPrivateThreadContext *pca = NULL;
1505 
1506  int result = 1;
1507  uint16_t id1, id2, id3;
1508 
1509  memset(&tv, 0, sizeof(ThreadVars));
1510 
1511  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1512  id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1513  id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1514 
1515  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1516  pca = &tv.perf_private_ctx;
1517 
1518  StatsIncr(&tv, id1);
1519  StatsAddUI64(&tv, id2, 100);
1520  StatsIncr(&tv, id3);
1521  StatsAddUI64(&tv, id3, 100);
1522 
1524 
1525  result = (1 == tv.perf_public_ctx.head->value);
1526  result &= (100 == tv.perf_public_ctx.head->next->value);
1527  result &= (101 == tv.perf_public_ctx.head->next->next->value);
1528 
1530  StatsReleasePrivateThreadContext(pca);
1531 
1532  return result;
1533 }
1534 
1535 static int StatsTestCounterValues11(void)
1536 {
1537  ThreadVars tv;
1538  StatsPrivateThreadContext *pca = NULL;
1539 
1540  int result = 1;
1541  uint16_t id1, id2, id3, id4;
1542 
1543  memset(&tv, 0, sizeof(ThreadVars));
1544 
1545  id1 = RegisterCounter("t1", "c1", &tv.perf_public_ctx);
1546  id2 = RegisterCounter("t2", "c2", &tv.perf_public_ctx);
1547  id3 = RegisterCounter("t3", "c3", &tv.perf_public_ctx);
1548  id4 = RegisterCounter("t4", "c4", &tv.perf_public_ctx);
1549 
1550  StatsGetAllCountersArray(&tv.perf_public_ctx, &tv.perf_private_ctx);
1551  pca = &tv.perf_private_ctx;
1552 
1553  StatsIncr(&tv, id1);
1554  StatsAddUI64(&tv, id2, 256);
1555  StatsAddUI64(&tv, id3, 257);
1556  StatsAddUI64(&tv, id4, 16843024);
1557 
1559 
1560  result &= (1 == tv.perf_public_ctx.head->value);
1561 
1562  result &= (256 == tv.perf_public_ctx.head->next->value);
1563 
1564  result &= (257 == tv.perf_public_ctx.head->next->next->value);
1565 
1566  result &= (16843024 == tv.perf_public_ctx.head->next->next->next->value);
1567 
1569  StatsReleasePrivateThreadContext(pca);
1570 
1571  return result;
1572 }
1573 
1574 #endif
1575 
1577 {
1578 #ifdef UNITTESTS
1579  UtRegisterTest("StatsTestCounterReg02", StatsTestCounterReg02);
1580  UtRegisterTest("StatsTestCounterReg03", StatsTestCounterReg03);
1581  UtRegisterTest("StatsTestCounterReg04", StatsTestCounterReg04);
1582  UtRegisterTest("StatsTestGetCntArray05", StatsTestGetCntArray05);
1583  UtRegisterTest("StatsTestGetCntArray06", StatsTestGetCntArray06);
1584  UtRegisterTest("StatsTestCntArraySize07", StatsTestCntArraySize07);
1585  UtRegisterTest("StatsTestUpdateCounter08", StatsTestUpdateCounter08);
1586  UtRegisterTest("StatsTestUpdateCounter09", StatsTestUpdateCounter09);
1587  UtRegisterTest("StatsTestUpdateGlobalCounter10",
1588  StatsTestUpdateGlobalCounter10);
1589  UtRegisterTest("StatsTestCounterValues11", StatsTestCounterValues11);
1590 #endif
1591 }
struct StatsCounter_ * next
Definition: counters.h:55
#define SCCondSignal(x)
#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:70
const char * stats_decoder_events_prefix
Definition: counters.c:103
#define SCLogDebug(...)
Definition: util-debug.h:335
#define SCMutexDestroy(x)
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:1263
SCCondT cond_q
Definition: decode.h:638
StatsCounter * pc
Definition: counters.h:81
#define STATS_WUT_TTS
Definition: counters.c:46
#define BUG_ON(x)
#define SCSetThreadName(n)
Definition: threads.h:301
#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:1282
StatsPrivateThreadContext perf_private_ctx
Definition: threadvars.h:104
void StatsThreadCleanup(ThreadVars *tv)
Definition: counters.c:1310
bool stats_decoder_events
Definition: counters.c:102
#define SCCtrlMutexUnlock(mut)
#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:69
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
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:1017
int OutputStatsLoggersRegistered(void)
Definition: output-stats.c:207
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:939
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:113
#define SCCalloc(nm, a)
Definition: util-mem.h:205
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:843
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:1998
#define SCMutexUnlock(mut)
#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:856
#define THV_INIT_DONE
Definition: threadvars.h:36
#define JSON_STATS_THREADS
uint16_t type
#define THV_CLOSED
Definition: threadvars.h:41
#define SCMutexInit(mut, mutattr)
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
Definition: conf.c:530
uint64_t updates
Definition: counters.h:90
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:1016
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:885
TmEcode OutputStatsLog(ThreadVars *tv, void *thread_data, StatsTable *st)
Definition: output-stats.c:86
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
#define SCCtrlCondTimedwait
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:121
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:868
uint16_t StatsRegisterGlobalCounter(const char *name, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1000
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:873
#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)
Syncs the counter array with the global counter variables.
Definition: counters.c:1219
#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:174
#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:1072
#define SCFree(a)
Definition: util-mem.h:236
uint64_t value
Definition: output-stats.h:32
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.h:73
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:960
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.
#define SCMutex
PacketQueue trans_q[256]
Definition: suricata.h:132
#define SCMutexLock(mut)
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:1201
int TmThreadsCheckFlag(ThreadVars *tv, uint16_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:105
void StatsRegisterTests(void)
Definition: counters.c:1576
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:94
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 SCCtrlMutexLock(mut)
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:1882
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:981
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:1277
void TmThreadTestThreadUnPaused(ThreadVars *tv)
Tests if the thread represented in the arg has been unpaused or not.
Definition: tm-threads.c:1980
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:1251
#define SCMUTEX_INITIALIZER