使用Hcache查看是谁占用了系统Buffer&Cache?

  1. 一、hcache

  2. Linux用户可能经常遇到的一个问题是内存大部分都被Buffer和Cache占用了,但是有时候我们想知道到底Cache了些什么内容却没有一个直观好用的工具。今天给你介绍一个可以查看Linux当前缓存了哪些文件的小工具hcache。

  3. Hcache作者:伊布

  4. 前几天看到有个人用go写了个pcstat,可以查看某个文件是否被缓存(作者的目的是数据库调优),也可以根据进程pid来查看都缓存了哪些文件,但是不能查看整个操作系统都cache了哪些文件。因此,我基于pcstat写了个hcache,增加了查看当前操作系统cache的前top个文件的特性,你可以从这里下载下来试用,如果有什么建议的话也欢迎反馈给我:hubottle@gmail.com

  5. 官网:https://github.com/silenceshell/hcache

  6. 二、安装

  7. hcache是使用GO开发的,安装非常简单,开箱即用。


  8. $ wget http://7xir15.com1.z0.glb.clouddn.com/hcache
  9. $ chmod +x hcache
  10. $ mv hcache /usr/local/bin/
  11. 1
  12. 2
  13. 3
  14. $ wget http://7xir15.com1.z0.glb.clouddn.com/hcache
  15. $ chmod +x hcache
  16. $ mv hcache /usr/local/bin/
  17. 三、使用


  18. $ free -m
  19.               total used free shared buff/cache available
  20. Mem: 7823 1952 174 9 5696 5518
  21. Swap: 0 0 0
  22. 1
  23. 2
  24. 3
  25. 4
  26. $ free -m
  27.               total used free shared buff/cache available
  28. Mem: 7823 1952 174 9 5696 5518
  29. Swap: 0 0 0
  30. 查看使用Cache最多的3个进程。


  31. $ hcache --top 3
  32. +----------------------------+----------------+------------+-----------+---------+
  33. | Name | Size (bytes) | Pages | Cached | Percent |
  34. |----------------------------+----------------+------------+-----------+---------|
  35. | /usr/bin/dockerd | 33368760 | 8147 | 8147 | 100.000 |
  36. | /usr/bin/docker-containerd | 7115584 | 1738 | 1738 | 100.000 |
  37. | /etc/udev/hwdb.bin | 6711310 | 1639 | 1639 | 100.000 |
  38. +----------------------------+----------------+------------+-----------+---------+
  39. 1
  40. 2
  41. 3
  42. 4
  43. 5
  44. 6
  45. 7
  46. 8
  47. $ hcache --top 3
  48. +----------------------------+----------------+------------+-----------+---------+
  49. | Name | Size (bytes) | Pages | Cached | Percent |
  50. |----------------------------+----------------+------------+-----------+---------|
  51. | /usr/bin/dockerd | 33368760 | 8147 | 8147 | 100.000 |
  52. | /usr/bin/docker-containerd | 7115584 | 1738 | 1738 | 100.000 |
  53. | /etc/udev/hwdb.bin | 6711310 | 1639 | 1639 | 100.000 |
  54. +----------------------------+----------------+------------+-----------+---------+
  55. 默认情况下会显示cache文件的全路径,会比较长。可以使用--bname选项来仅显示文件名。


  56. $ hcache --top 3 --bname
  57. +-------------------+----------------+------------+-----------+---------+
  58. | Name | Size (bytes) | Pages | Cached | Percent |
  59. |-------------------+----------------+------------+-----------+---------|
  60. | dockerd | 33368760 | 8147 | 8147 | 100.000 |
  61. | docker-containerd | 7115584 | 1738 | 1738 | 100.000 |
  62. | hwdb.bin | 6711310 | 1639 | 1639 | 100.000 |
  63. +-------------------+----------------+------------+-----------+---------+
  64. 1
  65. 2
  66. 3
  67. 4
  68. 5
  69. 6
  70. 7
  71. 8
  72. $ hcache --top 3 --bname
  73. +-------------------+----------------+------------+-----------+---------+
  74. | Name | Size (bytes) | Pages | Cached | Percent |
  75. |-------------------+----------------+------------+-----------+---------|
  76. | dockerd | 33368760 | 8147 | 8147 | 100.000 |
  77. | docker-containerd | 7115584 | 1738 | 1738 | 100.000 |
  78. | hwdb.bin | 6711310 | 1639 | 1639 | 100.000 |
  79. +-------------------+----------------+------------+-----------+---------+
  80. 查看指定进程的Cache使用情况。


  81. $ hcache -pid 2903 -bname
  82. 1
  83. $ hcache -pid 2903 -bname
  84. 另外还可使用指定格式输出,比如:JSON、纯文本。更多使用方法可参考hcache -h。


  85. $ hcache -h
  86. Usage of hcache:
  87.   -bname
  88.         convert paths to basename to narrow the output
  89.   -histo
  90.         print a simple histogram instead of raw data
  91.   -json
  92.         return data in JSON format
  93.   -nohdr
  94.         omit the header from terse & text output
  95.   -pid int
  96.         show all open maps for the given pid
  97.   -plain
  98.         return data with no box characters
  99.   -pps
  100.         include the per-page status in JSON output
  101.   -terse
  102.         show terse output
  103.   -top int
  104.         convert paths to basename to narrow the output
  105.   -unicode
  106.         return data with unicode box characters
  107. 1
  108. 2
  109. 3
  110. 4
  111. 5
  112. 6
  113. 7
  114. 8
  115. 9
  116. 10
  117. 11
  118. 12
  119. 13
  120. 14
  121. 15
  122. 16
  123. 17
  124. 18
  125. 19
  126. 20
  127. 21
  128. 22
  129. $ hcache -h
  130. Usage of hcache:
  131.   -bname
  132.         convert paths to basename to narrow the output
  133.   -histo
  134.         print a simple histogram instead of raw data
  135.   -json
  136.         return data in JSON format
  137.   -nohdr
  138.         omit the header from terse & text output
  139.   -pid int
  140.         show all open maps for the given pid
  141.   -plain
  142.         return data with no box characters
  143.   -pps
  144.         include the per-page status in JSON output
  145.   -terse
  146.         show terse output
  147.   -top int
  148.         convert paths to basename to narrow the output
  149.   -unicode
  150.         return data with unicode box characters
  151. 完结。。


当前题目:使用Hcache查看是谁占用了系统Buffer&Cache?
分享地址:http://pwwzsj.com/article/ghdopi.html