diff --git a/src/lib/config/protos/toggles.proto b/src/lib/config/protos/toggles.proto index 5e1fbd736..3aeedd91b 100644 --- a/src/lib/config/protos/toggles.proto +++ b/src/lib/config/protos/toggles.proto @@ -34,4 +34,8 @@ message SoundNotifications { message VolumeAtPrice{ optional bool enabled = 1 [default = true]; +} + +message MBOStats { + optional bool enabled = 1 [default = true]; } \ No newline at end of file diff --git a/src/lib/md/MBOProcessor.cpp b/src/lib/md/MBOProcessor.cpp index 4f89023a6..4d5f2a33d 100644 --- a/src/lib/md/MBOProcessor.cpp +++ b/src/lib/md/MBOProcessor.cpp @@ -1,9 +1,13 @@ #include "MBOProcessor.h" #include "lib/utils/Dispatcher.h" #include "lib/utils/ConcurrentPool.h" +#include "lib/config/config.h" +#include "lib/config/protos/toggles.pb.h" #include #include +#include +#include #include @@ -20,11 +24,50 @@ struct SecurityKeyHashCompare { } }; +void print_pool_stats(const utils::ConcurrentPool& pool) { + size_t count = 0; + std::vector bid_sz,ask_sz; + pool.unsafe_iterate([&](const MBOSnapshot& s) { + if (s.bids.size() > 0 || s.asks.size() > 0) { + count++; + bid_sz.push_back(s.bids.size()); + ask_sz.push_back(s.asks.size()); + } + }); + + std::sort(bid_sz.begin(), bid_sz.end()); + std::sort(ask_sz.begin(), ask_sz.end()); + size_t bid_tot =0, ask_tot = 0; + for(size_t i : bid_sz) { bid_tot += i; } + for(size_t i : ask_sz) { ask_tot += i; } + + std::stringstream bid_percentiles, ask_percentiles; + for (double x : { 0.5, 0.75, 0.9, 0.95, 0.99 }) { + size_t index = count * x; + bid_percentiles << bid_sz[index] << " "; + ask_percentiles << ask_sz[index] << " "; + } + bid_percentiles << bid_sz[count-1] << " "; + ask_percentiles << ask_sz[count-1] << " "; + LOG(md,Info, + "MBO statistics: count: {} - %[50,75,90,95,99,100] bid: [{}] - ask: [{}]", + count, bid_percentiles.str(), ask_percentiles.str()); +} + typedef tbb::concurrent_hash_map MapType; struct MBOProcessorImpl { utils::ConcurrentPool pool; MapType snapshots; - MBOProcessorImpl() : pool(2048,512), snapshots() {} + utils::DispatcherTimer timer; + MBOProcessorImpl() : + pool(2048,512), + snapshots(), + timer(utils::Dispatcher::background(), 600*1000, [&]() { print_pool_stats(pool); }) + { + if (config::get().enabled()) { + timer.start(); + } + } }; // diff --git a/src/lib/utils/ConcurrentPool.h b/src/lib/utils/ConcurrentPool.h index 91ad99095..92d86e7ea 100644 --- a/src/lib/utils/ConcurrentPool.h +++ b/src/lib/utils/ConcurrentPool.h @@ -10,11 +10,12 @@ template class ConcurrentPool { tbb::concurrent_vector> _all; tbb::concurrent_queue _free; + size_t _initial_size; size_t _grow_size; - std::atomic _outstanding; + size_t _outstanding; public: ConcurrentPool(size_t initial_size=256, size_t grow_size=32): - _all(), _free(), _grow_size(grow_size), _outstanding(0) { + _all(), _free(), _initial_size(initial_size), _grow_size(grow_size), _outstanding(0) { reserve(initial_size); } @@ -34,6 +35,7 @@ class ConcurrentPool { void reserve(size_t count) { std::unique_ptr buffer {new T[count]}; + _outstanding += count; for (size_t x = 0; x < count; ++x) { T* temp = &buffer[x]; release(&temp); @@ -50,6 +52,18 @@ class ConcurrentPool { } } + template + void unsafe_iterate(CB cb) const { + for (auto iter = _all.begin(); iter != _all.end(); ++iter) { + size_t size = (iter == _all.begin()) ? _initial_size : _grow_size; + for (size_t x = 0; x < size; ++x) { + const std::unique_ptr& bucket = *iter; + const T* start = bucket.get(); + cb(start[x]); + } + } + } + }; }} //namespace