stdin based pct control

This commit is contained in:
quackerd 2023-03-05 16:48:54 +01:00
parent 28d469e8ff
commit 25c18b4fc5

View File

@ -1,4 +1,5 @@
#include <sys/endian.h>
#include <sys/select.h>
#include <sys/signal.h>
#include "gen.hh"
#include <array>
@ -23,7 +24,6 @@ usage()
" -v: verbose mode\n"
" -b: buffer size\n"
" -q: bytes per second\n"
" -p: target throughput percentage (triggered by SIGUSR1)\n"
" -d: destination domain index\n"
" -s: worker threads cpu list\n"
" -S: enable shared buffer\n"
@ -35,16 +35,8 @@ usage()
" -M: print this string when threads are ready to run\n");
fflush(stdout);
}
static std::atomic<int> rate_control {0};
static char output_file[256] = "memloadgen_samples.txt";
void sig_handler(int sig)
{
if (sig == SIGUSR1 && rate_control.load(std::memory_order_relaxed) == 0) {
rate_control.store(1, std::memory_order_relaxed);
ntr(NTR_DEP_USER1, NTR_LEVEL_INFO, "enabling rate control....\n");
}
}
static char output_file[256] = "memloadgen_samples.txt";
int main(int argc, char * argv[])
{
@ -56,7 +48,6 @@ int main(int argc, char * argv[])
uint64_t bps = 0;
uint64_t transaction_size = arr_sz;
cpuset_t threads;
uint32_t pct = 0;
char magic[256] = {0};
CPU_ZERO(&threads);
CPU_SET(0, &threads);
@ -65,13 +56,14 @@ int main(int argc, char * argv[])
std::list<uint64_t> history;
int shared_buffer = 0;
int rate_ctrl = 0;
cpuset_t domain_mask;
CPU_ZERO(&domain_mask);
CPU_SET(0, &domain_mask);
{
int c;
// parse arguments
while ((c = getopt(argc, argv, "vhb:d:s:So:T:t:q:i:p:H:M:")) != -1) {
while ((c = getopt(argc, argv, "vhb:d:s:So:T:t:q:i:H:M:")) != -1) {
switch (c) {
case 'v':
ntr_set_level(NTR_DEP_USER1, ntr_get_level(NTR_DEP_USER1) + 1);
@ -106,9 +98,6 @@ int main(int argc, char * argv[])
case 'i':
strncpy(ia_dist, optarg, sizeof(ia_dist));
break;
case 'p':
pct = strtoul(optarg, nullptr, 10);
break;
case 'H':
history_sz = strtol(optarg, nullptr, 10);
break;
@ -127,7 +116,6 @@ int main(int argc, char * argv[])
" num threads: %d\n"
" target domain: %ld\n"
" bytes per second: %lu\n"
" percentage: %d%%\n"
" interarrival distribution: %s\n"
" shared buffer: %d\n"
" transaction time: %lu\n"
@ -136,7 +124,7 @@ int main(int argc, char * argv[])
" magic: %s\n",
arr_sz, CPU_COUNT(&threads),
CPU_FFS(&domain_mask) - 1, bps,
pct, ia_dist, shared_buffer,
ia_dist, shared_buffer,
transaction_size,time, history_sz, magic);
// init topo
@ -150,10 +138,7 @@ int main(int argc, char * argv[])
fprintf(stderr, "libnms init failed!\n");
exit(1);
}
if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
fprintf(stderr, "signal() failed %d!\n", errno);
exit(1);
}
bool success = false;
memload_generator::memload_generator_options opts;
opts.buffer_size = arr_sz;
@ -175,6 +160,11 @@ int main(int argc, char * argv[])
exit(1);
}
struct timeval stval;
stval.tv_sec = 0;
stval.tv_usec = 0;
char pct_line[64] = {0};
uint64_t prev_ts = topo_uptime_ns();
uint64_t prev_trans = mgen->get_transactions();
uint32_t cur_time = 0;
@ -191,18 +181,30 @@ int main(int argc, char * argv[])
prev_trans = trans;
cur_time++;
int rc = rate_control.load(std::memory_order_relaxed);
switch(rc) {
case 0:
case 1:
// keep history
history.emplace_back(bps);
if ((int)history.size() > history_sz) {
history.pop_front();
}
if (rate_ctrl == 0) {
// keep history
history.emplace_back(bps);
if ((int)history.size() > history_sz) {
history.pop_front();
}
if (rc == 1) {
rate_control.store(2, std::memory_order_relaxed);
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(STDIN_FILENO, &fdset);
int ret = select(1, &fdset, NULL, NULL, &stval);
if (ret < 0) {
if (errno != EINTR) {
fprintf(stderr, "select() failed with %d\n", errno);
exit(1);
}
} else if (ret > 0) {
if (FD_ISSET(STDIN_FILENO, &fdset)) {
ret = read(STDIN_FILENO, pct_line, sizeof(pct_line) - 1);
if (ret < 0) {
fprintf(stderr, "read() failed with %d\n", errno);
exit(1);
}
unsigned int pct = strtoul(pct_line, NULL, 10);
uint64_t sum = 0;
size_t sz = history.size();
while (history.size() > 0) {
@ -212,11 +214,11 @@ int main(int argc, char * argv[])
uint64_t newbps = ((sum / sz) * (double)pct / 100.0);
mgen->set_transactions(newbps / transaction_size);
ntr(NTR_DEP_USER1, NTR_LEVEL_INFO, "adjusted target bps = %ld ~= %ldM\n", newbps, newbps / 1024 / 1024);
ntr(NTR_DEP_USER1, NTR_LEVEL_INFO, "adjusted target bps to %u%% = %ldB ~= %ldM\n", pct, newbps, newbps / 1024 / 1024);
rate_ctrl = 1;
}
break;
default:
break;
}
}
}
mgen->stop();