C++Guns – RoboBlog

26.06.2016

malloc info fortran

Filed under: Allgemein — Tags: — Thomas @ 00:06

Code snipped to get detailed information about memory usage

mallocinfo.f95.zip

Related: Code snipped to read /proc status with fortran.


module MallocInfo_m
  use :: iso_c_binding
  implicit none
  
    !> This structure type is used to return information about the dynamic memory allocator.
    type, bind(c) :: MallInfo_t
      !> This is the total size of memory allocated with sbrk by malloc, in bytes.
      integer(c_int) :: arena                  
      !> This is the number of chunks not in use. (The memory allocator internally gets chunks of memory from the operating system, and then carves them up to satisfy individual malloc requests; see Efficiency and Malloc.)
      integer(c_int) :: ordblks        
      !> This field is unused.
      integer(c_int) :: smblks        
      !> This is the total number of chunks allocated with mmap.
      integer(c_int) :: hblks        
      !> This is the total size of memory allocated with mmap, in bytes.
      integer(c_int) :: hblkhd        
      !> This field is unused.
      integer(c_int) :: usmblks        
      !> This field is unused.
      integer(c_int) :: fsmblks        
      !> This is the total size of memory occupied by chunks handed out by malloc.
      integer(c_int) :: uordblks        
      !> This is the total size of memory occupied by free (not in use) chunks.
      integer(c_int) :: fordblks        
      !> This is the size of the top-most releasable chunk that normally borders the end of the heap (i.e., the high end of the virtual address space’s data segment).
      integer(c_int) :: keepcost      
    end type
    
    interface
      function mallinfo() bind(c, name="mallinfo") result(data)
        use :: iso_c_binding
        implicit none
        
        type, bind(c) :: MallInfo_t      
          integer(c_int) :: arena                        
          integer(c_int) :: ordblks              
          integer(c_int) :: smblks        
          integer(c_int) :: hblks        
          integer(c_int) :: hblkhd        
          integer(c_int) :: usmblks        
          integer(c_int) :: fsmblks        
          integer(c_int) :: uordblks        
          integer(c_int) :: fordblks        
          integer(c_int) :: keepcost      
        end type
        type(MallInfo_t) :: data
      end function
    end interface
  
  contains
  
  subroutine getMallocInfo(malinfo)
    implicit none
    type(MallInfo_t), intent(out) :: malinfo
    malinfo = mallinfo()
  end subroutine
  
  subroutine printMallInfo(malinfo)
    implicit none
    type(MallInfo_t), intent(in) :: malinfo
    write(*,*) "Total size of memory allocated with sbrk by malloc in byte.  ", malinfo%arena
    write(*,*) "Total size of memory allocated with mmap, in bytes.          ", malinfo%hblkhd
    write(*,*) "Total size of memory occupied by chunks handed out by malloc.", malinfo%uordblks
    write(*,*) "Total number of chunks allocated with mmap.                  ", malinfo%hblks
    write(*,*) "Number of chunks not in use.                                 ", malinfo%ordblks
    write(*,*) "Total size of memory occupied by free (not in use) chunks.   ", malinfo%fordblks
    write(*,*) "Size of the top-most releasable chunk borders end of the heap", malinfo%keepcost    
  end subroutine
  
  
end module

program test
  use MallocInfo_m
  implicit none
  type(MallInfo_t) :: mallinfos(10000)  
  integer :: i, nInfos
  integer, allocatable :: data(:)
  
  allocate(data(0))
  nInfos = 0
  do i=1, 10
    write(*,*) "Iteration",i
    deallocate(data)
    allocate(data(i*100000))
    nInfos = nInfos+1
    call getMallocInfo(mallinfos(nInfos))    
    call printMallInfo(mallInfos(nInfos))
    call sleep(1)
  end do
  
   do i=10, 1, -1
    write(*,*) "Iteration",i
    deallocate(data)
    allocate(data(i*100000))
    nInfos = nInfos+1
    call getMallocInfo(mallinfos(nInfos))    
    call printMallInfo(mallInfos(nInfos))    
    call sleep(1)
  end do
  
  write(*,*) "Total size of memory allocated with sbrk. min, mean, max", minval(mallinfos(1:nInfos)%arena), sum(mallinfos(1:nInfos)%arena)/nInfos, maxval(mallinfos(1:nInfos)%arena)
end program

./a.out
Iteration 1
Total size of memory allocated with sbrk by malloc in byte. 135168
Total size of memory allocated with mmap, in bytes. 401408
Total size of memory occupied by chunks handed out by malloc. 7080
Total number of chunks allocated with mmap. 1
Number of chunks not in use. 1
Total size of memory occupied by free (not in use) chunks. 128088
Size of the top-most releasable chunk borders end of the heap 128088
Iteration 2
Total size of memory allocated with sbrk by malloc in byte. 135168
Total size of memory allocated with mmap, in bytes. 802816
Total size of memory occupied by chunks handed out by malloc. 7080
Total number of chunks allocated with mmap. 1
Number of chunks not in use. 1
Total size of memory occupied by free (not in use) chunks. 128088
Size of the top-most releasable chunk borders end of the heap 128088
.
.
.
Total size of memory allocated with sbrk. min, mean, max 135168 155443 540672

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress