151 lines
4.4 KiB
C++
151 lines
4.4 KiB
C++
#include "run_md.h"
|
|
#include "lib/md/relay/RelayRecorder.h"
|
|
#include "lib/md/PlatformLoader.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/MarketData.pb.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/md/get_symbols.h"
|
|
#include "lib/md/UserSymbol.h"
|
|
#include "lib/md/Diag/ShmDumper.h"
|
|
#include "platforms/mdp3_onixs/MDP3Main.h"
|
|
#include <boost/filesystem.hpp>
|
|
#include <iostream>
|
|
#include <thread>
|
|
#include <set>
|
|
|
|
using namespace bts;
|
|
using namespace std;
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
void clean_dir(const std::string base_path) {
|
|
LOG(md, Info, "Cleaning shared memory");
|
|
namespace fs = boost::filesystem;
|
|
const fs::path p(base_path);
|
|
auto file_prefix = "bts_edge_" + config::get<PublisherNamespace>().shared_memory_namespace() + "_";
|
|
if (fs::is_directory(p)) {
|
|
fs::directory_iterator end_iter;
|
|
for (fs::directory_iterator dir_iter(p); dir_iter != end_iter; ++dir_iter) {
|
|
if (fs::is_regular_file(dir_iter->status())) {
|
|
auto name = dir_iter->path().filename().string();
|
|
if (name.find(file_prefix) != std::string::npos) {
|
|
boost::system::error_code err;
|
|
fs::remove(dir_iter->path(), err);
|
|
if (!err) LOG(md, Info, "Cleaned up {}", dir_iter->path());
|
|
}
|
|
}
|
|
else if (fs::is_directory(dir_iter->status())) {
|
|
std::string path = dir_iter->path().string();
|
|
clean_dir(path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void run_replay() {
|
|
auto naggle_ms = config::get<RelayOptions>().naggle_ms();
|
|
auto file = "http://mdreplay.bluetradesys.local/spark/futures.coconut";
|
|
replay_recording(file, naggle_ms);
|
|
}
|
|
|
|
void run_onixs() {
|
|
// get all glbx symbols
|
|
std::vector<UserSymbol> symbols;
|
|
get_system_md_symbols(api::VENUE_GLBX, api::SECURITYTYPE_UNSET, &symbols);
|
|
MDP3Main main;
|
|
main.run(symbols);
|
|
}
|
|
|
|
void run_cleanup() {
|
|
clean_dir("/dev/shm");
|
|
}
|
|
|
|
std::thread* _md_thread;
|
|
void run_market_datapublishers(bool should_return) {
|
|
bts_assert(_md_thread == nullptr);
|
|
// start a thread for the md
|
|
_md_thread = new std::thread([]() {
|
|
os::set_thread_name("md publisher");
|
|
run_cleanup();
|
|
|
|
if (config::get<MarketDataConfig>().use_replay()) {
|
|
run_replay();
|
|
}
|
|
else {
|
|
run_onixs();
|
|
}
|
|
});
|
|
|
|
if(should_return) {
|
|
std::this_thread::sleep_for(2s);
|
|
}
|
|
else {
|
|
_md_thread->join();
|
|
}
|
|
}
|
|
|
|
void run_record(const std::string& arg){
|
|
boost::filesystem::path p(arg);
|
|
record_everything(p.parent_path().string(), p.filename().string());
|
|
}
|
|
|
|
|
|
void run_dump(const std::string& name, const std::string& hotcold) {
|
|
if (name.empty()) return;
|
|
string temp = name;
|
|
if (name.find("bts_edge_") == string::npos) temp = "bts_edge_" + config::get<PublisherNamespace>().shared_memory_namespace() + "_" + name;
|
|
ShmDumper dumper(temp);
|
|
if (hotcold.empty() || hotcold == "cold" || hotcold == "c") {
|
|
dumper.dump_cold();
|
|
}
|
|
else {
|
|
dumper.dump_hot();
|
|
}
|
|
}
|
|
|
|
|
|
void run_rectool(const vector<string> args) {
|
|
if (args.size() < 2) {
|
|
cout << "rectool use: " << endl;
|
|
cout << "\tbtscmd md rectool print <filepath>" << endl;
|
|
cout << "\tbtscmd md rectool unprint <input> <output>" << endl;
|
|
cout << "\tbtscmd md rectool trace <filepath> <security_key>" << endl;
|
|
return;
|
|
}
|
|
if (args[0] == "print") {
|
|
print_recording(args[1]);
|
|
} else
|
|
if (args[0] == "unprint") {
|
|
unprint_recording(args[1], args[2]);
|
|
} else
|
|
if (args[0] == "trace") {
|
|
trace_recording(args[1], atoi(args[2].c_str()));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
string help() {
|
|
return "Coconut 2.0 market data tools:\n" ;
|
|
}
|
|
void printhelp() {
|
|
cout << help() << endl;
|
|
}
|
|
|
|
void md_tool(std::vector<std::string> const &args) {
|
|
const string& cmd = args.empty() ? "" : args[0];
|
|
if (args.empty()) printhelp();
|
|
else if (cmd == "record") run_record(args.size() > 1 ? args[1] : "");
|
|
else if (cmd == "rectool") run_rectool(vector<string>(args.begin()+1, args.end()));
|
|
else if (cmd == "cleanup") run_cleanup();
|
|
else if (cmd == "dump") run_dump(args.size() > 1 ? args[1] : "", args.size() > 2 ? args[2] : "");
|
|
else if (cmd == "replay") run_replay();
|
|
else if (cmd == "run") run_market_datapublishers(false);
|
|
else if (cmd == "relay") relay_everything();
|
|
else printhelp();
|
|
}
|
|
|
|
}}//namespace
|