Profiling a C or C++ based application that never

2019-05-06 22:03发布

问题:

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 ..

回答1:

I'd consider adding a graceful exit from the program.



回答2:

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.



回答3:

Profiling is intrusive, so you don't want to deploy the app with the profiler attached, anyway. Therefore, include some #ifdef PROFILE_MODE-code that exits the app after an appropriate amount of time. Compile with -DPROFLILE_MODE, profile. Deploy without PROFILE_MODE.



回答4:

Modify your program slightly so that you can request a Valgrind leak check at any point - when the command to do that is recieved, your program should use VALGRIND_DO_LEAK_CHECK from memcheck.h (this will have no effect if the program isn't running under Valgrind).



回答5:

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
}


回答6:

Rational Purify can do that, at least on windows. There seem to be a linux version but I don't know if it can do the same.



回答7:

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.



回答8:

Have you tried GNU Gprof?

Note that in this document, "cc" and "gcc" are interchangeable. ("cc" is assumed as an alias for "gcc.") http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html



回答9:

Your question reads as if you were looking for top. It nicely displays (among other things) the current memory consumption of all running processes. (Limited to one page in the terminal.) On Linux, hit “M” to sort by memory usage. The man page shows more options for sorting and filtering.



回答10:

I have used rational purify API's to check incremental leaks. Haven't used the API's in linux. I found the VALGRIND_DO_LEAK_CHECK option in Valgrind User Manual, I think this would suffice your requirement.



回答11:

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



回答12:

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.



回答13:

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