| 1 | <html> |
| 2 | <head> |
| 3 | <title>cpucycles: counting CPU cycles</title> |
| 4 | </head> |
| 5 | <body> |
| 6 | <h1><tt>cpucycles</tt>: counting CPU cycles</h1> |
| 7 | A C or C++ program can |
| 8 | call <tt>cpucycles()</tt> to receive a <tt>long long</tt> cycle count. |
| 9 | The program has to |
| 10 | <pre> |
| 11 | #include "cpucycles.h" |
| 12 | </pre> |
| 13 | and link to <tt>cpucycles.o</tt>. |
| 14 | The program can look at the constant string <tt>cpucycles_implementation</tt> |
| 15 | to see which implementation of <tt>cpucycles</tt> it's using. |
| 16 | The program can also call <tt>cpucycles_persecond()</tt> |
| 17 | to receive a <tt>long long</tt> estimate |
| 18 | of the number of cycles per second. |
| 19 | <p> |
| 20 | Here's how to create <tt>cpucycles.h</tt> and <tt>cpucycles.o</tt>: |
| 21 | <pre> |
| 22 | wget http://ebats.cr.yp.to/cpucycles-20060326.tar.gz |
| 23 | gunzip < cpucycles-20060326.tar.gz | tar -xf - |
| 24 | cd cpucycles-20060326 |
| 25 | sh do |
| 26 | </pre> |
| 27 | The <tt>do</tt> script creates <tt>cpucycles.h</tt> and <tt>cpucycles.o</tt>. |
| 28 | It also prints one line of output |
| 29 | showing the implementation selected, |
| 30 | the number of cycles per second, |
| 31 | a double-check of the number of cycles per second, |
| 32 | and the differences between several adjacent calls to the cpucycles() function. |
| 33 | <p> |
| 34 | Some systems have multiple incompatible formats for executable programs. |
| 35 | The most important reason is that some CPUs |
| 36 | (the Athlon 64, for example, and the UltraSPARC) |
| 37 | have two incompatible modes, a 32-bit mode and a 64-bit mode. |
| 38 | On these systems, you can run |
| 39 | <pre> |
| 40 | env ARCHITECTURE=32 sh do |
| 41 | </pre> |
| 42 | to create a 32-bit <tt>cpucycles.o</tt> |
| 43 | or |
| 44 | <pre> |
| 45 | env ARCHITECTURE=64 sh do |
| 46 | </pre> |
| 47 | to create a 64-bit <tt>cpucycles.o</tt>. |
| 48 | <h2>Notes on accuracy</h2> |
| 49 | Benchmarking tools are encouraged |
| 50 | to record several timings of a function: |
| 51 | call cpucycles(), function(), cpucycles(), function(), etc., |
| 52 | and then print one line reporting the differences |
| 53 | between successive cpucycles() results. |
| 54 | The median of several differences |
| 55 | is much more stable than the average. |
| 56 | <p> |
| 57 | Cycle counts continue to increase |
| 58 | while other programs are running, |
| 59 | while the operating system is handling an interruption such as a network packet, |
| 60 | etc. |
| 61 | This won't affect the median of several timings of a fast function---the |
| 62 | function usually won't be interrupted---but it can affect |
| 63 | the median of several timings of a slow function. |
| 64 | Hopefully a benchmarking machine isn't running other programs. |
| 65 | <p> |
| 66 | On dual-CPU systems (and dual-core systems such as the Athlon 64 X2), |
| 67 | the CPUs often don't have synchronized cycle counters, |
| 68 | so a process that switches CPUs can have its cycle counts jump forwards |
| 69 | or backwards. |
| 70 | I've never seen this affect the median of several timings. |
| 71 | <p> |
| 72 | Some CPUs dynamically reduce CPU speed to save power, |
| 73 | but deliberately keep their cycle counters running at full speed, |
| 74 | the idea being that measuring time is more important than measuring cycles. |
| 75 | Hopefully a benchmarking machine won't enter power-saving mode. |
| 76 | <p> |
| 77 | Cycle counts are occasionally off by a multiple of 2^32 on some CPUs, |
| 78 | as discussed below. |
| 79 | I've never seen this affect the median of several timings. |
| 80 | <p> |
| 81 | The estimate returned by <tt>cpucycles_persecond()</tt> |
| 82 | may improve accuracy after cpucycles() has been called repeatedly. |
| 83 | <h2>Implementations</h2> |
| 84 | <b>alpha.</b> |
| 85 | The Alpha's built-in cycle-counting function counts cycles modulo 2^32. |
| 86 | <tt>cpucycles</tt> usually manages to fix this |
| 87 | by calling <tt>gettimeofday</tt> |
| 88 | (which takes a large but low-variance number of cycles) |
| 89 | and automatically estimating the chip speed. |
| 90 | In extreme situations the resulting cycle counts |
| 91 | could still be off by a multiple of 2^32. |
| 92 | <p> |
| 93 | Results on td161: |
| 94 | alpha 499845359 499838717 423 360 336 349 353 348 469 329 348 345 348 345 348 345 348 345 348 345 348 348 348 345 348 345 348 345 348 348 348 345 348 345 348 345 348 348 348 345 348 345 348 345 348 348 348 345 348 345 348 345 348 348 348 468 318 348 345 348 345 348 345 348 345 348 |
| 95 | <p> |
| 96 | <b>amd64cpuinfo.</b> |
| 97 | <tt>cpucycles</tt> uses the CPU's RDTSC instruction to count cycles, |
| 98 | and reads <tt>/proc/cpuinfo</tt> |
| 99 | to see the kernel's estimate of cycles per second. |
| 100 | <p> |
| 101 | Results on dancer with ARCHITECTURE=64 (default): |
| 102 | amd64cpuinfo 2002653000 2002526765 22 9 9 8 8 17 6 10 5 9 8 8 8 17 6 10 5 9 8 8 8 17 6 10 5 9 8 8 8 17 6 10 5 9 8 8 11 14 15 28 10 8 9 12 23 106 10 8 8 8 8 8 8 17 6 10 5 9 8 8 8 17 6 10 |
| 103 | <p> |
| 104 | <b>amd64tscfreq.</b> |
| 105 | <tt>cpucycles</tt> uses the CPU's RDTSC instruction to count cycles, |
| 106 | and uses <tt>sysctlbyname("machdep.tsc_freq",...)</tt> |
| 107 | to see the kernel's estimate of cycles per second. |
| 108 | <p> |
| 109 | <b>clockmonotonic.</b> |
| 110 | Backup option, |
| 111 | using the POSIX clock_gettime(CLOCK_MONOTONIC) function to count nanoseconds |
| 112 | and using <tt>sysctlbyname("machdep.tsc_freq",...)</tt> |
| 113 | to see the kernel's estimate of cycles per second. |
| 114 | This often has much worse than microsecond precision. |
| 115 | <p> |
| 116 | Results on whisper (artificially induced): |
| 117 | clockmonotonic 1298904202 1298866469 2177 1815 2177 2177 1814 2177 2178 2177 1814 2178 2177 1814 2177 2177 1815 2177 2177 1814 2177 2179 1813 2178 2177 1815 2177 2177 1814 2177 2177 2177 1815 2178 2177 1813 2178 2177 1815 2177 2177 1814 2177 2177 1815 2177 2177 1814 2177 2179 2177 1814 2177 2177 2177 1815 2177 2177 1814 2177 2178 1814 2178 2177 1814 2177 |
| 118 | <p> |
| 119 | <b>gettimeofday.</b> |
| 120 | Backup option, |
| 121 | using the POSIX gettimeofday() function to count microseconds |
| 122 | and <tt>/proc/cpuinfo</tt> |
| 123 | to see the kernel's estimate of cycles per second. |
| 124 | This often has much worse than microsecond precision. |
| 125 | <p> |
| 126 | Results on dancer (artificially induced) with ARCHITECTURE=32: |
| 127 | gettimeofday 2002653000 2002307748 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 4005 0 4005 2003 0 4005 2003 2002 2003 2003 2002 2003 2003 2002 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 2003 2002 2003 4005 0 2003 4005 0 4006 2002 2003 |
| 128 | <p> |
| 129 | Results on dancer (artificially induced) with ARCHITECTURE=64 (default): |
| 130 | gettimeofday 2002653000 2002293956 2560 1792 2048 1792 2048 2304 1792 2048 1792 0 2048 2304 2048 1792 1792 2048 0 2304 2048 1792 2048 1792 2304 0 2048 1792 2048 1792 2048 0 2304 2048 1792 1792 2048 2304 2048 0 1792 2048 1792 2304 2048 1792 2048 0 1792 2560 1792 1792 2048 1792 0 2560 25600 2048 1792 2560 1792 0 2048 1792 2048 2304 |
| 131 | <p> |
| 132 | <b>hppapstat.</b> |
| 133 | <tt>cpucycles</tt> uses the CPU's MFCTL %cr16 instruction to count cycles, |
| 134 | and <tt>pstat(PSTAT_PROCESSOR,...)</tt> |
| 135 | to see the kernel's estimate of cycles per second. |
| 136 | <p> |
| 137 | Results on hp400: |
| 138 | hppapstat 440000000 439994653 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 |
| 139 | <p> |
| 140 | <b>powerpcaix.</b> |
| 141 | <tt>cpucycles</tt> uses the CPU's MFTB instruction to count ``time base''; |
| 142 | uses <tt>/usr/sbin/lsattr -E -l proc0 -a frequency</tt> |
| 143 | to see the kernel's estimate of cycles per second; |
| 144 | and spends some time comparing MFTB to gettimeofday() |
| 145 | to figure out the number of time-base counts per second. |
| 146 | <p> |
| 147 | I've seen |
| 148 | a 533MHz PowerPC G4 (7410) with a 16-cycle time base; |
| 149 | a 668MHz POWER RS64 IV (SStar) system with a 1-cycle time base; |
| 150 | a 1452MHz POWER with an 8-cycle time base; |
| 151 | and |
| 152 | a 2000MHz PowerPC G5 (970) with a 60-cycle time base. |
| 153 | <p> |
| 154 | Results on tigger: |
| 155 | powerpcaix 1452000000 1451981436 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 56 64 64 64 |
| 156 | <p> |
| 157 | <b>powerpclinux.</b> |
| 158 | <tt>cpucycles</tt> uses the CPU's MFTB instruction to count ``time base''; |
| 159 | reads <tt>/proc/cpuinfo</tt> |
| 160 | to see the kernel's estimate of cycles per second; |
| 161 | and spends some time comparing MFTB to gettimeofday() |
| 162 | to figure out the number of time-base counts per second. |
| 163 | <p> |
| 164 | Results on gggg: |
| 165 | powerpclinux 533000000 532650134 48 32 48 32 32 48 32 32 48 32 32 48 32 32 48 32 32 48 32 32 32 48 32 32 48 32 32 48 32 32 48 32 32 48 32 32 32 48 32 32 48 32 32 48 32 32 48 32 32 48 32 32 32 48 32 32 48 32 32 48 32 32 48 32 |
| 166 | <p> |
| 167 | <b>powerpcmacos.</b> |
| 168 | <tt>cpucycles</tt> uses the <tt>mach_absolute_time</tt> function |
| 169 | to count ``time base''; |
| 170 | uses <tt>sysctlbyname("hw.cpufrequency",...)</tt> |
| 171 | to see the kernel's estimate of cycles per second; |
| 172 | and uses <tt>sysctlbyname("hw.tbfrequency",...)</tt> |
| 173 | to see the kernel's estimate of time-base counts per second. |
| 174 | <p> |
| 175 | Results on geespaz with ARCHITECTURE=32 (default): |
| 176 | powerpcmacos 2000000000 1999891801 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 |
| 177 | <p> |
| 178 | Results on geespaz with ARCHITECTURE=64: |
| 179 | powerpcmacos 2000000000 1999896339 420 60 60 60 60 60 60 0 60 60 60 60 60 60 60 60 0 60 60 60 60 60 60 60 60 60 0 60 60 60 60 60 60 60 60 60 0 60 60 60 60 60 60 60 0 60 60 60 60 60 60 60 60 0 60 60 60 60 60 60 60 60 0 60 |
| 180 | <p> |
| 181 | <b>sparc32psrinfo.</b> |
| 182 | <tt>cpucycles</tt> uses the CPU's RDTICK instruction |
| 183 | in 32-bit mode to count cycles, |
| 184 | and runs <tt>/usr/sbin/psrinfo -v</tt> |
| 185 | to see the kernel's estimate of cycles per second. |
| 186 | <p> |
| 187 | Results on icarus with ARCHITECTURE=32 (default): |
| 188 | sparc32psrinfo 900000000 899920056 297 23 23 18 22 23 18 17 22 18 17 22 23 18 17 129 17 17 17 17 17 17 17 97 17 17 17 17 17 17 17 85 17 17 17 17 17 17 17 97 17 17 17 17 17 17 17 85 17 17 17 17 17 17 17 97 17 17 17 17 17 17 17 85 |
| 189 | <p> |
| 190 | Results on wessel with ARCHITECTURE=32 (default): |
| 191 | sparc32psrinfo 900000000 899997269 39 23 18 22 18 25 72 17 22 18 17 22 23 26 71 17 17 17 17 17 17 17 85 17 17 17 17 17 17 17 97 17 17 17 17 17 17 17 85 17 17 17 17 17 17 17 97 17 17 17 17 17 17 17 85 17 17 17 17 17 17 17 109 17 |
| 192 | <p> |
| 193 | <b>sparcpsrinfo.</b> |
| 194 | <tt>cpucycles</tt> uses the CPU's RDTICK instruction |
| 195 | in 64-bit mode to count cycles, |
| 196 | and runs <tt>/usr/sbin/psrinfo -v</tt> |
| 197 | to see the kernel's estimate of cycles per second. |
| 198 | <p> |
| 199 | Results on icarus with ARCHITECTURE=64: |
| 200 | sparcpsrinfo 900000000 899920264 289 12 12 12 12 12 12 19 12 113 19 12 12 12 12 12 12 130 12 12 12 12 12 12 12 144 12 12 12 12 12 12 12 144 12 12 12 12 12 12 12 144 12 12 12 12 12 12 12 144 12 12 12 12 12 12 12 144 12 12 12 12 12 12 |
| 201 | <p> |
| 202 | Results on wessel with ARCHITECTURE=64: |
| 203 | sparcpsrinfo 900000000 899997032 29 19 12 19 19 19 12 12 123 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 174 12 12 12 12 12 12 12 |
| 204 | <p> |
| 205 | <b>x86cpuinfo.</b> |
| 206 | <tt>cpucycles</tt> uses the CPU's RDTSC instruction to count cycles, |
| 207 | and reads <tt>/proc/cpuinfo</tt> |
| 208 | to see the kernel's estimate of cycles per second. |
| 209 | There have been reports of the 64-bit cycle counters on some x86 CPUs |
| 210 | being occasionally off by 2^32; |
| 211 | <tt>cpucycles</tt> makes no attempt to fix this. |
| 212 | <p> |
| 213 | Results on cruncher: |
| 214 | x86cpuinfo 132957999 132951052 60 36 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 |
| 215 | <p> |
| 216 | Results on dali: |
| 217 | x86cpuinfo 448882000 448881565 49 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 |
| 218 | <p> |
| 219 | Results on dancer with ARCHITECTURE=32: |
| 220 | x86cpuinfo 2002653000 2002538651 26 11 9 11 10 17 11 10 10 10 9 10 9 12 9 173 11 10 10 10 10 17 11 10 10 10 9 10 9 17 11 10 10 10 9 10 9 17 11 10 10 10 9 10 9 17 11 10 10 10 9 10 9 17 11 10 10 10 9 10 9 17 11 10 |
| 221 | <p> |
| 222 | Results on fireball: |
| 223 | x86cpuinfo 1894550999 1894188944 104 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 |
| 224 | <p> |
| 225 | Results on neumann: |
| 226 | x86cpuinfo 999534999 999456935 49 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 |
| 227 | <p> |
| 228 | Results on rzitsc: |
| 229 | x86cpuinfo 2799309000 2799170567 132 96 100 104 100 100 96 96 96 100 96 108 104 104 112 96 112 96 108 96 112 96 96 96 100 112 120 100 96 100 104 112 96 96 96 88 96 128 108 96 116 96 100 100 108 96 100 96 108 96 104 100 112 96 100 96 100 100 88 108 100 108 92 96 |
| 230 | <p> |
| 231 | Results on shell: |
| 232 | x86cpuinfo 3391548999 3391341751 108 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 |
| 233 | <p> |
| 234 | Results on thoth: |
| 235 | x86cpuinfo 900447000 900028758 67 19 18 18 19 188 16 16 16 19 19 18 19 147 16 16 16 19 19 17 16 16 16 16 16 19 19 17 16 16 16 16 16 19 19 17 16 16 16 16 16 19 19 18 19 156 16 16 16 19 19 18 19 147 16 16 16 19 19 18 19 147 16 16 |
| 236 | <p> |
| 237 | <b>x86tscfreq.</b> |
| 238 | <tt>cpucycles</tt> uses the CPU's RDTSC instruction to count cycles, |
| 239 | and uses <tt>sysctlbyname("machdep.tsc_freq",...)</tt> |
| 240 | to see the kernel's estimate of cycles per second. |
| 241 | <p> |
| 242 | Results on whisper: |
| 243 | x86tscfreq 1298904202 1298892874 72 72 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 |
| 244 | <h2>Version</h2> |
| 245 | This is the cpucycles-20060326.html web page. |
| 246 | This web page is in the public domain. |
| 247 | </body> |
| 248 | </html> |