Skip to content

Instantly share code, notes, and snippets.

@shirou
Last active June 30, 2022 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirou/bfd3458f97890fead45234559b5d2384 to your computer and use it in GitHub Desktop.
Save shirou/bfd3458f97890fead45234559b5d2384 to your computer and use it in GitHub Desktop.

top

ubuntu 20.04のtopコマンドは procps-ngを使用している。(man topの一番下に書いてある)

procps-ngのソースコードはこれ https://gitlab.com/procps-ng/procps/

/proc/stat を定義し、 https://gitlab.com/procps-ng/procps/-/blob/newlib/proc/stat.c#L39 stat_read_failed 関数で読み込んでいる https://gitlab.com/procps-ng/procps/-/blob/newlib/proc/stat.c#L607

CPUがbusyかどうかの判定はこのあたりに定義がコメントで書いてある。 https://gitlab.com/procps-ng/procps/-/blob/newlib/proc/stat.h#L63

STAT_TIC_SUM_USER,            //  ull_int        derived from USER + NICE tics
STAT_TIC_SUM_SYSTEM,          //  ull_int        derived from SYSTEM + IRQ + SOFTIRQ tics
STAT_TIC_SUM_IDLE,            //  ull_int        derived from IDLE + IOWAIT tics
STAT_TIC_SUM_BUSY,            //  ull_int        derived from SUM_TOTAL - SUM_IDLE tics
STAT_TIC_SUM_TOTAL,           //  ull_int        derived from sum of all 10 tics

実際のコードはここ https://gitlab.com/procps-ng/procps/-/blob/newlib/proc/stat.c#L396-427

node_exporter

updateStat reads /proc/stat とコメントに書いてある https://github.com/prometheus/node_exporter/blob/69a3f73a9b1e1452cb3a9e62f969f21a1baaeade/collector/cpu_linux.go#L279

これからみるに、node_exporterは/proc/statファイルの情報を出すだけで、topが出しているようなtotal/busyという情報は計算していないのでは?

(ちなみに/proc/cpuinfoはcpuのcore idなどの情報だけで、使用状況とかは載っていない)

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