-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.c
233 lines (185 loc) · 4.74 KB
/
init.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
/*
* init.c - initializes the runtime
*/
#include <pthread.h>
#include <base/cpu.h>
#include <base/init.h>
#include <base/log.h>
#include <base/limits.h>
#include <runtime/thread.h>
#include "defs.h"
static pthread_barrier_t init_barrier;
struct init_entry {
const char *name;
int (*init)(void);
};
static initializer_fn_t global_init_hook = NULL;
static initializer_fn_t perthread_init_hook = NULL;
static initializer_fn_t late_init_hook = NULL;
#define GLOBAL_INITIALIZER(name) \
{__cstr(name), &name ## _init}
/* global subsystem initialization */
static const struct init_entry global_init_handlers[] = {
/* runtime core */
GLOBAL_INITIALIZER(kthread),
GLOBAL_INITIALIZER(ioqueues),
GLOBAL_INITIALIZER(stack),
GLOBAL_INITIALIZER(sched),
GLOBAL_INITIALIZER(preempt),
GLOBAL_INITIALIZER(smalloc),
/* network stack */
GLOBAL_INITIALIZER(net),
GLOBAL_INITIALIZER(udp),
GLOBAL_INITIALIZER(directpath),
GLOBAL_INITIALIZER(arp),
GLOBAL_INITIALIZER(trans),
/* storage */
GLOBAL_INITIALIZER(storage),
#ifdef GC
GLOBAL_INITIALIZER(gc),
#endif
};
#define THREAD_INITIALIZER(name) \
{__cstr(name), &name ## _init_thread}
/* per-kthread subsystem initialization */
static const struct init_entry thread_init_handlers[] = {
/* runtime core */
THREAD_INITIALIZER(kthread),
THREAD_INITIALIZER(ioqueues),
THREAD_INITIALIZER(stack),
THREAD_INITIALIZER(sched),
THREAD_INITIALIZER(timer),
THREAD_INITIALIZER(smalloc),
/* network stack */
THREAD_INITIALIZER(net),
THREAD_INITIALIZER(directpath),
/* storage */
THREAD_INITIALIZER(storage),
};
#define LATE_INITIALIZER(name) \
{__cstr(name), &name ## _init_late}
static const struct init_entry late_init_handlers[] = {
/* network stack */
LATE_INITIALIZER(arp),
LATE_INITIALIZER(stat),
LATE_INITIALIZER(tcp),
LATE_INITIALIZER(rcu),
LATE_INITIALIZER(directpath),
};
static int run_init_handlers(const char *phase,
const struct init_entry *h, int nr)
{
int i, ret;
log_debug("entering '%s' init phase", phase);
for (i = 0; i < nr; i++) {
log_debug("init -> %s", h[i].name);
ret = h[i].init();
if (ret) {
log_debug("failed, ret = %d", ret);
return ret;
}
}
return 0;
}
static int runtime_init_thread(void)
{
int ret;
ret = base_init_thread();
if (ret) {
log_err("base library per-thread init failed, ret = %d", ret);
return ret;
}
ret = run_init_handlers("per-thread", thread_init_handlers,
ARRAY_SIZE(thread_init_handlers));
if (ret || perthread_init_hook == NULL)
return ret;
return perthread_init_hook();
}
static void *pthread_entry(void *data)
{
int ret;
ret = runtime_init_thread();
BUG_ON(ret);
pthread_barrier_wait(&init_barrier);
sched_start();
/* never reached unless things are broken */
BUG();
return NULL;
}
/**
* runtime_set_initializers - allow runtime to specifcy a function to run in
* each stage of intialization (called before runtime_init).
*/
int runtime_set_initializers(initializer_fn_t global_fn,
initializer_fn_t perthread_fn,
initializer_fn_t late_fn)
{
global_init_hook = global_fn;
perthread_init_hook = perthread_fn;
late_init_hook = late_fn;
return 0;
}
/**
* runtime_init - starts the runtime
* @cfgpath: the path to the configuration file
* @main_fn: the first function to run as a thread
* @arg: an argument to @main_fn
*
* Does not return if successful, otherwise return < 0 if an error.
*/
int runtime_init(const char *cfgpath, thread_fn_t main_fn, void *arg)
{
pthread_t tid[NCPU];
int ret, i;
ret = base_init();
if (ret) {
log_err("base library global init failed, ret = %d", ret);
return ret;
}
ret = cfg_load(cfgpath);
if (ret)
return ret;
log_info("process pid: %u", getpid());
pthread_barrier_init(&init_barrier, NULL, maxks);
ret = run_init_handlers("global", global_init_handlers,
ARRAY_SIZE(global_init_handlers));
if (ret)
return ret;
if (global_init_hook) {
ret = global_init_hook();
if (ret) {
log_err("User-specificed global initializer failed, ret = %d", ret);
return ret;
}
}
ret = runtime_init_thread();
BUG_ON(ret);
log_info("spawning %d kthreads", maxks);
for (i = 1; i < maxks; i++) {
ret = pthread_create(&tid[i], NULL, pthread_entry, NULL);
BUG_ON(ret);
}
pthread_barrier_wait(&init_barrier);
ret = ioqueues_register_iokernel();
if (ret) {
log_err("couldn't register with iokernel, ret = %d", ret);
return ret;
}
/* point of no return starts here */
ret = thread_spawn_main(main_fn, arg);
BUG_ON(ret);
ret = run_init_handlers("late", late_init_handlers,
ARRAY_SIZE(late_init_handlers));
BUG_ON(ret);
if (late_init_hook) {
ret = late_init_hook();
if (ret) {
log_err("User-specificed late initializer failed, ret = %d", ret);
return ret;
}
}
sched_start();
/* never reached unless things are broken */
BUG();
return 0;
}