proxygen
deadlock Namespace Reference

Classes

class  Deadlock
 
class  DiGraph
 
class  MutexType
 

Functions

def strongly_connected_components (G)
 
def simple_cycles (G)
 
def find_cycle (graph)
 
def get_stacktrace (thread_id)
 
def is_thread_blocked_with_frame (thread_id, top_line, expected_top_line, expected_frame)
 
def print_cycle (graph, lwp_to_thread_id, cycle)
 
def get_thread_info ()
 
def get_pthread_mutex_t_owner_and_address (lwp_to_thread_id, thread_lwp)
 
def get_pthread_rwlock_t_owner_and_address (lwp_to_thread_id, thread_lwp)
 
def load ()
 
def info ()
 

Function Documentation

def deadlock.find_cycle (   graph)
Looks for a cycle in the graph. If found, returns the first cycle.
If nodes a1, a2, ..., an are in a cycle, then this returns:
    [(a1,a2), (a2,a3), ... (an-1,an), (an, a1)]
Otherwise returns an empty list.

Definition at line 213 of file deadlock.py.

References bm.list, and simple_cycles().

Referenced by deadlock.Deadlock.invoke().

213 def find_cycle(graph):
214  '''
215  Looks for a cycle in the graph. If found, returns the first cycle.
216  If nodes a1, a2, ..., an are in a cycle, then this returns:
217  [(a1,a2), (a2,a3), ... (an-1,an), (an, a1)]
218  Otherwise returns an empty list.
219  '''
220  cycles = list(simple_cycles(graph))
221  if cycles:
222  nodes = cycles[0]
223  nodes.append(nodes[0])
224  edges = []
225  prev = nodes[0]
226  for node in nodes[1:]:
227  edges.append((prev, node))
228  prev = node
229  return edges
230  else:
231  return []
232 
233 
Encoder::MutableCompressedList list
def find_cycle(graph)
Definition: deadlock.py:213
def simple_cycles(G)
Definition: deadlock.py:143
def deadlock.get_pthread_mutex_t_owner_and_address (   lwp_to_thread_id,
  thread_lwp 
)
Finds the thread holding the mutex that this thread is blocked on.
Returns a pair of (lwp of thread owning mutex, mutex address),
or (None, None) if not found.

Definition at line 342 of file deadlock.py.

References folly::gen.dereference.

342 def get_pthread_mutex_t_owner_and_address(lwp_to_thread_id, thread_lwp):
343  '''
344  Finds the thread holding the mutex that this thread is blocked on.
345  Returns a pair of (lwp of thread owning mutex, mutex address),
346  or (None, None) if not found.
347  '''
348  # Go up the stack to the pthread_mutex_lock frame
349  gdb.execute(
350  'thread %d' % lwp_to_thread_id[thread_lwp],
351  from_tty=False,
352  to_string=True
353  )
354  gdb.execute('frame 1', from_tty=False, to_string=True)
355 
356  # Get the owner of the mutex by inspecting the internal
357  # fields of the mutex.
358  try:
359  mutex_info = gdb.parse_and_eval('mutex').dereference()
360  mutex_owner_lwp = int(mutex_info['__data']['__owner'])
361  return (mutex_owner_lwp, int(mutex_info.address))
362  except gdb.error:
363  return (None, None)
364 
365 
def get_pthread_mutex_t_owner_and_address(lwp_to_thread_id, thread_lwp)
Definition: deadlock.py:342
constexpr detail::Dereference dereference
Definition: Base-inl.h:2575
def deadlock.get_pthread_rwlock_t_owner_and_address (   lwp_to_thread_id,
  thread_lwp 
)
If the thread is waiting on a write-locked pthread_rwlock_t, this will
return the pair of:
    (lwp of thread that is write-owning the mutex, mutex address)
or (None, None) if not found, or if the mutex is read-locked.

Definition at line 366 of file deadlock.py.

References folly::gen.dereference.

366 def get_pthread_rwlock_t_owner_and_address(lwp_to_thread_id, thread_lwp):
367  '''
368  If the thread is waiting on a write-locked pthread_rwlock_t, this will
369  return the pair of:
370  (lwp of thread that is write-owning the mutex, mutex address)
371  or (None, None) if not found, or if the mutex is read-locked.
372  '''
373  # Go up the stack to the pthread_rwlock_{rd|wr}lock frame
374  gdb.execute(
375  'thread %d' % lwp_to_thread_id[thread_lwp],
376  from_tty=False,
377  to_string=True
378  )
379  gdb.execute('frame 2', from_tty=False, to_string=True)
380 
381  # Get the owner of the mutex by inspecting the internal
382  # fields of the mutex.
383  try:
384  rwlock_info = gdb.parse_and_eval('rwlock').dereference()
385  rwlock_owner_lwp = int(rwlock_info['__data']['__writer'])
386  # We can only track the owner if it is currently write-locked.
387  # If it is not write-locked or if it is currently read-locked,
388  # possibly by multiple threads, we cannot find the owner.
389  if rwlock_owner_lwp != 0:
390  return (rwlock_owner_lwp, int(rwlock_info.address))
391  else:
392  return (None, None)
393  except gdb.error:
394  return (None, None)
395 
396 
def get_pthread_rwlock_t_owner_and_address(lwp_to_thread_id, thread_lwp)
Definition: deadlock.py:366
constexpr detail::Dereference dereference
Definition: Base-inl.h:2575
def deadlock.get_stacktrace (   thread_id)
Returns the stack trace for the thread id as a list of strings.

Definition at line 234 of file deadlock.py.

References is_thread_blocked_with_frame(), and folly::gen.split().

Referenced by is_thread_blocked_with_frame().

234 def get_stacktrace(thread_id):
235  '''
236  Returns the stack trace for the thread id as a list of strings.
237  '''
238  gdb.execute('thread %d' % thread_id, from_tty=False, to_string=True)
239  output = gdb.execute('bt', from_tty=False, to_string=True)
240  stacktrace_lines = output.strip().split('\n')
241  return stacktrace_lines
242 
243 
S split(const StringPiece source, char delimiter)
Definition: String.h:61
def get_stacktrace(thread_id)
Definition: deadlock.py:234
def deadlock.get_thread_info ( )
Returns a pair of:
- map of LWP -> thread ID
- map of blocked threads LWP -> potential mutex type

Definition at line 313 of file deadlock.py.

References group, and folly::gen.split().

Referenced by deadlock.Deadlock.invoke().

314  '''
315  Returns a pair of:
316  - map of LWP -> thread ID
317  - map of blocked threads LWP -> potential mutex type
318  '''
319  # LWP -> thread ID
320  lwp_to_thread_id = {}
321 
322  # LWP -> potential mutex type it is blocked on
323  blocked_threads = {}
324 
325  output = gdb.execute('info threads', from_tty=False, to_string=True)
326  lines = output.strip().split('\n')[1:]
327  regex = re.compile(r'[\s\*]*(\d+).*Thread.*\(LWP (\d+)\).*')
328  for line in lines:
329  try:
330  thread_id = int(regex.match(line).group(1))
331  thread_lwp = int(regex.match(line).group(2))
332  lwp_to_thread_id[thread_lwp] = thread_id
333  mutex_type = MutexType.get_mutex_type(thread_id, line)
334  if mutex_type:
335  blocked_threads[thread_lwp] = mutex_type
336  except Exception:
337  continue
338 
339  return (lwp_to_thread_id, blocked_threads)
340 
341 
def get_thread_info()
Definition: deadlock.py:313
S split(const StringPiece source, char delimiter)
Definition: String.h:61
Optional< NamedGroup > group
def deadlock.is_thread_blocked_with_frame (   thread_id,
  top_line,
  expected_top_line,
  expected_frame 
)
Returns True if we found expected_top_line in top_line, and
we found the expected_frame in the thread's stack trace.

Definition at line 246 of file deadlock.py.

References folly::gen.any(), and get_stacktrace().

Referenced by deadlock.MutexType.get_mutex_type(), and get_stacktrace().

246 ):
247  '''
248  Returns True if we found expected_top_line in top_line, and
249  we found the expected_frame in the thread's stack trace.
250  '''
251  if expected_top_line not in top_line:
252  return False
253  stacktrace_lines = get_stacktrace(thread_id)
254  return any(expected_frame in line for line in stacktrace_lines)
255 
256 
Composed any(Predicate pred=Predicate())
Definition: Base.h:758
def get_stacktrace(thread_id)
Definition: deadlock.py:234
def deadlock.print_cycle (   graph,
  lwp_to_thread_id,
  cycle 
)
Prints the threads and mutexes involved in the deadlock.

Definition at line 300 of file deadlock.py.

Referenced by deadlock.Deadlock.invoke().

300 def print_cycle(graph, lwp_to_thread_id, cycle):
301  '''Prints the threads and mutexes involved in the deadlock.'''
302  for (m, n) in cycle:
303  print(
304  'Thread %d (LWP %d) is waiting on %s (0x%016x) held by '
305  'Thread %d (LWP %d)' % (
306  lwp_to_thread_id[m], m,
307  graph.attributes(m, n)['mutex_type'].value,
308  graph.attributes(m, n)['mutex'], lwp_to_thread_id[n], n
309  )
310  )
311 
312 
def print_cycle(graph, lwp_to_thread_id, cycle)
Definition: deadlock.py:300
def deadlock.simple_cycles (   G)
Adapted from networkx: http://networkx.github.io/
Parameters
----------
G : DiGraph
Returns
-------
cycle_generator: generator
   A generator that produces elementary cycles of the graph.
   Each cycle is represented by a list of nodes along the cycle.

Definition at line 143 of file deadlock.py.

References add, bm.list, and strongly_connected_components().

Referenced by find_cycle().

143 def simple_cycles(G): # noqa: C901
144  '''
145  Adapted from networkx: http://networkx.github.io/
146  Parameters
147  ----------
148  G : DiGraph
149  Returns
150  -------
151  cycle_generator: generator
152  A generator that produces elementary cycles of the graph.
153  Each cycle is represented by a list of nodes along the cycle.
154  '''
155 
156  def _unblock(thisnode, blocked, B):
157  stack = set([thisnode])
158  while stack:
159  node = stack.pop()
160  if node in blocked:
161  blocked.remove(node)
162  stack.update(B[node])
163  B[node].clear()
164 
165  # Johnson's algorithm requires some ordering of the nodes.
166  # We assign the arbitrary ordering given by the strongly connected comps
167  # There is no need to track the ordering as each node removed as processed.
168  # save the actual graph so we can mutate it here
169  # We only take the edges because we do not want to
170  # copy edge and node attributes here.
171  subG = G.subgraph(G.nodes())
172  sccs = list(strongly_connected_components(subG))
173  while sccs:
174  scc = sccs.pop()
175  # order of scc determines ordering of nodes
176  startnode = scc.pop()
177  # Processing node runs 'circuit' routine from recursive version
178  path = [startnode]
179  blocked = set() # vertex: blocked from search?
180  closed = set() # nodes involved in a cycle
181  blocked.add(startnode)
182  B = defaultdict(set) # graph portions that yield no elementary circuit
183  stack = [(startnode, list(subG.neighbors(startnode)))]
184  while stack:
185  thisnode, nbrs = stack[-1]
186  if nbrs:
187  nextnode = nbrs.pop()
188  if nextnode == startnode:
189  yield path[:]
190  closed.update(path)
191  elif nextnode not in blocked:
192  path.append(nextnode)
193  stack.append((nextnode, list(subG.neighbors(nextnode))))
194  closed.discard(nextnode)
195  blocked.add(nextnode)
196  continue
197  # done with nextnode... look for more neighbors
198  if not nbrs: # no more nbrs
199  if thisnode in closed:
200  _unblock(thisnode, blocked, B)
201  else:
202  for nbr in subG.neighbors(thisnode):
203  if thisnode not in B[nbr]:
204  B[nbr].add(thisnode)
205  stack.pop()
206  path.pop()
207  # done processing this node
208  subG.remove_node(startnode)
209  H = subG.subgraph(scc) # make smaller to avoid work in SCC routine
210  sccs.extend(list(strongly_connected_components(H)))
211 
212 
auto add
Definition: BaseTest.cpp:70
def strongly_connected_components(G)
Definition: deadlock.py:88
Encoder::MutableCompressedList list
Definition: Traits.h:592
def simple_cycles(G)
Definition: deadlock.py:143
def deadlock.strongly_connected_components (   G)
Adapted from networkx: http://networkx.github.io/
Parameters
----------
G : DiGraph
Returns
-------
comp : generator of sets
    A generator of sets of nodes, one for each strongly connected
    component of G.

Definition at line 88 of file deadlock.py.

References min.

Referenced by simple_cycles().

88 def strongly_connected_components(G): # noqa: C901
89  '''
90  Adapted from networkx: http://networkx.github.io/
91  Parameters
92  ----------
93  G : DiGraph
94  Returns
95  -------
96  comp : generator of sets
97  A generator of sets of nodes, one for each strongly connected
98  component of G.
99  '''
100  preorder = {}
101  lowlink = {}
102  scc_found = {}
103  scc_queue = []
104  i = 0 # Preorder counter
105  for source in G.nodes():
106  if source not in scc_found:
107  queue = [source]
108  while queue:
109  v = queue[-1]
110  if v not in preorder:
111  i = i + 1
112  preorder[v] = i
113  done = 1
114  v_nbrs = G.neighbors(v)
115  for w in v_nbrs:
116  if w not in preorder:
117  queue.append(w)
118  done = 0
119  break
120  if done == 1:
121  lowlink[v] = preorder[v]
122  for w in v_nbrs:
123  if w not in scc_found:
124  if preorder[w] > preorder[v]:
125  lowlink[v] = min([lowlink[v], lowlink[w]])
126  else:
127  lowlink[v] = min([lowlink[v], preorder[w]])
128  queue.pop()
129  if lowlink[v] == preorder[v]:
130  scc_found[v] = True
131  scc = {v}
132  while (
133  scc_queue and preorder[scc_queue[-1]] > preorder[v]
134  ):
135  k = scc_queue.pop()
136  scc_found[k] = True
137  scc.add(k)
138  yield scc
139  else:
140  scc_queue.append(v)
141 
142 
def strongly_connected_components(G)
Definition: deadlock.py:88
LogLevel min
Definition: LogLevel.cpp:30