mem_freebsd.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // +build freebsd
  2. package mem
  3. import (
  4. "context"
  5. "errors"
  6. "unsafe"
  7. "golang.org/x/sys/unix"
  8. )
  9. func VirtualMemory() (*VirtualMemoryStat, error) {
  10. return VirtualMemoryWithContext(context.Background())
  11. }
  12. func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
  13. pageSize, err := unix.SysctlUint32("vm.stats.vm.v_page_size")
  14. if err != nil {
  15. return nil, err
  16. }
  17. pageCount, err := unix.SysctlUint32("vm.stats.vm.v_page_count")
  18. if err != nil {
  19. return nil, err
  20. }
  21. free, err := unix.SysctlUint32("vm.stats.vm.v_free_count")
  22. if err != nil {
  23. return nil, err
  24. }
  25. active, err := unix.SysctlUint32("vm.stats.vm.v_active_count")
  26. if err != nil {
  27. return nil, err
  28. }
  29. inactive, err := unix.SysctlUint32("vm.stats.vm.v_inactive_count")
  30. if err != nil {
  31. return nil, err
  32. }
  33. cached, err := unix.SysctlUint32("vm.stats.vm.v_cache_count")
  34. if err != nil {
  35. return nil, err
  36. }
  37. buffers, err := unix.SysctlUint64("vfs.bufspace")
  38. if err != nil {
  39. return nil, err
  40. }
  41. wired, err := unix.SysctlUint32("vm.stats.vm.v_wire_count")
  42. if err != nil {
  43. return nil, err
  44. }
  45. p := uint64(pageSize)
  46. ret := &VirtualMemoryStat{
  47. Total: uint64(pageCount) * p,
  48. Free: uint64(free) * p,
  49. Active: uint64(active) * p,
  50. Inactive: uint64(inactive) * p,
  51. Cached: uint64(cached) * p,
  52. Buffers: uint64(buffers),
  53. Wired: uint64(wired) * p,
  54. }
  55. ret.Available = ret.Inactive + ret.Cached + ret.Free
  56. ret.Used = ret.Total - ret.Available
  57. ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0
  58. return ret, nil
  59. }
  60. // Return swapinfo
  61. func SwapMemory() (*SwapMemoryStat, error) {
  62. return SwapMemoryWithContext(context.Background())
  63. }
  64. // Constants from vm/vm_param.h
  65. // nolint: golint
  66. const (
  67. XSWDEV_VERSION = 1
  68. )
  69. // Types from vm/vm_param.h
  70. type xswdev struct {
  71. Version uint32 // Version is the version
  72. Dev uint32 // Dev is the device identifier
  73. Flags int32 // Flags is the swap flags applied to the device
  74. NBlks int32 // NBlks is the total number of blocks
  75. Used int32 // Used is the number of blocks used
  76. }
  77. func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
  78. // FreeBSD can have multiple swap devices so we total them up
  79. i, err := unix.SysctlUint32("vm.nswapdev")
  80. if err != nil {
  81. return nil, err
  82. }
  83. if i == 0 {
  84. return nil, errors.New("no swap devices found")
  85. }
  86. c := int(i)
  87. i, err = unix.SysctlUint32("vm.stats.vm.v_page_size")
  88. if err != nil {
  89. return nil, err
  90. }
  91. pageSize := uint64(i)
  92. var buf []byte
  93. s := &SwapMemoryStat{}
  94. for n := 0; n < c; n++ {
  95. buf, err = unix.SysctlRaw("vm.swap_info", n)
  96. if err != nil {
  97. return nil, err
  98. }
  99. xsw := (*xswdev)(unsafe.Pointer(&buf[0]))
  100. if xsw.Version != XSWDEV_VERSION {
  101. return nil, errors.New("xswdev version mismatch")
  102. }
  103. s.Total += uint64(xsw.NBlks)
  104. s.Used += uint64(xsw.Used)
  105. }
  106. if s.Total != 0 {
  107. s.UsedPercent = float64(s.Used) / float64(s.Total) * 100
  108. }
  109. s.Total *= pageSize
  110. s.Used *= pageSize
  111. s.Free = s.Total - s.Used
  112. return s, nil
  113. }