CPU负载观察及调优方法

红帽6实现无滴答 tick less

interrupt-driven

由软中断进行驱动

在SMP多CPU架构中,传统上多CPU对于内存的访问是总线方式。是总线就会存在资源争用和一致性问题,而且如果不断的增加CPU数量,总线的争用会愈演愈烈,这就体现在4核CPU的跑分性能达不到2核CPU的2倍,甚至1.5倍!理论上来说这种方式实现12core以上的CPU已经没有太大的意义。
Intel的NUMA解决方案,放弃总线的访问方式,将CPU划分到多个Node中,每个node有自己独立的内存空间。各个node之间通过高速互联通讯,通讯通道被成为QuickPath Interconnect即QPI


numactl控制命令,实现策略控制

-cpunodebind=nodes 将cpu跟某个node绑定,不让cpu跟访问其他node

-physcpubind=cpus 将进程和cpu完成绑定

--show显示当先使用的策略


numastat 显示命令

-p 查看某个特定进程的内存分配

-s node0 查看node0和全部的,主要用于排序

[root@localhost ~]# numastat 显示有几个node
                           node0
numa_hit                  283715 表示cpu到这个内存node(节点)找数据命中多少个
numa_miss                      0   没命中个数,命中过高需要绑定进程到特定cpu
numa_foreign                   0   被非本地cpu访问次数
interleave_hit             14317
local_node                283715
other_node                     0

numad 用户空间级别的守护进程,能够提供策略,通过观察cpu进程的状况,自动把某个进程绑定到特定的cpu上,实现自我优化,自我管理。


CPU affinity:cpu婚亲关系

numactl

numad

numastat


teskset:绑定进程至某cpu上

  mask:

     0x0000 0001   (16进制)

     0001 :表示第0号CPU

 

     0x0000 0003

     0011: 表示0号和1号CPU


     0x0000 0005

     0101:表示第0号和2号CPU


     0x0000 0007

列如把pid为101的进程绑定在3号cpu上

0100=4

#taskset -p  0x00000004 101

or

#taskset -p -c 3 101


绑定在0号和1号cpu上

#taskset -p -c 0,1 101


绑定在0-2号和7号cpu上

#taskset -p -c 0-2,7 101


[root@localhost ~]# cat /proc/irq/0/smp_affinity
ffffffff,ffffffff,ffffffff,ffffffff 表示0号中断可以运行到任何cpu上

#  echo "00000000,00000000,00000000,0000001"  >  /proc/irq/0/smp_affinity  
将0号中断绑定到 0号cpu上

echo CPU_MASK >/proc/irq/<irq number>/smp_affinity


应该将中断绑定至那些非隔离的CPU上,从而避免那些隔离的cpu处理中断程序:


查看运行状态命令

sar -q  1


09:32:17 AM   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15

              运行队列长度 

09:32:18 AM         0        96      0.00      0.00      0.00
09:32:19 AM         0        96      0.00      0.00      0.00

top

w

uptime

vmstat 1 5


查看cpu状态命令

mpstat 1 2 显示每一颗cpu的平均使用率,-P指定查看哪颗CPU

[root@localhost ~]# mpstat -P 0 1  只显示0号cpu
%usr用户空间的, %sys内核空间的,%sysIO等待的,%irq硬处理中断的,%soft软中断的,

%steal被虚拟机偷走的,%guest虚拟机使用的,%idle空闲的

09:35:33 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
09:35:34 AM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
09:35:35 AM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
09:35:36 AM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

mpstat -I CPU 1 显示cpu对中断处理的

sar -P ALL 1 2

iostat -c 1 2

[root@localhost ~]# cat /proc/stat

dstat  --top-cpu 查找谁最消耗cpu

          --top-cputime查找谁消耗cpu时间最长的

          --top-io谁消耗IO最多

          --top-latency哪个进程是最大延迟

          --top-mem 谁用了最多的内存

[root@localhost ~]# dstat --top-latency --top-io --top-mem  --top-cputime  --top-cpu

[root@localhost ~]# dstat -c   cpu使用率

[root@localhost ~]# sar -w 1每秒钟上下文切换的次数和进程创建的平均次数



CPU域的划分


[root@localhost ~]# mkdir /cpusets
[root@localhost ~]# vim /etc/fstab


cpusets                 /cpusets                cpuset  defaults        0 0

[root@localhost ~]# ls -l /cpusets/
total 0
--w--w--w-. 1 root root 0 Jun  5 10:39 cgroup.event_control
-rw-r--r--. 1 root root 0 Jun  5 10:39 cgroup.procs
-rw-r--r--. 1 root root 0 Jun  5 10:39 cpu_exclusive
-rw-r--r--. 1 root root 0 Jun  5 10:39 cpus
-rw-r--r--. 1 root root 0 Jun  5 10:39 mem_exclusive
-rw-r--r--. 1 root root 0 Jun  5 10:39 mem_hardwall
-rw-r--r--. 1 root root 0 Jun  5 10:39 memory_migrate
-r--r--r--. 1 root root 0 Jun  5 10:39 memory_pressure
-rw-r--r--. 1 root root 0 Jun  5 10:39 memory_pressure_enabled
-rw-r--r--. 1 root root 0 Jun  5 10:39 memory_spread_page
-rw-r--r--. 1 root root 0 Jun  5 10:39 memory_spread_slab
-rw-r--r--. 1 root root 0 Jun  5 10:39 mems
-rw-r--r--. 1 root root 0 Jun  5 10:39 notify_on_release
-rw-r--r--. 1 root root 0 Jun  5 10:39 release_agent
-rw-r--r--. 1 root root 0 Jun  5 10:39 sched_load_balance
-rw-r--r--. 1 root root 0 Jun  5 10:39 sched_relax_domain_level
-rw-r--r--. 1 root root 0 Jun  5 10:39 tasks
[root@localhost cpusets]# cat cpus  cpu查看的根域里包含的cpu
0
[root@localhost cpusets]# cat mems 查看内存根域有多少段
0
[root@localhost cpusets]# cat tasks  查看运行在根域的进程有哪些

创建cpu的子域

[root@localhost cpusets]# mkdir domain1
[root@localhost cpusets]# cd domain1/
[root@localhost domain1]# ls -l
total 0
--w--w--w-. 1 root root 0 Jun  5 10:46 cgroup.event_control
-rw-r--r--. 1 root root 0 Jun  5 10:46 cgroup.procs
-rw-r--r--. 1 root root 0 Jun  5 10:46 cpu_exclusive
-rw-r--r--. 1 root root 0 Jun  5 10:46 cpus
-rw-r--r--. 1 root root 0 Jun  5 10:46 mem_exclusive
-rw-r--r--. 1 root root 0 Jun  5 10:46 mem_hardwall
-rw-r--r--. 1 root root 0 Jun  5 10:46 memory_migrate
-r--r--r--. 1 root root 0 Jun  5 10:46 memory_pressure
-rw-r--r--. 1 root root 0 Jun  5 10:46 memory_spread_page
-rw-r--r--. 1 root root 0 Jun  5 10:46 memory_spread_slab
-rw-r--r--. 1 root root 0 Jun  5 10:46 mems
-rw-r--r--. 1 root root 0 Jun  5 10:46 notify_on_release
-rw-r--r--. 1 root root 0 Jun  5 10:46 sched_load_balance
-rw-r--r--. 1 root root 0 Jun  5 10:46 sched_relax_domain_level
-rw-r--r--. 1 root root 0 Jun  5 10:46 tasks


开始绑定第0颗cpu到子域上

[root@localhost domain1]# echo 0 > cpus
[root@localhost domain1]# cat cpus
0
绑定第0段内存到子域上

[root@localhost domain1]# echo 0 > mems
[root@localhost domain1]# cat mems
0

将某个进程绑定到子域上,这个进程就只能在子域的cpu和内存段运行

[root@localhost domain1]# ps axo pid,cmd
[root@localhost domain1]# echo 16380 > tasks

[root@localhost domain1]# ps -e -o psr,pid,cmd   |grep httpd  显示进程在哪一个cpu上运行
[root@localhost domain1]# watch -n 0.5  `ps -e -o psr,pid,cmd   |grep httpd` 压力测试

[root@localhost domain1]# taskset -p -c 0 1
pid 1's current affinity list: 0  显示1号进程以前可以运行在0号cpu上
pid 1's new affinity list: 0 现在可以运行在0号cpu上






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章