Branch data Line data Source code
1 : : // $Id: Stats.h 6703 2009-05-13 22:27:44Z vern $
2 : : //
3 : : // Classes that collect and report statistics.
4 : :
5 : : #ifndef STATS_H
6 : : #define STATS_H
7 : :
8 : : #include <sys/types.h>
9 : : #include <sys/time.h>
10 : : #include <sys/resource.h>
11 : :
12 : : #include "TCP_Endpoint.h"
13 : :
14 : :
15 : : // Object called by SegmentProfiler when it is done and reports its
16 : : // cumulative CPU/memory statistics.
17 : : class SegmentStatsReporter {
18 : : public:
19 : 0 : SegmentStatsReporter() { }
20 [ # # ][ # # ]: 0 : virtual ~SegmentStatsReporter() { }
[ # # ]
21 : :
22 : : virtual void SegmentProfile(const char* name, const Location* loc,
23 : : double dtime, int dmem) = 0;
24 : : };
25 : :
26 : :
27 : : // A SegmentProfiler tracks how much CPU and memory is consumed
28 : : // across its lifetime.
29 : : //
30 : : // ### This needs porting to Linux. It could also be improved by
31 : : // better efforts at measuring its own overhead.
32 : : class SegmentProfiler {
33 : : public:
34 : : // The constructor takes some way of identifying the segment.
35 : : SegmentProfiler(SegmentStatsReporter* arg_reporter,
36 : 215996 : const char* arg_name)
37 : : {
38 : 215996 : reporter = arg_reporter;
39 [ - + ]: 215996 : if ( reporter )
40 : : {
41 : 0 : name = arg_name;
42 : 0 : loc = 0;
43 : 0 : Init();
44 : : }
45 : 215996 : }
46 : :
47 : : SegmentProfiler(SegmentStatsReporter* arg_reporter,
48 : 94706 : const Location* arg_loc)
49 : : {
50 : 94706 : reporter = arg_reporter;
51 [ - + ]: 94706 : if ( reporter )
52 : : {
53 : 0 : name = 0;
54 : 0 : loc = arg_loc;
55 : 0 : Init();
56 : : }
57 : 94706 : }
58 : :
59 : 310702 : ~SegmentProfiler()
60 : : {
61 [ - + ]: 310702 : if ( reporter )
62 : 0 : Report();
63 : 310702 : }
64 : :
65 : : protected:
66 : : void Init();
67 : : void Report();
68 : :
69 : : SegmentStatsReporter* reporter;
70 : : const char* name;
71 : : const Location* loc;
72 : : struct rusage initial_rusage;
73 : : };
74 : :
75 : :
76 : : class ProfileLogger : public SegmentStatsReporter {
77 : : public:
78 : : ProfileLogger(BroFile* file, double interval);
79 : : ~ProfileLogger();
80 : :
81 : : void Log();
82 : : BroFile* File() { return file; }
83 : :
84 : : protected:
85 : : void SegmentProfile(const char* name, const Location* loc,
86 : : double dtime, int dmem);
87 : :
88 : : private:
89 : : BroFile* file;
90 : : unsigned int log_count;
91 : : };
92 : :
93 : :
94 : : // Generates load_sample() events.
95 : : class SampleLogger : public SegmentStatsReporter {
96 : : public:
97 : : SampleLogger();
98 : : ~SampleLogger();
99 : :
100 : : // These are called to report that a given function or location
101 : : // has been seen during the sampling.
102 : : void FunctionSeen(const Func* func);
103 : : void LocationSeen(const Location* loc);
104 : :
105 : : protected:
106 : : void SegmentProfile(const char* name, const Location* loc,
107 : : double dtime, int dmem);
108 : :
109 : : TableVal* load_samples;
110 : : };
111 : :
112 : :
113 : : extern ProfileLogger* profiling_logger;
114 : : extern ProfileLogger* segment_logger;
115 : : extern SampleLogger* sample_logger;
116 : :
117 : : // Connection statistics.
118 : : extern int killed_by_inactivity;
119 : :
120 : : // Content gap statistics.
121 : : extern uint32 tot_ack_events;
122 : : extern uint32 tot_ack_bytes;
123 : : extern uint32 tot_gap_events;
124 : : extern uint32 tot_gap_bytes;
125 : :
126 : :
127 : : // A TCPStateStats object tracks the distribution of TCP states for
128 : : // the currently active connections.
129 : : class TCPStateStats {
130 : : public:
131 : : TCPStateStats();
132 : 1 : ~TCPStateStats() { }
133 : :
134 : : void ChangeState(EndpointState o_prev, EndpointState o_now,
135 : : EndpointState r_prev, EndpointState r_now);
136 : : void FlipState(EndpointState orig, EndpointState resp);
137 : :
138 : 938 : void StateEntered (EndpointState o_state, EndpointState r_state)
139 : 938 : { ++state_cnt[o_state][r_state]; }
140 : 938 : void StateLeft (EndpointState o_state, EndpointState r_state)
141 : 938 : { --state_cnt[o_state][r_state]; }
142 : :
143 : : unsigned int Cnt(EndpointState state) const
144 : : { return Cnt(state, state); }
145 : : unsigned int Cnt(EndpointState state1, EndpointState state2) const
146 : : { return state_cnt[state1][state2]; }
147 : :
148 : : unsigned int NumStateEstablished() const
149 : : { return Cnt(TCP_ENDPOINT_ESTABLISHED); }
150 : : unsigned int NumStateHalfClose() const
151 : : { // corresponds to S2,S3
152 : : return Cnt(TCP_ENDPOINT_ESTABLISHED, TCP_ENDPOINT_CLOSED) +
153 : : Cnt(TCP_ENDPOINT_CLOSED, TCP_ENDPOINT_ESTABLISHED);
154 : : }
155 : : unsigned int NumStateHalfRst() const
156 : : {
157 : : return Cnt(TCP_ENDPOINT_ESTABLISHED, TCP_ENDPOINT_RESET) +
158 : : Cnt(TCP_ENDPOINT_RESET, TCP_ENDPOINT_ESTABLISHED);
159 : : }
160 : : unsigned int NumStateClosed() const
161 : : { return Cnt(TCP_ENDPOINT_CLOSED); }
162 : : unsigned int NumStateRequest() const
163 : : {
164 : : assert(Cnt(TCP_ENDPOINT_INACTIVE, TCP_ENDPOINT_SYN_SENT)==0);
165 : : return Cnt(TCP_ENDPOINT_SYN_SENT, TCP_ENDPOINT_INACTIVE);
166 : : }
167 : : unsigned int NumStateSuccRequest() const
168 : : {
169 : : return Cnt(TCP_ENDPOINT_SYN_SENT, TCP_ENDPOINT_SYN_ACK_SENT) +
170 : : Cnt(TCP_ENDPOINT_SYN_ACK_SENT, TCP_ENDPOINT_SYN_SENT);
171 : : }
172 : : unsigned int NumStateRstRequest() const
173 : : {
174 : : return Cnt(TCP_ENDPOINT_SYN_SENT, TCP_ENDPOINT_RESET) +
175 : : Cnt(TCP_ENDPOINT_RESET, TCP_ENDPOINT_SYN_SENT);
176 : : }
177 : : unsigned int NumStateInactive() const
178 : : { return Cnt(TCP_ENDPOINT_INACTIVE); }
179 : : unsigned int NumStatePartial() const;
180 : :
181 : : void PrintStats(BroFile* file, const char* prefix);
182 : :
183 : : private:
184 : : unsigned int state_cnt[TCP_ENDPOINT_RESET+1][TCP_ENDPOINT_RESET+1];
185 : : };
186 : :
187 : : class PacketProfiler {
188 : : public:
189 : : PacketProfiler(unsigned int mode, double freq, BroFile* arg_file);
190 : : ~PacketProfiler();
191 : :
192 : : static const unsigned int MODE_TIME = 1;
193 : : static const unsigned int MODE_PACKET = 2;
194 : : static const unsigned int MODE_VOLUME = 3;
195 : :
196 : : void ProfilePkt(double t, unsigned int bytes);
197 : :
198 : : protected:
199 : : BroFile* file;
200 : : unsigned int update_mode;
201 : : double update_freq;
202 : : double last_Utime, last_Stime, last_Rtime;
203 : : double last_timestamp, time;
204 : : unsigned int last_mem;
205 : : unsigned int pkt_cnt;
206 : : unsigned int byte_cnt;
207 : : };
208 : :
209 : : #endif
|