1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/* Copyright (C) 2007-2012 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Endace Technology Limited.
* \author Victor Julien <victor@inliniac.net>
*/
#ifndef __UTIL_PROFILE_H__
#define __UTIL_PROFILE_H__
#include "util-cpu.h"
#include "detect.h"
#ifdef PROFILING
#include "util-cpu.h"
#include "util-profiling-locks.h"
extern int profiling_rules_enabled;
extern int profiling_packets_enabled;
extern int profiling_sghs_enabled;
void SCProfilingPrintPacketProfile(Packet *);
void SCProfilingAddPacket(Packet *);
extern int profiling_keyword_enabled;
extern thread_local int profiling_keyword_entered;
#define KEYWORD_PROFILING_SET_LIST(ctx, list) { \
(ctx)->keyword_perf_list = (list); \
}
#define KEYWORD_PROFILING_START \
uint64_t profile_keyword_start_ = 0; \
uint64_t profile_keyword_end_ = 0; \
if (profiling_keyword_enabled) { \
if (profiling_keyword_entered > 0) { \
SCLogError("Re-entered profiling, exiting."); \
abort(); \
} \
profiling_keyword_entered++; \
profile_keyword_start_ = UtilCpuGetTicks(); \
}
/* we allow this macro to be called if profiling_keyword_entered == 0,
* so that we don't have to refactor some of the detection code. */
#define KEYWORD_PROFILING_END(ctx, type, m) \
if (profiling_keyword_enabled && profiling_keyword_entered) { \
profile_keyword_end_ = UtilCpuGetTicks(); \
SCProfilingKeywordUpdateCounter((ctx),(type),(profile_keyword_end_ - profile_keyword_start_),(m)); \
profiling_keyword_entered--; \
}
PktProfiling *SCProfilePacketStart(void);
#define PACKET_PROFILING_START(p) \
if (profiling_packets_enabled) { \
(p)->profile = SCProfilePacketStart(); \
if ((p)->profile != NULL) \
(p)->profile->ticks_start = UtilCpuGetTicks(); \
}
#define PACKET_PROFILING_RESTART(p) \
if (profiling_packets_enabled) { \
if ((p)->profile != NULL) \
(p)->profile->ticks_start = UtilCpuGetTicks(); \
}
#define PACKET_PROFILING_END(p) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
(p)->profile->ticks_end = UtilCpuGetTicks(); \
SCProfilingAddPacket((p)); \
}
#ifdef PROFILE_LOCKING
#define PACKET_PROFILING_RESET_LOCKS do { \
mutex_lock_cnt = 0; \
mutex_lock_wait_ticks = 0; \
mutex_lock_contention = 0; \
spin_lock_cnt = 0; \
spin_lock_wait_ticks = 0; \
spin_lock_contention = 0; \
rww_lock_cnt = 0; \
rww_lock_wait_ticks = 0; \
rww_lock_contention = 0; \
rwr_lock_cnt = 0; \
rwr_lock_wait_ticks = 0; \
rwr_lock_contention = 0; \
locks_idx = 0; \
record_locks = 1;\
} while (0)
#define PACKET_PROFILING_COPY_LOCKS(p, id) do { \
(p)->profile->tmm[(id)].mutex_lock_cnt = mutex_lock_cnt; \
(p)->profile->tmm[(id)].mutex_lock_wait_ticks = mutex_lock_wait_ticks; \
(p)->profile->tmm[(id)].mutex_lock_contention = mutex_lock_contention; \
(p)->profile->tmm[(id)].spin_lock_cnt = spin_lock_cnt; \
(p)->profile->tmm[(id)].spin_lock_wait_ticks = spin_lock_wait_ticks; \
(p)->profile->tmm[(id)].spin_lock_contention = spin_lock_contention; \
(p)->profile->tmm[(id)].rww_lock_cnt = rww_lock_cnt; \
(p)->profile->tmm[(id)].rww_lock_wait_ticks = rww_lock_wait_ticks; \
(p)->profile->tmm[(id)].rww_lock_contention = rww_lock_contention; \
(p)->profile->tmm[(id)].rwr_lock_cnt = rwr_lock_cnt; \
(p)->profile->tmm[(id)].rwr_lock_wait_ticks = rwr_lock_wait_ticks; \
(p)->profile->tmm[(id)].rwr_lock_contention = rwr_lock_contention; \
record_locks = 0; \
SCProfilingAddPacketLocks((p)); \
} while(0)
#else
#define PACKET_PROFILING_RESET_LOCKS
#define PACKET_PROFILING_COPY_LOCKS(p, id)
#endif
#define PACKET_PROFILING_TMM_START(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < TMM_SIZE) { \
(p)->profile->tmm[(id)].ticks_start = UtilCpuGetTicks();\
PACKET_PROFILING_RESET_LOCKS; \
} \
}
#define PACKET_PROFILING_TMM_END(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < TMM_SIZE) { \
PACKET_PROFILING_COPY_LOCKS((p), (id)); \
(p)->profile->tmm[(id)].ticks_end = UtilCpuGetTicks(); \
} \
}
#define FLOWWORKER_PROFILING_START(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROFILE_FLOWWORKER_SIZE) { \
(p)->profile->flowworker[(id)].ticks_start = UtilCpuGetTicks();\
} \
}
#define FLOWWORKER_PROFILING_END(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROFILE_FLOWWORKER_SIZE) { \
(p)->profile->flowworker[(id)].ticks_end = UtilCpuGetTicks(); \
} \
}
#define PACKET_PROFILING_RESET(p) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
SCFree((p)->profile); \
(p)->profile = NULL; \
}
#define PACKET_PROFILING_APP_START(dp, id) \
if (profiling_packets_enabled) { \
(dp)->ticks_start = UtilCpuGetTicks(); \
(dp)->alproto = (id); \
}
#define PACKET_PROFILING_APP_END(dp, id) \
if (profiling_packets_enabled) { \
BUG_ON((id) != (dp)->alproto); \
(dp)->ticks_end = UtilCpuGetTicks(); \
if ((dp)->ticks_start != 0 && (dp)->ticks_start < ((dp)->ticks_end)) { \
(dp)->ticks_spent = ((dp)->ticks_end - (dp)->ticks_start); \
} \
}
#define PACKET_PROFILING_APP_PD_START(dp) \
if (profiling_packets_enabled) { \
(dp)->proto_detect_ticks_start = UtilCpuGetTicks(); \
}
#define PACKET_PROFILING_APP_PD_END(dp) \
if (profiling_packets_enabled) { \
(dp)->proto_detect_ticks_end = UtilCpuGetTicks(); \
if ((dp)->proto_detect_ticks_start != 0 && (dp)->proto_detect_ticks_start < ((dp)->proto_detect_ticks_end)) { \
(dp)->proto_detect_ticks_spent = \
((dp)->proto_detect_ticks_end - (dp)->proto_detect_ticks_start); \
} \
}
#define PACKET_PROFILING_APP_RESET(dp) \
if (profiling_packets_enabled) { \
(dp)->ticks_start = 0; \
(dp)->ticks_end = 0; \
(dp)->ticks_spent = 0; \
(dp)->alproto = 0; \
(dp)->proto_detect_ticks_start = 0; \
(dp)->proto_detect_ticks_end = 0; \
(dp)->proto_detect_ticks_spent = 0; \
}
#define PACKET_PROFILING_APP_STORE(dp, p) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((dp)->alproto < ALPROTO_MAX) { \
(p)->profile->app[(dp)->alproto].ticks_spent += (dp)->ticks_spent; \
(p)->profile->proto_detect += (dp)->proto_detect_ticks_spent; \
} \
}
#define PACKET_PROFILING_DETECT_START(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROF_DETECT_SIZE) { \
(p)->profile->detect[(id)].ticks_start = UtilCpuGetTicks(); \
} \
}
#define PACKET_PROFILING_DETECT_END(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < PROF_DETECT_SIZE) { \
(p)->profile->detect[(id)].ticks_end = UtilCpuGetTicks();\
if ((p)->profile->detect[(id)].ticks_start != 0 && \
(p)->profile->detect[(id)].ticks_start < (p)->profile->detect[(id)].ticks_end) { \
(p)->profile->detect[(id)].ticks_spent += \
((p)->profile->detect[(id)].ticks_end - (p)->profile->detect[(id)].ticks_start); \
} \
} \
}
#define PACKET_PROFILING_LOGGER_START(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < LOGGER_SIZE) { \
(p)->profile->logger[(id)].ticks_start = UtilCpuGetTicks(); \
} \
}
#define PACKET_PROFILING_LOGGER_END(p, id) \
if (profiling_packets_enabled && (p)->profile != NULL) { \
if ((id) < LOGGER_SIZE) { \
(p)->profile->logger[(id)].ticks_end = UtilCpuGetTicks();\
if ((p)->profile->logger[(id)].ticks_start != 0 && \
(p)->profile->logger[(id)].ticks_start < (p)->profile->logger[(id)].ticks_end) { \
(p)->profile->logger[(id)].ticks_spent += \
((p)->profile->logger[(id)].ticks_end - (p)->profile->logger[(id)].ticks_start); \
} \
} \
}
#define SGH_PROFILING_RECORD(det_ctx, sgh) \
if (profiling_sghs_enabled) { \
SCProfilingSghUpdateCounter((det_ctx), (sgh)); \
}
extern int profiling_prefilter_enabled;
extern thread_local int profiling_prefilter_entered;
#define PREFILTER_PROFILING_START(det_ctx) \
(det_ctx)->prefilter_bytes = 0; \
(det_ctx)->prefilter_bytes_called = 0; \
uint64_t profile_prefilter_start_ = 0; \
uint64_t profile_prefilter_end_ = 0; \
if (profiling_prefilter_enabled) { \
if (profiling_prefilter_entered > 0) { \
SCLogError("Re-entered profiling, exiting."); \
abort(); \
} \
profiling_prefilter_entered++; \
profile_prefilter_start_ = UtilCpuGetTicks(); \
}
/* we allow this macro to be called if profiling_prefilter_entered == 0,
* so that we don't have to refactor some of the detection code. */
#define PREFILTER_PROFILING_END(ctx, profile_id) \
if (profiling_prefilter_enabled && profiling_prefilter_entered) { \
profile_prefilter_end_ = UtilCpuGetTicks(); \
if (profile_prefilter_end_ > profile_prefilter_start_) \
SCProfilingPrefilterUpdateCounter((ctx), (profile_id), \
(profile_prefilter_end_ - profile_prefilter_start_), (ctx)->prefilter_bytes, \
(ctx)->prefilter_bytes_called); \
profiling_prefilter_entered--; \
}
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes) \
(det_ctx)->prefilter_bytes += (bytes); \
(det_ctx)->prefilter_bytes_called++
struct SCProfileDetectCtx_;
void SCProfilingRulesGlobalInit(void);
void SCProfilingRuleDestroyCtx(struct SCProfileDetectCtx_ *);
void SCProfilingRuleInitCounters(DetectEngineCtx *);
void SCProfilingRuleUpdateCounter(DetectEngineThreadCtx *, uint16_t, uint64_t, int);
void SCProfilingRuleThreadSetup(struct SCProfileDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *);
void SCProfilingKeywordsGlobalInit(void);
void SCProfilingKeywordDestroyCtx(DetectEngineCtx *);//struct SCProfileKeywordDetectCtx_ *);
void SCProfilingKeywordInitCounters(DetectEngineCtx *);
void SCProfilingKeywordUpdateCounter(DetectEngineThreadCtx *det_ctx, int id, uint64_t ticks, int match);
void SCProfilingKeywordThreadSetup(struct SCProfileKeywordDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingKeywordThreadCleanup(DetectEngineThreadCtx *);
struct SCProfilePrefilterDetectCtx_;
void SCProfilingPrefilterGlobalInit(void);
void SCProfilingPrefilterDestroyCtx(DetectEngineCtx *);
void SCProfilingPrefilterInitCounters(DetectEngineCtx *);
void SCProfilingPrefilterUpdateCounter(DetectEngineThreadCtx *det_ctx, int id, uint64_t ticks,
uint64_t bytes, uint64_t bytes_called);
void SCProfilingPrefilterThreadSetup(struct SCProfilePrefilterDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingPrefilterThreadCleanup(DetectEngineThreadCtx *);
void SCProfilingSghsGlobalInit(void);
void SCProfilingSghDestroyCtx(DetectEngineCtx *);
void SCProfilingSghInitCounters(DetectEngineCtx *);
void SCProfilingSghUpdateCounter(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh);
void SCProfilingSghThreadSetup(struct SCProfileSghDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingSghThreadCleanup(DetectEngineThreadCtx *);
void SCProfilingInit(void);
void SCProfilingDestroy(void);
void SCProfilingRegisterTests(void);
void SCProfilingDump(void);
#else
#define KEYWORD_PROFILING_SET_LIST(a,b)
#define KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_END(a,b,c)
#define PACKET_PROFILING_START(p)
#define PACKET_PROFILING_RESTART(p)
#define PACKET_PROFILING_END(p)
#define PACKET_PROFILING_TMM_START(p, id)
#define PACKET_PROFILING_TMM_END(p, id)
#define PACKET_PROFILING_RESET(p)
#define PACKET_PROFILING_APP_START(dp, id)
#define PACKET_PROFILING_APP_END(dp, id)
#define PACKET_PROFILING_APP_RESET(dp)
#define PACKET_PROFILING_APP_STORE(dp, p)
#define PACKET_PROFILING_APP_PD_START(dp)
#define PACKET_PROFILING_APP_PD_END(dp)
#define PACKET_PROFILING_DETECT_START(p, id)
#define PACKET_PROFILING_DETECT_END(p, id)
#define PACKET_PROFILING_LOGGER_START(p, id)
#define PACKET_PROFILING_LOGGER_END(p, id)
#define SGH_PROFILING_RECORD(det_ctx, sgh)
#define FLOWWORKER_PROFILING_START(p, id)
#define FLOWWORKER_PROFILING_END(p, id)
#define PREFILTER_PROFILING_START(ctx)
#define PREFILTER_PROFILING_END(ctx, profile_id)
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
#endif /* PROFILING */
#ifdef PROFILE_RULES
extern int profiling_rules_enabled;
extern thread_local int profiling_rules_entered;
#ifndef PROFILING
void SCProfilingInit(void);
#endif
/**
* Extra data for rule profiling.
*/
typedef struct SCProfileData_ {
uint32_t sid;
uint32_t gid;
uint32_t rev;
uint64_t checks;
uint64_t matches;
uint64_t max;
uint64_t ticks_match;
uint64_t ticks_no_match;
} SCProfileData;
typedef struct SCProfileDetectCtx_ {
uint32_t size;
uint32_t id;
SCProfileData *data;
pthread_mutex_t data_m;
} SCProfileDetectCtx;
void SCProfilingRulesGlobalInit(void);
void SCProfilingRuleDestroyCtx(struct SCProfileDetectCtx_ *);
void SCProfilingRuleInitCounters(DetectEngineCtx *);
void SCProfilingRuleUpdateCounter(DetectEngineThreadCtx *, uint16_t, uint64_t, int);
void SCProfilingRuleThreadSetup(struct SCProfileDetectCtx_ *, DetectEngineThreadCtx *);
void SCProfilingRuleThreadCleanup(DetectEngineThreadCtx *);
int SCProfileRuleStart(Packet *p);
json_t *SCProfileRuleTriggerDump(DetectEngineCtx *de_ctx);
void SCProfileRuleStartCollection(void);
void SCProfileRuleStopCollection(void);
void SCProfilingRuleThreatAggregate(DetectEngineThreadCtx *det_ctx);
#define RULE_PROFILING_START(p) \
uint64_t profile_rule_start_ = 0; \
uint64_t profile_rule_end_ = 0; \
if (profiling_rules_enabled && SCProfileRuleStart((p))) { \
if (profiling_rules_entered > 0) { \
FatalError("Re-entered profiling, exiting."); \
} \
profiling_rules_entered++; \
profile_rule_start_ = UtilCpuGetTicks(); \
}
#define RULE_PROFILING_END(ctx, r, m, p) \
if (profiling_rules_enabled && ((p)->flags & PKT_PROFILE)) { \
profile_rule_end_ = UtilCpuGetTicks(); \
SCProfilingRuleUpdateCounter( \
ctx, r->profiling_id, profile_rule_end_ - profile_rule_start_, m); \
profiling_rules_entered--; \
BUG_ON(profiling_rules_entered < 0); \
}
#else /* PROFILE_RULES */
#define RULE_PROFILING_START(p)
#define RULE_PROFILING_END(a, b, c, p)
#endif /* PROFILE_RULES */
#endif /* ! __UTIL_PROFILE_H__ */
|