-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiprof.cpp
62 lines (56 loc) · 1.6 KB
/
iprof.cpp
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
// Copyright (c) 2015-2019 Paweł Cichocki
// License: https://opensource.org/licenses/MIT
#include "iprof.hpp"
namespace InternalProfiler
{
iprof_thread_local Tree tree;
iprof_thread_local std::vector<RawEntry> entries;
iprof_thread_local Stats stats;
#ifndef DISABLE_IPROF_MULTITHREAD
std::mutex allThreadStatLock;
Stats allThreadStats;
#endif
void aggregateEntries()
{
std::vector<RawEntry> unfinished;
if (entries.size() > 4)
unfinished.reserve(entries.size() >> 1);
for (auto ei : entries)
{
if (ei.start > ei.end)
{
unfinished.emplace_back(ei);
continue;
}
Stat& s = stats[ei.tree];
++s.numVisits;
s.totalTime += ei.end - ei.start;
}
std::swap(entries, unfinished);
}
#ifndef DISABLE_IPROF_MULTITHREAD
void addThisThreadEntriesToAllThreadStats()
{
std::lock_guard<std::mutex> bouncer(allThreadStatLock);
iprof_thread_local static Stats lastStats;
for (auto& s : lastStats)
allThreadStats[s.first] -= s.second;
for (auto& s : stats)
allThreadStats[s.first] += s.second;
lastStats = stats;
}
#endif
}
std::ostream& operator<<(std::ostream& os, const InternalProfiler::Stats& stats)
{
for (auto si : stats)
{
for (auto& ti : si.first)
os << ti << (&ti != &si.first.back() ? "/" : "");
if (si.first.capacity() < si.first.size())
os << "/...(" << si.first.size() - si.first.capacity() << ")";
os << ": " << MICRO_SECS(si.second.totalTime) / float(si.second.numVisits)
<< " (" << MICRO_SECS(si.second.totalTime) << " / " << si.second.numVisits << ")\n";
}
return os;
}