1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_Stopwatch.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  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   $Revision: 17401 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/lyt/lyt_Common.h>
19 #include <nw/lyt/lyt_Stopwatch.h>
20 
21 namespace nw {
22 namespace lyt {
23 namespace internal {
24 namespace {
25 
26 #ifdef NW_PLATFORM_CTR
operator /(const TimeSpan & lhs,int rhs)27 inline const TimeSpan operator / (const TimeSpan& lhs, int rhs)
28 {
29     if (rhs != 0)
30     {
31         return TimeSpan::FromNanoSeconds(lhs.GetNanoSeconds() / rhs);
32     }
33     else
34     {
35         return TimeSpan();
36     }
37 }
38 #endif
39 
40 } // namespace {anonymous}
41 
42 int Stopwatch::s_TargetNoMin = INT_MAX;
43 int Stopwatch::s_TargetNoMax = INT_MIN;
44 
45 #ifdef NW_PLATFORM_CTR
Stopwatch(int no,const char * label)46 Stopwatch::Stopwatch(int no, const char* label)
47   : m_Count(0),
48     m_No(no),
49     m_Label(label),
50     m_MinSample(S64_MAX),
51     m_MaxSample(0)
52 {
53     List& list = this->GetList();
54     list.push_back(this);
55 }
56 #else
Stopwatch(int,const char *)57 Stopwatch::Stopwatch(int, const char*)
58 {
59 }
60 #endif
61 
62 
Reset()63 void Stopwatch::Reset()
64 {
65 #ifdef NW_PLATFORM_CTR
66     m_Count = 0;
67     m_TotalTick = Tick();
68     m_MinSample = S64_MAX;
69     m_MaxSample = 0;
70 #endif
71 }
72 
ResetAll()73 void Stopwatch::ResetAll()
74 {
75     List& list = GetList();
76     for (List::Iterator it = list.begin(); it != list.end(); ++it)
77     {
78         it->Reset();
79     }
80 }
81 
Enable()82 void Stopwatch::Enable()
83 {
84     SetTargetNo(INT_MIN, INT_MAX);
85 }
86 
Disable()87 void Stopwatch::Disable()
88 {
89     SetTargetNo(INT_MAX, INT_MIN);
90 }
91 
Dump(const Stopwatch & stopwatch)92 void Stopwatch::Dump(const Stopwatch& stopwatch)
93 {
94 #ifndef NW_PLATFORM_CTR
95     NW_UNUSED_VARIABLE(stopwatch);
96     NW_LYT_PRINT("Stopwatch::Dump is not implemented.\n");
97 #else
98     TimeSpan total = stopwatch.GetElapsedTime();
99     int count = stopwatch.GetCount();
100 
101     const char* label = stopwatch.GetLabel();
102     if (label == NULL)
103     {
104         label = "-";
105     }
106 
107     {
108         NW_LYT_PRINT(
109             "| %s | %lld | %lld / %d | %lld | %lld | %d |\n",
110             label,
111             (total / count).GetMicroSeconds(),
112             total.GetMicroSeconds(),
113             count,
114             stopwatch.GetMaxSample().GetMicroSeconds(),
115             stopwatch.GetMinSample().GetMicroSeconds(),
116             stopwatch.GetNo());
117     }
118 #endif
119 }
120 
DumpHeader()121 void Stopwatch::DumpHeader()
122 {
123 #ifdef NW_PLATFORM_CTR
124     NW_LYT_PRINT("| label | average | total / count | max | min | no |h\n");
125 #endif
126 }
127 
Dump()128 void Stopwatch::Dump()
129 {
130 #ifndef NW_PLATFORM_CTR
131     NW_LYT_PRINT("Stopwatch::Dump is not implemented.\n");
132 #else
133     NW_LYT_PRINT("\n");
134     DumpHeader();
135 
136     List& list = GetList();
137     for (List::Iterator it = list.begin(); it != list.end(); ++it)
138     {
139         if (it->GetCount() > 0)
140         {
141             Dump(*it);
142         }
143     }
144 #endif
145 }
146 
147 } // namespace internal
148 } // namespace lyt
149 } // namespace nw
150