2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_TSC_H_
18 #define incl_HPHP_TSC_H_
20 #include <folly/portability/Asm.h>
22 #include "hphp/util/assertions.h"
30 * Return the underlying machine cycle counter. While this is slightly
31 * non-portable in theory, all the CPUs you're likely to care about support
32 * it in some way or another.
34 inline uint64_t cpuCycles() {
37 asm volatile("rdtsc" : "=a"((lo
)),"=d"(hi
));
38 return lo
| (hi
<< 32);
40 // This returns a time-base
42 asm volatile("mfspr %0, 268" : "=r" (tb
));
45 return (uint64_t)__rdtsc();
47 // FIXME: This returns the virtual timer which is not exactly
48 // the core cycles but has a different frequency.
50 asm volatile("mrs %0, cntvct_el0" : "=r" (tb
));
57 inline void cpuRelax() {
58 folly::asm_volatile_pause();
61 inline void cycleDelay(uint32_t numCycles
) {
62 auto start
= cpuCycles();
64 if (numCycles
> 100) cpuRelax();
65 } while (cpuCycles() - start
< numCycles
);