Class: Vernier::Output::Firefox::Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/vernier/output/firefox.rb

Constant Summary collapse

SAMPLE_CATEGORY_NAMES =
{
  1 => "Idle",
  2 => "Stalled"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_thread_id, profile, categorizer, name:, tid:, samples:, weights:, timestamps: nil, sample_categories: nil, markers:, started_at:, stopped_at: nil, allocations: nil, is_main: nil, is_start: nil) ⇒ Thread

Returns a new instance of Thread.



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/vernier/output/firefox.rb', line 277

def initialize(ruby_thread_id, profile, categorizer, name:, tid:, samples:, weights:, timestamps: nil, sample_categories: nil, markers:, started_at:, stopped_at: nil, allocations: nil, is_main: nil, is_start: nil)
  @ruby_thread_id = ruby_thread_id
  @profile = profile
  @categorizer = categorizer
  @tid = tid
  @name = name
  @is_main = is_main
  if is_main.nil?
    @is_main = @ruby_thread_id == ::Thread.main.object_id
  end
  @is_main = true if profile.threads.size == 1
  @is_start = is_start.nil? ? @is_main : is_start

  @stack_table = Vernier::StackTable.new
  samples = samples.map { |sample| @stack_table.convert(profile._stack_table, sample) }

  @samples = samples

  if allocations
    allocation_samples = allocations[:samples].dup
    allocation_samples.map! do |sample|
      @stack_table.convert(profile._stack_table, sample)
    end
    allocations = allocations.merge(samples: allocation_samples)
  end
  @allocations = allocations

  timestamps ||= [0] * samples.size
  @weights, @timestamps = weights, timestamps
  @sample_categories = sample_categories || ([0] * samples.size)
  @markers = markers.map do |marker|
    if stack_idx = marker[5]&.dig(:cause, :stack)
      marker = marker.dup
      new_idx = @stack_table.convert(profile._stack_table, stack_idx)
      marker[5] = marker[5].merge({ cause: { stack: new_idx }})
    end
    marker
  end

  @started_at, @stopped_at = started_at, stopped_at

  @stack_table_hash = @stack_table.to_h
  names = @stack_table_hash[:func_table].fetch(:name)
  filenames = @stack_table_hash[:func_table].fetch(:filename)

  stacks_size = @stack_table.stack_count
  @categorized_stacks = Hash.new do |h, k|
    h[k] = h.size + stacks_size
  end

  @strings = Hash.new { |h, k| h[k] = h.size }
  @func_names = names.map do |name|
    @strings[name]
  end

  @filenames = filter_filenames(filenames).map do |filename|
    @strings[filename]
  end

  func_implementations = filenames.map do |filename|
    # Must match strings in `src/profile-logic/profile-data.js`
    # inside the firefox profiler. See `getFriendlyStackTypeName`
    if filename == "<cfunc>"
      @strings["native"]
    else
      # nil means interpreter
      nil
    end
  end
  @frame_implementations = @stack_table_hash[:frame_table].fetch(:func).map do |func_idx|
    func_implementations[func_idx]
  end

  func_categories, func_subcategories = [], []
  filenames.each do |filename|
    category, subcategory = categorize_filename(filename)
    func_categories << category
    func_subcategories << subcategory
  end

  @frame_categories = @stack_table_hash[:frame_table].fetch(:func).map do |func_idx|
    func_categories[func_idx]
  end
  @frame_subcategories = @stack_table_hash[:frame_table].fetch(:func).map do |func_idx|
    func_subcategories[func_idx]
  end

  @sample_category_idx = SAMPLE_CATEGORY_NAMES.transform_values do |name|
    @categorizer.get_category(name).idx
  end

  @samples.zip(@sample_categories).each do |sample, raw_category|
    next if raw_category == 0

    @categorized_stacks[[sample, raw_category]]
  end

  base_stack_frames = @stack_table_hash[:stack_table].fetch(:frame)
  base_frame_count = @stack_table_hash[:frame_table].fetch(:func).size
  @extra_frames = []
  @categorized_frame_map = {}

  @categorized_stacks.each_key do |(stack, raw_category)|
    original_frame_idx = base_stack_frames[stack]
    key = [original_frame_idx, raw_category]
    next if @categorized_frame_map.key?(key)

    new_frame_idx = base_frame_count + @extra_frames.size
    @categorized_frame_map[key] = new_frame_idx
    @extra_frames << [original_frame_idx, @sample_category_idx[raw_category]]
  end
end

Instance Attribute Details

#is_startObject (readonly)

Returns the value of attribute is_start.



275
276
277
# File 'lib/vernier/output/firefox.rb', line 275

def is_start
  @is_start
end

#profileObject (readonly)

Returns the value of attribute profile.



275
276
277
# File 'lib/vernier/output/firefox.rb', line 275

def profile
  @profile
end

Instance Method Details

#allocations_tableObject



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/vernier/output/firefox.rb', line 495

def allocations_table
  return nil unless @allocations

  samples, weights, timestamps = @allocations.values_at(:samples, :weights, :timestamps)
  return nil if samples.empty?

  size = samples.size
  timestamps = timestamps.map { _1 / 1_000_000.0 }
  {
    "time": timestamps,
    "className": ["Object"]*size,
    "typeName": ["JSObject"]*size,
    "coarseType": ["Object"]*size,
    "weight": weights,
    "inNursery": [false] * size,
    "stack": samples,
    "length": size
  }
end

#categorize_filename(filename) ⇒ Object



390
391
392
393
394
395
396
397
# File 'lib/vernier/output/firefox.rb', line 390

def categorize_filename(filename)
  return cfunc_category_and_subcategory if filename == "<cfunc>"

  category, subcategory = find_category_and_subcategory(filename, Categorizer::ORDERED_CATEGORIES)
  return category, subcategory if subcategory

  ruby_category_and_subcategory
end

#cfunc_category_and_subcategoryObject



399
400
401
# File 'lib/vernier/output/firefox.rb', line 399

def cfunc_category_and_subcategory
  [@categorizer.get_category("cfunc"), 0]
end

#dataObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/vernier/output/firefox.rb', line 423

def data
  started_at = (@started_at - 0) / 1_000_000.0
  stopped_at = (@stopped_at - 0) / 1_000_000.0 if @stopped_at

  {
    name: @name,
    isMainThread: @is_main,
    processStartupTime: started_at,
    processShutdownTime: stopped_at,
    registerTime: started_at,
    unregisterTime: stopped_at,
    pausedRanges: [],
    pid: profile.pid || Process.pid,
    tid: @tid,
    frameTable: frame_table,
    funcTable: func_table,
    nativeSymbols: {},
    samples: samples_table,
    jsAllocations: allocations_table,
    stackTable: stack_table,
    resourceTable: {
      length: 0,
      lib: [],
      name: [],
      host: [],
      type: []
    },
    markers: markers_table,
    stringArray: string_table
  }.compact
end

#filter_filenames(filenames) ⇒ Object



416
417
418
419
420
421
# File 'lib/vernier/output/firefox.rb', line 416

def filter_filenames(filenames)
  filter = FilenameFilter.new
  filenames.map do |filename|
    filter.call(filename)
  end
end

#find_category_and_subcategory(filename, categories) ⇒ Object



407
408
409
410
411
412
413
414
# File 'lib/vernier/output/firefox.rb', line 407

def find_category_and_subcategory(filename, categories)
  categories.each do |category_name|
    category = @categorizer.get_category(category_name)
    subcategory = category.subcategories.detect { |c| c.matches?(filename) }&.idx
    return category, subcategory if subcategory
  end
  [nil, nil]
end

#frame_tableObject



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/vernier/output/firefox.rb', line 577

def frame_table
  funcs = @stack_table_hash[:frame_table].fetch(:func).dup
  lines = @stack_table_hash[:frame_table].fetch(:line).dup
  raise unless lines.size == funcs.size

  categories = @frame_categories.map(&:idx)
  subcategories = @frame_subcategories.dup
  implementations = @frame_implementations.dup

  @extra_frames.each do |(frame_idx, category_idx)|
    funcs << funcs[frame_idx]
    lines << lines[frame_idx]
    categories << category_idx
    subcategories << 0
    implementations << implementations[frame_idx]
  end

  size = funcs.size
  none = [nil] * size
  default = [0] * size
  unidentified = [-1] * size

  {
    address: unidentified,
    inlineDepth: default,
    category: categories,
    subcategory: subcategories,
    func: funcs,
    nativeSymbol: none,
    innerWindowID: none,
    implementation: implementations,
    line: lines,
    column: none,
    length: size
  }
end

#func_tableObject



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/vernier/output/firefox.rb', line 614

def func_table
  size = @func_names.size

  cfunc_idx = @strings["<cfunc>"]
  is_js = @filenames.map { |fn| fn != cfunc_idx }
  line_numbers = @stack_table_hash[:func_table].fetch(:first_line).map.with_index do |line, i|
    if is_js[i] || line != 0
      line
    else
      nil
    end
  end
  {
    name: @func_names,
    isJS: is_js,
    relevantForJS: is_js,
    resource: [-1] * size, # set to unidentified for now
    fileName: @filenames,
    lineNumber: line_numbers,
    columnNumber: [nil] * size,
    #columnNumber: functions.map { _1.column },
    length: size
  }
end

#markers_tableObject



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/vernier/output/firefox.rb', line 455

def markers_table
  string_indexes = []
  start_times = []
  end_times = []
  phases = []
  categories = []
  data = []

  @markers.each do |(_, name, start, finish, phase, datum)|
    string_indexes << @strings[name]
    start_times << (start / 1_000_000.0)

    # Please don't hate me. Divide by 1,000,000 only if finish is not nil
    end_times << (finish&./(1_000_000.0))
    phases << phase

    category =
      if name.start_with?("GC")
        gc_category.idx
      elsif name.start_with?("Thread")
        thread_category.idx
      else
        0
      end

    categories << category
    data << datum
  end

  {
    data: data,
    name: string_indexes,
    startTime: start_times,
    endTime: end_times,
    phase: phases,
    category: categories,
    length: start_times.size
  }
end

#ruby_category_and_subcategoryObject



403
404
405
# File 'lib/vernier/output/firefox.rb', line 403

def ruby_category_and_subcategory
  [@categorizer.get_category("Ruby"), 0]
end

#samples_tableObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/vernier/output/firefox.rb', line 515

def samples_table
  samples = @samples
  weights = @weights
  categories = @sample_categories
  size = samples.size
  if categories.empty?
    categories = [0] * size
  end

  if @timestamps
    times = @timestamps.map { _1 / 1_000_000.0 }
  else
    # FIXME: record timestamps for memory samples
    times = (0...size).to_a
  end

  raise unless weights.size == size
  raise unless times.size == size

  samples = samples.zip(categories).map do |sample, category|
    if category == 0
      sample
    else
      @categorized_stacks[[sample, category]]
    end
  end

  {
    stack: samples,
    time: times,
    weight: weights,
    weightType: profile.meta[:mode] == :retained ? "bytes" : "samples",
    length: samples.length
  }
end

#stack_tableObject



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/vernier/output/firefox.rb', line 551

def stack_table
  base_frames = @stack_table_hash[:stack_table].fetch(:frame)
  frames = base_frames.dup
  prefixes = @stack_table_hash[:stack_table].fetch(:parent).dup
  categories = frames.map { |idx| @frame_categories[idx].idx }
  subcategories = frames.map { |idx| @frame_subcategories[idx] }

  @categorized_stacks.each_key do |(stack, raw_category)|
    original_frame_idx = base_frames[stack]
    frames << @categorized_frame_map[[original_frame_idx, raw_category]]
    prefixes << prefixes[stack]
    categories << @sample_category_idx[raw_category]
    subcategories << 0
  end

  raise unless prefixes.size == frames.size

  {
    frame: frames,
    category: categories,
    subcategory: subcategories,
    prefix: prefixes,
    length: prefixes.length
  }
end

#string_tableObject



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/vernier/output/firefox.rb', line 639

def string_table
  @strings.keys.map do |string|
    if string.ascii_only?
      string
    elsif string.encoding == Encoding::UTF_8
      if string.valid_encoding?
        string
      else
        string.scrub
      end
    else
      # TODO: We might want to guess UTF-8 and escape the binary more explicitly
      string.dup.force_encoding("UTF-8").scrub
    end
  end
end