vmstat.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. )
  7. type VMStat struct {
  8. NrFreePages uint64 `json:"nr_free_pages"`
  9. NrAllocBatch uint64 `json:"nr_alloc_batch"`
  10. NrInactiveAnon uint64 `json:"nr_inactive_anon"`
  11. NrActiveAnon uint64 `json:"nr_active_anon"`
  12. NrInactiveFile uint64 `json:"nr_inactive_file"`
  13. NrActiveFile uint64 `json:"nr_active_file"`
  14. NrUnevictable uint64 `json:"nr_unevictable"`
  15. NrMlock uint64 `json:"nr_mlock"`
  16. NrAnonPages uint64 `json:"nr_anon_pages"`
  17. NrMapped uint64 `json:"nr_mapped"`
  18. NrFilePages uint64 `json:"nr_file_pages"`
  19. NrDirty uint64 `json:"nr_dirty"`
  20. NrWriteback uint64 `json:"nr_writeback"`
  21. NrSlabReclaimable uint64 `json:"nr_slab_reclaimable"`
  22. NrSlabUnreclaimable uint64 `json:"nr_slab_unreclaimable"`
  23. NrPageTablePages uint64 `json:"nr_page_table_pages"`
  24. NrKernelStack uint64 `json:"nr_kernel_stack"`
  25. NrUnstable uint64 `json:"nr_unstable"`
  26. NrBounce uint64 `json:"nr_bounce"`
  27. NrVmscanWrite uint64 `json:"nr_vmscan_write"`
  28. NrVmscanImmediateReclaim uint64 `json:"nr_vmscan_immediate_reclaim"`
  29. NrWritebackTemp uint64 `json:"nr_writeback_temp"`
  30. NrIsolatedAnon uint64 `json:"nr_isolated_anon"`
  31. NrIsolatedFile uint64 `json:"nr_isolated_file"`
  32. NrShmem uint64 `json:"nr_shmem"`
  33. NrDirtied uint64 `json:"nr_dirtied"`
  34. NrWritten uint64 `json:"nr_written"`
  35. NumaHit uint64 `json:"numa_hit"`
  36. NumaMiss uint64 `json:"numa_miss"`
  37. NumaForeign uint64 `json:"numa_foreign"`
  38. NumaInterleave uint64 `json:"numa_interleave"`
  39. NumaLocal uint64 `json:"numa_local"`
  40. NumaOther uint64 `json:"numa_other"`
  41. WorkingsetRefault uint64 `json:"workingset_refault"`
  42. WorkingsetActivate uint64 `json:"workingset_activate"`
  43. WorkingsetNodereclaim uint64 `json:"workingset_nodereclaim"`
  44. NrAnonTransparentHugepages uint64 `json:"nr_anon_transparent_hugepages"`
  45. NrFreeCma uint64 `json:"nr_free_cma"`
  46. NrDirtyThreshold uint64 `json:"nr_dirty_threshold"`
  47. NrDirtyBackgroundThreshold uint64 `json:"nr_dirty_background_threshold"`
  48. PagePagein uint64 `json:"pgpgin"`
  49. PagePageout uint64 `json:"pgpgout"`
  50. PageSwapin uint64 `json:"pswpin"`
  51. PageSwapout uint64 `json:"pswpout"`
  52. PageAllocDMA uint64 `json:"pgalloc_dma"`
  53. PageAllocDMA32 uint64 `json:"pgalloc_dma32"`
  54. PageAllocNormal uint64 `json:"pgalloc_normal"`
  55. PageAllocMovable uint64 `json:"pgalloc_movable"`
  56. PageFree uint64 `json:"pgfree"`
  57. PageActivate uint64 `json:"pgactivate"`
  58. PageDeactivate uint64 `json:"pgdeactivate"`
  59. PageFault uint64 `json:"pgfault"`
  60. PageMajorFault uint64 `json:"pgmajfault"`
  61. PageRefillDMA uint64 `json:"pgrefill_dma"`
  62. PageRefillDMA32 uint64 `json:"pgrefill_dma32"`
  63. PageRefillMormal uint64 `json:"pgrefill_normal"`
  64. PageRefillMovable uint64 `json:"pgrefill_movable"`
  65. PageStealKswapdDMA uint64 `json:"pgsteal_kswapd_dma"`
  66. PageStealKswapdDMA32 uint64 `json:"pgsteal_kswapd_dma32"`
  67. PageStealKswapdNormal uint64 `json:"pgsteal_kswapd_normal"`
  68. PageStealKswapdMovable uint64 `json:"pgsteal_kswapd_movable"`
  69. PageStealDirectDMA uint64 `json:"pgsteal_direct_dma"`
  70. PageStealDirectDMA32 uint64 `json:"pgsteal_direct_dma32"`
  71. PageStealDirectNormal uint64 `json:"pgsteal_direct_normal"`
  72. PageStealDirectMovable uint64 `json:"pgsteal_direct_movable"`
  73. PageScanKswapdDMA uint64 `json:"pgscan_kswapd_dma"`
  74. PageScanKswapdDMA32 uint64 `json:"pgscan_kswapd_dma32"`
  75. PageScanKswapdNormal uint64 `json:"pgscan_kswapd_normal"`
  76. PageScanKswapdMovable uint64 `json:"pgscan_kswapd_movable"`
  77. PageScanDirectDMA uint64 `json:"pgscan_direct_dma"`
  78. PageScanDirectDMA32 uint64 `json:"pgscan_direct_dma32"`
  79. PageScanDirectNormal uint64 `json:"pgscan_direct_normal"`
  80. PageScanDirectMovable uint64 `json:"pgscan_direct_movable"`
  81. PageScanDirectThrottle uint64 `json:"pgscan_direct_throttle"`
  82. ZoneReclaimFailed uint64 `json:"zone_reclaim_failed"`
  83. PageInodeSteal uint64 `json:"pginodesteal"`
  84. SlabsScanned uint64 `json:"slabs_scanned"`
  85. KswapdInodesteal uint64 `json:"kswapd_inodesteal"`
  86. KswapdLowWatermarkHitQuickly uint64 `json:"kswapd_low_wmark_hit_quickly"`
  87. KswapdHighWatermarkHitQuickly uint64 `json:"kswapd_high_wmark_hit_quickly"`
  88. PageoutRun uint64 `json:"pageoutrun"`
  89. AllocStall uint64 `json:"allocstall"`
  90. PageRotated uint64 `json:"pgrotated"`
  91. DropPagecache uint64 `json:"drop_pagecache"`
  92. DropSlab uint64 `json:"drop_slab"`
  93. NumaPteUpdates uint64 `json:"numa_pte_updates"`
  94. NumaHugePteUpdates uint64 `json:"numa_huge_pte_updates"`
  95. NumaHintFaults uint64 `json:"numa_hint_faults"`
  96. NumaHintFaults_local uint64 `json:"numa_hint_faults_local"`
  97. NumaPagesMigrated uint64 `json:"numa_pages_migrated"`
  98. PageMigrateSuccess uint64 `json:"pgmigrate_success"`
  99. PageMigrateFail uint64 `json:"pgmigrate_fail"`
  100. CompactMigrateScanned uint64 `json:"compact_migrate_scanned"`
  101. CompactFreeScanned uint64 `json:"compact_free_scanned"`
  102. CompactIsolated uint64 `json:"compact_isolated"`
  103. CompactStall uint64 `json:"compact_stall"`
  104. CompactFail uint64 `json:"compact_fail"`
  105. CompactSuccess uint64 `json:"compact_success"`
  106. HtlbBuddyAllocSuccess uint64 `json:"htlb_buddy_alloc_success"`
  107. HtlbBuddyAllocFail uint64 `json:"htlb_buddy_alloc_fail"`
  108. UnevictablePagesCulled uint64 `json:"unevictable_pgs_culled"`
  109. UnevictablePagesScanned uint64 `json:"unevictable_pgs_scanned"`
  110. UnevictablePagesRescued uint64 `json:"unevictable_pgs_rescued"`
  111. UnevictablePagesMlocked uint64 `json:"unevictable_pgs_mlocked"`
  112. UnevictablePagesMunlocked uint64 `json:"unevictable_pgs_munlocked"`
  113. UnevictablePagesCleared uint64 `json:"unevictable_pgs_cleared"`
  114. UnevictablePagesStranded uint64 `json:"unevictable_pgs_stranded"`
  115. THPFaultAlloc uint64 `json:"thp_fault_alloc"`
  116. THPFaultFallback uint64 `json:"thp_fault_fallback"`
  117. THPCollapseAlloc uint64 `json:"thp_collapse_alloc"`
  118. THPCollapseAllocFailed uint64 `json:"thp_collapse_alloc_failed"`
  119. THPSplit uint64 `json:"thp_split"`
  120. THPZeroPageAlloc uint64 `json:"thp_zero_page_alloc"`
  121. THPZeroPageAllocFailed uint64 `json:"thp_zero_page_alloc_failed"`
  122. }
  123. func ReadVMStat(path string) (*VMStat, error) {
  124. b, err := ioutil.ReadFile(path)
  125. if err != nil {
  126. return nil, err
  127. }
  128. content := string(b)
  129. lines := strings.Split(content, "\n")
  130. vmstat := VMStat{}
  131. for _, line := range lines {
  132. fields := strings.Fields(line)
  133. if len(fields) != 2 {
  134. continue
  135. }
  136. name := fields[0]
  137. value, _ := strconv.ParseUint(fields[1], 10, 64)
  138. switch name {
  139. case "nr_free_pages":
  140. vmstat.NrFreePages = value
  141. case "nr_alloc_batch":
  142. vmstat.NrAllocBatch = value
  143. case "nr_inactive_anon":
  144. vmstat.NrInactiveAnon = value
  145. case "nr_active_anon":
  146. vmstat.NrActiveAnon = value
  147. case "nr_inactive_file":
  148. vmstat.NrInactiveFile = value
  149. case "nr_active_file":
  150. vmstat.NrActiveFile = value
  151. case "nr_unevictable":
  152. vmstat.NrUnevictable = value
  153. case "nr_mlock":
  154. vmstat.NrMlock = value
  155. case "nr_anon_pages":
  156. vmstat.NrAnonPages = value
  157. case "nr_mapped":
  158. vmstat.NrMapped = value
  159. case "nr_file_pages":
  160. vmstat.NrFilePages = value
  161. case "nr_dirty":
  162. vmstat.NrDirty = value
  163. case "nr_writeback":
  164. vmstat.NrWriteback = value
  165. case "nr_slab_reclaimable":
  166. vmstat.NrSlabReclaimable = value
  167. case "nr_slab_unreclaimable":
  168. vmstat.NrSlabUnreclaimable = value
  169. case "nr_page_table_pages":
  170. vmstat.NrPageTablePages = value
  171. case "nr_kernel_stack":
  172. vmstat.NrKernelStack = value
  173. case "nr_unstable":
  174. vmstat.NrUnstable = value
  175. case "nr_bounce":
  176. vmstat.NrBounce = value
  177. case "nr_vmscan_write":
  178. vmstat.NrVmscanWrite = value
  179. case "nr_vmscan_immediate_reclaim":
  180. vmstat.NrVmscanImmediateReclaim = value
  181. case "nr_writeback_temp":
  182. vmstat.NrWritebackTemp = value
  183. case "nr_isolated_anon":
  184. vmstat.NrIsolatedAnon = value
  185. case "nr_isolated_file":
  186. vmstat.NrIsolatedFile = value
  187. case "nr_shmem":
  188. vmstat.NrShmem = value
  189. case "nr_dirtied":
  190. vmstat.NrDirtied = value
  191. case "nr_written":
  192. vmstat.NrWritten = value
  193. case "numa_hit":
  194. vmstat.NumaHit = value
  195. case "numa_miss":
  196. vmstat.NumaMiss = value
  197. case "numa_foreign":
  198. vmstat.NumaForeign = value
  199. case "numa_interleave":
  200. vmstat.NumaInterleave = value
  201. case "numa_local":
  202. vmstat.NumaLocal = value
  203. case "numa_other":
  204. vmstat.NumaOther = value
  205. case "workingset_refault":
  206. vmstat.WorkingsetRefault = value
  207. case "workingset_activate":
  208. vmstat.WorkingsetActivate = value
  209. case "workingset_nodereclaim":
  210. vmstat.WorkingsetNodereclaim = value
  211. case "nr_anon_transparent_hugepages":
  212. vmstat.NrAnonTransparentHugepages = value
  213. case "nr_free_cma":
  214. vmstat.NrFreeCma = value
  215. case "nr_dirty_threshold":
  216. vmstat.NrDirtyThreshold = value
  217. case "nr_dirty_background_threshold":
  218. vmstat.NrDirtyBackgroundThreshold = value
  219. case "pgpgin":
  220. vmstat.PagePagein = value
  221. case "pgpgout":
  222. vmstat.PagePageout = value
  223. case "pswpin":
  224. vmstat.PageSwapin = value
  225. case "pswpout":
  226. vmstat.PageSwapout = value
  227. case "pgalloc_dma":
  228. vmstat.PageAllocDMA = value
  229. case "pgalloc_dma32":
  230. vmstat.PageAllocDMA32 = value
  231. case "pgalloc_normal":
  232. vmstat.PageAllocNormal = value
  233. case "pgalloc_movable":
  234. vmstat.PageAllocMovable = value
  235. case "pgfree":
  236. vmstat.PageFree = value
  237. case "pgactivate":
  238. vmstat.PageActivate = value
  239. case "pgdeactivate":
  240. vmstat.PageDeactivate = value
  241. case "pgfault":
  242. vmstat.PageFault = value
  243. case "pgmajfault":
  244. vmstat.PageMajorFault = value
  245. case "pgrefill_dma":
  246. vmstat.PageRefillDMA = value
  247. case "pgrefill_dma32":
  248. vmstat.PageRefillDMA32 = value
  249. case "pgrefill_normal":
  250. vmstat.PageRefillMormal = value
  251. case "pgrefill_movable":
  252. vmstat.PageRefillMovable = value
  253. case "pgsteal_kswapd_dma":
  254. vmstat.PageStealKswapdDMA = value
  255. case "pgsteal_kswapd_dma32":
  256. vmstat.PageStealKswapdDMA32 = value
  257. case "pgsteal_kswapd_normal":
  258. vmstat.PageStealKswapdNormal = value
  259. case "pgsteal_kswapd_movable":
  260. vmstat.PageStealKswapdMovable = value
  261. case "pgsteal_direct_dma":
  262. vmstat.PageStealDirectDMA = value
  263. case "pgsteal_direct_dma32":
  264. vmstat.PageStealDirectDMA32 = value
  265. case "pgsteal_direct_normal":
  266. vmstat.PageStealDirectNormal = value
  267. case "pgsteal_direct_movable":
  268. vmstat.PageStealDirectMovable = value
  269. case "pgscan_kswapd_dma":
  270. vmstat.PageScanKswapdDMA = value
  271. case "pgscan_kswapd_dma32":
  272. vmstat.PageScanKswapdDMA32 = value
  273. case "pgscan_kswapd_normal":
  274. vmstat.PageScanKswapdNormal = value
  275. case "pgscan_kswapd_movable":
  276. vmstat.PageScanKswapdMovable = value
  277. case "pgscan_direct_dma":
  278. vmstat.PageScanDirectDMA = value
  279. case "pgscan_direct_dma32":
  280. vmstat.PageScanDirectDMA32 = value
  281. case "pgscan_direct_normal":
  282. vmstat.PageScanDirectNormal = value
  283. case "pgscan_direct_movable":
  284. vmstat.PageScanDirectMovable = value
  285. case "pgscan_direct_throttle":
  286. vmstat.PageScanDirectThrottle = value
  287. case "zone_reclaim_failed":
  288. vmstat.ZoneReclaimFailed = value
  289. case "pginodesteal":
  290. vmstat.PageInodeSteal = value
  291. case "slabs_scanned":
  292. vmstat.SlabsScanned = value
  293. case "kswapd_inodesteal":
  294. vmstat.KswapdInodesteal = value
  295. case "kswapd_low_wmark_hit_quickly":
  296. vmstat.KswapdLowWatermarkHitQuickly = value
  297. case "kswapd_high_wmark_hit_quickly":
  298. vmstat.KswapdHighWatermarkHitQuickly = value
  299. case "pageoutrun":
  300. vmstat.PageoutRun = value
  301. case "allocstall":
  302. vmstat.AllocStall = value
  303. case "pgrotated":
  304. vmstat.PageRotated = value
  305. case "drop_pagecache":
  306. vmstat.DropPagecache = value
  307. case "drop_slab":
  308. vmstat.DropSlab = value
  309. case "numa_pte_updates":
  310. vmstat.NumaPteUpdates = value
  311. case "numa_huge_pte_updates":
  312. vmstat.NumaHugePteUpdates = value
  313. case "numa_hint_faults":
  314. vmstat.NumaHintFaults = value
  315. case "numa_hint_faults_local":
  316. vmstat.NumaHintFaults_local = value
  317. case "numa_pages_migrated":
  318. vmstat.NumaPagesMigrated = value
  319. case "pgmigrate_success":
  320. vmstat.PageMigrateSuccess = value
  321. case "pgmigrate_fail":
  322. vmstat.PageMigrateFail = value
  323. case "compact_migrate_scanned":
  324. vmstat.CompactMigrateScanned = value
  325. case "compact_free_scanned":
  326. vmstat.CompactFreeScanned = value
  327. case "compact_isolated":
  328. vmstat.CompactIsolated = value
  329. case "compact_stall":
  330. vmstat.CompactStall = value
  331. case "compact_fail":
  332. vmstat.CompactFail = value
  333. case "compact_success":
  334. vmstat.CompactSuccess = value
  335. case "htlb_buddy_alloc_success":
  336. vmstat.HtlbBuddyAllocSuccess = value
  337. case "htlb_buddy_alloc_fail":
  338. vmstat.HtlbBuddyAllocFail = value
  339. case "unevictable_pgs_culled":
  340. vmstat.UnevictablePagesCulled = value
  341. case "unevictable_pgs_scanned":
  342. vmstat.UnevictablePagesScanned = value
  343. case "unevictable_pgs_rescued":
  344. vmstat.UnevictablePagesRescued = value
  345. case "unevictable_pgs_mlocked":
  346. vmstat.UnevictablePagesMlocked = value
  347. case "unevictable_pgs_munlocked":
  348. vmstat.UnevictablePagesMunlocked = value
  349. case "unevictable_pgs_cleared":
  350. vmstat.UnevictablePagesCleared = value
  351. case "unevictable_pgs_stranded":
  352. vmstat.UnevictablePagesStranded = value
  353. case "thp_fault_alloc":
  354. vmstat.THPFaultAlloc = value
  355. case "thp_fault_fallback":
  356. vmstat.THPFaultFallback = value
  357. case "thp_collapse_alloc":
  358. vmstat.THPCollapseAlloc = value
  359. case "thp_collapse_alloc_failed":
  360. vmstat.THPCollapseAllocFailed = value
  361. case "thp_split":
  362. vmstat.THPSplit = value
  363. case "thp_zero_page_alloc":
  364. vmstat.THPZeroPageAlloc = value
  365. case "thp_zero_page_alloc_failed":
  366. vmstat.THPZeroPageAllocFailed = value
  367. }
  368. }
  369. return &vmstat, nil
  370. }