12 #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);
47 if (unlink(filename) == -1) {
55 client_stats = servers->get_client_stats();
56 for (size_t i = 0; i < client_stats.size(); ++i) {
57 fprintf(fp, "%s %d %d %s %d %llu %llu %llu\n",
58 client_stats[i].remote_addr.c_str(),
60 client_stats[i].fwmark,
61 client_stats[i].url.c_str(),
62 int(now - client_stats[i].connect_time),
63 (long long unsigned)(client_stats[i].bytes_sent),
64 (long long unsigned)(client_stats[i].bytes_lost),
65 (long long unsigned)(client_stats[i].num_loss_events));
67 if (fclose(fp) == EOF) {
69 if (unlink(filename) == -1) {
76 if (rename(filename, stats_file.c_str()) == -1) {
78 if (unlink(filename) == -1) {
85 // Wait until we are asked to quit, stats_interval timeout,
86 // or a spurious signal. (The latter will cause us to write stats
87 // too often, but that's okay.)
89 timeout_ts.tv_sec = stats_interval;
90 timeout_ts.tv_nsec = 0;
91 wait_for_wakeup(&timeout_ts);