Skip to content

Commit a03d5fe

Browse files
committed
A large number of performance changes (related to faasm#156)
1 parent 8a70efd commit a03d5fe

32 files changed

+907
-287
lines changed

examples/server.cpp

+8-2
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

+35-10
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

-16
This file was deleted.
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

+2-1
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

+1
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

+2
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

+2
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

+52-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
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/scheduling.h>
1213
#include <faabric/util/snapshot.h>
1314
#include <faabric/util/timing.h>
1415

16+
#include <atomic>
17+
#include <condition_variable>
18+
#include <cstdint>
19+
#include <functional>
1520
#include <future>
21+
#include <optional>
1622
#include <shared_mutex>
1723

1824
#define AVAILABLE_HOST_SET "available_hosts"
@@ -55,6 +61,8 @@ class Executor
5561

5662
void finish();
5763

64+
virtual void setup(faabric::Message& msg);
65+
5866
virtual void reset(faabric::Message& msg);
5967

6068
virtual int32_t executeTask(
@@ -73,6 +81,8 @@ class Executor
7381
protected:
7482
virtual void restore(faabric::Message& msg);
7583

84+
virtual void softShutdown();
85+
7686
virtual void postFinish();
7787

7888
faabric::Message boundMessage;
@@ -88,11 +98,37 @@ class Executor
8898
std::vector<std::shared_ptr<std::thread>> threadPoolThreads;
8999
std::vector<std::shared_ptr<std::thread>> deadThreads;
90100

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

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

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

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

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

133175
void pushSnapshotDiffs(
@@ -183,7 +225,15 @@ class Scheduler
183225

184226
ExecGraph getFunctionExecGraph(unsigned int msgId);
185227

228+
void updateMonitoring();
229+
230+
std::atomic_int32_t monitorLocallyScheduledTasks;
231+
std::atomic_int32_t monitorStartedTasks;
232+
std::atomic_int32_t monitorWaitingTasks;
233+
186234
private:
235+
int monitorFd = -1;
236+
187237
std::string thisHost;
188238

189239
faabric::util::SystemConfig& conf;
@@ -208,8 +258,7 @@ class Scheduler
208258
std::set<std::string> availableHostsCache;
209259
std::unordered_map<std::string, std::set<std::string>> registeredHosts;
210260

211-
std::unordered_map<uint32_t,
212-
std::promise<std::unique_ptr<faabric::Message>>>
261+
std::unordered_map<uint32_t, std::shared_ptr<MessageLocalResult>>
213262
localResults;
214263
std::mutex localResultsMutex;
215264

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

224-
std::shared_ptr<Executor> claimExecutor(
225-
faabric::Message& msg,
226-
faabric::util::FullLock& schedulerLock);
273+
std::shared_ptr<Executor> claimExecutor(faabric::Message& msg);
227274

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

include/faabric/snapshot/SnapshotRegistry.h

+1-1
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,

include/faabric/util/asio.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include <boost/asio.hpp>
4+
#include <boost/beast/core.hpp>
5+
#include <boost/beast/http.hpp>
6+
#include <boost/beast/version.hpp>
7+
8+
namespace asio = boost::asio;
9+
namespace beast = boost::beast;
10+
11+
namespace faabric::util {
12+
using BeastHttpRequest = beast::http::request<beast::http::string_body>;
13+
using BeastHttpResponse = beast::http::response<beast::http::string_body>;
14+
}

include/faabric/util/config.h

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class SystemConfig
4343
int endpointPort;
4444
int endpointNumThreads;
4545

46+
// Monitoring
47+
std::string schedulerMonitorFile;
48+
4649
// Transport
4750
int functionServerThreads;
4851
int stateServerThreads;

include/faabric/util/delta.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstdint>
44
#include <functional>
55
#include <string>
6+
#include <utility>
67
#include <vector>
78

89
namespace faabric::util {
@@ -40,11 +41,13 @@ enum DeltaCommand : uint8_t
4041
DELTACMD_END = 0xFE,
4142
};
4243

43-
std::vector<uint8_t> serializeDelta(const DeltaSettings& cfg,
44-
const uint8_t* oldDataStart,
45-
size_t oldDataLen,
46-
const uint8_t* newDataStart,
47-
size_t newDataLen);
44+
std::vector<uint8_t> serializeDelta(
45+
const DeltaSettings& cfg,
46+
const uint8_t* oldDataStart,
47+
size_t oldDataLen,
48+
const uint8_t* newDataStart,
49+
size_t newDataLen,
50+
const std::vector<std::pair<uint32_t, uint32_t>>* excludedPtrLens = nullptr);
4851

4952
void applyDelta(const std::vector<uint8_t>& delta,
5053
std::function<void(uint32_t)> setDataSize,

src/endpoint/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
faabric_lib(endpoint
33
Endpoint.cpp
4-
FaabricEndpoint.cpp
54
FaabricEndpointHandler.cpp
65
)
76

0 commit comments

Comments
 (0)