Module: Benelux

Defined in:
lib/benelux.rb,
lib/benelux/mark.rb,
lib/benelux/range.rb,
lib/benelux/stats.rb,
lib/benelux/track.rb,
lib/benelux/packer.rb,
lib/benelux/timeline.rb

Defined Under Namespace

Classes: AlreadyTimed, BadRecursion, BeneluxError, Mark, MethodCounter, MethodPacker, MethodTimer, NotSupported, Range, Stats, Timeline, Tms, Track, UnknownTrack

Constant Summary collapse

VERSION =
"0.5.17"
NOTSUPPORTED =
[Class, Object, Kernel]
@@mutex =
Mutex.new
@@debug =
false
@@logger =
STDERR

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.known_threadsObject (readonly)

Returns the value of attribute known_threads.



32
33
34
# File 'lib/benelux.rb', line 32

def known_threads
  @known_threads
end

.packed_methodsObject (readonly)

Returns the value of attribute packed_methods.



27
28
29
# File 'lib/benelux.rb', line 27

def packed_methods
  @packed_methods
end

.timelineObject (readonly)

Returns the value of attribute timeline.



29
30
31
# File 'lib/benelux.rb', line 29

def timeline
  @timeline
end

.timeline_chunkObject (readonly)

Returns the value of attribute timeline_chunk.



30
31
32
# File 'lib/benelux.rb', line 30

def timeline_chunk
  @timeline_chunk
end

.timeline_updatesObject (readonly)

Returns the value of attribute timeline_updates.



31
32
33
# File 'lib/benelux.rb', line 31

def timeline_updates
  @timeline_updates
end

.tracksObject (readonly)

Returns the value of attribute tracks.



28
29
30
# File 'lib/benelux.rb', line 28

def tracks
  @tracks
end

Class Method Details

.add_counter(klass, meth, aliaz = nil, &blk) ⇒ Object

Raises:



160
161
162
163
# File 'lib/benelux.rb', line 160

def Benelux.add_counter klass, meth, aliaz=nil, &blk
  raise NotSupported, klass unless Benelux.supported? klass
  Benelux::MethodCounter.new klass, meth, aliaz, &blk
end

.add_thread_tag(*args) ⇒ Object



122
# File 'lib/benelux.rb', line 122

def Benelux.add_thread_tag(*args) add_thread_tags *args end

.add_thread_tags(args = Selectable::Tags.new) ⇒ Object

Thread tags become the default for any new Mark or Range.



119
120
121
# File 'lib/benelux.rb', line 119

def Benelux.add_thread_tags(args=Selectable::Tags.new)
  Benelux.thread_timeline.add_default_tags args
end

.add_timer(klass, meth, aliaz = nil, &blk) ⇒ Object

Raises:



154
155
156
157
158
# File 'lib/benelux.rb', line 154

def Benelux.add_timer klass, meth, aliaz=nil, &blk
  raise NotSupported, klass unless Benelux.supported? klass
  raise AlreadyTimed, klass if Benelux.packed_method? klass, meth
  Benelux::MethodTimer.new klass, meth, aliaz, &blk
end

.bm(n = 1, reps = 5, &blk) ⇒ Object

Run a benchmark the healthy way: with a warmup run, with multiple repetitions, and standard deviation.

  • n Number of times to execute blk (one data sample)

  • reps Number of data samples to collect

  • blk a Ruby block to benchmark

Returns a Benelux::Tms object



232
233
234
235
236
237
238
239
240
# File 'lib/benelux.rb', line 232

def Benelux.bm(n=1, reps=5, &blk) 
  require 'benchmark'
  n.times &blk
  tms = Benelux::Tms.new
  reps.times do |rep|
    tms.sample Benchmark.measure() {n.times &blk}
  end
  tms
end

.current_track(name = nil, group = nil) ⇒ Object

If name is specified, this will associate the current thread with that Track name (the Track will be created if necessary).

If track is nil, it returns the Track object for the Track associated to the current thread.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/benelux.rb', line 69

def Benelux.current_track(name=nil,group=nil)
  if name.nil?
    name = Thread.current.track_name
  else
    Thread.current.track_name = name
    @@mutex.synchronize do
      Thread.current.timeline ||= Benelux::Timeline.new
      Thread.current.rotated_timelines ||= []
      @tracks[name] ||= Track.new(name, group)
      @tracks[name].add_thread Thread.current
      @known_threads << Thread.current
    end
  end
  Benelux.track(name)
end

.debug?Boolean

Returns:

  • (Boolean)


172
# File 'lib/benelux.rb', line 172

def Benelux.debug?; @@debug; end

.disable_debugObject



171
# File 'lib/benelux.rb', line 171

def Benelux.disable_debug; @@debug = false; end

.enable_debugObject



170
# File 'lib/benelux.rb', line 170

def Benelux.enable_debug; @@debug = true; end

.inspectObject



130
131
132
133
134
# File 'lib/benelux.rb', line 130

def Benelux.inspect
  str = ["Benelux"]
  str << "tracks:" << Benelux.tracks.inspect
  str.join $/
end

.known_thread?(t = Thread.current) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/benelux.rb', line 141

def Benelux.known_thread?(t=Thread.current)
  Benelux.known_threads.member? t
end

.ld(*msg) ⇒ Object



165
166
167
# File 'lib/benelux.rb', line 165

def Benelux.ld(*msg)
  @@logger.puts "D:  " << msg.join("#{$/}D:  ") if debug?
end

.packed_method(klass, meth) ⇒ Object



145
146
147
148
# File 'lib/benelux.rb', line 145

def Benelux.packed_method(klass, meth)
  return nil unless defined?(Benelux.packed_methods[klass][meth])
  Benelux.packed_methods[klass][meth]
end

.packed_method?(klass, meth) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/benelux.rb', line 150

def Benelux.packed_method? klass, meth
  !Benelux.packed_method(klass, meth).nil?
end

.remove_thread_tag(*args) ⇒ Object



127
# File 'lib/benelux.rb', line 127

def Benelux.remove_thread_tag(*args) remove_thread_tags *args end

.remove_thread_tags(*args) ⇒ Object



124
125
126
# File 'lib/benelux.rb', line 124

def Benelux.remove_thread_tags(*args)
  Benelux.thread_timeline.remove_default_tags *args
end

.resetObject



35
36
37
38
39
40
41
42
# File 'lib/benelux.rb', line 35

def Benelux.reset
  @tracks = SelectableHash.new
  @timeline = Timeline.new
  @timeline_chunk = Timeline.new  # See: update_global_timeline
  @timeline_updates = 0
  @known_threads = []
  @processed_dead_threads = []
end

.supported?(klass) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/benelux.rb', line 136

def Benelux.supported?(klass)
  !NOTSUPPORTED.member?(klass)
end

.thread_timelineObject



49
50
51
# File 'lib/benelux.rb', line 49

def Benelux.thread_timeline
  Thread.current.timeline
end

.track(name) ⇒ Object

Raises:



53
54
55
56
# File 'lib/benelux.rb', line 53

def Benelux.track(name)
  raise UnknownTrack unless track? name
  @tracks[name]
end

.track?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/benelux.rb', line 58

def Benelux.track?(name)
  @tracks.has_key? name
end

.update_global_timelineObject

Only updates data from threads that are dead and rotated timelines.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/benelux.rb', line 88

def Benelux.update_global_timeline
  @@mutex.synchronize do
    dthreads = Benelux.known_threads.select { |t| 
      !t.timeline.nil? && (t.nil? || !t.status) &&
      !@processed_dead_threads.member?(t)
    }
    # Threads that have rotated timelines
    rthreads = Benelux.known_threads.select { |t|
      !t.rotated_timelines.empty?
    }
    dtimelines = dthreads.collect { |t| t.timeline }
    # Also get all rotated timelines. 
    rthreads.each { |t| 
      # We loop carefully through the rotated timelines
      # incase something was added while we're working. 
      while !t.rotated_timelines.empty?
        dtimelines.push t.rotated_timelines.shift 
      end
    }
    Benelux.ld [:update_global_timeline, dthreads.size, rthreads.size, dtimelines.size].inspect
    # Keep track of this update separately
    @timeline_chunk = Benelux::Timeline.new
    @timeline_chunk.merge! *dtimelines
    @processed_dead_threads.push *dthreads
    tl = Benelux.timeline.merge! Benelux.timeline_chunk
    @timeline_updates += 1
    tl
  end
end