TEMU  4.4
The Terma Emulator
Time.h
Go to the documentation of this file.
1 //===-- temu-c/Support/Time.h - Portable Timers -----------------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2015
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEMU_SUPPORT_TIME
10 #define TEMU_SUPPORT_TIME
11 #include "temu-c/Support/Temu3Compat.h"
12 #include "temu-c/Support/Attributes.h"
13 #include "temu-c/Support/Events.h"
14 #include <stdint.h>
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /*!
20  Get monotonic time in nanoseconds.
21 
22  The monotonic time is relative since some epoch of undefined start,
23  but it is monotonic, unaffected by adjustments due to leap seconds,
24  setting of the system clock, etc.
25 
26  This function is primarily useful for doing performance measurements
27  since the time returned is relative to an undefined point (although
28  that undefined point will be consistent while the program is
29  running).
30 
31  In practice, on systems with clock_gettime() implemented, this
32  function returns the timespec converted to nanoseconds, but on
33  systems without clock_gettime(), e.g. Darwin and Windows, the
34  function will only ensure that some notion of monotonic nanoseconds
35  are returned. In Darwin for example, this results in a monotonic
36  time returned which is relative to the first call to the function.
37 
38  \result Wall clock nanoseconds since an unspecified epoch.
39  */
40 TEMU_API uint64_t temu_timeGetMonotonicWct(void);
41 
42 /*!
43  * Returns wall clock nanoseconds of thread time
44  *
45  * NOTE: This is how much time the thread this is called from has been
46  * scheduled.
47  *
48  * \return wall clock nanoseconds of thread time
49  */
50 TEMU_API uint64_t temu_timeGetThreadWct(void);
51 
52 /*!
53  * Get the current simulated real-time in nanoseconds
54  * \param TS The object in question
55  * \return simulated time for the given object
56  */
57 TEMU_API uint64_t temu_timeGetCurrentSrtNanos(temu_TimeSource_ *TS);
58 
59 
60 /*!
61  * Convert cycles to steps, rounding upwards
62  * \param TS Time source object
63  * \param Cycles Cycle count to convert to steps
64  * \result Cycles converted to steps given the time source IPC/CPI configuration.
65  */
66 TEMU_API uint64_t temu_cyclesToStepsRoundedUp(temu_TimeSource *TS, uint64_t Cycles);
67 
68 /*!
69  * Convert steps to cycles, rounding upwards
70  * \param TS Time source object
71  * \param Steps Step count to convert to cycles
72  * \result Steps converted to cycles given the time source IPC/CPI configuration.
73  */
74 TEMU_API uint64_t temu_stepsToCyclesRoundedUp(temu_TimeSource *TS, uint64_t Steps);
75 
76 
77 /*!
78  * Convert cycles to steps
79  * \param TS Time source object
80  * \param Cycles Cycle count to convert to steps
81  * \result Cycles converted to steps given the time source IPC/CPI configuration.
82  */
83 TEMU_API uint64_t temu_cyclesToSteps(temu_TimeSource *TS, uint64_t Cycles);
84 
85 /*!
86  * Convert steps to cycles
87  * \param TS Time source object
88  * \param Steps Step count to convert to cycles
89  * \result Steps converted to cycles given the time source IPC/CPI configuration.
90  */
91 TEMU_API uint64_t temu_stepsToCycles(temu_TimeSource *TS, uint64_t Steps);
92 
93 /*!
94  * Get current step count
95  * \param TS Time source object
96  * \result Current steps as it is understood by the object.
97  */
98 TEMU_API int64_t temu_getSteps(temu_TimeSource *TS);
99 
100 
101 
102 /*!
103  * Get current time in cycles.
104  * \param TS Time source object
105  * \result Current cycles as it is understood by the object.
106  */
107 TEMU_API int64_t temu_getCycles(temu_TimeSource_ *TS);
108 
109 /*!
110  * Get current time in nanoseconds
111  * \param TS Time source object
112  * \result Current nanoseconds as it is understood by the object.
113  */
114 TEMU_API int64_t temu_getNanos(temu_TimeSource_ *TS);
115 
116 /*!
117  * Get current time in seconds
118  * \param TS Time source object
119  * \result Current seconds as it is understood by the object.
120  */
122 
123 /*
124  * Time conversion, note that this isn't super-trivial due to
125  * overflows. So use these functions, they are well tested and
126  * hardened against overflow. Note that due to rounding, there is no
127  * strict guarantee that the inverse functions are strictly inverse
128  * e.g. WE DO NOT GUARANTEE that:
129  * (cyclesToNanos(nanosToCycles(N, F), F)) == N
130  */
131 
132 /*!
133  * Convert cycles to nanoseconds
134  * \param Cycles Cycle count to convert
135  * \param Freq Frequency in Hz
136  * \result Cycles converted to nanoseconds
137  */
138 TEMU_API int64_t temu_cyclesToNanos(int64_t Cycles, int64_t Freq);
139 
140 /*!
141  * Convert cycles to another frequency base, with truncated result
142  * \param Cycles Cycle count in source frequency ticks
143  * \param SourceFreq Frequency in Hz of the source
144  * \param TargetFreq Frequency in Hz for the converted value
145  * \result Cycles, converted from source frequency to target frequency
146  */
147 TEMU_API int64_t temu_cyclesToOtherFreqTruncated(int64_t Cycles,
148  int64_t SourceFreq,
149  int64_t TargetFreq);
150 
151 /*!
152  * Convert cycles to another frequency base, rounding upwards
153  * \param Cycles Cycle count in source frequency ticks
154  * \param SourceFreq Frequency in Hz of the source
155  * \param TargetFreq Frequency in Hz for the converted value
156  * \result Cycles, converted from source frequency to target frequency
157  */
158 TEMU_API int64_t temu_cyclesToOtherFreqRoundedUp(int64_t Cycles,
159  int64_t SourceFreq,
160  int64_t TargetFreq);
161 
162 /*!
163  * Convert cycles to seconds
164  * \param Cycles Cycle count to convert
165  * \param Freq Frequency in Hz
166  * \result Cycles converted to seconds
167  */
169 
170 /*!
171  * Convert nanoseconds to cycles
172  *
173  * This function truncates the result in case of non-exact conversion.
174  * \param Nanos Nanoseconds to convert
175  * \param Freq Frequency in Hz
176  * \result Nanoseconds converted to cycles
177  */
178 TEMU_API int64_t temu_nanosToCycles(int64_t Nanos, int64_t Freq);
179 
180 /*!
181  * Convert nanoseconds to cycles rounded upwards
182  *
183  * This function rounds up the result in case of non-exact conversion.
184  * \param Nanos Nanoseconds to convert
185  * \param Freq Frequency in Hz
186  * \result Nanoseconds converted to cycles
187  */
188 TEMU_API int64_t temu_nanosToCyclesRoundedUp(int64_t Nanos, int64_t Freq);
189 
190 /*!
191  * Convert nanoseconds to seconds
192  * \param Nanos Nanoseconds to convert
193  * \result Nanoseconds converted to seconds
194  */
196 
197 /*!
198  * Convert seconds to nanoseconds
199  * \param Secs Seconds to convert
200  * \result Seconds converted to an integral number of nanoseconds
201  */
202 TEMU_API int64_t temu_secsToNanos(double Secs);
203 
204 /*!
205  * Convert seconds to cycles
206  * \param Secs Seconds to convert
207  * \param Freq Frequency in Hz
208  * \result Seconds converted to an integral number of cycles
209  */
210 TEMU_API int64_t temu_secsToCycles(double Secs, int64_t Freq);
211 
212 #ifdef __cplusplus
213 }
214 #endif
215 
216 #endif /* ! TEMU_SUPPORT_TIME */
temu_getSecs
TEMU_API double temu_getSecs(temu_TimeSource_ *TS)
temu_nanosToSecs
TEMU_API double temu_nanosToSecs(int64_t Nanos)
temu_cyclesToSecs
TEMU_API double temu_cyclesToSecs(int64_t Cycles, int64_t Freq)