-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
280 lines (256 loc) · 7.06 KB
/
main.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
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
//License: Apache License 2.0
//See LICENSE for details
//Authors (in alphabet order of last name):
// - Qijiang Fan <[email protected]>
// - Bin He <[email protected]>
// - Cheng Zhang <[email protected]>
// - Shiwei Zhou <[email protected]>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <infiniband/verbs.h>
#include "sock.h"
#include "config.h"
#define DEFAULT_PORT 19875
extern struct added_config_t added_config ;
extern struct config_t config;
pthread_mutex_t m1lockw, m1lockr, m2lockw, m2lockr;
pthread_t tRead, tWrite;
int is_client = 0;
struct resources *res;
void *fileBuf;
int verbose_mode = 0;
extern struct f_linkedlist* head_node;
void ReadFileWrapper(void *vargs) {
unsigned long long *args = vargs;
ReadFile(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
void RecvBufWrapper(void *vargs) {
unsigned long long *args = vargs;
RecvBuf(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
void WriteFileWrapper(void *vargs) {
unsigned long long *args = vargs;
WriteFile(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
}
void SendBufWrapper(void *vargs) {
unsigned long long *args = vargs;
SendBuf(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
}
int main(int argc, char *argv[])
{
srand(time(0));
int nprocs = 1;
config.dev_name=strdup("mlx4_0");
config.tcp_port=19879;
config.ib_port=1;
while (1) {
int c;
static struct option long_options[] = {
{.name = "port",.has_arg = 1,.val = 'p'},
{.name = "ib-dev",.has_arg = 1,.val = 'd'},
{.name = "windowsize",.has_arg = 1,.val = 'w'},
{.name = "file",.has_arg = 1,.val = 'f'},
{.name = "list",.has_arg = 1,.val = 'l'},
{.name = "reserve",.has_arg = 0,.val = 'r'},
{.name = "cover",.has_arg = 0,.val = 'c'},
{.name = "verbose",.has_arg = 0,.val = 'v'},
{.name = "ib-port",.has_arg = 1,.val = 'i'},
{.name = "server", .has_arg = 1, .val = 's'},
{.name = "nprocs", .has_arg = 1, .val = 'n'},
{.name = NULL, .has_arg = 0, .val = '\0'}
};
c = getopt_long(argc, argv, "p:d:w:f:l:r:cvi:s:n:", long_options,
NULL);
if (c == -1)
break;
switch (c) {
case 'p':
config.tcp_port = strtoul(optarg, NULL, 0);
break;
case 'd':
config.dev_name = strdup(optarg);
break;
case 'i':
config.ib_port = strtoul(optarg, NULL, 0);
if (config.ib_port < 0) {
printf("error tcp port");
return 1;
}
break;
case 'w':
added_config.windowsize = strtoul(optarg, NULL, 0);
/* other error control can added here */
break;
case 'f':
added_config.p_filename = strdup(optarg);
break;
case 'l':
added_config.p_listname = strdup(optarg);
break;
case 's':
config.server_name = strdup(optarg);
is_client = 1;
break;
case 'n':
nprocs = strtoul(optarg, NULL, 0);
break;
case 'r':
added_config.p_dir=strdup(optarg);
break;
case 'v':
verbose_mode = 1;
break;
}
}
pid_t server_conn_pid = 0;
if (is_client) {
server_conn_pid = fork();
if (server_conn_pid == 0) {
char cmd[256];
sprintf(cmd, "ssh %s ibcp -n %d -w %d >/dev/null 2>/dev/null", config.server_name, nprocs, added_config.windowsize);
system(cmd);
exit(0);
} else if (server_conn_pid < 0) {
printf("Failed to fork\n");
}
}
struct f_linkedlist **head_nodes = malloc(sizeof(struct f_linkedlist*) * nprocs);
struct f_linkedlist **nows = malloc(sizeof(struct f_linkedlist*) * nprocs);
memset(head_nodes, 0, sizeof(struct f_linkedlist*) * nprocs);
memset(nows, 0, sizeof(struct f_linkedlist*) * nprocs);
file_linkedlist_creat();
/*
struct f_linkedlist *ff = head_node;
while (ff) {
printf("File: %s\n", ff->f_name);
ff = ff->next;
}
*/
pid_t *pids = malloc(sizeof(pid_t) * nprocs);
if (nprocs >= 1) {
struct f_linkedlist *now = head_node;
int id = 0;
while (now != NULL) {
int random = id % nprocs;
id += 1;
if (nows[random] == NULL) {
nows[random] = now;
head_nodes[random] = now;
} else {
nows[random]->next = now;
nows[random] = now;
}
struct f_linkedlist *newnow = now->next;
nows[random]->next = NULL;
now = newnow;
}
int k = 0;
/*
for (k = 0; k < nprocs; k++) {
printf("Process %d\n", k);
struct f_linkedlist *f = head_nodes[k];
while (f) {
printf("File: %s\n", f->f_name);
f = f->next;
}
}
*/
int child = 0;
for (k = 0; k < nprocs; k++) {
pid_t pid = fork();
if (pid > 0) {
*(pids + k) = pid;
} else if (pid == 0) {
child = 1;
break;
} else {
printf("Failed to fork\n");
}
}
if (child) {
config.tcp_port += k;
head_node = head_nodes[k];
goto core;
} else {
for (k = 0; k < nprocs; k++) waitpid(pids[k], NULL, 0);
printf("-------------------------------------------\nClient send data end, waiting for remote to finish writing\n--------------------------------------------\n");
waitpid(server_conn_pid, NULL, 0);
printf("Remote write finished\n");
exit(0);
}
}
core:
printf("IS Client: %d\n", is_client);
long int bufSize = added_config.windowsize;
void *buf1 = malloc(bufSize*1024);
void *buf2 = malloc(bufSize*1024);
pthread_mutex_init(&m1lockw, NULL);
pthread_mutex_init(&m1lockr, NULL);
pthread_mutex_init(&m2lockw, NULL);
pthread_mutex_init(&m2lockr, NULL);
printf("start to read\n");
//ReadFile(head_node, bufSize*1024, buf1, buf2, &m1lock, &m2lock);
fileBuf = malloc(bufSize * 1024);
memset(fileBuf, 0, bufSize * 1024);
int tt = 50000;
while (tt--) {
res = init_ib(fileBuf, bufSize * 1024);
if (res) break;
usleep(100);
}
if (!res) {
printf("fail to init the ib");
return 0;
}
printf("Connection established\n");
unsigned long long *argsRead = malloc(sizeof(unsigned long long) * 8);
argsRead[0] = (unsigned long long) (head_node);
argsRead[1] = bufSize * 1024;
argsRead[2] = buf1;
argsRead[3] = buf2;
argsRead[4] = &m1lockw;
argsRead[5] = &m1lockr;
argsRead[6] = &m2lockw;
argsRead[7] = &m2lockr;
pthread_mutex_lock(&m1lockw);
pthread_mutex_lock(&m1lockr);
if (is_client)
pthread_create(&tRead, NULL, &ReadFileWrapper, argsRead);
else
pthread_create(&tRead, NULL, &RecvBufWrapper, argsRead);
unsigned long long *argsWrite = malloc(sizeof(unsigned long long) * 7);
argsWrite[0] = buf1;
argsWrite[1] = buf2;
argsWrite[2] = bufSize * 1024;
argsWrite[3] = &m1lockw;
argsWrite[4] = &m1lockr;
argsWrite[5] = &m2lockw;
argsWrite[6] = &m2lockr;
if (is_client)
pthread_create(&tWrite, NULL, &SendBufWrapper, argsWrite);
else
pthread_create(&tWrite, NULL, *WriteFileWrapper, argsWrite);
pthread_join(tRead, NULL);
pthread_join(tWrite, NULL);
//WriteFile(buf1, buf2, bufSize * 1024, &m1lock, &m2lock);
if (verbose_mode) {
printf("-----------SUMMARY-------------------\n");
printf("head_node\tfilename:%s\n", head_node->f_name);
struct f_linkedlist *temp;
temp = head_node;
while (temp != NULL) {
printf("name:%s\n", temp->f_name);
printf("size:%ld\n", temp->f_size);
printf("mode:%d\n", temp->f_mode);
temp = temp->next;
}
}
resources_destroy(res);
return 0;
}