Class: Roby::EventLogging::StackProfEventManager

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/event_logging/stackprof_event_manager.rb

Overview

A simple event logger that displays a textual representation of the events on plain I/O (e.g. log file or stdout)

Constant Summary collapse

TIMEPOINT_KIND_START =
%I[timepoint timepoint_group_start].freeze
TIMEPOINT_KIND_END =
%I[timepoint timepoint_group_end].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStackProfEventManager

Returns a new instance of StackProfEventManager.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 44

def initialize
    @results_path = "/tmp/roby-stackprof-#{Process.pid}"
    @skip = 0
    @count = nil
    @remaining_count = nil
    @remaining_skip = nil
    @mode = :cpu
    @raw = false
    @dumped_results = false
    @quit = false
end

Instance Attribute Details

#countInteger?

How many start/stop cycles until we dump the results

Returns:

  • (Integer, nil)

    the count, or nil to dump on close



37
38
39
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 37

def count
  @count
end

#modeSymbol

The stackprof mode

Returns:

  • (Symbol)

    stackprof sampling mode. At the time of this writing, can be one of :wall, :cpu or :object



14
15
16
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 14

def mode
  @mode
end

#quitBoolean

Quit the application when the profile data has been dumped

Returns:

  • (Boolean)


27
28
29
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 27

def quit
  @quit
end

#rawBoolean

Whether stackprof saves raw samples

They are needed for post-processing like e.g. flamegraph. The default is false

Returns:

  • (Boolean)


22
23
24
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 22

def raw
  @raw
end

#results_pathInteger?

How many start/stop cycles until we dump the results

Returns:

  • (Integer, nil)

    the count, or nil to dump on close



42
43
44
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 42

def results_path
  @results_path
end

#skipInteger?

How many start/stop cycles to ignore before we start profiling

Returns:

  • (Integer, nil)

    the configured skip cycles



32
33
34
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 32

def skip
  @skip
end

Instance Method Details

#closeObject



136
137
138
139
140
141
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 136

def close
    return unless StackProf.running?

    StackProf.stop
    stackprof_dump_results
end

#dump(name, time, args) ⇒ Object



119
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 119

def dump(name, time, args); end

#dump_timeObject



147
148
149
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 147

def dump_time
    0
end

#dump_timepoint(kind, _time, args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 124

def dump_timepoint(kind, _time, args)
    tp_name = args[2]

    if TIMEPOINT_KIND_START.include?(kind) && (@start_matcher === tp_name)
        stackprof_handle_start_timepoint(tp_name)
    elsif TIMEPOINT_KIND_END.include?(kind) && (@stop_matcher === tp_name)
        stackprof_handle_stop_timepoint(tp_name)
    end

    nil
end

#flush_cycle(name, *args) ⇒ Object



151
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 151

def flush_cycle(name, *args); end

#log_queue_sizeObject



143
144
145
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 143

def log_queue_size
    0
end

#log_timepoints?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 115

def log_timepoints?
    true
end

#stackprof_dump_resultsObject



110
111
112
113
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 110

def stackprof_dump_results
    StackProf.results(results_path)
    Robot.info "stackprof: results dumped on #{results_path}"
end

#stackprof_handle_start_timepoint(timepoint_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Start profiling



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 75

def stackprof_handle_start_timepoint(timepoint_name)
    return if StackProf.running?
    return if @remaining_count && @remaining_count <= 0

    @remaining_skip ||= @skip
    if @remaining_skip > 0
        @remaining_skip -= 1
        return
    end

    Robot.info "stackprof: started on timepoint #{timepoint_name}"
    StackProf.start(mode: @mode.to_sym, raw: @raw)
end

#stackprof_handle_stop_timepoint(timepoint_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handle the reception of a stop timepoint while stackprof was running



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 92

def stackprof_handle_stop_timepoint(timepoint_name)
    return unless StackProf.running?

    Robot.info "stackprof: stopped on timepoint #{timepoint_name}"
    StackProf.stop

    @remaining_count ||= @count

    if @remaining_count > 0
        @remaining_count -= 1
        Robot.info "stackprof: #{@count - @remaining_count} regions profiled"
        return unless @remaining_count == 0
    end

    stackprof_dump_results
    Roby.app.quit if @quit
end

#start_on(matcher) ⇒ Object

Start profiling whenever a timepoint name matches the given matcher

Parameters:

  • matcher (#===)

    the matcher object. It replaces an existing object (if there was one)



60
61
62
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 60

def start_on(matcher)
    @start_matcher = matcher
end

#stop_on(matcher) ⇒ Object

Stop profiling whenever a timepoint name matches the given matcher

Parameters:

  • matcher (#===)

    the matcher object. It replaces an existing object (if there was one)



68
69
70
# File 'lib/roby/event_logging/stackprof_event_manager.rb', line 68

def stop_on(matcher)
    @stop_matcher = matcher
end