bcc icon indicating copy to clipboard operation
bcc copied to clipboard

funccount "SyS_.*" stuck on-CPU

Open oujunli opened this issue 10 years ago • 8 comments

When run python funccount.py "SyS_.*", at the beginning, log:

write(/sys/kernel/debug/tracing/kprobe_events, "p:kprobes/p_SyS_sigsuspend SyS_sigsuspend") failed: Device or resource busy

when Ctrl-C, the tracing not end:

Tracing 337 functions for "SyS_.*"... Hit Ctrl-C to end.
^C
ADDR             FUNC                          COUNT
ffffffff81097230 sys_getrusage                     1
ffffffff81253d10 SyS_fstatfs                       1
ffffffff81092cc0 sys_kill                          1
ffffffff81095170 sys_getpriority                   1
ffffffff8121d030 sys_creat                         1
ffffffff8108bd30 sys_capset                        1
ffffffff81238c00 sys_getcwd                        1
ffffffff8170d4c0 sys_getpeername                   1
ffffffff81267d20 sys_epoll_create                  1
ffffffff81228220 SyS_pipe                          2
ffffffff810af5e0 sys_sched_getaffinity             2
ffffffff8170d6f0 SyS_shutdown                      2
ffffffff8121c2f0 sys_chdir                         2
ffffffff8113f250 sys_seccomp                       2
ffffffff8122fc60 sys_rmdir                         2
ffffffff8121d010 sys_openat                        2
ffffffff81223db0 SyS_readlink                      2
ffffffff810808f0 sys_set_tid_address               3
ffffffff8122fb60 sys_mkdir                         3
ffffffff81097140 sys_setrlimit                     3
ffffffff81228170 SyS_pipe2                         3
ffffffff812269e0 SyS_execve                        3
ffffffff8170da10 sys_sendmmsg                      3
ffffffff8170d470 sys_accept4                       3
ffffffff81097280 sys_umask                         3
ffffffff8123d620 sys_dup2                          4
ffffffff81085f50 sys_exit_group                    5
ffffffff81234d60 sys_ppoll                         6
ffffffff8170d480 sys_accept                        6
ffffffff8126bd10 sys_eventfd2                      7
ffffffff81086120 sys_wait4                         9
ffffffff8170d3b0 sys_bind                          9
ffffffff8122fcb0 sys_unlink                       10
ffffffff81096590 sys_newuname                     12
ffffffff81230d30 sys_rename                       12
ffffffff8170d4b0 sys_getsockname                  13
ffffffff81094f20 sys_setpriority                  13
ffffffff811d1d60 sys_brk                          15
ffffffff81096ec0 sys_getrlimit                    15
ffffffff8123d730 sys_dup                          16
ffffffff8151db10 SyS_getrandom                    17
ffffffff81085e80 SyS_exit                         18
ffffffff8170d4a0 sys_connect                      22
ffffffff81252890 sys_fdatasync                    23
ffffffff8126b0e0 sys_timerfd_settime              24
ffffffff810972b0 sys_prctl                        25
ffffffff81252870 sys_fsync                        26
ffffffff8121f940 sys_pread64                      27
ffffffff8121df40 sys_lseek                        27
ffffffff810923b0 sys_rt_sigprocmask               30
ffffffff8170d160 sys_socketpair                   31
ffffffff8170d610 sys_getsockopt                   32
ffffffff81085fa0 sys_waitid                       32
ffffffff8170d530 sys_setsockopt                   32
ffffffff81232ad0 sys_ioctl                        33
ffffffff81080e00 sys_clone                        35
ffffffff8170d0b0 sys_socket                       35
ffffffff81104fd0 SyS_set_robust_list              36
ffffffff8121bdf0 sys_ftruncate                    36
ffffffff81223c70 sys_newfstatat                   37
ffffffff810f2ae0 sys_nanosleep                    54
ffffffff810ec3e0 sys_kcmp                         58
ffffffff81223c90 sys_readlinkat                  118
ffffffff810f4a00 sys_clock_gettime               122
ffffffff81267df0 SyS_epoll_ctl                   123
ffffffff81234410 SyS_pselect6                    131
ffffffff8121f9f0 sys_pwrite64                    139
ffffffff8170d4d0 SyS_sendto                      182
ffffffff81253cf0 sys_statfs                      235
ffffffff811d3c00 SyS_mprotect                    267
ffffffff812646c0 sys_inotify_add_watch           441
ffffffff81093500 sys_rt_sigaction                514
ffffffff810f1460 SyS_alarm                       675
ffffffff8121c0c0 sys_access                      758
ffffffff81223c60 sys_newlstat                   1392
ffffffff811d0ff0 sys_munmap                     3126
ffffffff811d0450 sys_mmap_pgoff                 3410
ffffffff810341f0 sys_mmap                       3532
ffffffff812313f0 sys_fcntl                      3665
ffffffff81223c50 sys_newstat                   10342
ffffffff811ddd00 sys_madvise                   10452
ffffffff81233000 sys_getdents                  11831
ffffffff8170d810 sys_sendmsg                   13606
ffffffff81223c80 sys_newfstat                  27611
ffffffff8121cff0 sys_open                      33712
ffffffff8121b0e0 sys_close                     33929
ffffffff8121f880 sys_write                     46226
ffffffff81268c10 sys_epoll_wait                67218
ffffffff8121f7c0 sys_read                      89183
ffffffff81234300 sys_select                   161449
ffffffff8170d500 sys_recvfrom                 194010
ffffffff810f3380 sys_setitimer                297644
ffffffff81234c30 sys_poll                     407389
ffffffff8121fbd0 sys_writev                   483703
ffffffff8170dac0 sys_recvmsg                  730047
ffffffff81105df0 sys_futex                    932437
^C
^C
^C
^C
^C
^C
^C
^C

check the tracing pid cpu use:

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
28300 root      20   0  118424  57928  31504 R 100.0  2.8   1:41.93 python

I need to kill -9 PID to end the process.

Find info about the problem, the commit 0d93605b468f6a54980a81fd72952a0f2ade42a4 and issuse #233

bitsect the bcc, find commit reintroduced the deadlock problem:

commit 95728834ee60875b251a2696dd134f1c11d61979
Author: Brenden Blanco <[email protected]>
Date:   Tue Feb 23 13:13:57 2016 -0800

    Use pre-calculated function addresses in funccount

    Hash map updates are not safe in recursive context. Change the
    implementation of funccount to be read-only by converting
    lookup_or_init() to lookup(). When run in interval mode, zero the counts
    table instead of clearing it.

    Signed-off-by: Brenden Blanco <[email protected]>

diff --git a/tools/funccount.py b/tools/funccount.py
index f417087..427e7c1 100755
--- a/tools/funccount.py
+++ b/tools/funccount.py
@@ -65,9 +65,13 @@ BPF_HASH(counts, struct key_t);
 int trace_count(struct pt_regs *ctx) {
     FILTER
     struct key_t key = {};
-    u64 zero = 0, *val;
-    key.ip = ctx->ip;
-    val = counts.lookup_or_init(&key, &zero);
+    u64 *val;
+    // the kprobe pc is slightly after the function starting address, align
+    // back to the start (4 byte alignment) in order to match /proc/kallsyms
+    key.ip = ctx->ip & ~3ull;
+    val = counts.lookup(&key);
+    if (!val)
+        return 0;
     (*val)++;
     return 0;
 }
@@ -81,6 +85,16 @@ else:
 if debug:
     print(bpf_text)
 b = BPF(text=bpf_text)
+counts = b.get_table("counts")
+
+# pre-insert the function addresses into the counts table
+fns = b._get_kprobe_functions(pattern)
+for fn in fns:
+    addr = b.ksymname(fn)
+    if addr == -1:
+        raise Exception("Unknown symbol name %s" % fn)
+    counts[counts.Key(addr)] = counts.Leaf()
+
 b.attach_kprobe(event_re=pattern, fn_name="trace_count")
 matched = b.num_open_kprobes()
 if matched == 0:
@@ -106,10 +120,10 @@ while (1):
         print("%-8s\n" % strftime("%H:%M:%S"), end="")

     print("%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT"))
-    counts = b.get_table("counts")
     for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
+        if v.value == 0: continue
         print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value))
-    counts.clear()
+    counts.zero()

     if exiting:
         print("Detaching...")

debug funccount program, this line change introduce the problem:

-    counts.clear()
+    counts.zero()

back to counts.clear(), not reproduce the deadlock problem. if I change back to counts.clear(), the cache of function addresses don't work.

Step to step into funccount, cause by table.py:

    def __setitem__(self, key, leaf):
        key_p = ct.pointer(key)
        leaf_p = ct.pointer(leaf)
        res = lib.bpf_update_elem(self.map_fd,
                ct.cast(key_p, ct.c_void_p),
                ct.cast(leaf_p, ct.c_void_p), 0)
        if res < 0:
            errstr = os.strerror(ct.get_errno())
            raise Exception("Could not update table: %s" % errstr)

guess the code race bpf program.

Thanks.

oujunli avatar Aug 17 '16 07:08 oujunli

Thanks for the great analysis, we should get this fixed. I've hit this recently where I needed to kill -TERM the process.

I keep getting confused by the ticket title though, this isn't a "deadlock" (which would be a serious kernel bug), it's more a logic error causing an infinite loop. If we don't fix it soon, I'm tempted to change the title to "funccount "SyS_.*" stuck on-CPU".

brendangregg avatar Oct 19 '16 15:10 brendangregg

As a workaround we could not call counts.zero(), ever.

On exit that's easy, move it past the "if exiting" block.

But on interval (-i) it's a bit harder: we'd need to let the counters track cumulative calls, and maintain our own hash of last-seen values so we can calculate the delta between intervals. In fact, avoiding the map zero() might be a good idea anyway for performance. @4ast would that be a reasonable general strategy for any time we have a busy map with many keys (>100) and we want to do, say, per-second intervals?

brendangregg avatar Oct 19 '16 16:10 brendangregg

... maybe bcc's table.py zero() function can stash the current values, and return the delta when values are later fetched.

brendangregg avatar Oct 19 '16 16:10 brendangregg

yes, it cause by an infinite loop, I try to fix table.py by through the keys list once as the items and values do, the PR 676.

thanks.

oujunli avatar Oct 20 '16 01:10 oujunli

The root of issue is the fact than kernel exports SyS_sigsuspend twice and, at the same time, cache malfunctions. So kernel rejects second kprobe on same syscall.

vitalyvch avatar Oct 24 '16 14:10 vitalyvch

Is it kernel bug export SyS_sigsuspend twice or some other reason do this ? @vitalyvch . If it's kernel bug, I think the best way is fixed by kernel.

oujunli avatar Oct 25 '16 02:10 oujunli

@oujunli, I believe we should have workaround in any case. It will reduce head-ache of libbcc users a lot.

vitalyvch avatar Oct 25 '16 09:10 vitalyvch

fixed in 780, close.

oujunli avatar Oct 26 '16 05:10 oujunli