Add per-address cycle histogram for line-level profiling #5
Merged
Conversation
- Add collect_address_histogram option to ProfileOptions - Track cycles per instruction address in OnExecute() - Add WriteAddressHistogram() to export JSON for disassembly viewer Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- AddressHistogramDisabledByDefault: verify empty when not enabled - AddressHistogramCollected: verify populated when enabled - AddressHistogramSumsToTotal: verify sum equals total cycles - AddressHistogramWithSampling: verify works with sampling (with variance) - ResetClearsAddressHistogram: verify Reset() clears histogram - WriteAddressHistogramJSON: verify JSON output structure - WriteAddressHistogramRecordsSampleRate: verify sample_rate in JSON - WriteAddressHistogramInvalidPath: verify graceful failure - AddressHistogramContainsKnownAddresses: verify expected addresses Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR adds per-address cycle histogram tracking to the profiler for line-level profiling integration with disassembly viewers. It introduces a new collect_address_histogram option that, when enabled, tracks CPU cycles spent at each instruction address and provides JSON export functionality.
Changes:
- Added
collect_address_histogramboolean option toProfileOptionsstruct (defaults to false for backward compatibility) - Implemented cycle tracking per instruction address in
OnExecute()and histogram storage inaddress_cycles_map - Added
GetAddressHistogram()andWriteAddressHistogram()public API methods for retrieving and exporting histogram data in JSON format
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| include/profiler.h | Added ProfileOptions field, new member variables (address_cycles_, collect_address_histogram_), and public API methods for address histogram access and JSON export |
| src/profiler.cpp | Implemented histogram collection in OnExecute(), histogram clearing in Reset(), option initialization in Start(), and JSON export in WriteAddressHistogram() |
| tests/profiler_test.cpp | Added comprehensive test suite (9 tests) covering histogram collection, sampling interaction, reset behavior, JSON output validation, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace hardcoded /tmp paths with std::filesystem::temp_directory_path() for cross-platform compatibility. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Clarify that when sample_rate > 1, address histogram granularity is reduced since only every Nth instruction is recorded. Recommend sample_rate = 1 for accurate line-level profiling. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Use absolute difference to avoid underflow if histogram_total exceeds total_cycles. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
collect_address_histogramoption toProfileOptionsOnExecute()GetAddressHistogram()to retrieve the per-address cycle mapWriteAddressHistogram()to export JSON for disassembly viewer integrationJSON Output Format
{ "sample_rate": 1, "total_cycles": 1007454, "address_count": 42, "addresses": { "00000236": 523000, "0000023a": 412000 } } Test Coverage - AddressHistogramDisabledByDefault: verify empty when not enabled - AddressHistogramCollected: verify populated when enabled - AddressHistogramSumsToTotal: verify sum equals total cycles - AddressHistogramWithSampling: verify works with sampling - ResetClearsAddressHistogram: verify Reset() clears histogram - WriteAddressHistogramJSON: verify JSON output structure - WriteAddressHistogramRecordsSampleRate: verify sample_rate in JSON - WriteAddressHistogramInvalidPath: verify graceful failure - AddressHistogramContainsKnownAddresses: verify expected addresses 🤖 Generated with https://claude.ai/code