Original article link
Reference
Http://baobaoyangzhou.blog.163.com/blog/static/117831250201241232756488/
Http://wenku.baidu.com/view/5e5438186bd97f192279e964.html
Index
- GPROF usage
- Usage notes
- Additional reading
The common performance tool in Linux is GPROF with GCC. Here is an example.Program:
# Include <stdio. h> # Include <Stdlib. h> Void F1 (){ Int I; Int * P; For (I = 0 ; I < 10 ; I ++ ) {P = Malloc ( Sizeof ( Int )); * P = 10 ; Free (p );}} Void F2 (){ Int I; Int * P; For (I = 0 ; I < 20 ; I ++ ) {P = Malloc ( Sizeof ( Int )); * P = 10 ; Free (p );}} Void F3 (){ Int I; Int * P; For (I = 0 ; I < 30 ; I ++ ) {P = Malloc ( Sizeof ( Int )); * P = 10 ; Free (p );}} Int Main (){ Int I; For (I = 0 ; I < 1000000 ; I ++ ) {F1 (); F2 (); F3 ();} Return 0 ;}
Haha, it's a bad program. Now we need to use GPROF to find out what the CPU is used for when the program is running.
It is easy to enable GPROF. You just need to include the-PG parameter during GCC Compilation:
Gcc-G-PG test. C-o Test
Run./test. After running the program, we can see that there are multiple gmon. out files in the directory. This is the GPROF log, which records the CPU usage information of the program running. Open to see? Cup, binary file, we humans don't understand... Run the following command to generate a report:
GPROF./test> report.txt
Open report.txt and we can see two tables.
First:
Flat profile: each sample counts 0.01 Seconds. % Cumulative Self total Time Seconds seconds cils ns/call NS/ Call name 56.25 0.32 0.32 1000000 315.00 315.00 F3 33.04 0.50 0.18 1000000 185.00 185.00 F2 10.71 0.56 0.06 1000000 60.00 60.00 F1
This is the CPU usage time and percentage of each function. We can clearly see the time relationship between F1 (), F2 () and F3. Very accurate.
The second table describes the relationship between function calls:
Granularity: each sample hit covers 4 Byte (S) For 1.79 % 0.56 Seconds Index % Time Self children called name <Spontaneous> [ 1 ] 100.0 0.00 0.56 Main [ 1 ] 0.32 0.00 1000000 / 1000000 F3 [ 2 ] 0.18 0.00 1000000 / 1000000 F2 [ 3 ] 0.06 0.00 1000000 / 1000000 F1 [ 4 ] ----------------------------------------------- 0.32 0.00 1000000 / 1000000 Main [ 1 ] [ 2 ] 56.2 0.32 0.00 1000000 F3 [ 2 ] ----------------------------------------------- 0.18 0.00 1000000 / 1000000 Main [ 1 ] [ 3 ] 33.0 0.18 0.00 1000000 F2 [3 ] ----------------------------------------------- 0.06 0.00 1000000 / 1000000 Main [ 1 ] [ 4 ] 10.7 0.06 0.00 1000000 F1 [ 4 ] -----------------------------------------------
Take a closer look.
The following describes a more powerful tool to generate a report (in fact, a graph) -- gprof2dot: http://code.google.com/p/jrfonseca/wiki/gprof2dot.
Connect to the corresponding report.txt and execute the following command:
Gprof2dot report.txt>Test. Dot dot-Tpng-O test.png
The first sentence refers to converting a report into a dot file (graphviz http://www.graphviz.org/image file format ). The second sentence is to convert the file to PNG format. Now, use the image software to open it:
No need to explain it. The number of calls/CPU time consumed by the function itself/the time consumed by the function to be called is clear at a glance!
Atexit
Function Name: atexit
Header file: # include <stdlib. h>
Function: registers the termination function (that is, the function called after the main execution ends)
Usage: int atexit (void (* func) (void ));
Note: according to iso c, a process can register up to 32 functions, which will be automatically called by exit. The function type registered by atexit () should be void functions that do not accept any parameters. The order in which exit calls these registration functions is the opposite of the order in which they are registered. If the same function is registered multiple times, it will also be called multiple times.
Program example:
# Include <stdio. h>
# Include <stdlib. h>
Void exit_fn1 (void)
{
Printf ("exit function #1 called \ n ");
}
Void exit_fn2 (void)
{
Printf ("exit function #2 called \ n ");
}
Int main (void)
{
/* Post exit function #1 */
Atexit (exit_fn1 );
/* Post exit function #2 */
Atexit (exit_fn2 );
Return 0;
}
Output:
Exit function #2 called
Exit function #1 called
Process Termination method:
There are eight ways to terminate the process, the first five of which are normal termination, they are
1: return from main
2: Call exit
3: Call _ exit or _ exit
4: The last thread returns from its startup routine
5: The last thread calls pthread_exit.
There are three types of exceptional termination:
6: call abort
7. receive a signal and terminate
8: The last thread responds to the cancellation request.
# Include <stdlib. h?
Void exit (INT status );
Void _ exit (INT status );
# Include <unistd. h>
Void _ exit (Status );
None of the call _ exit and _ exit will call the termination program.
No exception will be terminated.
Sighup
SIGINT terminates process interruption
Sigquit creates a core file to terminate the process and generates a Core File
Invalid Command for creating core file in sigill
Sigtrap creates core file tracking self-traps
Sigbus Core File bus creation error
Invalid SIGSEGV Core File segment creation error
An error occurred while creating the core file floating point in sigfpe.
Sigiot creates Core File Execution I/O self-traps
Sigkill terminate the process and kill the process
Sigpipe terminates a process and writes data to an unread process pipeline.
When sigalarm terminates the process Timer
Sigterm termination process software termination signal
Sigstop stop signal of non-terminal stop Process
Sigtstp stop signal from the process Terminal
Sigcont ignores the signal and continues a stopped process.
Sigurg ignores signal I/O emergency signals
Sigio can ignore the I/O on the signal Descriptor
Sigchld ignores the signal and notifies the parent process when the child process stops or exits.
Sigttou stops the process Background Process Writing Terminal
Sigttin stops the process Background process read Terminal
Sigxgpu termination process CPU timeout
The length of the sigxfsz termination process file is too long.
Sigwinch ignores changing signal window size
Sigprof termination process statistical distribution when timer is used
SIGUSR1 termination process user-defined signal 1
Sigusr2 termination process user-defined Signal 2
When sigvtalrm terminates the virtual timer of the process
1) The sighup signal is sent at the end of the user terminal connection (normal or abnormal), usually in the terminal's control
When the process ends, each job in the same session is notified.
No longer associated.
2) The SIGINT program termination (Interrupt) signal is sent when you type the intr character (usually Ctrl-C)
3) sigquit is similar to SIGINT, but it is controlled by the quit character (usually Ctrl -).
The core file is generated when sigquit exits. In this sense, it is similar to a program error message.
No.
4) sigill executes invalid commands. This is usually because the executable file itself has an error or an attempt to execute
Data Segment. This signal may also be generated when the stack overflows.
5) sigtrap is generated by the breakpoint instruction or other trap instruction. It is used by the debugger.
6) when the SIGABRT program finds an error and calls abort.
6) sigiot is generated by Iot instructions on the PDP-11, and is the same as SIGABRT on other machines.
7) sigbus illegal address, including memory address alignment error. eg: access a four-character long
But its address is not a multiple of 4.
(8) sigfpe is triggered when a fatal arithmetic operation error occurs. This includes not only floating point operation errors, but also overflow.
An error occurs in all other arithmetic operations, such as 0 in the output and devisor.
9) sigkill is used to immediately end program running. This signal cannot be blocked, processed, or ignored.
10) SIGUSR1 is reserved for users
11) SIGSEGV tries to access the memory not allocated to itself, or try to write data to the memory address with no write permission.
12) sigusr2 is reserved for users.
13) sigpipe broken pipe
14) The sigalrm clock timing signal calculates the actual time or clock time. The alarm function uses this
Signal.
15) The sigterm program terminate signal. Unlike sigkill, this signal can be blocked and
Processing. It is usually used to require the program to exit normally. Shell Command kill will generate this by default
Signal (s.
17) when the sigchld sub-process ends, the parent process will receive this signal.
18) sigcont allows a stopped process to continue execution. This signal cannot be blocked.
A handler that enables a program to complete a specific task when it changes from stopped to continue execution.
Job. For example, re-display the prompt
19) sigstop stop (stopped) process execution. Note the differences between it and terminate and interrupt:
This process has not ended, but is paused. This signal cannot be blocked, processed, or ignored.
20) sigtstp stops the process, but the signal can be processed and ignored.
(Usually Ctrl-z) sends this signal
21) sigttin when the background job needs to read data from the user terminal, all processes in the job will receive sigttin
Signal. Due to lack of time, these processes will stop running.
22) sigttou is similar to sigttin, but it is received when the write terminal (or modify the terminal mode.
23) sigurg has "urgent" data or out-of-band data generated when it reaches the socket.
24) The sigxcpu exceeds the CPU time limit. This limit can be read by getrlimit/setrlimit/
Change
25) sigxfsz exceeds the file size resource limit.
26) sigvtalrm virtual clock signal. Similar to sigalrm, the CPU time occupied by the process is calculated.
27) sigprof is similar to sigalrm/sigvtalrm, but includes the CPU time used by the process and
Time.
28) it is issued when the sigwinch window size is changed.
29) The sigio file descriptor is ready for input/output operations.
30) sigpwr Power Failure
There are two signals to stop the process: sigterm and sigkill. Sigterm is relatively friendly, and processes can capture this signal and close the program as needed. Before closing the program, you can end the opened record file and complete ongoing tasks. In some cases, if the process is running and cannot be interrupted, the process can ignore this sigterm signal.
Processes cannot ignore sigkill signals. This is a signal "I don't care what you are doing, stop immediately. If you send a sigkill signal to the process, Linux stops the process.
1 ) Generally, GPROF can only view User Function information. To view the library function information, you must add"-Lc_p"Compile the parameter instead of"-LC "to compile the parameter. In this way, the program will link to the libc_p.a library to generate the profiling information of the library function.
2) GPROF can only beAfter the program ends normally and exitsTo generate a program evaluation report, because GPROF registers a function in atexit () to generate the result information, and does not execute the atexit () Action for any abnormal exit, therefore, gmon is not generated. out file. If your program is a service program that will not exit, you have to modify it.CodeTo achieve the goal. If you do not want to change the running method of the procedure, you canAdd a signal processing functionSolve the problem (with minimal modification to the Code), for example:
Static void sighandler (INT sig_no)
{
Exit (0 );
}
Signal (SIGUSR1, sighandler );
After the kill-usr1 PID is used, the program exits and generates the gmon. Out file.