Changeset 4764 for developers/werner/owping/rx.c
- Timestamp:
- 11/07/08 04:37:29 (5 years ago)
- File:
-
- 1 edited
-
developers/werner/owping/rx.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
developers/werner/owping/rx.c
r4763 r4764 15 15 #include <stdlib.h> 16 16 #include <stdio.h> 17 #include <string.h> 18 #include <math.h> 17 19 #include <sys/time.h> 18 20 #include <sys/socket.h> … … 23 25 24 26 25 static void delta(struct timeval t0, struct timeval t1) 27 struct stats { 28 long min, max; 29 double sum, sq; 30 int n; 31 }; 32 33 34 static void stats_init(struct stats *stats) 26 35 { 36 memset(stats, 0, sizeof(*stats)); 37 } 38 39 40 static void stats_print(const char *label, const struct stats *stats) 41 { 42 double avg = stats->sum/stats->n; 43 44 printf("%s min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms (%d packets)\n", 45 label, 46 stats->min/1000.0, avg/1000.0, stats->max/1000.0, 47 sqrt(stats->sq/stats->n-avg*avg)/1000.0, 48 stats->n); 49 } 50 51 52 static void delta(struct timeval t0, struct timeval t1, struct stats *stats) 53 { 54 char sign = ' '; 27 55 long us; 28 56 29 57 us = t1.tv_usec-t0.tv_usec; 30 58 us += (t1.tv_sec-t0.tv_sec)*1000000; 31 printf(" %ld.%03ldms", us/1000, us % 1000); 59 60 if (!stats->n) { 61 stats->min = stats->max = us; 62 } else { 63 if (stats->min > us) 64 stats->min = us; 65 if (stats->max < us) 66 stats->max = us; 67 } 68 stats->n++; 69 stats->sum += us; 70 stats->sq += (double) us*us; 71 72 if (us < 0) { 73 us = -us; 74 sign = '-'; 75 } 76 printf(" %c%ld.%03ldms", sign, us/1000, us % 1000); 32 77 } 33 78 … … 35 80 void rx(int port) 36 81 { 82 struct stats stats_itf, stats_user; 37 83 struct sockaddr_in addr; 38 84 int s; … … 52 98 } 53 99 54 // setsockopt timestamp 100 stats_init(&stats_itf); 101 stats_init(&stats_user); 55 102 56 103 while (1) { … … 68 115 } 69 116 if (got == 1) 70 return;117 break; 71 118 if (got != sizeof(buf)) { 72 119 fprintf(stderr, "bad read: expected %u, got %u\n", … … 86 133 t_src.tv_sec = ntohl(buf.tv_sec); 87 134 t_src.tv_usec = ntohl(buf.tv_usec); 88 delta(t_src, t_itf );89 delta(t_src, t_user );135 delta(t_src, t_itf, &stats_itf); 136 delta(t_src, t_user, &stats_user); 90 137 putchar('\n'); 91 138 } 139 stats_print("itf:", &stats_itf); 140 stats_print("app:", &stats_user); 141 92 142 }
Note: See TracChangeset
for help on using the changeset viewer.
