Module: UmbrellioUtils::Patches::ActiveSupportEvent

Defined in:
lib/umbrellio_utils/patches/active_support_event.rb

Overview

Extends ActiveSupport::Notifications::Event with GVL wait time and malloc stats. https://github.com/rails/rails/blob/main/activesupport/lib/active_support/notifications/instrumenter.rb # rubocop:disable Layout/LineLength

The GVL timer must be enabled in the application as early as possible:

GVLTools::LocalTimer.enable

Constant Summary collapse

STATS_PRECISION =
6

Instance Method Summary collapse

Instance Method Details

#finish!Object



32
33
34
35
36
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 32

def finish!
  super
  @gvl_time_finish = GVLTools::LocalTimer.monotonic_time
  @malloc_increase_bytes_finish = GC.stat(:malloc_increase_bytes)
end

#gvl_timeObject

Time the thread spent waiting for the GVL, in milliseconds



39
40
41
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 39

def gvl_time
  (@gvl_time_finish - @gvl_time_start) / 1_000_000.0
end

#initializeObject



18
19
20
21
22
23
24
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 18

def initialize(...)
  super
  @gvl_time_start = 0
  @gvl_time_finish = 0
  @malloc_increase_bytes_start = 0
  @malloc_increase_bytes_finish = 0
end

#malloc_increase_bytesObject

Delta of the process-global malloc counter: can be negative (it resets on GC) and includes sibling threads' allocations under threaded servers. Guard with #positive? where a counter is expected.



46
47
48
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 46

def malloc_increase_bytes
  @malloc_increase_bytes_finish - @malloc_increase_bytes_start
end

#start!Object



26
27
28
29
30
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 26

def start!
  super
  @gvl_time_start = GVLTools::LocalTimer.monotonic_time
  @malloc_increase_bytes_start = GC.stat(:malloc_increase_bytes)
end

#stats(precision: STATS_PRECISION) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/umbrellio_utils/patches/active_support_event.rb', line 50

def stats(precision: STATS_PRECISION)
  {
    gc_time: gc_time.round(precision),
    gvl_time: gvl_time.round(precision),
    cpu_time: cpu_time.round(precision),
    idle_time: idle_time.round(precision),
    allocations:,
    malloc_increase_bytes:,
  }
end