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 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) {
61 fprintf(fp, " %d", int(now - stats.connect_time));
63 fprintf(fp, " %llu", (long long unsigned)(stats.metadata_bytes_received));
64 if (!isfinite(stats.latency_sec)) {
67 fprintf(fp, " %.6f", stats.latency_sec);
71 if (fclose(fp) == EOF) {
73 if (unlink(filename) == -1) {
80 if (rename(filename, stats_file.c_str()) == -1) {
82 if (unlink(filename) == -1) {
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.)
93 timeout_ts.tv_sec = stats_interval;
94 timeout_ts.tv_nsec = 0;
95 wait_for_wakeup(&timeout_ts);