NAMD
Functions
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().

158  {
159 
160  unsigned long memtotal = 0;
161  const char* s = "ERROR";
162 
163  if ( ! CmiMemoryIs(CMI_MEMORY_IS_OS) ) {
164  memtotal = CmiMemoryUsage(); s = "CmiMemoryUsage";
165  }
166 
167 #if CMK_BLUEGENEQ
168  if( ! memtotal) { memtotal = memusage_bgq(); s="Kernel_GetMemorySize on BG/Q"; }
169 #endif
170 
171 #if CMK_BLUEGENEP
172  if( ! memtotal) { memtotal = memusage_bgp(); s="mallinfo on BG/P"; }
173 #endif
174 
175 #if defined(WIN32) && !defined(__CYGWIN__)
176  if ( ! memtotal ) {
177  memtotal = CmiMemoryUsage(); s = "GetProcessMemoryInfo";
178  }
179 #endif
180 
181  if ( ! memtotal ) {
182  memtotal = memusage_proc_self_stat(); s = "/proc/self/stat";
183  }
184 
185  if ( ! memtotal ) { memtotal = memusage_mstats(); s = "mstats"; }
186 
187  if ( ! memtotal ) { memtotal = memusage_mallinfo(); s = "mallinfo"; }
188 
189  if ( ! memtotal ) { memtotal = memusageinit::memusage_sbrk(); s = "sbrk"; }
190 
191  if ( ! memtotal ) { memtotal = memusage_ps(); s = "ps"; }
192 
193  if ( ! memtotal ) { memtotal = CmiMemoryUsage(); s = "CmiMemoryUsage"; }
194 
195  if ( ! memtotal ) s = "nothing";
196 
197  if ( source ) *source = s;
198 
199  return memtotal;
200 
201 }
unsigned long memusage_mallinfo()
Definition: memusage.C:68
unsigned long memusage_ps()
Definition: memusage.C:86
unsigned long memusage_proc_self_stat()
Definition: memusage.C:131
unsigned long memusage_mstats()
Definition: memusage.C:60
unsigned long memusage_mallinfo ( )

Definition at line 68 of file memusage.C.

References if().

Referenced by memusage().

68  {
69  struct mallinfo mi = mallinfo();
70  // unsigned long memtotal = mi.usmblks + mi.uordblks + mi.hblkhd;
71  unsigned long memtotal = (unsigned int) mi.uordblks;
72  unsigned long memtotal2 = (unsigned int) mi.usmblks;
73  memtotal2 += (unsigned int) mi.hblkhd;
74  if ( memtotal2 > memtotal ) memtotal = memtotal2;
75 
76  // printf("mallinfo %d %d %d\n", mi.usmblks, mi.uordblks, mi.hblkhd);
77 
78  return memtotal;
79 }
if(ComputeNonbondedUtil::goMethod==2)
unsigned long memusage_mstats ( )
inline

Definition at line 60 of file memusage.C.

Referenced by memusage().

60 { return 0; }
unsigned long memusage_proc_self_stat ( )
inline

Definition at line 131 of file memusage.C.

Referenced by memusage().

131  {
132 #ifdef NO_PS
133  return 0;
134 #else
135  static int failed_once = 0;
136  if ( failed_once ) return 0; // no point in retrying
137 
138  FILE *f = fopen("/proc/self/stat","r");
139  if ( ! f ) { failed_once = 1; return 0; }
140  for ( int i=0; i<22; ++i ) fscanf(f,"%*s");
141 #if defined(NAMD_CUDA) || defined(NAMD_HIP) || defined(NAMD_MIC)
142  // skip vss, next value is rss in pages
143  fscanf(f,"%*s");
144 #endif
145  unsigned long vsz = 0; // should remain 0 on failure
146  fscanf(f,"%lu",&vsz);
147  fclose(f);
148 #if defined(NAMD_CUDA) || defined(NAMD_HIP) || defined(NAMD_MIC)
149  vsz *= sysconf(_SC_PAGESIZE);
150 #endif
151  if ( ! vsz ) failed_once = 1;
152  // printf("/proc/self/stat reports %d MB\n", vsz/(1024*1024));
153  return vsz;
154 #endif
155 }
unsigned long memusage_ps ( )
inline

Definition at line 86 of file memusage.C.

Referenced by memusage().

86  {
87 #ifdef NO_PS
88  return 0;
89 #else
90  char pscmd[100];
91 #if defined(NAMD_CUDA) || defined(NAMD_HIP) || defined(NAMD_MIC)
92  sprintf(pscmd, "/bin/ps -o rss= -p %d", getpid());
93 #else
94  sprintf(pscmd, "/bin/ps -o vsz= -p %d", getpid());
95 #endif
96  unsigned long vsz = 0;
97  FILE *p = popen(pscmd, "r");
98  if ( p ) {
99  fscanf(p, "%ld", &vsz);
100  pclose(p);
101  }
102  return ( vsz * (unsigned long) 1024 );
103 #endif
104 }