-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlaunch.c
222 lines (175 loc) · 6.01 KB
/
launch.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
/*
* Copyright 2023 Regents of Nanjing University of Aeronautics and Astronautics and
* Hohai University, Miao Cai <[email protected]> and Junru Shen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* An easy-to-use FS launcher
*/
#define _GNU_SOURCE
#include <zookeeper/zookeeper.h>
#include <pthread.h>
#include <dlfcn.h>
#include <pthread.h>
#include <unistd.h>
#include <asm/unistd_64.h>
#include "ethanefs.h"
#include "ethane.h"
#include "bench.h"
#include "debug.h"
#include "coro.h"
#include "third_party/argparse/argparse.h"
#define STARTUP_SEC 16
typedef void (*worker_fn_t)(ethanefs_cli_t *cli);
static worker_fn_t get_worker_fn(const char *library_path) {
void *handle;
worker_fn_t worker_fn;
handle = dlopen(library_path, RTLD_LAZY);
if (unlikely(!handle)) {
pr_err("failed to load worker library: %s", dlerror());
exit(-1);
}
worker_fn = dlsym(handle, "worker_fn");
if (unlikely(!worker_fn)) {
pr_err("failed to load worker function: %s", dlerror());
exit(-1);
}
return worker_fn;
}
static ethanefs_cli_t *create_cli(ethanefs_t *fs, const char *cli_conf_path, unsigned uid, unsigned gid) {
ethanefs_cli_config_t *config;
ethanefs_cli_t *cli;
config = ethanefs_config_parse_cli(cli_conf_path);
if (unlikely(IS_ERR(config))) {
pr_err("failed to parse client config file: %s",
strerror((int) -PTR_ERR(config)));
exit(-1);
}
cli = ethanefs_cli_init(fs, config);
if (unlikely(IS_ERR(cli))) {
pr_err("failed to initialize client: %s",
strerror((int) -PTR_ERR(cli)));
exit(-1);
}
ethanefs_set_user(cli, uid, gid);
return cli;
}
struct run_arg {
ethanefs_t *fs;
const char *cli_conf_path;
unsigned uid, gid;
worker_fn_t fn;
int nr_coros;
zhandle_t *zh;
};
static void *run_worker(void *arg) {
struct run_arg *run_arg = arg;
ethanefs_cli_t **clis;
char thr_name[64];
int i;
coro_thread_init();
clis = malloc(sizeof(ethanefs_cli_t *) * run_arg->nr_coros);
if (unlikely(!clis)) {
pr_err("failed to allocate memory for clients");
exit(-1);
}
for (i = 0; i < run_arg->nr_coros; i++) {
pr_info("%ld: Creating client %d..", syscall(__NR_gettid), i);
clis[i] = create_cli(run_arg->fs, run_arg->cli_conf_path, run_arg->uid, run_arg->gid);
}
sprintf(thr_name, "ethane-wk-%d", ethanefs_get_cli_id(clis[0]));
pthread_setname_np(pthread_self(), thr_name);
ethanefs_post_ready(run_arg->zh);
ethanefs_wait_enable(run_arg->zh);
for (i = 0; i < run_arg->nr_coros; i++) {
coro_create(run_arg->fn, clis[i]);
}
coro_sched();
return NULL;
}
/* create client workers */
static void launch_clis(ethanefs_t *fs, zhandle_t *zh, const char *cli_conf_path, int nr_workers, int nr_coros,
unsigned uid, unsigned gid, worker_fn_t fn) {
struct run_arg *args;
pthread_t *workers;
int i;
workers = malloc(sizeof(pthread_t) * nr_workers);
if (unlikely(!workers)) {
pr_err("failed to allocate memory for workers");
exit(-1);
}
args = malloc(sizeof(struct run_arg) * nr_workers);
if (unlikely(!args)) {
pr_err("failed to allocate memory for workers");
exit(-1);
}
for (i = 0; i < nr_workers; i++) {
args[i].fs = fs;
args[i].cli_conf_path = cli_conf_path;
args[i].uid = uid;
args[i].gid = gid;
args[i].fn = fn;
args[i].nr_coros = nr_coros;
args[i].zh = zh;
pthread_create(&workers[i], NULL, run_worker, &args[i]);
}
for (i = 0; i < nr_workers; i++) {
pthread_join(workers[i], NULL);
}
}
int main(int argc, const char **argv) {
const char *zookeeper_ip = "localhost:2181";
const char *cli_config_path = NULL;
int nr_workers = 4, nr_coros = 4;
const char *library_path = NULL;
unsigned uid = 0, gid = 0;
worker_fn_t fn;
ethanefs_t *fs;
zhandle_t *zh;
struct argparse_option options[] = {
OPT_HELP(),
OPT_STRING('t', "cli-conf", &cli_config_path, "path to client config file"),
OPT_STRING('z', "zookeeper-ip", &zookeeper_ip, "zookeeper server ip (and port)"),
OPT_INTEGER('n', "nr-workers", &nr_workers, "number of worker threads"),
OPT_INTEGER('c', "nr-coros", &nr_coros, "number of coroutines per worker thread"),
OPT_INTEGER('u', "uid", &uid, "user id"),
OPT_INTEGER('g', "gid", &gid, "group id"),
OPT_STRING('l', "lib", &library_path, "worker library path"),
OPT_END(),
};
struct argparse argparse;
bench_timer_init_freq();
argparse_init(&argparse, options, NULL, 0);
argparse_describe(&argparse, "\nlogd", "\nETHANE Launcher");
argparse_parse(&argparse, argc, argv);
if (unlikely(!cli_config_path)) {
pr_err("client config file not specified");
return -1;
}
if (unlikely(!library_path)) {
pr_err("worker library not specified");
return -1;
}
fn = get_worker_fn(library_path);
zoo_set_debug_level(0);
zh = zookeeper_init(zookeeper_ip, NULL, 100000, NULL, NULL, 0);
if (unlikely(!zh)) {
pr_err("failed to connect to ZooKeeper server");
return -1;
}
reg_debug_sig_handler();
fs = ethanefs_init(zh, 8080);
launch_clis(fs, zh, cli_config_path, nr_workers, nr_coros, uid, gid, fn);
return 0;
}