tools/deadlock: support specifies maxnum of threads and edge cases (#3455)
authorzcy <zcy.chenyue.zhou@gmail.com>
Thu, 27 May 2021 16:50:23 +0000 (00:50 +0800)
committerGitHub <noreply@github.com>
Thu, 27 May 2021 16:50:23 +0000 (09:50 -0700)
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.

man/man8/deadlock.8
tools/deadlock.c
tools/deadlock.py
tools/deadlock_example.txt

index 0be3f4ab37b38b260d97780f31a5e8d67acf59af..3e7744ce88370fe7f2a93d2451f1091c328a6421 100644 (file)
@@ -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
index b34a207ddff2f154d05825f445220bbf7debe602..006dc121932ed9ae7122b2e61ab21fe36c3633af 100644 (file)
@@ -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 {
@@ -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 {
index 81122adb76618a0c733100b3cb5d21689b857f20..d6046c24178c27e21dbbb431081e1339bc60ec92 100755 (executable)
@@ -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:
@@ -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')
index 45d8126054c8c09a82f0f068102996e0ae1347f7..559ce028f2b73287c333888c3d3aa2f298b3c574 100644 (file)
@@ -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