Skip to content

Add per-address cycle histogram for line-level profiling #5

Merged
olaugh merged 6 commits intomainfrom
address-profiler
Jan 21, 2026
Merged

Add per-address cycle histogram for line-level profiling #5
olaugh merged 6 commits intomainfrom
address-profiler

Conversation

@olaugh
Copy link
Owner

@olaugh olaugh commented Jan 21, 2026

Summary

  • Add collect_address_histogram option to ProfileOptions
  • Track CPU cycles per instruction address in OnExecute()
  • Add GetAddressHistogram() to retrieve the per-address cycle map
  • Add WriteAddressHistogram() to export JSON for disassembly viewer integration

JSON 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                                                                                              

olaugh and others added 2 commits January 20, 2026 17:09
- 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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_histogram boolean option to ProfileOptions struct (defaults to false for backward compatibility)
  • Implemented cycle tracking per instruction address in OnExecute() and histogram storage in address_cycles_ map
  • Added GetAddressHistogram() and WriteAddressHistogram() 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.

olaugh and others added 3 commits January 20, 2026 18:35
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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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>
@olaugh olaugh merged commit 714a866 into main Jan 21, 2026
4 checks passed
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.

2 participants