MAPL_MemUsed Subroutine

public subroutine MAPL_MemUsed(memtotal, used, percent_used, RC)

Uses

  • proc~~mapl_memused~~UsesGraph proc~mapl_memused MAPL_MemUsed module~mapl_errorhandlingmod MAPL_ErrorHandlingMod proc~mapl_memused->module~mapl_errorhandlingmod module~mapl_throwmod MAPL_ThrowMod module~mapl_errorhandlingmod->module~mapl_throwmod mpi mpi module~mapl_errorhandlingmod->mpi

Arguments

Type IntentOptional Attributes Name
real, intent(out) :: memtotal
real, intent(out) :: used
real, intent(out) :: percent_used
integer, intent(out), optional :: RC

Calls

proc~~mapl_memused~~CallsGraph proc~mapl_memused MAPL_MemUsed mapl_rtrn mapl_rtrn proc~mapl_memused->mapl_rtrn proc~mapl_return MAPL_Return proc~mapl_memused->proc~mapl_return at at proc~mapl_return->at insert insert proc~mapl_return->insert proc~mapl_throw_exception MAPL_throw_exception proc~mapl_return->proc~mapl_throw_exception

Called by

proc~~mapl_memused~~CalledByGraph proc~mapl_memused MAPL_MemUsed proc~mapl_memreport MAPL_MemReport proc~mapl_memreport->proc~mapl_memused

Source Code

  subroutine MAPL_MemUsed ( memtotal, used, percent_used, RC )
     use MAPL_ErrorHandlingMod, only: MAPL_RTRN
     real, intent(out) :: memtotal, used, percent_used
     integer, optional, intent(OUT  ) :: RC

     ! This routine returns the memory usage on Linux systems.
     ! It does this by querying a system file (file_name below).

     character(len=32) :: meminfo   = '/proc/meminfo'
     character(len=32) :: string
     integer :: mem_unit
     real    :: multiplier, available

     character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtils:MAPL_MemUsed"
     integer :: status

#ifdef sysDarwin
     memtotal = 0.0
     used = 0.0
     percent_used = 0.0
     RETURN_(ESMF_SUCCESS)
#else
     available = -1
     memtotal = -1
#endif


     call get_unit(mem_unit)
     open(UNIT=mem_unit,FILE=meminfo,FORM='formatted',IOSTAT=STATUS)
     !_VERIFY(STATUS)

     ! Note: On at least one CircleCI compute machine, this was returning IOSTAT=9, with an IOMSG of:
     !          permission to access file denied, unit 100, file /proc/meminfo
     !       So, instead, if we get this issue, just act like macOS and not return useful info rather
     !       than crashing the model
     if (STATUS /= 0) then
        memtotal = 0.0
        used = 0.0
        percent_used = 0.0
        RETURN_(ESMF_SUCCESS)
     end if

     do
        read (mem_unit,'(a)', end=20) string
        if ( index ( string, 'MemTotal:' ) == 1 ) then  ! High Water Mark
           read (string(10:LEN_trim(string)-2),*) memtotal
           multiplier = 1.0
           if (trim(string(LEN_trim(string)-1:)) == "kB" ) &
                multiplier = 1.0/1024. ! Convert from kB to MB
           memtotal = memtotal * multiplier
        endif
        if ( index ( string, 'MemAvailable:' ) == 1 ) then  ! Resident Memory
           multiplier = 1.0
           read (string(14:LEN_trim(string)-2),*) available
           if (trim(string(LEN_trim(string)-1:)) == "kB" ) &
                multiplier = 1.0/1024. ! Convert from kB to MB
           available = available * multiplier
        endif
     enddo
20   close(mem_unit)

     if (memtotal >= 0 .and. available >= 0) then
        used = memtotal-available
        percent_used = 100.0*(used/memtotal)
     else
        ! fail, but don't crash
        used = -1
        percent_used = -1
     end if

     _RETURN(ESMF_SUCCESS)
  end subroutine MAPL_MemUsed