1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

Merge branch 'hook-rtp-rate-pid' into develop

This commit is contained in:
Steffen Vogel 2019-04-15 12:26:51 +02:00
commit 7ee01aabda
8 changed files with 145 additions and 179 deletions

2
common

@ -1 +1 @@
Subproject commit 117dda74728679b890e20fb8bb1fc4b5792fc46d
Subproject commit 05dc3202df047a2b38a264e7398d49a55704b90e

View file

@ -2,20 +2,31 @@ nodes = {
rtp_node = {
type = "rtp"
rate = 10000,
format = "raw.32.be"
rtcp = false
rtcp = {
enabled = false,
mode = "aimd"
throttle_mode = "decimate"
hook_type = "decimate"
}
aimd = {
a = 10,
b = 0.5
Kp = 1.0
Ki = 0.0
Kd = 0
rate_min = 100
rate_init = 2000
rate_source = 10000
log = "aimd-rates-%Y_%m_%d_%s.log"
hook_type = "limit_rate"
}
in = {

View file

@ -140,6 +140,15 @@ public:
}
};
class LimitHook : public Hook {
public:
using Hook::Hook;
virtual void setRate(double rate, double maxRate = -1) = 0;
};
class HookFactory : public plugin::Plugin {
protected:

View file

@ -31,14 +31,25 @@
namespace villas {
namespace node {
class DecimateHook : public Hook {
class DecimateHook : public LimitHook {
protected:
int ratio;
unsigned counter;
public:
using Hook::Hook;
using LimitHook::LimitHook;
virtual void setRate(double rate, double maxRate = -1)
{
assert(maxRate > 0);
int ratio = maxRate / rate;
if (ratio == 0)
ratio = 1;
setRatio(ratio);
}
void setRatio(int r)
{

View file

@ -31,7 +31,7 @@
namespace villas {
namespace node {
class LimitRateHook : public Hook {
class LimitRateHook : public LimitHook {
protected:
enum {
@ -44,18 +44,18 @@ protected:
timespec last;
public:
void setRate(double rate)
{
deadtime = 1.0 / rate;
}
LimitRateHook(struct path *p, struct node *n, int fl, int prio, bool en = true) :
Hook(p, n, fl, prio, en),
LimitHook(p, n, fl, prio, en),
mode(LIMIT_RATE_LOCAL)
{
last = (timespec) { 0, 0 };
}
virtual void setRate(double rate, double maxRate = -1)
{
deadtime = 1.0 / rate;
}
virtual void parse(json_t *cfg);
virtual int process(sample *smp);

View file

@ -45,6 +45,7 @@ extern "C" {
#include <villas/queue_signalled.h>
#include <villas/hooks/limit_rate.hpp>
#include <villas/hooks/decimate.hpp>
#include <villas/dsp/pid.hpp>
/* Forward declarations */
struct format_type;
@ -53,14 +54,10 @@ struct format_type;
#define RTP_INITIAL_BUFFER_LEN 1500
#define RTP_PACKET_TYPE 21
enum rtp_throttle_mode {
RTCP_THROTTLE_DISABLED,
RTCP_THROTTLE_HOOK_DECIMATE,
RTCP_THROTTLE_HOOK_LIMIT_RATE
};
enum rtp_rtcp_mode {
RTCP_MODE_AIMD
enum rtp_hook_type {
RTCP_HOOK_DISABLED,
RTCP_HOOK_DECIMATE,
RTCP_HOOK_LIMIT_RATE
};
struct rtp {
@ -76,28 +73,28 @@ struct rtp {
struct format_type *format;
struct io io;
double rate; /**< Sample rate of source */
struct {
int enabled;
int num_rrs;
enum rtp_rtcp_mode mode;
enum rtp_throttle_mode throttle_mode;
union {
villas::node::DecimateHook *decimate;
villas::node::LimitRateHook *limit_rate;
} throttle_hook;
} rtcp;
struct {
double a;
double b;
double last_rate;
enum rtp_hook_type rate_hook_type;
villas::node::LimitHook *rate_hook;
villas::dsp::PID rate_pid;
/* PID parameters for rate controller */
double Kp, Ki, Kd;
double rate_min;
double rate;
double rate_last;
double rate_source; /**< Sample rate of source */
std::ofstream *log;
char *log_filename;

View file

@ -31,7 +31,7 @@
#include <villas/hook.hpp>
#include <villas/sample.h>
#include <villas/window.h>
#include <villas/dsp/window.hpp>
#include <villas/utils.h>
using namespace std::complex_literals;
@ -57,27 +57,27 @@ protected:
int *fharmonics;
int fharmonics_len;
struct window window;
dsp::Window<double> window;
void step(double *in, std::complex<float> *out)
{
int N = window.steps;
int N = window.getSteps();
__attribute__((unused)) std::complex<double> om_k, corr;
double newest = *in;
__attribute__((unused)) double oldest = window_update(&window, newest);
__attribute__((unused)) double oldest = window.update(newest);
for (int k = 0; k < fharmonics_len; k++) {
om_k = 2.0i * M_PI * (double) fharmonics[k] / (double) N;
/* Correction for stationary phasor */
corr = std::exp(-om_k * (steps - (window.steps + 1)));
corr = std::exp(-om_k * (steps - (N + 1)));
//corr = 1;
#if 0
/* Recursive update */
coeffs[k] = std::exp(om) * (coeffs[k] + (newest - oldest));
out[k] = (2.0 / window.steps) * (coeffs[i] * corr);
out[k] = (2.0 / N) * (coeffs[i] * corr);
/* DC component */
if (fharmonics[k] == 0)
@ -87,7 +87,7 @@ protected:
std::complex<double> X_k = 0;
for (int n = 0; n < N; n++) {
double x_n = window.data[(window.pos - window.steps + n) & window.mask];
double x_n = window[window.getPos() + n];
X_k += x_n * std::exp(om_k * (double) n);
}
@ -135,8 +135,6 @@ public:
virtual void start()
{
int ret;
assert(state == STATE_PREPARED);
time = 0;
@ -145,26 +143,11 @@ public:
for (int i = 0; i < fharmonics_len; i++)
coeffs[i] = 0;
ret = window_init(&window, (1.0 / f0) / timestep, 0.0);
if (ret)
throw RuntimeError("Failed to initialize window");
window = dsp::Window<double>((1.0 / f0) / timestep, 0.0);
state = STATE_STARTED;
}
virtual void stop()
{
int ret;
assert(state == STATE_STARTED);
ret = window_destroy(&window);
if (ret)
throw RuntimeError("Failed to destroy window");
state = STATE_STOPPED;
}
virtual void parse(json_t *cfg)
{
int ret;

View file

@ -57,59 +57,31 @@ using namespace villas::node;
static struct plugin p;
static int rtp_set_rate(struct node *n, double rate)
{
struct rtp *r = (struct rtp *) n->_vd;
int ratio;
switch (r->rtcp.throttle_mode) {
case RTCP_THROTTLE_HOOK_LIMIT_RATE:
r->rtcp.throttle_hook.limit_rate->setRate(rate);
break;
case RTCP_THROTTLE_HOOK_DECIMATE:
ratio = r->rate / rate;
if (ratio == 0)
ratio = 1;
r->rtcp.throttle_hook.decimate->setRatio(ratio);
break;
case RTCP_THROTTLE_DISABLED:
return 0;
default:
return -1;
}
r->logger->info("Set rate limiting for node {} to {}", node_name(n), rate);
return 0;
}
static int rtp_aimd(struct node *n, double loss_frac)
{
struct rtp *r = (struct rtp *) n->_vd;
int ret;
double rate;
if (!r->rtcp.enabled)
return -1;
if (loss_frac < 0.01)
rate = r->aimd.last_rate + r->aimd.a;
rate = r->aimd.rate + r->aimd.a;
else
rate = r->aimd.last_rate * r->aimd.b;
rate = r->aimd.rate * r->aimd.b;
r->aimd.last_rate = rate;
r->aimd.rate = r->aimd.rate_pid.calculate(rate, r->aimd.rate);
ret = rtp_set_rate(n, rate);
if (ret)
return ret;
if (r->aimd.rate_hook) {
r->aimd.rate_hook->setRate(r->aimd.rate);
r->logger->debug("AIMD: Set rate limit to: {}", r->aimd.rate);
}
if (r->aimd.log)
*(r->aimd.log) << r->rtcp.num_rrs << "\t" << loss_frac << "\t" << rate << std::endl;
*(r->aimd.log) << r->rtcp.num_rrs << "\t" << loss_frac << "\t" << r->aimd.rate << std::endl;
r->logger->debug("AIMD: {}\t{}\t{}", r->rtcp.num_rrs, loss_frac, r->aimd.rate);
return 0;
}
@ -121,16 +93,20 @@ int rtp_init(struct node *n)
r->logger = villas::logging.get("node:rtp");
/* Default values */
r->rate = 1;
r->aimd.rate = 1;
r->aimd.a = 10;
r->aimd.b = 0.5;
r->aimd.last_rate = 2000;
r->aimd.Kp = 1;
r->aimd.Ki = 0;
r->aimd.Kd = 0;
r->aimd.rate_min = 1;
r->aimd.rate_source = 2000;
r->aimd.log_filename = nullptr;
r->aimd.log = nullptr;
r->rtcp.enabled = false;
r->rtcp.throttle_mode = RTCP_THROTTLE_DISABLED;
r->aimd.rate_hook_type = RTCP_HOOK_DISABLED;
return 0;
}
@ -153,15 +129,15 @@ int rtp_parse(struct node *n, json_t *cfg)
const char *local, *remote;
const char *format = "villas.binary";
const char *log = nullptr;
const char *hook_type = nullptr;
uint16_t port;
json_error_t err;
json_t *json_rtcp = nullptr, *json_aimd = nullptr;
json_t *json_aimd = nullptr;
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: F, s?: o, s?: o, s: { s: s }, s: { s: s } }",
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: b, s?: o, s: { s: s }, s: { s: s } }",
"format", &format,
"rate", &r->rate,
"rtcp", &json_rtcp,
"rtcp", &r->rtcp.enabled,
"aimd", &json_aimd,
"out",
"address", &remote,
@ -173,52 +149,39 @@ int rtp_parse(struct node *n, json_t *cfg)
/* AIMD */
if (json_aimd) {
ret = json_unpack_ex(json_aimd, &err, 0, "{ s?: F, s?: F, s?: F, s?: s }",
ret = json_unpack_ex(json_aimd, &err, 0, "{ s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: F, s?: s, s?: s }",
"a", &r->aimd.a,
"b", &r->aimd.b,
"start_rate", &r->aimd.last_rate,
"log", &log
"Kp", &r->aimd.Kp,
"Ki", &r->aimd.Ki,
"Kd", &r->aimd.Kd,
"rate_min", &r->aimd.rate_min,
"rate_source", &r->aimd.rate_source,
"rate_init", &r->aimd.rate,
"log", &log,
"hook_type", &hook_type
);
if (ret)
r->logger->error("Failed to parse configuration of node {}", node_name(n));
/* AIMD Hook type */
if (!r->rtcp.enabled)
r->aimd.rate_hook_type = RTCP_HOOK_DISABLED;
else if (hook_type) {
if (!strcmp(hook_type, "decimate"))
r->aimd.rate_hook_type = RTCP_HOOK_DECIMATE;
else if (!strcmp(hook_type, "limit_rate"))
r->aimd.rate_hook_type = RTCP_HOOK_LIMIT_RATE;
else if (!strcmp(hook_type, "disabled"))
r->aimd.rate_hook_type = RTCP_HOOK_DISABLED;
else
r->logger->error("Unknown RTCP hook_type: {}", hook_type);
}
}
if (log)
r->aimd.log_filename = strdup(log);
/* RTCP */
if (json_rtcp) {
const char *mode = "aimd";
const char *throttle_mode = "decimate";
/* Enable if RTCP section is available */
r->rtcp.enabled = 1;
ret = json_unpack_ex(json_rtcp, &err, 0, "{ s?: b, s?: s, s?: s }",
"enabled", &r->rtcp.enabled,
"mode", &mode,
"throttle_mode", &throttle_mode
);
if (ret)
r->logger->error("Failed to parse configuration of node {}", node_name(n));
/* RTCP Mode */
if (!strcmp(mode, "aimd"))
r->rtcp.mode = RTCP_MODE_AIMD;
else
r->logger->error("Unknown RTCP mode: {}", mode);
/* RTCP Throttle mode */
if (r->rtcp.enabled == false)
r->rtcp.throttle_mode = RTCP_THROTTLE_DISABLED;
else if (!strcmp(throttle_mode, "decimate"))
r->rtcp.throttle_mode = RTCP_THROTTLE_HOOK_DECIMATE;
else if (!strcmp(throttle_mode, "limit_rate"))
r->rtcp.throttle_mode = RTCP_THROTTLE_HOOK_LIMIT_RATE;
else
r->logger->error("Unknown RTCP throttle mode: {}", throttle_mode);
}
/* Format */
r->format = format_type_lookup(format);
if (!r->format)
@ -269,34 +232,27 @@ char * rtp_print(struct node *n)
r->rtcp.enabled ? "yes" : "no");
if (r->rtcp.enabled) {
const char *mode, *throttle_mode;
const char *hook_type;
switch (r->rtcp.mode) {
case RTCP_MODE_AIMD:
mode = "aimd";
}
switch (r->rtcp.throttle_mode) {
case RTCP_THROTTLE_HOOK_DECIMATE:
throttle_mode = "decimate";
switch (r->aimd.rate_hook_type) {
case RTCP_HOOK_DECIMATE:
hook_type = "decimate";
break;
case RTCP_THROTTLE_HOOK_LIMIT_RATE:
throttle_mode = "limit_rate";
case RTCP_HOOK_LIMIT_RATE:
hook_type = "limit_rate";
break;
case RTCP_THROTTLE_DISABLED:
throttle_mode = "disabled";
case RTCP_HOOK_DISABLED:
hook_type = "disabled";
break;
default:
throttle_mode = "unknown";
hook_type = "unknown";
}
strcatf(&buf, ", rtcp.mode=%s, rtcp.throttle_mode=%s", mode, throttle_mode);
if (r->rtcp.mode == RTCP_MODE_AIMD)
strcatf(&buf, ", aimd.a=%f, aimd.b=%f, aimd.start_rate=%f", r->aimd.a, r->aimd.b, r->aimd.last_rate);
strcatf(&buf, ", aimd.hook_type=%s", hook_type);
strcatf(&buf, ", aimd.a=%f, aimd.b=%f, aimd.start_rate=%f", r->aimd.a, r->aimd.b, r->aimd.rate);
}
free(local);
@ -385,40 +341,37 @@ int rtp_start(struct node *n)
if (ret)
return ret;
/* Initialize throttle hook */
if (r->rtcp.throttle_mode != RTCP_THROTTLE_DISABLED) {
/* Initialize AIMD hook */
if (r->aimd.rate_hook_type != RTCP_HOOK_DISABLED) {
#ifdef WITH_HOOKS
switch (r->rtcp.throttle_mode) {
case RTCP_THROTTLE_HOOK_DECIMATE:
r->rtcp.throttle_hook.decimate = new DecimateHook(nullptr, n, 0, 0);
r->rtcp.throttle_hook.decimate->parse();
r->rtcp.throttle_hook.decimate->check();
r->rtcp.throttle_hook.decimate->prepare();
r->rtcp.throttle_hook.decimate->start();
switch (r->aimd.rate_hook_type) {
case RTCP_HOOK_DECIMATE:
r->aimd.rate_hook = new DecimateHook(nullptr, n, 0, 0);
break;
case RTCP_THROTTLE_HOOK_LIMIT_RATE:
r->rtcp.throttle_hook.limit_rate = new LimitRateHook(nullptr, n, 0, 0);
r->rtcp.throttle_hook.limit_rate->parse();
r->rtcp.throttle_hook.limit_rate->check();
r->rtcp.throttle_hook.limit_rate->prepare();
r->rtcp.throttle_hook.limit_rate->start();
case RTCP_HOOK_LIMIT_RATE:
r->aimd.rate_hook = new LimitRateHook(nullptr, n, 0, 0);
break;
default:
return -1;
}
vlist_push(&n->out.hooks, (void *) r->rtcp.throttle_hook.limit_rate);
r->aimd.rate_hook->init();
vlist_push(&n->out.hooks, (void *) r->aimd.rate_hook);
r->aimd.rate_hook->setRate(r->aimd.rate_last);
#else
r->logger->error("Throttle hooks not supported");
r->logger->error("Rate limiting is not supported");
return -1;
#endif
}
ret = rtp_set_rate(n, r->aimd.last_rate);
if (ret)
return ret;
double dt = 5.0; // TODO
r->aimd.rate_pid = villas::dsp::PID(dt, r->aimd.rate_source, r->aimd.rate_min, r->aimd.Kp, r->aimd.Ki, r->aimd.Kd);
/* Initialize RTP socket */
uint16_t port = sa_port(&r->in.saddr_rtp) & ~1;
@ -430,7 +383,7 @@ int rtp_start(struct node *n)
rtcp_start(r->rs, node_name(n), &r->out.saddr_rtcp);
if (r->aimd.log) {
if (r->aimd.log_filename) {
char fn[128];
time_t ts = time(nullptr);
@ -444,6 +397,8 @@ int rtp_start(struct node *n)
*(r->aimd.log) << "# cnt\tfrac_loss\trate" << std::endl;
}
else
r->aimd.log = nullptr;
}
return ret;