-
Notifications
You must be signed in to change notification settings - Fork 125
/
photon.cpp
154 lines (132 loc) · 4.15 KB
/
photon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "photon.h"
#include <inttypes.h>
#include "io/fd-events.h"
#include "io/signal.h"
#include "io/aio-wrapper.h"
#include "thread/thread.h"
#include "thread/thread-pool.h"
#include "thread/stack-allocator.h"
#ifdef ENABLE_FSTACK_DPDK
#include "io/fstack-dpdk.h"
#endif
#include "io/reset_handle.h"
#ifdef ENABLE_CURL
#include "net/curl.h"
#endif
#include "net/socket.h"
#include "fs/exportfs.h"
#include "common/alog.h"
#include "common/callback.h"
#include <vector>
namespace photon {
using namespace fs;
using namespace net;
static bool reset_handle_registed = false;
static thread_local uint64_t g_event_engine = 0, g_io_engine = 0;
#define INIT_IO(name, prefix, ...) if (INIT_IO_##name & io_engine) { if (prefix##_init(__VA_ARGS__) < 0) return -1; }
#define FINI_IO(name, prefix) if (INIT_IO_##name & g_io_engine) { prefix##_fini(); }
class Shift {
uint8_t _n;
public:
constexpr Shift(uint64_t x) : _n(__builtin_ctz(x)) { }
operator uint64_t() { return 1UL << _n; }
};
// Try to init master engine with the recommended order
static const Shift recommended_order[] = {
#if defined(__linux__)
INIT_EVENT_EPOLL, INIT_EVENT_IOURING, INIT_EVENT_EPOLL_NG, INIT_EVENT_SELECT};
#else // macOS, FreeBSD ...
INIT_EVENT_KQUEUE, INIT_EVENT_SELECT};
#endif
int __photon_init(uint64_t event_engine, uint64_t io_engine, const PhotonOptions& options) {
if (options.use_pooled_stack_allocator) {
use_pooled_stack_allocator();
}
if (options.bypass_threadpool) {
set_bypass_threadpool(true);
}
if (vcpu_init() < 0)
return -1;
const uint64_t ALL_ENGINES =
INIT_EVENT_EPOLL | INIT_EVENT_EPOLL_NG |
INIT_EVENT_IOURING | INIT_EVENT_KQUEUE |
INIT_EVENT_SELECT | INIT_EVENT_IOCP;
if (event_engine & ALL_ENGINES) {
bool ok = false;
for (auto x : recommended_order) {
if ((x & event_engine) && fd_events_init(x) == 0) {
ok = true;
break;
}
}
if (!ok) {
LOG_ERROR_RETURN(0, -1, "All master engines init failed");
}
}
if ((INIT_EVENT_SIGNAL & event_engine) && sync_signal_init() < 0)
return -1;
#ifdef ENABLE_FSTACK_DPDK
INIT_IO(FSTACK_DPDK, fstack_dpdk);
#endif
INIT_IO(EXPORTFS, exportfs)
#ifdef ENABLE_CURL
INIT_IO(LIBCURL, libcurl)
#endif
#ifdef __linux__
INIT_IO(LIBAIO, libaio_wrapper, options.libaio_queue_depth)
INIT_IO(SOCKET_EDGE_TRIGGER, et_poller)
#endif
g_event_engine = event_engine;
g_io_engine = io_engine;
if (!reset_handle_registed) {
pthread_atfork(nullptr, nullptr, &reset_all_handle);
LOG_DEBUG("reset_all_handle registed ", VALUE(getpid()));
reset_handle_registed = true;
}
return 0;
}
int init(uint64_t event_engine, uint64_t io_engine, const PhotonOptions& options) {
return __photon_init(event_engine, io_engine, options);
}
static std::vector<Delegate<void>>& get_hook_vector() {
thread_local std::vector<Delegate<void>> hooks;
return hooks;
}
void fini_hook(Delegate<void> handler) {
get_hook_vector().emplace_back(handler);
}
int fini() {
for (auto h : get_hook_vector()) {
h.fire();
}
#ifdef __linux__
FINI_IO(LIBAIO, libaio_wrapper)
FINI_IO(SOCKET_EDGE_TRIGGER, et_poller)
#endif
#ifdef ENABLE_CURL
FINI_IO(LIBCURL, libcurl)
#endif
FINI_IO(EXPORTFS, exportfs)
#ifdef ENABLE_FSTACK_DPDK
FINI_IO(FSTACK_DPDK, fstack_dpdk)
#endif
if (INIT_EVENT_SIGNAL & g_event_engine)
sync_signal_fini();
fd_events_fini();
vcpu_fini();
g_event_engine = g_io_engine = 0;
return 0;
}
}