Class: Datadog::Profiling::Pprof::Builder
- Inherits:
-
Object
- Object
- Datadog::Profiling::Pprof::Builder
- Defined in:
- lib/datadog/profiling/pprof/builder.rb
Overview
Accumulates profile data and produces a Perftools::Profiles::Profile
Constant Summary collapse
- DEFAULT_ENCODING =
'UTF-8'- DESC_FRAME_OMITTED =
'frame omitted'- DESC_FRAMES_OMITTED =
'frames omitted'
Instance Attribute Summary collapse
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#locations ⇒ Object
readonly
Returns the value of attribute locations.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#sample_types ⇒ Object
readonly
Returns the value of attribute sample_types.
-
#samples ⇒ Object
readonly
Returns the value of attribute samples.
-
#string_table ⇒ Object
readonly
Returns the value of attribute string_table.
Instance Method Summary collapse
- #build_function(id, filename, function_name) ⇒ Object
- #build_line(function_id, line_number) ⇒ Object
- #build_location(id, backtrace_location) ⇒ Object
- #build_locations(backtrace_locations, length) ⇒ Object
- #build_mapping(id, filename) ⇒ Object
- #build_profile(start:, finish:) ⇒ Object
- #build_value_type(type, unit) ⇒ Object
- #encode_profile(profile) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#initialize_locations_hash ⇒ Object
The locations hash maps unique BacktraceLocation instances to their corresponding pprof Location objects; there’s a 1:1 correspondence, since BacktraceLocations were already deduped.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 25 def initialize @functions = MessageSet.new(1) @locations = initialize_locations_hash @mappings = MessageSet.new(1) @sample_types = MessageSet.new @samples = [] @string_table = StringTable.new # Cache this proc, since it's pretty expensive to keep recreating it @build_function = method(:build_function).to_proc end |
Instance Attribute Details
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def functions @functions end |
#locations ⇒ Object (readonly)
Returns the value of attribute locations.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def locations @locations end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def mappings @mappings end |
#sample_types ⇒ Object (readonly)
Returns the value of attribute sample_types.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def sample_types @sample_types end |
#samples ⇒ Object (readonly)
Returns the value of attribute samples.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def samples @samples end |
#string_table ⇒ Object (readonly)
Returns the value of attribute string_table.
17 18 19 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 17 def string_table @string_table end |
Instance Method Details
#build_function(id, filename, function_name) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 108 def build_function(id, filename, function_name) Perftools::Profiles::Function.new( id: id, name: @string_table.fetch(function_name), filename: @string_table.fetch(filename) ) end |
#build_line(function_id, line_number) ⇒ Object
101 102 103 104 105 106 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 101 def build_line(function_id, line_number) Perftools::Profiles::Line.new( function_id: function_id, line: line_number ) end |
#build_location(id, backtrace_location) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 87 def build_location(id, backtrace_location) Perftools::Profiles::Location.new( id: id, line: [build_line( @functions.fetch( backtrace_location.path, backtrace_location.base_label, &@build_function ).id, backtrace_location.lineno )] ) end |
#build_locations(backtrace_locations, length) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 73 def build_locations(backtrace_locations, length) locations = backtrace_locations.collect { |backtrace_location| @locations[backtrace_location] } omitted = length - backtrace_locations.length # Add placeholder stack frame if frames were truncated if omitted > 0 desc = omitted == 1 ? DESC_FRAME_OMITTED : DESC_FRAMES_OMITTED locations << @locations[Profiling::BacktraceLocation.new('', 0, "#{omitted} #{desc}")] end locations end |
#build_mapping(id, filename) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 116 def build_mapping(id, filename) Perftools::Profiles::Mapping.new( id: id, filename: @string_table.fetch(filename) ) end |
#build_profile(start:, finish:) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 50 def build_profile(start:, finish:) start_ns = Core::Utils::Time.as_utc_epoch_ns(start) finish_ns = Core::Utils::Time.as_utc_epoch_ns(finish) Perftools::Profiles::Profile.new( sample_type: @sample_types., sample: @samples, mapping: @mappings., location: @locations.values, function: @functions., string_table: @string_table.strings, time_nanos: start_ns, duration_nanos: finish_ns - start_ns, ) end |
#build_value_type(type, unit) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 66 def build_value_type(type, unit) Perftools::Profiles::ValueType.new( type: @string_table.fetch(type), unit: @string_table.fetch(unit) ) end |
#encode_profile(profile) ⇒ Object
46 47 48 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 46 def encode_profile(profile) Perftools::Profiles::Profile.encode(profile).force_encoding(DEFAULT_ENCODING) end |
#initialize_locations_hash ⇒ Object
The locations hash maps unique BacktraceLocation instances to their corresponding pprof Location objects; there’s a 1:1 correspondence, since BacktraceLocations were already deduped
39 40 41 42 43 44 |
# File 'lib/datadog/profiling/pprof/builder.rb', line 39 def initialize_locations_hash sequence = Core::Utils::Sequence.new(1) Hash.new do |locations_hash, backtrace_location| locations_hash[backtrace_location] = build_location(sequence.next, backtrace_location) end end |