Skip to content

Improved profiler (support for "Cpuprofile" and "Folded" formats) - #2956

Open
sungshik wants to merge 8 commits into
mainfrom
improved-profiler
Open

sungshik wants to merge 8 commits into
mainfrom
improved-profiler

Conversation

@sungshik

@sungshik sungshik commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds support to the profiler to write profiles in formats that can be visualized as flame charts/graphs by other tools. Two formats are currently supported:

  • The cpuprofile format originates from the Chrome Dev Tools (CDT) protocol. Files written in this format can be opened, for instance, in VS Code using the "Flame Chart Visualizer for JavaScript Profiles" extension, or in Chrome using the built-in developer tools. Example:
    profile
    Note: The video demonstrates the flame chart for a typecheck of bird-core. I cut a few frames in the middle of the video, though, to skip 45 seconds of typechecking time.

  • The folded format originates from Brendan Gregg's original flamegraph.pl script. Files written in this format can be post-processed by that script to SVG. Example:
    profile-latest

Anecdotally, the profiler adds 10% runtime overhead.

Design/implementation notes

  • Intuitively, each profiling run consists of two phases: (a) while the evaluator is running, the sampler produces a stream of samples, each of which contains the call stack and a timestamp; (b) after the evaluator has run, the formatter consumes the stream of samples and generates the formatted output.

  • Following these two phases, new package org.rascalmpl.interpreter.utils.profiler consists of new interfaces Sampler and Formatter, as well as their implementations. Through these interfaces, sampling and formatting are decoupled as much as possible.

  • To keep memory pressure low, samples are temporarily written to a file in the sampling phase, and lazily read from that file in the formatting phase, so samples don't need to be kept in memory all at the same time.

  • IO with the temporary file, as well as de/serialization (using base64-encoded IValues to avoid the need to pretty-print/parse ISourceLocations), are done in a separate auxiliary thread to not block the main thread.

  • Following the existing naming conventions, there are two new configuration properties:

    • rascal.profileFmt controls the format of the profile. Possible values: "none", "folded", and "cpuprofile" (default).
    • rascal.profileLoc controls the directory to which formatted profiles can be written. Possible values: any location (default: |cwd:///|).

    Like other configuration properties, the new ones can be set when the REPL process is started (e.g., '-Drascal.profileFmt="folded"' and -Drascal.profileDir=|home:///Desktop|) or when the REPL process is running (e.g., :set profileFmt "folded" and :set profileDir |home:///Desktop|).

  • To support strings and locations as arguments of -Drascal.xyz=... and :set, the existing code for configuration properties was adapted a bit (minor changes).

TODO

  • Add license headers
  • Update documentation on the website

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.27824% with 97 lines in your changes missing coverage. Please review.
✅ Project coverage is 46%. Comparing base (e2aeef0) to head (c0b73ae).

Files with missing lines Patch % Lines
src/org/rascalmpl/interpreter/Configuration.java 38% 22 Missing and 5 partials ⚠️
src/org/rascalmpl/interpreter/utils/Profiler.java 60% 14 Missing and 4 partials ⚠️
.../org/rascalmpl/semantics/dynamic/ShellCommand.java 30% 15 Missing and 1 partial ⚠️
...nterpreter/utils/profiler/CpuprofileFormatter.java 88% 7 Missing and 6 partials ⚠️
...pl/interpreter/utils/profiler/TempFileSampler.java 83% 8 Missing and 3 partials ⚠️
.../rascalmpl/interpreter/utils/profiler/Sampler.java 83% 2 Missing and 5 partials ⚠️
...mpl/interpreter/utils/profiler/DummyFormatter.java 50% 2 Missing ⚠️
...ascalmpl/interpreter/utils/profiler/Formatter.java 66% 2 Missing ⚠️
...pl/interpreter/utils/profiler/FoldedFormatter.java 93% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##              main   #2956    +/-   ##
========================================
  Coverage       45%     46%            
- Complexity    6815    6887    +72     
========================================
  Files          844     851     +7     
  Lines        68826   69174   +348     
  Branches     10040   10075    +35     
========================================
+ Hits         31434   31992   +558     
+ Misses       34995   34753   -242     
- Partials      2397    2429    +32     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

@sungshik
sungshik marked this pull request as ready for review October 29, 2026 11:50
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant