ifndef SNI_STATS_H define SNI_STATS_H include time include time_stats

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef SNI_STATS_H
#define SNI_STATS_H
#include <time.h>
#include "time_stats.H"
#include "thread_base.H"
#include "signals.H"
#include "bug_report.H"
#include "debug.H"
#include <sys/syscall.h>
class stats_thread;
extern stats_thread stats;
class stats_thread : public thread_base
{
public:
static void * thread_work(void * var)
{
stats_thread * instance = (stats_thread *) var;
try
{
instance->test_suspend();
int stats_tid = syscall(__NR_gettid);
common::bug_report(common::DEBUG, BRSRC(STATS), BRDSC(THREAD_STARTED, stats_tid));
// it must be a realtime thread
nice(-9);
CATCH_SIGNALS();
instance->set_cpu_affinity_from_hint();
time_t prev_system_time = time(NULL);
int outside_delta = 0;
while(1)
{
stats_time::system_time = time(NULL);
outside_delta = stats_time::system_time - prev_system_time;
if (outside_delta > 5 || outside_delta < 0)
{
//probably the time was changed by another process
stats_time::delta_total += outside_delta;
}
if(stats_time::delta_new)
{
time_t t = stats_time::system_time + stats_time::delta_new;
if(stime(&t) == -1)
{
common::bug_report(common::TROUBLE, BRSRC(STATS), BRDSC(CANT_CORRECT_TIME), strerror(errno));
}
else
{
stats_time::system_time = t;
stats_time::delta_total += stats_time::delta_new;
stats_time::delta_new = 0;
}
}
stats_time::curr_time = stats_time::system_time - stats_time::delta_total;
prev_system_time = stats_time::system_time;
usleep(500000);
}
}
catch( const char * errstr )
{
common::bug_report(common::CRITICAL, BRSRC(STATS), BRDSC(EXCEPTION), errstr );
}
catch (std::bad_alloc &a)
{
common::bug_report(common::CRITICAL, BRSRC(STATS), BRDSC(NOT_ENOUGH_MEMORY), a.what() );
}
shutdown_quietly();
return NULL;
}
public:
stats_thread() : thread_base( thread_work, "stats" )
{
}
void start()
{
thread_init();
thread_wakeup();
}
};
#endif