ppd/dsmbr/dsmbr.cc

969 lines
25 KiB
C++
Raw Normal View History

2023-03-11 02:09:28 +00:00
#include <sys/types.h>
2023-01-17 20:22:43 +00:00
#include <sys/param.h>
#include <sys/cpuset.h>
2023-01-05 16:17:48 +00:00
#include <sys/event.h>
2023-03-11 02:09:28 +00:00
#include <sys/queue.h>
#include <sys/socket.h>
2023-01-05 16:17:48 +00:00
#include <netinet/in.h>
#include <netinet/tcp.h>
2023-03-11 02:09:28 +00:00
2023-01-05 16:17:48 +00:00
#include <arpa/inet.h>
2023-03-11 02:09:28 +00:00
#include <bsock/bsock.h>
2023-01-05 16:17:48 +00:00
#include <netdb.h>
2023-03-11 02:09:28 +00:00
#include <openssl/err.h>
#include <openssl/ssl.h>
2023-01-17 20:22:43 +00:00
#include <pthread.h>
#include <pthread_np.h>
2023-03-11 02:09:28 +00:00
#include <semaphore.h>
#include <unistd.h>
2023-01-05 16:17:48 +00:00
2023-03-13 08:49:28 +00:00
#include "dmsg.hh"
#include "generator.hh"
#include "io.hh"
2023-01-17 20:22:43 +00:00
#include "logger.h"
#include "mod.h"
2023-03-13 08:49:28 +00:00
#include "msg.hh"
2023-03-11 02:09:28 +00:00
#include "util.h"
#include <atomic>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <set>
2023-03-13 07:32:55 +00:00
#include <sstream>
2023-03-11 02:09:28 +00:00
#include <thread>
#include <unordered_map>
#include <vector>
2023-01-17 20:22:43 +00:00
2023-03-13 08:49:28 +00:00
LOGGER_VAR_DECL;
2023-01-17 20:22:43 +00:00
static constexpr int MAX_MOD_ARGS = 32;
static constexpr int MAX_MOD_ARG_LEN = 128;
static constexpr int MAX_SLAVES = 32;
static constexpr int NEVENT = 64;
2023-03-14 13:30:03 +00:00
static constexpr int CTRL_PORT = 15545;
2023-03-02 14:12:35 +00:00
static constexpr int CTRL_TIMEOUT = 3;
static constexpr int CTRL_BACKLOG = 4096;
static constexpr int BSOCK_BUF_SZ = 4096;
2023-03-11 02:09:28 +00:00
static constexpr int THRD_INIT_PARALLEL = 2;
static constexpr int TIMER_MAGIC = 0x123456;
2023-03-08 02:46:23 +00:00
static constexpr int STATE_WAITING = 0;
static constexpr int STATE_RUN = 1;
static constexpr int STATE_STOP = 2;
2023-01-17 20:22:43 +00:00
struct dsmbr_thread_ctx {
int tid;
2023-03-11 02:09:28 +00:00
int core;
std::list<struct dsmbr_request_record *> stats;
2023-01-17 20:22:43 +00:00
pthread_t thrd;
};
struct dsmbr_request_record {
uint64_t recv_size;
uint64_t send_size;
uint64_t send_ts;
uint64_t recv_ts;
};
2023-01-05 16:17:48 +00:00
2023-01-17 20:22:43 +00:00
struct dsmbr_conn {
Generator *ia_gen;
2023-03-11 02:09:28 +00:00
SSL *ssl;
char *ssl_readbuf;
2023-01-17 20:22:43 +00:00
int conn_fd;
2023-03-11 02:09:28 +00:00
struct bsock *bsock;
2023-03-02 14:12:35 +00:00
struct ppd_bsock_io_ssl_ctx bsock_ctx;
2023-03-08 02:46:23 +00:00
2023-03-11 02:09:28 +00:00
void *m_ctx;
2023-01-17 20:22:43 +00:00
uint64_t next_send;
std::list<struct dsmbr_request_record *> req_in_flight;
};
2023-01-05 16:17:48 +00:00
2023-03-02 14:12:35 +00:00
struct dsmbr_slave {
int conn_fd;
/* stats */
2023-03-11 02:09:28 +00:00
uint64_t time;
uint64_t send_sz;
uint64_t recv_sz;
uint64_t reqs;
2023-03-02 14:12:35 +00:00
};
2023-01-17 20:22:43 +00:00
struct dsmbr_options {
int verbose;
char server_ip[64];
int server_port;
int conn_per_thread;
int target_qps;
int depth;
int warmup;
int duration;
char ia_dist[128];
char output_file[128];
cpuset_t cpuset;
char module_path[128];
2023-03-13 07:32:55 +00:00
char *mod_argk[MAX_MOD_ARGS];
char *mod_argv[MAX_MOD_ARGS];
2023-01-17 20:22:43 +00:00
int mod_argc;
int enable_tls;
2023-03-11 02:09:28 +00:00
int master_mode;
int slave_mode;
2023-01-17 20:22:43 +00:00
// master mode
2023-03-11 02:09:28 +00:00
char slave_ips[MAX_SLAVES][64];
2023-01-17 20:22:43 +00:00
int num_slaves;
2023-03-11 02:09:28 +00:00
2023-01-17 20:22:43 +00:00
// global states
2023-03-08 02:46:23 +00:00
std::atomic<int> state = STATE_WAITING;
2023-03-11 02:09:28 +00:00
sem_t worker_init_sem;
std::atomic<int> worker_init_comp = 0;
SSL_CTX *ssl_ctx;
ppd_mod_info *m_info;
void *m_global_ctx;
2023-01-05 16:17:48 +00:00
};
2023-03-11 02:09:28 +00:00
static struct dsmbr_options options = { .verbose = 0,
.server_ip = "127.0.0.1",
.conn_per_thread = 1,
.target_qps = 0,
.depth = 1,
.warmup = 0,
.duration = 5,
.ia_dist = "fb_ia",
.output_file = "samples.txt",
.cpuset = CPUSET_T_INITIALIZER(1),
.module_path = { 0 },
.mod_argc = 0,
.enable_tls = 0,
.master_mode = 0,
.slave_mode = 0,
.num_slaves = 0,
.ssl_ctx = nullptr,
.m_info = nullptr,
.m_global_ctx = nullptr };
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
static void
2023-01-17 20:22:43 +00:00
dsmbr_dump_options()
{
2023-03-13 07:32:55 +00:00
std::stringstream ss;
ss << "Configuration:\n"
<< " server: " << options.server_ip << std::endl
<< " port: " << options.server_port << std::endl
<< " connections per thread: " << options.conn_per_thread << std::endl
<< " enable TLS: " << options.enable_tls << std::endl
<< " target qps: " << options.target_qps << std::endl
<< " number of threads: " << CPU_COUNT(&options.cpuset) << std::endl
<< " verbose: " << options.verbose << std::endl
<< " master mode: " << options.master_mode << std::endl
<< " slave mode:" << options.slave_mode << std::endl
<< " module: " << options.module_path << std::endl
<< " run time: " << options.duration << std::endl
<< " warmup time: " << options.warmup << std::endl
<< " interarrival dist: " << options.ia_dist << std::endl
<< " output file: " << options.output_file << std::endl
<< " depth: " << options.depth << std::endl
<< " slaves:";
for (int i = 0; i < options.num_slaves; i++) {
ss << " " << options.slave_ips[i];
}
ss << std::endl;
V("%s", ss.str().c_str());
2023-01-17 20:22:43 +00:00
}
2023-01-05 16:17:48 +00:00
2023-01-17 20:22:43 +00:00
static SSL *
dsmbr_tls_handshake_client(int conn_fd)
2023-01-05 16:17:48 +00:00
{
2023-01-17 20:22:43 +00:00
SSL *ssl;
int r;
2023-01-05 16:17:48 +00:00
2023-01-17 20:22:43 +00:00
ssl = SSL_new(options.ssl_ctx);
if (ssl == NULL) {
E("SSL_new() failed: %ld\n", ERR_get_error());
}
2023-01-05 16:17:48 +00:00
2023-01-17 20:22:43 +00:00
if (SSL_set_fd(ssl, conn_fd) == 0) {
E("SSL_set_fd() failed: %ld\n", ERR_get_error());
2023-01-05 16:17:48 +00:00
}
2023-01-17 20:22:43 +00:00
if ((r = SSL_connect(ssl)) <= 0) {
E("SSL_connect() failed: %ld\n", ERR_get_error());
}
2023-03-02 14:12:35 +00:00
return ssl;
}
static void
dsmbr_create_tls_context()
{
SSL_CTX *ctx;
ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) {
E("SSL_CTX_new() failed: %ld\n", ERR_get_error());
2023-01-17 20:22:43 +00:00
}
2023-03-02 14:12:35 +00:00
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION);
options.ssl_ctx = ctx;
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
static struct dsmbr_conn *
dsmbr_conn_create(void *thrd_ctx)
2023-01-05 16:17:48 +00:00
{
2023-01-17 20:22:43 +00:00
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(options.server_port);
server_addr.sin_addr.s_addr = inet_addr(options.server_ip);
2023-03-11 02:09:28 +00:00
2023-01-17 20:22:43 +00:00
int enable = 1;
2023-03-02 14:12:35 +00:00
struct timeval tv = { .tv_sec = CTRL_TIMEOUT, .tv_usec = 0 };
2023-01-17 20:22:43 +00:00
int conn_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (conn_fd == -1) {
E("socket() error: %d\n", errno);
}
2023-03-11 02:09:28 +00:00
if (setsockopt(conn_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv)) != 0) {
2023-01-17 20:22:43 +00:00
E("setsockopt() rcvtimeo: %d\n", errno);
2023-03-11 02:09:28 +00:00
}
2023-01-17 20:22:43 +00:00
if (setsockopt(conn_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) != 0) {
E("setsockopt() reuseaddr: %d\n", errno);
}
if (setsockopt(conn_fd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable)) != 0) {
E("setsockopt() reuseport: %d\n", errno);
}
if (setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)) != 0) {
E("setsockopt() nodelay: %d\n", errno);
}
2023-03-11 02:09:28 +00:00
if (connect(conn_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) {
2023-01-17 20:22:43 +00:00
E("connect() failed: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-02 14:12:35 +00:00
2023-03-13 07:32:55 +00:00
V("Established client connection %d...\n", conn_fd);
2023-03-11 02:09:28 +00:00
struct dsmbr_conn *conn = new struct dsmbr_conn;
2023-01-17 20:22:43 +00:00
conn->conn_fd = conn_fd;
2023-03-02 14:12:35 +00:00
struct bsock_ringbuf_io io;
2023-03-11 02:09:28 +00:00
void *bsock_ctx = nullptr;
2023-03-02 14:12:35 +00:00
if (options.enable_tls) {
2023-03-13 07:32:55 +00:00
V("Initiating TLS handshake on connection %d...\n", conn_fd);
2023-03-02 14:12:35 +00:00
conn->ssl = dsmbr_tls_handshake_client(conn_fd);
io = ppd_bsock_io_ssl();
conn->ssl_readbuf = new char[BSOCK_BUF_SZ];
conn->bsock_ctx.ssl_readbuf = conn->ssl_readbuf;
conn->bsock_ctx.ssl_readbuf_len = BSOCK_BUF_SZ;
conn->bsock_ctx.ssl = conn->ssl;
bsock_ctx = &conn->bsock_ctx;
} else {
io = bsock_io_posix();
conn->ssl_readbuf = nullptr;
conn->ssl = nullptr;
bsock_ctx = (void *)(uintptr_t)conn_fd;
}
2023-03-11 02:09:28 +00:00
2023-03-12 13:13:10 +00:00
conn->bsock = bsock_create(bsock_ctx, &io, BSOCK_BUF_SZ, 0);
2023-03-02 14:12:35 +00:00
2023-01-17 20:22:43 +00:00
conn->ia_gen = createGenerator(options.ia_dist);
if (conn->ia_gen == NULL) {
E("Failed to create generator \"%s\"\n", options.ia_dist);
}
2023-03-11 02:09:28 +00:00
conn->ia_gen->set_lambda((double)options.target_qps /
(double)(options.conn_per_thread * CPU_COUNT(&options.cpuset)));
2023-01-17 20:22:43 +00:00
int status;
2023-03-13 07:32:55 +00:00
if ((status = options.m_info->conn_init_cb(options.m_global_ctx, thrd_ctx, &conn->m_ctx)) !=
0) {
2023-01-17 20:22:43 +00:00
E("Failed to create conn m_ctx: %d\n", status);
}
2023-03-08 02:46:23 +00:00
conn->next_send = 0;
2023-01-17 20:22:43 +00:00
return conn;
}
static void
2023-03-11 02:09:28 +00:00
dsmbr_conn_free(struct dsmbr_conn *conn, void *thrd_ctx)
2023-01-17 20:22:43 +00:00
{
if (conn->ssl != nullptr) {
SSL_shutdown(conn->ssl);
SSL_free(conn->ssl);
2023-03-02 14:12:35 +00:00
delete[] conn->ssl_readbuf;
2023-01-17 20:22:43 +00:00
}
2023-03-13 07:32:55 +00:00
options.m_info->conn_free_cb(options.m_global_ctx, thrd_ctx, conn->m_ctx);
2023-01-17 20:22:43 +00:00
2023-03-02 14:12:35 +00:00
bsock_free(conn->bsock);
2023-01-17 20:22:43 +00:00
close(conn->conn_fd);
delete conn->ia_gen;
2023-01-05 16:17:48 +00:00
delete conn;
}
2023-03-08 02:46:23 +00:00
void *
2023-03-11 02:09:28 +00:00
dsmbr_worker_main(void *ctx)
2023-01-17 20:22:43 +00:00
{
2023-03-11 02:09:28 +00:00
struct dsmbr_thread_ctx *tinfo = (struct dsmbr_thread_ctx *)ctx;
struct timespec timesp = { .tv_sec = 0, .tv_nsec = 0 };
2023-03-08 02:46:23 +00:00
struct kevent kevs[NEVENT];
2023-03-11 02:09:28 +00:00
std::vector<struct dsmbr_conn *> conns;
sem_wait(&options.worker_init_sem);
V("Thread %d initializing...\n", tinfo->tid);
int kqfd = kqueue();
2023-03-13 07:32:55 +00:00
if (kqfd < 0) {
2023-03-11 02:09:28 +00:00
E("Thread %d kqueue() failed: %d\n", tinfo->tid, errno);
}
struct ppd_msg *mbuf = (struct ppd_msg *)new char[PPD_MSG_MAX_SZ];
void *m_ctx;
2023-03-13 07:32:55 +00:00
if (options.m_info->thread_init_cb(tinfo->core, options.m_global_ctx, &m_ctx) != 0) {
2023-03-11 02:09:28 +00:00
E("thread_create_cb() failed: %d\n", errno);
}
for (int i = 0; i < options.conn_per_thread; i++) {
struct dsmbr_conn *conn = dsmbr_conn_create(m_ctx);
conns.push_back(conn);
2023-01-17 20:22:43 +00:00
2023-03-11 02:09:28 +00:00
struct kevent kev;
EV_SET(&kev, conn->conn_fd, EVFILT_READ, EV_ADD, 0, 0, conn);
if (kevent(kqfd, &kev, 1, NULL, 0, NULL) == -1) {
E("kevent() failed: %d\n", errno);
}
}
options.worker_init_comp.fetch_add(1, std::memory_order_relaxed);
sem_post(&options.worker_init_sem);
// create connections
2023-03-13 07:32:55 +00:00
V("Thread %d waiting...\n", tinfo->tid);
2023-03-11 02:09:28 +00:00
while (options.state.load(std::memory_order_relaxed) == STATE_WAITING) {
2023-03-08 02:46:23 +00:00
};
2023-03-11 02:09:28 +00:00
for (struct dsmbr_conn *conn : conns) {
2023-03-08 02:46:23 +00:00
conn->next_send = get_time_us();
2023-01-17 20:22:43 +00:00
}
2023-03-08 02:46:23 +00:00
V("Thread %d starting...\n", tinfo->tid);
2023-03-11 02:09:28 +00:00
while (true) {
2023-03-08 02:46:23 +00:00
int ret = 0;
2023-01-17 20:22:43 +00:00
2023-03-08 02:46:23 +00:00
if (options.state.load(std::memory_order_relaxed) == STATE_STOP) {
2023-01-17 20:22:43 +00:00
break;
2023-01-05 16:17:48 +00:00
}
2023-03-08 02:46:23 +00:00
// read resp first if possible
2023-03-11 02:09:28 +00:00
if ((ret = kevent(kqfd, NULL, 0, kevs, NEVENT, &timesp)) < 0) {
2023-03-08 02:46:23 +00:00
E("Thread %d kevent() error %d.\n", tinfo->tid, errno);
2023-01-05 16:17:48 +00:00
}
2023-03-08 02:46:23 +00:00
int size = ret;
for (int i = 0; i < size; i++) {
2023-03-11 02:09:28 +00:00
struct dsmbr_conn *conn = (struct dsmbr_conn *)kevs[i].udata;
2023-03-08 02:46:23 +00:00
if (kevs[i].flags & EV_EOF) {
2023-03-11 02:09:28 +00:00
E("Thread %d connection %d EV_EOF ERR: %d.\n", tinfo->tid,
conn->conn_fd, kevs[i].fflags);
2023-03-08 02:46:23 +00:00
}
ret = bsock_poll(conn->bsock);
if (ret < 0) {
2023-03-11 02:09:28 +00:00
E("Thread %d connection %d bsock_poll() ERR %d.\n", tinfo->tid,
conn->conn_fd, ret);
2023-01-05 16:17:48 +00:00
}
}
2023-03-08 02:46:23 +00:00
uint64_t cur_ts = get_time_us();
2023-03-11 02:09:28 +00:00
for (struct dsmbr_conn *conn : conns) {
while (true) {
// read as many messages as possible
ret = ppd_readmsg(conn->bsock, (char *)mbuf, PPD_MSG_MAX_SZ);
if (ret == 0) {
2023-03-13 07:32:55 +00:00
size_t recv_size = mbuf->size + PPD_MSG_HDR_SZ;
2023-03-11 02:09:28 +00:00
ret = options.m_info->conn_recv_cb(mbuf->data, mbuf->size,
options.m_global_ctx, m_ctx, conn->m_ctx);
if (ret != 0) {
E("Thread %d connection %d conn_recv_cb() failed ERR %d.\n",
tinfo->tid, conn->conn_fd, errno);
}
struct dsmbr_request_record *rec =
conn->req_in_flight.front();
conn->req_in_flight.pop_front();
rec->recv_size = recv_size;
rec->recv_ts = cur_ts;
tinfo->stats.push_back(rec);
2023-03-13 08:08:44 +00:00
D("Thread %d connection %d RECV msg of size %u latency %lu\n",
2023-03-13 07:32:55 +00:00
tinfo->tid, conn->conn_fd, mbuf->size,
rec->recv_ts - rec->send_ts);
2023-03-11 02:09:28 +00:00
} else {
if (errno == ENODATA) {
// not enough data for next msg
break;
} else {
E("ppd_readmsg failed with err %d\n", errno);
}
2023-03-08 02:46:23 +00:00
}
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
if (conn->next_send <= cur_ts &&
conn->req_in_flight.size() < (unsigned int)options.depth) {
2023-03-08 02:46:23 +00:00
// send
size_t out_sz;
2023-03-11 02:09:28 +00:00
ret = options.m_info->conn_send_cb(mbuf->data, PPD_MSG_MAX_DATA_SZ,
&out_sz, options.m_global_ctx, m_ctx, conn->m_ctx);
2023-03-08 02:46:23 +00:00
if (ret != 0) {
2023-03-11 02:09:28 +00:00
E("Thread %d connection %d conn_send_cb() failed ERR %d.\n",
tinfo->tid, conn->conn_fd, errno);
2023-03-08 02:46:23 +00:00
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
mbuf->size = out_sz;
struct dsmbr_request_record *rec = new struct dsmbr_request_record;
rec->send_size = mbuf->size + sizeof(struct ppd_msg);
rec->send_ts = get_time_us();
2023-03-08 02:46:23 +00:00
// no blocking, always flush
2023-03-11 02:09:28 +00:00
ret = ppd_writemsg(conn->bsock, mbuf);
2023-03-08 02:46:23 +00:00
if (ret != 0) {
2023-03-11 02:09:28 +00:00
E("Thread %d connection %d ppd_writemsg() failed ERR %d.\n",
tinfo->tid, conn->conn_fd, errno);
2023-03-08 02:46:23 +00:00
}
2023-03-11 02:09:28 +00:00
conn->req_in_flight.push_back(rec);
conn->next_send += (uint64_t)(conn->ia_gen->generate() *
(double)S2US);
2023-03-13 08:08:44 +00:00
D("Thread %d connection %d SEND msg of size %lu\n", tinfo->tid,
2023-03-13 07:32:55 +00:00
conn->conn_fd, out_sz);
2023-01-05 16:17:48 +00:00
}
}
}
2023-03-08 02:46:23 +00:00
V("Thread %d exiting...\n", tinfo->tid);
2023-03-11 02:09:28 +00:00
close(kqfd);
delete[] mbuf;
for (unsigned int i = 0; i < conns.size(); i++) {
dsmbr_conn_free(conns.at(i), m_ctx);
}
2023-03-13 07:32:55 +00:00
options.m_info->thread_free_cb(tinfo->core, options.m_global_ctx, m_ctx);
2023-03-08 02:46:23 +00:00
return NULL;
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
static void
2023-03-13 07:32:55 +00:00
dsmbr_free_worker(struct dsmbr_thread_ctx *worker)
2023-03-02 14:12:35 +00:00
{
2023-03-11 02:09:28 +00:00
for (auto stat : worker->stats) {
delete stat;
2023-03-02 14:12:35 +00:00
}
2023-03-11 02:09:28 +00:00
delete worker;
}
2023-03-02 14:12:35 +00:00
2023-03-11 02:09:28 +00:00
static struct dsmbr_thread_ctx *
dsmbr_create_worker(int tid, int core)
{
struct dsmbr_thread_ctx *tinfo = new struct dsmbr_thread_ctx;
tinfo->tid = tid;
tinfo->core = core;
2023-03-02 14:12:35 +00:00
pthread_attr_t attr;
int status;
if ((status = pthread_attr_init(&attr)) != 0) {
E("pthread_attr_init() failed: %d\n", status);
}
cpuset_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core, &cpuset);
status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset);
if (status != 0) {
E("pthread_attr_setaffinity_np() failed : %d\n", status);
}
2023-03-11 02:09:28 +00:00
2023-03-02 14:12:35 +00:00
status = pthread_create(&tinfo->thrd, &attr, dsmbr_worker_main, tinfo);
if (status != 0) {
E("pthread_create() failed: %d\n", status);
}
return tinfo;
}
2023-03-11 02:09:28 +00:00
static void
2023-03-13 07:32:55 +00:00
dsmbr_master_connect_slaves(struct dsmbr_slave *slaves)
2023-01-05 16:17:48 +00:00
{
/* create a connection to the clients */
2023-03-11 02:09:28 +00:00
for (int i = 0; i < options.num_slaves; i++) {
2023-01-05 16:17:48 +00:00
struct sockaddr_in csock_addr;
int c_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
csock_addr.sin_family = AF_INET;
2023-03-11 02:09:28 +00:00
csock_addr.sin_addr.s_addr = inet_addr(options.slave_ips[i]);
csock_addr.sin_port = htons(CTRL_PORT);
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
V("Connecting to slave %s...\n", options.slave_ips[i]);
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
if (connect(c_fd, (struct sockaddr *)&csock_addr, sizeof(csock_addr)) != 0) {
2023-01-05 16:17:48 +00:00
E("Connect failed. ERR %d\n", errno);
}
2023-03-11 02:09:28 +00:00
slaves[i].conn_fd = c_fd;
2023-01-05 16:17:48 +00:00
}
2023-01-17 20:22:43 +00:00
}
2023-01-05 16:17:48 +00:00
2023-03-02 14:12:35 +00:00
static int
2023-03-11 02:09:28 +00:00
dsmbr_slave_ctrl_sock_create(void)
2023-01-17 20:22:43 +00:00
{
struct sockaddr_in server_addr;
int status;
const int enable = 1;
server_addr.sin_family = AF_INET;
2023-03-02 14:12:35 +00:00
server_addr.sin_port = htons(CTRL_PORT);
2023-01-17 20:22:43 +00:00
server_addr.sin_addr.s_addr = INADDR_ANY;
2023-01-05 16:17:48 +00:00
2023-03-02 14:12:35 +00:00
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
2023-03-14 13:30:03 +00:00
E("socket() returned %d\n", errno);
2023-03-02 14:12:35 +00:00
}
2023-01-17 20:22:43 +00:00
2023-03-02 14:12:35 +00:00
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)) < 0) {
2023-03-14 13:30:03 +00:00
E("setsockopt() NODELAY %d\n", errno);
2023-03-02 14:12:35 +00:00
}
2023-01-17 20:22:43 +00:00
2023-03-02 14:12:35 +00:00
status = bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (status < 0) {
2023-03-14 13:30:03 +00:00
E("bind() returned %d\n", errno);
2023-03-02 14:12:35 +00:00
}
2023-01-17 20:22:43 +00:00
2023-03-02 14:12:35 +00:00
status = listen(fd, CTRL_BACKLOG);
if (status < 0) {
2023-03-14 13:30:03 +00:00
E("listen() returned %d\n", errno);
2023-01-17 20:22:43 +00:00
}
2023-03-02 14:12:35 +00:00
return fd;
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
static void
usage()
2023-01-05 16:17:48 +00:00
{
2023-03-11 02:09:28 +00:00
fprintf(stdout,
"Options:\n"
" -q: target qps.\n"
" -s: server addr.\n"
" -p: server port.\n"
" -o: output file path.\n"
" -a: worker thread cpulist.\n"
" -c: connections per thread.\n"
" -m: module path.\n"
" -M: slave ips, repeatable, enables master mode.\n"
" -S: slave mode.\n"
" -v: verbose mode.\n"
" -t: run time.\n"
" -T: warmup time.\n"
" -i: interarrival time distribution.\n"
" -O: module arguments ('key'='value'), repeatable.\n\n");
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
static int
dsmbr_slave_accept_master(int slave_ctrl_sock)
2023-01-05 16:17:48 +00:00
{
2023-03-11 02:09:28 +00:00
int ret;
int conn_fd;
struct sockaddr addr;
2023-03-14 13:47:29 +00:00
struct sockaddr_in * inaddr;
2023-03-11 02:09:28 +00:00
socklen_t addrlen;
2023-03-14 13:47:29 +00:00
char ip[INET_ADDRSTRLEN];
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
ret = accept(slave_ctrl_sock, &addr, &addrlen);
if (ret < 0) {
E("Failed to accept master connection: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
conn_fd = ret;
2023-01-05 16:17:48 +00:00
2023-03-14 13:47:29 +00:00
ret = getpeername(conn_fd, &addr, &addrlen);
if (ret < 0) {
E("getpeername failed: %d\n", errno);
}
inaddr = (struct sockaddr_in *)&addr;
if (inet_ntop(AF_INET, &inaddr->sin_addr, ip, INET_ADDRSTRLEN) == NULL) {
E("inet_ntop failed: %d\n", errno);
}
V("Accepted master connection %d from %s.\n", conn_fd, ip);
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
return conn_fd;
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
static void
dsmbr_getopt(int argc, char *argv[])
{
char ch;
2023-03-13 07:32:55 +00:00
while ((ch = getopt(argc, argv, "q:s:p:o:a:c:m:M:Svt:T:i:O:h")) != -1) {
2023-03-11 02:09:28 +00:00
switch (ch) {
case 'q': {
options.target_qps = strtoul(optarg, NULL, 10);
break;
}
case 's': {
2023-03-11 02:38:01 +00:00
if (nslookup(optarg, options.server_ip, sizeof(options.server_ip)) != 0) {
E("nslookup failed with %d for %s\n", errno, optarg);
}
2023-03-11 02:09:28 +00:00
break;
}
case 'p': {
options.server_port = strtoul(optarg, NULL, 10);
break;
}
case 'o': {
strncpy(options.output_file, optarg, sizeof(options.output_file));
break;
}
case 'a': {
cpulist_to_cpuset(optarg, &options.cpuset);
break;
}
case 'c': {
options.conn_per_thread = strtoul(optarg, NULL, 10);
break;
}
case 'm': {
strncpy(options.module_path, optarg, sizeof(options.module_path));
break;
}
case 'M': {
options.master_mode = 1;
2023-03-13 07:32:55 +00:00
if (nslookup(optarg, options.slave_ips[options.num_slaves],
sizeof(options.slave_ips[0])) != 0) {
2023-03-11 02:38:01 +00:00
E("nslookup failed with %d for %s\n", errno, optarg);
}
2023-03-11 02:09:28 +00:00
options.num_slaves++;
if (options.num_slaves > MAX_SLAVES) {
E("Too many slaves: %d > %d\n", options.num_slaves, MAX_SLAVES);
}
break;
}
case 'S': {
options.slave_mode = 1;
break;
}
case 'v': {
2023-03-13 08:08:44 +00:00
options.verbose++;
if (options.verbose == INFO_VERBOSITY) {
W("Verbose mode may cause SUBSTANTIAL latency fluctuation in some terminals emulators\n");
} else if (options.verbose == DEBUG_VERBOSITY) {
W("Debug mode enabled. Expect very verbose output.\n");
}
logger_set_verbosity(options.verbose);
2023-03-11 02:09:28 +00:00
break;
}
case 't': {
options.duration = strtoul(optarg, NULL, 10);
break;
}
case 'T': {
options.warmup = strtoul(optarg, NULL, 10);
break;
}
case 'i': {
strncpy(options.ia_dist, optarg, sizeof(options.ia_dist));
break;
}
case 'O': {
2023-03-13 07:32:55 +00:00
options.mod_argk[options.mod_argc] = new char[MAX_MOD_ARG_LEN];
options.mod_argv[options.mod_argc] = new char[MAX_MOD_ARG_LEN];
2023-03-11 02:09:28 +00:00
split_kvstr(optarg, "=", options.mod_argk[options.mod_argc],
2023-03-13 07:32:55 +00:00
MAX_MOD_ARG_LEN, options.mod_argv[options.mod_argc], MAX_MOD_ARG_LEN);
2023-03-11 02:09:28 +00:00
options.mod_argc++;
break;
}
case 'h':
usage();
exit(0);
default:
E("Unrecognized option -%c\n\n", ch);
}
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
static void
2023-03-14 13:30:03 +00:00
dsmbr_send_ctrl_code(int sock, uint32_t code)
2023-03-11 02:09:28 +00:00
{
int ret;
struct dsmbr_ctrl_msg msg;
msg.code = code;
ret = dsmbr_send_ctrl_msg(sock, &msg);
if (ret != 0) {
E("Failed to send ctrl message %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
return;
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
static void
2023-03-14 13:30:03 +00:00
dsmbr_wait_ctrl_code(int sock, uint32_t code)
2023-01-05 16:17:48 +00:00
{
2023-03-11 02:09:28 +00:00
int ret;
struct dsmbr_ctrl_msg msg;
ret = dsmbr_recv_ctrl_msg(sock, &msg);
if (ret != 0) {
E("Failed to recv ctrl message %d\n", errno);
}
2023-03-14 13:30:03 +00:00
if (msg.code != code) {
E("Unexpected message code 0x%x, expected 0x%x\n", msg.code, code);
2023-03-11 02:09:28 +00:00
}
return;
2023-01-05 16:17:48 +00:00
}
/*
* protocol:
2023-03-11 02:09:28 +00:00
*
2023-01-17 20:22:43 +00:00
* master -> client SYNC
2023-01-05 16:17:48 +00:00
* client and master all establish connections to the server
2023-01-17 20:22:43 +00:00
* client -> master ACK
2023-03-11 02:09:28 +00:00
* master -> client START
* master RUNS for X seconds, client runs forever
2023-01-05 16:17:48 +00:00
* master -> client STOP
2023-03-11 02:09:28 +00:00
* client -> master STAT
2023-01-05 16:17:48 +00:00
*/
int
2023-03-11 02:09:28 +00:00
main(int argc, char *argv[])
2023-01-05 16:17:48 +00:00
{
2023-03-11 02:09:28 +00:00
dsmbr_getopt(argc, argv);
dsmbr_dump_options();
2023-01-05 16:17:48 +00:00
2023-03-11 02:38:01 +00:00
dsmbr_create_tls_context();
2023-03-13 07:32:55 +00:00
2023-03-11 02:09:28 +00:00
if (sem_init(&options.worker_init_sem, 0, THRD_INIT_PARALLEL) != 0) {
E("sem_init failed: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-13 07:32:55 +00:00
V("Loading module %s...\n", options.module_path);
options.m_info = ppd_load_module(options.module_path);
V("Loaded module \"%s\".\n", options.m_info->name);
V("Initializing module...\n");
options.m_info->global_init_cb(options.mod_argc, options.mod_argk, options.mod_argv,
&options.m_global_ctx);
2023-03-11 02:09:28 +00:00
FILE *resp_fp_csv = NULL;
if (!options.slave_mode) {
resp_fp_csv = fopen(options.output_file, "w");
2023-01-05 16:17:48 +00:00
if (resp_fp_csv == NULL) {
2023-03-11 02:09:28 +00:00
E("Cannot open file %s for writing %d.", options.output_file, errno);
2023-01-05 16:17:48 +00:00
exit(1);
}
}
2023-03-11 02:09:28 +00:00
struct dsmbr_slave slaves[MAX_SLAVES];
int ret;
int slave_ctrl_sock = -1;
int master_ctrl_conn = -1;
std::vector<struct dsmbr_thread_ctx *> workers;
uint64_t start_ts;
uint64_t end_ts;
2023-03-13 07:32:55 +00:00
uint64_t warmup_ts;
2023-03-02 14:12:35 +00:00
struct dsmbr_ctrl_msg ctrl_msg;
2023-03-11 02:09:28 +00:00
int kqfd = kqueue();
if (kqfd < 0) {
E("kqueue() failed with %d\n", errno);
}
2023-01-05 16:17:48 +00:00
if (options.master_mode) {
2023-03-02 14:12:35 +00:00
/* initiate connections to clients */
2023-03-11 02:09:28 +00:00
V("M: connecting to slaves.\n");
dsmbr_master_connect_slaves(slaves);
V("M: SYNC to slaves.\n");
for (int i = 0; i < options.num_slaves; i++) {
dsmbr_send_ctrl_code(slaves[i].conn_fd, CTRL_SYNC);
2023-03-02 14:12:35 +00:00
}
2023-03-11 02:09:28 +00:00
} else if (options.slave_mode) {
slave_ctrl_sock = dsmbr_slave_ctrl_sock_create();
V("S: waiting for master connection...\n");
master_ctrl_conn = dsmbr_slave_accept_master(slave_ctrl_sock);
// add to kqueue
2023-03-14 13:30:03 +00:00
V("S: waiting for master SYNC %d...\n", master_ctrl_conn);
dsmbr_wait_ctrl_code(master_ctrl_conn, CTRL_SYNC);
2023-03-11 02:09:28 +00:00
struct kevent kev;
EV_SET(&kev, master_ctrl_conn, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kqfd, &kev, 1, NULL, 0, NULL) == -1) {
E("kevent() failed: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
// here slaves and master are on the same page */
int core;
int tid = 0;
2023-03-13 07:32:55 +00:00
CPU_FOREACH_ISSET (core, &options.cpuset) {
2023-03-11 02:09:28 +00:00
workers.push_back(dsmbr_create_worker(tid, core));
V("Created thread %d on core %d.\n", tid, core);
tid++;
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
V("Waiting for worker threads connection establishment.\n");
2023-03-13 07:32:55 +00:00
while (options.worker_init_comp.load(std::memory_order_relaxed) !=
CPU_COUNT(&options.cpuset)) {
};
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
// now all connections are established
2023-01-05 16:17:48 +00:00
if (options.master_mode) {
2023-03-11 02:09:28 +00:00
V("M: waiting for slaves ACK...\n");
for (int i = 0; i < options.num_slaves; i++) {
dsmbr_wait_ctrl_code(slaves[i].conn_fd, CTRL_ACK);
}
V("M: START to slaves...\n");
for (int i = 0; i < options.num_slaves; i++) {
dsmbr_send_ctrl_code(slaves[i].conn_fd, CTRL_START);
}
} else if (options.slave_mode) {
V("S: ACK to master...\n");
dsmbr_send_ctrl_code(master_ctrl_conn, CTRL_ACK);
V("S: waiting for master START...\n");
dsmbr_wait_ctrl_code(master_ctrl_conn, CTRL_START);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
V("Starting benchmark...\n");
start_ts = get_time_us();
2023-03-13 07:32:55 +00:00
warmup_ts = start_ts;
// seq_cst to make sure start_ts happens before store
options.state.store(STATE_RUN, std::memory_order_seq_cst);
2023-03-11 02:09:28 +00:00
int cur_second = 0;
2023-01-05 16:17:48 +00:00
struct kevent kev;
2023-03-11 02:09:28 +00:00
if (!options.slave_mode) {
// slave mode runs forever
2023-03-13 07:32:55 +00:00
EV_SET(&kev, TIMER_MAGIC, EVFILT_TIMER, EV_ADD, NOTE_USECONDS, S2US, NULL);
2023-03-11 02:09:28 +00:00
if (kevent(kqfd, &kev, 1, NULL, 0, NULL) == -1) {
E("kevent() failed: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
}
while (true) {
if (kevent(kqfd, NULL, 0, &kev, 1, NULL) == -1) {
E("kevent() failed: %d\n", errno);
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
if (kev.ident == TIMER_MAGIC) {
2023-03-13 07:32:55 +00:00
if (kev.data != 1) {
E("Timer expired multiple times\n");
}
2023-03-11 02:09:28 +00:00
cur_second++;
2023-03-13 07:32:55 +00:00
if ((int)cur_second == options.warmup) {
warmup_ts = get_time_us();
}
2023-03-11 02:09:28 +00:00
if ((int)cur_second >= options.duration + options.warmup) {
2023-01-05 16:17:48 +00:00
break;
}
2023-03-11 02:09:28 +00:00
} else if (options.slave_mode && (int)kev.ident == master_ctrl_conn) {
dsmbr_wait_ctrl_code(master_ctrl_conn, CTRL_STOP);
V("S: received STOP from master.\n");
break;
} else {
E("Unknown kevent ident %d\n", (int)kev.ident);
2023-01-05 16:17:48 +00:00
}
}
2023-03-11 02:09:28 +00:00
V("Stopping benchmark...\n");
2023-03-13 07:32:55 +00:00
options.state.store(STATE_STOP, std::memory_order_seq_cst);
2023-03-11 02:09:28 +00:00
end_ts = get_time_us();
if (options.master_mode) {
V("M: STOP to slaves...\n");
for (int i = 0; i < options.num_slaves; i++) {
dsmbr_send_ctrl_code(slaves[i].conn_fd, CTRL_STOP);
2023-01-05 16:17:48 +00:00
}
}
2023-03-13 07:32:55 +00:00
2023-03-11 02:09:28 +00:00
// wait for thread exit
for (unsigned int i = 0; i < workers.size(); i++) {
if ((ret = pthread_join(workers.at(i)->thrd, NULL)) != 0) {
E("pthread_join tid %d err %d\n", workers.at(i)->tid, ret);
}
2023-01-05 16:17:48 +00:00
}
2023-03-11 02:09:28 +00:00
// obtain stats from threads
uint64_t total_req = 0;
uint64_t total_send = 0;
uint64_t total_recv = 0;
for (auto worker : workers) {
2023-03-13 07:32:55 +00:00
for (auto stat : worker->stats) {
2023-03-11 02:09:28 +00:00
total_req++;
total_send += stat->send_size;
total_recv += stat->recv_size;
}
2023-01-05 16:17:48 +00:00
}
2023-03-13 07:32:55 +00:00
V("Reqs: %lu Time: %lu us Send: %lu bytes Recv: %lu bytes\n", total_req, end_ts - start_ts,
total_send, total_recv);
2023-01-05 16:17:48 +00:00
if (options.master_mode) {
2023-03-11 02:09:28 +00:00
V("M: waiting for slaves STAT...\n");
for (int i = 0; i < options.num_slaves; i++) {
if (dsmbr_recv_ctrl_msg(slaves[i].conn_fd, &ctrl_msg) != 0) {
E("dsmbr_recv_ctrl_msg failed with %d.\n", errno);
}
if (ctrl_msg.code != CTRL_STAT) {
2023-03-13 07:32:55 +00:00
E("Unexpected ctrl code %d, expected: %d.\n", ctrl_msg.code,
CTRL_STAT);
2023-03-11 02:09:28 +00:00
}
slaves[i].reqs = ctrl_msg.data[0];
slaves[i].time = ctrl_msg.data[1];
slaves[i].send_sz = ctrl_msg.data[2];
slaves[i].recv_sz = ctrl_msg.data[3];
2023-03-13 07:32:55 +00:00
V("M: received stats - Reqs: %lu Time: %lu us Send: %lu bytes Recv: %lu bytes\n",
slaves[i].reqs, slaves[i].time, slaves[i].send_sz, slaves[i].recv_sz);
2023-03-11 02:09:28 +00:00
}
} else if (options.slave_mode) {
V("M: STAT to master...\n");
ctrl_msg.code = CTRL_STAT;
ctrl_msg.data[0] = total_req;
ctrl_msg.data[1] = end_ts - start_ts;
ctrl_msg.data[2] = total_send;
2023-03-14 13:30:03 +00:00
ctrl_msg.data[3] = total_recv;
2023-03-11 02:09:28 +00:00
if (dsmbr_send_ctrl_msg(master_ctrl_conn, &ctrl_msg) != 0) {
E("dsmbr_send_ctrl_msg failed with %d.\n", errno);
}
}
if (!options.slave_mode) {
2023-03-14 13:30:03 +00:00
V("Saving results to %s ...\n", options.output_file);
2023-03-11 02:09:28 +00:00
for (int i = 0; i < options.num_slaves; i++) {
2023-03-13 07:32:55 +00:00
fprintf(resp_fp_csv, "S,%lu,%lu,%lu,%lu\n", slaves[i].reqs, slaves[i].time,
slaves[i].send_sz, slaves[i].recv_sz);
2023-03-11 02:09:28 +00:00
}
2023-03-13 07:32:55 +00:00
fprintf(resp_fp_csv, "M,%lu,%lu,%lu,%lu\n", total_req, end_ts - start_ts,
total_send, total_recv);
2023-03-11 02:09:28 +00:00
for (auto worker : workers) {
for (auto stat : worker->stats) {
2023-03-13 07:32:55 +00:00
if (stat->send_ts < warmup_ts) {
fprintf(resp_fp_csv, "W,%lu,%lu,%lu,%lu\n", stat->send_ts,
stat->recv_ts, stat->send_size, stat->recv_size);
} else {
fprintf(resp_fp_csv, "R,%lu,%lu,%lu,%lu\n", stat->send_ts,
stat->recv_ts, stat->send_size, stat->recv_size);
}
2023-01-05 16:17:48 +00:00
}
}
2023-03-11 02:09:28 +00:00
fflush(resp_fp_csv);
}
2023-01-05 16:17:48 +00:00
2023-03-11 02:09:28 +00:00
// clean up
if (resp_fp_csv != NULL) {
2023-01-05 16:17:48 +00:00
fclose(resp_fp_csv);
}
2023-03-11 02:09:28 +00:00
sem_destroy(&options.worker_init_sem);
close(kqfd);
if (slave_ctrl_sock != -1) {
close(slave_ctrl_sock);
}
if (master_ctrl_conn != -1) {
close(master_ctrl_conn);
}
for (int i = 0; i < options.num_slaves; i++) {
close(slaves[i].conn_fd);
}
for (auto worker : workers) {
dsmbr_free_worker(worker);
}
2023-03-13 07:32:55 +00:00
options.m_info->global_free_cb(options.m_global_ctx);
for (int i = 0; i < options.mod_argc; i++) {
delete[] options.mod_argk[i];
delete[] options.mod_argv[i];
}
2023-01-05 16:17:48 +00:00
return 0;
}