LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.6 KiB

2 months ago
  1. # Reducing Variance
  2. <a name="disabling-cpu-frequency-scaling" />
  3. ## Disabling CPU Frequency Scaling
  4. If you see this error:
  5. ```
  6. ***WARNING*** CPU scaling is enabled, the benchmark real time measurements may be noisy and will incur extra overhead.
  7. ```
  8. you might want to disable the CPU frequency scaling while running the
  9. benchmark, as well as consider other ways to stabilize the performance of
  10. your system while benchmarking.
  11. See [Reducing Variance](reducing_variance.md) for more information.
  12. Exactly how to do this depends on the Linux distribution,
  13. desktop environment, and installed programs. Specific details are a moving
  14. target, so we will not attempt to exhaustively document them here.
  15. One simple option is to use the `cpupower` program to change the
  16. performance governor to "performance". This tool is maintained along with
  17. the Linux kernel and provided by your distribution.
  18. It must be run as root, like this:
  19. ```bash
  20. sudo cpupower frequency-set --governor performance
  21. ```
  22. After this you can verify that all CPUs are using the performance governor
  23. by running this command:
  24. ```bash
  25. cpupower frequency-info -o proc
  26. ```
  27. The benchmarks you subsequently run will have less variance.
  28. <a name="reducing-variance" />
  29. ## Reducing Variance in Benchmarks
  30. The Linux CPU frequency governor [discussed
  31. above](user_guide#disabling-cpu-frequency-scaling) is not the only source
  32. of noise in benchmarks. Some, but not all, of the sources of variance
  33. include:
  34. 1. On multi-core machines not all CPUs/CPU cores/CPU threads run the same
  35. speed, so running a benchmark one time and then again may give a
  36. different result depending on which CPU it ran on.
  37. 2. CPU scaling features that run on the CPU, like Intel's Turbo Boost and
  38. AMD Turbo Core and Precision Boost, can temporarily change the CPU
  39. frequency even when the using the "performance" governor on Linux.
  40. 3. Context switching between CPUs, or scheduling competition on the CPU the
  41. benchmark is running on.
  42. 4. Intel Hyperthreading or AMD SMT causing the same issue as above.
  43. 5. Cache effects caused by code running on other CPUs.
  44. 6. Non-uniform memory architectures (NUMA).
  45. These can cause variance in benchmarks results within a single run
  46. (`--benchmark_repetitions=N`) or across multiple runs of the benchmark
  47. program.
  48. Reducing sources of variance is OS and architecture dependent, which is one
  49. reason some companies maintain machines dedicated to performance testing.
  50. Some of the easier and and effective ways of reducing variance on a typical
  51. Linux workstation are:
  52. 1. Use the performance governor as [discussed
  53. above](user_guide#disabling-cpu-frequency-scaling).
  54. 1. Disable processor boosting by:
  55. ```sh
  56. echo 0 | sudo tee /sys/devices/system/cpu/cpufreq/boost
  57. ```
  58. See the Linux kernel's
  59. [boost.txt](https://www.kernel.org/doc/Documentation/cpu-freq/boost.txt)
  60. for more information.
  61. 2. Set the benchmark program's task affinity to a fixed cpu. For example:
  62. ```sh
  63. taskset -c 0 ./mybenchmark
  64. ```
  65. 3. Disabling Hyperthreading/SMT. This can be done in the Bios or using the
  66. `/sys` file system (see the LLVM project's [Benchmarking
  67. tips](https://llvm.org/docs/Benchmarking.html)).
  68. 4. Close other programs that do non-trivial things based on timers, such as
  69. your web browser, desktop environment, etc.
  70. 5. Reduce the working set of your benchmark to fit within the L1 cache, but
  71. do be aware that this may lead you to optimize for an unrelistic
  72. situation.
  73. Further resources on this topic:
  74. 1. The LLVM project's [Benchmarking
  75. tips](https://llvm.org/docs/Benchmarking.html).
  76. 1. The Arch Wiki [Cpu frequency
  77. scaling](https://wiki.archlinux.org/title/CPU_frequency_scaling) page.