]> git.sesse.net Git - jam/blobdiff - jam.c
Make a per-second traffic counter.
[jam] / jam.c
diff --git a/jam.c b/jam.c
index d1637f30fb7a8a6601fceacd242fb9a532cb130c..3d11408d9e9c6c4be49a3b7ecae83718ab9988ae 100644 (file)
--- a/jam.c
+++ b/jam.c
@@ -163,15 +163,11 @@ void *sender_worker(void *arg)
                sin.sin_port = htons(port);
                sin.sin_addr = destinations[dst_num];
        
-               fprintf(stderr, "connecting %u\n", i);
-
                if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
                        perror("connect()");
                        exit(1);
                }
                
-               fprintf(stderr, "connected\n");
-
                while (num_bytes > 0) {
                        unsigned bytes_to_send = num_bytes;
                        unsigned ret;
@@ -216,18 +212,18 @@ void *receiver_worker(void *arg)
        char buf[65536];
        unsigned long long bytes_received = 0;
 
-       fprintf(stderr, "Receiver started\n");
-
        for ( ;; ) {
                int ret = read(sock, buf, 65536);
                if (ret == 0)
                        break;
 
+               bytes_received += ret;
+
                // update the central counter after every 1MB (8ms
                // at gigabit speeds, should be enough) of received data
                if (bytes_received > 1048576) {
                        pthread_mutex_lock(&receive_mutex);
-                       total_bytes_sent += bytes_received;
+                       total_bytes_received += bytes_received;
                        pthread_mutex_unlock(&receive_mutex);
 
                        bytes_received = 0;
@@ -378,7 +374,31 @@ int main(int argc, char **argv)
                }
        }
 
-       sleep(3600);
+       // Just stay around collecting statistics until we're done.
+       for ( ;; ) {
+               unsigned long long sent, received;
+               static unsigned long long last_sent = 0, last_received = 0;
+               double recv_rate, send_rate;
+
+               pthread_mutex_lock(&send_mutex);
+               sent = total_bytes_sent;
+               pthread_mutex_unlock(&send_mutex);
+               
+               pthread_mutex_lock(&receive_mutex);
+               received = total_bytes_received;
+               pthread_mutex_unlock(&receive_mutex);
+
+               send_rate = (sent - last_sent) * 8.0 / 1048576.0;       
+               recv_rate = (received - last_received) * 8.0 / 1048576.0;       
+               
+               printf("%12llu %12llu  %5.0f Mbit/sec  %5.0f Mbit/sec\n", sent, received,
+                       send_rate, recv_rate);
+
+               last_sent = sent;
+               last_received = received;
+
+               sleep(1);
+       }
 
        exit(0);
 }