-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.cpp
More file actions
161 lines (132 loc) · 3.84 KB
/
common.cpp
File metadata and controls
161 lines (132 loc) · 3.84 KB
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
#include <cstdio>
#include <iostream>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include "common.h"
EPollRSelector::EPollRSelector(int sock_count) {
#ifdef EPOLL_CALL_STATS
sock_activation_count = 0;
wait_count = 0;
#endif
efd = epoll_create1(0);
if (-1 == efd) {
perror("epoll_create");
}
events.events.resize(sock_count);
current_ready = end_of_ready = events.events.begin();
}
EPollRSelector::EPollRSelector(EPollRSelector && rsel) {
efd = rsel.efd;
rsel.efd = -1;
#ifdef EPOLL_CALL_STATS
sock_activation_count = rsel.sock_activation_count;
wait_count = rsel.wait_count;
#endif
current_ready = end_of_ready = events.events.begin();
}
EPollRSelector::~EPollRSelector() {
#ifdef EPOLL_CALL_STATS
if (0 != wait_count) {
std::cout << "Avg socket per wait = ";
std::cout << sock_activation_count / wait_count << "\n";
}
#endif
if (-1 != efd)
close(efd);
}
bool EPollRSelector::add_fd(int sockfd, int event_mask) {
epoll_event event;
event.data.fd = sockfd;
event.events = event_mask;
if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, sockfd, &event)) {
perror("epoll_ctl");
return false;
}
return true;
}
bool EPollRSelector::wait(long int timeout_ns) {
if (not epoll_wait_ex(efd, events, timeout_ns))
return false;
current_ready = events.events.begin();
end_of_ready = current_ready + events.num_ready;
#ifdef EPOLL_CALL_STATS
wait_count += 1;
sock_activation_count += n;
#endif
return true;
}
bool EPollRSelector::next(int & sockfd, uint32_t & flags) {
if(end_of_ready == current_ready)
return false;
sockfd = current_ready->data.fd;
flags = current_ready->events;
++current_ready;
return true;
}
bool EPollRSelector::next(int & sockfd) {
if(end_of_ready == current_ready)
return false;
sockfd = current_ready->data.fd;
++current_ready;
return true;
}
void EPollRSelector::remove_current_ready() {
epoll_ctl(efd, EPOLL_CTL_DEL, (current_ready - 1)->data.fd, nullptr);
}
int EPollRSelector::ready_count() const {
return end_of_ready - current_ready;
}
// epoll_wait support timeout only with ms granularity
// while we need at least us presicion
bool epoll_wait_ex(int epollfd,
EventsList & ready,
const long int timeout_ns)
{
bool already_polled = false;
ready.num_ready = 0;
auto curr_time = get_fast_time();
auto return_time = curr_time + timeout_ns;
for(;;) {
auto time_left = (long int)return_time - (long int)curr_time;
if (time_left <= 0 && timeout_ns != -1) {
if (already_polled)
return true;
else
time_left = 0;
}
auto poll_timeout = (timeout_ns == -1 ? -1 : time_left / 1000000);
ready.num_ready = epoll_wait(epollfd,
&(ready.events[0]),
ready.events.size(),
poll_timeout);
already_polled = true;
curr_time = get_fast_time();
if (ready.num_ready == 0) {
if (curr_time >= return_time)
return true;
continue;
} else if ( 0 > ready.num_ready ) {
if (errno == EINTR) {
ready.num_ready = 0;
continue;
} else {
perror("epoll_wait failed");
return false;
}
}
#ifdef EPOLL_CALL_STATS
if (0 != ready.num_ready) {
socket_count_from_wait += ready.num_ready;
epoll_wait_calls += 1;
}
#endif
ready.recv_time = curr_time;
return true;
}
}