HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timing.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_TIMING_H
25 #define PXR_BASE_ARCH_TIMING_H
26 
27 /// \file arch/timing.h
28 /// \ingroup group_arch_SystemFunctions
29 /// High-resolution, low-cost timing routines.
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/api.h"
33 #include "pxr/base/arch/defines.h"
34 #include "pxr/base/arch/inttypes.h"
35 
36 /// \addtogroup group_arch_SystemFunctions
37 ///@{
38 
39 #if defined(ARCH_OS_LINUX) && defined(ARCH_CPU_INTEL)
40 #include <x86intrin.h>
41 #elif defined(ARCH_OS_DARWIN)
42 #include <mach/mach_time.h>
43 #elif defined(ARCH_OS_WINDOWS)
44 #include <intrin.h>
45 #endif
46 
48 
49 /// Macro that defines the clocks per second
50 ///
51 /// Unfortunately, Red Hat 7.1 does not define CLK_TCK correctly, so
52 /// ARCH_CLK_TCK is the only guaranteed way to get the value of CLK_TCK. The
53 /// value is currently the same on all of our platforms.
54 #define ARCH_CLK_TCK 100
55 
56 /// Return the current time in system-dependent units.
57 ///
58 /// The current time is returned as a number of "ticks", where each tick
59 /// represents some system-dependent amount of time. The resolution of the
60 /// timing routines varies, but on all systems, it is well under one
61 /// microsecond. The cost of this routine is in the tens of nanoseconds
62 /// on GHz class machines.
63 inline uint64_t
65 {
66 #if defined(ARCH_OS_DARWIN)
67  // On Darwin we'll use mach_absolute_time().
68  return mach_absolute_time();
69 #elif defined(ARCH_CPU_INTEL)
70  // On Intel we'll use the rdtsc instruction.
71  return __rdtsc();
72 #elif defined (ARCH_CPU_ARM)
73  uint64_t result;
74  __asm __volatile("mrs %0, CNTVCT_EL0" : "=&r" (result));
75  return result;
76 #else
77 #error Unknown architecture.
78 #endif
79 }
80 
81 /// Convert a duration measured in "ticks", as returned by
82 /// \c ArchGetTickTime(), to nanoseconds.
83 ///
84 /// An example to test the timing routines would be:
85 /// \code
86 /// int64_t t1 = ArchGetTickTime();
87 /// sleep(10);
88 /// int64_t t2 = ArchGetTickTime();
89 ///
90 /// // duration should be approximately 10/// 1e9 = 1e10 nanoseconds.
91 /// int64_t duration = ArchTicksToNanoseconds(t2 - t1);
92 /// \endcode
93 ///
95 int64_t ArchTicksToNanoseconds(uint64_t nTicks);
96 
97 /// Convert a duration measured in "ticks", as returned by
98 /// \c ArchGetTickTime(), to seconds.
100 double ArchTicksToSeconds(uint64_t nTicks);
101 
102 /// Convert a duration in seconds to "ticks", as returned by
103 /// \c ArchGetTickTime().
104 ARCH_API
105 uint64_t ArchSecondsToTicks(double seconds);
106 
107 /// Get nanoseconds per tick. Useful when converting ticks obtained from
108 /// \c ArchTickTime()
109 ARCH_API
111 
112 ///@}
113 
115 
116 #endif // PXR_BASE_ARCH_TIMING_H
ARCH_API double ArchTicksToSeconds(uint64_t nTicks)
uint64_t ArchGetTickTime()
Definition: timing.h:64
ARCH_API double ArchGetNanosecondsPerTick()
ARCH_API int64_t ArchTicksToNanoseconds(uint64_t nTicks)
ARCH_API uint64_t ArchSecondsToTicks(double seconds)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
#define ARCH_API
Definition: api.h:40
GLfloat seconds
Definition: wglew.h:950