跟踪Fortran 90中的内存使用情况

Ele*_*ios 7 memory fortran

我试图跟踪Fortran 90程序中子程序的内存使用情况和CPU时间.要跟踪cpu时间的跟踪,我使用以下内容:

call cpu_time(tic) call subroutine(args) call cpu_time(toc) time = toc-tic

有没有办法做类似记录内存使用的东西?做这个的最好方式是什么?在此先感谢您的帮助.

小智 11

liskawc有一个非常好的解决方案,我一直在寻找这样的东西.

他要求提供反馈,有几个方面可以改进.

  • 有几个系统调用可以通过直接从Fortran程序读取系统文件来消除
  • 解决方案取决于users目录中的临时文件
  • 我的fortran编译器不喜欢打开以波浪号开头的文件

我修改了原来的程序来克服这些问题:

subroutine system_mem_usage(valueRSS)
implicit none
use ifport !if on intel compiler
integer, intent(out) :: valueRSS

character(len=200):: filename=' '
character(len=80) :: line
character(len=8)  :: pid_char=' '
integer :: pid
logical :: ifxst

valueRSS=-1    ! return negative number if not found

!--- get process ID

pid=getpid()
write(pid_char,'(I8)') pid
filename='/proc/'//trim(adjustl(pid_char))//'/status'

!--- read system file

inquire (file=filename,exist=ifxst)
if (.not.ifxst) then
  write (*,*) 'system file does not exist'
  return
endif

open(unit=100, file=filename, action='read')
do
  read (100,'(a)',end=120) line
  if (line(1:6).eq.'VmRSS:') then
     read (line(7:),*) valueRSS
     exit
  endif
enddo
120 continue
close(100)

return
end subroutine system_mem_usage
Run Code Online (Sandbox Code Playgroud)

如果您可以进一步改进此计划,请随时更新!


小智 4

根据内存使用情况,我假设您在 Linux 机器上的程序中的给定时刻需要 RSS。如果您不想使用分析器,那么这个例程可能会有所帮助:

我使用这个例程(说实话,实际上很难看),但到目前为止还有效。子程序简要说明:

当您调用子例程时,它会发出一系列系统调用,在 /proc/ 文件系统中搜索有关您的程序的信息。无论里面有什么,如果你修改这个子,我会专门使用这个来获取 RSS 值。该例程将 valueRSS 作为字符返回,如果您希望了解程序中 RAM 使用情况的概况,则可以将其写入特定文件。您还可以将字符串转换为数字,但如果您需要的话,我将其留给您。

subroutine system_mem_usage(valueRSS)
 use ifport !if on intel compiler
 character(len=30) :: count_char,pid_char, dummy
 character(len=200) :: filename
 character(len=200) :: command
 integer :: count,pid,res
 character(len=50), intent(out) :: valueRSS

 call system_clock(count)

 pid=getpid()

 write(count_char,'(I10)') count
 write(pid_char,'(I10)') pid

 filename='~/tmp/mem_use.'//trim(count_char)

 command='cat /proc/'//trim(adjustl(pid_char))//'/status >'//trim(adjustl(filename))

 res=system(command)

 command='cat '//trim(adjustl(filename))//' | grep RSS > ~/tmp/rss_use.'//trim(count_char)

 res=system(command)

 open(unit=100, file='~/tmp/rss_use.'//trim(count_char))
 read(100,*) dummy, valueRSS
 close(100)

 return
end subroutine
Run Code Online (Sandbox Code Playgroud)

如果您对 RSS 以外的其他值感兴趣,则只需编辑“grep RSS”部分即可。该子例程还假设您的主目录中有一个 tmp 目录。

哦,是的,请随意修改脚本,如果有人对如何更优雅地做到这一点有任何想法,我将非常感激。