Linux性能监测工具-cpu基本概念：•上下文切换(Context Switches):如果可运行的线程数大于CPU的数量，那么OS最终会强行换出正在执行的线程，从而使其他线程能够使用CPU，它会保存当前运行线程的执行上下文，并重建新调入线程的执行上下文。•运行队列（ Run Queue ）：每个CPU 都维护一个线程的运行队列。如果CPU 子系统处于高负荷下,那就意味着内核调度将无法及时响应系统请求.导致结果,可运行状态进程拥塞在运行队列里.当运行队列越来越巨大,进程线程将花费更多的时间获取被执行.
Linux性能监测工具-cpu基本概念：•load ：就是指在CPU 队列中有多少数目的线程,以及其中当前有多少进程线程数目被执行的组合. 安全的load ：一般是cpu的个数•CPU 利用率(CPU Utilization):CPU 使用的百分比 User Time System Time Wait IO Idle•中断(Interrupts) – Devices tell the kernel that they are done processing。例子,当一块网卡设备递送网络数据包或者一块硬件提供了一次IO 请求.
Linux性能监测工具-cpu•Run Queues - 每个处理器应该运行队列丌超过1-3个线程.例子,一个双核处理器应该运行队列丌要超过6 个线程。•CPU Utiliation - 如果一个CPU 被充分使用,利用率分类之间均衡的比例应该是： 65% - 70% User Time 30% - 35% System Time 0% - 5% Idle Time
Linux性能监测工具-内存使用vmstatField DescriptionSwapd The amount of virtual memory in KB currently in use. As free memory reaches low thresholds, more data is paged to the swap device. 当前虚拟内存使用的总额(单位:KB).空闲内存达到最低的阀值时,更多的 数据被转换成页到交换设备中.Free The amount of physical RAM in kilobytes currently available to running applications. 当前内存中可用空 间字节数. The amount of physical memory in kilobytes in the buffer cache as a result of read() and write()Buff operations. 当前内存中用于read()和write()操作的缓冲区中缓存字节数 The amount of physical memory in kilobytes mapped into process address space. 当前内存中映射到进Cache 程地址空间字节数so The amount of data in kilobytes written to the swap disk. 写入交换空间的字节数总额 The amount of data in kilobytes written from the swap disk back into RAM. 从交换空间写回内存的字节si 数总额 The amount of disk blocks paged out from the RAM to the filesystem or swap device. 磁盘块页面从内存bo 到文件或交换设备的总额 The amount of disk blocks paged into RAM from the filesystem or swap device. 磁盘块页面从文件或交bi 换设备到内存的总额
Linux性能监测工具-networksar -n SOCK查看网络连接资源totsck Total number of used sockets.tcpsck Number of TCP sockets currently in use.
Linux性能监测工具-networksar -n DEV 查看网络流量 rxpck/s Total number of packets received per second.txpck/s Total number of packets transmitted per second.rxbyt/s Total number of bytes received per second.txbyt/s Total number of bytes transmitted per second.
Linux性能监测工具-其他lsof：可以列出被进程所打开的文件的信息。COMMAND：进程的名称 PID：进程标识符 USER：进程所有者 FD：文件描述符，应用程序通过文件描述符识别该文件。如cwd、txt等TYPE：文件类型，如DIR、REG等 DEVICE：指定磁盘的名称 SIZE：文件的大小 NODE：索引节点（文件在磁盘上的标识）NAME：打开文件的确切名称 too many open file错误常用的参数 -p +D
JVM 堆结构•Young Generation ：Eden where new objects get Instantiated 2 Survivor Spaces to hold live objects during minor GC•Old Generation： Objects that are longer-lived are eventuallypromoted or tenured, to the old generationPermanent Generation： VM and Java class metadata as wellas interned Strings and class static variables
jvm gc Minor garbage collection illustration•One survivor space is always empty， Serves as destination for minorcollections。•At the end of the minor garbage collection, the two survivor spaces swaproles•The eden is entirely empty; only one survivor space is in use; and theoccupancy of the old generation has grown slightly•Major collections occur when the tenured space fills up ，Major collectionsfree up Eden and both survivor spaces
Selecting a CollectorIf the application has a small data set (up to approximately 100MB), then select the serial collector with -XX:+UseSerialGC.If the application will be run on a single processor and there are no pause time requirements, then let the VM select the collector, or select the serial collector with -XX:+UseSerialGC.
Selecting a CollectorIf (a) peak application performance is the first priority and (b) there are no pause time requirements or pauses of one second or longer are acceptable, then let the VM select the collector, or select the parallel collector with -XX:+UseParallelGC and (optionally) enable parallel compaction with - XX:+UseParallelOldGC.If response time is more important than overall throughput and garbage collection pauses must be kept shorter than approximately one second, then
jvm主要参数Behavioral options change the basic behavior of the VM.Performance tuning options are knobs which can be used to tune VMperformance.Debugging options generally enable tracing, printing, or output of VMinformation.
Heap Size Starting Point• From the GC log you will get Approximation of the Live Data Size (LDS) It is the heap occupancy after each full GC Approximation of max perm gen size It is the perm gen occupancy after each full GC GC log example:170.517: [Full GC[PSYoungGen: 10128K->0K(163392K)][ParOldGen: 348898K->161378K(350208K)]359026K->161378K(513600K)
Initial Heap Configuration• You can now make an informed decision on choosing a reasonable heap size Rule of thumb Set -Xms and -Xmx to 3x to 4x LDS Set both -XX:PermSize and -XX:MaxPermSize to around 1.2x to 1.5x the max perm gen size• Set the generation sizes accordingly Rule of thumb Young gen should be around 1x to 1.5x LDS Old gen should be around 2x to 3x LDS e.g., young gen should be around 1/3-1/4 of the heap
JVM调优建议• You should try to maximize the number of objects reclaimed in the young generation This is probably the most important piece of advice when sizing a heap and/or tuning the young generation• Your applications memory footprint should not exceed the available physical memory This is probably the second most important piece of advice when sizing a heap
JVM调优建议• Applications with emphasis on performance tend to set -Xms and -Xmx to the same value When -Xms != -Xmx, heap growth or shrinking requires a Full GC• Applications with emphasis on performance almost always set -XX:PermSize and - XX:MaxPermSize to the same value Growing or shrinking the permanent generation requires a Full GC too
JVM调优建议• Try to retain as many objects as possible in the survivor spaces so that they can be reclaimed in the young generation Less promotion into the old generation Less frequent old GCs• But also, try not to unnecessarily copy very long lived objects between the survivors Unnecessary overhead on minor GCs• Not always easy to find the perfect balance
JVM调优建议1. Higher tenuring threshold → promotes fewer objects Possibly (but not necessarily) longer young GC times Increases the number of objects reclaimed in the young gen Better overall efficiency2. Lower tenuring threshold → promotes more objects Possibly (but not necessarily) shorter young GC times More load / pressure on the old gen More frequent old GCs Could make fragmentation more severe
Java crash• A crash, or fatal error, causes a process to terminate abnormally。• Crash后会产生 hs_err_pid开头的文件，（有时候可能来不及）• 可能的原因： – Java虚拟机自身的Bug； – 系统的库文件、API或第三方的库文件造成； – 系统资源的短缺
Java crash文件格式Crash 文件由以下几部分组成:• A header that provides a brief description of the crash.• A section with thread information. – Thread Information – Signal Information – Register Context – Machine Instructions – Thread Stack – Further Details• A section with process information. – Thread List – VMState – Mutexes and Monitors – Heap Summary – MemoryMap
java性能监测工具Eclipse Memory Analyzer: a fast and feature-rich heap analyzerthat helps you find memory leaks and high memory consumption issues
java性能监测工具Java VisualVM: a tool that provides a visual interface for viewing detailedinformation about Java technology-based applications (Java applications)while they are running on a Java Virtual Machine (JVM)