1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 #pragma once
7 
8 #include <folly/Portability.h>
9 
10 #include <chrono>
11 #include <cstdint>
12 
13 #if _MSC_VER
14 extern "C" std::uint64_t __rdtsc();
15 #pragma intrinsic(__rdtsc)
16 #endif
17 
18 namespace folly {
19 
hardware_timestamp()20 inline std::uint64_t hardware_timestamp() {
21 #if _MSC_VER
22   return __rdtsc();
23 #elif __GNUC__ && (__i386__ || FOLLY_X64)
24   return __builtin_ia32_rdtsc();
25 #else
26   // use steady_clock::now() as an approximation for the timestamp counter on
27   // non-x86 systems
28   return std::chrono::steady_clock::now().time_since_epoch().count();
29 #endif
30 }
31 
32 } // namespace folly
33 
34