12 #include "input_stats.h"
18 InputStatsThread::InputStatsThread(const string &stats_file, int stats_interval, const vector<Input*> &inputs)
19 : stats_file(stats_file),
20 stats_interval(stats_interval),
25 void InputStatsThread::do_work()
27 while (!should_stop()) {
32 // Open a new, temporary file.
33 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
34 fd = mkostemp(filename, O_WRONLY | O_CLOEXEC);
45 if (unlink(filename) == -1) {
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)) {
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) {
68 fprintf(fp, " %d", int(now - stats.connect_time));
70 fprintf(fp, " %llu", (long long unsigned)(stats.metadata_bytes_received));
71 if (!isfinite(stats.latency_sec)) {
74 fprintf(fp, " %.6f", stats.latency_sec);
78 if (fclose(fp) == EOF) {
80 if (unlink(filename) == -1) {
87 if (rename(filename, stats_file.c_str()) == -1) {
89 if (unlink(filename) == -1) {
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.)
100 timeout_ts.tv_sec = stats_interval;
101 timeout_ts.tv_nsec = 0;
102 wait_for_wakeup(&timeout_ts);