-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.cpp
More file actions
78 lines (65 loc) · 2.09 KB
/
profiler.cpp
File metadata and controls
78 lines (65 loc) · 2.09 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
/*
* File: profiler.h
* Author: sonnyit
*
* Created on January 18, 2017, 10:07 AM
*/
#include <stdexcept>
#include "profiler.h"
using namespace std;
/*-----------------------------------------------------------------------------
* Task class
*----------------------------------------------------------------------------*/
Task::Task(const std::string& name) : _name(name) {
_time_point_start = std::chrono::steady_clock::now();
}
Task::~Task() {
Profiler::getInstance().addProfiler(_name, chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - _time_point_start).count());
}
/*-----------------------------------------------------------------------------
* Profiler class
*----------------------------------------------------------------------------*/
bool Profiler::_destroyed = false;
Profiler::Profiler() {
}
Profiler::~Profiler() {
_destroyed = true;
}
Profiler& Profiler::getInstance() {
static Profiler instance;
if (_destroyed) {
throw std::runtime_error("Profiler: Dead reference access");
}
return instance;
}
void Profiler::reset(const std::string& task_name) {
lock_guard<mutex> lock(_mutex);
if (_map_table.find(task_name) != _map_table.end()) { /* found -> reset task */
ProfilerTask task;
_map_table[task_name] = task;
}
}
void Profiler::addProfiler(const std::string& task_name, long ms) {
lock_guard<mutex> lock(_mutex);
if (_map_table.find(task_name) != _map_table.end()) {
++_map_table[task_name].count;
_map_table[task_name].time_pass += ms;
_map_table[task_name].last_time_pass = ms;
} else {
ProfilerTask task;
++task.count;
task.time_pass += ms;
task.last_time_pass = ms;
_map_table[task_name] = task;
}
}
const std::map<std::string, ProfilerTask>& Profiler::getAll() const {
return _map_table;
}
ProfilerTask Profiler::get(const std::string& task_name) {
lock_guard<mutex> lock(_mutex);
if (_map_table.find(task_name) != _map_table.end()) {
return _map_table[task_name];
}
return ProfilerTask();
}