Ignore:
Timestamp:
11/07/08 04:37:29 (5 years ago)
Author:
werner
Message:
  • added README with a list of known issues
  • receiver prints ping-like latency statistics at the end
  • sender supports -c count option, like ping
  • sender properly shuts down the receiver if failing or stopped with SIGINT
File:
1 edited

Legend:

Unmodified
Added
Removed
  • developers/werner/owping/rx.c

    r4763 r4764  
    1515#include <stdlib.h> 
    1616#include <stdio.h> 
     17#include <string.h> 
     18#include <math.h> 
    1719#include <sys/time.h> 
    1820#include <sys/socket.h> 
     
    2325 
    2426 
    25 static void delta(struct timeval t0, struct timeval t1) 
     27struct stats { 
     28    long min, max; 
     29    double sum, sq; 
     30    int n; 
     31}; 
     32 
     33 
     34static void stats_init(struct stats *stats) 
    2635{ 
     36    memset(stats, 0, sizeof(*stats)); 
     37} 
     38 
     39 
     40static 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 
     52static void delta(struct timeval t0, struct timeval t1, struct stats *stats) 
     53{ 
     54    char sign = ' '; 
    2755    long us; 
    2856 
    2957    us = t1.tv_usec-t0.tv_usec; 
    3058    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); 
    3277} 
    3378 
     
    3580void rx(int port) 
    3681{ 
     82    struct stats stats_itf, stats_user; 
    3783    struct sockaddr_in addr; 
    3884    int s; 
     
    5298    } 
    5399     
    54     // setsockopt timestamp 
     100    stats_init(&stats_itf); 
     101    stats_init(&stats_user); 
    55102 
    56103    while (1) { 
     
    68115        } 
    69116        if (got == 1) 
    70             return; 
     117            break; 
    71118        if (got != sizeof(buf)) { 
    72119            fprintf(stderr, "bad read: expected %u, got %u\n", 
     
    86133        t_src.tv_sec = ntohl(buf.tv_sec); 
    87134        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); 
    90137        putchar('\n'); 
    91138    } 
     139    stats_print("itf:", &stats_itf); 
     140    stats_print("app:", &stats_user); 
     141 
    92142} 
Note: See TracChangeset for help on using the changeset viewer.