forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.cpp
72 lines (60 loc) · 1.88 KB
/
logging.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
63
64
65
66
67
68
69
70
71
72
#include <torch/csrc/jit/runtime/logging.h>
#include <atomic>
#include <mutex>
#include <unordered_map>
namespace torch {
namespace jit {
namespace logging {
// TODO: multi-scale histogram for this thing
void LockingLogger::addStatValue(const std::string& stat_name, int64_t val) {
std::unique_lock<std::mutex> lk(m);
auto& raw_counter = raw_counters[stat_name];
raw_counter.sum += val;
raw_counter.count++;
}
int64_t LockingLogger::getCounterValue(const std::string& name) const {
std::unique_lock<std::mutex> lk(m);
if (!raw_counters.count(name)) {
return 0;
}
AggregationType type =
agg_types.count(name) ? agg_types.at(name) : AggregationType::SUM;
const auto& raw_counter = raw_counters.at(name);
switch (type) {
case AggregationType::SUM: {
return raw_counter.sum;
} break;
case AggregationType::AVG: {
return raw_counter.sum / raw_counter.count;
} break;
}
throw std::runtime_error("Unknown aggregation type!");
}
void LockingLogger::setAggregationType(
const std::string& stat_name,
AggregationType type) {
agg_types[stat_name] = type;
}
std::atomic<LoggerBase*> global_logger{new NoopLogger()};
LoggerBase* getLogger() {
return global_logger.load();
}
LoggerBase* setLogger(LoggerBase* logger) {
LoggerBase* previous = global_logger.load();
while (!global_logger.compare_exchange_strong(previous, logger)) {
previous = global_logger.load();
}
return previous;
}
JITTimePoint timePoint() {
return JITTimePoint{std::chrono::high_resolution_clock::now()};
}
void recordDurationSince(const std::string& name, const JITTimePoint& tp) {
auto end = std::chrono::high_resolution_clock::now();
// Measurement in microseconds.
auto seconds = std::chrono::duration<double>(end - tp.point).count() * 1e9;
logging::getLogger()->addStatValue(name, seconds);
}
} // namespace logging
} // namespace jit
} // namespace torch