8 #include "serverpool.h"
13 extern ServerPool *servers;
15 StatsThread::StatsThread(const std::string &stats_file, int stats_interval)
16 : stats_file(stats_file),
17 stats_interval(stats_interval)
21 void StatsThread::run()
24 pthread_create(&worker_thread, NULL, do_work_thunk, this);
27 void StatsThread::stop()
30 pthread_kill(worker_thread, SIGHUP);
31 if (pthread_join(worker_thread, NULL) == -1) {
32 perror("pthread_join");
37 void *StatsThread::do_work_thunk(void *arg)
39 StatsThread *stats_thread = reinterpret_cast<StatsThread *>(arg);
40 stats_thread->do_work();
44 void StatsThread::do_work()
46 while (!should_stop) {
50 vector<ClientStats> client_stats;
52 // Open a new, temporary file.
53 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
54 fd = mkostemp(filename, O_WRONLY);
71 client_stats = servers->get_client_stats();
72 for (size_t i = 0; i < client_stats.size(); ++i) {
73 fprintf(fp, "%s %s %d %llu\n",
74 client_stats[i].remote_addr.c_str(),
75 client_stats[i].stream_id.c_str(),
76 int(now - client_stats[i].connect_time),
77 (long long unsigned)(client_stats[i].bytes_sent));
79 if (fclose(fp) == EOF) {
86 if (rename(filename, stats_file.c_str()) == -1) {
92 int left_to_sleep = stats_interval;
94 left_to_sleep = sleep(left_to_sleep);
95 } while (left_to_sleep > 0 && !should_stop);