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

 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命令就可以定位问题了。
Solaris prstat -mL 5命令:
Java代码   收藏代码
  1. PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROC/LWPID  
  2. 3897 huntch 6.0 5.7 0.1 0.0 0.0 2.6 8.2 78 9K 8K 64K 0 java/2  
  3. 3897 huntch 4.9 4.8 0.0 0.0 0.0 59 0.0 31 6K 6K 76K 0 java/13  
  4. 3897 huntch 4.7 4.6 0.0 0.0 0.0 56 0.0 35 5K 6K 72K 0 java/14  
  5. 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的线程堆栈:
Java代码   收藏代码
  1. ----------------- lwp# 2 / thread# 2 --------------------  
  2. fef085c7 _lwp_cond_signal (81f4200) + 7  
  3. feb45f04 __1cNObjectMonitorKExitEpilog6MpnGThread_pnMObjectWaiter__v_  
  4. (829f2d4, 806f800, e990d710) + 64  
  5. fe6e7e26 __1cNObjectMonitorEexit6MpnGThread__v_ (829f2d4, 806f800) + 4fe  
  6. fe6cabcb __1cSObjectSynchronizerJfast_exit6FpnHoopDesc_  
在linux下,top -H查看线程情况,包括CPU,内存等占用情况。进入到top环境后,可以使用P来按照CPU排序,使用M按照内存排序。
 from: http://chinese-darren.iteye.com/blog/1752469
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值