Actual source code: mem.c

  1: /*$Id: mem.c,v 1.55 2001/06/21 21:15:26 bsmith Exp $*/

 3:  #include petsc.h
 4:  #include petscsys.h
  5: #include "petscfix.h"
  6: #if defined(PETSC_HAVE_PWD_H)
  7: #include <pwd.h>
  8: #endif
  9: #include <ctype.h>
 10: #include <sys/types.h>
 11: #include <sys/stat.h>
 12: #if defined(PETSC_HAVE_UNISTD_H)
 13: #include <unistd.h>
 14: #endif
 15: #if defined(PETSC_HAVE_STDLIB_H)
 16: #include <stdlib.h>
 17: #endif
 18: #if !defined(PARCH_win32)
 19: #include <sys/utsname.h>
 20: #endif
 21: #if defined(PARCH_win32)
 22: #include <windows.h>
 23: #include <io.h>
 24: #include <direct.h>
 25: #endif
 26: #if defined (PARCH_win32_gnu)
 27: #include <windows.h>
 28: #endif
 29: #include <fcntl.h>
 30: #include <time.h>  
 31: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 32: #include <sys/systeminfo.h>
 33: #endif
 34: #include "petscfix.h"
 35: #ifndef MAXPATHLEN
 36: #define MAXPATHLEN 1024
 37: #endif

 39: #if defined (PETSC_HAVE_SYS_RESOURCE_H)
 40: #include <sys/resource.h>
 41: #endif
 42: #if defined(PETSC_HAVE_SYS_PROCFS_H)
 43: /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */
 44: #include <sys/procfs.h>
 45: #endif
 46: #if defined(PETSC_HAVE_FCNTL_H)
 47: #include <fcntl.h>
 48: #endif

 50: /*@C
 51:    PetscGetResidentSetSize - Returns the maximum resident set size (memory used)
 52:    for the program.

 54:    Not Collective

 56:    Output Parameter:
 57: .   mem - memory usage in bytes

 59:    Options Database Key:
 60: .  -get_resident_set_size - Print memory usage at end of run
 61: .  -trmalloc_log - Activate logging of memory usage

 63:    Level: intermediate

 65:    Notes:
 66:    The memory usage reported here includes all Fortran arrays 
 67:    (that may be used in application-defined sections of code).
 68:    This routine thus provides a more complete picture of memory
 69:    usage than PetscTrSpace() for codes that employ Fortran with
 70:    hardwired arrays.

 72: .seealso: PetscTrSpace()

 74:    Concepts: resident set size
 75:    Concepts: memory usage

 77: @*/
 78: int PetscGetResidentSetSize(PetscLogDouble *foo)
 79: {
 80: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
 81:   int             fd;
 82:   char            proc[1024];
 83:   prpsinfo_t      prusage;
 84: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
 85:   long            *ii = sbreak(0);
 86:   int             fd = ii - (long*)0;
 87: #elif defined(PETSC_HAVE_NO_GETRUSAGE)
 88: #else
 89:   static struct   rusage temp;
 90: #endif

 93: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
 94:   sprintf(proc,"/proc/%d",(int)getpid());
 95:   if ((fd = open(proc,O_RDONLY)) == -1) {
 96:     SETERRQ(PETSC_ERR_FILE_OPEN,"Unable to access system file to get memory usage data");
 97:   }
 98:   if (ioctl(fd,PIOCPSINFO,&prusage) == -1) {
 99:     SETERRQ(PETSC_ERR_FILE_READ,"Unable to access system file  to get memory usage data");
100:   }
101:   *foo = (double)prusage.pr_byrssize;
102:   close(fd);
103: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
104:   *foo = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
105: #elif defined(PETSC_HAVE_NO_GETRUSAGE)
106:   *foo = 0.0;
107: #else
108:   getrusage(RUSAGE_SELF,&temp);
109: #if defined(PETSC_USE_KBYTES_FOR_SIZE)
110:   *foo = 1024.0 * ((double)temp.ru_maxrss);
111: #else
112:   *foo = ((double)getpagesize())*((double)temp.ru_maxrss);
113: #endif
114: #endif
115:   return(0);
116: }