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 __COUNTERS_H__
26 #define __COUNTERS_H__
27 
28 /* forward declaration of the ThreadVars structure */
29 struct ThreadVars_;
30 
31 /**
32  * \brief Container to hold the counter variable
33  */
34 typedef struct StatsCounter_ {
35  int type;
36 
37  /* local id for this counter in this thread */
38  uint16_t id;
39 
40  /* global id, used in output */
41  uint16_t gid;
42 
43  /* counter value(s): copies from the 'private' counter */
44  uint64_t value; /**< sum of updates/increments, or 'set' value */
45  uint64_t updates; /**< number of updates (for avg) */
46 
47  /* when using type STATS_TYPE_Q_FUNC this function is called once
48  * to get the counter value, regardless of how many threads there are. */
49  uint64_t (*Func)(void);
50 
51  /* name of the counter */
52  const char *name;
53 
54  /* the next perfcounter for this tv's tm instance */
56 } StatsCounter;
57 
58 /**
59  * \brief Stats Context for a ThreadVars instance
60  */
61 typedef struct StatsPublicThreadContext_ {
62  /* flag set by the wakeup thread, to inform the client threads to sync */
63  uint32_t perf_flag;
64 
65  /* pointer to the head of a list of counters assigned under this context */
67 
68  /* holds the total no of counters already assigned for this perf context */
69  uint16_t curr_id;
70 
71  /* mutex to prevent simultaneous access during update_counter/output_stat */
74 
75 /**
76  * \brief Storage for local counters, with a link to the public counter used
77  * for syncs
78  */
79 typedef struct StatsLocalCounter_ {
80  /* pointer to the counter that corresponds to this local counter */
82 
83  /* local counter id of the above counter */
84  uint16_t id;
85 
86  /* total value of the adds/increments, or exact value in case of 'set' */
87  uint64_t value;
88 
89  /* no of times the local counter has been updated */
90  uint64_t updates;
92 
93 /**
94  * \brief used to hold the private version of the counters registered
95  */
97  /* points to the array holding local counters */
99 
100  /* size of head array in elements */
101  uint32_t size;
102 
105 
106 /* the initialization functions */
107 void StatsInit(void);
110 void StatsSpawnThreads(void);
111 void StatsRegisterTests(void);
112 
113 /* functions used to free the resources alloted by the Stats API */
114 void StatsReleaseResources(void);
115 
116 /* counter registration functions */
117 uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *);
118 uint16_t StatsRegisterAvgCounter(const char *, struct ThreadVars_ *);
119 uint16_t StatsRegisterMaxCounter(const char *, struct ThreadVars_ *);
120 uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
121 
122 /* functions used to update local counter values */
123 void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t);
124 void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t);
125 void StatsIncr(struct ThreadVars_ *, uint16_t);
126 
127 /* utility functions */
129 uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t);
130 int StatsSetupPrivate(struct ThreadVars_ *);
131 void StatsThreadCleanup(struct ThreadVars_ *);
132 
133 #define StatsSyncCounters(tv) \
134  StatsUpdateCounterArray(&(tv)->perf_private_ctx, &(tv)->perf_public_ctx); \
135 
136 #define StatsSyncCountersIfSignalled(tv) \
137  do { \
138  if ((tv)->perf_public_ctx.perf_flag == 1) { \
139  StatsUpdateCounterArray(&(tv)->perf_private_ctx, \
140  &(tv)->perf_public_ctx); \
141  } \
142  } while (0)
143 
144 #ifdef BUILD_UNIX_SOCKET
145 TmEcode StatsOutputCounterSocket(json_t *cmd,
146  json_t *answer, void *data);
147 #endif
148 
149 #endif /* __COUNTERS_H__ */
150 
struct StatsCounter_ * next
Definition: counters.h:55
#define SCMutex
Container to hold the counter variable.
Definition: counters.h:34
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:981
struct StatsCounter_ StatsCounter
Container to hold the counter variable.
uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *)
Registers a normal, unqualified counter.
Definition: counters.c:939
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:868
StatsLocalCounter * head
Definition: counters.h:98
StatsCounter * pc
Definition: counters.h:81
void StatsReleaseResources(void)
Releases the resources alloted by the Stats API.
Definition: counters.c:1263
int StatsUpdateCounterArray(StatsPrivateThreadContext *, StatsPublicThreadContext *)
Syncs the counter array with the global counter variables.
Definition: counters.c:1219
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:960
uint16_t gid
Definition: counters.h:41
uint64_t value
Definition: counters.h:44
struct StatsPrivateThreadContext_ StatsPrivateThreadContext
used to hold the private version of the counters registered
uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition: counters.c:1000
uint64_t updates
Definition: counters.h:90
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:873
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:885
uint64_t(* Func)(void)
Definition: counters.h:49
Stats Context for a ThreadVars instance.
Definition: counters.h:61
uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t)
Get the value of the local copy of the counter that hold this id.
Definition: counters.c:1251
void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Sets a value of type double to the local counter.
Definition: counters.c:185
const char * name
Definition: counters.h:52
void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:142
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
used to hold the private version of the counters registered
Definition: counters.h:96
void StatsRegisterTests(void)
Definition: counters.c:1576
int StatsSetupPrivate(struct ThreadVars_ *)
Definition: counters.c:1201
Storage for local counters, with a link to the public counter used for syncs.
Definition: counters.h:79
struct StatsLocalCounter_ StatsLocalCounter
Storage for local counters, with a link to the public counter used for syncs.
Per thread variable structure.
Definition: threadvars.h:57
uint64_t value
Definition: counters.h:87
void StatsThreadCleanup(struct ThreadVars_ *)
Definition: counters.c:1310
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
StatsCounter * head
Definition: counters.h:66
void StatsIncr(struct ThreadVars_ *, uint16_t)
Increments the local counter.
Definition: counters.c:163
uint64_t updates
Definition: counters.h:45
uint16_t id
Definition: counters.h:38