欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

性能监控/优化系列——CPU相关

程序员文章站 2022-07-14 12:19:03
...

 CPU相关

1. 从下向上定位
     1.1 CUP监控(指令执行速度-单位时间内执行的指令条数, CUP缓存的miss率)
     1.2 OS的监控
     1.3 JVM的监控
       工具:Intel VTune or AMD’s CodeAnalyst Performance Analyzer(window/linux)
       选择正确的CPU架构和平台系统很重要,确认应用是否正运行在一个正确的CPU和系统架构上(涉及multiple cores per CPU and multiple hardware threads per core),每个hardware thread在OS看来就是一个processor。
       Oracle SPARC T-series CPU的特点:每个CPU包含多个核,每个核有4个hardware thread,任何时候只有一个hardware thread运行在clock cycle上,当发生latency events时(eg CPU MISS,那么需要去内存取数据,相对于CPU会有延时),另外一个runnable的hardware thread就会进入下一个clock cycle,因此整体的执行效率要高。特别是有大量多线程运行的应用程序。它拥有slower clock rate。
      传统的CPU的特点:每个CPU包含多个核,每个核仅有一个hardware thread或者是hyperthreaded cores,因此当latency events到来时会造成阻塞,从而clock cycle被浪费。另外,因为hardware thread的有限性,会引起大量的线程上下文交换80000以上 clock cycles才能完成)。
2. User CPU utilization is the percent of time the application spends in application code.
3. kernel or system CPU utilization is the percent of time the application spends executing operating system kernel code on behalf of the application
4. 高的system CPU utilization暗示共享资源竞争激励或IO设备交互过多
5. 对于计算密集型应用需要关注:instructions per clock(IPC)和 cycles per instruction(CPI),这个数据的获取不好弄,没有比较好的监控工具,为了提高这种应用的性能主要是减少stalls量,提高CPU的缓存利用率。
6. stall的概念:它是停止运转的意思,发生在当cpu执行时,所需要的数据却不在寄存器或cache中,需要去装载内存的数据,这期间有一个等待,这里叫做stall。这个时间大概是several hundred clock cycles
7. 现代操作系统所提供的CPU利用率的数据包括了CPU的stall,实际上stall期间CPU并没有工作。
8. CPU运行队列的概念(vmstat查看):The run queue is where lightweight processes(linux下进程和线程是统一的,是轻量级进程的两种形式) are held that are ready to run but are waiting for a CPU where it can execute,当准备执行的进程数多于处理器数时就会放入到运行队中。一个长的运行队列表明系统在饱和工作状态。当运行队列的长度4倍于虚拟处理器数或更大时,系统会出现明显的迟缓,需要及时处理。方法一般两个:1. 增加CPU数;2. 减少当个CPU的负载,其实质是reduces the number of active threads per virtual processor and as a result fewer lightweight processes build up in the run queue。当队列长度达到处理器数的1倍时需要开始关注,但不需要处理。
9. 虚拟处理器数的概念:The number of virtual processors is the number of hardware threads on the system,它的值等于Runtime.availableProcessors()。
10.垃圾回收/优秀的算法和数据结构的重要性:reducing garbage collection frequency or alternative algorithms and data structures will result in fewer CPU instructions/ fewer CPU cycles to execute the same work。
11. 常用命令:vmstat/mpstat(列出多个CPU的情况)/top;pstack/jstack查看线程堆栈;如果一个Java进程的CPU使用率很高,命令 prstat -mL 5 can be mapped to a Java process and Java thread(s),然后再结合pstack 3897/2,/jstack命令就可以定位问题了。
Solarisprstat -mL 5命令:
PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROC/LWPID
3897 huntch 6.0 5.7 0.1 0.0 0.0 2.6 8.2 78 9K 8K 64K 0 java/2
3897 huntch 4.9 4.8 0.0 0.0 0.0 59 0.0 31 6K 6K 76K 0 java/13
3897 huntch 4.7 4.6 0.0 0.0 0.0 56 0.0 35 5K 6K 72K 0 java/14
3917 huntch 7.4 1.5 0.0 0.0 0.0 3.8 53 34 5K 887 16K 0 java/28
 
发现3897这个进程有三个轻量级进程2/13/14,现在可以通过pstack 3897/2查看这个轻量级进程2的线程堆栈:
----------------- lwp# 2 / thread# 2 --------------------
fef085c7 _lwp_cond_signal (81f4200) + 7
feb45f04 __1cNObjectMonitorKExitEpilog6MpnGThread_pnMObjectWaiter__v_
(829f2d4, 806f800, e990d710) + 64
fe6e7e26 __1cNObjectMonitorEexit6MpnGThread__v_ (829f2d4, 806f800) + 4fe
fe6cabcb __1cSObjectSynchronizerJfast_exit6FpnHoopDesc_
在linux下,top -H查看线程情况,包括CPU,内存等占用情况。进入到top环境后,可以使用P来按照CPU排序,使用M按照内存排序。