1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_Stopwatch.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/lyt/lyt_Common.h>
21 #include <nw/lyt/lyt_Stopwatch.h>
22 
23 namespace nw {
24 namespace lyt {
25 namespace internal {
26 namespace {
27 
28 #ifdef NW_PLATFORM_CTR
operator /(const TimeSpan & lhs,int rhs)29 inline const TimeSpan operator / (const TimeSpan& lhs, int rhs)
30 {
31     if (rhs != 0)
32     {
33         return TimeSpan::FromNanoSeconds(lhs.GetNanoSeconds() / rhs);
34     }
35     else
36     {
37         return TimeSpan();
38     }
39 }
40 #endif
41 
42 } // namespace {anonymous}
43 
44 int Stopwatch::s_TargetNoMin = INT_MAX;
45 int Stopwatch::s_TargetNoMax = INT_MIN;
46 
47 #ifdef NW_PLATFORM_CTR
Stopwatch(int no,const char * label)48 Stopwatch::Stopwatch(int no, const char* label)
49   : m_Count(0),
50     m_No(no),
51     m_Label(label),
52     m_MinSample(S64_MAX),
53     m_MaxSample(0)
54 {
55     List& list = this->GetList();
56     list.push_back(this);
57 }
58 #else
Stopwatch(int,const char *)59 Stopwatch::Stopwatch(int, const char*)
60 {
61 }
62 #endif
63 
64 
Reset()65 void Stopwatch::Reset()
66 {
67 #ifdef NW_PLATFORM_CTR
68     m_Count = 0;
69     m_TotalTick = Tick();
70     m_MinSample = S64_MAX;
71     m_MaxSample = 0;
72 #endif
73 }
74 
ResetAll()75 void Stopwatch::ResetAll()
76 {
77     List& list = GetList();
78     for (List::Iterator it = list.begin(); it != list.end(); ++it)
79     {
80         it->Reset();
81     }
82 }
83 
Enable()84 void Stopwatch::Enable()
85 {
86     SetTargetNo(INT_MIN, INT_MAX);
87 }
88 
Disable()89 void Stopwatch::Disable()
90 {
91     SetTargetNo(INT_MAX, INT_MIN);
92 }
93 
Dump(const Stopwatch & stopwatch)94 void Stopwatch::Dump(const Stopwatch& stopwatch)
95 {
96 #ifndef NW_PLATFORM_CTR
97     NW_UNUSED_VARIABLE(stopwatch);
98     NW_LYT_PRINT("Stopwatch::Dump is not implemented.\n");
99 #else
100     TimeSpan total = stopwatch.GetElapsedTime();
101     int count = stopwatch.GetCount();
102 
103     const char* label = stopwatch.GetLabel();
104     if (label == NULL)
105     {
106         label = "-";
107     }
108 
109     {
110         NW_LYT_PRINT(
111             "| %s | %lld | %lld / %d | %lld | %lld | %d |\n",
112             label,
113             (total / count).GetMicroSeconds(),
114             total.GetMicroSeconds(),
115             count,
116             stopwatch.GetMaxSample().GetMicroSeconds(),
117             stopwatch.GetMinSample().GetMicroSeconds(),
118             stopwatch.GetNo());
119     }
120 #endif
121 }
122 
DumpHeader()123 void Stopwatch::DumpHeader()
124 {
125 #ifdef NW_PLATFORM_CTR
126     NW_LYT_PRINT("| label | average | total / count | max | min | no |h\n");
127 #endif
128 }
129 
Dump()130 void Stopwatch::Dump()
131 {
132 #ifndef NW_PLATFORM_CTR
133     NW_LYT_PRINT("Stopwatch::Dump is not implemented.\n");
134 #else
135     NW_LYT_PRINT("\n");
136     DumpHeader();
137 
138     List& list = GetList();
139     for (List::Iterator it = list.begin(); it != list.end(); ++it)
140     {
141         if (it->GetCount() > 0)
142         {
143             Dump(*it);
144         }
145     }
146 #endif
147 }
148 
149 } // namespace internal
150 } // namespace lyt
151 } // namespace nw
152