Linux Code Performance Detection Tool (ii)--OPROFILE Code Analysis Example __linux

Source: Internet
Author: User

http://blog.csdn.net/fenghaibo00/article/details/9007263

For development at the application level (relative to kernel development), if you just want to quickly find code performance bottlenecks that are not interested in how Oprofile works, just look at the example should suffice.


If our code file is/home/leo/oprofile_test/main.cpp, the content is:

[CPP] View plain copy #include  <stdio.h>      #include  <unistd.h>      #include  <string.h>             class  fileop      {                public:                          fileop (char *file)                           {                                    strcpy (M_filename, file);                      & nbsp;           m_buff = new char[1024];                                    memset (M_buff, 0,  1024);                          }                                 ~fileop ( )                           {                                    delete&nbsp []m_buff;                                   m_buff =  NULL;                          }                                 void  dowork ()                           {                                                             file *fp = fopen (m_filename,  "R");                                    if (!FP)                                    {                                             return;                                   }            &nbsp                              int count = 0;                                    int i = 0;                                    while (!feof (FP))                                    {                                &nbSp;            fgets (M_BUFF,&NBSP;1023,&NBSP;FP);                                              count = strlen (m_buff);                                             usleep (100);                                              fprintf (stderr,  "the %ld has %ld characters!\n",  + +i, count);                                   }                                           fclose (FP);                          }                       private:                          char m_fileName[1024];                          char *m_buff;     };                    Int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV)       {                if (argc != 2)                {                          return -1;                }                                      fileop op (argv[1]);                op. DoWork ();                             return 0;      }  

This program reads a file that is passed from the command line and prints out the number of characters per line (this code runs on a 64-bit machine).

A) use "g++-G main.cpp" to compile the source code into an executable program

b) Parser initialization using "Opcontrol--init" (Start Analyzer driver module)

(c) Use of "Opcontrol--setup--image=/home/leo/oprofile_test/a.out--event=cpu_clk_unhalted:6000:0" and "Opcontrol--no-vmli Nux the Setup Analyzer, as shown in the following illustration:



d) Use "Opcontrol--start" to start the analyzer



E to run the program, such as: "./a.out/var/log/messages.1"

After the run is complete, use the following command to obtain the analysis results.

A) Run "Opcontrol--dump" to send sampled data into the analyzer

b Run "opreport-l" to view the analysis results at the function level, as shown in the following illustration:



As you can see from the figure Fileop::D Owork's sampling percentage accounts for 99.0909%, which means most CPU time is in this function.

C Run "Opannotate--source" to view the analysis results at the code level, as shown in the following illustration:



The image above simply intercepts the key part of the output, from this output you can see that the strlen line accounts for 77% (the number 768 in the red rectangle indicates that the number of rows sampled during the profiling sampling is 768, that is, the location of the code at which the run is viewed at the time of the event that is set, and the number of samples added to the row by 1 77.5758 represents a row of samples of all the sample number of 77.5758%), indicating that this code is very inefficient operation, found a performance bottleneck, the next task is to find a function to replace strlen. (Strlen is very inefficient, especially when strings are long)

OK, the results of this analysis have come out, but for the project is very large, opreport and opannotate output a lot, this time to export the analysis results to the file is easier to see, for Opreport, you can use the Opreport-l-O. Analysis.log "The analysis results are exported to the file, as shown in the following illustration:



And for opannotate, you can use the "Opannotate--source--output-dir=/tmp/" for analysis results corresponding to the corresponding source code files, as shown in the following figure:



The above command means to put the corresponding tagged source code files in the "/tmp/" directory, in the directory, according to the source code of the file structure generated corresponding file results, as shown in the "/tmp/" directory Generated "home/leo/oprofile_test" directory, Consistent with the source code directory, and "Main.cpp" is in the directory, open the directory under the "Main.cpp" can see each code corresponding to the sample data and the percentage of resource occupancy.

Using the analyzer, stop the sampling with the command "Opcontrol--stop", command "Opcontrol--reset" to clear the sampled data in the current session, command "Opcontrol--shutdown" to shut down the parser kernel module, "Opcontrol- -stop "and the command" Opcontrol--reset "are very necessary because these two steps will be affected by this result the next time they are analyzed.

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.