libfoedus-core
FOEDUS Core Library
rdtsc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_DEBUGGING_RDTSC_HPP_
19 #define FOEDUS_DEBUGGING_RDTSC_HPP_
20 
27 #include <stdint.h>
28 
29 namespace foedus {
30 namespace debugging {
35 inline uint64_t get_rdtsc() {
36 #ifndef __aarch64__
37  // x86.
38  uint32_t low, high;
39  asm volatile("rdtsc" : "=a" (low), "=d" (high));
40  return (static_cast<uint64_t>(high) << 32) | low;
41 #else // __aarch64__
42  // AArch64. "cntvct_el0" gives read-only physical 64bit timer.
43  // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0488d/ch09s03s01.html
44  uint64_t ret;
45  asm volatile("isb; mrs %0, cntvct_el0" : "=r" (ret));
46  return ret;
47 #endif // __aarch64__
48 }
49 
60 inline void wait_rdtsc_cycles(uint64_t cycles) {
61  uint64_t cycle_error = get_rdtsc() - cycles;
62  uint64_t cycle_until = get_rdtsc() + cycles;
63  while (true) {
64  uint64_t current = get_rdtsc();
65  if (current >= cycle_until || current <= cycle_error) {
66  break;
67  }
68  }
69 }
70 
71 } // namespace debugging
72 } // namespace foedus
73 #endif // FOEDUS_DEBUGGING_RDTSC_HPP_
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
void wait_rdtsc_cycles(uint64_t cycles)
Wait until the given CPU cycles elapse.
Definition: rdtsc.hpp:60
uint64_t get_rdtsc()
Returns the current CPU cycle via x86 RDTSC.
Definition: rdtsc.hpp:35