1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     rdt_Stopwatch.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 
17 #ifndef NN_LIBRARIES_RDT_CTR_RDT_STOPWATCH_H_
18 #define NN_LIBRARIES_RDT_CTR_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     // Clear the management region. Even if the Initialize function is not explicitly called, it is appropriately called in the constructor.
42     //
43     static void Initialize(void);
44 
45     // Clean up. Call if there is no longer any intent to use Stopwatch.
46     static void Finalize(void);
47 
48     // In an initialized state and can be used?
IsInitialized(void)49     static bool IsInitialized(void) { return s_initialized; }
50 
51     // A no-name stopwatch.
52     Stopwatch(void);
53 
54     // A named stopwatch. Up to 32 characters.
55     explicit 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 the measured value. Do not reset the name given to the stopwatch.
70     void Reset(void);
71 
72     // Print the stopwatch result to the console.
73     void Print(void) const;
74 
75     // Reset all stopwatch content.
76     static void ResetAll(void);
77 
78     // Display all stopwatch content.
79     static void PrintAll(void);
80 
81     // Stand-alone 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 to hold whether initialization has finished.
93     static bool s_initialized;
94 
95     // Instance initialization.
96     bool initInstance(const char *name);
97 
98     // Register own instance in the management list.
99     // Return FALSE when there is no registered space.
100     bool addToList(void);
101 
102     // Delete own instance from the management list.
103     void removeFromList(void);
104 
105     // Assign a name. When the 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 the Start function is called
119     s64 m_start;
120 
121     // Of the measurements, the smallest.
122     s64 m_min;
123 
124     // Of the measurements, the greatest.
125     s64 m_max;
126 
127     // Number of times measured up until now.
128     s32 m_times;
129 
130     // Padding. Because the s64 type is included in this class, an 8-byte alignment is required.
131     NN_PADDING4;
132 
133     // Name given to the 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     explicit 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_LIBRARIES_RDT_CTR_RDT_STOPWATCH_H_
189