Holds a map of atomic counters keyed by name. More...
#include <stats.h>
Public Member Functions | |
StatValue * | add (const std::string &name) |
Add a new counter with given name. More... | |
void | publish (ExportedStatList &exported, bool reset=false) |
Populate an ExportedStatList with current counter values. More... | |
ExportedStatList | publish (bool reset=false) |
void | update (const ExportedStatList &data) |
Update values of counters contained in the given ExportedStatList to the values provided, creating counters that don't exist. | |
Static Public Member Functions | |
static StatRegistry & | get () |
Retrieve the singleton StatRegistry, which gets populated through the CAFFE_EVENT macro. | |
Holds a map of atomic counters keyed by name.
The StatRegistry singleton, accessed through StatRegistry::get(), holds counters registered through the macro CAFFE_EXPORTED_STAT. Example of usage:
struct MyCaffeClass { MyCaffeClass(const std::string& instanceName): stats_(instanceName) {} void run(int numRuns) { try { CAFFE_EVENT(stats_, num_runs, numRuns); tryRun(numRuns); CAFFE_EVENT(stats_, num_successes); } catch (std::exception& e) { CAFFE_EVENT(stats_, num_failures, 1, "arg_to_usdt", e.what()); } CAFFE_EVENT(stats_, usdt_only, 1, "arg_to_usdt"); } private: struct MyStats { CAFFE_STAT_CTOR(MyStats); CAFFE_EXPORTED_STAT(num_runs); CAFFE_EXPORTED_STAT(num_successes); CAFFE_EXPORTED_STAT(num_failures); CAFFE_STAT(usdt_only); } stats_; };
int main() { MyCaffeClass a("first"); MyCaffeClass b("second"); for (int i = 0; i < 10; ++i) { a.run(10); b.run(5); } ExportedStatList finalStats; StatRegistry::get().publish(finalStats); }
For every new instance of MyCaffeClass, a new counter is created with the instance name as prefix. Everytime run() is called, the corresponding counter will be incremented by the given value, or 1 if value not provided.
Counter values can then be exported into an ExportedStatList. In the example above, considering "tryRun" never throws, finalStats
will be populated as follows:
first/num_runs 100 first/num_successes 10 first/num_failures 0 second/num_runs 50 second/num_successes 10 second/num_failures 0
The event usdt_only is not present in ExportedStatList because it is declared as CAFFE_STAT, which does not create a counter.
Additionally, for each call to CAFFE_EVENT, a USDT probe is generated. The probe will be set up with the following arguments:
It is also possible to create additional StatRegistry instances beyond the singleton. These instances are not automatically populated with CAFFE_EVENT. Instead, they can be populated from an ExportedStatList structure by calling StatRegistry::update().
StatValue * caffe2::StatRegistry::add | ( | const std::string & | name | ) |
void caffe2::StatRegistry::publish | ( | ExportedStatList & | exported, |
bool | reset = false |
||
) |