3535#define NODELOOPPLUGIN_DECL
3636#endif
3737
38- #define LOOP_INTERVAL 60000
38+ #define LOOP_INTERVAL 5000 // Same as `eventloop` metric
3939namespace 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;
5454uv_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) {
6465static 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
110112void 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