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 #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 
56  /* the next perfcounter for this tv's tm instance */
59 
60 /**
61  * \brief Stats Context for a ThreadVars instance
62  */
63 typedef struct StatsPublicThreadContext_ {
64  /* flag set by the wakeup thread, to inform the client threads to sync */
65  uint32_t perf_flag;
66 
67  /* pointer to the head of a list of counters assigned under this context */
69 
70  /* holds the total no of counters already assigned for this perf context */
71  uint16_t curr_id;
72 
73  /* mutex to prevent simultaneous access during update_counter/output_stat */
76 
77 /**
78  * \brief Storage for local counters, with a link to the public counter used
79  * for syncs
80  */
81 typedef struct StatsLocalCounter_ {
82  /* pointer to the counter that corresponds to this local counter */
84 
85  /* local counter id of the above counter */
86  uint16_t id;
87 
88  /* total value of the adds/increments, or exact value in case of 'set' */
89  int64_t value;
90 
91  /* no of times the local counter has been updated */
92  uint64_t updates;
94 
95 /**
96  * \brief used to hold the private version of the counters registered
97  */
99  /* points to the array holding local counters */
101 
102  /* size of head array in elements */
103  uint32_t size;
104 
107 
108 /* the initialization functions */
109 void StatsInit(void);
112 void StatsSpawnThreads(void);
113 void StatsRegisterTests(void);
114 bool StatsEnabled(void);
115 
116 /* functions used to free the resources alloted by the Stats API */
117 void StatsReleaseResources(void);
118 
119 /* counter registration functions */
120 uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *);
121 uint16_t StatsRegisterAvgCounter(const char *, struct ThreadVars_ *);
122 uint16_t StatsRegisterMaxCounter(const char *, struct ThreadVars_ *);
123 uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
124 
125 /* functions used to update local counter values */
126 void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t);
127 void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t);
128 void StatsIncr(struct ThreadVars_ *, uint16_t);
129 void StatsDecr(struct ThreadVars_ *, uint16_t);
130 
131 /* utility functions */
133 uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t);
134 int StatsSetupPrivate(struct ThreadVars_ *);
135 void StatsThreadCleanup(struct ThreadVars_ *);
136 
137 #define StatsSyncCounters(tv) \
138  StatsUpdateCounterArray(&(tv)->perf_private_ctx, &(tv)->perf_public_ctx); \
139 
140 #define StatsSyncCountersIfSignalled(tv) \
141  do { \
142  if ((tv)->perf_public_ctx.perf_flag == 1) { \
143  StatsUpdateCounterArray(&(tv)->perf_private_ctx, \
144  &(tv)->perf_public_ctx); \
145  } \
146  } while (0)
147 
148 #ifdef BUILD_UNIX_SOCKET
149 TmEcode StatsOutputCounterSocket(json_t *cmd,
150  json_t *answer, void *data);
151 #endif
152 
153 #endif /* __COUNTERS_H__ */
154 
StatsSetupPostConfigPostOutput
void StatsSetupPostConfigPostOutput(void)
Definition: counters.c:894
StatsIncr
void StatsIncr(struct ThreadVars_ *, uint16_t)
Increments the local counter.
Definition: counters.c:167
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:1256
threads.h
StatsCounter_
Container to hold the counter variable.
Definition: counters.h:36
StatsSetupPostConfigPreOutput
void StatsSetupPostConfigPreOutput(void)
Definition: counters.c:889
StatsCounter_::updates
uint64_t updates
Definition: counters.h:47
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1570
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:1228
StatsLocalCounter_::updates
uint64_t updates
Definition: counters.h:92
StatsDecr
void StatsDecr(struct ThreadVars_ *, uint16_t)
Decrements the local counter.
Definition: counters.c:188
StatsCounter_::next
struct StatsCounter_ * next
Definition: counters.h:57
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
StatsPublicThreadContext_
Stats Context for a ThreadVars instance.
Definition: counters.h:63
StatsLocalCounter_::pc
StatsCounter * pc
Definition: counters.h:83
StatsSetUI64
void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Sets a value of type double to the local counter.
Definition: counters.c:210
StatsRegisterCounter
uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *)
Registers a normal, unqualified counter.
Definition: counters.c:955
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:1013
TmEcode
TmEcode
Definition: tm-threads-common.h:83
StatsEnabled
bool StatsEnabled(void)
Definition: counters.c:118
StatsLocalCounter_::id
uint16_t id
Definition: counters.h:86
StatsPrivateThreadContext_
used to hold the private version of the counters registered
Definition: counters.h:98
StatsLocalCounter_::value
int64_t value
Definition: counters.h:89
StatsPublicThreadContext_::m
SCMutex m
Definition: counters.h:74
StatsLocalCounter_
Storage for local counters, with a link to the public counter used for syncs.
Definition: counters.h:81
StatsSetupPrivate
int StatsSetupPrivate(struct ThreadVars_ *)
Definition: counters.c:1210
StatsPublicThreadContext
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
StatsPublicThreadContext_::perf_flag
uint32_t perf_flag
Definition: counters.h:65
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:975
StatsCounter_::gid
uint16_t gid
Definition: counters.h:43
StatsPrivateThreadContext_::initialized
int initialized
Definition: counters.h:105
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:103
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:878
StatsThreadCleanup
void StatsThreadCleanup(struct ThreadVars_ *)
Definition: counters.c:1309
StatsPublicThreadContext_::head
StatsCounter * head
Definition: counters.h:68
StatsPublicThreadContext_::curr_id
uint16_t curr_id
Definition: counters.h:71
StatsSpawnThreads
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition: counters.c:906
SCMutex
#define SCMutex
Definition: threads-debug.h:114
StatsPrivateThreadContext_::head
StatsLocalCounter * head
Definition: counters.h:100
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:995
StatsReleaseResources
void StatsReleaseResources(void)
Releases the resources alloted by the Stats API.
Definition: counters.c:1268