4
4
#include < chrono>
5
5
#include < unistd.h>
6
6
#include < fstream>
7
+ #include < iostream>
8
+
9
+ namespace {
10
+ double GetMemoryUsed ()
11
+ {
12
+ std::ifstream file (" /proc/self/statm" );
13
+ if (!file.is_open ()) {
14
+ return 0 ;
15
+ }
16
+
17
+ long mem_pages = 0 ;
18
+ file >> mem_pages;
19
+ file.close ();
20
+ return mem_pages * (double )getpagesize ();
21
+ }
22
+ }
7
23
8
24
MetricsCollector::MetricsCollector (const char *gateway_address, const char *gateway_port, const char *worker_name)
9
25
: gateway(gateway_address, gateway_port, worker_name),
@@ -31,8 +47,8 @@ MetricsCollector::MetricsCollector(const char *gateway_address, const char *gate
31
47
while (true ) {
32
48
CPUInfo cpu;
33
49
34
- file >> cpu_name >> cpu.last_total_user >> cpu.last_total_user_low
35
- >> cpu.last_total_sys >> cpu.last_total_idle
50
+ file >> cpu_name >> cpu.time . user >> cpu.time . user_low
51
+ >> cpu.time . sys >> cpu.time . idle
36
52
>> ign >> ign >> ign >> ign >> ign >> ign;
37
53
38
54
if (cpu_name.find (" cpu" ) != 0 )
@@ -48,30 +64,30 @@ MetricsCollector::MetricsCollector(const char *gateway_address, const char *gate
48
64
task_processing_time_gauge = &task_processing_time_family.Add ({});
49
65
50
66
gateway.RegisterCollectable (registry);
51
-
52
- is_running = true ;
53
- thread = std::thread (&MetricsCollector::mainLoop, this );
54
-
67
+ thread = std::thread (&MetricsCollector::MainLoop, this );
55
68
is_task_running = false ;
56
69
}
57
70
58
- void MetricsCollector::mainLoop ()
71
+ void MetricsCollector::MainLoop ()
59
72
{
60
73
while (is_running) {
61
74
std::this_thread::sleep_for (std::chrono::seconds (1 ));
62
75
63
- getMemoryUsed ( );
64
- getCPUUsage ();
76
+ memory_used_gauge-> Set (:: GetMemoryUsed () );
77
+ GetCPUUsage ();
65
78
66
79
if (is_task_running) {
67
- struct timespec time;
68
- clock_gettime (CLOCK_MONOTONIC, &time );
69
- task_processing_time_gauge->Set ((time .tv_sec - task_start.tv_sec ) + (time .tv_nsec - task_start.tv_nsec ) * 1e-9 );
80
+ auto cur_time = std::chrono::high_resolution_clock::now ();
81
+ task_processing_time_gauge->Set (std::chrono::duration<double >(cur_time - task_start).count ());
70
82
}
71
- else
83
+ else {
72
84
task_processing_time_gauge->Set (0 );
85
+ }
73
86
74
- gateway.PushAdd ();
87
+ int status = gateway.PushAdd ();
88
+ if (status != 200 ) {
89
+ std::cerr << " [ERROR] Failed to push metrics. Status " << status << std::endl;
90
+ }
75
91
}
76
92
}
77
93
@@ -81,73 +97,53 @@ MetricsCollector::~MetricsCollector()
81
97
thread.join ();
82
98
}
83
99
84
- void MetricsCollector::getMemoryUsed ()
85
- {
86
- std::ifstream file (" /proc/self/statm" );
87
- if (!file.is_open ()) {
88
- memory_used_gauge->Set (0 );
89
- return ;
90
- }
91
-
92
- long mem_pages = 0 ;
93
- file >> mem_pages;
94
- file.close ();
95
-
96
- memory_used_gauge->Set (mem_pages * (double )getpagesize ());
97
- }
98
-
99
- void MetricsCollector::getCPUUsage ()
100
+ void MetricsCollector::GetCPUUsage ()
100
101
{
101
102
std::ifstream file (" /proc/stat" );
102
- uint64_t total_user, total_user_low, total_sys, total_idle, total ;
103
+ CPUInfo::Time cur_time ;
103
104
double percent;
104
105
105
106
std::string cpu_name;
106
107
int ign;
107
108
108
109
while (true ) {
109
- file >> cpu_name >> total_user >> total_user_low >> total_sys >> total_idle
110
+ file >> cpu_name >> cur_time. user >> cur_time. user_low >> cur_time. sys >> cur_time. idle
110
111
>> ign >> ign >> ign >> ign >> ign >> ign;
111
112
112
113
if (cpu_name.find (" cpu" ) != 0 )
113
114
break ;
114
115
115
116
CPUInfo &cpu = cpu_usage[cpu_name];
116
- if (total_user < cpu.last_total_user || total_user_low < cpu.last_total_user_low ||
117
- total_sys < cpu.last_total_sys || total_idle < cpu.last_total_idle ) {
117
+ if (cur_time. user < cpu.time . user || cur_time. user_low < cpu.time . user_low ||
118
+ cur_time. sys < cpu.time . sys || cur_time. idle < cpu.time . idle ) {
118
119
// overflow detection
119
120
percent = -1.0 ;
120
121
}
121
122
else {
122
- total = (total_user - cpu.last_total_user ) + (total_user_low - cpu.last_total_user_low ) +
123
- (total_sys - cpu.last_total_sys );
123
+ uint64_t total = (cur_time. user - cpu.time . user ) + (cur_time. user_low - cpu.time . user_low ) +
124
+ (cur_time. sys - cpu.time . sys );
124
125
125
126
percent = total;
126
- total += (total_idle - cpu.last_total_idle );
127
- percent /= total;
128
- percent *= 100 ;
127
+ total += (cur_time.idle - cpu.time .idle );
128
+ percent = (total == 0 ) ? -1.0 : (percent / total) * 100.0 ;
129
129
}
130
130
131
- cpu.last_total_user = total_user;
132
- cpu.last_total_user_low = total_user_low;
133
- cpu.last_total_sys = total_sys;
134
- cpu.last_total_idle = total_idle;
135
-
131
+ cpu.time = cur_time;
136
132
cpu.gauge ->Set (percent);
137
133
}
138
134
139
135
file.close ();
140
136
}
141
137
142
- void MetricsCollector::startTask ()
138
+ void MetricsCollector::StartTask ()
143
139
{
144
140
is_task_running = true ;
145
- clock_gettime (CLOCK_MONOTONIC, & task_start);
141
+ task_start = std::chrono::high_resolution_clock::now ( );
146
142
}
147
143
148
- void MetricsCollector::stopTask ()
144
+ void MetricsCollector::StopTask ()
149
145
{
150
146
is_task_running = false ;
151
147
task_processing_time_gauge->Set (0 );
152
148
gateway.PushAdd ();
153
- }
149
+ }
0 commit comments