r4764 - developers/werner/owping

werner at docs.openmoko.org werner at docs.openmoko.org
Fri Nov 7 04:37:30 CET 2008


Author: werner
Date: 2008-11-07 04:37:29 +0100 (Fri, 07 Nov 2008)
New Revision: 4764

Added:
   developers/werner/owping/README
Modified:
   developers/werner/owping/Makefile
   developers/werner/owping/owping.c
   developers/werner/owping/rx.c
   developers/werner/owping/tx.c
Log:
- 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



Modified: developers/werner/owping/Makefile
===================================================================
--- developers/werner/owping/Makefile	2008-11-07 01:15:33 UTC (rev 4763)
+++ developers/werner/owping/Makefile	2008-11-07 03:37:29 UTC (rev 4764)
@@ -1,6 +1,7 @@
 CC=arm-angstrom-linux-gnueabi-gcc
 
 CFLAGS=-Wall -Wshadow -g -O
+LDFLAGS=-lm
 
 PREFIX=/usr
 

Added: developers/werner/owping/README
===================================================================
--- developers/werner/owping/README	                        (rev 0)
+++ developers/werner/owping/README	2008-11-07 03:37:29 UTC (rev 4764)
@@ -0,0 +1,39 @@
+owping - One-way ping (UDP)
+===========================
+
+owping sends small UDP packets to determine the one-way latency and,
+indirectly, the one-way jitter.
+
+
+Usage
+-----
+
+Receiver:
+
+% owping
+
+Sender:
+
+% owping [-c count] ip_address
+
+
+Known issues
+------------
+
+- accuracy of latency measurements depends on precisely synchronized system
+  clocks on both sides (this is fundamentally unavoidable)
+
+- jitter measurements include application delays in the sending owping.
+  Should increase CPU priority.
+
+- even better jitter measurements may be possible if the sender includes
+  the time between transmissions in the packet data and the receiver
+  compares this with the inter-arrival time it measures.
+
+- receiver should also handle SIGINT, and print statistics
+
+- receiver should count gaps in the sequence numbers
+
+- should support host name resolution
+
+- should support setting the port number

Modified: developers/werner/owping/owping.c
===================================================================
--- developers/werner/owping/owping.c	2008-11-07 01:15:33 UTC (rev 4763)
+++ developers/werner/owping/owping.c	2008-11-07 03:37:29 UTC (rev 4764)
@@ -14,6 +14,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <unistd.h>
 #include <arpa/inet.h>
 
 #include "owping.h"
@@ -24,7 +25,7 @@
 
 static void usage(const char *name)
 {
-    fprintf(stderr, "usage: %s [ip_address]\n", name);
+    fprintf(stderr, "usage: %s [[-c count] ip_address]\n", name);
     exit(1);
 }
 
@@ -32,18 +33,32 @@
 int main(int argc, char **argv)
 {
     struct sockaddr_in addr;
+    int count = -1;
+    int c;
+    char *end;
 
-    switch (argc) {
-    case 1:
+    while ((c = getopt(argc, argv, "c:")) != EOF)
+	switch (c) {
+	case 'c':
+	    count = strtoul(optarg, &end, 10);
+	    if (*end)
+		usage(*argv);
+	    break;
+	default:
+	    usage(*argv);
+	}
+
+    switch (argc-optind) {
+    case 0:
 	rx(PORT);
 	break;
-    case 2:
+    case 1:
 	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = inet_addr(argv[1]);
+	addr.sin_addr.s_addr = inet_addr(argv[optind]);
 	addr.sin_port = htons(PORT);
 	if (addr.sin_addr.s_addr == (in_addr_t) -1)
 	    usage(*argv);
-	tx(addr, 100);
+	tx(addr, count);
 	break;
     default:
 	usage(*argv);

Modified: developers/werner/owping/rx.c
===================================================================
--- developers/werner/owping/rx.c	2008-11-07 01:15:33 UTC (rev 4763)
+++ developers/werner/owping/rx.c	2008-11-07 03:37:29 UTC (rev 4764)
@@ -14,6 +14,8 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
+#include <math.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
@@ -22,18 +24,62 @@
 #include "owping.h"
 
 
-static void delta(struct timeval t0, struct timeval t1)
+struct stats {
+    long min, max;
+    double sum, sq;
+    int n;
+};
+
+
+static void stats_init(struct stats *stats)
 {
+    memset(stats, 0, sizeof(*stats));
+}
+
+
+static void stats_print(const char *label, const struct stats *stats)
+{
+    double avg = stats->sum/stats->n;
+
+    printf("%s min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms (%d packets)\n",
+      label,
+      stats->min/1000.0, avg/1000.0, stats->max/1000.0,
+      sqrt(stats->sq/stats->n-avg*avg)/1000.0,
+      stats->n);
+}
+
+
+static void delta(struct timeval t0, struct timeval t1, struct stats *stats)
+{
+    char sign = ' ';
     long us;
 
     us = t1.tv_usec-t0.tv_usec;
     us += (t1.tv_sec-t0.tv_sec)*1000000;
-    printf(" %ld.%03ldms", us/1000, us % 1000);
+
+    if (!stats->n) {
+	stats->min = stats->max = us;
+    } else {
+	if (stats->min > us)
+	    stats->min = us;
+	if (stats->max < us)
+	    stats->max = us;
+    }
+    stats->n++;
+    stats->sum += us;
+    stats->sq += (double) us*us;
+
+    if (us < 0) {
+	us = -us;
+	sign = '-';
+    }
+    printf(" %c%ld.%03ldms", sign, us/1000, us % 1000);
 }
 
 
 void rx(int port)
 {
+    struct stats stats_itf, stats_user;
     struct sockaddr_in addr;
     int s;
 
@@ -51,7 +97,8 @@
 	exit(1);
     }
     
-    // setsockopt timestamp
+    stats_init(&stats_itf);
+    stats_init(&stats_user);
 
     while (1) {
 	struct owping buf;
@@ -67,7 +114,7 @@
 	    exit(1);
 	}
 	if (got == 1)
-	    return;
+	    break;
 	if (got != sizeof(buf)) {
 	    fprintf(stderr, "bad read: expected %u, got %u\n",
 	      (unsigned) sizeof(buf), (unsigned) got);
@@ -85,8 +132,11 @@
 	  inet_ntoa(addr.sin_addr));
 	t_src.tv_sec = ntohl(buf.tv_sec);
 	t_src.tv_usec = ntohl(buf.tv_usec);
-	delta(t_src, t_itf);
-	delta(t_src, t_user);
+	delta(t_src, t_itf, &stats_itf);
+	delta(t_src, t_user, &stats_user);
 	putchar('\n');
     }
+    stats_print("itf:", &stats_itf);
+    stats_print("app:", &stats_user);
+
 }

Modified: developers/werner/owping/tx.c
===================================================================
--- developers/werner/owping/tx.c	2008-11-07 01:15:33 UTC (rev 4763)
+++ developers/werner/owping/tx.c	2008-11-07 03:37:29 UTC (rev 4764)
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <signal.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -22,10 +23,26 @@
 #include "owping.h"
 
 
+static int s;
+
+
+static void cleanup(void)
+{
+    write(s, "", 1);
+}
+
+
+static void handler(int sig)
+{
+    cleanup();
+    _exit(0);
+}
+
+
 void tx(struct sockaddr_in addr, int num)
 {
     ssize_t sent;
-    int s, seq;
+    int seq;
 
     s = socket(PF_INET, SOCK_DGRAM, 0);
     if (s < 0) {
@@ -38,7 +55,16 @@
 	exit(1);
     }
 
-    for (seq = 0; seq != num; seq++) {
+    if (atexit(cleanup)) {
+	perror("atexit");
+	exit(1);
+    }
+    if (signal(SIGINT, handler) == SIG_ERR) {
+	perror("signal(SIGINT)");
+	exit(1);
+    }
+
+    for (seq = 0; num < 0 || seq != num; seq++) {
 	struct owping buf;
 	struct timeval now;
 
@@ -63,14 +89,4 @@
 	    exit(1);
 	}
     }
-
-    sent = write(s, "", 1);
-    if (sent < 0) {
-	perror("write");
-	exit(1);
-    }
-    if (sent != 1) {
-	fprintf(stderr, "bad write: %u < 1\n", (unsigned) sent);
-	exit(1);
-    }
 }




More information about the commitlog mailing list