1 /*--------------------------------------------------------------------------
2   Project:  Horizon
3   File:     rdt_Stopwatch.h
4 
5   Copyright (C)2010 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: 22867 $
14  *-------------------------------------------------------------------------*/
15 
16 
17 #ifndef NN_RDT_STOPWATCH_H_
18 #define NN_RDT_STOPWATCH_H_
19 
20 #ifdef _WIN32
21 // Windows環境ではストップウォッチは使わないことにする。
22 // ビルドエラーの問題が解決できれば、使えるようにしたいのだが。
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 /*!
37     @brief これはRDTライブラリのパフォーマンス計測のために用意したクラスです。
38 */
39 class Stopwatch{
40 public:
41     // 管理領域のクリア等を行う。Initialize()が明示的に呼ばれていなくても、
42     // コンストラクタの中で適宜呼ばれる。
43     static void Initialize(void);
44 
45     // 後始末。Stopwatchを利用するつもりが無くなったら、呼ぶこと。
46     static void Finalize(void);
47 
48     // 初期化されて、利用可能な状態?
IsInitialized(void)49     static bool IsInitialized(void) { return s_initialized; }
50 
51     // 名前なしストップウォッチ。
52     Stopwatch(void);
53 
54     // 名前付きストップウォッチ。32文字まで。
55     Stopwatch(const char *name);
56 
57     // デストラクタ。リストから自分を取り除く。
58     ~Stopwatch(void);
59 
60     // 計測開始。
61     void Start(void);
62 
63     // 計測終了。
64     void Stop(void);
65 
66     // 何度測定を実行したか?
67     int  GetTimes(void) const;
68 
69     // 計測した数値をリセット。ストップウォッチに付けた名前まではリセットしない。
70     void Reset(void);
71 
72     // ストップウォッチの結果をコンソールにプリント。
73     void Print(void) const;
74 
75     // 全ストップウォッチの内容をリセット
76     static void ResetAll(void);
77 
78     // 全ストップウォッチの内容を表示
79     static void PrintAll(void);
80 
81     // 単体テスト。
82     static bool Test(void);
83 
84 private:
85     // 定数
86     static const int MAX_STOPWATCHES = 32;
87     static const int MAX_NAME_LENGTH = 32;
88 
89     // インスタンスの管理リスト
90     static Stopwatch *s_paStopwatches[MAX_STOPWATCHES];
91 
92     // 初期化済みかどうかを保持する変数。
93     static bool s_initialized;
94 
95     // インスタンスの初期化
96     bool initInstance(const char *name);
97 
98     // 管理リストに自分のインスタンスを登録。
99     // 登録するスペースがなかった場合にfalseが返る。
100     bool addToList(void);
101 
102     // 管理リストから自分のインスタンスを削除。
103     void removeFromList(void);
104 
105     // 名前を付ける。NULLポインタが渡された場合は、
106     // "(NULL)"という名前がセットされる。
107     void setName(const char *name);
108 
109     // 項目名を表示。
110     static void printHeadLine(void);
111 
112     // 中身(計測結果)を表示。
113     void printContents(void) const;
114 
115     // トータル時間。
116     s64 m_total;
117 
118     // Start()が呼ばれた時点でのTick
119     s64 m_start;
120 
121     // 計測したなかで、最小だったもの
122     s64 m_min;
123 
124     // 計測したなかで、最大だったもの
125     s64 m_max;
126 
127     // これまでの計測回数。
128     s32 m_times;
129 
130     // パディング。このクラスにはs64型が含まれているため、8バイトアライメントが要求される。
131     NN_PADDING4;
132 
133     // ストップウォッチに付けられた名前。
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