Skip to content

Store sampling context in ContinuationPreservedEmbeddedData #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

szegedi
Copy link

@szegedi szegedi commented Dec 11, 2024

What does this PR do?:
Moves profiler's sampling context into the V8 isolate's ContinuationPreservedEmbeddedData (CPED.) In fact, moves it into a private symbol-keyed property within the object as Node.js from version 23 will store AsyncContextFrame objects in the CPED.

Motivation:
Before this change, we rely on async_hooks in dd-trace-js to update the sampling context on every context switch. It is both somewhat costly to do this on every async context switch and we should also migrate away from async hooks, as profiling is their last user, they'll be deprecated in Node, and they bring their own set of performance overhead by using them.

Additional Notes:
In an R&D week experiment, we concluded that this works. It was not clear whether:

  • creating a HandleScope
  • Invoking Global::Get to get a local for a symbol,
  • invoking Isolate::GetContinuationPreservedEmbeddedData, and
  • Invoking Object::Get on it with the symbol
    will work from a signal handler, but they do, provided we guard them with an explicit GC guard.

The code using this in dd-trace-js' wall.js sets the context on AsyncLocalStorage.{enterWith|run}, and the before callback of AsyncHooks.createHook. With CPED storage enabled, we can eliminate the before callback and "only" keep the AsyncLocalStorage.{enterWith|run} callbacks – they are the minimum amount of instrumentation needed for the feature to work correctly then.

This branch can be tested with the same-named branch in dd-trace-js.

Jira: PROF-11771

@szegedi szegedi changed the base branch from main to szegedi/atomic-context December 11, 2024 20:10
@szegedi szegedi force-pushed the szegedi/cped-context branch from 5b796ea to 1b15c3a Compare December 11, 2024 20:48
@szegedi szegedi changed the base branch from szegedi/atomic-context to main December 11, 2024 20:53
@szegedi szegedi force-pushed the szegedi/cped-context branch from 1b15c3a to 3decb46 Compare December 11, 2024 21:04
@szegedi szegedi force-pushed the szegedi/cped-context branch 4 times, most recently from bfa23b5 to 32b2170 Compare December 17, 2024 11:26
@szegedi szegedi force-pushed the szegedi/cped-context branch from 32b2170 to 9f398f2 Compare January 20, 2025 16:43
@szegedi szegedi force-pushed the szegedi/cped-context branch from 9f398f2 to 965a627 Compare February 26, 2025 16:40
@szegedi szegedi changed the base branch from main to szegedi/async-id-2 February 26, 2025 16:41
@szegedi szegedi force-pushed the szegedi/cped-context branch from 965a627 to 0c1791e Compare February 26, 2025 16:51
@szegedi szegedi force-pushed the szegedi/cped-context branch from 0c1791e to 8f88f3d Compare February 27, 2025 10:32
@@ -66,6 +66,7 @@ export interface TimeProfilerOptions {
withContexts?: boolean;
workaroundV8Bug?: boolean;
collectCpuTime?: boolean;
useCPED?: boolean;

Choose a reason for hiding this comment

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

Code Quality Violation

Prop name (useCPED) doesn't match rule (is|has) (...read more)

Enforces a consistent naming pattern for boolean props.

The pattern is: "^(is|has)[A-Z]([A-Za-z0-9]?)+" to enforce is and has prefixes.

View in Datadog  Leave us feedback  Documentation

@szegedi szegedi force-pushed the szegedi/cped-context branch from 8f88f3d to 717f17b Compare March 5, 2025 12:53
@szegedi szegedi force-pushed the szegedi/async-id-2 branch from 3c86526 to 79992a0 Compare March 6, 2025 17:05
Base automatically changed from szegedi/async-id-2 to main March 19, 2025 15:27
@szegedi szegedi force-pushed the szegedi/cped-context branch from 717f17b to 2b9041d Compare March 20, 2025 15:12
Copy link

github-actions bot commented Mar 20, 2025

Overall package size

Self size: 9.57 MB
Deduped: 9.94 MB
No deduping: 9.94 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | source-map | 0.7.4 | 226 kB | 226 kB | | pprof-format | 2.1.0 | 111.69 kB | 111.69 kB | | p-limit | 3.1.0 | 7.75 kB | 13.78 kB | | delay | 5.0.0 | 11.17 kB | 11.17 kB | | node-gyp-build | 3.9.0 | 8.81 kB | 8.81 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@pr-commenter
Copy link

pr-commenter bot commented Mar 20, 2025

Benchmarks

Benchmark execution time: 2025-05-16 14:25:07

Comparing candidate commit cc4fbd7 in PR branch szegedi/cped-context with baseline commit 55ac3fa in branch main.

Found 1 performance improvements and 0 performance regressions! Performance is the same for 90 metrics, 29 unstable metrics.

scenario:profiler-light-load-no-wall-profiler-24

  • 🟩 cpu_user_time [-8.006ms; -1.237ms] or [-10.359%; -1.601%]

@szegedi szegedi changed the title Experiment for storing profiling context in the ContinuationPreservedEmbeddedData Store profiling context in ContinuationPreservedEmbeddedData Mar 20, 2025
@szegedi szegedi changed the title Store profiling context in ContinuationPreservedEmbeddedData Store sampling context in ContinuationPreservedEmbeddedData Mar 20, 2025
@szegedi szegedi added the semver-minor Usually minor non-breaking improvements label Mar 20, 2025
@szegedi szegedi force-pushed the szegedi/cped-context branch from 2b9041d to c475e2f Compare March 20, 2025 16:14
@szegedi szegedi force-pushed the szegedi/cped-context branch 3 times, most recently from 9ba3bf1 to 9c4090e Compare May 7, 2025 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor Usually minor non-breaking improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant