Class: Pf2::Reporter::FirefoxProfiler::ThreadReport
- Inherits:
-
Object
- Object
- Pf2::Reporter::FirefoxProfiler::ThreadReport
- Defined in:
- lib/pf2/reporter/firefox_profiler.rb
Instance Method Summary collapse
- #array_of_paths_to_tree(paths) ⇒ Object
- #build_frame_table ⇒ Object
- #build_func_table ⇒ Object
- #build_samples ⇒ Object
- #build_stack_table(func_table, frame_table) ⇒ Object
- #build_string_table ⇒ Object
- #emit ⇒ Object
-
#initialize(thread) ⇒ ThreadReport
constructor
A new instance of ThreadReport.
- #inspect ⇒ Object
- #markers ⇒ Object
- #string_id(str) ⇒ Object
- #tree_to_array_of_paths(stack_tree, frames, path, collected_paths) ⇒ Object
-
#weave_native_stack(stack_tree) ⇒ Object
“Weave” the native stack into the Ruby stack.
Constructor Details
#initialize(thread) ⇒ ThreadReport
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 61 def initialize(thread) @thread = thread # Populated in other methods @func_id_map = {} @frame_id_map = {} @stack_tree_id_map = {} @string_table = {} end |
Instance Method Details
#array_of_paths_to_tree(paths) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 288 def array_of_paths_to_tree(paths) new_stack_tree = { children: {}, node_id: 0, frame_id: 0 } paths.each do |path| current = new_stack_tree path[1..].each do |frame| frame_id = frame[:frame_id] node_id = frame[:node_id] current[:children][frame_id] ||= { children: {}, node_id: node_id, frame_id: frame_id } current = current[:children][frame_id] end end new_stack_tree end |
#build_frame_table ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 139 def build_frame_table ret = { address: [], category: [], subcategory: [], func: [], inner_window_id: [], implementation: [], line: [], column: [], optimizations: [], inline_depth: [], native_symbol: [], } @thread[:frames].each.with_index do |(id, frame), i| ret[:address] << frame[:address].to_s ret[:category] << 1 ret[:subcategory] << 1 ret[:func] << i # TODO ret[:inner_window_id] << nil ret[:implementation] << nil ret[:line] << frame[:callsite_lineno] ret[:column] << nil ret[:optimizations] << nil ret[:inline_depth] << 0 ret[:native_symbol] << nil @frame_id_map[id] = i end ret[:length] = ret[:address].length ret end |
#build_func_table ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 174 def build_func_table ret = { name: [], is_js: [], relevant_for_js: [], resource: [], file_name: [], line_number: [], column_number: [], } @thread[:frames].each.with_index do |(id, frame), i| native = (frame[:entry_type] == 'Native') label = "#{native ? "Native: " : ""}#{frame[:full_label]}" ret[:name] << string_id(label) ret[:is_js] << !native ret[:relevant_for_js] << false ret[:resource] << -1 ret[:file_name] << string_id(frame[:file_name]) ret[:line_number] << frame[:function_first_lineno] ret[:column_number] << nil @func_id_map[id] = i end ret[:length] = ret[:name].length ret end |
#build_samples ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 118 def build_samples ret = { event_delay: [], stack: [], time: [], duration: [], # weight: nil, # weight_type: 'samples', } @thread[:samples].each do |sample| ret[:stack] << @stack_tree_id_map[sample[:stack_tree_id]] ret[:time] << sample[:elapsed_ns] / 1000000 # ns -> ms ret[:duration] << 1 ret[:event_delay] << 0 end ret[:length] = ret[:stack].length ret end |
#build_stack_table(func_table, frame_table) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 302 def build_stack_table(func_table, frame_table) ret = { frame: [], category: [], subcategory: [], prefix: [], } queue = [] @thread[:stack_tree][:children].each {|_, c| queue << [nil, c] } loop do break if queue.size == 0 prefix, node = queue.shift ret[:frame] << @frame_id_map[node[:frame_id]] ret[:category] << (build_string_table[func_table[:name][frame_table[:func][@frame_id_map[node[:frame_id]]]]].start_with?('Native:') ? 2 : 1) ret[:subcategory] << nil ret[:prefix] << prefix # The index of this frame - children can refer to this frame using this index as prefix frame_index = ret[:frame].length - 1 @stack_tree_id_map[node[:node_id]] = frame_index # Enqueue children nodes node[:children].each {|_, c| queue << [frame_index, c] } end ret[:length] = ret[:frame].length ret end |
#build_string_table ⇒ Object
335 336 337 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 335 def build_string_table @string_table.sort_by {|_, v| v}.map {|s| s[0] } end |
#emit ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 76 def emit x = weave_native_stack(@thread[:stack_tree]) @thread[:stack_tree] = x func_table = build_func_table frame_table = build_frame_table stack_table = build_stack_table(func_table, frame_table) samples = build_samples string_table = build_string_table { process_type: 'default', process_name: 'ruby', process_startup_time: 0, process_shutdown_time: nil, register_time: 0, unregister_time: nil, paused_ranges: [], name: "Thread (tid: #{@thread[:thread_id]})", is_main_thread: true, is_js_tracer: true, # FIXME: We can fill the correct PID only after we correctly fill is_main_thread # (only one thread could be marked as is_main_thread in a single process) pid: @thread[:thread_id], tid: @thread[:thread_id], samples: samples, markers: markers, stack_table: stack_table, frame_table: frame_table, string_array: build_string_table, func_table: func_table, resource_table: { lib: [], name: [], host: [], type: [], length: 0, }, native_symbols: [], } end |
#inspect ⇒ Object
72 73 74 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 72 def inspect "" # TODO: provide something better end |
#markers ⇒ Object
345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 345 def markers { data: [], name: [], time: [], start_time: [], end_time: [], phase: [], category: [], length: 0 } end |
#string_id(str) ⇒ Object
339 340 341 342 343 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 339 def string_id(str) return @string_table[str] if @string_table.has_key?(str) @string_table[str] = @string_table.length @string_table[str] end |
#tree_to_array_of_paths(stack_tree, frames, path, collected_paths) ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 277 def tree_to_array_of_paths(stack_tree, frames, path, collected_paths) new_path = path + [{ frame_id: stack_tree[:frame_id], node_id: stack_tree[:node_id] }] if stack_tree[:children].empty? collected_paths << new_path else stack_tree[:children].each do |frame_id, child| tree_to_array_of_paths(child, frames, new_path, collected_paths) end end end |
#weave_native_stack(stack_tree) ⇒ Object
“Weave” the native stack into the Ruby stack.
Strategy:
-
Split the stack into Ruby and Native parts
-
Start from the root of the Native stack
-
Dig in to the native stack until we hit a rb_vm_exec(), which marks a call into Ruby code
-
Switch to Ruby stack. Keep digging until we hit a Cfunc call, then switch back to Native stack
-
Repeat until we consume the entire stack
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/pf2/reporter/firefox_profiler.rb', line 211 def weave_native_stack(stack_tree) collected_paths = [] tree_to_array_of_paths(stack_tree, @thread[:frames], [], collected_paths) collected_paths = collected_paths.map do |path| next if path.size == 0 new_path = [] new_path << path.shift # root # Split the stack into Ruby and Native parts native_path, ruby_path = path.partition do |frame| frame_id = frame[:frame_id] @thread[:frames][frame_id][:entry_type] == 'Native' end mode = :native loop do break if ruby_path.size == 0 && native_path.size == 0 case mode when :ruby if ruby_path.size == 0 mode = :native next end next_node = ruby_path[0] new_path << ruby_path.shift next_node_frame = @thread[:frames][next_node[:frame_id]] if native_path.size > 0 # Search the remainder of the native stack for the same address # Note: This isn't a very efficient way for the job... but it still works ruby_addr = next_node_frame[:address] native_path[0..].each do |native_node| native_addr = @thread[:frames][native_node[:frame_id]][:address] if ruby_addr && native_addr && ruby_addr == native_addr # A match has been found. Switch to native mode mode = :native break end end end when :native if native_path.size == 0 mode = :ruby next end # Dig until we meet a rb_vm_exec next_node = native_path[0] new_path << native_path.shift if @thread[:frames][next_node[:frame_id]][:full_label] =~ /vm_exec_core/ # VM_EXEC in vm_exec.h mode = :ruby end end end new_path end # reconstruct stack_tree new_stack_tree = array_of_paths_to_tree(collected_paths) new_stack_tree end |