1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_TRACE_COMMON_CLOCK_H_
16 : #define SYZYGY_TRACE_COMMON_CLOCK_H_
17 :
18 : #include <intrin.h>
19 : #include <windows.h>
20 :
21 : #include "base/basictypes.h"
22 : #include "syzygy/common/assertions.h"
23 :
24 : namespace trace {
25 : namespace common {
26 :
27 : // A structure representing information about a timer. This can be used
28 : // (along with reference times) to translate between timers and clocks.
29 : // Both values will be set to zero for a timer that is not valid on a given
30 : // system.
31 : // NOTE: This is meant to be POD so that it can be written directly as is to and
32 : // from disk.
33 : struct TimerInfo {
34 : // The frequency of this timer, in counts per second.
35 : uint64 frequency;
36 : // The resolution of this timer, in counts.
37 : uint64 resolution;
38 : };
39 : COMPILE_ASSERT_IS_POD(TimerInfo);
40 :
41 : // Gets timer information about the various timers. A timer whose information
42 : // can not be found will have the frequency set to 0.
43 : // @param timer_info Will be populated with the information about the timer.
44 : void GetTickTimerInfo(TimerInfo* timer_info);
45 : void GetTscTimerInfo(TimerInfo* timer_info);
46 :
47 : // @returns the current value of the ticks timer.
48 : uint64 GetTicks();
49 :
50 : // @returns the current value of the TSC register using RDTSC.
51 E : inline uint64 GetTsc() { return ::__rdtsc(); }
52 :
53 : // Given a file time, a reference time and TimerInfo, convert the given
54 : // timer value to the corresponding file time. This can fail if the timer
55 : // info is invalid (frequency is 0, ie: unknown).
56 : // @param file_time_ref A reference file time.
57 : // @param timer_info Information regarding the timer frequency.
58 : // @param timer_ref The corresponding reference timer value.
59 : // @param timer_value The timer value to be converted.
60 : // @param file_time The file time to be populated.
61 : // @returns true on success, false otherwise.
62 : bool TimerToFileTime(const FILETIME& file_time_ref,
63 : const TimerInfo& timer_info,
64 : const uint64& timer_ref,
65 : const uint64& timer_value,
66 : FILETIME* file_time);
67 :
68 : // Information about the system clock and various timers.
69 : // NOTE: This is meant to be POD so that it can be written directly as is to and
70 : // from disk.
71 : struct ClockInfo {
72 : // Reference times. Used for converting between time formats.
73 : FILETIME file_time;
74 : uint64 ticks_reference;
75 : uint64 tsc_reference;
76 :
77 : // Information about the timers at our disposal.
78 : TimerInfo ticks_info;
79 : TimerInfo tsc_info;
80 : };
81 : COMPILE_ASSERT_IS_POD(ClockInfo);
82 :
83 : // Populates a ClockInfo struct with information about the system clock and
84 : // timers.
85 : // NOTE: This requires read access to the registry to get full information, and
86 : // is intended to be run from a process that has no restrictions. For
87 : // example, if this is run from a sandboxed process the TSC timer
88 : // information will be incomplete. A warning will be logged if this is the
89 : // case.
90 : // @param clock_info The struct to be populated.
91 : void GetClockInfo(ClockInfo* clock_info);
92 :
93 : // Converts a timer value to a file time given the clock info. This can fail if
94 : // the given timer has an invalid corresponding TimerInfo in @p clock_info.
95 : // @param clock_info The system clock information.
96 : // @param ticks The value of the tick counter.
97 : // @param tsc The value of the TSC counter.
98 : // @param file_time The file time to be populated.
99 : // @returns true on success, false otherwise.
100 : bool TicksToFileTime(const ClockInfo& clock_info,
101 : uint64 ticks,
102 : FILETIME* file_time);
103 : bool TscToFileTime(const ClockInfo& clock_info,
104 : uint64 tsc,
105 : FILETIME* file_time);
106 :
107 : } // namespace common
108 : } // namespace trace
109 :
110 : #endif // SYZYGY_TRACE_COMMON_CLOCK_H_
|