Skip to content

Commit 69ffb65

Browse files
sam-githubsjanuary
authored andcommitted
epoll blocked time should not be counted in loop (#467)
1 parent 87e0a3e commit 69ffb65

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

appmetrics-api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ function API(agent, appmetrics) {
213213
lines.forEach(function(line) {
214214
var values = line.split(/[,]+/);
215215
var loop = {
216-
minimum: parseInt(values[1]),
217-
maximum: parseInt(values[2]),
216+
minimum: parseFloat(values[1]),
217+
maximum: parseFloat(values[2]),
218218
count: parseInt(values[3]),
219-
average: parseInt(values[4]),
219+
average: parseFloat(values[4]),
220220
};
221221
that.emit('loop', loop);
222222
});

bin/appmetrics-cli.js

100644100755
File mode changed.

src/plugins/node/loop/nodeloopplugin.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
#define NODELOOPPLUGIN_DECL
3636
#endif
3737

38-
#define LOOP_INTERVAL 60000
38+
#define LOOP_INTERVAL 5000 // Same as `eventloop` metric
3939
namespace plugin {
4040
agentCoreFunctions api;
4141
uint32 provid = 0;
42-
bool timingOK;
4342
uv_timer_t *timer;
4443
}
4544

@@ -51,11 +50,13 @@ static char* NewCString(const std::string& s) {
5150
return result;
5251
}
5352

53+
uv_prepare_t prepare_handle;
5454
uv_check_t check_handle;
55-
int32_t min = 9999;
56-
int32_t max = 0;
57-
int32_t num = 0;
58-
int32_t sum = 0;
55+
uint64_t tick_start;
56+
uint64_t min = UINT64_MAX;
57+
uint64_t max = 0;
58+
uint64_t num = 0;
59+
uint64_t sum = 0;
5960

6061

6162
#if NODE_VERSION_AT_LEAST(0, 11, 0) // > v0.11+
@@ -64,20 +65,21 @@ static void GetLoopInformation(uv_timer_s *data) {
6465
static void GetLoopInformation(uv_timer_s *data, int status) {
6566
#endif
6667
if (num != 0) {
67-
double mean = 0;
68-
mean = sum / num;
68+
// Convert from nanoseconds to milliseconds.
69+
70+
double mean = (sum / 1e6) / num;
6971

7072
std::stringstream contentss;
7173
contentss << "NodeLoopData";
72-
contentss << "," << min;
73-
contentss << "," << max;
74+
contentss << "," << (min / 1e6);
75+
contentss << "," << (max / 1e6);
7476
contentss << "," << num;
7577
contentss << "," << mean;
7678
contentss << '\n';
7779

7880
std::string content = contentss.str();
7981

80-
min = 9999;
82+
min = UINT64_MAX;
8183
max = 0;
8284
num = 0;
8385
sum = 0;
@@ -108,11 +110,19 @@ pushsource* createPushSource(uint32 srcid, const char* name) {
108110
}
109111

110112
void OnCheck(uv_check_t* handle) {
111-
const uv_loop_t* const loop = handle->loop;
112-
const uint64_t now = uv_hrtime() / static_cast<uint64_t>(1e6);
113+
tick_start = uv_hrtime();
114+
}
115+
116+
void OnPrepare(uv_prepare_t* handle) {
117+
if (!tick_start) return;
113118

114-
const int32_t delta = static_cast<int32_t>(
115-
now <= loop->time ? 0 : (now - loop->time));
119+
const uint64_t tick_end = uv_hrtime();
120+
if (tick_end < tick_start) {
121+
// Should not happen, but ignore, next check will reset
122+
// the start time.
123+
return;
124+
}
125+
const double delta = tick_end - tick_start;
116126

117127
if (delta < min) {
118128
min = delta;
@@ -135,6 +145,8 @@ extern "C" {
135145
}
136146

137147
NODELOOPPLUGIN_DECL int ibmras_monitoring_plugin_init(const char* properties) {
148+
uv_prepare_init(uv_default_loop(), &prepare_handle);
149+
uv_unref(reinterpret_cast<uv_handle_t*>(&prepare_handle));
138150
uv_check_init(uv_default_loop(), &check_handle);
139151
uv_unref(reinterpret_cast<uv_handle_t*>(&check_handle));
140152

@@ -148,7 +160,8 @@ extern "C" {
148160
NODELOOPPLUGIN_DECL int ibmras_monitoring_plugin_start() {
149161
plugin::api.logMessage(fine, "[loop_node] Starting");
150162

151-
uv_check_start(&check_handle, reinterpret_cast<uv_check_cb>(OnCheck));
163+
uv_prepare_start(&prepare_handle, OnPrepare);
164+
uv_check_start(&check_handle, OnCheck);
152165
uv_timer_start(plugin::timer, GetLoopInformation, LOOP_INTERVAL, LOOP_INTERVAL);
153166

154167
return 0;
@@ -158,6 +171,7 @@ extern "C" {
158171
plugin::api.logMessage(fine, "[loop_node] Stopping");
159172

160173
uv_timer_stop(plugin::timer);
174+
uv_prepare_stop(&prepare_handle);
161175
uv_check_stop(&check_handle);
162176

163177
return 0;

0 commit comments

Comments
 (0)