]> git.sesse.net Git - cubemap/blob - input_stats.cpp
Fix a crash when a HTTP input connected to an UDP output goes unavailable.
[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 | O_CLOEXEC);
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                         for (const char ch : stats.url) {
56                                 if (isspace(ch) || !isprint(ch)) {
57                                         putc('_', fp);
58                                 } else {
59                                         putc(ch, fp);
60                                 }
61                         }
62                         fprintf(fp, " %llu %llu",
63                                 (long long unsigned)(stats.bytes_received),
64                                 (long long unsigned)(stats.data_bytes_received));
65                         if (stats.connect_time == -1) {
66                                 fprintf(fp, " -");
67                         } else {
68                                 fprintf(fp, " %d", int(now - stats.connect_time));
69                         }
70                         fprintf(fp, " %llu", (long long unsigned)(stats.metadata_bytes_received));
71                         if (!isfinite(stats.latency_sec)) {
72                                 fprintf(fp, " -");
73                         } else {
74                                 fprintf(fp, " %.6f", stats.latency_sec);
75                         }
76                         fprintf(fp, "\n");
77                 }
78                 if (fclose(fp) == EOF) {
79                         log_perror("fclose");
80                         if (unlink(filename) == -1) {
81                                 log_perror(filename);
82                         }
83                         free(filename);
84                         goto sleep;
85                 }
86                 
87                 if (rename(filename, stats_file.c_str()) == -1) {
88                         log_perror("rename");
89                         if (unlink(filename) == -1) {
90                                 log_perror(filename);
91                         }
92                 }
93                 free(filename);
94
95 sleep:
96                 // Wait until we are asked to quit, stats_interval timeout,
97                 // or a spurious signal. (The latter will cause us to write stats
98                 // too often, but that's okay.)
99                 timespec timeout_ts;
100                 timeout_ts.tv_sec = stats_interval;
101                 timeout_ts.tv_nsec = 0;
102                 wait_for_wakeup(&timeout_ts);
103         }
104 }