Newer
Older
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* Timer.cpp
*
* Created on: Apr 4, 2017
* Author: wfg
*/
#include "utilities/profiling/iochrono/Timer.h"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
namespace adios
{
namespace profiling
{
Timer::Timer(const std::string process, const Support::Resolutions resolution,
const bool debug)
: m_Process{process}, m_Resolution{resolution}, m_DebugMode{debug}
{
}
Timer::~Timer() {}
void Timer::SetInitialTime()
{
m_InitialTime = std::chrono::high_resolution_clock::now();
m_InitialTimeSet = true;
}
void Timer::SetTime()
{
m_ElapsedTime = std::chrono::high_resolution_clock::now();
m_ProcessTime += GetCurrentTime();
}
std::string Timer::GetUnits() const
{
std::string units;
if (m_Resolution == Support::Resolutions::mus)
units = "mus";
else if (m_Resolution == Support::Resolutions::ms)
units = "ms";
else if (m_Resolution == Support::Resolutions::s)
units = "s";
else if (m_Resolution == Support::Resolutions::m)
units = "m";
else if (m_Resolution == Support::Resolutions::h)
units = "h";
return units;
}
// PRIVATE
long long int Timer::GetCurrentTime()
{
if (m_DebugMode == true)
{
if (m_InitialTimeSet == false)
throw std::invalid_argument("ERROR: SetInitialTime() in process " +
m_Process + " not called\n");
}
if (m_Resolution == Support::Resolutions::mus)
{
return std::chrono::duration_cast<std::chrono::microseconds>(m_ElapsedTime -
m_InitialTime)
.count();
}
else if (m_Resolution == Support::Resolutions::ms)
{
return std::chrono::duration_cast<std::chrono::milliseconds>(m_ElapsedTime -
m_InitialTime)
.count();
}
else if (m_Resolution == Support::Resolutions::s)
{
return std::chrono::duration_cast<std::chrono::seconds>(m_ElapsedTime -
m_InitialTime)
.count();
}
else if (m_Resolution == Support::Resolutions::m)
{
return std::chrono::duration_cast<std::chrono::minutes>(m_ElapsedTime -
m_InitialTime)
.count();
}
else if (m_Resolution == Support::Resolutions::h)
{
return std::chrono::duration_cast<std::chrono::hours>(m_ElapsedTime -
m_InitialTime)
.count();
}
return -1; // failure
}
} // end namespace
} // end namespace