|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <string.h> |
| 5 | +#include <stdarg.h> |
| 6 | + |
| 7 | +#include <sys/timerfd.h> |
| 8 | +#include <sys/epoll.h> |
| 9 | +#include <sys/types.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <netdb.h> |
| 12 | +#include <errno.h> |
| 13 | + |
| 14 | +#ifdef __cplusplus |
| 15 | +extern "C" { |
| 16 | +#endif |
| 17 | + |
| 18 | +#define NUM_SOCKETS 10 |
| 19 | +uint64_t listen_socket_epoll_data = 0; |
| 20 | +epoll_event ready_events[NUM_SOCKETS] = {}; |
| 21 | +static int accepted_sockets = 0; |
| 22 | + |
| 23 | +int __wrap_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { |
| 24 | + |
| 25 | + // the listen socket |
| 26 | + if (fd == 500) { |
| 27 | + listen_socket_epoll_data = event->data.u64; |
| 28 | + return 0; |
| 29 | + } else { |
| 30 | + if (fd < 500) { |
| 31 | + |
| 32 | + } else { |
| 33 | + // on our FDs |
| 34 | + ready_events[fd - 500 - 1].data.u64 = event->data.u64; |
| 35 | + ready_events[fd - 500 - 1].events = EPOLLIN; |
| 36 | + } |
| 37 | + |
| 38 | + return 0; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +int __wrap_epoll_wait(int epfd, struct epoll_event *events, |
| 43 | + int maxevents, int timeout) { |
| 44 | + |
| 45 | + if (accepted_sockets != NUM_SOCKETS) { |
| 46 | + events[0].events = EPOLLIN; |
| 47 | + events[0].data.u64 = listen_socket_epoll_data; |
| 48 | + return 1; |
| 49 | + } else { |
| 50 | + for (int i = 0; i < NUM_SOCKETS; i++) { |
| 51 | + events[i] = ready_events[i]; |
| 52 | + } |
| 53 | + return NUM_SOCKETS; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +int __wrap_recv(int sockfd, void *buf, size_t len, int flags) { |
| 58 | + const char request[] = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"; |
| 59 | + memcpy(buf, request, sizeof(request) - 1); |
| 60 | + return sizeof(request) - 1; |
| 61 | +} |
| 62 | + |
| 63 | +int __wrap_send(int sockfd, const void *buf, size_t len, int flags) { |
| 64 | + static int sent = 0; |
| 65 | + static clock_t lastTime = clock(); |
| 66 | + if (++sent == 1000000) { |
| 67 | + // print how long it took to make 1 million requests |
| 68 | + clock_t newTime = clock(); |
| 69 | + float elapsed = float(newTime - lastTime) / CLOCKS_PER_SEC; |
| 70 | + printf("Req/sec: %f\n", 1000000.0f / elapsed); |
| 71 | + sent = 0; |
| 72 | + lastTime = newTime; |
| 73 | + } |
| 74 | + |
| 75 | + return len; |
| 76 | +} |
| 77 | + |
| 78 | +int __wrap_bind() { |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +int __wrap_setsockopt() { |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int __wrap_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { |
| 87 | + if (accepted_sockets < NUM_SOCKETS) { |
| 88 | + accepted_sockets++; |
| 89 | + return accepted_sockets + 500; |
| 90 | + } else { |
| 91 | + return -1; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +int __wrap_listen() { |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +int __wrap_socket(int domain, int type, int protocol) { |
| 100 | + return 500; |
| 101 | +} |
| 102 | + |
| 103 | +#ifdef __cplusplus |
| 104 | +} |
| 105 | +#endif |
0 commit comments