Linux基础——top用法

 

# 批处理模式
# 参数"-o":按照指定内容降序输出
top -b -o %CPU |head
top -b -o %MEM | head -20

# 以下方法实现多个进程PID用逗号分割,同屏输出进程详情
top -H -p 9,16

# 只输出PID数字部分
top -b -n 1 -o %MEM | awk 'NR>7 {print $1}'

# 批量杀死java进程
for pid in $(top -b -n 1 -o %MEM | awk 'NR>7 {print $1}'); do kill -15 $pid; done

# 使用 tr 将换行符替换为逗号,并去除末尾可能的逗号
top -H -p $(pgrep ksoftirqd | tr '\n' ',' | sed 's/,$//')
‌解释‌:
pgrep ksoftirqd:获取所有 ksoftirqd 的 PID。
tr '\n' ',':将每行之间的换行符替换为逗号,例如 9\n16 变为 9,16。

# -d ',' 表示用逗号分隔输出的 PID
top -H -p $(pgrep -d ',' ksoftirqd)

举例:

深入分析:为什么 ksoftirqd 会占用高 CPU?
结合你之前提到的“NET-RX/NET_TX 软中断高”,ksoftirqd 高占用意味着:

‌软中断处理不过来‌:硬中断(Hard IRQ)快速触发,将数据包交给软中断队列,但软中断处理速度跟不上,导致 ksoftirqd 线程被频繁唤醒以处理积压的软中断任务。
‌单核瓶颈‌:如果只有 ksoftirqd/0 (PID 9) 高,说明网卡中断主要打在 CPU 0 上,且 CPU 0 处理不过来。
‌优化建议回顾‌:

‌检查中断分布‌:cat /proc/interrupts | grep eth0,看是否所有中断都在 CPU 0。
‌启用 RPS‌:如果网卡队列少,启用 RPS 将软中断处理分散到其他 CPU。
bash
echo ff > /sys/class/net/eth0/queues/rx-0/rps_cpus # 假设 8 核
‌调整 Netdev Budget‌:
bash
sysctl -w net.core.netdev_budget=600


检查项命令/操作预期结果
‌中断均衡‌ cat /proc/interrupts | grep eth0 各 CPU 列数值分布均匀,无单核极高
‌软中断分布‌ watch -n 1 'grep NET_RX /proc/softirqs' 各 CPU 增量基本一致,无单核飙升
‌RPS 启用‌ cat /sys/class/net/eth0/queues/rx-0/rps_cpus 非 0 值,掩码覆盖可用 CPU
‌网卡队列‌ ethtool -l eth0 Combined 队列数尽可能匹配 CPU 核数
‌Ksoftirqd‌ top -H -p $(pgrep ksoftirqd) CPU 占用率下降,不再持续占满单核
 

image

 

image

 

posted @ 2026-07-14 09:13  gkhost  阅读(11)  评论(0)    收藏  举报