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