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

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

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



272
273
274
275
276
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
# File 'lib/vernier/output/firefox.rb', line 272

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
end

Instance Attribute Details

#is_startObject (readonly)

Returns the value of attribute is_start.



270
271
272
# File 'lib/vernier/output/firefox.rb', line 270

def is_start
  @is_start
end

#profileObject (readonly)

Returns the value of attribute profile.



270
271
272
# File 'lib/vernier/output/firefox.rb', line 270

def profile
  @profile
end

Instance Method Details

#allocations_tableObject



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/vernier/output/firefox.rb', line 465

def allocations_table
  return nil if !@allocations
  samples, weights, timestamps = @allocations.values_at(:samples, :weights, :timestamps)
  return nil if samples.size == 0
  size = samples.size
  timestamps = timestamps.map { _1 / 1_000_000.0 }
  ret = {
    "time": timestamps,
    "className": ["Object"]*size,
    "typeName": ["JSObject"]*size,
    "coarseType": ["Object"]*size,
    "weight": weights,
    "inNursery": [false] * size,
    "stack": samples,
    "length": size
  }
  ret
end

#categorize_filename(filename) ⇒ Object



360
361
362
363
364
365
366
367
# File 'lib/vernier/output/firefox.rb', line 360

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



369
370
371
# File 'lib/vernier/output/firefox.rb', line 369

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

#dataObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/vernier/output/firefox.rb', line 393

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



386
387
388
389
390
391
# File 'lib/vernier/output/firefox.rb', line 386

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

#find_category_and_subcategory(filename, categories) ⇒ Object



377
378
379
380
381
382
383
384
# File 'lib/vernier/output/firefox.rb', line 377

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



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/vernier/output/firefox.rb', line 545

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

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

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

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

#func_tableObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/vernier/output/firefox.rb', line 573

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



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
454
455
456
457
458
459
460
461
462
463
# File 'lib/vernier/output/firefox.rb', line 425

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

  @markers.each_with_index do |(_, name, start, finish, phase, datum), i|
    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



373
374
375
# File 'lib/vernier/output/firefox.rb', line 373

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

#samples_tableObject



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/vernier/output/firefox.rb', line 484

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



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/vernier/output/firefox.rb', line 520

def stack_table
  frames =   @stack_table_hash[:stack_table].fetch(:frame).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, category)|
    frames << frames[stack]
    prefixes << prefixes[stack]
    categories << category
    subcategories << 0
  end

  size = frames.length
  raise unless frames.size == size
  raise unless prefixes.size == size
  {
    frame: frames,
    category: categories,
    subcategory: subcategories,
    prefix: prefixes,
    length: prefixes.length
  }
end

#string_tableObject



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/vernier/output/firefox.rb', line 598

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
    elsif string.encoding == Encoding::BINARY
      # TODO: We might want to guess UTF-8 and escape the binary more explicitly
      string.dup.force_encoding("UTF-8").scrub
    else
      # TODO: ideally we should attempt to properly re-encode here, but right now I think this is dead code
      string.dup.force_encoding("UTF-8").scrub
    end
  end
end