]> git.sesse.net Git - cubemap/blob - input_stats.cpp
Unbreak header parsing; it was broken by the case-sensitivity fixes.
[cubemap] / input_stats.cpp
1 #include <fcntl.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7 #include <unistd.h>
8 #include <math.h>
9 #include <vector>
10
11 #include "input.h"
12 #include "input_stats.h"
13 #include "log.h"
14 #include "util.h"
15
16 using namespace std;
17
18 InputStatsThread::InputStatsThread(const string &stats_file, int stats_interval, const vector<Input*> &inputs)
19         : stats_file(stats_file),
20           stats_interval(stats_interval),
21           inputs(inputs)
22 {
23 }
24
25 void InputStatsThread::do_work()
26 {
27         while (!should_stop()) {
28                 int fd;
29                 FILE *fp;
30                 time_t now;
31
32                 // Open a new, temporary file.
33                 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
34                 fd = mkostemp(filename, O_WRONLY);
35                 if (fd == -1) {
36                         log_perror(filename);
37                         free(filename);
38                         goto sleep;
39                 }
40
41                 fp = fdopen(fd, "w");
42                 if (fp == nullptr) {
43                         log_perror("fdopen");
44                         safe_close(fd);
45                         if (unlink(filename) == -1) {
46                                 log_perror(filename);
47                         }
48                         free(filename);
49                         goto sleep;
50                 }
51
52                 now = time(nullptr);
53                 for (size_t i = 0; i < inputs.size(); ++i) {
54                         InputStats stats = inputs[i]->get_stats();
55                         fprintf(fp, "%s %llu %llu", stats.url.c_str(),
56                                 (long long unsigned)(stats.bytes_received),
57                                 (long long unsigned)(stats.data_bytes_received));
58                         if (stats.connect_time == -1) {
59                                 fprintf(fp, " -");
60                         } else {
61                                 fprintf(fp, " %d", int(now - stats.connect_time));
62                         }
63                         fprintf(fp, " %llu", (long long unsigned)(stats.metadata_bytes_received));
64                         if (!isfinite(stats.latency_sec)) {
65                                 fprintf(fp, " -");
66                         } else {
67                                 fprintf(fp, " %.6f", stats.latency_sec);
68                         }
69                         fprintf(fp, "\n");
70                 }
71                 if (fclose(fp) == EOF) {
72                         log_perror("fclose");
73                         if (unlink(filename) == -1) {
74                                 log_perror(filename);
75                         }
76                         free(filename);
77                         goto sleep;
78                 }
79                 
80                 if (rename(filename, stats_file.c_str()) == -1) {
81                         log_perror("rename");
82                         if (unlink(filename) == -1) {
83                                 log_perror(filename);
84                         }
85                 }
86                 free(filename);
87
88 sleep:
89                 // Wait until we are asked to quit, stats_interval timeout,
90                 // or a spurious signal. (The latter will cause us to write stats
91                 // too often, but that's okay.)
92                 timespec timeout_ts;
93                 timeout_ts.tv_sec = stats_interval;
94                 timeout_ts.tv_nsec = 0;
95                 wait_for_wakeup(&timeout_ts);
96         }
97 }