forked from cppcheck-opensource/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
150 lines (131 loc) · 5.05 KB
/
timer.cpp
File metadata and controls
150 lines (131 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timer.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>
namespace {
using dataElementType = std::pair<std::string, TimerResultsData>;
bool more_second_sec(const dataElementType& lhs, const dataElementType& rhs)
{
return lhs.second.getSeconds() > rhs.second.getSeconds();
}
// TODO: remove and print through (synchronized) ErrorLogger instead
std::mutex stdCoutLock;
}
// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
// that could also get rid of the broader locking
void TimerResults::showResults(ShowTime mode, bool metrics, bool format) const
{
if (mode == ShowTime::NONE)
return;
std::vector<dataElementType> data;
{
std::lock_guard<std::mutex> l(mResultsSync);
data.reserve(mResults.size());
data.insert(data.begin(), mResults.cbegin(), mResults.cend());
}
std::sort(data.begin(), data.end(), more_second_sec);
// lock the whole logging operation to avoid multiple threads printing their results at the same time
std::lock_guard<std::mutex> l(stdCoutLock);
size_t ordinal = 1; // maybe it would be nice to have an ordinal in output later!
for (auto iter=data.cbegin(); iter!=data.cend(); ++iter) {
const double sec = iter->second.getSeconds().count();
const double secAverage = sec / static_cast<double>(iter->second.mNumberOfResults);
if ((mode != ShowTime::TOP5_FILE && mode != ShowTime::TOP5_SUMMARY) || (ordinal<=5)) {
std::cout << iter->first << ": ";
if (format)
std::cout << TimerResultsData::durationToString(iter->second.mDuration);
else
std::cout << sec << "s";
if (metrics)
std::cout << " (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))";
std::cout << std::endl;
}
++ordinal;
}
}
void TimerResults::addResults(const std::string& str, std::chrono::milliseconds duration)
{
std::lock_guard<std::mutex> l(mResultsSync);
mResults[str].mDuration += duration;
mResults[str].mNumberOfResults++;
}
void TimerResults::reset()
{
std::lock_guard<std::mutex> l(mResultsSync);
mResults.clear();
}
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, Type type)
: mName(std::move(str))
, mMode(showtimeMode)
, mType(type)
, mStart(Clock::now())
, mResults(timerResults)
{}
Timer::~Timer()
{
stop();
}
void Timer::stop()
{
if (mMode == ShowTime::NONE)
return;
if (mType == Type::OVERALL && mMode != ShowTime::TOP5_SUMMARY && mMode != ShowTime::SUMMARY) {
mMode = ShowTime::NONE;
return;
}
if (mType == Type::FILE && mMode != ShowTime::TOP5_FILE && mMode != ShowTime::FILE && mMode != ShowTime::FILE_TOTAL) {
mMode = ShowTime::NONE;
return;
}
if (mStart != TimePoint{}) {
if (!mResults) {
assert(false);
}
else {
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
mResults->addResults(mName, diff);
}
}
mMode = ShowTime::NONE; // prevent multiple stops
}
std::string TimerResultsData::durationToString(std::chrono::milliseconds duration)
{
// Extract hours
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
duration -= hours; // Subtract the extracted hours
// Extract minutes
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
duration -= minutes; // Subtract the extracted minutes
// Extract seconds
std::chrono::duration<double> seconds = std::chrono::duration_cast<std::chrono::duration<double>>(duration);
std::string ellapsedTime;
if (hours.count() > 0)
ellapsedTime += std::to_string(hours.count()) + "h ";
if (minutes.count() > 0)
ellapsedTime += std::to_string(minutes.count()) + "m ";
std::string secondsStr{std::to_string(seconds.count())};
auto pos = secondsStr.find_first_of('.');
if (pos != std::string::npos && (pos + 4) < secondsStr.size())
secondsStr.resize(pos + 4); // keep three decimal
return (ellapsedTime + secondsStr + "s");
}