-
Notifications
You must be signed in to change notification settings - Fork 960
Expand file tree
/
Copy pathecho_client_fstack_epoll.c
More file actions
151 lines (135 loc) · 4.99 KB
/
Copy pathecho_client_fstack_epoll.c
File metadata and controls
151 lines (135 loc) · 4.99 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
/*
* F-Stack TCP client for issue #842 — ff_epoll version.
* Uses ff_epoll (EPOLLOUT → EPOLLIN via MOD) instead of kqueue native API.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include "ff_config.h"
#include "ff_api.h"
#include "ff_event.h"
#include "ff_epoll.h"
#define MAX_EVENTS 10
#define BUFFER_SIZE 4096
static int epfd;
static int sockfd;
static int request_sent = 0;
static int64_t br = 0;
static int64_t buffer_reads = 0;
static int64_t last_latency = 0;
static int64_t first_latency = 0;
static struct timespec t0;
static int64_t get_time_difference(const char *buffer, size_t bytes_read)
{
if (bytes_read < 20)
return -1;
int64_t d = strtoll(buffer + (bytes_read - 20), NULL, 10);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
int64_t now = (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
return now - d;
}
static int loop(void *arg)
{
struct epoll_event events[MAX_EVENTS];
int n = ff_epoll_wait(epfd, events, MAX_EVENTS, 0);
for (int i = 0; i < n; i++) {
if ((events[i].events & EPOLLOUT) && !request_sent) {
int sent = ff_send(sockfd,
"GET / HTTP/1.1\r\nHost: 9.134.211.87\r\nConnection: keep-alive\r\n\r\n", 62, 0);
if (sent > 0) {
request_sent = 1;
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = sockfd;
ff_epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
}
}
if ((events[i].events & EPOLLIN) && request_sent) {
char buffer[BUFFER_SIZE];
int bytes_read;
while ((bytes_read = ff_recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) {
br += bytes_read;
++buffer_reads;
buffer[bytes_read] = '\0';
last_latency = get_time_difference(buffer, bytes_read);
if (first_latency < 10 || first_latency > 10000000000LL)
first_latency = last_latency;
}
if (bytes_read == 0) {
struct timespec t1;
clock_gettime(CLOCK_MONOTONIC, &t1);
double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
float avg_bytes = buffer_reads > 0 ? (float)br / (float)buffer_reads : 0;
printf("avg_bytes: %.2f\n", avg_bytes);
printf("buffer_reads: %ld\n", buffer_reads);
printf("bytes_read: %ld\n", br);
printf("first_latency_ns: %ld\n", first_latency);
printf("last_latency_ns: %ld\n", last_latency);
printf("lat_diff_ns: %ld\n", last_latency - first_latency);
printf("total_time_s: %.3f\n", elapsed);
fflush(stdout);
ff_close(sockfd);
ff_close(epfd);
exit(0);
} else if (bytes_read == -1 && errno != EAGAIN) {
struct timespec t1;
clock_gettime(CLOCK_MONOTONIC, &t1);
double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
float avg_bytes = buffer_reads > 0 ? (float)br / (float)buffer_reads : 0;
printf("avg_bytes: %.2f\n", avg_bytes);
printf("buffer_reads: %ld\n", buffer_reads);
printf("bytes_read: %ld\n", br);
printf("first_latency_ns: %ld\n", first_latency);
printf("last_latency_ns: %ld\n", last_latency);
printf("lat_diff_ns: %ld\n", last_latency - first_latency);
printf("total_time_s: %.3f\n", elapsed);
printf("recv_error: %s\n", strerror(errno));
fflush(stdout);
ff_close(sockfd);
ff_close(epfd);
exit(1);
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
ff_init(argc, argv);
clock_gettime(CLOCK_MONOTONIC, &t0);
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) { perror("ff_socket"); exit(EXIT_FAILURE); }
int opt = 1;
ff_ioctl(sockfd, FIONBIO, &opt);
epfd = ff_epoll_create(10);
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
ev.data.fd = sockfd;
ff_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo("9.134.211.87", "12373", &hints, &res) != 0) {
perror("getaddrinfo"); exit(EXIT_FAILURE);
}
int rc = ff_connect(sockfd, (struct linux_sockaddr *)res->ai_addr, res->ai_addrlen);
if (rc == -1 && errno != EINPROGRESS) {
perror("ff_connect"); exit(EXIT_FAILURE);
}
freeaddrinfo(res);
ff_run(loop, NULL);
return 0;
}