-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMemUtils.F90
More file actions
executable file
·689 lines (581 loc) · 24 KB
/
MemUtils.F90
File metadata and controls
executable file
·689 lines (581 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
!------------------------------------------------------------------------------
! Global Modeling and Assimilation Office (GMAO) !
! Goddard Earth Observing System (GEOS) !
! MAPL Component !
!------------------------------------------------------------------------------
#include "MAPL.h"
!>
!### MODULE: `MAPL_MemUtilsMod`
!
! Author: GMAO SI-Team
!
! `MAPL_MemUtilsMod` -- A Module to query/print memory use per processor (Adapted by WMP from FMS memuse utility)
!
module mapl_MemUtils_mod
use ESMF
use MAPL_CommsMod
use mapl_Shmem_mod
use mapl_ErrorHandling_mod
use mapl_FileIO_mod, only: WRITE_PARALLEL
use, intrinsic :: iso_fortran_env, only: INT64
use, intrinsic :: iso_fortran_env, only: REAL64
use mpi
!Author: Balaji (V.Balaji@noaa.gov)
!Various operations for memory management
!these currently include efficient methods for memory-to-memory copy
!including strided data and arbitrary gather-scatter vectors
!also various memory and cache inquiry operators
implicit none
private
integer(kind=INT64) :: l1_cache_line_size, l1_cache_size, l1_associativity
integer(kind=INT64) :: l2_cache_line_size, l2_cache_size, l2_associativity
logical :: memutils_initialized=.FALSE.
interface memcpy
module procedure memcpy_r8
module procedure memcpy_r8_gather
module procedure memcpy_r8_scatter
module procedure memcpy_r8_gather_scatter
end interface
interface MAPL_MemUtilsWrite
module procedure MAPL_MemUtilsWriteVM
module procedure MAPL_MemUtilsWriteComm
end interface
public MAPL_MemUtilsInit
public MAPL_MemUtilsDisable
public MAPL_MemUtilsWrite
public MAPL_MemUtilsIsDisabled
public MAPL_MemUtilsFree
public MAPL_MemCommited
public MAPL_MemUsed
public MAPL_MemReport
#ifdef sysDarwin
logical, save :: DISABLED = .true.
#else
logical, save :: DISABLED = .false.
#endif
integer, public, parameter :: MAPL_MemUtilsModeNode = 2
integer, public, parameter :: MAPL_MemUtilsModeFull = 1
integer, public, parameter :: MAPL_MemUtilsModeBase = 0
integer, save :: MAPL_MemUtilsMode
real, save :: gmax_save
contains
!********************************************************
logical function MAPL_MemUtilsIsDisabled()
MAPL_MemUtilsIsDisabled = DISABLED
end function MAPL_MemUtilsIsDisabled
!********************************************************
subroutine MAPL_MemUtilsDisable(RC)
integer, optional, intent(OUT) :: RC
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsDisable"
DISABLED = .true.
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsDisable
!********************************************************
!********************************************************
subroutine MAPL_MemUtilsInit(mode,RC)
integer, optional, intent(IN ) :: mode
integer, optional, intent(OUT) :: RC
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsInit"
!initialize memutils module
!currently sets default cache characteristics
!(will provide overrides later)
l1_cache_line_size = 1
l1_cache_size = 1
l1_associativity = 1
l2_cache_line_size = 1
l2_cache_size = 1
l2_associativity = 1
memutils_initialized = .TRUE.
gmax_save = 0.0
if (present(mode)) then
MAPL_MemUtilsMode=mode
else
MAPL_memUtilsMode=MAPL_MemUtilsModeBase
endif
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsInit
!-----------------------------------------------------------------------
! !
!MEMCPY routines: <nelems> real*8 words are copied from RHS to LHS !
! Either side can have constant stride (lhs_stride, rhs_stride) !
! or indexed by a gather/scatter array (lhs_indx, rhs_indx) !
! index arrays are 0-based (i.e C-like not fortran-like) !
! !
! EXAMPLES: !
! !
!Replace !
! a(0:n-1) = b(0:n-1) !
!with !
! call memcpy(a,b,n) !
! !
!Replace !
! a(0:2*n-1:2) = b(0:3*n-1:3) !
!with !
! call memcpy(a,b,dim,n,2,3) !dim.GE.3*n !
! !
!Replace !
! a(0:n-1) = b(indx(1:n)) !
!with !
! call memcpy(a,b,dim,n,1,indx) !dim.GE.max(indx) !
! !
!Replace !
! a(indx(1:n)) = b(0:n-1) !
!with !
! call memcpy(a,b,dim,n,indx,1) !dim.GE.max(indx) !
! !
!Replace !
! a(indxa(1:n)) = b(indxb(1:n)) !
!with !
! call memcpy(a,b,dim,n,indx,indxb) !dim.GE.max(indxa,indxb) !
! !
! There are no error checks!!! (routines are built for speed) !
! Specifically there is no bounds-checking: if the stride or !
! indexing causes you to exceed <dim> you will have done a !
! potentially unsafe memory load !
! !
!-----------------------------------------------------------------------
subroutine memcpy_r8( lhs, rhs, dim, nelems, lhs_stride, rhs_stride )
!base routine: handles constant stride memcpy
!default strides are of course 1
integer, intent(in) :: dim
real(kind=REAL64), dimension(0:dim-1), intent(in) :: rhs
real(kind=REAL64), dimension(0:dim-1), intent(out) :: lhs
integer, intent(in), optional :: nelems, lhs_stride, rhs_stride
integer :: n, rs, ls
!defaults
n = dim
ls = 1
rs = 1
if( PRESENT(nelems) )then
n = nelems
!only check for stride if nelems is present
if( PRESENT(lhs_stride) )ls = lhs_stride
if( PRESENT(rhs_stride) )rs = rhs_stride
endif
if( ls.EQ.1 .AND. rs.EQ.1 )then
lhs(0:n-1) = rhs(0:n-1)
else
lhs(0:n*ls-1:ls) = rhs(0:n*rs-1:rs)
endif
return
end subroutine memcpy_r8
subroutine memcpy_r8_gather( lhs, rhs, dim, nelems, lhs_stride, rhs_indx )
!memcpy routine with gather: copies nelems words from rhs(indx(:)) to lhs(:)
integer, intent(in) :: dim, nelems, lhs_stride
real(kind=REAL64), dimension(0:dim-1), intent(in) :: rhs
real(kind=REAL64), dimension(0:dim-1), intent(out) :: lhs
integer, intent(in), dimension(nelems) :: rhs_indx
lhs(0:nelems*lhs_stride-1:lhs_stride) = rhs(rhs_indx(1:nelems))
return
end subroutine memcpy_r8_gather
subroutine memcpy_r8_scatter( lhs, rhs, dim, nelems, lhs_indx, rhs_stride )
!memcpy routine with scatter: copies nelems words from rhs(:) to lhs(indx(:))
integer, intent(in) :: dim, nelems, rhs_stride
real(kind=REAL64), dimension(0:dim-1), intent(in) :: rhs
real(kind=REAL64), dimension(0:dim-1), intent(out) :: lhs
integer, intent(in), dimension(nelems) :: lhs_indx
lhs(lhs_indx(1:nelems)) = rhs(0:nelems*rhs_stride-1:rhs_stride)
return
end subroutine memcpy_r8_scatter
subroutine memcpy_r8_gather_scatter( lhs, rhs, dim, nelems, lhs_indx, rhs_indx )
!memcpy routine with gather/scatter: copies nelems words from rhs(indx(:)) to lhs(indx(:))
integer, intent(in) :: dim, nelems
real(kind=REAL64), dimension(0:dim-1), intent(in) :: rhs
real(kind=REAL64), dimension(0:dim-1), intent(out) :: lhs
integer, intent(in), dimension(nelems) :: lhs_indx, rhs_indx
lhs(lhs_indx(1:nelems)) = rhs(rhs_indx(1:nelems))
return
end subroutine memcpy_r8_gather_scatter
subroutine MAPL_MemUtilsWriteVM( vm, text, always, RC )
type(ESMF_VM) :: vm
character(len=*), intent(in) :: text
logical, intent(in), optional :: always
integer, optional, intent(OUT ) :: RC
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsWriteVM"
integer :: status
integer :: comm
call ESMF_VMGet(vm,mpicommunicator=comm,rc=status)
_VERIFY(status)
if( PRESENT(always) )then
if( .NOT.always ) then
_RETURN(ESMF_SUCCESS)
endif
call MAPL_MemUtilsWriteComm(text,comm=comm,always=always,rc=status)
_VERIFY(STATUS)
else
if( DISABLED ) then
_RETURN(ESMF_SUCCESS)
endif
call MAPL_MemUtilsWriteComm(text,comm=comm,rc=status)
_VERIFY(STATUS)
end if
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsWriteVM
subroutine MAPL_MemUtilsWriteComm( text, comm, always, RC )
character(len=*), intent(in) :: text
integer, optional :: Comm
logical, intent(in), optional :: always
integer, optional, intent(OUT ) :: RC
real :: mhwm, mrss, memused, swapused
real :: mmin, mmax, mavg, mstd
real :: gmin, gmax, gavg, gstd
real :: lhwm, ghwm
real :: lmem, gmem, lswap, gswap
real :: commitlimit, committed_as
real :: lcommitlimit, lcommitted_as
real :: gcommitlimit, gcommitted_as
integer :: npes
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsWriteComm"
integer :: status
character(len=ESMF_MAXSTR) :: outString
integer :: comm_
if (present(comm)) then
comm_=comm
else
comm_=MPI_COMM_WORLD
end if
if( PRESENT(always) )then
if( .NOT.always ) then
_RETURN(ESMF_SUCCESS)
endif
else
if( DISABLED ) then
_RETURN(ESMF_SUCCESS)
endif
end if
call mem_dump(mhwm, mrss, memused, swapused, commitlimit, committed_as, _RC)
call MPI_Comm_Size(comm_,npes,status)
_VERIFY(status)
if (MAPL_MemUtilsMode == MAPL_MemUtilsModeFull) then
lhwm = mhwm; call MPI_AllReduce(lhwm,ghwm,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
mmin = mrss; call MPI_AllReduce(mmin,gmin,1,MPI_REAL,MPI_MIN,comm_,status)
_VERIFY(STATUS)
mmax = mrss; call MPI_AllReduce(mmax,gmax,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
mavg = mrss; call MPI_AllReduce(mavg,gavg,1,MPI_REAL,MPI_SUM,comm_,status)
_VERIFY(STATUS)
gavg = gavg/npes
mstd = (mrss-gavg)**2; call MPI_AllReduce(mstd,gstd,1,MPI_REAL,MPI_SUM,comm_,status)
_VERIFY(STATUS)
gstd = sqrt( gstd/npes )
gmax_save = gmax
lcommitlimit = commitlimit; call MPI_AllReduce(lcommitlimit,gcommitlimit,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
lcommitted_as = committed_as; call MPI_AllReduce(lcommitted_as,gcommitted_as,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
end if
if (MAPL_MemUtilsMode == MAPL_MemUtilsModeFull .or. MAPL_MemUtilsMode == MAPL_MemUtilsModeBase) then
lmem = memused; call MPI_AllReduce(lmem,gmem,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
lswap = swapused; call MPI_AllReduce(lswap,gswap,1,MPI_REAL,MPI_MAX,comm_,status)
_VERIFY(STATUS)
end if
if (MAPL_MemUtilsMode == MAPL_MemUtilsModeBase) then
write(outString,'(a64,2es11.3)') &
'Mem/Swap Used (MB) at '//trim(text)//'=', gmem, gswap
call WRITE_PARALLEL(trim(outString),format='(a132)')
end if
if (MAPL_MemUtilsMode == MAPL_MemUtilsModeFull) then
write(outString,'(a64,5es11.3)') &
'Memuse(MB) at '//trim(text)//'=', ghwm, gmax, gmin, gavg, gmax-gmax_save
gmax_save = gmax
call WRITE_PARALLEL(trim(outString),format='(a132)')
write(outString,'(a64,2es11.3)') &
'Mem/Swap Used (MB) at '//trim(text)//'=', gmem, gswap
call WRITE_PARALLEL(trim(outString),format='(a132)')
write(outString,'(a64,2es11.3)') &
'CommitLimit/Committed_AS (MB) at '//trim(text)//'=', gcommitlimit, gcommitted_as
call WRITE_PARALLEL(trim(outString),format='(a132)')
end if
if (MAPL_MemUtilsMode == MAPL_MemUtilsModeNode) then
if (MAPL_AmNodeRoot) then
write(*,'(a64,i3,a2,es11.3)')'Memory use at '//trim(text)//' on node ',MAPL_MyNodeNum,': ',memused
end if
end if
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsWriteComm
!#######################################################################
subroutine MAPL_MemUsed ( memtotal, used, percent_used, RC )
use mapl_ErrorHandling_mod, 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',ACTION='READ',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
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',ACTION='READ',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
subroutine mem_dump ( memhwm, memrss, memused, swapused, commitlimit, committed_as, RC )
real, intent(out) :: memhwm, memrss, memused, swapused, commitlimit, committed_as
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) :: proc_self = '/proc/self/status'
character(len=32) :: meminfo = '/proc/meminfo'
character(len=32) :: string
integer(kind=INT64) :: memtot, memfree, swaptot, swapfree
integer :: mem_unit
real :: multiplier
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtils:mem_dump"
integer :: status
memhwm = 0.0
memrss = 0.0
multiplier = 1.0
call get_unit(mem_unit)
open(UNIT=mem_unit,FILE=proc_self,FORM='formatted',ACTION='READ',IOSTAT=STATUS)
_VERIFY(STATUS)
do; read (mem_unit,'(a)', end=10) string
if ( INDEX ( string, 'VmHWM:' ) == 1 ) then ! High Water Mark
read (string(7:LEN_TRIM(string)-2),*) memhwm
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
memhwm = memhwm * multiplier
endif
if ( INDEX ( string, 'VmRSS:' ) == 1 ) then ! Resident Memory
read (string(7:LEN_TRIM(string)-2),*) memrss
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
memrss = memrss * multiplier
endif
enddo
10 close(mem_unit)
call get_unit(mem_unit)
open(UNIT=mem_unit,FILE=meminfo,FORM='formatted',ACTION='READ',IOSTAT=STATUS)
_VERIFY(STATUS)
do; read (mem_unit,'(a)', end=20) string
if ( INDEX ( string, 'MemTotal:' ) == 1 ) then ! High Water Mark
read (string(10:LEN_TRIM(string)-2),*) memtot
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
memtot = memtot * multiplier
endif
if ( INDEX ( string, 'MemFree:' ) == 1 ) then ! High Water Mark
read (string(9:LEN_TRIM(string)-2),*) memfree
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
memfree = memfree * multiplier
endif
if ( INDEX ( string, 'SwapTotal:' ) == 1 ) then ! Resident Memory
read (string(11:LEN_TRIM(string)-2),*) swaptot
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
swaptot = swaptot * multiplier
endif
if ( INDEX ( string, 'SwapFree:' ) == 1 ) then ! Resident Memory
read (string(10:LEN_TRIM(string)-2),*) swapfree
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
swapfree = swapfree * multiplier
endif
if ( INDEX ( string, 'CommitLimit:' ) == 1 ) then ! Resident Memory
read (string(13:LEN_TRIM(string)-2),*) commitlimit
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
commitlimit = commitlimit * 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)
memused = memtot-memfree
swapused = swaptot-swapfree
_RETURN(ESMF_SUCCESS)
end subroutine mem_dump
subroutine MAPL_MemUtilsFree ( totmemfree, RC )
real, intent(out) :: totmemfree
integer, optional, intent(OUT ) :: RC
! This routine returns the amount of free memory on Linux systems.
! It does this by querying a system file (file_name below).
character(len=32) :: meminfo = '/proc/meminfo'
character(len=32) :: string
real :: memfree, buffers, cached
integer :: mem_unit
real :: multiplier
character(len=ESMF_MAXSTR), parameter :: IAm="MAPL_MemUtilsFree"
integer :: status
totmemfree = 0.0
memfree = 0.0
buffers = 0.0
cached = 0.0
multiplier = 1.0
call get_unit(mem_unit)
open(UNIT=mem_unit,FILE=meminfo,FORM='formatted',ACTION='READ',IOSTAT=STATUS)
_VERIFY(STATUS)
do; read (mem_unit,'(a)', end=20) string
if ( INDEX ( string, 'MemFree:' ) == 1 ) then ! Free memory
read (string(9:LEN_TRIM(string)-2),*) memfree
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
memfree = memfree * multiplier
endif
if ( INDEX ( string, 'Buffers:' ) == 1 ) then ! Buffers
read (string(9:LEN_TRIM(string)-2),*) buffers
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
buffers = buffers * multiplier
endif
if ( INDEX ( string, 'Cached:' ) == 1 ) then ! Cached
read (string(8:LEN_TRIM(string)-2),*) cached
if (TRIM(string(LEN_TRIM(string)-1:)) == "kB" ) &
multiplier = 1.0/1024. ! Convert from kB to MB
cached = cached * multiplier
endif
enddo
20 close(mem_unit)
totmemfree = memfree + cached + buffers
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_MemUtilsFree
subroutine get_unit ( iunit )
implicit none
!
integer i
integer ios
integer iunit
logical lopen
iunit = 0
do i = 1, 99
if ( i /= 5 .and. i /= 6 ) then
inquire ( unit = i, opened = lopen, iostat = ios )
if ( ios == 0 ) then
if ( .not. lopen ) then
iunit = i
return
end if
end if
end if
end do
return
end subroutine get_unit
subroutine MAPL_MemReport(comm,file_name,line,decorator,rc)
integer, intent(in) :: comm
character(len=*), intent(in) :: file_name
integer, intent(in) :: line
character(len=*), intent(in), optional :: decorator
integer, intent(out), optional :: rc
real :: mem_total,mem_used,percent_used
real :: committed_total,committed,percent_committed
integer :: rank,status
character(len=:), allocatable :: extra_message
#ifdef sysDarwin
_RETURN(ESMF_SUCCESS)
#endif
call MPI_Barrier(comm,status)
_VERIFY(status)
if (present(decorator)) then
extra_message = decorator
else
extra_message = ""
end if
call MAPL_MemUsed(mem_total,mem_used,percent_used)
call MAPL_MemCommited(committed_total,committed,percent_committed)
call MPI_Comm_Rank(comm,rank,status)
_VERIFY(status)
if (rank == 0) write(*,'("Mem report ",A20," ",A30," ",i7," ",f5.1,"% : ",f5.1,"% Mem Comm:Used")')trim(extra_message),file_name,line,percent_committed,percent_used
end subroutine
end module mapl_MemUtils_mod