Skip to content

Commit 3518406

Browse files
committed
A large number of performance changes (related to faasm#156)
1 parent 5034dec commit 3518406

37 files changed

+955
-297
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ add_dependencies(faabric pistache_ext spdlog_ext)
105105
target_link_libraries(faabric PUBLIC
106106
faabricmpi
107107
hiredis
108-
boost_system
109-
boost_filesystem
108+
Boost::system
109+
Boost::filesystem
110+
Boost::Boost
110111
zstd::libzstd_static
111112
${PISTACHE_LIBRARY}
112113
${PROTOBUF_LIBRARY}

cmake/ExternalProjects.cmake

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include(FindGit)
22
find_package(Git)
33
include (ExternalProject)
44
include (FetchContent)
5+
find_package (Threads REQUIRED)
56

67
# Protobuf
78
set(PROTOBUF_LIBRARY ${CMAKE_INSTALL_PREFIX}/lib/libprotobuf.so)
@@ -116,6 +117,42 @@ FetchContent_MakeAvailable(zstd_ext)
116117
target_include_directories(libzstd_static INTERFACE $<BUILD_INTERFACE:${zstd_ext_SOURCE_DIR}/lib>)
117118
add_library(zstd::libzstd_static ALIAS libzstd_static)
118119

120+
# Boost libraries, the header-only ones
121+
FetchContent_Declare(boost_ext
122+
URL "https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2"
123+
URL_HASH "SHA256=fc9f85fc030e233142908241af7a846e60630aa7388de9a5fafb1f3a26840854"
124+
)
125+
FetchContent_GetProperties(boost_ext)
126+
if(NOT boost_ext_POPULATED)
127+
FetchContent_Populate(boost_ext)
128+
endif()
129+
add_library(Boost INTERFACE)
130+
target_compile_definitions(Boost INTERFACE
131+
BOOST_BEAST_USE_STD_STRING_VIEW
132+
BOOST_ASIO_NO_DEPRECATED
133+
BOOST_ASIO_NO_TS_EXECUTORS
134+
BOOST_ASIO_NO_DEFAULT_LINKED_LIBS
135+
)
136+
target_include_directories(Boost INTERFACE ${boost_ext_SOURCE_DIR})
137+
target_link_libraries(Boost INTERFACE Threads::Threads)
138+
target_compile_features(Boost INTERFACE cxx_std_17)
139+
add_library(Boost::Boost ALIAS Boost)
140+
# Header-only aliases
141+
add_library(Boost::atomic ALIAS Boost)
142+
add_library(Boost::core ALIAS Boost)
143+
add_library(Boost::assert ALIAS Boost)
144+
add_library(Boost::config ALIAS Boost)
145+
add_library(Boost::container_hash ALIAS Boost)
146+
add_library(Boost::detail ALIAS Boost)
147+
add_library(Boost::io ALIAS Boost)
148+
add_library(Boost::iterator ALIAS Boost)
149+
add_library(Boost::smart_ptr ALIAS Boost)
150+
add_library(Boost::system ALIAS Boost)
151+
add_library(Boost::type_traits ALIAS Boost)
152+
add_library(Boost::predef ALIAS Boost)
153+
add_library(Boost::Boost ALIAS Boost)
154+
add_subdirectory(${boost_ext_SOURCE_DIR}/libs/filesystem ${boost_ext_BINARY_DIR}/libs/filesystem EXCLUDE_FROM_ALL)
155+
119156
# ZeroMQ
120157
set(ZEROMQ_LIBRARY ${CMAKE_INSTALL_PREFIX}/lib/libzmq.so)
121158
ExternalProject_Add(libzeromq_ext
@@ -129,7 +166,7 @@ ExternalProject_Get_Property(libzeromq_ext SOURCE_DIR)
129166
set(LIBZEROMQ_INCLUDE_DIR ${SOURCE_DIR})
130167
ExternalProject_Add(cppzeromq_ext
131168
GIT_REPOSITORY "https://github.com/zeromq/cppzmq.git"
132-
GIT_TAG "v4.7.1"
169+
GIT_TAG "v4.8.0"
133170
CMAKE_CACHE_ARGS "-DCPPZMQ_BUILD_TESTS:BOOL=OFF"
134171
"-DCMAKE_INSTALL_PREFIX:STRING=${CMAKE_INSTALL_PREFIX}"
135172
)

examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function(add_example example_name)
2121
${FAABRIC_LIB_DIR}/libprotobuf.so
2222
${FAABRIC_LIB_DIR}/libpistache.so
2323
${FAABRIC_LIB_DIR}/libzmq.so
24-
boost_system
25-
boost_filesystem
24+
Boost::system
25+
Boost::filesystem
2626
hiredis
2727
pthread
2828
)

examples/server.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#include <faabric/endpoint/FaabricEndpoint.h>
1+
#include <faabric/endpoint/Endpoint.h>
2+
#include <faabric/endpoint/FaabricEndpointHandler.h>
23
#include <faabric/runner/FaabricMain.h>
34
#include <faabric/scheduler/ExecutorFactory.h>
45
#include <faabric/transport/context.h>
6+
#include <faabric/util/config.h>
57
#include <faabric/util/logging.h>
68

79
using namespace faabric::scheduler;
@@ -50,7 +52,11 @@ int main()
5052

5153
// Start endpoint (will also have multiple threads)
5254
SPDLOG_INFO("Starting endpoint");
53-
faabric::endpoint::FaabricEndpoint endpoint;
55+
const auto& config = faabric::util::getSystemConfig();
56+
faabric::endpoint::Endpoint endpoint(
57+
config.endpointPort,
58+
config.endpointNumThreads,
59+
std::make_shared<faabric::endpoint::FaabricEndpointHandler>());
5460
endpoint.start();
5561

5662
SPDLOG_INFO("Shutting down endpoint");

include/faabric/endpoint/Endpoint.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
#pragma once
22

3+
#include <functional>
4+
#include <memory>
5+
36
#include <faabric/proto/faabric.pb.h>
7+
#include <faabric/util/asio.h>
48
#include <faabric/util/config.h>
5-
#include <pistache/endpoint.h>
6-
#include <pistache/http.h>
79

810
namespace faabric::endpoint {
11+
namespace detail {
12+
struct EndpointState;
13+
}
14+
15+
struct HttpRequestContext
16+
{
17+
asio::io_context& ioc;
18+
asio::any_io_executor executor;
19+
std::function<void(faabric::util::BeastHttpResponse&&)> sendFunction;
20+
};
21+
22+
class HttpRequestHandler
23+
{
24+
public:
25+
virtual void onRequest(HttpRequestContext&& ctx,
26+
faabric::util::BeastHttpRequest&& request) = 0;
27+
};
28+
929
class Endpoint
1030
{
1131
public:
12-
Endpoint();
32+
Endpoint() = delete;
33+
Endpoint(const Endpoint&) = delete;
34+
Endpoint(Endpoint&&) = delete;
35+
Endpoint& operator=(const Endpoint&) = delete;
36+
Endpoint& operator=(Endpoint&&) = delete;
37+
virtual ~Endpoint();
1338

14-
Endpoint(int port, int threadCount);
39+
Endpoint(int port,
40+
int threadCount,
41+
std::shared_ptr<HttpRequestHandler> requestHandlerIn);
1542

1643
void start(bool awaitSignal = true);
1744

1845
void stop();
1946

20-
virtual std::shared_ptr<Pistache::Http::Handler> getHandler() = 0;
21-
2247
private:
23-
int port = faabric::util::getSystemConfig().endpointPort;
24-
int threadCount = faabric::util::getSystemConfig().endpointNumThreads;
25-
26-
Pistache::Http::Endpoint httpEndpoint;
48+
int port;
49+
int threadCount;
50+
std::unique_ptr<detail::EndpointState> state;
51+
std::shared_ptr<HttpRequestHandler> requestHandler;
2752
};
2853
}

include/faabric/endpoint/FaabricEndpoint.h

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#pragma once
22

3+
#include <faabric/endpoint/Endpoint.h>
34
#include <faabric/proto/faabric.pb.h>
4-
#include <pistache/http.h>
55

66
namespace faabric::endpoint {
7-
class FaabricEndpointHandler : public Pistache::Http::Handler
7+
class FaabricEndpointHandler final
8+
: public HttpRequestHandler
9+
, public std::enable_shared_from_this<FaabricEndpointHandler>
810
{
911
public:
10-
HTTP_PROTOTYPE(FaabricEndpointHandler)
11-
12-
void onTimeout(const Pistache::Http::Request& request,
13-
Pistache::Http::ResponseWriter writer) override;
14-
15-
void onRequest(const Pistache::Http::Request& request,
16-
Pistache::Http::ResponseWriter response) override;
17-
18-
std::pair<int, std::string> handleFunction(const std::string& requestStr);
12+
void onRequest(HttpRequestContext&& ctx,
13+
faabric::util::BeastHttpRequest&& request) override;
1914

2015
private:
21-
std::pair<int, std::string> executeFunction(faabric::Message& msg);
16+
void executeFunction(HttpRequestContext&& ctx,
17+
faabric::util::BeastHttpResponse&& partialResponse,
18+
faabric::Message&& msg);
19+
20+
void onFunctionResult(HttpRequestContext&& ctx,
21+
faabric::util::BeastHttpResponse&& partialResponse,
22+
faabric::Message& msg);
2223
};
2324
}

include/faabric/mpi-native/MpiExecutor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

3-
#include <faabric/endpoint/FaabricEndpoint.h>
3+
#include <faabric/endpoint/Endpoint.h>
4+
#include <faabric/endpoint/FaabricEndpointHandler.h>
45
#include <faabric/scheduler/ExecutorFactory.h>
56
#include <faabric/scheduler/Scheduler.h>
67

include/faabric/scheduler/FunctionCallApi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ enum FunctionCalls
99
Unregister = 3,
1010
GetResources = 4,
1111
SetThreadResult = 5,
12+
DirectResult = 6,
1213
};
1314
}

include/faabric/scheduler/FunctionCallClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class FunctionCallClient : public faabric::transport::MessageEndpointClient
4747
void executeFunctions(
4848
const std::shared_ptr<faabric::BatchExecuteRequest> req);
4949

50+
void sendDirectResult(faabric::Message msg);
51+
5052
void unregister(faabric::UnregisterRequest& req);
5153

5254
private:

include/faabric/scheduler/FunctionCallServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ class FunctionCallServer final
3232
void recvExecuteFunctions(const uint8_t* buffer, size_t bufferSize);
3333

3434
void recvUnregister(const uint8_t* buffer, size_t bufferSize);
35+
36+
void recvDirectResult(const uint8_t* buffer, size_t bufferSize);
3537
};
3638
}

include/faabric/scheduler/Scheduler.h

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
#include <faabric/scheduler/FunctionCallClient.h>
66
#include <faabric/scheduler/InMemoryMessageQueue.h>
77
#include <faabric/snapshot/SnapshotClient.h>
8+
#include <faabric/util/asio.h>
89
#include <faabric/util/config.h>
910
#include <faabric/util/func.h>
1011
#include <faabric/util/queue.h>
1112
#include <faabric/util/snapshot.h>
1213
#include <faabric/util/timing.h>
1314

15+
#include <atomic>
16+
#include <condition_variable>
17+
#include <cstdint>
18+
#include <functional>
1419
#include <future>
20+
#include <optional>
1521
#include <shared_mutex>
1622

1723
#define AVAILABLE_HOST_SET "available_hosts"
@@ -54,6 +60,8 @@ class Executor
5460

5561
void finish();
5662

63+
virtual void setup(faabric::Message& msg);
64+
5765
virtual void reset(faabric::Message& msg);
5866

5967
virtual int32_t executeTask(
@@ -72,6 +80,8 @@ class Executor
7280
protected:
7381
virtual void restore(faabric::Message& msg);
7482

83+
virtual void softShutdown();
84+
7585
virtual void postFinish();
7686

7787
faabric::Message boundMessage;
@@ -87,11 +97,37 @@ class Executor
8797
std::vector<std::shared_ptr<std::thread>> threadPoolThreads;
8898
std::vector<std::shared_ptr<std::thread>> deadThreads;
8999

100+
std::mutex setupMutex;
101+
std::atomic_bool setupDone;
102+
90103
std::vector<faabric::util::Queue<ExecutorTask>> threadTaskQueues;
91104

92105
void threadPoolThread(int threadPoolIdx);
93106
};
94107

108+
struct MessageLocalResult final
109+
{
110+
std::promise<std::unique_ptr<faabric::Message>> promise;
111+
int event_fd = -1;
112+
113+
MessageLocalResult();
114+
MessageLocalResult(const MessageLocalResult&) = delete;
115+
inline MessageLocalResult(MessageLocalResult&& other)
116+
{
117+
this->operator=(std::move(other));
118+
}
119+
MessageLocalResult& operator=(const MessageLocalResult&) = delete;
120+
inline MessageLocalResult& operator=(MessageLocalResult&& other)
121+
{
122+
this->promise = std::move(other.promise);
123+
this->event_fd = other.event_fd;
124+
other.event_fd = -1;
125+
return *this;
126+
}
127+
~MessageLocalResult();
128+
void set_value(std::unique_ptr<faabric::Message>&& msg);
129+
};
130+
95131
class Scheduler
96132
{
97133
public:
@@ -127,6 +163,12 @@ class Scheduler
127163

128164
faabric::Message getFunctionResult(unsigned int messageId, int timeout);
129165

166+
void getFunctionResultAsync(unsigned int messageId,
167+
int timeoutMs,
168+
asio::io_context& ioc,
169+
asio::any_io_executor& executor,
170+
std::function<void(faabric::Message&)> handler);
171+
130172
void setThreadResult(const faabric::Message& msg, int32_t returnValue);
131173

132174
void pushSnapshotDiffs(
@@ -182,7 +224,15 @@ class Scheduler
182224

183225
ExecGraph getFunctionExecGraph(unsigned int msgId);
184226

227+
void updateMonitoring();
228+
229+
std::atomic_int32_t monitorLocallyScheduledTasks;
230+
std::atomic_int32_t monitorStartedTasks;
231+
std::atomic_int32_t monitorWaitingTasks;
232+
185233
private:
234+
int monitorFd = -1;
235+
186236
std::string thisHost;
187237

188238
faabric::util::SystemConfig& conf;
@@ -207,8 +257,7 @@ class Scheduler
207257
std::set<std::string> availableHostsCache;
208258
std::unordered_map<std::string, std::set<std::string>> registeredHosts;
209259

210-
std::unordered_map<uint32_t,
211-
std::promise<std::unique_ptr<faabric::Message>>>
260+
std::unordered_map<uint32_t, std::shared_ptr<MessageLocalResult>>
212261
localResults;
213262
std::mutex localResultsMutex;
214263

@@ -220,9 +269,7 @@ class Scheduler
220269
std::vector<std::string> getUnregisteredHosts(const std::string& funcStr,
221270
bool noCache = false);
222271

223-
std::shared_ptr<Executor> claimExecutor(
224-
faabric::Message& msg,
225-
faabric::util::FullLock& schedulerLock);
272+
std::shared_ptr<Executor> claimExecutor(faabric::Message& msg);
226273

227274
faabric::HostResources getHostResources(const std::string& host);
228275

include/faabric/snapshot/SnapshotRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SnapshotRegistry
1818

1919
bool snapshotExists(const std::string& key);
2020

21-
void mapSnapshot(const std::string& key, uint8_t* target);
21+
uint8_t* mapSnapshot(const std::string& key, uint8_t* target);
2222

2323
void takeSnapshot(const std::string& key,
2424
faabric::util::SnapshotData data,

0 commit comments

Comments
 (0)