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 +----------------------------------------------------------------------+
19 #include <folly/portability/Asm.h>
21 #include "hphp/util/assertions.h"
29 * Return the underlying machine cycle counter. While this is slightly
30 * non-portable in theory, all the CPUs you're likely to care about support
31 * it in some way or another.
33 inline uint64_t cpuCycles() {
36 asm volatile("rdtsc" : "=a"((lo
)),"=d"(hi
));
37 return lo
| (hi
<< 32);
39 // This returns a time-base
41 asm volatile("mfspr %0, 268" : "=r" (tb
));
44 return (uint64_t)__rdtsc();
46 // FIXME: This returns the virtual timer which is not exactly
47 // the core cycles but has a different frequency.
49 asm volatile("mrs %0, cntvct_el0" : "=r" (tb
));
56 inline void cpuRelax() {
57 folly::asm_volatile_pause();
60 inline void cycleDelay(uint32_t numCycles
) {
61 auto start
= cpuCycles();
63 if (numCycles
> 100) cpuRelax();
64 } while (cpuCycles() - start
< numCycles
);