This repository was archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathreuse_thread_svr.c
161 lines (128 loc) · 3.65 KB
/
reuse_thread_svr.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <sched.h>
#include <linux/filter.h>
#include <evhtp/thread.h>
#include <evhtp/evhtp.h>
#ifndef SO_ATTACH_REUSEPORT_CBPF
#define SO_ATTACH_REUSEPORT_CBPF 51
#endif
#define CPU__COUNT sysconf(_SC_NPROCESSORS_ONLN)
static int _init = 0;
static int _threads = 0;
#define _valid_response(REQ, TYPE) \
(REQ)->method == htp_method_ ## TYPE ? \
EVHTP_RES_OK : EVHTP_RES_400
static void
on_request_index(evhtp_request_t * req, void * _)
{
return evhtp_send_reply(req, _valid_response(req, GET));
}
static void
on_request_user_register(evhtp_request_t * req, void * _)
{
return evhtp_send_reply(req, _valid_response(req, POST));
}
#define upm_start uri->path->match_start
#define upm_eoff uri->path->matched_eoff
#define upm_soff uri->path->matched_soff
static void
on_request_user_index(evhtp_request_t * req, void * _)
{
if (req->method == htp_method_GET) {
evbuffer_add_reference(req->buffer_out,
req->upm_start,
req->upm_eoff - req->upm_soff, NULL, NULL);
return evhtp_send_reply(req, EVHTP_RES_OK);
}
return evhtp_send_reply(req, EVHTP_RES_400);
}
static void
dummy_eventcb_(int sock, short which, void * args)
{
(void)sock;
(void)which;
(void)args;
}
static void
attach_cbpf_(int fd)
{
struct sock_filter code[] = {
{ BPF_LD | BPF_W | BPF_ABS, 0, 0,
(__u32)(SKF_AD_OFF + SKF_AD_CPU) }, /* A = raw_smp_processor_id() */
{ BPF_RET | BPF_A, 0, 0,0}, /* return A */
};
struct sock_fprog p = {
.len = 2,
.filter = code,
};
if (setsockopt(fd, SOL_SOCKET,
SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)) == -1) {
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
static void
htp_worker_init_(evthr_t * thread, void * args)
{
struct event_base * evbase;
struct evhtp * htp;
int core;
cpu_set_t cpu_set;
if (!(evbase = evthr_get_base(thread))) {
evthr_stop(thread);
return;
}
if (!(htp = evhtp_new(evbase, thread))) {
evthr_stop(thread);
return;
}
evhtp_set_regex_cb(htp, "^/user/([^/]+)", on_request_user_index, NULL);
evhtp_set_cb(htp, "/user", on_request_user_register, NULL);
evhtp_set_cb(htp, "/", on_request_index, NULL);
core = _threads++ % CPU__COUNT;
CPU_ZERO(&cpu_set);
CPU_SET(core, &cpu_set);
pthread_setaffinity_np(pthread_self(),
sizeof(cpu_set_t), &cpu_set);
#if 0
if (pthread_getaffinity_np(pthread_self(),
sizeof(cpu_set_t), &cpu_set) == 0) {
int i;
for (i = 0; i < CPU__COUNT; i++) {
if (CPU_ISSET(i, &cpu_set)) {
}
}
}
#endif
evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_ALL);
evhtp_bind_socket(htp, "0.0.0.0", 3000, 1024);
attach_cbpf_(evconnlistener_get_fd(htp->server));
} /* htp_worker_init_ */
int
main(int argc, char ** argv)
{
struct event_base * evbase;
struct event * dummy_ev;
evthr_pool_t * workers;
evbase = event_base_new();
dummy_ev = event_new(evbase, -1, EV_READ | EV_PERSIST,
dummy_eventcb_, NULL);
event_add(dummy_ev, NULL);
if (!(workers = evthr_pool_wexit_new(CPU__COUNT,
htp_worker_init_, NULL, NULL))) {
exit(EXIT_FAILURE);
}
evthr_pool_start(workers);
event_base_loop(evbase, 0);
return 0;
}