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 /**
47  * \brief Container to hold the counter variable
48  */
49 typedef struct StatsCounter_ {
50  int type;
51 
52  /* local id for this counter in this thread */
53  uint16_t id;
54 
55  /* global id, used in output */
56  uint16_t gid;
57 
58  /* when using type STATS_TYPE_Q_FUNC this function is called once
59  * to get the counter value, regardless of how many threads there are. */
60  uint64_t (*Func)(void);
61 
62  /* name of the counter */
63  const char *name;
64  const char *short_name;
65 
66  /* the next perfcounter for this tv's tm instance */
69 
70 /**
71  * \brief counter type for local (private) increments.
72  * For AVG counters we use 2 to track values and updates.
73  */
74 typedef struct StatsLocalCounter_ {
75  int64_t v;
77 
78 /**
79  * \brief Stats Context for a ThreadVars instance
80  */
81 typedef struct StatsPublicThreadContext_ {
82  /* flag set by the wakeup thread, to inform the client threads to sync */
83  SC_ATOMIC_DECLARE(bool, sync_now);
84 
85  /* pointer to the head of a list of counters assigned under this context */
87 
88  /* holds the total no of counters already assigned for this perf context */
89  uint16_t curr_id;
90 
91  /* array of pointers to the StatsCounters in `head` above, indexed by the per
92  * thread counter id.
93  * Size is `curr_id + 1` after all counters have been registered.
94  * Ownership of counters is with `head` above. */
96 
98 
99  /* lock to prevent simultaneous access during update_counter/output_stat */
102 
103 /**
104  * \brief used to hold the private version of the counters registered
105  */
107  /* points to the array holding local counters */
109 
110  /* size of head array in elements */
111  uint32_t size;
112 
115 
116 typedef struct StatsThreadContext_ {
120 
121 /* the initialization functions */
122 void StatsInit(void);
125 void StatsSpawnThreads(void);
126 void StatsRegisterTests(void);
127 bool StatsEnabled(void);
128 
129 /* functions used to free the resources allotted by the Stats API */
130 void StatsReleaseResources(void);
131 
132 /* counter registration functions */
136 StatsCounterGlobalId StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
137 
138 /* functions used to update local counter values */
143 
146 
147 /* utility functions */
150 int StatsSetupPrivate(StatsThreadContext *, const char *);
152 
155 
156 #ifdef BUILD_UNIX_SOCKET
157 TmEcode StatsOutputCounterSocket(json_t *cmd,
158  json_t *answer, void *data);
159 #endif
160 
161 #endif /* SURICATA_COUNTERS_H */
StatsPublicThreadContext_::lock
SCSpinlock lock
Definition: counters.h:100
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:940
StatsPublicThreadContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(bool, sync_now)
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *, StatsCounterId)
Increments the local counter.
Definition: counters.c:165
StatsCounterGlobalId
struct StatsCounterGlobalId StatsCounterGlobalId
StatsCounter_::type
int type
Definition: counters.h:50
StatsSyncCounters
void StatsSyncCounters(StatsThreadContext *)
Definition: counters.c:478
StatsRegisterAvgCounter
StatsCounterAvgId StatsRegisterAvgCounter(const char *, StatsThreadContext *)
Registers a counter, whose value holds the average of all the values assigned to it.
Definition: counters.c:1019
threads.h
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:49
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:935
StatsCounterMaxId
struct StatsCounterMaxId StatsCounterMaxId
StatsPublicThreadContext_::copy_of_private
StatsLocalCounter * copy_of_private
Definition: counters.h:97
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1578
StatsRegisterGlobalCounter
StatsCounterGlobalId StatsRegisterGlobalCounter(const char *cname, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1053
StatsCounter_::name
const char * name
Definition: counters.h:63
StatsCounter_::Func
uint64_t(* Func)(void)
Definition: counters.h:60
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:1001
StatsCounter_::short_name
const char * short_name
Definition: counters.h:64
StatsCounter_::next
struct StatsCounter_ * next
Definition: counters.h:67
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:1037
StatsThreadContext_::pub
StatsPublicThreadContext pub
Definition: counters.h:117
StatsCounterAvgId
struct StatsCounterAvgId StatsCounterAvgId
StatsSetupPrivate
int StatsSetupPrivate(StatsThreadContext *, const char *)
Definition: counters.c:1238
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:81
StatsCounterGetLocalValue
int64_t StatsCounterGetLocalValue(StatsThreadContext *, StatsCounterId)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1301
StatsCounterGlobalId::id
uint16_t id
Definition: counters.h:43
StatsCounter_::id
uint16_t id
Definition: counters.h:53
StatsCounterAddI64
void StatsCounterAddI64(StatsThreadContext *, StatsCounterId, int64_t)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:146
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *)
Definition: counters.c:1354
TmEcode
TmEcode
Definition: tm-threads-common.h:80
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:116
StatsCounterAvgAddI64
void StatsCounterAvgAddI64(StatsThreadContext *, StatsCounterAvgId id, int64_t x)
Definition: counters.c:240
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:106
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:74
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(StatsThreadContext *)
Definition: counters.c:483
StatsCounterMaxId::id
uint16_t id
Definition: counters.h:39
StatsPublicThreadContext_::pc_array
const StatsCounter ** pc_array
Definition: counters.h:95
StatsCounterAvgId::id
uint16_t id
Definition: counters.h:35
StatsThreadContext_
Definition: counters.h:116
StatsPublicThreadContext
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
StatsCounter_::gid
uint16_t gid
Definition: counters.h:56
StatsCounterAvgId
Definition: counters.h:34
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:113
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:111
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:224
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:235
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:925
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:86
StatsCounterSetI64
void StatsCounterSetI64(StatsThreadContext *, StatsCounterId, int64_t)
set, so overwrite, the value of the local counter
Definition: counters.c:204
StatsThreadInit
void StatsThreadInit(StatsThreadContext *)
Definition: counters.c:1258
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:89
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:952
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:108
StatsLocalCounter_::v
int64_t v
Definition: counters.h:75
StatsThreadContext_::priv
StatsPrivateThreadContext priv
Definition: counters.h:118
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition: counters.c:1313
StatsCounterDecr
void StatsCounterDecr(StatsThreadContext *, StatsCounterId)
Decrements the local counter.
Definition: counters.c:184
StatsCounterId
struct StatsCounterId StatsCounterId