memusage.C File Reference

#include "memusage.h"
#include "converse.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <malloc.h>

Go to the source code of this file.

Functions

unsigned long memusage_mstats ()
unsigned long memusage_mallinfo ()
unsigned long memusage_ps ()
unsigned long memusage_proc_self_stat ()
unsigned long memusage (const char **source)

Function Documentation

unsigned long memusage ( const char **  source = 0  ) 

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 158 of file memusage.C.

References memusage_mallinfo(), memusage_mstats(), memusage_proc_self_stat(), and memusage_ps().

Referenced by main::main(), memusage_kB(), and memusage_MB().

00158                                             {
00159 
00160   unsigned long memtotal = 0;
00161   const char* s = "ERROR";
00162 
00163   if ( ! CmiMemoryIs(CMI_MEMORY_IS_OS) ) {
00164     memtotal = CmiMemoryUsage();  s = "CmiMemoryUsage";
00165   }
00166 
00167 #if CMK_BLUEGENEQ
00168   if( ! memtotal) { memtotal = memusage_bgq(); s="Kernel_GetMemorySize on BG/Q"; }
00169 #endif
00170 
00171 #if CMK_BLUEGENEP
00172   if( ! memtotal) { memtotal = memusage_bgp(); s="mallinfo on BG/P"; }
00173 #endif
00174 
00175 #if defined(WIN32) && !defined(__CYGWIN__)
00176   if ( ! memtotal ) {
00177     memtotal = CmiMemoryUsage();  s = "GetProcessMemoryInfo";
00178   }
00179 #endif
00180 
00181   if ( ! memtotal ) {
00182     memtotal = memusage_proc_self_stat();  s = "/proc/self/stat";
00183   }
00184 
00185   if ( ! memtotal ) { memtotal = memusage_mstats(); s = "mstats"; }
00186 
00187   if ( ! memtotal ) { memtotal = memusage_mallinfo(); s = "mallinfo"; }
00188 
00189   if ( ! memtotal ) { memtotal = memusageinit::memusage_sbrk(); s = "sbrk"; }
00190 
00191   if ( ! memtotal ) { memtotal = memusage_ps(); s = "ps"; }
00192 
00193   if ( ! memtotal ) { memtotal = CmiMemoryUsage();  s = "CmiMemoryUsage"; }
00194 
00195   if ( ! memtotal ) s = "nothing";
00196 
00197   if ( source ) *source = s;
00198 
00199   return memtotal;
00200 
00201 }

unsigned long memusage_mallinfo (  ) 

Definition at line 68 of file memusage.C.

References if().

Referenced by memusage().

00068                                   {
00069   struct mallinfo mi = mallinfo();
00070   // unsigned long memtotal = mi.usmblks + mi.uordblks + mi.hblkhd;
00071   unsigned long memtotal = (unsigned int) mi.uordblks;
00072   unsigned long memtotal2 = (unsigned int) mi.usmblks;
00073   memtotal2 += (unsigned int) mi.hblkhd;
00074   if ( memtotal2 > memtotal ) memtotal = memtotal2;
00075 
00076   // printf("mallinfo %d %d %d\n", mi.usmblks, mi.uordblks, mi.hblkhd);
00077 
00078   return memtotal;
00079 }

unsigned long memusage_mstats (  )  [inline]

Definition at line 60 of file memusage.C.

Referenced by memusage().

00060 { return 0; }

unsigned long memusage_proc_self_stat (  )  [inline]

Definition at line 131 of file memusage.C.

Referenced by memusage().

00131                                                {
00132 #ifdef NO_PS
00133   return 0;
00134 #else
00135   static int failed_once = 0;
00136   if ( failed_once ) return 0;  // no point in retrying
00137 
00138   FILE *f = fopen("/proc/self/stat","r");
00139   if ( ! f ) { failed_once = 1; return 0; }
00140   for ( int i=0; i<22; ++i ) fscanf(f,"%*s");
00141 #if defined(NAMD_CUDA) || defined(NAMD_MIC)
00142   // skip vss, next value is rss in pages
00143   fscanf(f,"%*s");
00144 #endif
00145   unsigned long vsz = 0;  // should remain 0 on failure
00146   fscanf(f,"%lu",&vsz);
00147   fclose(f);
00148 #if defined(NAMD_CUDA) || defined(NAMD_MIC)
00149   vsz *= sysconf(_SC_PAGESIZE);
00150 #endif
00151   if ( ! vsz ) failed_once = 1;
00152   // printf("/proc/self/stat reports %d MB\n", vsz/(1024*1024));
00153   return vsz;
00154 #endif
00155 }

unsigned long memusage_ps (  )  [inline]

Definition at line 86 of file memusage.C.

Referenced by memusage().

00086                                    {
00087 #ifdef NO_PS
00088   return 0;
00089 #else
00090   char pscmd[100];
00091 #if defined(NAMD_CUDA) || defined(NAMD_MIC)
00092   sprintf(pscmd, "/bin/ps -o rss= -p %d", getpid());
00093 #else
00094   sprintf(pscmd, "/bin/ps -o vsz= -p %d", getpid());
00095 #endif
00096   unsigned long vsz = 0;
00097   FILE *p = popen(pscmd, "r");
00098   if ( p ) {
00099     fscanf(p, "%ld", &vsz);
00100     pclose(p);
00101   }
00102   return ( vsz * (unsigned long) 1024 );
00103 #endif
00104 }


Generated on 6 Dec 2019 for NAMD by  doxygen 1.6.1