问题

  • 执行sync的时候,操作系统在做了什么?
    • dd命令做系统后,立即弹出U盘,系统会提示U盘正在使用中,继续持续一段时间后才可以正常弹出,这段时间系统做了什么?
  • sudo dd if=/dev/sda1 of=/dev/null bs=1M count=1000连续读取两次为什么差距巨大

    proc文件系统

    /proc是LInux内核提供的一种特殊文件系统,可以通过proc和内核进行交互。比如我们可以从/proc中查询内核的运行状态和配置选项,查询进程的运行状态、统计数据等等。
    man proc ```cpp Buffers %lu Relatively temporary storage for raw disk blocks that shouldn’t get tremendously large (20MB or so).

Cached %lu In-memory cache for files read from the disk (the page cache). Doesn’t include SwapCached.

  1. - Buffer是相对原始磁盘块的临时存储,用来**缓存磁盘的数据。**这样内核就可以把写操作集中起来。假如我们当我们从网络上下载文件时,就可以利用buffer将分散的写操作集中起来合并成大的单次的写。
  2. > wget [https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.12/5.12.8/single/qt-everywhere-src-5.12.8.zip](https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/5.12/5.12.8/single/qt-everywhere-src-5.12.8.zip)
  3. > 观察vmstat输出
  4. - Cached是从磁盘读取文件的页缓存,也就是用来**缓存从文件读取的数据**。下次访问磁盘的时候,就可以直接从内存中快速访问到,不需要再次访问缓慢的磁盘
  5. <a name="VlCfS"></a>
  6. # 问题
  7. - Buffer是磁盘读缓存还是写缓存?
  8. - Cached是磁盘文件的读缓存,那么会不会也缓存写文件的数据?
  9. <a name="aY59o"></a>
  10. ## 磁盘和文件写案例
  11. ```bash
  12. # 清理文件页、目录项、Inodes等
  13. echo 3 > /proc/sys/vm/drop_caches

运行以下命令,观察buff(KB)、cached、bi(block input)、bo
dd if=/dev/urandom of=/tmp/file bs=1M count=1000

  1. # 清理下buffer/cached
  2. echo 3 > /proc/sys/vm/drop_chches
  3. # 执行命令
  4. kylin@kylin-thinkpad-t14-gen-1 ~> sudo dd if=/dev/urandom of=/tmp/file bs=1M count=1000 status=progress;time sync
  5. 1017118720 bytes (1.0 GB, 970 MiB) copied, 27 s, 37.6 MB/s
  6. 1000+0 records in
  7. 1000+0 records out
  8. 1048576000 bytes (1.0 GB, 1000 MiB) copied, 27.8993 s, 37.6 MB/s
  9. ________________________________________________________
  10. Executed in 5.19 millis fish external
  11. usr time 992.00 micros 161.00 micros 831.00 micros
  12. sys time 70.00 micros 70.00 micros 0.00 micros
  1. # vmstat变化情况
  2. kylin@kylin-thinkpad-t14-gen-1 ~> vmstat 1
  3. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  4. r b swpd free buff cache si so bi bo in cs us sy id wa st
  5. 1 0 26880 3777832 4280 2150192 0 0 0 168 1651 2593 0 1 99 0 0
  6. 2 0 26880 3777840 4280 2150196 0 0 0 0 1648 2630 0 1 99 0 0
  7. 0 0 26880 3777832 4396 2150148 0 0 116 0 1789 3012 1 1 98 0 0
  8. 0 0 26880 3777840 4396 2150148 0 0 0 0 1626 2557 0 1 99 0 0
  9. 0 0 26880 3777572 4396 2150148 0 0 0 0 1666 2617 1 1 98 0 0
  10. 1 0 26880 3756112 5504 2168320 0 0 2464 244 2596 3441 1 9 90 0 0
  11. 1 0 26880 3717296 5504 2207164 0 0 0 0 2404 2846 0 14 85 0 0
  12. 1 0 26880 3679400 5504 2245068 0 0 0 0 2731 2871 0 13 87 0 0
  13. 1 0 22784 3641520 5504 2287352 0 0 0 0 2768 3156 1 18 81 0 0
  14. 1 0 22784 3603632 5504 2325240 0 0 0 0 2646 2862 0 17 83 0 0
  15. 1 0 22784 3565752 5536 2363140 0 0 20 187936 2835 2807 0 15 84 1 0
  16. 1 0 22784 3527856 5536 2401012 0 0 0 0 2576 2984 0 16 84 0 0
  17. 1 0 22784 3488968 5536 2439952 0 0 0 0 2215 2543 0 15 85 0 0
  18. 1 0 22784 3451096 5536 2477844 0 0 0 0 2321 2720 0 17 83 0 0
  19. 1 0 22784 3413704 5544 2515728 0 0 0 100 2721 3116 0 18 81 0 0
  20. 1 0 22784 3376084 5548 2553604 0 0 4 188420 2579 2785 0 17 82 1 0
  21. 1 0 22784 3338352 5548 2591660 0 0 0 0 2255 2597 0 15 85 0 0
  22. 1 0 22784 3301112 5548 2628876 0 0 0 0 2602 3063 0 17 83 0 0
  23. 1 0 22784 3261972 5548 2667828 0 0 0 0 2438 2641 0 16 84 0 0
  24. 1 0 22784 3223808 5556 2705260 0 0 0 68 2274 2719 0 15 85 0 0
  25. 1 0 22784 3187164 5564 2742104 0 0 8 188528 2714 3156 1 17 82 1 0
  26. 1 0 22784 3149064 5564 2779976 0 0 0 0 2514 2789 0 18 82 0 0
  27. 1 0 22784 3111184 5564 2817884 0 0 0 0 2368 2766 0 15 84 0 0
  28. 1 0 22784 3073548 5564 2855756 0 0 0 0 2531 2999 0 16 84 0 0
  29. 1 0 22784 3035668 5572 2893848 0 0 0 36 2308 2659 0 14 86 0 0
  30. 1 0 22784 2998528 5576 2930528 0 0 4 188420 2508 2750 0 15 84 1 0
  31. 1 0 22784 2960648 5576 2968396 0 0 0 0 2576 2996 0 15 85 0 0
  32. 1 0 22784 2923028 5576 3006288 0 0 0 0 2429 2790 0 15 85 0 0
  33. 1 0 22784 2885156 5576 3044192 0 0 0 0 2467 2664 0 15 84 0 0
  34. 1 0 22784 2847252 5584 3082384 0 0 0 36 2598 3096 0 16 83 0 0
  35. 1 1 22784 2809364 5584 3120200 0 0 0 33820 2557 2881 0 14 86 0 0
  36. 1 0 22784 2772752 5592 3156872 0 0 8 153604 2759 2900 0 14 85 0 0
  37. 1 0 22784 2734620 5592 3194716 0 0 0 0 2504 2974 0 16 83 0 0
  38. 0 0 22784 2721460 5956 3208672 0 0 400 84184 2228 3198 0 6 94 0 0
  39. 0 0 22784 2721712 5956 3208672 0 0 0 0 1600 2473 0 0 99 0 0
  40. 0 0 22784 2721728 5956 3208672 0 0 0 0 1713 2784 1 1 99 0 0

计算:
(187936 + 100 + 188420 + 188528 + 188420 + 33820 + 153604 + 84184)/1024 = 1000MB
观察到的现象:

  • Cahed在不停增长,但是Buff基本不变
  • 观察块设备的I/O,bi很少,bo过一段时间会出现一个大量的设备写
  • dd命令结束后,cache不再增长,bo会再持续一小会儿。

前边说Cache是文件读的页缓存,为什么写文件时也会增长?

  1. root@kylin-thinkpad-t14-gen-1 ~>echo 3 > /proc/sys/vm/drop_chches
  2. kylin@kylin-thinkpad-t14-gen-1 ~>sudo dd if=/dev/urandom of=/dev/sda1 bs=1M count=1000;time sync
  3. 1012924416 bytes (1.0 GB, 966 MiB) copied, 27 s, 37.5 MB/s
  4. 1000+0 records in
  5. 1000+0 records out
  6. 1048576000 bytes (1.0 GB, 1000 MiB) copied, 27.9153 s, 37.6 MB/s
  7. Executed in 71.87 secs fish external
  8. usr time 0.01 secs 518.00 micros 0.01 secs
  9. sys time 2.40 secs 238.00 micros 2.40 secs
  1. kylin@kylin-thinkpad-t14-gen-1 ~>vmstat 1
  2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  3. r b swpd free buff cache si so bi bo in cs us sy id wa st
  4. 0 0 35328 3820400 2712 1976404 0 0 0 0 1624 2752 0 0 99 0 0
  5. 0 0 35328 3820400 2712 1976388 0 0 0 0 1572 2540 0 0 99 0 0
  6. 0 0 35328 3820400 3136 1976304 0 0 416 40 1624 2673 1 1 99 0 0
  7. 0 0 35328 3820148 3136 1976404 0 0 0 0 1692 2744 1 0 99 0 0
  8. 0 0 35328 3820148 3136 1976388 0 0 0 0 1613 2572 1 1 99 0 0
  9. 0 0 35328 3820148 3136 1976404 0 0 0 0 1579 2523 1 0 99 0 0
  10. 0 0 35328 3817940 4572 1978384 0 0 3340 64 1890 3151 1 1 98 0 0
  11. 0 0 35328 3817964 4572 1978308 0 0 0 172 1725 2713 1 0 99 0 0
  12. 0 0 35328 3817712 4572 1978324 0 0 0 0 1627 2581 0 1 99 0 0
  13. 1 0 35328 3793456 22680 1982396 0 0 1440 28 2348 3302 1 7 92 0 0
  14. 1 0 35328 3749356 59544 1989908 0 0 0 0 2601 2869 1 16 83 0 0
  15. 1 0 35328 3704956 96408 1997528 0 0 0 0 2412 2849 1 16 83 0 0
  16. 1 0 35328 3660280 133272 2005152 0 0 0 0 2631 2943 0 19 80 0 0
  17. 1 0 35328 3615572 170136 2012812 0 0 0 0 2421 2825 0 18 82 0 0
  18. 1 1 35328 3569996 208024 2020676 0 0 0 12569 2637 2938 0 19 77 4 0
  19. 1 2 35328 3525224 244896 2028272 0 0 0 6172 2617 3047 0 18 66 16 0
  20. 2 1 35328 3480928 281760 2035932 0 0 0 18432 2453 2913 0 14 70 15 0
  21. 2 1 35328 3436512 318624 2043592 0 0 0 0 2483 2701 0 18 70 12 0
  22. 1 1 35328 3393296 354464 2051008 0 0 0 18432 2536 3238 0 19 74 7 0
  23. 1 1 35328 3348208 391328 2059172 0 0 0 2048 2605 2799 0 16 72 12 0
  24. 1 1 35328 3303576 428200 2066896 0 0 0 16420 2703 2935 0 18 71 11 0
  25. 1 1 35328 3259040 465072 2074388 0 0 8 9216 2542 3073 0 17 72 11 0
  26. 1 2 35328 3214412 501936 2081620 0 0 0 9216 2420 2764 0 15 72 12 0
  27. 1 1 35328 3170860 537776 2090064 0 0 0 15360 2434 2882 0 16 72 12 0
  28. 1 1 35328 3126108 574640 2097704 0 0 0 3072 2377 2927 0 18 70 12 0
  29. 1 1 35328 3091164 610484 2104228 0 0 4 18440 2454 2996 0 18 71 10 0
  30. 1 1 35328 3046964 647348 2111792 0 0 0 0 2525 2791 0 17 71 12 0
  31. 1 1 35328 3003624 683196 2119212 0 0 0 18456 2434 3134 0 16 72 11 0
  32. 1 1 35328 2959312 720068 2126868 0 0 60 5120 2566 2781 0 16 73 11 0
  33. 1 2 35328 2914476 756932 2134572 0 0 0 13312 2503 2968 0 15 68 17 0
  34. 1 1 35328 2871172 792772 2141996 0 0 0 12288 2375 2964 0 17 66 17 0
  35. 1 1 35328 2827068 829636 2149656 0 0 0 6144 2533 2969 0 17 71 11 0
  36. 1 1 35328 2783476 865492 2157076 0 0 0 18540 2507 2992 0 17 73 9 0
  37. 1 1 35328 2738896 902492 2164800 0 0 136 0 2496 2892 0 16 72 12 0
  38. 1 1 35328 2695872 938332 2173244 0 0 0 18432 2624 3046 0 17 72 11 0
  39. 1 1 35328 2651528 975196 2180888 0 0 0 2048 2477 2812 0 15 73 12 0
  40. 1 2 35328 2605828 1013084 2187756 0 0 0 16384 2660 3168 0 18 69 13 0
  41. 0 1 35328 2584676 1030364 2191104 0 0 896 8428 2221 3356 0 8 72 20 0
  42. 0 1 35328 2585300 1030364 2191432 0 0 0 10240 1678 2635 1 1 89 10 0
  43. 0 1 35328 2584948 1030364 2191448 0 0 0 13312 1712 2905 0 1 88 10 0
  44. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  45. r b swpd free buff cache si so bi bo in cs us sy id wa st
  46. 1 1 35328 2584796 1030364 2191960 0 0 0 5120 1667 2701 1 1 87 12 0
  47. 0 1 35328 2585580 1030364 2191960 0 0 0 18432 1715 2834 1 1 89 10 0
  48. 0 1 35328 2586348 1030508 2191528 0 0 240 0 1613 2663 0 1 86 13 0
  49. 1 1 35328 2586256 1030788 2191528 0 0 272 18468 1798 3003 1 2 86 11 0
  50. 0 1 35328 2586200 1031520 2191476 0 0 732 0 1704 2730 1 1 86 13 0
  51. 0 3 35328 2585340 1031672 2191972 0 0 184 17848 1887 4038 1 2 80 18 0
  52. 0 2 35328 2585720 1031672 2191932 0 0 0 3672 1817 4357 0 1 66 33 0
  53. 0 2 35328 2585400 1031672 2191868 0 0 0 14640 2797 10283 1 2 84 13 0
  54. 1 2 35328 2585452 1031672 2191868 0 0 0 5476 2016 5263 0 2 77 21 0
  55. 0 2 35328 2585156 1031672 2191868 0 0 0 13004 2541 9218 0 2 82 16 0
  56. 0 2 26624 2585292 1031672 2200880 0 0 0 8076 2168 5985 1 1 79 19 0
  57. 0 2 26624 2585220 1031812 2200724 0 0 140 10324 2521 8076 1 2 79 18 0
  58. 2 1 26624 2585432 1031812 2200880 0 0 0 10206 2317 7896 0 1 81 18 0
  59. 1 2 26624 2585204 1031812 2200880 0 0 0 8222 2349 6851 1 2 80 18 0
  60. 0 2 26624 2585396 1031812 2200880 0 0 0 11996 2570 8964 1 2 84 14 0
  61. 0 2 26624 2584784 1031812 2200880 0 0 0 6456 2119 5859 1 1 77 21 0
  62. 0 3 26624 2584912 1031820 2200888 0 0 0 14268 2695 9619 1 2 84 14 0
  63. 0 2 26624 2585008 1031820 2200896 0 0 0 4160 1973 4800 0 1 77 22 0
  64. 2 0 26624 2585184 1031820 2200880 0 0 0 16588 2760 10483 0 3 83 14 0
  65. 0 2 26624 2585072 1031820 2200864 0 0 0 1828 1774 3488 1 1 75 24 0
  66. 1 2 26624 2585272 1031820 2200864 0 0 0 17984 3110 11751 1 3 81 15 0
  67. 0 3 26624 2584560 1031820 2201408 0 0 0 632 1664 2844 0 1 63 36 0
  68. 0 2 26624 2584888 1031836 2201360 0 0 0 18388 3141 12309 0 3 83 13 0
  69. 0 2 26624 2584904 1031836 2200920 0 0 0 0 1598 2679 1 1 74 25 0
  70. 0 2 26624 2584704 1031836 2200896 0 0 0 18368 3198 13033 0 4 85 12 0
  71. 0 2 26624 2584384 1031836 2200896 0 0 0 0 1641 2597 0 1 74 25 0
  72. 0 2 26624 2584368 1031836 2200912 0 0 0 18532 3258 12265 1 2 85 11 0
  73. 0 2 26624 2584372 1031836 2200912 0 0 0 1592 1749 3359 0 1 76 23 0
  74. 0 2 26624 2584508 1031844 2200896 0 0 0 16876 3173 11559 1 2 86 11 0
  75. 0 2 26624 2584508 1031844 2200896 0 0 0 3496 1946 4549 1 1 76 22 0
  76. 0 2 26624 2584532 1031844 2200944 0 0 0 14864 2921 11310 1 2 84 13 0
  77. 0 2 26624 2584108 1031844 2200864 0 0 0 5352 2016 5443 0 1 77 21 0
  78. 0 2 26624 2584220 1031844 2200880 0 0 0 13584 2557 9503 0 2 81 17 0
  79. 0 2 26624 2584388 1031844 2200880 0 0 0 7012 2151 6031 0 1 79 20 0
  80. 0 2 26624 2584400 1031844 2200896 0 0 0 11008 2388 8253 0 2 80 19 0
  81. 0 2 26624 2584196 1031844 2200896 0 0 0 9424 2402 7631 1 2 81 17 0
  82. 0 3 26624 2584336 1031844 2200896 0 0 0 8968 2144 7117 1 1 71 27 0
  83. 0 2 26624 2584464 1031844 2200880 0 0 0 11752 2650 9452 0 2 76 22 0
  84. 0 2 26624 2584256 1031844 2200880 0 0 0 6680 2155 6340 0 1 79 20 0
  85. 1 1 26624 2584400 1031844 2200880 0 0 0 13631 2779 9646 0 2 83 15 0
  86. 0 2 26624 2584464 1031844 2200880 0 0 0 4833 1960 5223 0 1 78 21 0
  87. 0 2 26624 2584420 1031844 2201320 0 0 0 15240 2794 10887 1 1 86 12 0
  88. 0 2 26624 2583964 1031844 2201408 0 0 0 3440 1828 4161 1 0 77 22 0
  89. 0 2 26624 2584184 1031844 2200952 0 0 0 17168 3066 11326 0 3 83 13 0
  90. 0 2 26624 2584264 1031844 2200896 0 0 0 992 1788 3266 1 0 75 24 0
  91. 0 2 26624 2584288 1031844 2200896 0 0 0 18392 2783 10701 0 3 86 11 0
  92. 0 2 26624 2584336 1031852 2200880 0 0 0 16 1630 2504 1 1 74 25 0
  93. 0 2 26624 2584696 1031852 2200880 0 0 0 18492 3134 12068 1 3 86 11 0
  94. 0 2 26624 2584424 1031852 2200864 0 0 0 0 1537 2441 0 0 74 25 0
  95. 0 2 26624 2584356 1031852 2200880 0 0 0 18408 3077 12589 0 3 86 10 0
  96. 0 2 26624 2584100 1031852 2200880 0 0 0 1284 1734 3420 1 1 75 24 0
  97. 1 2 26624 2584100 1031852 2200944 0 0 0 17152 3180 11862 1 2 85 12 0
  98. 0 2 26624 2584068 1031872 2200924 0 0 4 2992 1894 4130 1 1 76 22 0
  99. 0 3 26624 2584124 1031872 2200896 0 0 0 15544 2638 10458 0 3 81 15 0
  100. 0 2 26624 2583956 1031872 2200896 0 0 0 5640 2147 5501 1 1 79 20 0
  101. 0 2 26624 2583680 1031872 2200944 0 0 0 12780 2653 9480 1 2 84 14 0
  102. 0 2 26624 2584016 1031872 2200944 0 0 0 6928 2315 6600 1 2 79 19 0
  103. 0 2 26624 2583408 1031872 2200896 0 0 0 12308 2420 7792 1 1 81 17 0
  104. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  105. r b swpd free buff cache si so bi bo in cs us sy id wa st
  106. 2 2 26624 2583808 1031880 2200888 0 0 0 8964 2299 6721 1 1 79 19 0
  107. 0 2 26624 2583588 1031880 2200880 0 0 0 8740 2424 7294 1 2 79 18 0
  108. 0 2 26624 2582756 1031880 2200880 0 0 0 12428 2621 9100 1 3 81 16 0
  109. 0 2 26624 2583384 1031880 2201392 0 0 0 5988 2108 5515 1 1 78 21 0
  110. 0 2 26624 2582184 1031880 2201392 0 0 0 13888 3242 12555 1 2 84 13 0
  111. 0 2 26624 2583464 1031880 2200896 0 0 0 4520 2136 5258 1 1 77 21 0
  112. 2 2 26624 2583656 1031880 2200880 0 0 0 14908 2899 11503 1 3 85 12 0
  113. 0 2 26624 2583588 1031880 2200880 0 0 0 3624 1930 4443 0 1 76 22 0
  114. 2 0 26624 2584284 1031880 2200880 0 0 0 16434 3083 11187 1 3 84 13 0
  115. 0 2 26624 2583588 1031880 2200864 0 0 0 1938 1756 3453 0 0 76 23 0
  116. 1 2 26624 2584380 1031880 2200880 0 0 0 18444 3127 12491 1 4 84 11 0
  117. 0 2 26624 2583604 1031888 2200880 0 0 0 12 1626 2567 0 1 74 25 0
  118. 0 3 22272 2583808 1031888 2204988 0 0 0 18404 3224 12941 1 3 83 13 0
  119. 0 3 22272 2583840 1031888 2205080 0 0 0 0 1724 2851 1 0 74 25 0
  120. 0 2 22272 2584120 1031888 2205080 0 0 0 18432 3280 11932 1 2 87 11 0
  121. 0 2 22272 2583840 1031888 2205080 0 0 0 676 1663 2911 1 1 75 24 0
  122. 0 2 22272 2583400 1031888 2205064 0 0 0 17764 3159 11545 0 3 86 11 0
  123. 0 2 22272 2582996 1031888 2205080 0 0 0 3168 1764 3792 0 1 76 23 0
  124. 0 2 22272 2583548 1031888 2205064 0 0 0 23992 3566 14771 0 3 90 6 0
  125. 0 0 22272 2583432 1031896 2205064 0 0 0 2475 2055 4464 1 1 96 1 0
  126. 0 0 22272 2583440 1031896 2205064 0 0 0 0 1596 2514 0 1 99 0 0
  127. 0 0 22272 2583340 1031896 2205128 0 0 0 0 1530 2448 0 0 99 0 0
  128. 0 0 22272 2583348 1031896 2205144 0 0 0 0 1573 2709 0 0 99 0 0
  129. 0 0 22272 2583592 1031896 2205144 0 0 0 0 1561 2546 0 0 99 0 0

观察到的现象:
虽然同样是写数据,但是写文件和写磁盘的现象不同。写磁盘时,buffer和cached都在增长,但是buffer的增长还是快的多。
说明写磁盘用到了大量的buffer
对比上述两个案例,可以发现写文件时会用到cache缓存数据,写磁盘会用buffer来缓存数据。所以Cache虽然只说了cache是文件读的缓存,但实际上,写文件时cache也会缓存

磁盘和文件读案例

读取文件数据

  1. echo 3 > /proc/sys/vm/drop_caches
  2. kylin@kylin-thinkpad-t14-gen-1 /tmp> dd if=/tmp/file of=/dev/null;time sync
  3. 2048000+0 records in
  4. 2048000+0 records out
  5. 1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.945094 s, 1.1 GB/s
  6. ________________________________________________________
  7. Executed in 5.09 millis fish external
  8. usr time 878.00 micros 146.00 micros 732.00 micros
  9. sys time 57.00 micros 57.00 micros 0.00 micros
  1. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  2. r b swpd free buff cache si so bi bo in cs us sy id wa st
  3. 0 0 35072 3822304 2744 1975760 0 0 0 0 1562 2609 0 0 99 0 0
  4. 0 0 35072 3822288 2744 1975832 0 0 0 0 1714 2942 1 0 98 0 0
  5. 0 0 35072 3822760 2744 1975376 0 0 0 0 1533 2489 0 0 100 0 0
  6. 0 0 35072 3822540 2752 1975304 0 0 0 48 1553 2519 0 0 99 0 0
  7. 0 0 35072 3822784 2752 1975320 0 0 0 0 1666 2810 1 0 99 0 0
  8. 0 0 35072 3822800 2752 1975304 0 0 0 0 1568 2562 1 0 99 0 0
  9. 1 0 35072 3409084 3492 2388644 0 0 413608 0 5034 2835 1 5 93 0 0
  10. 0 0 35072 2795704 3988 3001184 0 0 611748 76 6903 3232 1 8 91 0 0
  11. 0 0 35072 2795720 4132 3001504 0 0 240 0 1535 2483 1 0 99 0 0
  12. 0 0 35072 2795704 4132 3001504 0 0 0 0 1542 2565 0 0 99 0 0
  13. 0 0 35072 2795720 4132 3001504 0 0 0 0 1653 2889 1 0 99 0 0
  14. 1 0 35072 2795452 4132 3001488 0 0 0 0 1551 2503 0 1 99 0 0
  15. 0 0 35072 2795468 4132 3001472 0 0 0 0 1505 2505 1 0 99 0 0
  16. 0 0 35072 2795460 4132 3001488 0 0 0 0 1605 2722 1 1 99 0 0

cache猛增 buffer基本不变,和查到的定义相符
读取磁盘数据

  1. echo 3 > /proc/sys/vm/drop_caches
  2. dd if=/dev/sda1 of=/dev/null bs=1M count=1024
  3. Input Password
  4. 100+0 records in
  5. 100+0 records out
  6. 104857600 bytes (105 MB, 100 MiB) copied, 0.928879 s, 113 MB/s
  7. ________________________________________________________
  8. Executed in 14.45 millis fish external
  9. usr time 0.57 millis 574.00 micros 0.00 millis
  10. sys time 2.37 millis 0.00 micros 2.37 millis
  1. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  2. r b swpd free buff cache si so bi bo in cs us sy id wa st
  3. 0 0 26368 3780484 12856 2001404 0 0 0 0 1546 2560 1 1 99 0 0
  4. 1 0 26368 3777604 13552 2002396 0 0 1812 116 1924 3439 2 1 97 0 0
  5. 0 0 26368 3777648 13552 2002560 0 0 0 0 1572 2580 0 0 100 0 0
  6. 0 0 26368 3777648 13552 2002544 0 0 0 0 1526 2598 0 0 99 0 0
  7. 0 1 26368 3674964 114044 2003484 0 0 101232 60 2344 4231 1 2 93 4 0
  8. 0 0 26368 3667908 122468 2003612 0 0 8460 436 1695 2845 1 1 97 1 0
  9. 0 0 26368 3667908 122468 2003612 0 0 0 0 1478 2578 0 0 99 0 0
  10. 0 0 26368 3667884 122468 2003676 0 0 0 0 1505 2738 1 1 99 0 0
  11. 0 0 26368 3667656 122468 2004124 0 0 0 0 1540 2582 1 0 99 0 0
  12. 0 0 26368 3668112 122468 2003668 0 0 0 0 1510 2519 0 0 99 0 0
  13. 0 0 26368 3667844 122644 2003644 0 0 0 1296 1909 3608 1 1 97 1 0
  14. 1 0 26368 3667592 122644 2003644 0 0 0 0 1511 2561 1 0 99 0 0
  15. 1 0 26368 3667340 122644 2003648 0 0 4 0 1451 2455 1 1 99 0 0
  16. 0 0 26368 3667592 122644 2003648 0 0 0 0 1585 2826 1 1 99 0 0
  17. 0 0 26368 3667592 122644 2003664 0 0 0 0 1453 2432 0 0 99 0 0

buffer增长,说明数据都到了buffer中。
所以对于man手册的进一步补充的、更加精确的定义应该为:

  • Buffer:将要写入磁盘数据的缓存,以及从磁盘读取数据的缓存
  • Cache:从文件读取数据的页缓存,以及写文件的缓存

    磁盘和文件的区别

    磁盘是块设备,可以划为不同的分区,分区上还可以再建立文件系统,之后再挂载到某个目录,之后才可以在这个目录读写文件
    cache中所指的文件是普通文件,磁盘是块设备文件。
    所以Cache是page cache,buffer是block buffer。
    一个文件的读,应该是先到block buffer然后到page cache,这样一个文件会被cache两次,所以后来的linux将两个cache统一了,page cache直接指向block buffer,对于非文件就直接是block buffer。

    TODO

  • 动态追踪——Perf