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