Skip to content

Commit

Permalink
src: initialize prev_idle_time_ on ThreadMetrics
Browse files Browse the repository at this point in the history
Make sure `prev_idle_time_` is initialized in the
`ThreadMetrics::ThreadMetrics(SharedEnvInst envinst)` constructor
so the loop utilization calculations are correct, otherwise the default
value 0 is used which may lead to meaningless huge values (greater than
1 for the matter).
No need to initialize this value in the
`ThreadMetrics::ThreadMetrics(uint64_t thread_id)` as this is only used
in the `EnvInst` constructor on which having a value of 0 makes sense.
  • Loading branch information
santigimeno committed Jul 24, 2024
1 parent 849bdd4 commit e1aac60
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/nsolid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ ThreadMetrics::ThreadMetrics(SharedEnvInst envinst)
stor_.thread_id = thread_id_;
stor_.prev_call_time_ = uv_hrtime();
stor_.current_hrtime_ = stor_.prev_call_time_;
stor_.prev_idle_time_ = uv_metrics_idle_time(envinst->event_loop());
}


Expand All @@ -300,6 +301,8 @@ ThreadMetrics::ThreadMetrics(uint64_t thread_id)
stor_.thread_id = thread_id_;
stor_.prev_call_time_ = uv_hrtime();
stor_.current_hrtime_ = stor_.prev_call_time_;
// Letting prev_idle_time_ be 0 is ok as this is only used by the EnvInst
// constructor.
}


Expand Down
1 change: 1 addition & 0 deletions test/addons/nsolid-env-metrics/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Metrics {

static void got_env_metrics(SharedThreadMetrics tm_sp, Metrics* m) {
assert(tm_sp == m->tm);
assert(tm_sp->Get().loop_utilization <= 1);
cb_cntr++;
delete m;
}
Expand Down
6 changes: 5 additions & 1 deletion test/addons/nsolid-env-metrics/nsolid-env-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ for (let i = 0; i < 10; i++) {
}));
}

binding.getMetrics();
// Let then main thread be a bit idle so we can check that loop_utilization is
// correctly calculdate.
setTimeout(() => {
binding.getMetrics();
}, 100);

0 comments on commit e1aac60

Please sign in to comment.