Commit aedf6079 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Move asm_rdtsc to its own header and rename

Summary: [Folly] Move `asm_rdtsc` to its own header and rename to `hardware_timestamp`.

Reviewed By: aary, Orvid

Differential Revision: D10368436

fbshipit-source-id: 88a6d1d50702e80fb3ebba5035116850c49df6ea
parent 89622fb3
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/Portability.h>
#include <chrono>
#include <cstdint>
#if _MSC_VER
extern "C" std::uint64_t __rdtsc();
#pragma intrinsic(__rdtsc)
#endif
namespace folly {
inline std::uint64_t hardware_timestamp() {
#if _MSC_VER
return __rdtsc();
#elif __GNUC__ && (__i386__ || FOLLY_X64)
return __builtin_ia32_rdtsc();
#else
// use steady_clock::now() as an approximation for the timestamp counter on
// non-x86 systems
return std::chrono::steady_clock::now().time_since_epoch().count();
#endif
}
} // namespace folly
......@@ -18,7 +18,6 @@
#include <folly/Portability.h>
#include <chrono>
#include <cstdint>
#ifdef _MSC_VER
......@@ -45,20 +44,4 @@ inline void asm_volatile_pause() {
asm volatile("or 27,27,27");
#endif
}
inline std::uint64_t asm_rdtsc() {
#if _MSC_VER
return (uint64_t)__rdtsc();
#elif defined(__i386__) || FOLLY_X64
// read the timestamp counter on x86
auto hi = std::uint32_t{};
auto lo = std::uint32_t{};
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (((std::uint64_t)lo) + (((std::uint64_t)hi) << 32));
#else
// use steady_clock::now() as an approximation for the timestamp counter on
// non-x86 systems
return std::chrono::steady_clock::now().time_since_epoch().count();
#endif
}
} // namespace folly
......@@ -20,6 +20,7 @@
#include <folly/Portability.h>
#include <folly/ScopeGuard.h>
#include <folly/Utility.h>
#include <folly/chrono/Hardware.h>
#include <folly/detail/Futex.h>
#include <folly/lang/Align.h>
#include <folly/portability/Asm.h>
......@@ -188,7 +189,7 @@ class Waiter {
* https://c9x.me/x86/html/file_module_x86_id_278.html
*/
inline std::chrono::nanoseconds time() {
return std::chrono::nanoseconds{asm_rdtsc()};
return std::chrono::nanoseconds{hardware_timestamp()};
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment