Class: Yast::Profiler

Inherits:
Object
  • Object
show all
Extended by:
Logger
Defined in:
src/ruby/yast/profiler.rb

Constant Summary collapse

RESULT_PATH =
"/var/log/YaST2/profiler_result.txt".freeze

Class Method Summary collapse

Methods included from Logger

log

Class Method Details

.startObject

Start the Ruby Profiler. It start profilling. It also disables ruby VM optimizations, so code execution will be slower.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'src/ruby/yast/profiler.rb', line 13

def start
  raise "multiple profiller start detected" if @started
  @original_compile_options = RubyVM::InstructionSequence.compile_option
  @started = true
  require "profiler"

  # turn on tracing and turn off specialized instruction which replace
  # some core ruby methods with its optimized variant to get complete
  # profiling. More info in book "Ruby Under a Microscope: Learning Ruby
  # Internals Through Experiment". Code is taken from ruby/lib/profile.rb
  RubyVM::InstructionSequence.compile_option = {
    trace_instruction:       true,
    specialized_instruction: false
  }

  at_exit { stop }
  Profiler__.start_profile
end

.start_from_envObject

start the Ruby profiler if "Y2PROFILER" environment variable is set to "1" or "true"(the test is case insensitive, "y2profiler" variable can be also used)



46
47
48
49
50
51
52
53
54
55
# File 'src/ruby/yast/profiler.rb', line 46

def start_from_env
  # do not evaluate again for each client started, run the evaluation only once
  return if @profiler_handled
  @profiler_handled = true

  return unless env_value

  log.info "profiler enabled"
  start
end

.stop(output = nil) ⇒ Object

Stops profiling

Parameters:

  • output (IO) (defaults to: nil)

    an IO stream to print the profile to; if nil, uses a file at RESULT_PATH



34
35
36
37
38
39
40
41
# File 'src/ruby/yast/profiler.rb', line 34

def stop(output = nil)
  return File.open(RESULT_PATH, "w") { |f| stop(f) } unless output

  Profiler__.print_profile(output)

  RubyVM::InstructionSequence.compile_option = @original_compile_options
  @started = false
end