-
Notifications
You must be signed in to change notification settings - Fork 88
/
rate.c
407 lines (320 loc) · 12.3 KB
/
rate.c
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
/*
----------------------------------------------------
httpry - HTTP logging and information retrieval tool
----------------------------------------------------
Copyright (c) 2005-2014 Jason Bittel <[email protected]>
Licensed under GPLv2. For further information, see COPYING file.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include "config.h"
#include "error.h"
#include "rate.h"
#include "utility.h"
#define MAX_HOST_LEN 255
#define HASHSIZE 2048
#define NODE_BLOCKSIZE 100
#define NODE_ALLOC_BLOCKSIZE 10
struct host_stats {
char host[MAX_HOST_LEN + 1];
unsigned int count;
time_t first_packet;
time_t last_packet;
struct host_stats *next;
};
struct thread_args {
char *use_infile;
unsigned int rate_interval;
int rate_threshold;
};
void create_rate_stats_thread(int rate_interval, char *use_infile, int rate_threshold);
void exit_rate_stats_thread();
void *run_stats(void *args);
struct host_stats *remove_node(struct host_stats *node, struct host_stats *prev);
struct host_stats *get_host(char *str);
struct host_stats *get_node();
static pthread_t thread;
static int thread_created = 0;
static pthread_mutex_t stats_lock;
static struct host_stats **stats = NULL;
static struct host_stats *free_stack = NULL;
static struct host_stats **block_alloc = NULL;
static struct host_stats totals;
static struct thread_args thread_args;
/* Initialize rate stats counters and structures, and
start up the stats thread if necessary */
void init_rate_stats(int rate_interval, char *use_infile, int rate_threshold) {
/* Initialize host totals */
totals.count = 0;
totals.first_packet = 0;
totals.last_packet = 0;
/* Allocate host stats hash array */
if ((stats = (struct host_stats **) calloc(HASHSIZE, sizeof(struct host_stats *))) == NULL)
LOG_DIE("Cannot allocate memory for host stats");
if (!use_infile)
create_rate_stats_thread(rate_interval, use_infile, rate_threshold);
return;
}
/* Spawn a thread for updating and printing rate statistics */
void create_rate_stats_thread(int rate_interval, char *use_infile, int rate_threshold) {
sigset_t set;
int s;
if (thread_created) return;
thread_args.use_infile = use_infile;
thread_args.rate_interval = rate_interval;
thread_args.rate_threshold = rate_threshold;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGHUP);
s = pthread_mutex_init(&stats_lock, NULL);
if (s != 0)
LOG_DIE("Statistics thread mutex initialization failed with error %d", s);
s = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (s != 0)
LOG_DIE("Statistics thread signal blocking failed with error %d", s);
s = pthread_create(&thread, NULL, run_stats, (void *) &thread_args);
if (s != 0)
LOG_DIE("Statistics thread creation failed with error %d", s);
s = pthread_sigmask(SIG_UNBLOCK, &set, NULL);
if (s != 0)
LOG_DIE("Statistics thread signal unblocking failed with error %d", s);
thread_created = 1;
return;
}
/* Attempt to cancel the stats thread, cleanup allocated
memory and clear necessary counters and structures */
void cleanup_rate_stats() {
struct host_stats **i;
exit_rate_stats_thread();
if (block_alloc != NULL) {
for (i = block_alloc; *i; i++) {
free(*i);
}
free(block_alloc);
block_alloc = NULL;
}
if (stats != NULL) {
free(stats);
stats = NULL;
}
free_stack = NULL;
return;
}
/* Explicitly exit rate statistics thread */
void exit_rate_stats_thread() {
int s;
void *retval;
if (!thread_created) return;
s = pthread_cancel(thread);
if (s != 0)
LOG_WARN("Statistics thread cancellation failed with error %d", s);
s = pthread_join(thread, &retval);
if (s != 0)
LOG_WARN("Statistics thread join failed with error %d", s);
if (retval != PTHREAD_CANCELED)
LOG_WARN("Statistics thread exit value was unexpected");
thread_created = 0;
s = pthread_mutex_destroy(&stats_lock);
if (s != 0)
LOG_WARN("Statistcs thread mutex destroy failed with error %d", s);
return;
}
/* This is our statistics thread */
void *run_stats (void *args) {
struct thread_args *thread_args = (struct thread_args *) args;
while (1) {
sleep(thread_args->rate_interval);
display_rate_stats(thread_args->use_infile, thread_args->rate_threshold);
}
return (void *) 0;
}
/* Display the running average within each valid stats node */
void display_rate_stats(char *use_infile, int rate_threshold) {
time_t now;
char st_time[MAX_TIME_LEN];
unsigned int delta, rps = 0;
int i;
struct host_stats *node, *prev;
if (stats == NULL) return;
if (thread_created)
pthread_mutex_lock(&stats_lock);
if (use_infile) {
now = totals.last_packet;
} else {
now = time(NULL);
}
strftime(st_time, MAX_TIME_LEN, "%Y-%m-%d %H:%M:%S", localtime(&now));
#ifdef DEBUG
int j, num_buckets = 0, num_chain, max_chain = 0, num_nodes = 0;
for (j = 0; j < HASHSIZE; j++) {
if (stats[j]) num_buckets++;
num_chain = 0;
for (node = stats[j]; node != NULL; node = node->next) num_chain++;
if (num_chain > max_chain) max_chain = num_chain;
num_nodes += num_chain;
}
PRINT("----------------------------");
PRINT("Hash buckets: %d", HASHSIZE);
PRINT("Nodes inserted: %d", num_nodes);
PRINT("Buckets in use: %d", num_buckets);
PRINT("Hash collisions: %d", num_nodes - num_buckets);
PRINT("Longest hash chain: %d", max_chain);
PRINT("----------------------------");
#endif
/* Display rate stats for each valid host */
for (i = 0; i < HASHSIZE; i++) {
node = stats[i];
prev = NULL;
while (node != NULL) {
delta = now - node->first_packet;
if (delta > 0) {
rps = (unsigned int) ceil(node->count / (float) delta);
} else {
rps = 0;
}
if (rps >= rate_threshold) {
printf("%s%s%s%s%u rps\n", st_time, FIELD_DELIM, node->host, FIELD_DELIM, rps);
prev = node;
node = node->next;
} else {
node = remove_node(node, prev);
}
}
}
/* Display rate totals */
delta = (unsigned int) (now - totals.first_packet);
if (delta > 0)
printf("%s%stotals%s%3.2f rps\n", st_time, FIELD_DELIM, FIELD_DELIM, (float) totals.count / delta);
if (thread_created)
pthread_mutex_unlock(&stats_lock);
return;
}
/* Remove the given node from the hash and return it to the free stack;
returns the correct node for continuing to traverse the hash */
struct host_stats *remove_node(struct host_stats *node, struct host_stats *prev) {
struct host_stats *next;
unsigned int hashval;
/* Unlink the node from the hash */
if (prev == NULL) {
hashval = hash_str(node->host, HASHSIZE);
if (node->next) {
stats[hashval] = node->next;
} else {
stats[hashval] = NULL;
}
next = stats[hashval];
} else {
if (node->next) {
prev->next = node->next;
} else {
prev->next = NULL;
}
next = prev->next;
}
/* Add the node to the head of the free stack */
node->next = free_stack;
free_stack = node;
return next;
}
/* Update the stats for a given host; if the host is not
found in the hash, add it */
void update_host_stats(char *host, time_t t) {
struct host_stats *node;
unsigned int hashval;
if ((host == NULL) || (stats == NULL)) return;
if (thread_created)
pthread_mutex_lock(&stats_lock);
if ((node = get_host(host)) == NULL) {
node = get_node();
hashval = hash_str(host, HASHSIZE);
#ifdef DEBUG
ASSERT((hashval >= 0) && (hashval < HASHSIZE));
#endif
str_copy(node->host, host, MAX_HOST_LEN);
node->count = 0;
node->first_packet = t;
/* Link node into hash */
node->next = stats[hashval];
stats[hashval] = node;
}
if (node->first_packet == 0)
node->first_packet = t;
node->last_packet = t;
node->count++;
if (totals.first_packet == 0)
totals.first_packet = t;
totals.last_packet = t;
totals.count++;
if (thread_created)
pthread_mutex_unlock(&stats_lock);
return;
}
/* Lookup a particular node in hash; return pointer to node
if found, NULL otherwise */
struct host_stats *get_host(char *str) {
struct host_stats *node;
#ifdef DEBUG
ASSERT(str);
ASSERT(strlen(str) > 0);
ASSERT((hash_str(str, HASHSIZE) >= 0) && (hash_str(str, HASHSIZE) < HASHSIZE));
#endif
for (node = stats[hash_str(str, HASHSIZE)]; node != NULL; node = node->next)
if (str_compare(str, node->host) == 0)
return node;
return NULL;
}
/* Get a new node from either the free stack or an allocated block;
if the block is empty, allocate a new chunk of memory */
struct host_stats *get_node() {
static struct host_stats *block, *tail, **mv;
struct host_stats *head, **tmp;
static int alloc_size;
/* Initialize static variables as necessary */
if (block_alloc == NULL) {
block = NULL;
alloc_size = 0;
}
if (free_stack != NULL) { /* Get node from free stack */
head = free_stack;
free_stack = free_stack->next;
head->next = NULL;
} else if (block != NULL) { /* Get node from allocated block */
head = block;
if (block == tail) {
block = NULL;
} else {
block++;
}
} else { /* Out of nodes, allocate a new block */
if ((block = (struct host_stats *) malloc(NODE_BLOCKSIZE * sizeof(struct host_stats))) == NULL) {
LOG_DIE("Cannot allocate memory for node block");
}
/* Store pointer to allocated block so we can free it later */
if (block_alloc == NULL) {
if ((block_alloc = (struct host_stats **) malloc(NODE_ALLOC_BLOCKSIZE * sizeof(struct host_stats *))) == NULL) {
LOG_DIE("Cannot allocate memory for blocks array");
}
mv = block_alloc;
}
*mv = block;
if (++alloc_size % NODE_ALLOC_BLOCKSIZE == 0) {
tmp = realloc(block_alloc, ((alloc_size + NODE_ALLOC_BLOCKSIZE) * sizeof(struct host_stats *)));
if (tmp == NULL) {
LOG_DIE("Cannot re-allocate memory for blocks array");
}
block_alloc = tmp;
mv = block_alloc + alloc_size - 1;
}
mv++;
*mv = NULL;
tail = block + NODE_BLOCKSIZE - 1;
head = block;
block++;
}
return head;
}