add signal control

This commit is contained in:
quackerd 2023-03-05 15:58:06 +01:00
parent 521a49d945
commit 6cd0e7d12f

View File

@ -1,5 +1,8 @@
#include <sys/endian.h>
#include <sys/signal.h>
#include "gen.hh"
#include <array>
#include <atomic>
#include <cstdlib>
#include <cstring>
#include <list>
@ -8,6 +11,7 @@
#include "ntr.h"
#include "nms.h"
#include <getopt.h>
#include <pthread.h>
#include <unistd.h>
#include <topo.h>
@ -19,7 +23,7 @@ usage()
" -v: verbose mode\n"
" -b: buffer size\n"
" -q: bytes per second\n"
" -p: target throughput percentage\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"
@ -28,13 +32,20 @@ usage()
" -i: inter arrival time distribution\n"
" -o: output file path\n"
" -H: history size for pct adjustment\n"
" -w: warmup time before pct adjustment\n"
" -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");
}
}
int main(int argc, char * argv[])
{
ntr_init();
@ -50,8 +61,7 @@ int main(int argc, char * argv[])
CPU_ZERO(&threads);
CPU_SET(0, &threads);
char ia_dist[32] = "fixed";
int history_sz = 0;
int warmup = 0;
int history_sz = 5;
std::list<uint64_t> history;
int shared_buffer = 0;
@ -61,7 +71,7 @@ int main(int argc, char * argv[])
{
int c;
// parse arguments
while ((c = getopt(argc, argv, "vhb:d:s:So:T:t:q:i:p:H:w:M:")) != -1) {
while ((c = getopt(argc, argv, "vhb:d:s:So:T:t:q:i:p:H:M:")) != -1) {
switch (c) {
case 'v':
ntr_set_level(NTR_DEP_USER1, ntr_get_level(NTR_DEP_USER1) + 1);
@ -102,9 +112,6 @@ int main(int argc, char * argv[])
case 'H':
history_sz = strtol(optarg, nullptr, 10);
break;
case 'w':
warmup = strtol(optarg, nullptr, 10);
break;
case 'M':
strncpy(magic, optarg, sizeof(magic));
break;
@ -126,11 +133,11 @@ int main(int argc, char * argv[])
" transaction time: %lu\n"
" runtime: %d\n"
" history: %d\n"
" warmup: %d\n",
" magic: %s\n",
arr_sz, CPU_COUNT(&threads),
CPU_FFS(&domain_mask) - 1, bps,
pct, ia_dist, shared_buffer,
transaction_size,time, history_sz, warmup);
transaction_size,time, history_sz, magic);
// init topo
if (topo_init(ntr_get_level(NTR_DEP_USER1) != NTR_LEVEL_DEFAULT)) {
@ -143,7 +150,10 @@ 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;
@ -181,26 +191,32 @@ int main(int argc, char * argv[])
prev_trans = trans;
cur_time++;
if((int)cur_time <= warmup) {
// keep history
history.emplace_back(bps);
if ((int)history.size() > history_sz) {
history.pop_front();
}
// adjust time
if ((int)cur_time == warmup) {
uint64_t sum = 0;
size_t sz = history.size();
while (history.size() > 0) {
sum += history.front();
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();
}
uint64_t newbps = (uint64_t)((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);
}
if (rc == 1) {
rate_control.store(2, std::memory_order_relaxed);
uint64_t sum = 0;
size_t sz = history.size();
while (history.size() > 0) {
sum += history.front();
history.pop_front();
}
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);
}
break;
default:
break;
}
}
mgen->stop();