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.

preface

After the latest project update, the system will appear super lag in uncertain scenarios. When you open the server, you will find that the CPU has been jammed, and the lag will still occur after several restarts. If you do not find the reason, it is likely that you will run away with buckets.

The project is a mature product, now basically is according to customer demand for small changes, so let two students just graduated soon, caton case appeared several times, the server restart after n times repeatedly that they and I said the problem, after I placed a small operation, succeeded in winning the two little brother, do you have any need, Pack it up and send it away.

Next through the following small example to demonstrate the process of positioning.

The test code

First, create a new demo to demonstrate this CPU full situation, the code is very simple, is a while loop, print a log indefinitely.

import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: huage * @date: Created in 2021/10/25 * @description: */ @slf4j @restController public Class DemoController2 {@requestMapping ("test") public void testHashMap(){while (true){log.info(" You are the best "); }}}Copy the code

Packaged deployment

It is also very convenient to package with IDEA. You can directly click package. Here, FLOWER brother uses JAR package to package.

Package stage, flower elder brother shares two commonly used scripts (start/stop scripts), want to start deployment partners can certainly use.

  • The startup script
Jar export CLASSPATH=.: JAR package path /:${NAME} export JAVA_OPS=" -server-dappName = program NAME (corresponding to the NAME in the stop script) "echo ${CLASSPATH} nohup java ${JAVA_OPS} -cp ${CLASSPATH} org.springframework.boot.loader.JarLauncher >/dev/null 2>&1 &Copy the code
  • Stop scripts
Ps - ef | grep -v grep | grep Java | grep startup scripts DappName name | awk '{print "kill 9" $2}' | shCopy the code

  • Start the project

After the above steps are complete, the project can be started by using./run.sh and the startup logs can be seen in logs.

Determine the process

After the startup is successful, run the top -c command to check the usage of system resources. You can use the uppercase P repeatedly to sort the process that occupies the largest CPU usage. As you can see from the figure below, the current project takes up only 0.7% of the CPU,

  • Calling the Test interface

If the test interface is called, the project directly occupies 107.3% of the CPU, which is almost stuck to the explosion, and the command is almost impossible to listen to, of course, it may be that the server is too hot.

Open the log, we can see the log is constantly brush [flower brother you are the best], because it is convenient to test, here the use of circular printing log, in the real project, there are many many obscure problems will not be reflected in the log, can only through other means to determine the source of the problem.

Find the thread

You can use the top-c command to identify the faulty process. The next step is to find out which thread in the process is faulty. This is also relatively easy to determine, just type the command: top-hp PID. For example, enter top-HP 9828 in this example.

The thread 9877 is using 87.5% of the CPU resource temporarily, so it can be determined that the thread is faulty. So far, we have completed the location of the problem thread, and the next step is to determine where the thread is in the code.

Location code

In the JVM process snapshot thread is displayed in hexadecimal, so we need to convert the PID [9877] to hexadecimal, using the online conversion tool in the browser, or using the command [printf “%x\n” 9877].

printf “%x\n” PID

In this example, the converted hexadecimal is [2698]. Then use the jstack command to locate the specific problem code, as shown in the figure below, and finally locate the testHashMap method of DemoController.

Jstack process PID | grep -c thread hexadecimal display rows

conclusion

Positioning online environmental problems in the work is very useful, such as a user feedback a problem, your boss lost some server configuration to you, let you position, if you do not know what to see, and embarrassed to ask, is it also very embarrassing…. Of course, in addition to the work of the need, this kind of question in the interview will often be asked, fluent answer can let the interviewer feel that you this guy is not the back of the test, but the real use, salary can also get 200 dollars more.