Index: trunk/tools/counter/fast_counter.c |
— | — | @@ -15,7 +15,7 @@ |
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | * |
18 | 18 | * Compilation: |
19 | | - * gcc -static -O3 -o fast_counter fast_counter.c -lJudy -lm |
| 19 | + * gcc -static -O3 -o fast_counter fast_counter.c -lJudy -lm |
20 | 20 | * |
21 | 21 | * Usage: |
22 | 22 | * ./fast_counter factor hourlythreshold dailythreshold weeklythreshold |
— | — | @@ -53,10 +53,89 @@ |
54 | 54 | #define GETFIELD 7 |
55 | 55 | #define TIMEFIELD 2 |
56 | 56 | #define URLFIELD 8 |
| 57 | +#define IPFIELD 4 |
57 | 58 | |
| 59 | + |
| 60 | +Word_t ipaddr; |
58 | 61 | uint8_t inbuffer[MAXINPUT]; |
59 | 62 | uint8_t buffer[MAXINPUT]; |
| 63 | +uint8_t host[64]; |
60 | 64 | |
| 65 | + |
| 66 | + |
| 67 | +int |
| 68 | +freeJSI (Pvoid_t agg) |
| 69 | +{ |
| 70 | + Pvoid_t iarray; |
| 71 | + PWord_t value; |
| 72 | + uint8_t fbuffer[MAXINPUT]; |
| 73 | + Word_t Bytes = 0; |
| 74 | + |
| 75 | + fbuffer[0] = '\0'; |
| 76 | + if (agg) |
| 77 | + { |
| 78 | + JSLF (value, agg, fbuffer); |
| 79 | + while (value != NULL) |
| 80 | + { |
| 81 | + iarray = (Pvoid_t) * value; |
| 82 | + JLFA (Bytes, iarray); |
| 83 | + JSLN (value, agg, fbuffer); |
| 84 | + } |
| 85 | + JSLFA (Bytes, agg); |
| 86 | + } |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +int |
| 91 | +freeJSS (Pvoid_t agg) |
| 92 | +{ |
| 93 | + Pvoid_t iarray; |
| 94 | + PWord_t value; |
| 95 | + uint8_t fbuffer[MAXINPUT]; |
| 96 | + Word_t Bytes = 0; |
| 97 | + |
| 98 | + fbuffer[0] = '\0'; |
| 99 | + if (agg) |
| 100 | + { |
| 101 | + JSLF (value, agg, fbuffer); |
| 102 | + while (value != NULL) |
| 103 | + { |
| 104 | + iarray = (Pvoid_t) * value; |
| 105 | + JSLFA (Bytes, iarray); |
| 106 | + JSLN (value, agg, fbuffer); |
| 107 | + } |
| 108 | + JSLFA (Bytes, agg); |
| 109 | + } |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +int |
| 114 | +freeJS1 (Pvoid_t agg) |
| 115 | +{ |
| 116 | + Pvoid_t iarray; |
| 117 | + PWord_t value; |
| 118 | + uint8_t fbuffer[MAXINPUT]; |
| 119 | + Word_t Bytes = 0; |
| 120 | + Word_t TBytes = 0; |
| 121 | + |
| 122 | + fbuffer[0] = '\0'; |
| 123 | + if (agg) |
| 124 | + { |
| 125 | + JSLF (value, agg, fbuffer); |
| 126 | + while (value != NULL) |
| 127 | + { |
| 128 | + iarray = (Pvoid_t) * value; |
| 129 | + J1FA (Bytes, iarray); |
| 130 | + TBytes += Bytes; |
| 131 | + JSLN (value, agg, fbuffer); |
| 132 | + } |
| 133 | + JSLFA (Bytes, agg); |
| 134 | + TBytes += Bytes; |
| 135 | + } |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | + |
61 | 140 | void |
62 | 141 | dumpAgg (Pvoid_t agg, int aggType, int thresh, int ftime, int ltime) |
63 | 142 | { |
— | — | @@ -183,9 +262,9 @@ |
184 | 263 | char *hbuf; |
185 | 264 | |
186 | 265 | char timestamp[32]; |
187 | | - char host[64]; |
188 | 266 | char page[256]; |
189 | 267 | int i; |
| 268 | + char *x; |
190 | 269 | |
191 | 270 | char *result = NULL; |
192 | 271 | |
— | — | @@ -205,6 +284,24 @@ |
206 | 285 | strncpy (timestamp, result, 32); |
207 | 286 | break; |
208 | 287 | |
| 288 | + case IPFIELD: |
| 289 | + if (strlen (result) < 8) |
| 290 | + return -1; |
| 291 | + ipaddr = 0; |
| 292 | + ipaddr += (strtol (result, &x, 10) << 24); |
| 293 | + if (result == x) |
| 294 | + return -1; |
| 295 | + ipaddr += (strtol (x + 1, &x, 10) << 16); |
| 296 | + if (result == x) |
| 297 | + return -1; |
| 298 | + ipaddr += (strtol (x + 1, &x, 10) << 8); |
| 299 | + if (result == x) |
| 300 | + return -1; |
| 301 | + ipaddr += strtol (x + 1, &x, 10); |
| 302 | + if (result == x) |
| 303 | + return -1; |
| 304 | + break; |
| 305 | + |
209 | 306 | case URLFIELD: |
210 | 307 | if (strlen (result) <= 9 || strncmp ("http://", result, 7) != 0) |
211 | 308 | return -1; |
— | — | @@ -241,10 +338,13 @@ |
242 | 339 | int |
243 | 340 | main (int argc, char *argv[]) |
244 | 341 | { |
245 | | - Pvoid_t aggHour = (Pvoid_t) NULL; // hour array |
246 | | - Pvoid_t aggDay = (Pvoid_t) NULL; // day array |
247 | | - Pvoid_t aggWeek = (Pvoid_t) NULL; // week array |
| 342 | + Pvoid_t aggHour = (Pvoid_t) NULL; /* hour array */ |
| 343 | + Pvoid_t aggDay = (Pvoid_t) NULL; /* day array */ |
| 344 | + Pvoid_t aggWeek = (Pvoid_t) NULL; /* week array */ |
248 | 345 | |
| 346 | + Pvoid_t aggSiteUnqW = (Pvoid_t) NULL; /* per site unique visitors per week */ |
| 347 | + Pvoid_t uniqsite; |
| 348 | + |
249 | 349 | pid_t aggHourpid = -1; |
250 | 350 | pid_t aggDaypid = -1; |
251 | 351 | pid_t aggWeekpid = -1; |
— | — | @@ -255,7 +355,8 @@ |
256 | 356 | |
257 | 357 | int sampleRate; |
258 | 358 | |
259 | | - PWord_t value; // item counter pointer |
| 359 | + |
| 360 | + PWord_t value; |
260 | 361 | Word_t Bytes; |
261 | 362 | int linetime; |
262 | 363 | |
— | — | @@ -265,6 +366,7 @@ |
266 | 367 | exit (1); |
267 | 368 | } |
268 | 369 | |
| 370 | + |
269 | 371 | sampleRate = atoi (argv[1]); |
270 | 372 | |
271 | 373 | signal (SIGCHLD, SIG_IGN); |
— | — | @@ -281,7 +383,7 @@ |
282 | 384 | if (aggWeekStart == 0) |
283 | 385 | aggWeekStart = linetime; |
284 | 386 | |
285 | | - //Handle Hourly aggregate |
| 387 | + /*Handle Hourly aggregate */ |
286 | 388 | if (linetime >= aggHourStart + 3600) |
287 | 389 | { |
288 | 390 | waitpid (aggHourpid, NULL, 1); |
— | — | @@ -291,9 +393,12 @@ |
292 | 394 | dumpAgg (aggHour, 0, atoi (argv[2]), aggHourStart, |
293 | 395 | aggHourStart + 3600); |
294 | 396 | } |
295 | | - JSLFA (Bytes, aggHour); // free array |
296 | | -// fprintf (stderr,"Freed %lu bytes of memory from hourly aggregate.\n", Bytes); |
297 | | - |
| 397 | + JSLFA (Bytes, aggHour); |
| 398 | +#ifdef DEBUG |
| 399 | + fprintf (stderr, |
| 400 | + "Freed %lu bytes of memory from hourly aggregate.\n", |
| 401 | + Bytes); |
| 402 | +#endif |
298 | 403 | aggHour = NULL; |
299 | 404 | aggHourStart = aggHourStart + 3600; |
300 | 405 | } |
— | — | @@ -305,7 +410,7 @@ |
306 | 411 | } |
307 | 412 | (*value) += sampleRate; |
308 | 413 | |
309 | | - //Handle daily aggregate |
| 414 | + /*Handle daily aggregate */ |
310 | 415 | if (linetime >= aggDayStart + 86400) |
311 | 416 | { |
312 | 417 | waitpid (aggDaypid, NULL, 1); |
— | — | @@ -315,9 +420,12 @@ |
316 | 421 | dumpAgg (aggDay, 1, atoi (argv[3]), aggDayStart, |
317 | 422 | aggDayStart + 86400); |
318 | 423 | } |
319 | | - JSLFA (Bytes, aggDay); // free array |
320 | | -// fprintf (stderr,"Freed %lu bytes of memory from daily aggregate.\n", Bytes); |
321 | | - |
| 424 | + JSLFA (Bytes, aggDay); |
| 425 | +#ifdef DEBUG |
| 426 | + fprintf (stderr, |
| 427 | + "Freed %lu bytes of memory from daily aggregate.\n", |
| 428 | + Bytes); |
| 429 | +#endif |
322 | 430 | aggDay = NULL; |
323 | 431 | aggDayStart = aggDayStart + 86400; |
324 | 432 | } |
— | — | @@ -329,7 +437,7 @@ |
330 | 438 | } |
331 | 439 | (*value) += sampleRate; |
332 | 440 | |
333 | | - //Handle weekly aggregate |
| 441 | + /*Handle weekly aggregate */ |
334 | 442 | if (linetime >= aggWeekStart + (86400 * 7)) |
335 | 443 | { |
336 | 444 | waitpid (aggWeekpid, NULL, 1); |
— | — | @@ -339,10 +447,15 @@ |
340 | 448 | dumpAgg (aggWeek, 2, atoi (argv[4]), aggWeekStart, |
341 | 449 | aggWeekStart + (86400 * 7)); |
342 | 450 | } |
343 | | - JSLFA (Bytes, aggWeek); // free array |
344 | | -// fprintf (stderr,"Freed %lu bytes of memory from weekly aggregate.\n", Bytes); |
345 | | - |
| 451 | + JSLFA (Bytes, aggWeek); |
| 452 | + freeJS1 (aggSiteUnqW); |
| 453 | +#ifdef DEBUG |
| 454 | + fprintf (stderr, |
| 455 | + "Freed %lu bytes of memory from weekly aggregate.\n", |
| 456 | + Bytes); |
| 457 | +#endif |
346 | 458 | aggWeek = NULL; |
| 459 | + aggSiteUnqW = NULL; |
347 | 460 | aggWeekStart = aggWeekStart + (86400 * 7); |
348 | 461 | } |
349 | 462 | JSLI (value, aggWeek, buffer); |
— | — | @@ -353,6 +466,15 @@ |
354 | 467 | } |
355 | 468 | (*value) += sampleRate; |
356 | 469 | |
| 470 | + JSLI (value, aggSiteUnqW, host); |
| 471 | + if (value == PJERR) |
| 472 | + { |
| 473 | + printf ("-EMALLOCBOOM\n"); |
| 474 | + exit (1); |
| 475 | + } |
| 476 | + uniqsite = (Pvoid_t) * value; |
| 477 | + J1S (Bytes, uniqsite, ipaddr); |
| 478 | + (*value) = (Word_t) uniqsite; |
357 | 479 | } |
358 | 480 | } |
359 | 481 | waitpid (aggWeekpid, NULL, 1); |