Hello everybody. I've spent the last 2 or 3 (4?) days trying to figure out how to do accuratelly what's written on the subject. Hope will help someone. I have some questions, too
All was tested on 7.0-RELEASE, net-snmp-5.4.1.2_2. One of my goals was to find where the snmp values come from.
You could use 2 subtrees to collect system memory information. These are HOST-RESOURCES-MIB and UCD-SNMP-MIB.
HOST-RESOURCES-MIB::hrStorage*
==============================
hw.(physmem/pagesize) ->hrStorageSize.1 (Physical memory)
vm.vmtotal."Free Memory Pages"(t_free) ->hrStorageUsed.1
vm.vmtotal."Real Memory: total"(t_rm) ->hrStorageSize.2 (Real memory)
vm.vmtotal."Real Memory: active"(t_arm) ->hrStorageUsed.2
vm.vmtotal."Virtual Memory: total"(t_vm) ->hrStorageSize.3 (Virtual memory)
vm.vmtotal."Virtual Memory: active"(t_avm) ->hrStorageUsed.3
vfs.bufspace ->hrStorageSize.6 (Memory Buffer)
vm.stats.vm.(v_cache_count + v_inactive_count) ->hrStorageSize.7 (Cached memory)
vm.vmtotal."Shared Virtual Memory: total"(t_vmshr) ->hrStorageSize.8 (Shared virtual memory)
vm.vmtotal."Shared Virtual Memory: active"(t_avmshr) ->hrStorageUsed.8
vm.vmtotal."Shared Real Memory: total"(t_rmshr) ->hrStorageSize.9 (Shared real memory)
vm.vmtotal."Shared Real Memory: active"(t_armshr) ->hrStorageUsed.9
kvm_getswapinfo() ->hrStorage{Size, Used}.10 (Swap space)
===============================
The '->' means the left side (sysctl oids) determines the right side (snmp oids) using simple arithmetics with hw.pagesize and the *Size preceding value (where applicable).
(By the way, 'real' means 'resident'.)
The kernel computes the free pages value like that:
total.t_free = cnt.v_free_count + cnt.v_cache_count;
as written in vm_meter.c.
But, the "Free Memory Pages" values (from `sysctl vm.vmtotal`) is always different from the free physical memory value returned from HOST-RESOURCES-MIB (which equals, all the time, to UCD-SNMP-MIB::memAvailReal.0), which again is always equal to the sum of v_free_count and v_cache_count returned by `sysctl vm`, multiplied by 4 (page size in kB).
So:
free memory = vm.stats.vm.(free+cache)_page_count * hw.pagesize
cached memory = vm.stats.vm.(cache+inactive)_page_count * hw.pagesize
1. Why is "Free Memory Pages" (?= t_free) from `sysctl vm.vmtotal` different from the free memory calculated as above?
2. Why counting v_cache_count twice: one time for the free memory and one time for the cached memory? Now we can not sum them (not many reasons to do that, but still).
The UCD-SNMP-MIB returned values (although related to those from HOST-RESOURCES-MIB, except memTotalFree) remain a mistery to me even after reading memory_freebsd2.c from the net-snmp source tree. It seems they are computed somewhere else, but I could not figure where.
From observations:
memTotalSwap = 4 * hrStorageSize.3
memAvailSwap = 4* (hrStorageSize.3 - hrStorageUsed.3)
memTotalReal = 4 * hrStorageSize.1
memAvailReal = 4 * (hrStorageSize.1 - hrStorageUsed.1)
memTotalFree = ???? (have no ideea)
memShared = 4 * hrStorageSize.8
memBuffer = 4 * hrStorageSize.6
memCached = 4 * hrStorageSize.7
'4' is the hrStorageAllocationUnits size in kB and 'Real' means 'physical' here.
3. Where are these values (UCD-SNMP-MIB::mem*) computed?