Today in the training process, also mentioned that the analysis should be specific to the code, if the direction of thinking is right, for Java applications and C/C++ applications, it is also a few commands can jump to the code line. You have to be able to read the stack. So I’ve been drawing a diagram like this all along in the course of my lectures.

In the performance analysis, if it is C/C++ application, there are also some tools can do.

Today we’ll look at a simple C code example to see how to do these steps. I saw a sample code on the Internet, and I didn’t have to write it myself. I’m just going to compile it. So let’s look at the operation.

[root@7dgroup Sample6]# gcc -o test6 -g test6.c

When compiling, remember to add the -g parameter to generate debugging information.

[root@7dgroup Sample6]# ./test6

To run:

[root@7dgroup Sample6]# ./test6

The return value: 3

The return value of 5

The return value: 5

The return value of 7

The return value: 7

The return value of 9

The execution process produces such data. Also check top.



You can see that the process 31356 has consumed CPU. Because the process is so simple, I won’t zoom down to the thread level here. Just hit the stack and see.

(For complex applications, one more refinement is to print thread-level state at this stage. You can use top-h, you can use pidstat, or you can use attach to check threaddump. Just choose the way you like.

Direct GStack prints the stack.

[root@7dgroup ~]# gstack 31356

#0 0x00000000004005ed in function2 (input=963) at test6.c:4

#1 0x000000000040065b in function1 (a=9, b=10) at test6.c:21

#2 0x00000000004006e8 in main () at test6.c:39

Of course you can also pstack to print the stack (since I ran it again, the PID changed).

[root@7dgroup ~]# pstack 31438

#0 0x0000000000400620 in function3 (input=3524) at test6.c:14

#1 0x000000000040067e in function1 (a=5, b=6) at test6.c:25

#2 0x00000000004006e8 in main () at test6.c:39

The first print of the stack is 39 lines ->, 21 lines ->, 4 lines. The second print is 39 lines ->, 25 lines ->, 14 lines.

This can be used in C/C++ applications from the CPU analysis to specific lines of code.

Again, the integrity of the analytical thinking is very important. You have to know what data you want to look at before you know what tools to use to do it. Knowing the tools is nothing, but understanding the principles and understanding them is the real deal. Some people might say, I don’t even know how to use the tools, how can I know what data to look at. A seemingly paradoxical problem, in fact, is lack of experience, need to learn more basic knowledge. For example, if you understand the analysis process of running the Java language on Linux, the other analysis processes are similar, but the tools are different. This is not to say that you can only analyze Java running on Linux, instead of HPUNIX +C/C++ there is no idea. ,

It’s like a math problem in elementary school: there are four trees in a row, four rows in total, how many trees are there? 16 tree! But replace the tree with a telephone pole and some people won’t forget.