Caffe2 - C++ API
A deep learning, cross platform ML framework
stats.cc
1 #include "caffe2/core/stats.h"
2 
3 #include <condition_variable>
4 #include <thread>
5 
6 namespace caffe2 {
7 
8 ExportedStatMap toMap(const ExportedStatList& stats) {
9  ExportedStatMap statMap;
10  for (const auto& stat : stats) {
11  // allow for multiple instances of a key
12  statMap[stat.key] += stat.value;
13  }
14  return statMap;
15 }
16 
17 StatValue* StatRegistry::add(const std::string& name) {
18  std::lock_guard<std::mutex> lg(mutex_);
19  auto it = stats_.find(name);
20  if (it != stats_.end()) {
21  return it->second.get();
22  }
23  auto v = std::unique_ptr<StatValue>(new StatValue);
24  auto value = v.get();
25  stats_.insert(std::make_pair(name, std::move(v)));
26  return value;
27 }
28 
29 void StatRegistry::publish(ExportedStatList& exported, bool reset) {
30  std::lock_guard<std::mutex> lg(mutex_);
31  exported.resize(stats_.size());
32  int i = 0;
33  for (const auto& kv : stats_) {
34  auto& out = exported.at(i++);
35  out.key = kv.first;
36  out.value = reset ? kv.second->reset() : kv.second->get();
37  out.ts = std::chrono::high_resolution_clock::now();
38  }
39 }
40 
42  for (const auto& stat : data) {
43  add(stat.key)->increment(stat.value);
44  }
45 }
46 
47 StatRegistry::~StatRegistry() {}
48 
50  static StatRegistry r;
51  return r;
52 }
53 }
void publish(ExportedStatList &exported, bool reset=false)
Populate an ExportedStatList with current counter values.
Definition: stats.cc:29
void update(const ExportedStatList &data)
Update values of counters contained in the given ExportedStatList to the values provided, creating counters that don&#39;t exist.
Definition: stats.cc:41
Holds a map of atomic counters keyed by name.
Definition: stats.h:117
StatValue * add(const std::string &name)
Add a new counter with given name.
Definition: stats.cc:17
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
std::vector< ExportedStatValue > ExportedStatList
Holds names and values of counters exported from a StatRegistry.
Definition: stats.h:40
static StatRegistry & get()
Retrieve the singleton StatRegistry, which gets populated through the CAFFE_EVENT macro...
Definition: stats.cc:49