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 */
31struct ThreadVars_;
32
33/**
34 * \brief Container to hold the counter variable
35 */
36typedef 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 */
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 */
82typedef 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 */
110void StatsInit(void);
113void StatsSpawnThreads(void);
114void StatsRegisterTests(void);
115bool StatsEnabled(void);
116
117/* functions used to free the resources allotted by the Stats API */
118void StatsReleaseResources(void);
119
120/* counter registration functions */
121uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *);
122uint16_t StatsRegisterAvgCounter(const char *, struct ThreadVars_ *);
123uint16_t StatsRegisterMaxCounter(const char *, struct ThreadVars_ *);
124uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t (*Func)(void));
125
126/* functions used to update local counter values */
127void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t);
128void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t);
129void StatsIncr(struct ThreadVars_ *, uint16_t);
130void StatsDecr(struct ThreadVars_ *, uint16_t);
131
132/* utility functions */
134uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t);
135int StatsSetupPrivate(struct ThreadVars_ *);
136void StatsThreadCleanup(struct ThreadVars_ *);
137
138void StatsSyncCounters(struct ThreadVars_ *tv);
140
141#ifdef BUILD_UNIX_SOCKET
142TmEcode StatsOutputCounterSocket(json_t *cmd,
143 json_t *answer, void *data);
144#endif
145
146#endif /* SURICATA_COUNTERS_H */
void StatsSyncCounters(struct ThreadVars_ *tv)
Definition counters.c:445
void StatsSetUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Sets a value of type double to the local counter.
Definition counters.c:207
uint16_t StatsRegisterCounter(const char *, struct ThreadVars_ *)
Registers a normal, unqualified counter.
Definition counters.c:952
void StatsSpawnThreads(void)
Spawns the wakeup, and the management thread used by the stats api.
Definition counters.c:903
void StatsDecr(struct ThreadVars_ *, uint16_t)
Decrements the local counter.
Definition counters.c:186
void StatsAddUI64(struct ThreadVars_ *, uint16_t, uint64_t)
Adds a value of type uint64_t to the local counter.
Definition counters.c:146
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:992
void StatsThreadCleanup(struct ThreadVars_ *)
Definition counters.c:1308
int StatsUpdateCounterArray(StatsPrivateThreadContext *, StatsPublicThreadContext *)
the private stats store with the public stats store
Definition counters.c:1227
struct StatsLocalCounter_ StatsLocalCounter
Storage for local counters, with a link to the public counter used for syncs.
void StatsInit(void)
Initializes the perf counter api. Things are hard coded currently. More work to be done when we imple...
Definition counters.c:876
bool StatsEnabled(void)
Definition counters.c:118
struct StatsPublicThreadContext_ StatsPublicThreadContext
Stats Context for a ThreadVars instance.
uint64_t StatsGetLocalCounterValue(struct ThreadVars_ *, uint16_t)
Get the value of the local copy of the counter that hold this id.
Definition counters.c:1255
void StatsIncr(struct ThreadVars_ *, uint16_t)
Increments the local counter.
Definition counters.c:166
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:972
struct StatsPrivateThreadContext_ StatsPrivateThreadContext
used to hold the private version of the counters registered
int StatsSetupPrivate(struct ThreadVars_ *)
Definition counters.c:1209
uint16_t StatsRegisterGlobalCounter(const char *cname, uint64_t(*Func)(void))
Registers a counter, which represents a global value.
Definition counters.c:1010
struct StatsCounter_ StatsCounter
Container to hold the counter variable.
void StatsSetupPostConfigPostOutput(void)
Definition counters.c:891
void StatsReleaseResources(void)
Releases the resources allotted by the Stats API.
Definition counters.c:1267
void StatsSyncCountersIfSignalled(struct ThreadVars_ *tv)
Definition counters.c:450
void StatsSetupPostConfigPreOutput(void)
Definition counters.c:886
void StatsRegisterTests(void)
Definition counters.c:1569
ThreadVars * tv
Container to hold the counter variable.
Definition counters.h:36
uint64_t updates
Definition counters.h:47
int64_t value
Definition counters.h:46
uint16_t gid
Definition counters.h:43
uint64_t(* Func)(void)
Definition counters.h:51
struct StatsCounter_ * next
Definition counters.h:58
const char * short_name
Definition counters.h:55
const char * name
Definition counters.h:54
uint16_t id
Definition counters.h:40
Storage for local counters, with a link to the public counter used for syncs.
Definition counters.h:82
uint64_t updates
Definition counters.h:93
StatsCounter * pc
Definition counters.h:84
used to hold the private version of the counters registered
Definition counters.h:99
StatsLocalCounter * head
Definition counters.h:101
Stats Context for a ThreadVars instance.
Definition counters.h:64
StatsCounter * head
Definition counters.h:69
SC_ATOMIC_DECLARE(bool, sync_now)
Per thread variable structure.
Definition threadvars.h:58
#define SCMutex