1 /*--------------------------------------------------------------------------
2   Project:  Horizon
3   File:     rdt_Stopwatch.h
4   Copyright (C)2010 Nintendo Co., Ltd.  All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   $Rev: 22867 $
11  *-------------------------------------------------------------------------
12 
13 
14 */
15 
16 
17 #ifndef NN_RDT_STOPWATCH_H_
18 #define NN_RDT_STOPWATCH_H_
19 
20 #ifdef _WIN32
21 // Do not use the stopwatch in a Windows environment.
22 // Want to be able to use it once the build error problem is resolved.
23 #else
24 //  #define ENABLE_RDT_STOPWATCH
25 #endif
26 
27 
28 #ifdef ENABLE_RDT_STOPWATCH
29 
30 #include <nn/os.h>
31 
32 #include <limits.h>
33 
34 namespace nn { namespace rdt { namespace CTR { namespace detail {
35 
36 /* Please see man pages for details
37 
38 */
39 class Stopwatch{
40 public:
41     // Performs clearing of management region, etc. Even if Initialize function is not explicitly called,
42     // it is appropriately called in the constructor.
43     static void Initialize(void);
44 
45     // Clean up. Call when there is no intention to use Stopwatch.
46     static void Finalize(void);
47 
48     // Initialized, useable state?
IsInitialized(void)49     static bool IsInitialized(void) { return s_initialized; }
50 
51     // Unnamed stopwatch.
52     Stopwatch(void);
53 
54     // Named stopwatch. Up to 32 characters.
55     Stopwatch(const char *name);
56 
57     // Destructor Remove self from list.
58     ~Stopwatch(void);
59 
60     // Start measuring
61     void Start(void);
62 
63     // End measurement.
64     void Stop(void);
65 
66     // How many times was measurement run?
67     int  GetTimes(void) const;
68 
69     // Reset measured numeric value. Do not reset name given to stopwatch.
70     void Reset(void);
71 
72     // Print stopwatch results on console.
73     void Print(void) const;
74 
75     // Reset contents of all stopwatches
76     static void ResetAll(void);
77 
78     // Display contents of all stopwatches
79     static void PrintAll(void);
80 
81     // Single unit test.
82     static bool Test(void);
83 
84 private:
85     // Constants
86     static const int MAX_STOPWATCHES = 32;
87     static const int MAX_NAME_LENGTH = 32;
88 
89     // Instance management list
90     static Stopwatch *s_paStopwatches[MAX_STOPWATCHES];
91 
92     // Variable maintaining whether initialization completed.
93     static bool s_initialized;
94 
95     // Instance initialization
96     bool initInstance(const char *name);
97 
98     // Register own instance in management list.
99     // Returns false when there is no space for registration.
100     bool addToList(void);
101 
102     // Delete own instance from management list.
103     void removeFromList(void);
104 
105     // Attach name. When NULL pointer is passed, the name "NULL" is set.
106     //
107     void setName(const char *name);
108 
109     // Display item name.
110     static void printHeadLine(void);
111 
112     // Display content (measurement result).
113     void printContents(void) const;
114 
115     // Total time.
116     s64 m_total;
117 
118     // Tick when Start function was called
119     s64 m_start;
120 
121     // Smallest of the measurements
122     s64 m_min;
123 
124     // Largest of the measurements
125     s64 m_max;
126 
127     // Number of measurements so far.
128     s32 m_times;
129 
130     // Padding. Since this class includes the s64 format, 8-byte alignment is required.
131     NN_PADDING4;
132 
133     // Name given to stopwatch.
134     char m_name[MAX_NAME_LENGTH];
135 };
136 
137 
Start(void)138 inline void Stopwatch::Start(void)
139 {
140     m_start = nn::os::Tick::GetSystemCurrent();
141 }
142 
143 
GetTimes(void)144 inline int Stopwatch::GetTimes(void) const
145 {
146     return m_times;
147 }
148 
149 
Reset(void)150 inline void Stopwatch::Reset(void)
151 {
152     m_total = 0;
153     m_start = 0;
154     m_min   = LLONG_MAX;
155     m_max   = 0;
156     m_times = 0;
157 }
158 
159 
160 }}}} // namespace nn::rdt::CTR::detail
161 
162 #else  // else of ENABLE_RDT_STOPWATCH
163 
164 namespace nn { namespace rdt { namespace CTR { namespace detail {
165 
166 class Stopwatch {
167 public:
Initialize(void)168     static void Initialize   (void)        {               }
Finalize(void)169     static void Finalize     (void)        {               }
IsInitialized(void)170     static bool IsInitialized(void)        { return false; }
Stopwatch(void)171     Stopwatch                (void)        {               }
Stopwatch(const char *)172     Stopwatch                (const char*) {               }
~Stopwatch(void)173     ~Stopwatch               (void)        {               }
Start(void)174     void Start               (void)        {               }
Stop(void)175     void Stop                (void)        {               }
GetTimes(void)176     int  GetTimes            (void) const  { return 0;     }
Reset(void)177     void Reset               (void)        {               }
Print(void)178     void Print               (void) const  {               }
ResetAll(void)179     static void ResetAll     (void)        {               }
PrintAll(void)180     static void PrintAll     (void)        {               }
Test(void)181     static bool Test         (void)        { return false; }
182 };
183 
184 }}}} // namespace nn::rdt::CTR::detail
185 
186 #endif  // end of ENABLE_RDT_STOPWATCH
187 
188 #endif  // end of NN_RDT_STOPWATCH_H_
189