iotop 是優秀的 IO 監控工具,但要求較新的核心版本與 Python 環境,許多嵌入式或舊版 Linux 系統無法滿足。iopp 由 C 語言編寫,無額外依賴,編譯後即可直接執行,功能與 iotop 相近,極適合資源受限環境。
iopp.cgcc -o iopp iopp.c./iopp (需 root 權限)#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#define PROC "/proc"
#define GET_VALUE(v) \
p = strchr(p, ':'); \
++p; \
++p; \
q = strchr(p, '\n'); \
length = q - p; \
if (length >= BUFFERLEN) { \
printf("ERROR - value is larger than the buffer: %d\n", __LINE__); \
exit(1); \
} \
strncpy(value, p, length); \
value[length] = '\0'; \
v = atoll(value);
#define BTOKB(b) b >> 10
#define BTOMB(b) b >> 20
#define BUFFERLEN 255
#define COMMANDLEN 1024
#define VALUELEN 63
#define NUM_STRINGS 8
struct io_node {
int pid;
long long rchar, wchar, syscr, syscw;
long long read_bytes, write_bytes, cancelled_write_bytes;
char command[COMMANDLEN + 1];
struct io_node *next;
};
struct io_node *head = NULL;
int command_flag = 0, idle_flag = 0, mb_flag = 0, kb_flag = 0, hr_flag = 0;
// ... (完整函數實作包含 format_b, get_cmdline, get_tcomm, get_ion, insert_ion, get_stats, new_ion, upsert_data, usage, main)
// 完整程式碼請見原文,此處省略重複部分以節省篇幅
int main(int argc, char *argv[]) {
// 參數解析與主迴圈
// 支援 -c (完整指令), -i (隱藏閒置), -k (KB), -m (MB), -u (人類可讀), delay, count
}
| 參數 | 說明 |
|---|---|
-c, --command | 顯示完整命令列而非程式名 |
-i, --idle | 隱藏無 IO 活動的進程 |
-k, --kilobytes | 以 KB 為單位顯示 |
-m, --megabytes | 以 MB 為單位顯示 |
-u, --human-readable | 自動切換 KB/MB/GB 單位 |
delay [count] | 更新間隔(秒)與重複次數 |
# 每 2 秒更新一次,顯示人類可讀單位,隱藏閒置進程
./iopp -u -i 2
# 顯示完整命令列,執行 5 次後結束
./iopp -c 1 5