-
Notifications
You must be signed in to change notification settings - Fork 1
/
simucached.cc
266 lines (215 loc) · 6.5 KB
/
simucached.cc
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
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
//#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "cmdline.h"
#include "log.h"
#include "simucached.h"
#include "thread.h"
#include "work.h"
#include <mtcp_api.h>
#include <mtcp_epoll.h>
#include "debug.h"
#define MAX_FLOW_NUM (10000)
#define RCVBUF_SIZE (2*1024)
#define SNDBUF_SIZE (8*1024)
#define MAX_EVENTS (MAX_FLOW_NUM * 3)
#define MAX_CPUS 12
#define HT_SUPPORT FALSE
/*----------------------------------------------------------------------------*/
struct thread_context
{
mctx_t mctx;
int efd;
};
/*----------------------------------------------------------------------------*/
static int num_cores;
static int core_limit;
//static pthread_t app_thread[MAX_CPUS];
Thread td[MAX_CPUS];
static int done[MAX_CPUS];
static int finished;
/*----------------------------------------------------------------------------*/
/* simucached
Model A
One listener, one epoll set per child.
The main thread responsibilities:
* Create epoll set for each child.
* Spawn children.
* Open socket for listening.
* Add new connections to children epoll sets, round-robin.
Child thread responsibilities:
* Wait for events from epoll set.
* Close hung-up connections, housekeep.
* Read from fds, parse commands, generate responses.
Model 2
One listener, one epoll set total.
Model 3
One listener, one epoll set per child, fds assigned to random pair of sets.
*/
using namespace std;
gengetopt_args_info args;
static int open_listen_socket(struct thread_context *ctx, int port) {
/*struct sockaddr_in sa;
int optval = 1;
int fd = mtcp_socket(ctx->mctx, AF_INET, SOCK_STREAM, 0);
if (fd < 0) DIE("socket() failed: %s", strerror(errno));
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))) //???
DIE("setsockopt(SO_REUSEADDR) failed: %s", strerror(errno));
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(port);
if (mtcp_bind(ctx->mctx, fd, (struct sockaddr *) &sa, sizeof(sa)) < 0)
DIE("bind(port=%d) failed: %s", port, strerror(errno));
if (mtcp_listen(ctx->mctx, fd, 1024) < 0)
DIE("listen() failed: %s", strerror(errno));
return fd;*/
int listener;
struct mtcp_epoll_event ev;
struct sockaddr_in saddr;
int ret;
/* create socket and set it as nonblocking */
listener = mtcp_socket(ctx->mctx, AF_INET, SOCK_STREAM, 0);
if (listener < 0) {
TRACE_ERROR("Failed to create listening socket!\n");
return -1;
}
ret = mtcp_setsock_nonblock(ctx->mctx, listener);
if (ret < 0) {
TRACE_ERROR("Failed to set socket in nonblocking mode.\n");
return -1;
}
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons(port);
ret = mtcp_bind(ctx->mctx, listener,
(struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if (ret < 0) {
TRACE_ERROR("Failed to bind to the listening socket!\n");
return -1;
}
/* listen (backlog: 4K) */
ret = mtcp_listen(ctx->mctx, listener, 4096);
if (ret < 0) {
TRACE_ERROR("mtcp_listen() failed!\n");
return -1;
}
/* wait for incoming accept events */
ev.events = MTCP_EPOLLIN;
ev.data.sockid = listener;
mtcp_epoll_ctl(ctx->mctx, ctx->ep, MTCP_EPOLL_CTL_ADD, listener, &ev);
return listener;
}
static void set_nonblocking(struct thread_context *ctx, int fd) {
int ret;
ret = mtcp_setsock_nonblock(ctx->mctx, fd);
if (ret < 0) {
TRACE_ERROR("Failed to set socket in nonblocking mode.\n");
return -1;
}
}
/*----------------------------------------------------------------------------*/
struct thread_context *
InitializeServerThread(int core)
{
struct thread_context *ctx;
/* affinitize application thread to a CPU core */
#if HT_SUPPORT
mtcp_core_affinitize(core + (num_cores / 2));
#else
mtcp_core_affinitize(core);
#endif /* HT_SUPPORT */
ctx = (struct thread_context *)calloc(1, sizeof(struct thread_context));
if (!ctx) {
TRACE_ERROR("Failed to create thread context!\n");
return NULL;
}
/* create mtcp context: this will spawn an mtcp thread */
ctx->mctx = mtcp_create_context(core);
if (!ctx->mctx) {
TRACE_ERROR("Failed to create mtcp context!\n");
return NULL;
}
/* create epoll descriptor */
ctx->ep = mtcp_epoll_create(ctx->mctx, MAX_EVENTS);
if (ctx->ep < 0) {
TRACE_ERROR("Failed to create epoll descriptor!\n");
return NULL;
}
return ctx;
}
/*----------------------------------------------------------------------------*/
void
SignalHandler(int signum)
{
int i;
for (i = 0; i < core_limit; i++) {
if (td[i]->pt == pthread_self()) {
//TRACE_INFO("Server thread %d got SIGINT\n", i);
done[i] = TRUE;
} else {
if (!done[i]) {
pthread_kill(td[i]->pt, signum);
}
}
}
}
/*----------------------------------------------------------------------------*/
int main(int argc, char **argv) {
int fd;
int ret;
int cores[MAX_CPUS];
int i;
num_cores = GetNumCPUs();
core_limit = args.threads_arg;
if (core_limit > num_cores) {
TRACE_CONFIG("CPU limit should be smaller than the "
"number of CPUS: %d\n", num_cores);
return FALSE;
}
if (cmdline_parser(argc, argv, &args) != 0) DIE("cmdline_parser failed");
for (unsigned int i = 0; i < args.verbose_given; i++)
log_level = (log_level_t) ((int) log_level - 1);
if (args.quiet_given) log_level = QUIET;
if (args.work_given && !args.calibration_arg) {
I("Calibrating busy-work loop.");
args.calibration_arg = work_per_sec(10000000);
I("calibration = %d", args.calibration_arg);
}
// signal(SIGPIPE, SIG_IGN); //??
ret = mtcp_init("epserver.conf");
if (ret) {
TRACE_ERROR("Failed to initialize mtcp\n");
exit(EXIT_FAILURE);
}
mtcp_register_signal(SIGINT, SignalHandler);
TRACE_INFO("Application initialization finished.\n");
V("%s v%s ready to roll",
CMDLINE_PARSER_PACKAGE_NAME, CMDLINE_PARSER_VERSION);
//Thread td[args.threads_arg];
for (int i = 0; i < core_limit; i++) {
cores[i] = i;
done[i] = FALSE;
td[i]->ctx = InitializeServerThread(core[i]);
//spawn_thread(&td[i]);
if (pthread_create(&td[i]->pt, NULL, thread_main, td))
DIE("pthread_create() failed: %s", strerror(errno));
}
for (i = 0; i < core_limit; i++) {
pthread_join(td[i]->pt, NULL);
}
mtcp_destroy();
return 0;
}