Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real, | intent(out) | :: | memtotal | |||
real, | intent(out) | :: | used | |||
real, | intent(out) | :: | percent_used | |||
integer, | intent(out), | optional | :: | RC |
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