1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
8 * Author: Haomai Wang <haomaiwang@gmail.com>
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
16 /* Copyright (c) 2011-2014 Stanford University
18 * Permission to use, copy, modify, and distribute this software for any
19 * purpose with or without fee is hereby granted, provided that the above
20 * copyright notice and this permission notice appear in all copies.
22 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
23 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
25 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
27 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 * This class provides static methods that read the fine-grain CPU
37 * cycle counter and translate between cycle-level times and absolute
45 * Return the current value of the fine-grain CPU cycle counter
46 * (accessed via the RDTSC instruction).
48 static __inline __attribute__((always_inline)) uint64_t rdtsc() {
51 __asm__ volatile ("rdtsc" : "=A" (ret) );
53 #elif defined(__x86_64__) || defined(__amd64__)
55 __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
56 return (((uint64_t)hi << 32) | lo);
57 #elif defined(__aarch64__)
59 // arch/arm64/include/asm/arch_timer.h
61 // static inline u64 arch_counter_get_cntvct(void)
66 // asm volatile("mrs %0, cntvct_el0" : "=r" (cval));
71 // https://github.com/cloudius-systems/osv/blob/master/arch/aarch64/arm-clock.cc
73 asm volatile ("isb; mrs %0, cntvct_el0; isb; " : "=r" (cntvct) :: "memory");
75 #elif defined(__powerpc__) || defined (__powerpc64__)
77 // https://github.com/randombit/botan/blob/net.randombit.botan/src/lib/entropy/hres_timer/hres_timer.cpp
78 uint32_t lo = 0, hi = 0;
79 asm volatile("mftbu %0; mftb %1" : "=r" (hi), "=r" (lo));
80 return (((uint64_t)hi << 32) | lo);
82 #warning No high-precision counter available for your OS/arch
87 static double per_second();
88 static double to_seconds(uint64_t cycles, double cycles_per_sec = 0);
89 static uint64_t from_seconds(double seconds, double cycles_per_sec = 0);
90 static uint64_t to_microseconds(uint64_t cycles, double cycles_per_sec = 0);
91 static uint64_t to_nanoseconds(uint64_t cycles, double cycles_per_sec = 0);
92 static uint64_t from_nanoseconds(uint64_t ns, double cycles_per_sec = 0);
93 static void sleep(uint64_t us);
98 /// Conversion factor between cycles and the seconds; computed by
100 static double cycles_per_sec;
103 * Returns the conversion factor between cycles in seconds, using
104 * a mock value for testing when appropriate.
106 static __inline __attribute__((always_inline)) double get_cycles_per_sec() {
107 return cycles_per_sec;
111 #endif // CEPH_CYCLES_H