2

I'm having trouble finding a generic code that work, here, for both CentOS 5/6 and CentOS 7 versions pc the /proc/meminfo file. I Want to compute the "Real Used memory" like free does.

(amounts divided by 1024 for easier mental calculus)

CentOS 6 :

[root@web2-1 ~]# cat /proc/meminfo | gawk '{ $2=int($2/1024) ; print $0}' && free -m
MemTotal: 3038 kB
MemFree: 2580 kB
Buffers: 34 kB
Cached: 350 kB
SwapCached: 0 kB
Active: 216 kB
Inactive: 189 kB
Active(anon): 21 kB
Inactive(anon): 1 kB
Active(file): 195 kB
Inactive(file): 188 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 2185 kB
HighFree: 1807 kB
LowTotal: 852 kB
LowFree: 772 kB
SwapTotal: 4095 kB
SwapFree: 4095 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 21 kB
Mapped: 12 kB
Shmem: 1 kB
Slab: 30 kB
SReclaimable: 22 kB
SUnreclaim: 8 kB
KernelStack: 1 kB
PageTables: 1 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 5615 kB
Committed_AS: 145 kB
VmallocTotal: 120 kB
VmallocUsed: 6 kB
VmallocChunk: 99 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2 kB
DirectMap4k: 9 kB
DirectMap2M: 876 kB
             total       used       free     shared    buffers     cached
Mem:          3038        458       2580          1         34        350
-/+ buffers/cache:         72       2966 
Swap:         4095          0       4095 

Desired output : 73 which is MemTotal - MemFree - Buffers - Cached

CentOS 7 :

[maxime@g7r0x64-pkgbuild-mock-dev libexec]$ cat /proc/meminfo | gawk '{ $2=int($2/1024) ; print $0}' && free -m
MemTotal: 993 kB
MemFree: 89 kB
MemAvailable: 710 kB
Buffers: 0 kB
Cached: 681 kB
SwapCached: 5 kB
Active: 341 kB
Inactive: 361 kB
Active(anon): 4 kB
Inactive(anon): 23 kB
Active(file): 336 kB
Inactive(file): 337 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4095 kB
SwapFree: 4072 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 16 kB
Mapped: 11 kB
Shmem: 7 kB
Slab: 134 kB
SReclaimable: 111 kB
SUnreclaim: 23 kB
KernelStack: 2 kB
PageTables: 3 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 4592 kB
Committed_AS: 174 kB
VmallocTotal: 33554431 kB
VmallocUsed: 145 kB
VmallocChunk: 33554275 kB
HardwareCorrupted: 0 kB
AnonHugePages: 2 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2 kB
DirectMap4k: 39 kB
DirectMap2M: 984 kB
              total        used        free      shared  buff/cache   available
Mem:            993          87          89           7         816         710
Swap:          4095          23        4072

Problem is, free gives me 816 MB for buffer + cache at the same time. Where are the missing 130 MB~?

I know I could do MemTotal - MemAvailable to get my real used, but MemAvailable doesn't exist on the previous version, and I would like my code to work with both versions of the meminfo file...

EDIT : by "portable" I mean I'd like the code to work for both 2.6 and 3.10 kernels (which means versions 5-7 of CentOS/RHEL), but I think I'll just have to make a big "if" in my code...

mveroone
  • 937
  • Can you provide the output of free on both machines. Also make clear on which system you have a problem. – Thomas Erker Sep 10 '15 at 10:08
  • A possible guess is that the "Slab" (kernel cache for itsd data sucture) was included in "Cached" before, and now isn't. because in COS7 Total - (Slab + Cache + Buff + Free) = RealUsed – mveroone Sep 10 '15 at 10:10
  • Edited with full meminfo + free. Problem is on CentOS 7, free's "buffer+cache" differs from meminfo "Buffer" + "Cache" unless you also add "Slab" – mveroone Sep 10 '15 at 10:12
  • 1
    /proc/meminfo is Linux only, so there is no need to make things portable as /proc/meminfo itself is non-portable already. – schily Sep 10 '15 at 11:10
  • @schily the OP has shown the output from two different versions of CentOS. They want it to be portable across different Linux flavors. – terdon Sep 10 '15 at 12:12
  • I can't find a changelog in kernel saying Slab was removed from the "Cached" counter, although it's my guess. Anyway, I think it's most usefull for me to use the "Avail" counter, and compute it manually for older versions. – mveroone Sep 10 '15 at 12:50

0 Answers0