@@ -22,30 +22,63 @@ import (
2222 "golang.org/x/sys/unix"
2323)
2424
25+ type sysctlType uint8
26+
27+ const (
28+ sysctlTypeUint32 sysctlType = iota
29+ sysctlTypeUint64
30+ )
31+
32+ type meminfoSysctl struct {
33+ name string
34+ dataType sysctlType
35+ conversion func (uint64 ) uint64
36+ }
37+
2538func (c * meminfoCollector ) getMemInfo () (map [string ]float64 , error ) {
2639 info := make (map [string ]float64 )
2740
28- size , err := unix .SysctlUint32 ("vm.stats.vm.v_page_size" )
41+ tmp32 , err := unix .SysctlUint32 ("vm.stats.vm.v_page_size" )
2942 if err != nil {
3043 return nil , fmt .Errorf ("sysctl(vm.stats.vm.v_page_size) failed: %s" , err )
3144 }
45+ size := uint64 (tmp32 )
46+ fromPage := func (v uint64 ) uint64 {
47+ return v * size
48+ }
3249
33- for key , v := range map [string ]string {
34- "active" : "vm.stats.vm.v_active_count" ,
35- "inactive" : "vm.stats.vm.v_inactive_count" ,
36- "wire" : "vm.stats.vm.v_wire_count" ,
37- "cache" : "vm.stats.vm.v_cache_count" ,
38- "free" : "vm.stats.vm.v_free_count" ,
39- "swappgsin" : "vm.stats.vm.v_swappgsin" ,
40- "swappgsout" : "vm.stats.vm.v_swappgsout" ,
41- "total" : "vm.stats.vm.v_page_count" ,
50+ for key , v := range map [string ]meminfoSysctl {
51+ "active_bytes" : {"vm.stats.vm.v_active_count" , sysctlTypeUint32 , fromPage },
52+ "inactive_bytes" : {"vm.stats.vm.v_inactive_count" , sysctlTypeUint32 , fromPage },
53+ "wired_bytes" : {"vm.stats.vm.v_wire_count" , sysctlTypeUint32 , fromPage },
54+ "cache_bytes" : {"vm.stats.vm.v_cache_count" , sysctlTypeUint32 , fromPage },
55+ "buffer_bytes" : {"vfs.bufspace" , sysctlTypeUint32 , nil },
56+ "free_bytes" : {"vm.stats.vm.v_free_count" , sysctlTypeUint32 , fromPage },
57+ "size_bytes" : {"vm.stats.vm.v_page_count" , sysctlTypeUint32 , fromPage },
58+ "swap_in_bytes_total" : {"vm.stats.vm.v_swappgsin" , sysctlTypeUint32 , fromPage },
59+ "swap_out_bytes_total" : {"vm.stats.vm.v_swappgsout" , sysctlTypeUint32 , fromPage },
60+ "swap_size_bytes" : {"vm.swap_total" , sysctlTypeUint64 , nil },
4261 } {
43- value , err := unix .SysctlUint32 (v )
62+ var tmp64 uint64
63+ switch v .dataType {
64+ case sysctlTypeUint32 :
65+ tmp32 , err = unix .SysctlUint32 (v .name )
66+ tmp64 = uint64 (tmp32 )
67+ case sysctlTypeUint64 :
68+ tmp64 , err = unix .SysctlUint64 (v .name )
69+ }
4470 if err != nil {
4571 return nil , err
4672 }
47- // Convert metrics to kB (same as Linux meminfo).
48- info [key ] = float64 (value ) * float64 (size )
73+
74+ if v .conversion != nil {
75+ // Convert to bytes.
76+ info [key ] = float64 (v .conversion (tmp64 ))
77+ continue
78+ }
79+
80+ info [key ] = float64 (tmp64 )
4981 }
82+
5083 return info , nil
5184}
0 commit comments