Class: FiberAudit::Runtime::ActiveOperations

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/runtime/active_operations.rb

Overview

Bounded process-local registry shared by the watchdog and future probes.

Defined Under Namespace

Classes: Entry, Handle

Constant Summary collapse

MAX_ENTRIES =
10_000
MAX_SNAPSHOT =
100

Instance Method Summary collapse

Constructor Details

#initialize(pid_source: Process.method(:pid), capacity: MAX_ENTRIES, snapshot_limit: MAX_SNAPSHOT) ⇒ ActiveOperations

Returns a new instance of ActiveOperations.



25
26
27
28
29
30
31
32
33
# File 'lib/fiber_audit/runtime/active_operations.rb', line 25

def initialize(pid_source: Process.method(:pid), capacity: MAX_ENTRIES, snapshot_limit: MAX_SNAPSHOT)
  validate_source!(pid_source)
  validate_limit!(capacity, 'capacity', MAX_ENTRIES)
  validate_limit!(snapshot_limit, 'snapshot_limit', MAX_SNAPSHOT)
  @pid_source = pid_source
  @capacity = capacity
  @snapshot_limit = snapshot_limit
  reset_for_process!(current_pid)
end

Instance Method Details

#finish(handle) ⇒ Object



69
70
71
72
73
74
# File 'lib/fiber_audit/runtime/active_operations.rb', line 69

def finish(handle)
  ensure_current_process!
  return unless handle.is_a?(Handle) && handle.pid == @owner_pid

  @mutex.synchronize { @entries.delete(handle) }
end

#register(operation:, monotonic_ns:, location: nil, execution_context: :unknown, thread: Thread.current, fiber: Fiber.current) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fiber_audit/runtime/active_operations.rb', line 35

def register(
  operation:,
  monotonic_ns:,
  location: nil,
  execution_context: :unknown,
  thread: Thread.current,
  fiber: Fiber.current
)
  ensure_current_process!
  values = normalize_entry(
    operation: operation,
    location: location,
    execution_context: execution_context,
    monotonic_ns: monotonic_ns,
    thread: thread,
    fiber: fiber
  )

  @mutex.synchronize do
    return if @entries.size >= @capacity

    @sequence += 1
    entry = Entry.new(sequence: @sequence, **values)
    handle = Handle.new(
      pid: @owner_pid,
      thread_id: entry.thread_id,
      fiber_id: entry.fiber_id,
      sequence: entry.sequence
    )
    @entries[handle] = entry
    handle
  end
end

#sizeObject



86
87
88
89
# File 'lib/fiber_audit/runtime/active_operations.rb', line 86

def size
  ensure_current_process!
  @mutex.synchronize { @entries.size }
end

#snapshot(thread_id: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/fiber_audit/runtime/active_operations.rb', line 76

def snapshot(thread_id: nil)
  ensure_current_process!
  normalized_thread_id = Validation.integer(thread_id, 'thread_id', allow_nil: true)
  @mutex.synchronize do
    entries = @entries.values
    entries = entries.select { |entry| entry.thread_id == normalized_thread_id } if normalized_thread_id
    entries.sort_by(&:sequence).first(@snapshot_limit).freeze
  end
end