Actual source code: mem.c

 2:  #include petsc.h
 3:  #include petscsys.h
  4: #include "petscfix.h"
  5: #if defined(PETSC_HAVE_PWD_H)
  6: #include <pwd.h>
  7: #endif
  8: #include <ctype.h>
  9: #include <sys/types.h>
 10: #include <sys/stat.h>
 11: #if defined(PETSC_HAVE_UNISTD_H)
 12: #include <unistd.h>
 13: #endif
 14: #if defined(PETSC_HAVE_STDLIB_H)
 15: #include <stdlib.h>
 16: #endif
 17: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 18: #include <sys/utsname.h>
 19: #endif
 20: #include <fcntl.h>
 21: #include <time.h>  
 22: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 23: #include <sys/systeminfo.h>
 24: #endif
 25: #include "petscfix.h"

 27: #if defined (PETSC_HAVE_SYS_RESOURCE_H)
 28: #include <sys/resource.h>
 29: #endif
 30: #if defined(PETSC_HAVE_SYS_PROCFS_H)
 31: /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */
 32: #include <sys/procfs.h>
 33: #endif
 34: #if defined(PETSC_HAVE_FCNTL_H)
 35: #include <fcntl.h>
 36: #endif

 40: /*@C
 41:    PetscGetResidentSetSize - Returns the maximum resident set size (memory used)
 42:    for the program.

 44:    Not Collective

 46:    Output Parameter:
 47: .   mem - memory usage in bytes

 49:    Options Database Key:
 50: .  -get_resident_set_size - Print memory usage at end of run
 51: .  -trmalloc_log - Activate logging of memory usage

 53:    Level: intermediate

 55:    Notes:
 56:    The memory usage reported here includes all Fortran arrays 
 57:    (that may be used in application-defined sections of code).
 58:    This routine thus provides a more complete picture of memory
 59:    usage than PetscTrSpace() for codes that employ Fortran with
 60:    hardwired arrays.

 62: .seealso: PetscTrSpace()

 64:    Concepts: resident set size
 65:    Concepts: memory usage

 67: @*/
 68: PetscErrorCode PetscGetResidentSetSize(PetscLogDouble *mem)
 69: {
 70: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
 71:   FILE            *file;
 72:   int             fd;
 73:   char            proc[PETSC_MAX_PATH_LEN];
 74:   prpsinfo_t      prusage;
 75: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
 76:   long            *ii = sbreak(0);
 77:   int             fd = ii - (long*)0;
 78: #elif defined(PETSC_USE_PROC_FOR_SIZE)
 79:   FILE            *file;
 80:   char            proc[PETSC_MAX_PATH_LEN];
 81: #elif defined(PETSC_HAVE_NO_GETRUSAGE)
 82: #else
 83:   static struct   rusage temp;
 84: #endif

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