Profiling a C or C++ based application that never

2019-05-06 22:16发布

I have a small doubt regarding profiling applications which never exit until we manually reboot the machine.

I used tools like valgrind which talks about memory leaks or bloating of any application which exits after sometime.

But is there any tool which can be used to tell about memory consumption, bloating, overhead created by the application at various stages if possible?

NOTE: I am more intrested to know about apps which dont exit ... If an app exits I can use tools like valgrind ..

13条回答
Deceive 欺骗
2楼-- · 2019-05-06 22:41

Some tools allow you to force a memory analysis at any point during the program's execution. This method is not as reliable as checking on exit, but it gives you a starting point.

Here's a Windows example using LeakDiag.

查看更多
看我几分像从前
3楼-- · 2019-05-06 22:42

You need stackshots. Either use pstack or lsstack, or just run it under a debugger or IDE and pause it (Ctrl-C) at random. It will not tell you about memory leaks, but it will give you a good idea of how the time is being used and why.

If time is being used because of memory leaks, you will see a good percent of samples ending in memory management routines. If they are in malloc or new, higher up the stack you will see what objects are being allocated and why, and you can consider how to do that less often.

查看更多
乱世女痞
4楼-- · 2019-05-06 22:45

For windows, DebugDiag does that. Generates a report in the end with probable memory leaks. Also has memory pressure analysis. And it's available for free @ microsoft. Download it from here

查看更多
干净又极端
5楼-- · 2019-05-06 22:50

dtrosset's point is well put but apparently misunderstood. Add a means to terminate the program so you can perform a clean analysis. This can be something as simple as adding a signal handler for SIGUSR1, for example, that terminates the program at a point in time you decide. There are a variety of methods at your disposal depending on your OS.

There's a big difference between an application which never exits (embedded, daemons, etc) and one that cannot be exited. The prior is normal, the latter is bad design.

If anything, that application can be forcibly aborted (SIGKILL on *nix, terminate on win32) and you'd get your analysis. That method doesn't give your application the opportunity to clean up before it's destroyed so there will be very likely be retained memory reported.

查看更多
疯言疯语
6楼-- · 2019-05-06 22:52

Work of program that profiling memory leaks is based on detecting memory that was freed by OS not by program.

查看更多
Root(大扎)
7楼-- · 2019-05-06 22:54

You can use GNU gprof, but it has also the problem that it requires an exit of the program. You can overcom this by calling internal functions of gprof. (see below) It may be a real "dirty" hack, depending on the version of gcc and, and, and,... but it works.


#include "sys/gmon.h"


extern "C" //the internal functions and vars of gprof
{
  void moncontrol (int mode);
  void monstartup (unsigned long lowpc, unsigned long highpc);
  void _mcleanup (void);
  extern void _start(void), etext(void);
  extern int __libc_enable_secure;
}

// call this whenever you want to write profiling information to file
void WriteProfilingInformation(char* Name)
{
  setenv("GMON_OUT_PREFIX",Name,1);  // set file name

  int old = __libc_enable_secure; // save old value
  __libc_enable_secure = 0;       // has to be zero to change profile file name
  _mcleanup();
  __libc_enable_secure = old;     // reset to old value

  monstartup(lowpc, highpc);      // restart profiler
  moncontrol(1);                  // enable profiler
}
查看更多
登录 后发表回答