| 1 | #include <inttypes.h> |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <stdbool.h> |
|---|
| 4 | #include <time.h> |
|---|
| 5 | #include <string.h> |
|---|
| 6 | #include "libtrace.h" |
|---|
| 7 | #include "tracereport.h" |
|---|
| 8 | |
|---|
| 9 | static double starttime; |
|---|
| 10 | static double endtime; |
|---|
| 11 | static bool has_starttime = false; |
|---|
| 12 | static bool has_endtime = false; |
|---|
| 13 | static uint64_t packets = 0; |
|---|
| 14 | |
|---|
| 15 | void misc_per_packet(struct libtrace_packet_t *packet) |
|---|
| 16 | { |
|---|
| 17 | double ts = trace_get_seconds(packet); |
|---|
| 18 | if (!has_starttime || starttime > ts) |
|---|
| 19 | starttime = ts; |
|---|
| 20 | if (!has_endtime || endtime < ts) |
|---|
| 21 | endtime = ts; |
|---|
| 22 | has_starttime = has_endtime = true; |
|---|
| 23 | ++packets; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | static char *ts_to_date(double ts) |
|---|
| 27 | { |
|---|
| 28 | time_t sec = (time_t)ts; |
|---|
| 29 | static char ret[1024]; |
|---|
| 30 | strcpy(ret,ctime(&sec)); |
|---|
| 31 | ret[strlen(ret)-1]='\0'; /* Get rid of the annoying \n */ |
|---|
| 32 | return ret; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | static char *duration(double ts) |
|---|
| 36 | { |
|---|
| 37 | static char ret[1024]; |
|---|
| 38 | char tmp[1024]; |
|---|
| 39 | ret[0]='\0'; |
|---|
| 40 | if (ts == 0) |
|---|
| 41 | return "0 seconds"; |
|---|
| 42 | if (ts>=24*60*60) { |
|---|
| 43 | snprintf(ret,sizeof(ret),"%i days",(int)(ts/(24*60*60))); |
|---|
| 44 | ts-=(int)(ts/(24*60*60))*24*60*60; |
|---|
| 45 | } |
|---|
| 46 | if (ts>=60*60) { |
|---|
| 47 | snprintf(tmp,sizeof(tmp),"%s%i hours", |
|---|
| 48 | ret[0]?", ":"", |
|---|
| 49 | (int)ts/(60*60)); |
|---|
| 50 | strcat(ret,tmp); |
|---|
| 51 | ts-=(int)(ts/(60*60))*60*60; |
|---|
| 52 | } |
|---|
| 53 | if (ts>=60) { |
|---|
| 54 | snprintf(tmp,sizeof(tmp),"%s%i minutes", |
|---|
| 55 | ret[0]?", ":"", |
|---|
| 56 | (int)ts/60); |
|---|
| 57 | strcat(ret,tmp); |
|---|
| 58 | ts-=(int)(ts/60)*60; |
|---|
| 59 | } |
|---|
| 60 | if (ts>0) { |
|---|
| 61 | snprintf(tmp,sizeof(tmp),"%s%.04f seconds", |
|---|
| 62 | ret[0]?", ":"", |
|---|
| 63 | ts); |
|---|
| 64 | strcat(ret,tmp); |
|---|
| 65 | } |
|---|
| 66 | return ret; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void misc_report(void) |
|---|
| 70 | { |
|---|
| 71 | FILE *out = fopen("misc.rpt", "w"); |
|---|
| 72 | if (!out) { |
|---|
| 73 | perror("fopen"); |
|---|
| 74 | return; |
|---|
| 75 | } |
|---|
| 76 | fprintf(out, "Start time: %.04f (%s)\n",starttime,ts_to_date(starttime)); |
|---|
| 77 | fprintf(out, "End time: %.04f (%s)\n",endtime,ts_to_date(endtime)); |
|---|
| 78 | fprintf(out, "Duration: %.04f (%s)\n",endtime-starttime, |
|---|
| 79 | duration(endtime-starttime)); |
|---|
| 80 | fprintf(out, "Total Packets: %" PRIu64 "\n",packets); |
|---|
| 81 | fprintf(out, "Average packet rate: %.02f packets/sec\n", |
|---|
| 82 | packets/(endtime-starttime)); |
|---|
| 83 | } |
|---|