7 #include <unordered_map> 9 #include "caffe2/core/logging.h" 10 #include "caffe2/core/static_tracepoint.h" 15 std::atomic<int64_t> v_{0};
18 int64_t increment(int64_t inc) {
22 int64_t reset(int64_t value = 0) {
23 return v_.exchange(value);
34 std::chrono::time_point<std::chrono::high_resolution_clock> ts;
41 using ExportedStatMap = std::unordered_map<std::string, int64_t>;
119 std::unordered_map<std::string, std::unique_ptr<StatValue>> stats_;
143 publish(stats, reset);
157 std::string groupName;
159 Stat(
const std::string& gn,
const std::string& n) : groupName(gn), name(n) {}
161 template <
typename... Unused>
162 int64_t increment(Unused...) {
171 ExportedStat(
const std::string& gn,
const std::string& n)
174 int64_t increment(int64_t value = 1) {
175 return value_->increment(value);
178 template <
typename T,
typename Unused1,
typename... Unused>
179 int64_t increment(T value, Unused1, Unused...) {
180 return increment(value);
190 :
ExportedStat(gn, n +
"/sum"), count_(gn, n +
"/count") {}
192 int64_t increment(int64_t value = 1) {
194 return ExportedStat::increment(value);
197 template <
typename T,
typename Unused1,
typename... Unused>
198 int64_t increment(T value, Unused1, Unused...) {
199 return increment(value);
205 std::vector<ExportedStat> details_;
211 void setDetails(
const std::vector<std::string>& detailNames) {
213 for (
const auto& detailName : detailNames) {
214 details_.emplace_back(groupName, name +
"/" + detailName);
218 template <
typename T,
typename... Unused>
219 int64_t increment(T value,
size_t detailIndex, Unused...) {
220 if (detailIndex < details_.size()) {
221 details_[detailIndex].increment(value);
223 return ExportedStat::increment(value);
232 std::chrono::high_resolution_clock::time_point start_;
235 : f_(f), start_(std::chrono::high_resolution_clock::now()) {}
237 using namespace std::chrono;
238 auto duration = high_resolution_clock::now() - start_;
239 int64_t nanos = duration_cast<nanoseconds>(duration).count();
256 #define CAFFE_STAT_CTOR(ClassName) \ 257 ClassName(std::string name) : groupName(name) {} \ 258 std::string groupName 260 #define CAFFE_EXPORTED_STAT(name) \ 261 ExportedStat name { \ 265 #define CAFFE_AVG_EXPORTED_STAT(name) \ 266 AvgExportedStat name { \ 270 #define CAFFE_DETAILED_EXPORTED_STAT(name) \ 271 DetailedExportedStat name { \ 275 #define CAFFE_STAT(name) \ 280 #define CAFFE_EVENT(stats, field, ...) \ 282 auto __caffe_event_value_ = stats.field.increment(__VA_ARGS__); \ 285 stats.field.groupName.c_str(), \ 286 __caffe_event_value_, \ 290 #define CAFFE_DURATION(stats, field, ...) \ 291 if (auto g = detail::ScopeGuard([&](int64_t nanos) { \ 292 CAFFE_EVENT(stats, field, nanos, ##__VA_ARGS__); \
Holds a map of atomic counters keyed by name.
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
ScopeGuardImplBase && ScopeGuard
This is largely unneeded if you just use auto for your guards.
std::vector< ExportedStatValue > ExportedStatList
Holds names and values of counters exported from a StatRegistry.
static StatRegistry & get()
Retrieve the singleton StatRegistry, which gets populated through the CAFFE_EVENT macro...