MAPL_MemCommited Subroutine

public subroutine MAPL_MemCommited(memtotal, committed_as, percent_committed, RC)

Arguments

Type IntentOptional Attributes Name
real, intent(out) :: memtotal
real, intent(out) :: committed_as
real, intent(out) :: percent_committed
integer, intent(out), optional :: RC

Calls

proc~~mapl_memcommited~~CallsGraph proc~mapl_memcommited MAPL_MemCommited mapl_rtrn mapl_rtrn proc~mapl_memcommited->mapl_rtrn proc~mapl_return MAPL_Return proc~mapl_memcommited->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_memcommited~~CalledByGraph proc~mapl_memcommited MAPL_MemCommited proc~mapl_memreport MAPL_MemReport proc~mapl_memreport->proc~mapl_memcommited

Source Code

subroutine MAPL_MemCommited ( memtotal, committed_as, percent_committed, RC )

real, intent(out) :: memtotal, committed_as, percent_committed
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

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

#ifdef sysDarwin
  memtotal = 0.0
  committed_as = 0.0
  percent_committed = 0.0
  _RETURN(ESMF_SUCCESS)
#endif

  multiplier = 1.0

  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
     committed_as = 0.0
     percent_committed = 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
      if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
        multiplier = 1.0/1024. ! Convert from kB to MB
      memtotal = memtotal * multiplier
    endif
    if ( INDEX ( string, 'Committed_AS:' ) == 1 ) then  ! Resident Memory
      read (string(14:LEN_TRIM(string)-2),*) committed_as
      if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
        multiplier = 1.0/1024. ! Convert from kB to MB
      committed_as = committed_as * multiplier
    endif
  enddo
20 close(mem_unit)

   percent_committed = 100.0*(committed_as/memtotal)

   _RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemCommited