Class: OsCtld::LockRegistry

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Exception, OsCtl::Lib::Utils::Log, Singleton
Defined in:
lib/osctld/lock_registry.rb

Overview

Global registry of all inclusive/exclusive logs for debugging purposes

All inclusive/exclusive locks that are attempted and held are registered in this class. The idea is that when a deadlock occurs, you can inspect the data available within this class to see what threads have acquired what locks on what objects and get backtraces.

Defined Under Namespace

Classes: Lock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLockRegistry

Returns a new instance of LockRegistry.



30
31
32
33
34
35
36
# File 'lib/osctld/lock_registry.rb', line 30

def initialize
  @enabled = nil
  @mutex = Mutex.new
  @queue = Queue.new
  @registry = []
  @last_id = Concurrent::AtomicFixnum.new(0)
end

Instance Attribute Details

#registryObject (readonly, protected)

Returns the value of attribute registry.



115
116
117
# File 'lib/osctld/lock_registry.rb', line 115

def registry
  @registry
end

Instance Method Details

#do_register(lock) ⇒ Object (protected)



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/osctld/lock_registry.rb', line 130

def do_register(lock)
  sync do
    case lock.state
    when :waiting
      if lock.type != :exclusive || !registry.detect { |v| v == lock && v.state == :waiting }
        registry << lock
      end

    when :locked
      waiting = registry.detect { |v| v == lock && v.state == :waiting }

      if waiting
        waiting.state = :locked
        waiting.backtrace = lock.backtrace
      else
        registry << lock
      end

    when :unlocked, :timeout
      registry.delete_if { |v| v == lock }

    else
      registry << lock
    end
  end
end

#dumpObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osctld/lock_registry.rb', line 89

def dump
  unless @enabled
    log(:debug, 'locks', 'Lock registry disabled')
    return
  end

  log(:debug, 'locks', 'Dumping lock registry')

  export.each do |lock|
    log(
      :debug,
      "id=#{lock[:id]},thread=#{lock[:thread]},type=#{lock[:type]}," \
      "state=#{lock[:state]}"
    )
    log(:debug, denixstorify(lock[:backtrace]).join("\n"))
  end

  log(:debug, 'locks', 'End of dump')
end

#enabled?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/osctld/lock_registry.rb', line 47

def enabled?
  @enabled
end

#exportObject



83
84
85
86
87
# File 'lib/osctld/lock_registry.rb', line 83

def export
  return [] unless @enabled

  sync { registry.map(&:to_h) }
end

#log_typeObject



109
110
111
# File 'lib/osctld/lock_registry.rb', line 109

def log_type
  'locks'
end

#register(object, type, state) ⇒ Object

Parameters:

  • object (Object)
  • type (:inclusive, :exclusive)
  • state (:waiting, :locked, :unlocked, :timeout)


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

def register(object, type, state)
  return unless @enabled

  @queue << Lock.new(
    @last_id.increment,
    Time.now,
    Thread.current,
    object,
    type,
    state,
    caller[1..]
  )
end

#runObject (protected)



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/osctld/lock_registry.rb', line 117

def run
  loop do
    v = @queue.pop

    if v.is_a?(Lock)
      do_register(v)

    elsif v == :stop
      break
    end
  end
end

#setup(enabled) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/osctld/lock_registry.rb', line 38

def setup(enabled)
  unless @enabled.nil?
    raise 'programming error: setup can be called only once'
  end

  @enabled = enabled
  start if enabled
end

#startObject



51
52
53
54
55
# File 'lib/osctld/lock_registry.rb', line 51

def start
  return unless @enabled

  @thread = Thread.new { run }
end

#stopObject



57
58
59
60
61
62
63
64
# File 'lib/osctld/lock_registry.rb', line 57

def stop
  return unless @thread

  @queue.clear
  @queue << :stop
  @thread.join
  @thread = nil
end

#syncObject (protected)



157
158
159
160
161
162
163
# File 'lib/osctld/lock_registry.rb', line 157

def sync(&)
  if @mutex.owned?
    yield
  else
    @mutex.synchronize(&)
  end
end