use new hugify method only in fb builds
[hiphop-php.git] / hphp / util / cycles.h
blob4845cb7ac02fb3f3bc9e8b013543cfc98d9097d1
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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"
23 #ifdef _MSC_VER
24 #include <intrin.h>
25 #endif
27 namespace HPHP {
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() {
35 #ifdef __x86_64__
36 uint64_t lo, hi;
37 asm volatile("rdtsc" : "=a"((lo)),"=d"(hi));
38 return lo | (hi << 32);
39 #elif __powerpc64__
40 // This returns a time-base
41 uint64_t tb;
42 asm volatile("mfspr %0, 268" : "=r" (tb));
43 return tb;
44 #elif _MSC_VER
45 return (uint64_t)__rdtsc();
46 #elif __aarch64__
47 // FIXME: This returns the virtual timer which is not exactly
48 // the core cycles but has a different frequency.
49 uint64_t tb;
50 asm volatile("mrs %0, cntvct_el0" : "=r" (tb));
51 return tb;
52 #else
53 not_implemented();
54 #endif
57 inline void cpuRelax() {
58 folly::asm_volatile_pause();
61 inline void cycleDelay(uint32_t numCycles) {
62 auto start = cpuCycles();
63 do {
64 if (numCycles > 100) cpuRelax();
65 } while (cpuCycles() - start < numCycles);
70 #endif