bcc icon indicating copy to clipboard operation
bcc copied to clipboard

stackcount failure

Open protik77 opened this issue 5 years ago • 11 comments

Also I am trying the code listing in page 101 of the book "BPF Performance Tools". The code is,

stackcount -f -P -D 10 ktime_get > out.stackcount01.txt

A file named out.stackcount01.txt is created but it reads,

sequence item 1: expected str instance, bytes found

The OS is a SLES 15 SP1 with kernel version 4.12.14-197.34-default. The Python version is 3.6.10.

protik77 avatar Mar 12 '20 21:03 protik77

This probably a python3 comparability issue. Could you help debug and submit a pull request?

yonghong-song avatar Mar 14 '20 00:03 yonghong-song

I would love to, but I still a newbie by all means. I will try to fix it and submit a pull request if I can resolve the issue.

protik77 avatar Mar 22 '20 03:03 protik77

@protik77

  1. can you please try without -f option just to narrow down the issue.

if that doesn't give the error then 2. can you try this stack counr version on my branch

this has diff

@@ -356,7 +356,7 @@ class Tool(object):
                         reversed(user_stack)] + \
                         (self.need_delimiter and ["-"] or []) + \
                         [b.ksym(addr).decode('utf-8', 'replace') for addr in reversed(kernel_stack)]
-                    print("%s %d" % (";".join(line), v.value))
+                    print("%s %d" % (";".join(str(line)), v.value))
                 else:
                     # print multi-line stack output
                     for addr in kernel_stack:

if you confirm I an create pull request.

Explanation

lets start with your error

sequence item 1: expected str instance, bytes found

is type mismatch error mostly this error occurs in .join()

item 1 indicates its argument passed to join is issue. ie, in our case line is its line

although this is just patch not addressing real issue why line may have byte type. may be you can print and check whats exact type and value of line in your case.

devidasjadhav avatar Mar 28 '20 09:03 devidasjadhav

  1. Yes, removing -f option provides the right output.
  2. I tried the stackcount branch, but it puts ; between every character. For example,
[;';d;o;c;k;e;r;-;c;o;n;t;a;i;n;e;';,; ;';r;u;n;t;i;m;e;.;g;o;e;x;i;t;';,; ;';g;i;t;h;u;b;.;c;o;m;/;c;o;n;t;a;i;n;e;r;d;/;c;o;n;t;a;i;n;e;r;d;/;v;e;n;d;o;r;/;g;o;o;g;l;e;.;g;o;l;a;n;g;.;o;r;g;/;g;r;p;c;/;i;n;t;e;r;n;a;l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;n;t;';,; ;';k;t;i;m;e;_;g;e;t;';] 1

protik77 avatar Mar 30 '20 19:03 protik77

thanks. here is work temporary around.

diff --git a/tools/stackcount.py b/tools/stackcount.py
index 1691ef68..2e811ef2 100755
--- a/tools/stackcount.py
+++ b/tools/stackcount.py
@@ -356,7 +356,7 @@ class Tool(object):
                         reversed(user_stack)] + \
                         (self.need_delimiter and ["-"] or []) + \
                         [b.ksym(addr).decode('utf-8', 'replace') for addr in reversed(kernel_stack)]
-                    print("%s %d" % (";".join(line), v.value))
+                    print("%s %d" % (b"".join(str(line)), v.value))
                 else:
                     # print multi-line stack output
                     for addr in kernel_stack:

output

$ sudo ./tools/stackcount.py -f -P -D 10 ktime_get
[u'DOM Worker', u'[unknown]', u'[unknown]', u'[unknown]', u'[unknown]', u'entry_SYSCALL_64_after_hwframe', u'do_syscall_64', u'syscall_trace_enter', u'__seccomp_filter', u'__bpf_prog_run32', u'___bpf_prog_run', u'apic_timer_interrupt', u'smp_apic_timer_interrupt', u'hrtimer_interrupt', u'__hrtimer_run_queues', u'tick_sched_timer', u'ktime_get'] 1
[u'Timer', u'[unknown]', u'[unknown]', u'entry_SYSCALL_64_after_hwframe', u'do_syscall_64', u'ksys_write', u'vfs_write', u'new_sync_write', u'pipe_write', u'__wake_up_common_lock', u'_raw_spin_unlock_irqrestore', u'apic_timer_interrupt', u'smp_apic_timer_interrupt', u'hrtimer_interrupt', u'__hrtimer_run_queues', u'tick_sched_timer', u'ktime_get'] 1
[u'swapper/2', u'secondary_startup_64', u'start_secondary', u'cpu_startup_entry', u'do_idle', u'cpuidle_enter', u'cpuidle_enter_state', u'ret_from_intr', u'do_IRQ', u'irq_exit', u'__softirqentry_text_start', u'blk_done_softirq', u'scsi_io_completion', u'scsi_end_request', u'mq_flush_data_end_io', u'blk_flush_complete_seq', u'blk_rq_init', u'ktime_get'] 1

need to figure out why we receive single characters instead of line.

devidasjadhav avatar Mar 31 '20 04:03 devidasjadhav

root-cause

in print("%s %d" % (";".join(line), v.value)) line is expected to be list of 'str' type.

so with following patch we are ensuring any non string char is converted to string. this is preventing probable errors mentioned in this issue.

--- a/tools/stackcount.py
+++ b/tools/stackcount.py
@@ -356,7 +356,7 @@ class Tool(object):
                         reversed(user_stack)] + \
                         (self.need_delimiter and ["-"] or []) + \
                         [b.ksym(addr).decode('utf-8', 'replace') for addr in reversed(kernel_stack)]
-                    print("%s %d" % (";".join(line), v.value))
+                    print("%s %d" % (b";".join([str(word) for word in line]), v.value))
                 else:
                     # print multi-line stack output
                     for addr in kernel_stack:

@protik77 can you try stackcount from strcnt branch it has above diff applied.

devidasjadhav avatar Mar 31 '20 05:03 devidasjadhav

I tried the strcnt branch one, now the out.stackcount01.txt file reads,

sequence item 0: expected a bytes-like object, str found

protik77 avatar Mar 31 '20 17:03 protik77

I have changed code. cause of this was b before `";". from python bug 24892

>>> x = b'foo'
>>> y = b'barbaz'
>>> x.join(y)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    x.join(y)
TypeError: sequence item 0: expected a bytes-like object, int found

devidasjadhav avatar Apr 01 '20 07:04 devidasjadhav

Now the output looks like this,

swapper/10;secondary_startup_64;start_secondary;cpu_startup_entry;do_idle;tick_nohz_idle_exit;apic_timer_interrupt;smp_apic_timer_interrupt;hrtimer_interrupt;clockevents_program_event;ktime_get 1
prometheus;runtime.goexit;github.com/prometheus/prometheus/scrape.(*scrapeLoop).run;runtime.selectgo;runtime.mcall;runtime.park_m;runtime.schedule;runtime.findrunnable;runtime.runqsteal;runtime.runqgrab;runtime.usleep;entry_SYSCALL_64_after_hwframe;do_syscall_64;sys_nanosleep;hrtimer_nanosleep;do_nanosleep;hrtimer_start_range_ns;ktime_get 1
swapper/2;secondary_startup_64;start_secondary;cpu_startup_entry;do_idle;cpuidle_enter_state;apic_timer_interrupt;smp_apic_timer_interrupt;irq_exit;__tick_nohz_idle_enter;tick_nohz_stop_sched_tick;hrtimer_start_range_ns;clockevents_program_event;ktime_get 1
git-remote-http;[unknown];apic_timer_interrupt;smp_apic_timer_interrupt;hrtimer_interrupt;clockevents_program_event;ktime_get 1

protik77 avatar Apr 03 '20 17:04 protik77

correct now you can use flamegraph. if it works close the issue

devidasjadhav avatar Apr 04 '20 07:04 devidasjadhav

Ubuntu 20.04 LTS with python 3.8.10 (default python3) faces related issue. Ubuntu 22.04 LTS and Ubuntu 24.04 LTS doesn't

$ sudo /sbin/profile-bpfcc -afd -p pgrep $MY_PROCESS --stack-storage-size 2097152 > profile.stacks

Traceback (most recent call last): File "/sbin/profile-bpfcc", line 342, in print("%s %d" % (b";".join(line).decode('utf-8', 'replace'), v.value)) TypeError: sequence item 10: expected a bytes-like object, str found

ami-GS avatar Aug 20 '24 04:08 ami-GS