Actual source code: mem.c
petsc-3.10.5 2019-03-28
Report Typos and Errors

      1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for getpagesize() with c89 */
      2:  #include <petscsys.h>
      3: #if defined(PETSC_HAVE_PWD_H)
      4: #include <pwd.h>
      5: #endif
      6: #include <ctype.h>
      7: #include <sys/stat.h>
      8: #if defined(PETSC_HAVE_UNISTD_H)
      9: #include <unistd.h>
     10: #endif
     11: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
     12: #include <sys/utsname.h>
     13: #endif
     14: #include <fcntl.h>
     15: #include <time.h>
     16: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
     17: #include <sys/systeminfo.h>
     18: #endif

     20: #if defined(PETSC_HAVE_SYS_RESOURCE_H)
     21: #include <sys/resource.h>
     22: #endif
     23: #if defined(PETSC_HAVE_SYS_PROCFS_H)
     24: /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */
     25: #include <sys/procfs.h>
     26: #endif
     27: #if defined(PETSC_HAVE_FCNTL_H)
     28: #include <fcntl.h>
     29: #endif

     31: /*@
     32:    PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
     33:    for the program.

     35:    Not Collective

     37:    Output Parameter:
     38: .   mem - memory usage in bytes

     40:    Options Database Key:
     41: .  -memory_view - Print memory usage at end of run
     42: .  -malloc_log - Activate logging of memory usage

     44:    Level: intermediate

     46:    Notes:
     47:    The memory usage reported here includes all Fortran arrays
     48:    (that may be used in application-defined sections of code).
     49:    This routine thus provides a more complete picture of memory
     50:    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
     51:    hardwired arrays.

     53: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage(), PetscMemorySetGetMaximumUsage(), PetscMemoryView()

     55:    Concepts: resident set size
     56:    Concepts: memory usage

     58: @*/
     59: PetscErrorCode  PetscMemoryGetCurrentUsage(PetscLogDouble *mem)
     60: {
     61: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
     62:   FILE       *file;
     63:   int        fd;
     64:   char       proc[PETSC_MAX_PATH_LEN];
     65:   prpsinfo_t prusage;
     66: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
     67:   long       *ii = sbreak(0);
     68:   int        fd  = ii - (long*)0;
     69: #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
     70:   FILE       *file;
     71:   char       proc[PETSC_MAX_PATH_LEN];
     72:   int        mm,rss,err;
     73: #elif defined(PETSC_HAVE_GETRUSAGE)
     74:   static struct rusage temp;
     75: #endif

     78: #if defined(PETSC_USE_PROCFS_FOR_SIZE)

     80:   sprintf(proc,"/proc/%d",(int)getpid());
     81:   if ((fd = open(proc,O_RDONLY)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",file);
     82:   if (ioctl(fd,PIOCPSINFO,&prusage) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Unable to access system file %s to get memory usage data",file);
     83:   *mem = (PetscLogDouble)prusage.pr_byrssize;
     84:   close(fd);

     86: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)

     88:   *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */

     90: #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
     91:   sprintf(proc,"/proc/%d/statm",(int)getpid());
     92:   if (!(file = fopen(proc,"r"))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",proc);
     93:   if (fscanf(file,"%d %d",&mm,&rss) != 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"Failed to read two integers (mm and rss) from %s",proc);
     94:   *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
     95:   err  = fclose(file);
     96:   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");

     98: #elif defined(PETSC_HAVE_GETRUSAGE)
     99:   getrusage(RUSAGE_SELF,&temp);
    100: #if defined(PETSC_USE_KBYTES_FOR_SIZE)
    101:   *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
    102: #elif defined(PETSC_USE_PAGES_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
    103:   *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
    104: #else
    105:   *mem = temp.ru_maxrss;
    106: #endif

    108: #else
    109:   *mem = 0.0;
    110: #endif
    111:   return(0);
    112: }

    114: PETSC_INTERN PetscBool      PetscMemoryCollectMaximumUsage;
    115: PETSC_INTERN PetscLogDouble PetscMemoryMaximumUsage;

    117: PetscBool      PetscMemoryCollectMaximumUsage = PETSC_FALSE;
    118: PetscLogDouble PetscMemoryMaximumUsage        = 0;

    120: /*@
    121:    PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
    122:    for the program.

    124:    Not Collective

    126:    Output Parameter:
    127: .   mem - memory usage in bytes

    129:    Options Database Key:
    130: .  -memory_view - Print memory usage at end of run
    131: .  -malloc_log - Activate logging of memory usage

    133:    Level: intermediate

    135:    Notes:
    136:    The memory usage reported here includes all Fortran arrays
    137:    (that may be used in application-defined sections of code).
    138:    This routine thus provides a more complete picture of memory
    139:    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
    140:    hardwired arrays.

    142: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
    143:           PetscMemorySetGetMaximumUsage()

    145:    Concepts: resident set size
    146:    Concepts: memory usage

    148: @*/
    149: PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
    150: {
    152:   if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
    153:   *mem = PetscMemoryMaximumUsage;
    154:   return(0);
    155: }

    157: /*@
    158:    PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
    159:        PetscMemoryGetMaximumUsage() will work.

    161:    Not Collective

    163:    Options Database Key:
    164: .  -memory_view - Print memory usage at end of run
    165: .  -malloc_log - Activate logging of memory usage

    167:    Level: intermediate

    169: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
    170:           PetscMemoryGetMaximumUsage()

    172:    Concepts: resident set size
    173:    Concepts: memory usage

    175: @*/
    176: PetscErrorCode  PetscMemorySetGetMaximumUsage(void)
    177: {
    179:   PetscMemoryCollectMaximumUsage = PETSC_TRUE;
    180:   return(0);
    181: }