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);
210 std::atomic<int64_t> first_{std::numeric_limits<int64_t>::min()};
211 int64_t const_min_{std::numeric_limits<int64_t>::min()};
216 count_(gn, n +
"/count"),
217 sumsqoffset_(gn, n +
"/sumsqoffset"),
218 sumoffset_(gn, n +
"/sumoffset") {}
220 int64_t increment(int64_t value = 1) {
221 first_.compare_exchange_strong(const_min_, value);
222 int64_t offset_value = first_.load();
223 int64_t orig_value = value;
224 value -= offset_value;
226 sumsqoffset_.increment(value * value);
227 sumoffset_.increment(value);
228 return ExportedStat::increment(orig_value);
231 template <
typename T,
typename Unused1,
typename... Unused>
232 int64_t increment(
T value, Unused1, Unused...) {
233 return increment(value);
239 std::vector<ExportedStat> details_;
245 void setDetails(
const std::vector<std::string>& detailNames) {
247 for (
const auto& detailName : detailNames) {
248 details_.emplace_back(groupName, name +
"/" + detailName);
252 template <
typename T,
typename... Unused>
253 int64_t increment(
T value,
size_t detailIndex, Unused...) {
254 if (detailIndex < details_.size()) {
255 details_[detailIndex].increment(value);
257 return ExportedStat::increment(value);
266 StaticStat(
const std::string& groupName,
const std::string& name)
267 :
Stat(groupName, name),
270 int64_t increment(int64_t value = 1) {
271 return value_->reset(value);
274 template <
typename T,
typename Unused1,
typename... Unused>
275 int64_t increment(
T value, Unused1, Unused...) {
276 return increment(value);
285 std::chrono::high_resolution_clock::time_point start_;
288 : f_(f), start_(std::chrono::high_resolution_clock::now()) {}
290 using namespace std::chrono;
291 auto duration = high_resolution_clock::now() - start_;
292 int64_t nanos = duration_cast<nanoseconds>(duration).count();
309 #define CAFFE_STAT_CTOR(ClassName) \ 310 ClassName(std::string name) : groupName(name) {} \ 311 std::string groupName 313 #define CAFFE_EXPORTED_STAT(name) \ 314 ExportedStat name { \ 318 #define CAFFE_AVG_EXPORTED_STAT(name) \ 319 AvgExportedStat name { \ 323 #define CAFFE_STDDEV_EXPORTED_STAT(name) \ 324 StdDevExportedStat name { \ 328 #define CAFFE_DETAILED_EXPORTED_STAT(name) \ 329 DetailedExportedStat name { \ 333 #define CAFFE_STAT(name) \ 338 #define CAFFE_STATIC_STAT(name) \ 343 #define CAFFE_EVENT(stats, field, ...) \ 345 auto __caffe_event_value_ = stats.field.increment(__VA_ARGS__); \ 348 stats.field.groupName.c_str(), \ 349 __caffe_event_value_, \ 353 #define CAFFE_DURATION(stats, field, ...) \ 354 if (auto g = ::caffe2::detail::ScopeGuard([&](int64_t nanos) { \ 355 CAFFE_EVENT(stats, field, nanos, ##__VA_ARGS__); \
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
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...
Holds a map of atomic counters keyed by name.