利用Pin分析程序的动态行为特征

http://blog.csdn.net/weiffun/article/details/4307852

在程序设计和优化过程中,我们通常希望能有工具帮助我们分析热点代码、覆盖率、内存泄露等这些特性,对多线程程序还希望能分析并发性、竞争和死锁等等。但这些特征通常在静态编译时很难获取,而且往往只能在运行时才能确定。Pin是intel开发的动态插桩工具,它提供一套API让我们开发和定制自己的性能分析工具,此外它还被广泛用在体系结构研究、安全等领域。目前intel parallel studio的很多特性便是基于pin开发而成。

Pin的工作机制

目前对程序进行剖析(profiling),主要有二种方法:统计硬件计数器和插桩。Pin采用了插装技术,在程序的执行过程中动态插入分析代码。下面的代码片段演示了插装的过程,假设我们要统计程序执行的指令数,可以在程序每执行一条指令时,插入一条count++语句,当程序执行完时,count的值就是程序执行的指令数。

[cpp]  view plain  copy
  1. count++  
  2. sub $0xff, %edx  
  3. count++  
  4. cmp %esi, %edx  
  5. count++  
  6. jle <L1>  
  7. count++  
  8. mov $0x1, %edi  
  9. count++  
  10. add $0x10, %eax  

Pin是采用动态编译技术实现动态插装,将分析代码和被分析程序动态编译后放置到代码缓存,然后由cpu动态执行代码缓存里的代码。

 

利用pin统计程序执行的指令数

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. #include "pin.H"  
  4.   
  5. // The running count of instructions is kept here  
  6. // make it static to help the compiler optimize docount  
  7. static UINT64 icount = 0;  
  8.   
  9. // This function is called before every instruction is executed  
  10. VOID docount() { icount++; }  
  11.       
  12. // Pin calls this function every time a new instruction is encountered  
  13. VOID Instruction(INS ins, VOID *v)  
  14. {  
  15.     // Insert a call to docount before every instruction, no arguments are passed  
  16.     INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END);  
  17. }  
  18.   
  19. KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",  
  20.     "o""inscount.out""specify output file name");  
  21.   
  22. // This function is called when the application exits  
  23. VOID Fini(INT32 code, VOID *v)  
  24. {  
  25.     // Write to a file since cout and cerr maybe closed by the application  
  26.     ofstream OutFile;  
  27.     OutFile.open(KnobOutputFile.Value().c_str());  
  28.     OutFile.setf(<a href="http://lib.csdn.net/base/ios" class='replace_word' title="iOS知识库" target='_blank' style='color:#df3434; font-weight:bold;'>iOS</a>::showbase);  
  29.     OutFile << "Count " << icount << endl;  
  30.     OutFile.close();  
  31. }  
  32.   
  33. // argc, argv are the entire command line, including pin -t <toolname> -- ...  
  34. int main(int argc, char * argv[])  
  35. {  
  36.     // Initialize pin  
  37.     PIN_Init(argc, argv);  
  38.   
  39.     // Register Instruction to be called to instrument instructions  
  40.     INS_AddInstrumentFunction(Instruction, 0);  
  41.   
  42.     // Register Fini to be called when the application exits  
  43.     PIN_AddFiniFunction(Fini, 0);  
  44.       
  45.     // Start the program, never returns  
  46.     PIN_StartProgram();  
  47.       
  48.     return 0;  
  49. }  

注释比较详细,很容易看懂

Pin主页:http://www.pintool.org

https://software.intel.com/en-us/articles/pintool-downloads


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值