Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.


The background,

A concrete example of how Java applications navigate to specific steps of code through methods is shown below.

Second, analysis steps

  1. Use the TOP command to find out which processes are consuming more CPU, for example, PID = 1232
  2. Monitor this process individually using top -P 1232
  3. Enter a capital H to list all threads under the current process
  4. View the threads that consume the most CPU, and look at the thread number, for example: 2399
  5. Run jstack 1232>pagainfo.dump to obtain information about the dump thread in the current process
  6. Convert the thread number 12399 obtained in step 4 to hexadecimal 306f (printf “%x\n” 12399)
  7. Look for the thread with TID =0x306 in the stack information obtained in step 5 based on 306f
  8. Locate the code (see where the code is based on the printed stack)

Note:

The number of local threads on the VM displayed by the operating system is the same as the number of threads in the Java thread stack, indicating a one-to-one correspondence. It’s just that nids in Java threads are expressed in hexadecimal, whereas ids in local threads are expressed in decimal.

3. An example

1. Write a demo

An example of Springboot:

/ * * *@author 7d
 * @Title: PageIndex
 * @Description: Run the top- >top -p PID -- H jstack PID * command@date 2019/11/9 / 10:23
 */

@Log4j2
@Controller
public class PageIndexController {

    /** * test demo *@return* /
    @GetMapping("/7d")
    @ResponseBody
    public Object topDemo(a) {
        StudentInfo stInfo = new StudentInfo();
        stInfo.setName("topJava");
        stInfo.setAge(30);
        stInfo.setDes("Test Java to view JVM with top command");
        stInfo.setGrade("7DGroup");

        for (int i = 0; i < 100000; i++) {
            i++;
            try {
                log.info("my is print" + i);
                Thread.sleep(i);
            } catch(InterruptedException e) { e.printStackTrace(); }}return stInfo;
    }

    /** * debug *@return* /
    @GetMapping("/7ddemo")
    @ResponseBody
    public Object topDemo1(a) {
        StudentInfo stInfo = new StudentInfo();
        stInfo.setName("topJava");
        stInfo.setAge(30);
        stInfo.setDes("Test Java to view JVM with top command");
        stInfo.setGrade("7DGroup");

        returnstInfo; }}Copy the code

2. Deploy demo

Upload to Linux server to run:

Java jar jdktop - 0.0.1 - the SNAPSHOT. The jarCopy the code

Open your browser and visit:

3. Problem phenomenon

Jmeter pressure test can be used to facilitate performance positioning, or IDEA can be used to simply initiate a request:

Click on request:

Access the written requesthttp://ip:port/7dYou can view logs to show that the system is running in the background, but the front end is in the waiting state.

Open the Linux server and press the top command to view the Java process that consumes CPU. The process does not consume too much CPU in the operating system. The following steps are shown:

You can run the top -p PID command to view the information about the threads in the process.

Next, use the jstack command to print the dump information

[root@localhost bin]# ./jstack 93114 >/home/7d/7djava.dump   
Copy the code

Switch to the directory generated by dump:

We can download to the local view or use the Jdk built-in JVisualVM tool to view. Here’s an example of how to open it directly on a Linux server:

[root@localhost 7d]# vim 7djava.dump
Copy the code

To demonstrate the need, find the Java thread shown below:

PID decimal conversion to hexadecimal:

Open dump file with vim search command to find:

It can be seen from the above that the Thread is currently in TIMED_WAITING state, indicating that it is currently suspended for a period of time specified in the parameter, such as thread.sleep (1000). Therefore, the thread is not currently consuming CPU.

Here’s a quick look at threads:

http-nio-8080-exec-11" #30 daemon prio=5 os_prio=0 tid=0x00007fadf8001000 nid=0x16bf8 waiting on condition [0x00007fae6c7d0000]
Copy the code
  • Thread name: “http-niO-8080-exec-11”
  • Thread priority: prio=5
  • Local thread ID: nID = 0x16BF8
  • Thread state: waiting
  • Thread Memory address: [0x00007FAe6C7D0000]

Next we find the package name and line of the code:

From the following information:

Check the source code again:

Four,

We can quickly know what state the program is in through thread dump analysis, so we can know how to analyze the next step.