|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'etc' |
| 4 | +require 'stackprof' |
| 5 | +require 'securerandom' |
| 6 | + |
| 7 | +module Sentry |
| 8 | + class Profiler |
| 9 | + |
| 10 | + VERSION = '1' |
| 11 | + PLATFORM = 'ruby' |
| 12 | + # 101 Hz in microseconds |
| 13 | + DEFAULT_INTERVAL = 1e6 / 101 |
| 14 | + |
| 15 | + def initialize |
| 16 | + @event_id = SecureRandom.uuid.delete('-') |
| 17 | + @started = false |
| 18 | + end |
| 19 | + |
| 20 | + def start |
| 21 | + @started = StackProf.start(interval: DEFAULT_INTERVAL, |
| 22 | + mode: :wall, |
| 23 | + raw: true, |
| 24 | + aggregate: false) |
| 25 | + |
| 26 | + log('Not started since running elsewhere') unless @started |
| 27 | + end |
| 28 | + |
| 29 | + def stop |
| 30 | + StackProf.stop |
| 31 | + end |
| 32 | + |
| 33 | + def to_hash |
| 34 | + return nil unless Sentry.initialized? |
| 35 | + |
| 36 | + results = StackProf.results |
| 37 | + return nil unless results |
| 38 | + return nil if results.empty? |
| 39 | + |
| 40 | + frame_map = {} |
| 41 | + |
| 42 | + frames = results[:frames].to_enum.with_index.map do |frame, idx| |
| 43 | + frame_id, frame_data = frame |
| 44 | + |
| 45 | + # need to map over stackprof frame ids to ours |
| 46 | + frame_map[frame_id] = idx |
| 47 | + |
| 48 | + # TODO-neel module, filename, in_app |
| 49 | + { |
| 50 | + abs_path: frame_data[:file], |
| 51 | + function: frame_data[:name], |
| 52 | + lineno: frame_data[:line] |
| 53 | + }.compact |
| 54 | + end |
| 55 | + |
| 56 | + idx = 0 |
| 57 | + stacks = [] |
| 58 | + num_seen = [] |
| 59 | + |
| 60 | + # extract stacks from raw |
| 61 | + # raw is a single array of [.., len_stack, *stack_frames(len_stack), num_stack_seen , ..] |
| 62 | + while (len = results[:raw][idx]) |
| 63 | + idx += 1 |
| 64 | + |
| 65 | + # our call graph is reversed |
| 66 | + stack = results[:raw].slice(idx, len).map { |id| frame_map[id] }.compact.reverse |
| 67 | + stacks << stack |
| 68 | + |
| 69 | + num_seen << results[:raw][idx + len] |
| 70 | + idx += len + 1 |
| 71 | + |
| 72 | + log('Unknown frame in stack') if stack.size != len |
| 73 | + end |
| 74 | + |
| 75 | + idx = 0 |
| 76 | + elapsed_since_start_ns = 0 |
| 77 | + samples = [] |
| 78 | + |
| 79 | + num_seen.each_with_index do |n, i| |
| 80 | + n.times do |
| 81 | + # stackprof deltas are in microseconds |
| 82 | + delta = results[:raw_timestamp_deltas][idx] |
| 83 | + elapsed_since_start_ns += (delta * 1e3).to_i |
| 84 | + idx += 1 |
| 85 | + |
| 86 | + # Not sure why but some deltas are very small like 0/1 values, |
| 87 | + # they pollute our flamegraph so just ignore them for now. |
| 88 | + # Open issue at https://github.com/tmm1/stackprof/issues/201 |
| 89 | + next if delta < 10 |
| 90 | + |
| 91 | + samples << { |
| 92 | + stack_id: i, |
| 93 | + # TODO-neel we need to patch rb_profile_frames and write our own C extension to enable threading info |
| 94 | + # till then, on multi-threaded servers like puma, we will get frames from other active threads when the one |
| 95 | + # we're profiling is idle/sleeping/waiting for IO etc |
| 96 | + # https://bugs.ruby-lang.org/issues/10602 |
| 97 | + thread_id: '0', |
| 98 | + elapsed_since_start_ns: elapsed_since_start_ns.to_s |
| 99 | + } |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + log('Some samples thrown away') if samples.size != results[:samples] |
| 104 | + |
| 105 | + if samples.size <= 2 |
| 106 | + log('Not enough samples, discarding profiler') |
| 107 | + return nil |
| 108 | + end |
| 109 | + |
| 110 | + profile = { |
| 111 | + frames: frames, |
| 112 | + stacks: stacks, |
| 113 | + samples: samples |
| 114 | + } |
| 115 | + |
| 116 | + { |
| 117 | + event_id: @event_id, |
| 118 | + platform: PLATFORM, |
| 119 | + version: VERSION, |
| 120 | + profile: profile |
| 121 | + } |
| 122 | + end |
| 123 | + |
| 124 | + private |
| 125 | + |
| 126 | + def log(message) |
| 127 | + Sentry.logger.debug(LOGGER_PROGNAME) { "[Profiler] #{message}" } |
| 128 | + end |
| 129 | + |
| 130 | + end |
| 131 | +end |
0 commit comments