e3e89e635d24b392c23d1ac3b02e3564878aad44
[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 <vector>
9
10 #include "client.h"
11 #include "log.h"
12 #include "input.h"
13 #include "input_stats.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 == NULL) {
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(NULL);
53                 for (size_t i = 0; i < inputs.size(); ++i) {
54                         InputStats stats = inputs[i]->get_stats();
55                         if (stats.connect_time == -1) {
56                                 fprintf(fp, "%s %llu %llu -\n", stats.url.c_str(),
57                                         (long long unsigned)(stats.bytes_received),
58                                         (long long unsigned)(stats.data_bytes_received));
59                         } else {
60                                 fprintf(fp, "%s %llu %llu %d\n", stats.url.c_str(),
61                                         (long long unsigned)(stats.bytes_received),
62                                         (long long unsigned)(stats.data_bytes_received),
63                                         int(now - stats.connect_time));
64                         }
65                 }
66                 if (fclose(fp) == EOF) {
67                         log_perror("fclose");
68                         if (unlink(filename) == -1) {
69                                 log_perror(filename);
70                         }
71                         free(filename);
72                         goto sleep;
73                 }
74                 
75                 if (rename(filename, stats_file.c_str()) == -1) {
76                         log_perror("rename");
77                         if (unlink(filename) == -1) {
78                                 log_perror(filename);
79                         }
80                 }
81                 free(filename);
82
83 sleep:
84                 // Wait until we are asked to quit, stats_interval timeout,
85                 // or a spurious signal. (The latter will cause us to write stats
86                 // too often, but that's okay.)
87                 timespec timeout_ts;
88                 timeout_ts.tv_sec = stats_interval;
89                 timeout_ts.tv_nsec = 0;
90                 wait_for_wakeup(&timeout_ts);
91         }
92 }