[Java] system-level performance monitoring and optimization

Source: Internet
Author: User

[Java] system-level performance monitoring and optimization
For those who are concerned about Java Performance, they probably know the book "Java Performance". In general, many students seldom care about Performance issues when writing Java Code on a daily basis, however, when writing Code, we must consider the impact of performance on the program. As small as we use bitwise operations to implement arithmetic operations, and as large as our overall architecture design for Java code, "performance" is actually very close to us. This article mainly mentions several points and hopes to inspire you.



For performance tuning, we usually need to take the following three steps: 1. Performance monitoring; 2. Performance analysis; 3. Performance Tuning

As a leading APM vendor in China at the technical level, One APM's AI products provide excellent indicators for Java Application performance optimization:



Performance monitoring: Multidimensional metrics monitoring that affect Java Performance



Performance analysis: Application Performance Analysis



Performance Tuning: analyzes the root cause of Application performance problems and optimizes the Application;

We focus on the performance of the operating system in the following aspects: CPU utilization, CPU scheduling execution queue, memory utilization, network I/O, and disk I/O.

1. CPU utilization

For an application, in order to achieve the best performance and scalability, we should not only make full use of the available parts in the CPU cycle, but also make the use of this part of CPU more valuable, rather than waste. It makes full use of CPU cycles and is very challenging for multi-threaded applications to run on multi-processor and multi-core systems. In addition, when the CPU reaches saturation, it does not indicate that the CPU performance and scalability have reached the optimal state.

To distinguish how an application uses CPU resources, we must detect them at the operating system level. In many operating systems, statistical reports on CPU utilization usually include the use of operating systems by users and systems or kernels. CPU usage refers to the time required by the application to execute application code. In contrast, CPU usage by the kernel and system refers to the time it takes for an application to execute the kernel code lock of the operating system. High kernel or system CPU usage can indicate that shared resources are insufficient, or there is a large number of I/O device interactions. Ideally, to improve application performance and scalability, set the CPU time of the kernel or system to 0%, because the time spent executing the kernel or system code can be used to execute the application code. Therefore, the correct way to optimize CPU usage is to minimize the time that the CPU spends on executing kernel code or system code.

For Computing-intensive applications, performance monitoring is deeper than monitoring users' CPU usage and kernel or system CPU usage. In computing-intensive applications, we need to monitor the number of executions (Instructions per clock; IPC) in the CPU clock cycle, or the cycle (cycles per instruction; CPI) used for each CPU execution ). For Computing-intensive applications, it is a good choice to monitor the CPU from these two dimensions, because the packaging CPU performance report tool of modern operating systems usually only prints the CPU utilization, instead of printing the time when the CPU is used to execute commands within the CPU cycle. This means that when the CPU is waiting for data in the memory, the CPU performance reporting tool of the operating system will also think that the CPU is in use. We call this scenario "Stall 」, this scenario often occurs. For example, when the CPU is executing a command, as long as the data required by the command is not ready, that is, it is not in the register or CPU cache, "Stall" scenario will occur.

When the "Stall" scenario occurs, the CPU will waste the clock cycle, because the CPU must wait for the data required by the command to reach the register or buffer. In this scenario, it is normal that hundreds of CPU clock cycles are wasted. Therefore, in computing-intensive applications, the performance improvement strategy is to reduce the occurrence of the "Stall" scenario or enhance the CPU Cache Usage so that less CPU cycles are wasted due to data waiting. This kind of performance monitoring knowledge has exceeded the content of this book and needs the help of performance experts. However, the Oracle Solaris Studio Performance Analyzer Performance profiling tool later will include this type of data.

2. CPU scheduling queue

In addition to monitoring the CPU usage, we can also monitor the CPU execution queue to check whether the system is fully loaded. The execution queue is used to store lightweight processes. These processes are usually ready for execution but waiting for CPU scheduling and waiting in the scheduling queue, when a lightweight process does not have the current processor to process more data, the scheduling queue is generated. A deep CPU scheduling queue indicates that the system is fully loaded. The execution queue depth of the system is equal to the number of waits that the virtual processor cannot execute, and the number of virtual processors is equal to the number of hardware threads of the system. We can use JAVA APIs to get the number of virtual processors.

Runtime. avaliableProcessors (). When the execution queue depth is greater than four or more times the number of virtual processors, the operating system will be unresponsive.

A common guide to CPU scheduling queue detection is to pay attention when we find that the queue depth is twice the number of virtual processes, but there is no need to take immediate action. When it is greater than three or four times or higher, you must pay attention to it and it is imperative to solve the problem.

There are usually two optional ways to observe the depth of the queue. The first is to increase the CPU to share the load or reduce the load on the existing CPU. This approach essentially reduces the number of load threads for each execution unit, thus reducing the depth of the execution queue.

Another way is to increase the CPU usage by analyzing the applications running the system. Another way is to find a way to reduce the CPU cycle spent on garbage collection, or look for better algorithms to execute CPU commands with less CPU cycles. Performance experts usually focus on one of the following ways: reduce the code execution path length and better CPU instruction selection. Java programmers can improve code execution efficiency by better executing algorithms and data structures.

3. Memory Utilization

In fact, in addition to CPU usage, the system's memory attributes also need to be monitored. These attributes include paging, switching, locking, and context switching caused by multithreading.

The swap usually occurs when the memory required by the application is greater than the actual physical memory. In this case, the operating system usually configures a corresponding region called the swap zone. The swap zone is usually on a physical disk. When the physical memory is used up, the operating system temporarily exchanges some memory data to the disk space, this part of the memory area is usually the area with the lowest access frequency, without affecting the busy memory area. When the memory exchanged to the disk area is accessed by the application, in this case, you need to read pages into the memory from the disk swap area, which affects the application performance.

The Spam collector of a virtual machine has very poor performance during swap, because most of the regions accessed by the spam collector are inaccessible, that is, the spam Collector may cause SWAp activities. The scenario is dramatic. If the heap area of the garbage collection has been switched to the disk space, the page will be switched in order to be scanned by the garbage collector, during The switching process, The collection time of The garbage collector will be dramatically extended. If The garbage collector is "Stop The World, this time will be extended.

4. Network I/O

The performance and scalability of distributed Java applications are limited by network bandwidth and network performance. For example, if we send more data packets to network interfaces than they can process, the data packets will pile up in the buffer zone of the operating system, which will lead to application latency, in other cases, network application latency may also occur.

Tools for distinguishing and monitoring are usually hard to find in operating system packaging tools. Although Linux provides the Netstat command, both Linux and Solaris provide network usage, and they provide statistics on Packet sending, packet receiving, error packets, conflicts, and other information per second. In Ethernet, a small number of packet conflicts are normal. If the number of wrong packets is high, the NIC may be faulty. At the same time, although netstat can collect statistics on the sending and receiving data of network interfaces, it is difficult to determine whether the NIC is fully utilized. For example, if Netstat-I shows that 2500 packets are sent from the NIC every second, but we still cannot determine whether the current network utilization is 100% or 1%, we can only know that there is traffic currently. This is only the conclusion that the network package size is unknown. Simply put, we cannot use the Netstat provided by Linux and Solaris to determine whether the current network has affected the performance. We need some other tools to monitor the network while running our Java application.

5. Disk I/O

If the application performs operations on the disk, we need to monitor the disk to monitor possible disk performance problems. Some applications are I/O-intensive, such as databases. Disk usage usually exists in the Application Log System. logs are usually used to record important information during system operation.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.