Skip to content

fix(syscall-stat): stop skipping syscall key 0 during stats traversal and no top mode - #119

Open
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/syscall-stat-first-key
Open

fix(syscall-stat): stop skipping syscall key 0 during stats traversal and no top mode#119
yuKing123-king wants to merge 1 commit into
DKapture:mainfrom
yuKing123-king:fix/syscall-stat-first-key

Conversation

@yuKing123-king

@yuKing123-king yuKing123-king commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

修复了两个测试过程中发现的 Bug

  • Bug 1: 修复系统调用号 0 (read) 统计丢失问题

    • 原因:原逻辑中 key 初始化为 0,且使用 bpf_map_get_next_key 查找下一个 key。第一个 key 就是 0,get_next_key 会跳过它直接找下一个,后续使用nxt_key作为真正第一个开始查找的key,导致 系统调用号为0的read syscall 永远不被统计。
    • 修复:调整遍历逻辑,确保从第一个 key 开始完整遍历。
    • 代码如下
      u32 key = 0, nxt_key;
      u32 total = 0;
      std::vector<std::pair<u32, info>> stats; 
      
      // 先尝试获取第一个 key
      if (bpf_map_get_next_key(stats_fd, nullptr, &key) != 0) {
          return; // Map is empty
      }
      
      do {
          info sys_stat;
          if (bpf_map_lookup_elem(stats_fd, &key, &sys_stat) != 0) {
              key = nxt_key;
              continue;
          }
      
          if (key >= sizeof(sys_tbl) / sizeof(sys_tbl[0])) {
              break; 
          }
      
          if (sys_stat.cnt > 0) {
              stats.push_back({key, sys_stat});
              total += sys_stat.cnt;
              
              // Clear the stat after reading
              memset(&sys_stat, 0, sizeof(sys_stat));
              bpf_map_update_elem(stats_fd, &key, &sys_stat, BPF_ANY);
          }
          
          // Get next key for the next iteration
          if (bpf_map_get_next_key(stats_fd, &key, &nxt_key) != 0) {
              break; // No more keys
          }
          key = nxt_key;
      } while (true);
  • Bug 2: 修复 --top / -t 动态刷新功能无效问题

    • 原因:虽然底层动态刷新逻辑已实现,但在 parse_args 参数解析函数中遗漏了对 -t--top 选项的处理。
    • 修复:在 parse_args 中添加了对应的 case 分支,使终端能正确识别并开启动态刷新模式。

@yuKing123-king
yuKing123-king force-pushed the fix/syscall-stat-first-key branch from 3b8787c to 2d9bbe7 Compare August 4, 2026 08:24
@yuKing123-king yuKing123-king changed the title fix(syscall-stat): stop skipping syscall key 0 during stats traversal fix(syscall-stat): stop skipping syscall key 0 during stats traversal and no top mode Aug 4, 2026
@yuKing123-king
yuKing123-king force-pushed the fix/syscall-stat-first-key branch 2 times, most recently from f411ec9 to e379b4d Compare August 12, 2026 08:27
@github-actions

github-actions Bot commented Aug 12, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 5dabdbb)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ No major issues detected

… and no top mode

Signed-off-by: Wang Yu <wangyu6@uniontech.com>
@yuKing123-king
yuKing123-king force-pushed the fix/syscall-stat-first-key branch from e379b4d to 5dabdbb Compare August 12, 2026 08:34
@yus-cpu

yus-cpu commented Aug 12, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
Recommended focus areas for review

Loop Termination Bug

在成功处理一条统计记录后,第 321 行将 bpf_map_get_next_key 的返回值赋给 lret,而 while 循环条件检查的是 retret 在该路径上从未被更新,导致循环无法通过正常路径检测到"没有更多 key"的情况。当前依赖一个间接兜底路径终止循环:重新访问上一个 key(其 cnt 已被清零),走 sys_stat.cnt == 0 分支时再次调用 bpf_map_get_next_key 并更新 ret 才能退出。如果 BPF 程序在两次迭代之间又对该 key 的计数器进行了递增,兜底路径将失效,同一 key 会被重复处理,造成统计数据重复累加。应将第 321 行的 lret 改为 ret

已更改

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit 5dabdbb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants