Skip to content

src: add event loop based metrics into node#62935

Open
pabloerhard wants to merge 9 commits into
nodejs:mainfrom
pabloerhard:pabloerhard/add-event-loop-metrics
Open

src: add event loop based metrics into node#62935
pabloerhard wants to merge 9 commits into
nodejs:mainfrom
pabloerhard:pabloerhard/add-event-loop-metrics

Conversation

@pabloerhard

@pabloerhard pabloerhard commented Apr 24, 2026

Copy link
Copy Markdown

Adds an opt in into monitorEventLoopDelay(), this option adds support for per iteration sampling using libuv hooks, while preserving the existing timer based approach.

When the new samplePerIteration option is set to true, monitorEventLoopDelay() samples directly from event loop iterations instead of using the interval timer. When samplePerIteration is omitted or false, the existing interval based implementation remains unchanged, including resolution handling and the older interval based trace counters.

Motivation
The interval based implementation has two limitations that the new option addresses:

  • Inaccurate measurements. Delay was approximated from a timer dispatch jitter (RecordDelta() between timer ticks) rather than measured from the loop itself. Samples had an implicit floor of resolution ms and could miss delays that didn't happen to coincide with a timer tick.
  • Wasteful wake ups. The timer forced the event loop to iterate every resolution ms just to record a sample, even when the application was otherwise idle and no one was reading the histogram.

This is an alternative implementation to the one in #62934

This PR directly addresses the following issue #56064

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Apr 24, 2026
Comment thread src/histogram.cc Outdated
Comment thread src/histogram.cc Outdated
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from edb66ea to e7093c8 Compare April 28, 2026 20:12
@pabloerhard pabloerhard marked this pull request as ready for review April 29, 2026 18:38
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from 6f5cdca to bc0a59c Compare April 30, 2026 17:55
@codecov

codecov Bot commented Apr 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.51852% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.64%. Comparing base (80e0f14) to head (27ead1e).
⚠️ Report is 109 commits behind head on main.

Files with missing lines Patch % Lines
src/histogram.cc 79.50% 11 Missing and 14 partials ⚠️
src/histogram.h 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62935      +/-   ##
==========================================
- Coverage   89.65%   89.64%   -0.02%     
==========================================
  Files         708      708              
  Lines      220413   220546     +133     
  Branches    42275    42290      +15     
==========================================
+ Hits       197607   197702      +95     
- Misses      14658    14675      +17     
- Partials     8148     8169      +21     
Files with missing lines Coverage Δ
lib/internal/perf/event_loop_delay.js 95.95% <100.00%> (+0.12%) ⬆️
src/node_perf.cc 86.61% <100.00%> (+0.22%) ⬆️
src/histogram.h 50.00% <0.00%> (-10.00%) ⬇️
src/histogram.cc 79.08% <79.50%> (+0.12%) ⬆️

... and 33 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from bc0a59c to 27ead1e Compare April 30, 2026 23:33
@BridgeAR BridgeAR added the request-ci Add this label to start a Jenkins CI on a PR. label May 11, 2026
@pabloerhard pabloerhard requested review from addaleax and bengl May 12, 2026 19:51
add a samplePerIteration option to monitorEventLoopDelay
that records event loop delay from libuv event loop iterations
instead of the existing timer interval sampler. The default remains
the interval based, uses of monitorEventLoopDelay() will still
behave the same unless the samplePerIteration options
is passed through.

Signed-off-by: Pablo Erhard Hernandez <pablo.erhardhernandez@datadoghq.com>
Add test coverage for the per iteration event loop delay histogram start
and stop callbacks

Use disitinct debug tracking keys for the event loop delay histogram
callbacks.
Use the super Close method instead of duplicating code
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from 27ead1e to 3b21617 Compare May 19, 2026 21:21
Comment thread doc/api/perf_hooks.md
Comment thread doc/api/perf_hooks.md
Comment thread src/histogram.cc Outdated
Comment thread test/sequential/test-performance-eventloopdelay.js
Added extra test to add coverage of the overall functionality of the new
ELD histogram.
Modified docs to be on par with the changes and modifications made.
@pabloerhard pabloerhard requested a review from BridgeAR June 11, 2026 09:05

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty much LGTM, just left two nits. I would feel more comfortable with someone else reviewing it as well.

@addaleax @jasnell maybe?

Comment thread doc/api/perf_hooks.md Outdated
Comment thread src/histogram.cc Outdated
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 17, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

The C++ class `ELDHistogram` is the per-iteration sampling
implementation that sits next to `IntervalHistogram` (the
timer-based one). Its name collided with the JS exposed

`ELDHistogram` class that `monitorEventLoopDelay()` returns
regardless of sampling mode, making the layering confusing:
"ELDHistogram" was both the generic JS concept and the name of
one specific C++ backing.
align perf_hooks docs after renaming internal ELDHistogram to
IterationHistogram
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from 71f121f to 88fce8d Compare June 17, 2026 16:56
@pabloerhard pabloerhard requested a review from BridgeAR June 17, 2026 20:18
Added shared templates for duplicated start/stop
logic in IntevalHistogram and IterationHistogram.
@pabloerhard pabloerhard force-pushed the pabloerhard/add-event-loop-metrics branch from 50f8eab to 29b708d Compare June 18, 2026 13:32

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, while I would still like to get another LG who works on CPP frequently

@BridgeAR BridgeAR added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants