1、 cpu负载相关的工具
[root@ chenc01 ~]# uptime
11:19:34 up 0 min, 2 users, load average: 0.00, 0.00, 0.00
11:19:34 : 当前时间
up 0 min 系统运行时间
2 users: 当前用户登录数
[root@ chenc01 ~]# w
11:20:38 up 1 min, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 11:18 1:52 0.00s 0.00s -bash
root pts/1 10.0.0.1 11:19 0.00s 0.00s 0.00s w
load average: 0.00, 0.00, 0.00 系统负载,也就是任务队列的平均长度。三个数值分别表示 1分钟,5分钟,15分钟前到现在的平均值;
举例:
服务器A DELL R720 load average:1.11, 0.08, 0.01 1核;
服务器B DELL R730 load average: 5.25, 7.22, 60.1 1核;
服务器C DELL R720 load average:10.15, 1011, 10.01 4核;
答案: 服务器B负载过高;
经验: 单核心,1分钟内系统平均负载不超过3. 4核心不要超过12;
从高往低说明负载是下降的;
从低往高的说明负载上升的;
1)问题1.: 找出系统中占用CPU最多的进程;
2)问题2: cpu一直飚高,如何处理;
-
top查找出哪个进程消耗的CPU高(top -c)
-
top -h -p查找出哪个线程消耗的cpu高(top -h -p pid)
这个命令就能显示刚刚找到的进程的所有线程的资源消耗情况。 -
printf%x进行pid的进制转换
找到CPU负载高的线程pid 8627, 把这个数字转换成16进制,21B3(10进制转16进制,用linux命令: printf %x 8627) -
jstack记录进程的堆栈信息
执行jstack -l pid,拿到进程的线程dump文件。这个命令会打出这个进程的所有线程的运行堆栈。 -
找出消耗CPU最高的线程信息
搜索“21B3”,就是搜一下16进制显示的线程id。搜到后,下面的堆栈就是这个线程打出来的。
[root@ chenc01 ~]# top
top - 11:22:13 up 3 min, 2 users, load average: 0.07, 0.02, 0.01
Tasks: 73 total, 1 running, 72 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1004136k total, 109676k used, 894460k free, 8888k buffers
Swap: 786428k total, 0k used, 786428k free, 26616k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7 root 20 0 0 0 0 S 0.3 0.0 0:00.69 events/0
1 root 20 0 19344 1524 1228 S 0.0 0.2 0:00.64 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 stopper/0
# 按照实际使用cpu,从大到小来排序显示所有进程
[root@ chenc01 ~]# ps -aux --sort -pcpu |more
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1171 1.0 0.1 110356 1152 pts/1 R+ 11:23 0:00 ps -aux --sort -
pcpu
root 1 0.2 0.1 19344 1524 ? Ss 11:18 0:00 /sbin/init
root 7 0.2 0.0 0 0 ? S 11:18 0:00 [events/0]
root 2 0.0 0.0 0 0 ? S 11:18 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 11:18 0:00 [migration/0]
root 4 0.0 0.0 0 0 ? S 11:18 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S 11:18 0:00 [stopper/0]
root 6 0.0 0.0 0 0 ? S 11:18 0:00 [watchdog/0]
root 8 0.0 0.0 0 0 ? S 11:18 0:00 [events/0]
root 9 0.0 0.0 0 0 ? S 11:18 0:00 [events_long/0]
root 10 0.0 0.0 0 0 ? S 11:18 0:00 [events_power_ef]
# -pcpu 可以显示绝对路径,方便找出木马进程
2、查看CPU信息
[root@ chenc01 ~]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 158
model name : Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz
stepping : 10
microcode : 180
cpu MHz : 2303.998
cache size : 8192 KB
physical id : 0
siblings : 1
core id : 0
cpu cores : 1
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
问题: ps 中的VIRT , RES, HSR?
1、VIRT:virtual memory usage 虚拟内存;
1)进程“需要的”虚拟内存大小,包括进程使用的库、代码、数据等;
2)假如进程申请100m的内存,但实际只使用了10m,那么它会增长100m,而不是实际的使用;
2、RES:resident memory usage 常驻内存;
1)进程当前使用的内存大小,但不包括swap out;
2)包含其他进程的共享;
3)如果申请100m的内存,实际使用10m,它只增长10m,与VIRT相反;
4)关于库占用内存的情况,它只统计加载的库文件所占内存大小;
3、SHR:shared memory 共享内存;
1)除了自身进程的共享内存,也包括其他进程的共享内存;
2)虽然进程只使用了几个共享库的函数,但它包含了整个共享库的大小;
3、 mpstat
[root@ chenc01 ~]# mpstat
Linux 2.6.32-642.el6.x86_64 (chenc01.localdomain) 11/29/2019 _x86_64_ (1 CPU)
11:27:06 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
11:27:06 AM all 0.12 0.00 0.51 0.35 0.00 0.07 0.00 0.00 98.95
问题: 你们公司用的操作系统是什么?
low:我们用的centos
high:我们用的是centos6.7的版本
great:我们用的centos, 系统版本6.7,内核用的是2.6.32-431 64位的
%usr:用户控件CPU使用占比
%nice:低优先级进程使用CPU占比,nice大于0
%sys:内核空间CPU使用占比
%iowait cpu:等待Io占比
%irq cpu:处理硬中断占比
%soft:处理软中断的cpu占比
%steal %guest 与虚拟机有关
%idle cpu:空间时间占比
4、free
[root@ chenc01 ~]# free -m
total used free shared buffers cached
Mem: 980 107 873 0 8 26
-/+ buffers/cache: 72 908
Swap: 767 0 767
[root@ chenc01 ~]# cat /proc/meminfo
MemTotal: 1004136 kB
MemFree: 894260 kB
Buffers: 8924 kB
Cached: 26804 kB
SwapCached: 0 kB
Active: 16056 kB
Inactive: 25780 kB
Active(anon): 6132 kB # 活跃内存
Inactive(anon): 216 kB # 非活跃的内存
Active(file): 9924 kB
Inactive(file): 25564 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 786428 kB
# 查看与io运行相关的工具
[root@ chenc01 ~]# tune2fs -l /dev/sda1 |grep size
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Block size: 1024 # 1个字节,一个扇区512字节
Fragment size: 1024
Flex block group size: 16
Inode size: 128