Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit dd7f026

Browse files
committed
Drop folly use tbb instead
1 parent dc87cda commit dd7f026

File tree

10 files changed

+75
-38
lines changed

10 files changed

+75
-38
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
build
2-
builder
2+
core
3+
Debug
34

45
# Prerequisites
56
*.d

CMakeLists.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ option(ENABLE_BENCHMARKS "Build benchmarks" YES)
2626

2727
# Compiler configuration
2828
set(CMAKE_CXX_STANDARD 17)
29-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -Wdeprecated -Wextra -Wno-switch-enum -Wno-float-equal -Wno-unused-parameter -Wno-unused-member-function -Werror -fno-omit-frame-pointer -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -lpthread -lm")
30-
31-
#set(CMAKE_BUILD_TYPE Release)
29+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -Wdeprecated -Wextra -Wno-switch-enum -Wno-float-equal -Wno-unused-parameter -Wno-unused-member-function -Werror -fno-omit-frame-pointer -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -lpthread -lm -march=native")
3230

3331
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
3432
set(CMAKE_HAVE_THREADS_LIBRARY TRUE)

Matching/CMakeLists.txt

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ target_sources(matching INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
44
target_link_libraries(
55
matching
66
INTERFACE
7-
CONAN_PKG::jemalloc
8-
CONAN_PKG::boost
9-
CONAN_PKG::spdlog
10-
CONAN_PKG::tbb
11-
CONAN_PKG::folly
12-
CONAN_PKG::nlohmann_json
7+
CONAN_PKG::jemalloc
8+
CONAN_PKG::boost
9+
CONAN_PKG::spdlog
10+
CONAN_PKG::tbb
11+
CONAN_PKG::nlohmann_json
1312
)
1413

1514
target_include_directories(
1615
matching
1716
INTERFACE
18-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
19-
$<INSTALL_INTERFACE:include/matching>
17+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
18+
$<INSTALL_INTERFACE:include/matching>
2019
)

Matching/src/order_router.hpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
#include <orderbook.hpp>
44
#include "influxdb.hpp"
55
#include "spdlog/spdlog.h"
6-
#include <future>
76
#include <sys/types.h>
87
#include <unistd.h>
98
#include <pthread.h>
109
#include <map>
11-
#include <folly/concurrency/UnboundedQueue.h>
1210
#include <boost/asio.hpp>
1311
#include <boost/asio/thread_pool.hpp>
14-
//#include <boost/lockfree/queue.hpp>
12+
#include <tbb/concurrent_queue.h>
1513

1614
namespace matching_engine
1715
{
@@ -32,21 +30,22 @@ class consumer
3230
}
3331
void push(OrderPtr order)
3432
{
35-
queue_.enqueue(std::move(order));
33+
queue_.push(std::move(order));
3634
}
3735
void register_market(std::string_view market)
3836
{
3937
markets.emplace(market, market);
4038
}
4139
void listen()
4240
{
43-
for (const auto& [name, _] : markets) {
44-
console_->info("Consumer of {} started @{}", name, (pid_t) syscall (SYS_gettid));
45-
}
41+
if (console_ != nullptr)
42+
for (const auto& [name, _] : markets) {
43+
console_->info("Consumer of {} started @{}", name, (pid_t) syscall (SYS_gettid));
44+
}
4645
OrderPtr order;
4746
auto last_log = Time::now();
4847
while(should_consume_()) {
49-
queue_.dequeue(order);
48+
queue_.pop(order);
5049
auto &ob = markets.at(order->market_name());
5150
const auto start = Time::now();
5251
ob.match(std::move(order));
@@ -66,6 +65,7 @@ class consumer
6665
});
6766
last_log = start;
6867
}
68+
6969
}
7070
}
7171
private:
@@ -74,8 +74,7 @@ class consumer
7474
return !should_exit_ or !queue_.empty();
7575
}
7676
std::unordered_map<std::string_view, OrderBook> markets;
77-
folly::UnboundedQueue<OrderPtr, true, true, true, 16> queue_;
78-
//boost::lockfree::queue<OrderPtr> queue_;
77+
tbb::concurrent_bounded_queue<OrderPtr> queue_;
7978
std::atomic_bool should_exit_;
8079
std::shared_ptr<spdlog::logger> console_;
8180
};
@@ -85,8 +84,8 @@ class dispatcher
8584
public:
8685
dispatcher() = default;
8786
dispatcher(const dispatcher&) = delete;
88-
dispatcher(std::shared_ptr<spdlog::logger> console,
89-
std::vector<std::string_view> markets,
87+
dispatcher(std::vector<std::string_view> markets,
88+
std::shared_ptr<spdlog::logger> console = nullptr,
9089
const uint64_t available_cores = std::max(1u, std::thread::hardware_concurrency() - 1)):
9190
pool_{available_cores},
9291
console_{console}
@@ -121,10 +120,17 @@ class dispatcher
121120
auto& market_consumer = market_registry_.at(order->market_name());
122121
market_consumer->push(std::move(order));
123122
}
124-
std::string_view registered_market_name(std::string_view market) const
123+
std::string_view registered_market_name(const std::string_view market) const
125124
{
126125
return market_registry_.find(market)->first;
127126
}
127+
void shutdown()
128+
{
129+
for (auto const& [_, c] : market_registry_) {
130+
c->shutdown();
131+
}
132+
pool_.join();
133+
}
128134
private:
129135
//tbb::concurrent_unordered_map<std::string, std::shared_ptr<consumer>> market_registry_;
130136
std::unordered_map<std::string_view, std::shared_ptr<consumer>> market_registry_;

Matching/src/orderbook.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Order
9292
bool operator==(const Order &rhs) const;
9393
};
9494

95-
using OrderPtr = std::unique_ptr<Order>;
95+
using OrderPtr = std::shared_ptr<Order>;
9696

9797
using queue_allocator = boost::container::allocator<OrderPtr>;
9898
using order_queue_type = boost::container::deque<OrderPtr, queue_allocator>;

Service/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ add_executable(
99
target_link_libraries(
1010
matching_service
1111
PRIVATE
12-
matching
13-
CONAN_PKG::jemalloc
12+
matching
13+
CONAN_PKG::jemalloc
1414
)
1515

1616
set_target_properties(service PROPERTIES LINKER_LANGUAGE CXX)

Service/src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, char *argv[])
4141
u8"EUR_AUD",
4242
u8"GBP_JPY", u8"USD_JPY"
4343
};
44-
auto dispatcher = std::make_shared<me::router::dispatcher>(console, markets);
44+
auto dispatcher = std::make_shared<me::router::dispatcher>(markets, console);
4545

4646
/* Initialise TCP transport layer */
4747
boost::asio::io_context ioc{(int)std::thread::hardware_concurrency()};

Testing/Benchmark/main.cpp

+38-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ static void OrderCreation(benchmark::State& state)
2323
Price price = rand() % 100 + 1;
2424
Quantity quantity = double(rand() % 100 + 1) / (rand() % 20 + 1);
2525
for (auto _ : state) {
26+
auto start = std::chrono::high_resolution_clock::now();
2627
auto order = std::make_unique<Order>(market, side, price, quantity);
28+
auto end = std::chrono::high_resolution_clock::now();
29+
auto elapsed_seconds =
30+
std::chrono::duration_cast<std::chrono::duration<double>>(
31+
end - start);
32+
state.SetIterationTime(elapsed_seconds.count());
2733
}
34+
state.SetComplexityN(state.range(0));
2835
}
29-
BENCHMARK(OrderCreation)->DenseRange(0, 1000, 250);
36+
BENCHMARK(OrderCreation)->DenseRange(1, 1000, 250)->UseManualTime()->Complexity(benchmark::oN);
3037

3138
static void OrderMatching(benchmark::State& state)
3239
{
@@ -39,11 +46,38 @@ static void OrderMatching(benchmark::State& state)
3946
auto side = rand() % 2 ? SIDE::BUY : SIDE::SELL;
4047
auto quantity = double(rand() % 10 + 1) / (rand() % 20 + 1);
4148
auto order = std::make_unique<Order>(market, side, price, quantity);
42-
//dispatcher->send(std::move(order));
49+
auto start = std::chrono::high_resolution_clock::now();
4350
ob.match(std::move(order));
51+
auto end = std::chrono::high_resolution_clock::now();
52+
auto elapsed_seconds =
53+
std::chrono::duration_cast<std::chrono::duration<double>>(
54+
end - start);
55+
state.SetIterationTime(elapsed_seconds.count());
4456
}
4557
}
58+
state.SetComplexityN(state.range(0));
4659
}
47-
BENCHMARK(OrderMatching)->DenseRange(0, 1000, 250);
48-
// Run the benchmark
60+
BENCHMARK(OrderMatching)->DenseRange(1, 1000, 250)->UseManualTime()->Complexity(benchmark::oLogN);
61+
62+
// TODO Starts and hangs, issue with thread pool. Fix it.
63+
static void OrderDispatching(benchmark::State& state)
64+
{
65+
// Perform setup here
66+
const std::vector<std::string_view> markets = {u8"USD_JPY"};
67+
auto dispatcher = std::make_shared<router::dispatcher>(markets);
68+
auto prices = SimulateMarket(state.range(0));
69+
for(auto _ : state) {
70+
for (auto price : prices) {
71+
auto side = rand() % 2 ? SIDE::BUY : SIDE::SELL;
72+
auto quantity = double(rand() % 10 + 1) / (rand() % 20 + 1);
73+
auto order = std::make_unique<Order>(markets[0], side, price, quantity);
74+
dispatcher->send(std::move(order));
75+
}
76+
}
77+
dispatcher->shutdown();
78+
}
79+
80+
//BENCHMARK(OrderDispatching)->DenseRange(0, 1000, 250)->MeasureProcessCPUTime();
81+
82+
/* Run the benchmark */
4983
BENCHMARK_MAIN();

Testing/UnitTest/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ add_executable(
66
target_link_libraries(
77
unit_tests
88
PRIVATE
9-
matching
10-
Threads::Threads
11-
CONAN_PKG::jemalloc
12-
CONAN_PKG::gtest
9+
matching
10+
Threads::Threads
11+
CONAN_PKG::jemalloc
12+
CONAN_PKG::gtest
1313
)
1414

conanfile.txt

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ spdlog/1.5.0
66
nlohmann_json/3.7.3
77
jemalloc/5.2.1
88
tbb/2020.0
9-
folly/2018.11.12.00@bincrafters/stable
109

1110
[generators]
1211
txt

0 commit comments

Comments
 (0)