Skip to content

Commit

Permalink
test with shm
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu1 committed Mar 18, 2020
1 parent 4392aec commit 80524be
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 580 deletions.
3 changes: 2 additions & 1 deletion external/common/include/core/base_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "struct/market_snapshot.h"
#include "struct/order.h"
#include "util/sender.hpp"
#include "util/shm_sender.hpp"
#include "util/dater.h"
#include "define.h"
#include "struct/command.h"
Expand Down Expand Up @@ -67,7 +68,7 @@ class BaseStrategy {
void UpdatePos(Order* o, const ExchangeInfo& info);

bool position_ready;
Sender<Order>* order_sender;
ShmSender<Order>* order_sender;
Sender<MarketSnapshot>* ui_sender;
unordered_map<std::string, MarketSnapshot> shot_map;
unordered_map<std::string, Order*> order_map;
Expand Down
11 changes: 10 additions & 1 deletion external/common/include/util/shm_sender.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#ifndef SHM_SENDER_HPP_
#define SHM_SENDER_HPP_

#include <mutex>
#include "shm_worker.hpp"

template <typename T>
class ShmSender: public ShmWorker {
public:
ShmSender(const std::string& key, int size = 100000) {
ShmSender(const std::string& key, int size = 100000, const std::string& file_name = "")
: f(file_name.empty() ? nullptr : new std::ofstream(file_name.c_str(), ios::out | ios::binary)) {
init <T> (key, size);
mutex = reinterpret_cast<pthread_mutex_t*>(m_data + 3*sizeof(atomic_int));
}
Expand All @@ -21,9 +23,16 @@ class ShmSender: public ShmWorker {
memcpy(m_data+header_size+(tail->load()%m_size)*sizeof(T), &shot, sizeof(T));
tail->fetch_add(1);
pthread_mutex_unlock(mutex);
if (f.get()) {
std::lock_guard<std::mutex> lck(mtx); // for mutli-thread backtest file writting
f.get()->write((char*)&shot, sizeof(T));
f.get()->flush();
}
}
private:
pthread_mutex_t* mutex;
std::mutex mtx;
unique_ptr<std::ofstream> f;
};

#endif // SHM_SENDER_HPP_
Binary file modified external/common/lib/libnick.so
Binary file not shown.
5 changes: 4 additions & 1 deletion src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#include "util/time_controller.h"
#include "util/sender.hpp"
#include "util/recver.hpp"
#include "util/shm_sender.hpp"
#include "util/shm_recver.hpp"
#include "util/dater.h"
#include "util/history_worker.h"
#include "struct/market_snapshot.h"
#include "./strategy.h"

std::unique_ptr<Sender<MarketSnapshot> > ui_sender(new Sender<MarketSnapshot>("*:33333", "bind", "tcp", "mid.dat"));
std::unique_ptr<Sender<Order> > order_sender(new Sender<Order>("order_sub", "connect", "ipc", "order.dat"));
// std::unique_ptr<Sender<Order> > order_sender(new Sender<Order>("order_sub", "connect", "ipc", "order.dat"));
std::unique_ptr<ShmSender<Order> > order_sender(new ShmSender<Order>("order_sub", 100000, "order.dat"));
std::pair< std::unordered_map<std::string, std::vector<BaseStrategy*> >, std::vector<std::string> > GenTSM() {
std::string default_path = GetDefaultPath();

Expand Down
Loading

0 comments on commit 80524be

Please sign in to comment.