gwnavruntime/kernel/SF_Timer.h Source File

SF_Timer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 Autodesk, Inc. All rights reserved.
3  * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4  * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5  */
6 
7 #pragma once
8 
10 
11 namespace Kaim
12 {
13 
14 class Timer
15 {
16 public:
17  enum
18  {
19  MsPerSecond = 1000, // Milliseconds in one second.
20  MksPerMs = 1000, // Microseconds in one millisecond.
21  MksPerSecond = MsPerSecond * MksPerMs
22  };
23 
24  // Returns ticks in milliseconds, as a 32-bit number. May wrap around every 49.2 days. Use either time difference of two values of GetTicks to avoid wrap-around.
25  // GetTicksMs may perform better then GetTicks.
26  static UInt32 GetTicksMs();
27 
28  // GetTicks returns general-purpose high resolution application timer value, measured in microseconds (mks, or 1/1000000 of a second).
29  // The actual precision is system-specific and may be much lower, such as 1 ms.
30  static UInt64 GetTicks();
31 
32  // ***** Profiling APIs.
33  // These functions should be used for profiling, but may have system specific artifacts that make them less appropriate for general system use.
34  // On Win32, for example these rely on QueryPerformanceConter may have problems with thread-core switching and power modes.
35 
36  // Return a hi-res timer value in mks (1/1000000 of a sec).
37  // Generally you want to call this at the start and end of an operation, and pass the difference to TicksToSeconds() to find out how long the operation took.
38  static UInt64 GetProfileTicks()
39  {
40  return (GetRawTicks() * MksPerSecond) / GetRawFrequency();
41  }
42 
43  // More convenient zero-based profile timer in seconds. First call initializes the "zero" value; future calls return the difference. Not thread safe for first call.
44  // Due to low precision of double, may malfunction after long runtime.
45  static double GetProfileSeconds()
46  {
47  static UInt64 StartTime = GetProfileTicks();
48  return TicksToSeconds(GetProfileTicks()-StartTime);
49  }
50 
51  // Get the raw cycle counter value, providing the maximum possible timer resolution.
52  static UInt64 GetRawTicks();
53  static UInt64 GetRawFrequency();
54 
55  // ***** Tick and time unit conversion.
56 
57  // Convert micro-second ticks value into seconds value.
58  static inline double TicksToSeconds(UInt64 ticks) { return static_cast<double>(ticks) * ((double)1.0 / (double)MksPerSecond); }
59 
60  // ***** Timer implementation overriding.
61  // Setting a non-null override instance will cause all Timer static functions to use the overridden implementation, instead of the default.
62  // This can be used (for example) to create deterministic timers, instead of wall-clock timers.
63  class TimerOverride
64  {
65  public:
66  virtual ~TimerOverride() {}
67 
68  virtual UInt32 GetTicksMs() = 0;
69  virtual UInt64 GetRawTicks() = 0;
70  virtual UInt64 GetRawFrequency() = 0;
71  };
72  static void SetTimerOverride(TimerOverride* instance);
73 
74 private:
75  friend class System;
76  // System called during program startup/shutdown.
77  static void initializeTimerSystem();
78  static void shutdownTimerSystem();
79 };
80 
81 } // Kaim::Timer
std::uint32_t UInt32
uint32_t
Definition: SF_Types.h:137
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::uint64_t UInt64
uint64_t
Definition: SF_Types.h:138