apisix-profiler icon indicating copy to clipboard operation
apisix-profiler copied to clipboard

Fix: folded format & lookup_batch error

Open chensunny opened this issue 11 months ago • 0 comments

环境

Linux  5.15.0-1047-gcp #55~20.04.1-Ubuntu SMP Wed Nov 15 11:38:25 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

当前存以下几个问题

  1. Makefile 路径不对,导致无法正常编译

  2. 当使用 ford 格式的时候,如果 nr_kip = 0; std::size_t 类型会出现异常,出现 j -1 则会发生下溢(underflow); 从而导致死循环!直到出现错误内核抛堆栈异常。 解决方案:std::size_t 这个类型 改成跟其他是一致的 int 类型 。

  3. 当前使用的是 batch_map_ops=true 批量获取的方式,这里存在几个问题: 3.1 使用 bpf_map_lookup_batch 的异常, 参数 keys 和 vals 的参数有些问题

  • @param keys pointer to an array large enough for count keys
  • @param values pointer to an array large enough for count values

解决方案: 参考: https://github.com/iovisor/bcc/blob/6fbcf16b4a4222b2ea92ea32ceed2532a28b8080/libbpf-tools/map_helpers.c#L58

改成

keys +  n_read * sizeof(stack_key)

3.2 使用 bpf_map_lookup_batch 的异常,没有添加 BPF_F_LOCK,counts map 导致 value 出现 0 的情况,

image

但是直接用bpftool

 ./bpftool map dump  name counts

显示正常的,因为 bpftool 使用的是 iter 遍历的方式 image (https://github.com/libbpf/bpftool/blob/3e6f814e534421c4fd638c1bffc7e8318da78d16/src/map.c#L823)

使用 GDB attach bpf_map_lookup_batch 调用出来就是会出现 0 的情况

image

深入内核代码 https://elixir.bootlin.com/linux/v5.15/source/kernel/bpf/hashtab.c#L1585

image

https://elixir.bootlin.com/linux/v5.15/source/kernel/bpf/hashtab.c#L1722

同时遍历的时候,也是有 添加锁的 image

https://elixir.bootlin.com/linux/v5.15/source/kernel/bpf/syscall.c#L1262

解决方案:

添加batch_opts 之后测试正常


    // BPF_F_LOCK 
       const struct bpf_map_batch_opts batch_opts = {
         .sz         = sizeof(struct stack_key),  
        .elem_flags = BPF_F_LOCK, 
    };
    

测试结果 ./profile -K -p129677

image

chensunny avatar Mar 19 '24 05:03 chensunny