예제 #1
0
int gptlget_memusage (int *size, int *rss, int *share, int *text, int *datastack)
{
  return GPTLget_memusage (size, rss, share, text, datastack);
}
예제 #2
0
int GPTLprint_memusage (const char *str)
{
  int size, size2;                        /* process size (returned from OS) */
  int rss, rss2;                          /* resident set size (returned from OS) */
  int share, share2;                      /* shared data segment size (returned from OS) */
  int text, text2;                        /* text segment size (returned from OS) */
  int datastack, datastack2;              /* data/stack size (returned from OS) */
  static int bytesperblock = -1;          /* convert to bytes (init to invalid) */
  static const int nbytes = 1024*1024*10; /* allocate 10 MB */
  static double blockstomb;               /* convert blocks to MB */
  void *space;                            /* allocated space */
  
  if (GPTLget_memusage (&size, &rss, &share, &text, &datastack) < 0)
    return -1;

#if (defined HAVE_SLASHPROC || defined __APPLE__)
  /*
  ** Determine size in bytes of memory usage info presented by the OS. Method: allocate a 
  ** known amount of memory and see how much bigger the process becomes.
  */

  if (convert_to_mb && bytesperblock == -1 && (space = malloc (nbytes))) {
    if (GPTLget_memusage (&size2, &rss2, &share2, &text2, &datastack2) == 0) {
      /*
      ** Estimate bytes per block, then refine to nearest power of 2.
      ** The assumption is that the OS presents memory usage info in
      ** units that are a power of 2. 
      */
      bytesperblock = (int) ((nbytes / (double) (size2 - size)) + 0.5);
      bytesperblock = nearest_powerof2 (bytesperblock);
      blockstomb = bytesperblock / (1024.*1024.);
      printf ("GPTLprint_memusage: Using bytesperblock=%d\n", bytesperblock);
    }
    free (space);
  }
  
  if (bytesperblock > 0)
    printf ("%s size=%.1f MB rss=%.1f MB share=%.1f MB text=%.1f MB datastack=%.1f MB\n", 
	    str, size*blockstomb, rss*blockstomb, share*blockstomb, 
	    text*blockstomb, datastack*blockstomb);
  else
    printf ("%s size=%d rss=%d share=%d text=%d datastack=%d\n", 
	    str, size, rss, share, text, datastack);

#else

  /*
  ** Use max rss as returned by getrusage. If someone knows how to 
  ** get the process size under AIX please tell me.
  */

  bytesperblock = 1024;
  blockstomb = bytesperblock / (1024.*1024.);
  if (convert_to_mb)
    printf ("%s max rss=%.1f MB\n", str, rss*blockstomb);
  else
    printf ("%s max rss=%d\n", str, rss);
#endif

  return 0;
}