1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <folly/Portability.h>
20 
21 #include <chrono>
22 #include <cstdint>
23 
24 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
25 extern "C" std::uint64_t __rdtsc();
26 #pragma intrinsic(__rdtsc)
27 #endif
28 
29 namespace folly {
30 
hardware_timestamp()31 inline std::uint64_t hardware_timestamp() {
32 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
33   return __rdtsc();
34 #elif defined(__GNUC__) && (defined(__i386__) || FOLLY_X64)
35   return __builtin_ia32_rdtsc();
36 #else
37   // use steady_clock::now() as an approximation for the timestamp counter on
38   // non-x86 systems
39   return std::chrono::steady_clock::now().time_since_epoch().count();
40 #endif
41 }
42 
43 } // namespace folly
44