13 #include "serverpool.h"
18 extern ServerPool *servers;
20 StatsThread::StatsThread(const std::string &stats_file, int stats_interval)
21 : stats_file(stats_file),
22 stats_interval(stats_interval)
26 void StatsThread::do_work()
28 while (!should_stop) {
32 vector<ClientStats> client_stats;
34 // Open a new, temporary file.
35 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
36 fd = mkostemp(filename, O_WRONLY);
53 client_stats = servers->get_client_stats();
54 for (size_t i = 0; i < client_stats.size(); ++i) {
55 fprintf(fp, "%s %s %d %llu %llu %llu\n",
56 client_stats[i].remote_addr.c_str(),
57 client_stats[i].stream_id.c_str(),
58 int(now - client_stats[i].connect_time),
59 (long long unsigned)(client_stats[i].bytes_sent),
60 (long long unsigned)(client_stats[i].bytes_lost),
61 (long long unsigned)(client_stats[i].num_loss_events));
63 if (fclose(fp) == EOF) {
70 if (rename(filename, stats_file.c_str()) == -1) {
76 // Wait until the stop_fd pipe is closed, stats_interval timeout,
77 // or a spurious signal. (The latter will cause us to write stats
78 // too often, but that's okay.)
80 pfd.fd = stop_fd_read;
81 pfd.events = POLLIN | POLLRDHUP;
83 int nfds = poll(&pfd, 1, stats_interval * 1000);
84 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {