-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathserver.cpp
More file actions
334 lines (305 loc) · 12.1 KB
/
Copy pathserver.cpp
File metadata and controls
334 lines (305 loc) · 12.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <thread>
#include <unordered_map>
#ifndef _WIN32
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <unistd.h>
#endif
#include "codegen/gen_api.h"
#include "codegen/gen_server.h"
#include "lupine_log.h"
#include "manual_server.h"
#include "rpc.h"
#define DEFAULT_PORT 14833
#define MAX_CLIENTS 10
static void lupine_log_manual_handler_error(const char *name) {
LUPINE_LOG_ERROR("Error handling manual " << name << " request.");
}
struct lupine_manual_handler {
RequestHandler handler;
const char *name;
};
// Manual handlers are looked up before the auto-generated handlers from
// get_handler(), so entries here take precedence for overlapping ops.
static const std::unordered_map<int, lupine_manual_handler> &
lupine_manual_handlers() {
static const std::unordered_map<int, lupine_manual_handler> handlers = {
{LUPINE_RPC_cuGetExportTableMetadata,
{handle_manual_cuGetExportTableMetadata, "cuGetExportTable metadata"}},
{LUPINE_RPC_cuPrivateGetModuleNode,
{handle_manual_cuPrivateGetModuleNode, "private module node"}},
{RPC_cuModuleLoad, {handle_manual_cuModuleLoad, "cuModuleLoad"}},
{RPC_cuModuleLoadData,
{handle_manual_cuModuleLoadData, "cuModuleLoadData"}},
{RPC_cuLibraryLoadData,
{handle_manual_cuLibraryLoadData, "cuLibraryLoadData"}},
{RPC_cuCtxCreate_v2, {handle_manual_cuCtxCreate_v2, "cuCtxCreate_v2"}},
{RPC_cuMemPoolSetAttribute,
{handle_manual_cuMemPoolSetAttribute, "cuMemPoolSetAttribute"}},
{RPC_cuMemPoolGetAttribute,
{handle_manual_cuMemPoolGetAttribute, "cuMemPoolGetAttribute"}},
{RPC_cuPointerGetAttribute,
{handle_manual_cuPointerGetAttribute, "cuPointerGetAttribute"}},
{RPC_cuPointerGetAttributes,
{handle_manual_cuPointerGetAttributes, "cuPointerGetAttributes"}},
{RPC_cuLinkCreate_v2, {handle_manual_cuLinkCreate_v2, "cuLinkCreate_v2"}},
{RPC_cuLinkAddData_v2,
{handle_manual_cuLinkAddData_v2, "cuLinkAddData_v2"}},
{RPC_cuLinkAddFile_v2,
{handle_manual_cuLinkAddFile_v2, "cuLinkAddFile_v2"}},
{RPC_cuLinkComplete, {handle_manual_cuLinkComplete, "cuLinkComplete"}},
{RPC_cuLinkDestroy, {handle_manual_cuLinkDestroy, "cuLinkDestroy"}},
{RPC_cuMemcpy3D_v2, {handle_manual_cuMemcpy3D_v2, "cuMemcpy3D_v2"}},
{RPC_cuMemcpy2D_v2, {handle_manual_cuMemcpy2D_v2, "cuMemcpy2D_v2"}},
{RPC_cuMemcpy2DUnaligned_v2,
{handle_manual_cuMemcpy2DUnaligned_v2, "cuMemcpy2DUnaligned_v2"}},
{RPC_cuMemcpy2DAsync_v2,
{handle_manual_cuMemcpy2DAsync_v2, "cuMemcpy2DAsync_v2"}},
{RPC_cuDeviceGetGraphMemAttribute,
{handle_manual_cuDeviceGetGraphMemAttribute,
"cuDeviceGetGraphMemAttribute"}},
{RPC_cuDeviceSetGraphMemAttribute,
{handle_manual_cuDeviceSetGraphMemAttribute,
"cuDeviceSetGraphMemAttribute"}},
{RPC_cuLibraryGetModule,
{handle_manual_cuLibraryGetModule, "cuLibraryGetModule"}},
{RPC_cuLibraryUnload, {handle_manual_cuLibraryUnload, "cuLibraryUnload"}},
{RPC_cuModuleGetGlobal_v2,
{handle_manual_cuModuleGetGlobal_v2, "cuModuleGetGlobal_v2"}},
{LUPINE_RPC_cuFuncGetParamLayout,
{handle_manual_cuFuncGetParamLayout, "cuFuncGetParamLayout"}},
{RPC_cuOccupancyMaxPotentialBlockSize,
{[](conn_t *conn) {
return handle_manual_cuOccupancyMaxPotentialBlockSize(conn, false);
},
"cuOccupancyMaxPotentialBlockSize"}},
{RPC_cuOccupancyMaxPotentialBlockSizeWithFlags,
{[](conn_t *conn) {
return handle_manual_cuOccupancyMaxPotentialBlockSize(conn, true);
},
"cuOccupancyMaxPotentialBlockSizeWithFlags"}},
{RPC_cuLaunchKernel, {handle_manual_cuLaunchKernel, "cuLaunchKernel"}},
{RPC_cuLaunchCooperativeKernel,
{handle_manual_cuLaunchCooperativeKernel, "cuLaunchCooperativeKernel"}},
{RPC_cuGraphAddKernelNode_v2,
{handle_manual_cuGraphAddKernelNode, "cuGraphAddKernelNode"}},
{RPC_cuGraphKernelNodeGetParams_v2,
{handle_manual_cuGraphKernelNodeGetParams,
"cuGraphKernelNodeGetParams_v2"}},
{RPC_cuGraphKernelNodeSetParams_v2,
{handle_manual_cuGraphKernelNodeSetParams,
"cuGraphKernelNodeSetParams_v2"}},
{RPC_cuGraphAddMemcpyNode,
{handle_manual_cuGraphAddMemcpyNode, "cuGraphAddMemcpyNode"}},
{RPC_cuGraphAddMemsetNode,
{handle_manual_cuGraphAddMemsetNode, "cuGraphAddMemsetNode"}},
{RPC_cuGraphAddHostNode,
{handle_manual_cuGraphAddHostNode, "cuGraphAddHostNode"}},
{RPC_cuGraphExecKernelNodeSetParams_v2,
{handle_manual_cuGraphExecKernelNodeSetParams,
"cuGraphExecKernelNodeSetParams_v2"}},
{LUPINE_RPC_cuGraphConditionalHandleCreate,
{handle_manual_cuGraphConditionalHandleCreate,
"cuGraphConditionalHandleCreate"}},
{LUPINE_RPC_cuGraphAddNode_v2,
{handle_manual_cuGraphAddNode, "cuGraphAddNode"}},
{RPC_cuGraphLaunch, {handle_manual_cuGraphLaunch, "cuGraphLaunch"}},
{RPC_cuGraphGetEdges_v2,
{handle_manual_cuGraphGetEdges, "cuGraphGetEdges"}},
{RPC_cuGraphNodeGetDependencies_v2,
{handle_manual_cuGraphNodeGetDependencies,
"cuGraphNodeGetDependencies"}},
{RPC_cuGraphNodeGetDependentNodes_v2,
{handle_manual_cuGraphNodeGetDependentNodes,
"cuGraphNodeGetDependentNodes"}},
{LUPINE_RPC_cuMemPrefetchAsync,
{handle_manual_cuMemPrefetchAsync, "cuMemPrefetchAsync"}},
{RPC_cuGraphHostNodeGetParams,
{handle_manual_cuGraphHostNodeGetParams, "cuGraphHostNodeGetParams"}},
{RPC_cuGraphHostNodeSetParams,
{handle_manual_cuGraphHostNodeSetParams, "cuGraphHostNodeSetParams"}},
{RPC_cuGraphExecHostNodeSetParams,
{handle_manual_cuGraphExecHostNodeSetParams,
"cuGraphExecHostNodeSetParams"}},
{RPC_cuLaunchHostFunc,
{handle_manual_cuLaunchHostFunc, "cuLaunchHostFunc"}},
{RPC_cuStreamAddCallback,
{handle_manual_cuStreamAddCallback, "cuStreamAddCallback"}},
{RPC_cuEventRecord,
{[](conn_t *conn) { return handle_manual_cuEventRecord(conn, false); },
"cuEventRecord"}},
{RPC_cuEventRecordWithFlags,
{[](conn_t *conn) { return handle_manual_cuEventRecord(conn, true); },
"cuEventRecordWithFlags"}},
{RPC_cuEventQuery, {handle_manual_cuEventQuery, "cuEventQuery"}},
{RPC_cuStreamWaitEvent,
{handle_manual_cuStreamWaitEvent, "cuStreamWaitEvent"}},
{LUPINE_RPC_cuStreamBeginCaptureToGraph,
{handle_manual_cuStreamBeginCaptureToGraph,
"cuStreamBeginCaptureToGraph"}},
{RPC_cuStreamUpdateCaptureDependencies_v2,
{handle_manual_cuStreamUpdateCaptureDependencies,
"cuStreamUpdateCaptureDependencies"}},
{LUPINE_RPC_cuStreamGetCaptureInfo_v3,
{handle_manual_cuStreamGetCaptureInfo, "cuStreamGetCaptureInfo"}},
{RPC_cuStreamBeginCapture_v2,
{handle_manual_cuStreamBeginCapture, "cuStreamBeginCapture"}},
{RPC_cuStreamEndCapture,
{handle_manual_cuStreamEndCapture, "cuStreamEndCapture"}},
{RPC_cuGraphClone, {handle_manual_cuGraphClone, "cuGraphClone"}},
{RPC_cuGraphInstantiateWithFlags,
{handle_manual_cuGraphInstantiateWithFlags,
"cuGraphInstantiateWithFlags"}},
{RPC_cuGraphInstantiateWithParams,
{handle_manual_cuGraphInstantiateWithParams,
"cuGraphInstantiateWithParams"}},
{RPC_cuGraphExecDestroy,
{handle_manual_cuGraphExecDestroy, "cuGraphExecDestroy"}},
{RPC_cuGraphDestroy, {handle_manual_cuGraphDestroy, "cuGraphDestroy"}},
{RPC_cuMemcpyHtoD_v2, {handle_manual_cuMemcpyHtoD_v2, "cuMemcpyHtoD_v2"}},
{RPC_cuMemcpyHtoDAsync_v2,
{handle_manual_cuMemcpyHtoDAsync_v2, "cuMemcpyHtoDAsync_v2"}},
{RPC_cuMemcpyDtoHAsync_v2,
{handle_manual_cuMemcpyDtoHAsync_v2, "cuMemcpyDtoHAsync_v2"}},
{RPC_cuCtxSynchronize,
{handle_manual_cuCtxSynchronize, "cuCtxSynchronize"}},
{RPC_cuStreamSynchronize,
{handle_manual_cuStreamSynchronize, "cuStreamSynchronize"}},
{RPC_cuEventSynchronize,
{handle_manual_cuEventSynchronize, "cuEventSynchronize"}},
};
return handlers;
}
void client_handler(lupine_socket_t connfd) {
conn_t conn = {connfd, 1};
conn.request_id = 1;
conn.local_request_parity = conn.request_id & 1;
if (pthread_mutex_init(&conn.read_mutex, NULL) < 0 ||
pthread_mutex_init(&conn.write_mutex, NULL) < 0 ||
pthread_mutex_init(&conn.call_mutex, NULL) < 0 ||
pthread_cond_init(&conn.read_cond, NULL) < 0 ||
rpc_http2_server_init(&conn) < 0) {
LUPINE_LOG_ERROR("Error initializing mutex.");
return;
}
printf("Client connected.\n");
while (1) {
int op = rpc_dispatch(&conn, 0);
if (op < 0) {
LUPINE_LOG_ERROR("RPC dispatch failed; closing client.");
break;
}
LUPINE_TRACE_LOG("LUPINE server handling op " << op);
const auto &manual_handlers = lupine_manual_handlers();
auto manual = manual_handlers.find(op);
if (manual != manual_handlers.end()) {
if (manual->second.handler(&conn) < 0) {
lupine_log_manual_handler_error(manual->second.name);
break;
}
continue;
}
auto opHandler = get_handler(op);
if (opHandler == nullptr) {
LUPINE_LOG_ERROR("No RPC handler for op " << op << "; closing client.");
break;
}
if (opHandler(&conn) < 0) {
LUPINE_LOG_ERROR("Error handling request.");
break;
}
}
if (pthread_mutex_destroy(&conn.read_mutex) < 0 ||
pthread_mutex_destroy(&conn.write_mutex) < 0)
LUPINE_LOG_ERROR("Error destroying mutex.");
lupine_socket_close(connfd);
}
int main() {
int port = DEFAULT_PORT;
struct sockaddr_in servaddr, cli;
if (lupine_socket_init() < 0) {
printf("Socket initialization failed.\n");
exit(EXIT_FAILURE);
}
lupine_socket_t sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == LUPINE_INVALID_SOCKET) {
printf("Socket creation failed.\n");
exit(EXIT_FAILURE);
}
char *p = getenv("LUPINE_PORT");
if (p == NULL) {
port = DEFAULT_PORT;
} else {
port = atoi(p);
}
// Bind the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
if (lupine_socket_set_reuseaddr(sockfd) < 0) {
printf("Socket bind failed.\n");
exit(EXIT_FAILURE);
}
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
printf("Socket bind failed.\n");
exit(EXIT_FAILURE);
}
if (listen(sockfd, MAX_CLIENTS) != 0) {
printf("Listen failed.\n");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", port);
#ifndef _WIN32
// reap exited connection processes automatically
signal(SIGCHLD, SIG_IGN);
#endif
// Server loop
while (1) {
socklen_t len = sizeof(cli);
lupine_socket_t connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd == LUPINE_INVALID_SOCKET) {
LUPINE_LOG_ERROR("Server accept failed.");
continue;
}
#ifndef _WIN32
int flag = 1;
setsockopt(connfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
#endif
#ifndef _WIN32
// fork a process per connection so each client gets its own CUDA driver
// state (primary context, allocations, modules). this matches local
// semantics: a client resetting or corrupting its context cannot affect
// other clients, and everything is released when the client disconnects.
// the parent must never initialize CUDA; forked children cannot use a
// parent's initialized driver.
fflush(stdout);
fflush(stderr);
pid_t pid = fork();
if (pid < 0) {
LUPINE_LOG_ERROR("Server fork failed.");
lupine_socket_close(connfd);
continue;
}
if (pid == 0) {
lupine_socket_close(sockfd);
client_handler(connfd);
exit(0);
}
lupine_socket_close(connfd);
#else
// Windows has no fork; connections share the server process.
std::thread client_thread(client_handler, connfd);
// detach the thread so it runs independently
client_thread.detach();
#endif
}
lupine_socket_close(sockfd);
return 0;
}