suricata
counters.h
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 
25 #ifndef SURICATA_COUNTERS_H
26 #define SURICATA_COUNTERS_H
27 
28 #include "threads.h"
29 
30 /* forward declaration of the ThreadVars structure */
31 struct ThreadVars_;
32 
33 /**
34  * \brief Container to hold the counter variable
35  */
36 typedef struct StatsCounter_ {
37  int type;
38 
39  /* local id for this counter in this thread */
40  uint16_t id;
41 
42  /* global id, used in output */
43  uint16_t gid;
44 
45  /* counter value(s): copies from the 'private' counter */
46  int64_t value; /**< sum of updates/increments, or 'set' value */
47  uint64_t updates; /**< number of updates (for avg) */
48 
49  /* when using type STATS_TYPE_Q_FUNC this function is called once
50  * to get the counter value, regardless of how many threads there are. */
51  uint64_t (*Func)(void);
52 
53  /* name of the counter */
54  const char *name;
55  const char *short_name;
56 
57  /* the next perfcounter for this tv's tm instance */
60 
61 /**
62  * \brief Stats Context for a ThreadVars instance
63  */
64 typedef struct StatsPublicThreadContext_ {
65  /* flag set by the wakeup thread, to inform the client threads to sync */
66  SC_ATOMIC_DECLARE(bool, sync_now);
67 
68  /* pointer to the head of a list of counters assigned under this context */
70 
71  /* holds the total no of counters already assigned for this perf context */
72  uint16_t curr_id;
73 
74  /* mutex to prevent simultaneous access during update_counter/output_stat */
77 
78 /**
79  * \brief Storage for local counters, with a link to the public counter used
80  * for syncs
81  */
82 typedef struct StatsLocalCounter_ {
83  /* pointer to the counter that corresponds to this local counter */
85 
86  /* local counter id of the above counter */
87  uint16_t id;
88 
89  /* total value of the adds/increments, or exact value in case of 'set' */
90  int64_t value;
91 
92  /* no of times the local counter has been updated */
93  uint64_t updates;
95 
96 /**
97  * \brief used to hold the private version of the counters registered
98  */
100  /* points to the array holding local counters */
102 
103  /* size of head array in elements */
104  uint32_t size;
105 
108 
109 /* the initialization functions */
110 void StatsInit(void);
113 void StatsSpawnThreads(void);
114 void StatsRegisterTests(void);
115 bool StatsEnabled(void);
116 
117 /* functions used to free the resources allotted by the Stats API */
118 void StatsReleaseResources(void);
119 
120 /* counter registration functions */
121 uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *);
122 uint16_t StatsRegisterAvgCounter(const char *, struct ThreadVars_ *);
123 uint16_t StatsRegisterMaxCounter(const char *, struct ThreadVars_ *);
124 uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
125 
126 /* functions used to update local counter values */
127 void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t);
128 void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t);
129 void StatsIncr(struct ThreadVars_ *, uint16_t);
130 void StatsDecr(struct ThreadVars_ *, uint16_t);
131 
132 /* utility functions */
134 uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t);
135 int StatsSetupPrivate(struct ThreadVars_ *);
136 void StatsThreadCleanup(struct ThreadVars_ *);
137 
138 void StatsSyncCounters(struct ThreadVars_ *tv);
140 
141 #ifdef BUILD_UNIX_SOCKET
142 TmEcode StatsOutputCounterSocket(json_t *cmd,
143  json_t *answer, void *data);
144 #endif
145 
146 #endif /* SURICATA_COUNTERS_H */
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:900
StatsIncr
void StatsIncr(struct ThreadVars_ *, uint16_t)
Increments the local counter.
Definition: counters.c:166
StatsPublicThreadContext_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(bool, sync_now)
StatsSyncCountersIfSignalled
void StatsSyncCountersIfSignalled(struct ThreadVars_ *tv)
Definition: counters.c:454
StatsCounter_::type
int type
Definition: counters.h:37
StatsGetLocalCounterValue
uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1260
threads.h
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:36
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:895
StatsCounter_::updates
uint64_t updates
Definition: counters.h:47
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1574
StatsCounter_::name
const char * name
Definition: counters.h:54
StatsCounter_::Func
uint64_t(* Func)(void)
Definition: counters.h:51
StatsCounter_::value
int64_t value
Definition: counters.h:46
StatsCounter
struct StatsCounter_ StatsCounter
Container to hold the counter variable.
StatsPrivateThreadContext
struct StatsPrivateThreadContext_ StatsPrivateThreadContext
used to hold the private version of the counters registered
StatsUpdateCounterArray
int StatsUpdateCounterArray(StatsPrivateThreadContext *, StatsPublicThreadContext *)
the private stats store with the public stats store
Definition: counters.c:1232
StatsLocalCounter_::updates
uint64_t updates
Definition: counters.h:93
StatsCounter_::short_name
const char * short_name
Definition: counters.h:55
StatsDecr
void StatsDecr(struct ThreadVars_ *, uint16_t)
Decrements the local counter.
Definition: counters.c:186
StatsCounter_::next
struct StatsCounter_ * next
Definition: counters.h:58
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
StatsSyncCounters
void StatsSyncCounters(struct ThreadVars_ *tv)
Definition: counters.c:449
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:64
StatsLocalCounter_::pc
StatsCounter * pc
Definition: counters.h:84
StatsSetUI64
void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Sets a value of type double to the local counter.
Definition: counters.c:207
StatsRegisterCounter
uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *)
Registers a normal, unqualified counter.
Definition: counters.c:961
StatsCounter_::id
uint16_t id
Definition: counters.h:40
StatsAddUI64
void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:146
StatsRegisterGlobalCounter
uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1019
TmEcode
TmEcode
Definition: tm-threads-common.h:81
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:118
StatsLocalCounter_::id
uint16_t id
Definition: counters.h:87
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:99
StatsLocalCounter_::value
int64_t value
Definition: counters.h:90
StatsPublicThreadContext_::m
SCMutex m
Definition: counters.h:75
StatsLocalCounter_
Storage for local counters, with a link to the public counter used for syncs.
Definition: counters.h:82
StatsSetupPrivate
int StatsSetupPrivate(struct ThreadVars_ *)
Definition: counters.c:1214
StatsPublicThreadContext
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
StatsRegisterAvgCounter
uint16_t StatsRegisterAvgCounter(const char *, struct ThreadVars_ *)
Registers a counter, whose value holds the average of all the values assigned to it.
Definition: counters.c:981
StatsCounter_::gid
uint16_t gid
Definition: counters.h:43
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:106
StatsLocalCounter
struct StatsLocalCounter_ StatsLocalCounter
Storage for local counters, with a link to the public counter used for syncs.
StatsPrivateThreadContext_::size
uint32_t size
Definition: counters.h:104
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:885
StatsThreadCleanup
void StatsThreadCleanup(struct ThreadVars_ *)
Definition: counters.c:1313
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:69
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:72
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:912
SCMutex
#define SCMutex
Definition: threads-debug.h:114
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:101
StatsRegisterMaxCounter
uint16_t StatsRegisterMaxCounter(const char *, struct ThreadVars_ *)
Registers a counter, whose value holds the maximum of all the values assigned to it.
Definition: counters.c:1001
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition: counters.c:1272