-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fdproxy.c
142 lines (116 loc) · 3.04 KB
/
test_fdproxy.c
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
/*
* test_fdproxy.c
*
* Copyright 2007 Jean-Marc Saffroy <[email protected]>
* This file is part of the Driller library.
* Driller is free software, distributed under the terms of the
* GNU Lesser General Public License version 2.1.
*
*/
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include "mmpi.h"
#include "log.h"
#include "fdproxy.h"
static void usage(char *progname) {
err("usage: %s <job id> <job size> <rank> <iter>", progname);
}
int main(int argc, char**argv) {
int jobid, nprocs, rank, iter, i, j;
struct fdkey key1, key2;
size_t sz;
/* parse args */
if(argc != 5)
usage(argv[0]);
jobid = atoi(argv[1]);
nprocs = atoi(argv[2]);
rank = atoi(argv[3]);
iter = atoi(argv[4]);
mmpi_init(jobid, nprocs, rank);
mmpi_barrier();
/* fdproxy already initialized by mmpi_init */
/* let siblings duplicate stdout/stderr from rank 0
* - for stdout we have fdproxy make the fd key,
* and we send it to siblings
* - for stderr we use a "well-known" key id
*/
if(rank == 0) {
printf("rank 0 sends stdout\n");
memset(&key1, 0, sizeof(key1));
fdproxy_client_send_fd(1, &key1);
for(i = 1; i < nprocs; i++)
mmpi_send(i, &key1, sizeof(key1));
printf("rank 0 sends stderr\n");
fdproxy_set_key_id(&key2, 0x123);
fdproxy_client_send_fd(2, &key2);
} else {
mmpi_recv(0, &key1, &sz);
assert(sz == sizeof(key1));
fdproxy_set_key_id(&key2, 0x123);
}
mmpi_barrier();
if(rank != 0) {
int fd_out, fd_err;
FILE *new_out, *new_err;
struct fdkey bogus;
printf("rank %d fetches bogus fd\n", rank);
fd_err = fdproxy_client_get_fd(&bogus);
assert(fd_err == -1);
printf("rank %d fetches new stdout\n", rank);
fd_out = fdproxy_client_get_fd(&key1);
printf("rank %d fetches new stderr\n", rank);
fd_err = fdproxy_client_get_fd(&key2);
new_out = fdopen(fd_out, "w");
if(new_out == NULL)
perr("fdopen");
new_err = fdopen(fd_err, "w");
if(new_err == NULL)
perr("fdopen");
fprintf(new_out, "rank %d writes to rank 0's stdout\n", rank);
fprintf(new_err, "rank %d writes to rank 0's stderr\n", rank);
fclose(new_out);
fclose(new_err);
}
mmpi_barrier();
/* repeatedly fetch rank 0 fd 1 */
for(i = 0; i < iter/nprocs ; i++) {
int fd;
fd = fdproxy_client_get_fd(&key1);
assert(fd != -1);
assert(close(fd) == 0);
}
mmpi_barrier();
if(rank == 0) {
fdproxy_client_invalidate_fd(&key1);
fdproxy_client_invalidate_fd(&key2);
}
mmpi_barrier();
/* repeatedly send / inval rank 0 fd 1 */
for(i = 0; i < iter/nprocs ; i++) {
int fd;
size_t sz;
struct fdkey key;
if(rank == 0) {
fd = dup(1);
assert(fd >= 0);
memset(&key, 0, sizeof(key));
fdproxy_client_send_fd(fd, &key);
for(j = 1; j < nprocs; j++)
mmpi_send(j, &key, sizeof(key));
mmpi_barrier();
fdproxy_client_invalidate_fd(&key);
assert(close(fd) == 0);
} else {
mmpi_recv(0, &key, &sz);
assert(sz == sizeof(key));
fd = fdproxy_client_get_fd(&key);
assert(fd != -1);
assert(close(fd) == 0);
mmpi_barrier();
}
}
mmpi_barrier();
printf("SUCCESS! rank %d exits\n", rank);
return 0;
}