suricata
counters.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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 
25 #ifndef SURICATA_COUNTERS_H
26 #define SURICATA_COUNTERS_H
27 
28 #include "threads.h"
29 
30 typedef struct StatsCounterId {
31  uint16_t id;
33 
34 typedef struct StatsCounterAvgId {
35  uint16_t id;
37 
38 typedef struct StatsCounterMaxId {
39  uint16_t id;
41 
42 typedef struct StatsCounterGlobalId {
43  uint16_t id;
45 
46 /* derive counters are counters that are derived from 2 other
47  * counters. */
48 typedef struct StatsCounterDeriveId {
49  uint16_t id;
51 
52 /**
53  * \brief Container to hold the counter variable
54  */
55 typedef struct StatsCounter_ {
56  int type; /**< enum StatsType from counters.c */
57 
58  /* local id for this counter in this thread */
59  uint16_t id;
60 
61  /* global id, used in output */
62  uint16_t gid;
63 
64  /* derive id's: thread specific id's for the 2 counters part
65  * of this derive counter. */
66  uint16_t did1;
67  uint16_t did2;
68 
69  /* when using type STATS_TYPE_FUNC this function is called once
70  * to get the counter value, regardless of how many threads there are. */
71  uint64_t (*Func)(void);
72 
73  /* name of the counter */
74  const char *name;
75  const char *short_name;
76 
77  /* the next perfcounter for this tv's tm instance */
80 
81 /**
82  * \brief counter type for local (private) increments.
83  * For AVG counters we use 2 to track values and updates.
84  */
85 typedef struct StatsLocalCounter_ {
86  int64_t v;
88 
89 /**
90  * \brief Stats Context for a ThreadVars instance
91  */
92 typedef struct StatsPublicThreadContext_ {
93  /* pointer to the head of a list of counters assigned under this context */
95 
96  /* flag set by the wakeup thread, to inform the client threads to sync */
97  SC_ATOMIC_DECLARE(bool, sync_now);
98 
99  /* holds the total no of counters already assigned for this perf context */
100  uint16_t curr_id;
101 
102  /* separate id space for derive counters. These are set up per thread, but should not be part
103  * the StatsLocalCounter array as they are not updated in the thread directly. */
104  uint16_t derive_id;
105 
106  /* array of pointers to the StatsCounters in `head` above, indexed by the per
107  * thread counter id.
108  * Size is `curr_id + 1` after all counters have been registered.
109  * Ownership of counters is with `head` above. */
111 
113 
114  /* lock to prevent simultaneous access during update_counter/output_stat */
117 
118 /**
119  * \brief used to hold the private version of the counters registered
120  */
122  /* points to the array holding local counters */
124 
125  /* size of head array in elements */
126  uint32_t size;
127 
130 
131 typedef struct StatsThreadContext_ {
135 
136 /* the initialization functions */
137 void StatsInit(void);
140 void StatsSpawnThreads(void);
141 void StatsRegisterTests(void);
142 bool StatsEnabled(void);
143 
144 /* functions used to free the resources allotted by the Stats API */
145 void StatsReleaseResources(void);
146 
147 /* counter registration functions */
151 StatsCounterGlobalId StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
152 
154  const char *cname, const char *dname1, const char *dname2, StatsThreadContext *);
155 
156 /* functions used to update local counter values */
161 
164 
165 /* utility functions */
168 int StatsSetupPrivate(StatsThreadContext *, const char *);
170 
173 
174 #ifdef BUILD_UNIX_SOCKET
175 TmEcode StatsOutputCounterSocket(json_t *cmd,
176  json_t *answer, void *data);
177 #endif
178 
179 #endif /* SURICATA_COUNTERS_H */
StatsPublicThreadContext_::lock
SCSpinlock lock
Definition: counters.h:115
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:978
StatsPublicThreadContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(bool, sync_now)
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *, StatsCounterId)
Increments the local counter.
Definition: counters.c:166
StatsCounterGlobalId
struct StatsCounterGlobalId StatsCounterGlobalId
StatsCounter_::type
int type
Definition: counters.h:56
StatsSyncCounters
void StatsSyncCounters(StatsThreadContext *)
Definition: counters.c:479
StatsRegisterAvgCounter
StatsCounterAvgId StatsRegisterAvgCounter(const char *, StatsThreadContext *)
Registers a counter, whose value holds the average of all the values assigned to it.
Definition: counters.c:1058
threads.h
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:55
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:973
StatsCounterMaxId
struct StatsCounterMaxId StatsCounterMaxId
StatsPublicThreadContext_::copy_of_private
StatsLocalCounter * copy_of_private
Definition: counters.h:112
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1653
StatsRegisterGlobalCounter
StatsCounterGlobalId StatsRegisterGlobalCounter(const char *cname, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1094
StatsCounter_::name
const char * name
Definition: counters.h:74
StatsCounter_::did1
uint16_t did1
Definition: counters.h:66
StatsCounter_::Func
uint64_t(* Func)(void)
Definition: counters.h:71
StatsThreadContext
struct StatsThreadContext_ StatsThreadContext
StatsCounter
struct StatsCounter_ StatsCounter
Container to hold the counter variable.
StatsPrivateThreadContext
struct StatsPrivateThreadContext_ StatsPrivateThreadContext
used to hold the private version of the counters registered
StatsCounterId
Definition: counters.h:30
StatsRegisterCounter
StatsCounterId StatsRegisterCounter(const char *, StatsThreadContext *)
Registers a normal, unqualified counter.
Definition: counters.c:1039
StatsCounter_::short_name
const char * short_name
Definition: counters.h:75
StatsCounter_::next
struct StatsCounter_ * next
Definition: counters.h:78
StatsCounterMaxId
Definition: counters.h:38
StatsRegisterMaxCounter
StatsCounterMaxId StatsRegisterMaxCounter(const char *, StatsThreadContext *)
Registers a counter, whose value holds the maximum of all the values assigned to it.
Definition: counters.c:1077
StatsThreadContext_::pub
StatsPublicThreadContext pub
Definition: counters.h:132
StatsCounterAvgId
struct StatsCounterAvgId StatsCounterAvgId
StatsSetupPrivate
int StatsSetupPrivate(StatsThreadContext *, const char *)
Definition: counters.c:1313
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:92
StatsCounterGetLocalValue
int64_t StatsCounterGetLocalValue(StatsThreadContext *, StatsCounterId)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1376
StatsCounterGlobalId::id
uint16_t id
Definition: counters.h:43
StatsCounter_::id
uint16_t id
Definition: counters.h:59
StatsCounterAddI64
void StatsCounterAddI64(StatsThreadContext *, StatsCounterId, int64_t)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:147
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *)
Definition: counters.c:1429
TmEcode
TmEcode
Definition: tm-threads-common.h:80
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:117
StatsCounterAvgAddI64
void StatsCounterAvgAddI64(StatsThreadContext *, StatsCounterAvgId id, int64_t x)
Definition: counters.c:241
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:121
StatsCounterDeriveId
struct StatsCounterDeriveId StatsCounterDeriveId
StatsCounterGlobalId
Definition: counters.h:42
StatsLocalCounter_
counter type for local (private) increments. For AVG counters we use 2 to track values and updates.
Definition: counters.h:85
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *)
Definition: counters.c:484
StatsCounterMaxId::id
uint16_t id
Definition: counters.h:39
StatsPublicThreadContext_::pc_array
const StatsCounter ** pc_array
Definition: counters.h:110
StatsCounterAvgId::id
uint16_t id
Definition: counters.h:35
StatsThreadContext_
Definition: counters.h:131
StatsPublicThreadContext
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
StatsCounter_::gid
uint16_t gid
Definition: counters.h:62
StatsCounterAvgId
Definition: counters.h:34
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:128
StatsLocalCounter
struct StatsLocalCounter_ StatsLocalCounter
counter type for local (private) increments. For AVG counters we use 2 to track values and updates.
StatsPrivateThreadContext_::size
uint32_t size
Definition: counters.h:126
StatsCounterId::id
uint16_t id
Definition: counters.h:31
StatsCounterMaxUpdateI64
void StatsCounterMaxUpdateI64(StatsThreadContext *, StatsCounterMaxId id, int64_t x)
update the value of the localmax counter
Definition: counters.c:225
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:235
StatsRegisterDeriveDivCounter
StatsCounterDeriveId StatsRegisterDeriveDivCounter(const char *cname, const char *dname1, const char *dname2, StatsThreadContext *)
Registers a counter which tracks the result of the calculating the value of counter dname1 divided by...
Definition: counters.c:1122
StatsInit
void StatsInit(void)
Initializes the perf counter api. Things are hard coded currently. More work to be done when we imple...
Definition: counters.c:963
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:94
StatsCounterSetI64
void StatsCounterSetI64(StatsThreadContext *, StatsCounterId, int64_t)
set, so overwrite, the value of the local counter
Definition: counters.c:205
StatsCounter_::did2
uint16_t did2
Definition: counters.h:67
StatsCounterDeriveId
Definition: counters.h:48
StatsThreadInit
void StatsThreadInit(StatsThreadContext *)
Definition: counters.c:1333
StatsPublicThreadContext_::derive_id
uint16_t derive_id
Definition: counters.h:104
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:100
StatsCounterDeriveId::id
uint16_t id
Definition: counters.h:49
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:990
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:123
StatsLocalCounter_::v
int64_t v
Definition: counters.h:86
StatsThreadContext_::priv
StatsPrivateThreadContext priv
Definition: counters.h:133
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition: counters.c:1388
StatsCounterDecr
void StatsCounterDecr(StatsThreadContext *, StatsCounterId)
Decrements the local counter.
Definition: counters.c:185
StatsCounterId
struct StatsCounterId StatsCounterId