我有一个在Linux上运行的C++应用程序,我正在优化它.如何确定代码的哪些区域运行缓慢?
我想在每个函数调用之前添加一些代码来进行一些检查.我知道的唯一方法是:
#define SOME_CODE printf("doing something...");
class testObject
{
void function1()
{
SOME_CODE
...
}
void function2()
{
SOME_CODE
...
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的方法来实现这一目标?我正在寻找一种方法,所以我没有手动为每个函数添加"SOME_CODE".
这些天我在Ubuntu上用g ++共享对象(.so)文件测试-finstrument-functions.我发现了一个奇怪的行为 - 只有在静态链接库时,-finstrument-functions似乎才有效.如果我用dlopen/dlsym等链接到库,代码的功能仍然有效,但它不会调用__cyg_profile*函数.
以下是一些快速重现问题的代码:
MyLib.h
#ifndef __MYLIB_H__
#define __MYLIB_H__
class MyLib
{
public:
void sayHello();
};
#endif
Run Code Online (Sandbox Code Playgroud)
MyLib.cpp
#include "MyLib.h"
#include <iostream>
using namespace std;
void MyLib::sayHello()
{
cout<<"Hello"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
MyLibStub.cpp(.so的C接口)
#include "MyLib.h"
extern "C" void LoadMyLib ()
{
MyLib().sayHello();
}
Run Code Online (Sandbox Code Playgroud)
Trace.cpp
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
void __cyg_profile_func_enter(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
void __cyg_profile_func_exit(void *this_fn, void *call_site)
__attribute__((no_instrument_function));
}
#endif
void __cyg_profile_func_enter(void* this_fn, void* call_site)
{
printf("entering %p\n", (int*)this_fn);
}
void __cyg_profile_func_exit(void* this_fn, void* call_site)
{ …Run Code Online (Sandbox Code Playgroud)