Skip to content

Commit

Permalink
tools/deadlock: support specifies maxnum of threads and edge cases (i…
Browse files Browse the repository at this point in the history
…ovisor#3455)

support to specify maxinum of threads and edge cases. The default values make map taking more than 0.5G memory which cause out-of-memory issue on some systems.
also fix an issue with python `open` so the open file is automatically closed upon file reading is done.
  • Loading branch information
chenyuezhou committed May 27, 2021
1 parent e6aa65e commit 77f5252
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
8 changes: 8 additions & 0 deletions man/man8/deadlock.8
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ These symbols cannot be inlined in the binary.
Comma-separated list of unlock symbols to trace. Default is
pthread_mutex_unlock. These symbols cannot be inlined in the binary.
.TP
\-t THREADS, --threads THREADS
Specifies the maximum number of threads to trace. default 65536.
Note. 40 bytes per thread.
.TP
\-e EDGES, --edges EDGES
Specifies the maximum number of edge cases that can be
recorded. default 65536. Note. 88 bytes per edge case.
.TP
pid
Pid to trace
.SH EXAMPLES
Expand Down
4 changes: 2 additions & 2 deletions tools/deadlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct thread_to_held_mutex_leaf_t {
};

// Map of thread ID -> array of (mutex addresses, stack id)
BPF_HASH(thread_to_held_mutexes, u32, struct thread_to_held_mutex_leaf_t, 2097152);
BPF_HASH(thread_to_held_mutexes, u32, struct thread_to_held_mutex_leaf_t, MAX_THREADS);

// Key type for edges. Represents an edge from mutex1 => mutex2.
struct edges_key_t {
Expand All @@ -47,7 +47,7 @@ struct edges_leaf_t {
};

// Represents all edges currently in the mutex wait graph.
BPF_HASH(edges, struct edges_key_t, struct edges_leaf_t, 2097152);
BPF_HASH(edges, struct edges_key_t, struct edges_leaf_t, MAX_EDGES);

// Info about parent thread when a child thread is created.
struct thread_created_leaf_t {
Expand Down
16 changes: 15 additions & 1 deletion tools/deadlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ def main():
help='Comma-separated list of unlock symbols to trace. Default is '
'pthread_mutex_unlock. These symbols cannot be inlined in the binary.',
)
parser.add_argument(
'-t', '--threads', type=int, default=65536,
help='Specifies the maximum number of threads to trace. default 65536. '
'Note. 40 bytes per thread.'
)
parser.add_argument(
'-e', '--edges', type=int, default=65536,
help='Specifies the maximum number of edge cases that can be recorded. '
'default 65536. Note. 88 bytes per edge case.'
)
args = parser.parse_args()
if not args.binary:
try:
Expand All @@ -465,7 +475,11 @@ def main():
print('%s. Is the process (pid=%d) running?' % (str(e), args.pid))
sys.exit(1)

bpf = BPF(src_file=b'deadlock.c')
with open('deadlock.c') as f:
text = f.read()
text = text.replace(b'MAX_THREADS', str(args.threads));
text = text.replace(b'MAX_EDGES', str(args.edges));
bpf = BPF(text=text)

# Trace where threads are created
bpf.attach_kretprobe(event=bpf.get_syscall_fnname('clone'), fn_name='trace_clone')
Expand Down
6 changes: 6 additions & 0 deletions tools/deadlock_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ optional arguments:
Comma-separated list of unlock symbols to trace.
Default is pthread_mutex_unlock. These symbols cannot
be inlined in the binary.
-t THREADS, --threads THREADS
Specifies the maximum number of threads to trace.
default 65536. Note. 40 bytes per thread.
-e EDGES, --edges EDGES
Specifies the maximum number of edge cases that can be
recorded. default 65536. Note. 88 bytes per edge case.

Examples:
deadlock 181 # Analyze PID 181
Expand Down

0 comments on commit 77f5252

Please sign in to comment.