Caffe2 - C++ API
A deep learning, cross platform ML framework
stats.cc
1 
17 #include "caffe2/core/stats.h"
18 
19 #include <condition_variable>
20 #include <thread>
21 
22 namespace caffe2 {
23 
24 ExportedStatMap toMap(const ExportedStatList& stats) {
25  ExportedStatMap statMap;
26  for (const auto& stat : stats) {
27  // allow for multiple instances of a key
28  statMap[stat.key] += stat.value;
29  }
30  return statMap;
31 }
32 
33 StatValue* StatRegistry::add(const std::string& name) {
34  std::lock_guard<std::mutex> lg(mutex_);
35  auto it = stats_.find(name);
36  if (it != stats_.end()) {
37  return it->second.get();
38  }
39  auto v = std::unique_ptr<StatValue>(new StatValue);
40  auto value = v.get();
41  stats_.insert(std::make_pair(name, std::move(v)));
42  return value;
43 }
44 
45 void StatRegistry::publish(ExportedStatList& exported, bool reset) {
46  std::lock_guard<std::mutex> lg(mutex_);
47  exported.resize(stats_.size());
48  int i = 0;
49  for (const auto& kv : stats_) {
50  auto& out = exported.at(i++);
51  out.key = kv.first;
52  out.value = reset ? kv.second->reset() : kv.second->get();
53  out.ts = std::chrono::high_resolution_clock::now();
54  }
55 }
56 
58  for (const auto& stat : data) {
59  add(stat.key)->increment(stat.value);
60  }
61 }
62 
63 StatRegistry::~StatRegistry() {}
64 
66  static StatRegistry r;
67  return r;
68 }
69 }
void publish(ExportedStatList &exported, bool reset=false)
Populate an ExportedStatList with current counter values.
Definition: stats.cc:45
StatValue * add(const std::string &name)
Add a new counter with given name.
Definition: stats.cc:33
Copyright (c) 2016-present, Facebook, Inc.
std::vector< ExportedStatValue > ExportedStatList
Holds names and values of counters exported from a StatRegistry.
Definition: stats.h:56
static StatRegistry & get()
Retrieve the singleton StatRegistry, which gets populated through the CAFFE_EVENT macro...
Definition: stats.cc:65
Holds a map of atomic counters keyed by name.
Definition: stats.h:133
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:57