-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
358 lines (295 loc) · 6.95 KB
/
io.c
File metadata and controls
358 lines (295 loc) · 6.95 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/time.h>
#include <poll.h>
#include "io.h"
extern char **environ;
int ifd = -1;
pid_t pid = -1;
cci_endpoint_t *endpoint = NULL;
cci_connection_t *connection = NULL;
cci_rma_handle_t *local = NULL;
int irank = -1;
cci_os_handle_t *cfd = NULL, ep_fd;
struct pollfd pfd;
static void
handle_sigchld(int sig)
{
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
fprintf(stderr, "%s: daemon exited, status=%d\n",
__func__, WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "%s: killed by signal %d\n",
__func__, WTERMSIG(status));
abort();
}
return;
}
static int
start_daemon(char **args)
{
int ret = 0;
signal(SIGCHLD, handle_sigchld);
pid = fork();
if (pid == -1) {
ret = errno;
fprintf(stderr, "%s: fork() failed with %s\n", __func__,
strerror(ret));
} else if (pid == 0) {
execve(args[0], args, environ);
/* if we return, exec() failed */
ret = errno;
fprintf(stderr, "%s: execve() failed with %s\n", __func__,
strerror(ret));
exit(ret);
} else {
fprintf(stderr, "%s daemon started with PID %d\n", args[0], pid);
}
return ret;
}
int io_init(void *buffer, uint32_t len, uint32_t rank, uint32_t ranks,
char **daemon_args, int blocking)
{
int ret = 0, fd_iod = -1, ready = 0;
uint32_t caps = 0;
io_msg_t msg;
char hostname[256], server[256];
if (!buffer || !len) {
ret = EINVAL;
goto out;
}
memset(hostname, 0, sizeof(hostname));
gethostname(hostname, sizeof(hostname));
ifd = open(hostname, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
if (ifd != -1) {
ret = start_daemon( daemon_args);
if (ret) {
goto out;
}
}
ret = cci_init(CCI_ABI_VERSION, 0, &caps);
if (ret) {
fprintf(stderr, "%s: cci_init() failed with %s\n",
__func__, cci_strerror(NULL, ret));
goto out;
}
if (blocking)
cfd = &ep_fd;
ret = cci_create_endpoint(NULL, 0, &endpoint, cfd);
if (ret) {
fprintf(stderr, "%s: cci_create_endpoint() failed with %s\n",
__func__, cci_strerror(NULL, ret));
goto out;
}
if (blocking) {
pfd.fd = ep_fd;
pfd.events = POLLIN;
}
ret = cci_rma_register(endpoint, buffer, (uint64_t)len,
CCI_FLAG_WRITE|CCI_FLAG_READ, &local);
if (ret) {
fprintf(stderr, "%s: cci_rma_register() failed with %s\n",
__func__, cci_strerror(endpoint, ret));
goto out;
}
irank = rank;
msg.connect.type = CONNECT;
msg.connect.rank = rank;
msg.connect.ranks = ranks;
msg.connect.len = len;
memcpy((void*)(uintptr_t)&msg.connect.handle, local, sizeof(*local));
memset(server, 0, sizeof(server));
snprintf(server, sizeof(server), "%s-iod", hostname);
do {
fd_iod = open(server, O_RDONLY);
} while (fd_iod == -1);
again:
ret = read(fd_iod, server, sizeof(server));
if (ret == -1) {
ret = errno;
if (ret == EINTR)
goto again;
fprintf(stderr, "%s: read() failed with %s\n",
__func__, strerror(ret));
goto out;
} else if (ret == 0) {
goto again;
}
ret = cci_connect(endpoint, server, &msg, sizeof(msg.connect),
CCI_CONN_ATTR_RO, NULL, 0, NULL);
if (ret) {
fprintf(stderr, "%s: cci_connect() failed with %s\n",
__func__, cci_strerror(endpoint, ret));
goto out;
}
do {
cci_event_t *event = NULL;
ret = cci_get_event(endpoint, &event);
if (!ret) {
switch (event->type) {
case CCI_EVENT_CONNECT:
connection = event->connect.connection;
ready++;
break;
default:
fprintf(stderr, "%s: ignoring %s\n", __func__,
cci_event_type_str(event->type));
break;
}
cci_return_event(event);
}
} while (!ready);
if (!connection) {
ret = ENOTCONN;
fprintf(stderr, "%s: CCI connect failed\n", __func__);
}
out:
if (fd_iod != -1)
close(fd_iod);
if (ret)
io_finalize();
return ret;
}
static void
print_perf(uint32_t len, struct timeval start, struct timeval end)
{
uint64_t usecs = 0;
double lat = 0.0, bw = 0.0;
usecs = (end.tv_sec - start.tv_sec) * 1000000 +
end.tv_usec - start.tv_usec;
lat = (double) usecs / 1000000.0;
bw = (double) len / 1000000.0 / lat;
fprintf(stderr, "rank %d: %u bytes %10"PRIu64" usecs %10.2lf MB/s\n",
irank, len, usecs, bw);
return;
}
int io_write(uint32_t len)
{
int ret = 0, done = 0;
static int i = 0;
io_msg_t msg;
i++;
msg.request.type = WRITE_REQ;
msg.request.len = len;
ret = cci_send(connection, &msg, sizeof(msg.request), (void*)(uintptr_t)i, 0);
if (ret) {
fprintf(stderr, "%s: cci_send() failed with %s\n",
__func__, cci_strerror(endpoint, ret));
goto out;
}
do {
cci_event_t *event = NULL;
if (cfd)
poll(&pfd, 1, 0);
ret = cci_get_event(endpoint, &event);
if (!ret) {
const io_msg_t *rx = NULL;
switch (event->type) {
case CCI_EVENT_SEND:
assert(event->send.context == (void*)(uintptr_t)i);
break;
case CCI_EVENT_RECV:
rx = event->recv.ptr;
assert(rx->type == WRITE_DONE);
break;
default:
fprintf(stderr, "%s: ignoring %s\n",
__func__, cci_event_type_str(event->type));
break;
}
cci_return_event(event);
done++;
}
} while (done < 2);
#if 0
gettimeofday(&end, NULL);
print_perf(len, start, end);
#endif
out:
return ret;
}
int io_finalize(void)
{
int ret = 0;
io_msg_t msg;
char hostname[256];
msg.type = BYE;
if (connection) {
ret = cci_send(connection, &msg, sizeof(msg.bye),
(void*)(uintptr_t)0xdeadbeef, 0);
if (!ret) {
int done = 0, i = 0;
cci_event_t *event = NULL;
do {
ret = cci_get_event(endpoint, &event);
if (!ret) {
const io_msg_t *rx = NULL;
switch (event->type) {
case CCI_EVENT_SEND:
assert(event->send.context ==
(void*)(uintptr_t)0xdeadbeef);
break;
case CCI_EVENT_RECV:
rx = event->recv.ptr;
assert(rx->type == FINISHED);
break;
default:
break;
}
cci_return_event(event);
done++;
}
} while (done < 2);
/* to allow CCI to ack the send? */
for (i = 0; i < 10; i++)
cci_get_event(endpoint, &event);
}
ret = cci_disconnect(connection);
if (ret) {
fprintf(stderr, "%s: cci_disconnect() failed with %s\n",
__func__, cci_strerror(endpoint, ret));
}
}
if (local) {
ret = cci_rma_deregister(endpoint, local);
if (ret) {
fprintf(stderr, "%s: cci_rma_deregister() failed with %s\n",
__func__, cci_strerror(endpoint, ret));
}
}
if (endpoint) {
ret = cci_destroy_endpoint(endpoint);
if (ret) {
fprintf(stderr, "%s: cci_destroy_endpoint() failed with %s\n",
__func__, cci_strerror(NULL, ret));
}
}
ret = cci_finalize();
if (ret) {
fprintf(stderr, "%s: cci_destroy_endpoint() failed with %s\n", __func__,
cci_strerror(NULL, ret));
}
sleep(1);
if (pid != -1) {
signal(SIGCHLD, SIG_IGN);
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
}
if (ifd != -1) {
close(ifd);
}
memset(hostname, 0, sizeof(hostname));
gethostname(hostname, sizeof(hostname));
unlink(hostname);
return ret;
}