linux性能统计
进程
问题 统计一个进程的实时cpu数据需要用到哪个命令? top
#把ps和top man全看完
ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 4月12 ? 00:00:14 /lib/systemd/systemd --system --deserialize 35
root 2 0 0 4月12 ? 00:00:00 [kthreadd]
root 3 2 0 4月12 ? 00:00:00 [rcu_gp]
root 4 2 0 4月12 ? 00:00:00 [rcu_par_gp]
root 6 2 0 4月12 ? 00:00:00 [kworker/0:0H-kb]
root 9 2 0 4月12 ? 00:00:00 [mm_percpu_wq]
root 10 2 0 4月12 ? 00:00:00 [ksoftirqd/0]
root 11 2 0 4月12 ? 00:00:03 [rcu_sched]
root 12 2 0 4月12 ? 00:00:00 [migration/0]
ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 225804 9612 ? Ss 4月12 0:15 /lib/systemd/systemd --system --deserialize 35
root 2 0.0 0.0 0 0 ? S 4月12 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 4月12 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 4月12 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 4月12 0:00 [kworker/0:0H-kb]
root 9 0.0 0.0 0 0 ? I< 4月12 0:00 [mm_percpu_wq]
root 10 0.0 0.0 0 0 ? S 4月12 0:00 [ksoftirqd/0]
root 11 0.0 0.0 0 0 ? I 4月12 0:03 [rcu_sched]
root 12 0.0 0.0 0 0 ? S 4月12 0:00 [migration/0]
ps的CPU信息是累积的cpu时间,%cpu是平均值
可以使用-o打印自己想要的参数
练习:
统计某一个进程在20s CPU和mem内存,病最后空出一行统计平均CPU和平均内存
pid会发生变化
grep要用line-buffered才有输出
perf_get ()
{
top -b -d 1 -n 20 |
grep --color=auto --line-buffered -i firefox$ |
awk '
BEGIN{print "cpu mem"}{cpu+= $9;mem+=$10;print $9,$10}
END{print "";print cpu/NR,mem/NR}
'
}
网络
netstat -tlnp
统计不同端口在不同状态下的个数
netstat_count()
{
netstat -tn |
awk 'NR>2{print $4,$6}'|
awk -F":" '{print $2}'|
sort -n|
uniq -c|
sort -nr|
awk '
BEGIN{print "port""\t""status""\t\t""num"}
{print $2"\t"$3"\t"$1}
';
}