Store and log connection time for inputs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 16 Aug 2013 00:09:34 +0000 (02:09 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 16 Aug 2013 00:09:34 +0000 (02:09 +0200)
httpinput.cpp
input.h
input_stats.cpp
state.proto
udpinput.cpp

index 1bd14de..b066b24 100644 (file)
@@ -39,6 +39,7 @@ HTTPInput::HTTPInput(const string &url)
        stats.url = url;
        stats.bytes_received = 0;
        stats.data_bytes_received = 0;
+       stats.connect_time = -1;
 }
 
 HTTPInput::HTTPInput(const InputProto &serialized)
@@ -68,6 +69,11 @@ HTTPInput::HTTPInput(const InputProto &serialized)
        stats.url = url;
        stats.bytes_received = serialized.bytes_received();
        stats.data_bytes_received = serialized.data_bytes_received();
+       if (serialized.has_connect_time()) {
+               stats.connect_time = serialized.connect_time();
+       } else {
+               stats.connect_time = time(NULL);
+       }
 }
 
 void HTTPInput::close_socket()
@@ -75,6 +81,9 @@ void HTTPInput::close_socket()
        if (sock != -1) {
                safe_close(sock);
        }
+
+       MutexLock lock(&stats_mutex);
+       stats.connect_time = -1;
 }
 
 InputProto HTTPInput::serialize() const
@@ -91,6 +100,7 @@ InputProto HTTPInput::serialize() const
        serialized.set_sock(sock);
        serialized.set_bytes_received(stats.bytes_received);
        serialized.set_data_bytes_received(stats.data_bytes_received);
+       serialized.set_connect_time(stats.connect_time);
        return serialized;
 }
 
@@ -301,6 +311,9 @@ void HTTPInput::do_work()
                                        request = "GET " + path + " HTTP/1.0\r\nUser-Agent: cubemap\r\n\r\n";
                                        request_bytes_sent = 0;
                                }
+
+                               MutexLock lock(&stats_mutex);
+                               stats.connect_time = time(NULL);
                        }
                        break;
                case SENDING_REQUEST: {
diff --git a/input.h b/input.h
index f139755..a00113d 100644 (file)
--- a/input.h
+++ b/input.h
@@ -32,7 +32,10 @@ struct InputStats {
        // Not reset across connections.
        size_t data_bytes_received;
 
-       // TODO: Number of loss events and connection time might both be useful,
+       // When the current connection was initiated. -1 if we are not currently connected.
+       time_t connect_time;
+
+       // TODO: Number of loss events might both be useful,
        // similar to for clients. Also, per-connection byte counters.
 };
 
index 1edfbde..e3e89e6 100644 (file)
@@ -27,6 +27,7 @@ void InputStatsThread::do_work()
        while (!should_stop()) {
                int fd;
                FILE *fp;
+               time_t now;
 
                // Open a new, temporary file.
                char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
@@ -48,11 +49,19 @@ void InputStatsThread::do_work()
                        goto sleep;
                }
 
+               now = time(NULL);
                for (size_t i = 0; i < inputs.size(); ++i) {
                        InputStats stats = inputs[i]->get_stats();
-                       fprintf(fp, "%s %llu %llu\n", stats.url.c_str(),
-                               (long long unsigned)(stats.bytes_received),
-                               (long long unsigned)(stats.data_bytes_received));
+                       if (stats.connect_time == -1) {
+                               fprintf(fp, "%s %llu %llu -\n", stats.url.c_str(),
+                                       (long long unsigned)(stats.bytes_received),
+                                       (long long unsigned)(stats.data_bytes_received));
+                       } else {
+                               fprintf(fp, "%s %llu %llu %d\n", stats.url.c_str(),
+                                       (long long unsigned)(stats.bytes_received),
+                                       (long long unsigned)(stats.data_bytes_received),
+                                       int(now - stats.connect_time));
+                       }
                }
                if (fclose(fp) == EOF) {
                        log_perror("fclose");
index d40acc9..99c1e92 100644 (file)
@@ -45,6 +45,7 @@ message InputProto {
        optional int32 sock = 9;
        optional int64 bytes_received = 11;
        optional int64 data_bytes_received = 12;
+       optional int64 connect_time = 13;
 };
 
 // Corresponds to class Acceptor.
index e444bbc..96e0607 100644 (file)
@@ -35,6 +35,7 @@ UDPInput::UDPInput(const string &url)
        stats.url = url;
        stats.bytes_received = 0;
        stats.data_bytes_received = 0;
+       stats.connect_time = time(NULL);
 }
 
 UDPInput::UDPInput(const InputProto &serialized)
@@ -52,6 +53,11 @@ UDPInput::UDPInput(const InputProto &serialized)
        stats.url = url;
        stats.bytes_received = serialized.bytes_received();
        stats.data_bytes_received = serialized.data_bytes_received();
+       if (serialized.has_connect_time()) {
+               stats.connect_time = serialized.connect_time();
+       } else {
+               stats.connect_time = time(NULL);
+       }
 }
 
 InputProto UDPInput::serialize() const
@@ -61,6 +67,7 @@ InputProto UDPInput::serialize() const
        serialized.set_sock(sock);
        serialized.set_bytes_received(stats.bytes_received);
        serialized.set_data_bytes_received(stats.data_bytes_received);
+       serialized.set_connect_time(stats.connect_time);
        return serialized;
 }