Class: Profiler::Collectors::FunctionProfilerCollector

Inherits:
BaseCollector
  • Object
show all
Defined in:
lib/profiler/collectors/function_profiler_collector.rb

Defined Under Namespace

Classes: CallFrame

Constant Summary collapse

GC_FRAME_NAME =
"(garbage collection)"

Instance Attribute Summary

Attributes inherited from BaseCollector

#profile

Instance Method Summary collapse

Methods inherited from BaseCollector

descendants, inherited, #initialize, #panel_content, #render_html, #render_mode

Constructor Details

This class inherits a constructor from Profiler::Collectors::BaseCollector

Instance Method Details

#collectObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 77

def collect
  mode  = Thread.current[:fn_profiler_mode]  || Profiler.function_profiling_mode
  clock = Thread.current[:fn_profiler_clock] || Profiler.function_profiling_clock
  Thread.current[:fn_profiler_mode]  = nil
  Thread.current[:fn_profiler_clock] = nil

  if mode == "lite" && defined?(StackProf)
    StackProf.stop
    result   = StackProf.results
    wall_ms  = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - (Thread.current[:fn_profiler_wall_start] || 0)) * 1000
    cpu_ms   = (Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID) - (Thread.current[:fn_profiler_cpu_start] || 0)) * 1000
    Thread.current[:fn_profiler_wall_start] = nil
    Thread.current[:fn_profiler_cpu_start]  = nil
    collect_stackprof(result, mode, clock, wall_ms, cpu_ms)
  else
    collect_tracepoint(mode)
  end
end

#has_data?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 41

def has_data?
  false
end

#iconObject



22
23
24
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 22

def icon
  ""
end

#nameObject



18
19
20
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 18

def name
  "function_profile"
end

#priorityObject



26
27
28
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 26

def priority
  31
end

#subscribeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 45

def subscribe
  unless Profiler.function_profiling_enabled
    store_data({
      enabled:    false,
      max_frames: Profiler.function_profiling_max_frames,
      mode:       Profiler.function_profiling_mode,
      clock:      Profiler.function_profiling_clock
    })
    return
  end

  mode  = Profiler.function_profiling_mode
  clock = Profiler.function_profiling_clock
  Thread.current[:fn_profiler_mode]  = mode
  Thread.current[:fn_profiler_clock] = clock

  if mode == "lite" && defined?(StackProf)
    # Always record wall + cpu at request level for the comparison stat
    Thread.current[:fn_profiler_wall_start] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    Thread.current[:fn_profiler_cpu_start]  = Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID)

    sp_mode = case clock
              when "cpu"    then :cpu
              when "object" then :object
              else               :wall
              end
    StackProf.start(mode: sp_mode, interval: 1000, raw: true)
  else
    subscribe_tracepoint(mode)
  end
end

#tab_configObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 30

def tab_config
  {
    key: "function_profile",
    label: "Function Profile",
    icon: icon,
    priority: priority,
    enabled: false,
    default_active: false
  }
end

#toolbar_summaryObject



96
97
98
99
100
101
102
# File 'lib/profiler/collectors/function_profiler_collector.rb', line 96

def toolbar_summary
  return { text: "off", color: "gray" } unless Profiler.function_profiling_enabled
  case Profiler.function_profiling_mode
  when "lite" then { text: "sampling on", color: "blue" }
  else             { text: "fn profiling on", color: "purple" }
  end
end