Introduction to BPF Performance Tools - Mars Cheng

tags: COSCUP2020 入門 TR214

歡迎來到 https://hackmd.io/@coscup/2020 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。

請從這裡開始
slides: https://docs.google.com/presentation/d/1vjtrc5u33x4Oir06sJLIKuNfRDZIfjNOd7Xr04IhJ7k/

Who am I

  • TLKH Study club
  • interesting in BPF performance

What is BPF

extend BPF

  • 2011 classic BPF JIT
  • 2012 seccomp systemcall 對 file descriptor 限制的範圍
  • 2013 eBPF merge 對設計簡單的 VM Instructions 做改善
  • 2014 開 systemcall to user space
  • 2015-2017 perf_event added
  • can replace TCP 阻塞算法換 kernel module

專注在 performance 應用
BPF 通常比其他有效率

  • trace-cmd
    trace-cmd -> kernel -> trace.dat
    post-processing -> result
  • bpf
    bpf progs-> kernel -> reult1(caculated by step 2)

perffomance is the currency of computing
MIT 6.172

較輕小 bpf 可以跑在 online 上

  • bpf is programmable

    • 一般讀希望在 produce 放 kernel 的程式
    • vpf 提供 sub kernel 驗證程式
  • Tools for BPF performance

寫起來跟 awk 類似

bpftrace -e "kretprobe:vfs_read{@bytes=hits(retval);}"

type:identifier1 讀多少 bytes 的次數

  • 對 CPU Bound 程式分析:分析最常被呼叫的 Function
    bpftrace -e 'profile:hz:49 /comm == "wc_nvive" / {@[ustack] = count();}'

舉例 1

  • producer - Consumer Problem

    • produce list of words from files
    • update word frequency
    • find the max frequency of some word
  • Dessign Decisions

    • size of buffer
      • 太小會塞爆
      • lock free 不容易設計
    • 各開多少 thread ?
  • Naive Producer-Consumer Solution

  • 分析

    • cpu 用了多少時間
    • 或是 cpu 被 block 多少時間
      • 找 off cpu
  • Off-CPU analysis

    • 常見情形
      • 主動 call system call
      • 被中斷等等
    • 偵測方法
      • 傳統方法全部列出來
      • bpf 直接看 kernel status
        • tgid the thread ID of process
        • 看他 block 的時間和被喚回的時間差
  • Producer 的 wait 不真正影響時間

  • consumer

    • 何時 wait
      • queue 空的時候?
        • Use HeatMap to know

舉例2

  • gzip 壓縮一個檔案
    • 哪部分用了最多的 memory
    • 找什麼時候觸發 page fault
      • 累積越多次就是觸發越多的路徑
      • 可以知道哪部分佔最多
  • 有時候 malloc 最多的地方可能不是 pagefault 最多的地方
    • memory leak
    • 要了沒用
      • malloc 被使用時去讀他的 stack trace
        • 把要多少 bytes 累加起來

How to use bpf

  • really know your problem is
  • USE CPU and OFF CPU analysis to have a bird's eye view
    • 觀察 system call : clone 何時被執行
  • Have basic knowledge of kernel and target SW
  • USE uprobe/probe/USDT to realize behaviors of a function
    • hist heatmap
  • Browse the tools

  • why bpf 做成 VM 跟有安全檢查的 kernel module 有啥不一樣
    • 有 VM 的基本好處 sandbox 也可以直接對應到 machine code
    • VM 通常是不能 load 不太會 crash
  • Off cpu queue empty 改進方法

Select a repo