gooderp18绿色标准版
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.

179 lines
12KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pg_test_timing</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="pgtestfsync.html" title="pg_test_fsync" /><link rel="next" href="pgupgrade.html" title="pg_upgrade" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span xmlns="http://www.w3.org/1999/xhtml" class="application">pg_test_timing</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="pgtestfsync.html" title="pg_test_fsync">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-server.html" title="PostgreSQL Server Applications">Up</a></td><th width="60%" align="center">PostgreSQL Server Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pgupgrade.html" title="pg_upgrade">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="PGTESTTIMING"><div class="titlepage"></div><a id="id-1.9.5.11.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pg_test_timing</span></span></h2><p>pg_test_timing — measure timing overhead</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.5.11.4.1"><code class="command">pg_test_timing</code> [<em class="replaceable"><code>option</code></em>...]</p></div></div><div class="refsect1" id="id-1.9.5.11.5"><h2>Description</h2><p>
  3. <span class="application">pg_test_timing</span> is a tool to measure the timing overhead
  4. on your system and confirm that the system time never moves backwards.
  5. Systems that are slow to collect timing data can give less accurate
  6. <code class="command">EXPLAIN ANALYZE</code> results.
  7. </p></div><div class="refsect1" id="id-1.9.5.11.6"><h2>Options</h2><p>
  8. <span class="application">pg_test_timing</span> accepts the following
  9. command-line options:
  10. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-d <em class="replaceable"><code>duration</code></em></code><br /></span><span class="term"><code class="option">--duration=<em class="replaceable"><code>duration</code></em></code></span></dt><dd><p>
  11. Specifies the test duration, in seconds. Longer durations
  12. give slightly better accuracy, and are more likely to discover
  13. problems with the system clock moving backwards. The default
  14. test duration is 3 seconds.
  15. </p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
  16. Print the <span class="application">pg_test_timing</span> version and exit.
  17. </p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>
  18. Show help about <span class="application">pg_test_timing</span> command line
  19. arguments, and exit.
  20. </p></dd></dl></div><p>
  21. </p></div><div class="refsect1" id="id-1.9.5.11.7"><h2>Usage</h2><div class="refsect2" id="id-1.9.5.11.7.2"><h3>Interpreting Results</h3><p>
  22. Good results will show most (&gt;90%) individual timing calls take less than
  23. one microsecond. Average per loop overhead will be even lower, below 100
  24. nanoseconds. This example from an Intel i7-860 system using a TSC clock
  25. source shows excellent performance:
  26. </p><pre class="screen">
  27. Testing timing overhead for 3 seconds.
  28. Per loop time including overhead: 35.96 ns
  29. Histogram of timing durations:
  30. &lt; us % of total count
  31. 1 96.40465 80435604
  32. 2 3.59518 2999652
  33. 4 0.00015 126
  34. 8 0.00002 13
  35. 16 0.00000 2
  36. </pre><p>
  37. </p><p>
  38. Note that different units are used for the per loop time than the
  39. histogram. The loop can have resolution within a few nanoseconds (ns),
  40. while the individual timing calls can only resolve down to one microsecond
  41. (us).
  42. </p></div><div class="refsect2" id="id-1.9.5.11.7.3"><h3>Measuring Executor Timing Overhead</h3><p>
  43. When the query executor is running a statement using
  44. <code class="command">EXPLAIN ANALYZE</code>, individual operations are timed as well
  45. as showing a summary. The overhead of your system can be checked by
  46. counting rows with the <span class="application">psql</span> program:
  47. </p><pre class="screen">
  48. CREATE TABLE t AS SELECT * FROM generate_series(1,100000);
  49. \timing
  50. SELECT COUNT(*) FROM t;
  51. EXPLAIN ANALYZE SELECT COUNT(*) FROM t;
  52. </pre><p>
  53. </p><p>
  54. The i7-860 system measured runs the count query in 9.8 ms while
  55. the <code class="command">EXPLAIN ANALYZE</code> version takes 16.6 ms, each
  56. processing just over 100,000 rows. That 6.8 ms difference means the timing
  57. overhead per row is 68 ns, about twice what pg_test_timing estimated it
  58. would be. Even that relatively small amount of overhead is making the fully
  59. timed count statement take almost 70% longer. On more substantial queries,
  60. the timing overhead would be less problematic.
  61. </p></div><div class="refsect2" id="id-1.9.5.11.7.4"><h3>Changing Time Sources</h3><p>
  62. On some newer Linux systems, it's possible to change the clock source used
  63. to collect timing data at any time. A second example shows the slowdown
  64. possible from switching to the slower acpi_pm time source, on the same
  65. system used for the fast results above:
  66. </p><pre class="screen">
  67. # cat /sys/devices/system/clocksource/clocksource0/available_clocksource
  68. tsc hpet acpi_pm
  69. # echo acpi_pm &gt; /sys/devices/system/clocksource/clocksource0/current_clocksource
  70. # pg_test_timing
  71. Per loop time including overhead: 722.92 ns
  72. Histogram of timing durations:
  73. &lt; us % of total count
  74. 1 27.84870 1155682
  75. 2 72.05956 2990371
  76. 4 0.07810 3241
  77. 8 0.01357 563
  78. 16 0.00007 3
  79. </pre><p>
  80. </p><p>
  81. In this configuration, the sample <code class="command">EXPLAIN ANALYZE</code> above
  82. takes 115.9 ms. That's 1061 ns of timing overhead, again a small multiple
  83. of what's measured directly by this utility. That much timing overhead
  84. means the actual query itself is only taking a tiny fraction of the
  85. accounted for time, most of it is being consumed in overhead instead. In
  86. this configuration, any <code class="command">EXPLAIN ANALYZE</code> totals involving
  87. many timed operations would be inflated significantly by timing overhead.
  88. </p><p>
  89. FreeBSD also allows changing the time source on the fly, and it logs
  90. information about the timer selected during boot:
  91. </p><pre class="screen">
  92. # dmesg | grep "Timecounter"
  93. Timecounter "ACPI-fast" frequency 3579545 Hz quality 900
  94. Timecounter "i8254" frequency 1193182 Hz quality 0
  95. Timecounters tick every 10.000 msec
  96. Timecounter "TSC" frequency 2531787134 Hz quality 800
  97. # sysctl kern.timecounter.hardware=TSC
  98. kern.timecounter.hardware: ACPI-fast -&gt; TSC
  99. </pre><p>
  100. </p><p>
  101. Other systems may only allow setting the time source on boot. On older
  102. Linux systems the "clock" kernel setting is the only way to make this sort
  103. of change. And even on some more recent ones, the only option you'll see
  104. for a clock source is "jiffies". Jiffies are the older Linux software clock
  105. implementation, which can have good resolution when it's backed by fast
  106. enough timing hardware, as in this example:
  107. </p><pre class="screen">
  108. $ cat /sys/devices/system/clocksource/clocksource0/available_clocksource
  109. jiffies
  110. $ dmesg | grep time.c
  111. time.c: Using 3.579545 MHz WALL PM GTOD PIT/TSC timer.
  112. time.c: Detected 2400.153 MHz processor.
  113. $ pg_test_timing
  114. Testing timing overhead for 3 seconds.
  115. Per timing duration including loop overhead: 97.75 ns
  116. Histogram of timing durations:
  117. &lt; us % of total count
  118. 1 90.23734 27694571
  119. 2 9.75277 2993204
  120. 4 0.00981 3010
  121. 8 0.00007 22
  122. 16 0.00000 1
  123. 32 0.00000 1
  124. </pre></div><div class="refsect2" id="id-1.9.5.11.7.5"><h3>Clock Hardware and Timing Accuracy</h3><p>
  125. Collecting accurate timing information is normally done on computers using
  126. hardware clocks with various levels of accuracy. With some hardware the
  127. operating systems can pass the system clock time almost directly to
  128. programs. A system clock can also be derived from a chip that simply
  129. provides timing interrupts, periodic ticks at some known time interval. In
  130. either case, operating system kernels provide a clock source that hides
  131. these details. But the accuracy of that clock source and how quickly it can
  132. return results varies based on the underlying hardware.
  133. </p><p>
  134. Inaccurate time keeping can result in system instability. Test any change
  135. to the clock source very carefully. Operating system defaults are sometimes
  136. made to favor reliability over best accuracy. And if you are using a virtual
  137. machine, look into the recommended time sources compatible with it. Virtual
  138. hardware faces additional difficulties when emulating timers, and there are
  139. often per operating system settings suggested by vendors.
  140. </p><p>
  141. The Time Stamp Counter (TSC) clock source is the most accurate one available
  142. on current generation CPUs. It's the preferred way to track the system time
  143. when it's supported by the operating system and the TSC clock is
  144. reliable. There are several ways that TSC can fail to provide an accurate
  145. timing source, making it unreliable. Older systems can have a TSC clock that
  146. varies based on the CPU temperature, making it unusable for timing. Trying
  147. to use TSC on some older multicore CPUs can give a reported time that's
  148. inconsistent among multiple cores. This can result in the time going
  149. backwards, a problem this program checks for. And even the newest systems
  150. can fail to provide accurate TSC timing with very aggressive power saving
  151. configurations.
  152. </p><p>
  153. Newer operating systems may check for the known TSC problems and switch to a
  154. slower, more stable clock source when they are seen. If your system
  155. supports TSC time but doesn't default to that, it may be disabled for a good
  156. reason. And some operating systems may not detect all the possible problems
  157. correctly, or will allow using TSC even in situations where it's known to be
  158. inaccurate.
  159. </p><p>
  160. The High Precision Event Timer (HPET) is the preferred timer on systems
  161. where it's available and TSC is not accurate. The timer chip itself is
  162. programmable to allow up to 100 nanosecond resolution, but you may not see
  163. that much accuracy in your system clock.
  164. </p><p>
  165. Advanced Configuration and Power Interface (ACPI) provides a Power
  166. Management (PM) Timer, which Linux refers to as the acpi_pm. The clock
  167. derived from acpi_pm will at best provide 300 nanosecond resolution.
  168. </p><p>
  169. Timers used on older PC hardware include the 8254 Programmable Interval
  170. Timer (PIT), the real-time clock (RTC), the Advanced Programmable Interrupt
  171. Controller (APIC) timer, and the Cyclone timer. These timers aim for
  172. millisecond resolution.
  173. </p></div></div><div class="refsect1" id="id-1.9.5.11.8"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-explain.html" title="EXPLAIN"><span class="refentrytitle">EXPLAIN</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pgtestfsync.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-server.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgupgrade.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_test_fsync</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pg_upgrade</span></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1