FiWiTSF: single exit points in helpers and main
- follower_var_pop, kf1_update: one return each. - stats_maybe_print, apply_correction: goto out. - run_single: goto out on alloc failure; main: funnel to out. Made-with: Cursor
This commit is contained in:
parent
dc55851c74
commit
f831f47adc
|
|
@ -219,9 +219,11 @@ static void welford_add(struct follower_acc *acc, double x)
|
||||||
|
|
||||||
static double follower_var_pop(const struct follower_acc *a)
|
static double follower_var_pop(const struct follower_acc *a)
|
||||||
{
|
{
|
||||||
if (a->n == 0)
|
double v = 0.0;
|
||||||
return 0.0;
|
|
||||||
return a->m2 / (double)a->n;
|
if (a->n != 0)
|
||||||
|
v = a->m2 / (double)a->n;
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void acc_add_sample(struct follower_acc *acc, int64_t err, int64_t step,
|
static void acc_add_sample(struct follower_acc *acc, int64_t err, int64_t step,
|
||||||
|
|
@ -279,18 +281,21 @@ static double kf1_update(struct kf1 *k, double z, double q, double r)
|
||||||
{
|
{
|
||||||
double p_pred;
|
double p_pred;
|
||||||
double Kgain;
|
double Kgain;
|
||||||
|
double xf;
|
||||||
|
|
||||||
if (!k->inited) {
|
if (!k->inited) {
|
||||||
k->x = z;
|
k->x = z;
|
||||||
k->P = r;
|
k->P = r;
|
||||||
k->inited = 1;
|
k->inited = 1;
|
||||||
return k->x;
|
xf = k->x;
|
||||||
}
|
} else {
|
||||||
p_pred = k->P + q;
|
p_pred = k->P + q;
|
||||||
Kgain = p_pred / (p_pred + r);
|
Kgain = p_pred / (p_pred + r);
|
||||||
k->x += Kgain * (z - k->x);
|
k->x += Kgain * (z - k->x);
|
||||||
k->P = (1.0 - Kgain) * p_pred;
|
k->P = (1.0 - Kgain) * p_pred;
|
||||||
return k->x;
|
xf = k->x;
|
||||||
|
}
|
||||||
|
return xf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stats_maybe_print(const struct cli *cli, struct follower_acc *acc,
|
static void stats_maybe_print(const struct cli *cli, struct follower_acc *acc,
|
||||||
|
|
@ -299,16 +304,16 @@ static void stats_maybe_print(const struct cli *cli, struct follower_acc *acc,
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
|
|
||||||
if (!cli->stats_interval_sec || !acc)
|
if (!cli->stats_interval_sec || !acc)
|
||||||
return;
|
goto out;
|
||||||
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
|
if (clock_gettime(CLOCK_MONOTONIC, &now) < 0)
|
||||||
return;
|
goto out;
|
||||||
if (last_mono->tv_sec == 0 && last_mono->tv_nsec == 0) {
|
if (last_mono->tv_sec == 0 && last_mono->tv_nsec == 0) {
|
||||||
*last_mono = now;
|
*last_mono = now;
|
||||||
return;
|
goto out;
|
||||||
}
|
}
|
||||||
if ((unsigned long)(now.tv_sec - last_mono->tv_sec) <
|
if ((unsigned long)(now.tv_sec - last_mono->tv_sec) <
|
||||||
cli->stats_interval_sec)
|
cli->stats_interval_sec)
|
||||||
return;
|
goto out;
|
||||||
if (mu)
|
if (mu)
|
||||||
pthread_mutex_lock(mu);
|
pthread_mutex_lock(mu);
|
||||||
stats_print_and_reset(stderr, cli->follower_paths, cli->n_followers,
|
stats_print_and_reset(stderr, cli->follower_paths, cli->n_followers,
|
||||||
|
|
@ -316,6 +321,8 @@ static void stats_maybe_print(const struct cli *cli, struct follower_acc *acc,
|
||||||
if (mu)
|
if (mu)
|
||||||
pthread_mutex_unlock(mu);
|
pthread_mutex_unlock(mu);
|
||||||
*last_mono = now;
|
*last_mono = now;
|
||||||
|
out:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void apply_correction(const struct cli *cli, uint64_t master,
|
static void apply_correction(const struct cli *cli, uint64_t master,
|
||||||
|
|
@ -332,7 +339,7 @@ static void apply_correction(const struct cli *cli, uint64_t master,
|
||||||
|
|
||||||
if (read_tsf_hex(slave_path, &slave) < 0) {
|
if (read_tsf_hex(slave_path, &slave) < 0) {
|
||||||
fprintf(stderr, "read follower TSF failed: %s\n", slave_path);
|
fprintf(stderr, "read follower TSF failed: %s\n", slave_path);
|
||||||
return;
|
goto out;
|
||||||
}
|
}
|
||||||
err = (int64_t)(master - slave);
|
err = (int64_t)(master - slave);
|
||||||
if (cli->use_kalman && kf_states)
|
if (cli->use_kalman && kf_states)
|
||||||
|
|
@ -355,10 +362,12 @@ static void apply_correction(const struct cli *cli, uint64_t master,
|
||||||
|
|
||||||
if (write_tsf_decimal(slave_path, new_tsf) < 0) {
|
if (write_tsf_decimal(slave_path, new_tsf) < 0) {
|
||||||
fprintf(stderr, "set follower TSF failed: %s\n", slave_path);
|
fprintf(stderr, "set follower TSF failed: %s\n", slave_path);
|
||||||
return;
|
goto out;
|
||||||
}
|
}
|
||||||
if (stats_all)
|
if (stats_all)
|
||||||
acc_add_sample(&stats_all[idx], err, step, stats_mu);
|
acc_add_sample(&stats_all[idx], err, step, stats_mu);
|
||||||
|
out:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ---------- single RT thread (default) ---------- */
|
/* ---------- single RT thread (default) ---------- */
|
||||||
|
|
@ -760,15 +769,16 @@ static int run_single(const struct cli *cli)
|
||||||
stats = calloc(cli->n_followers, sizeof(struct follower_acc));
|
stats = calloc(cli->n_followers, sizeof(struct follower_acc));
|
||||||
if (!stats) {
|
if (!stats) {
|
||||||
perror("calloc stats");
|
perror("calloc stats");
|
||||||
return 1;
|
ret = 1;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cli->use_kalman) {
|
if (cli->use_kalman) {
|
||||||
kf = calloc(cli->n_followers, sizeof(struct kf1));
|
kf = calloc(cli->n_followers, sizeof(struct kf1));
|
||||||
if (!kf) {
|
if (!kf) {
|
||||||
perror("calloc kf");
|
perror("calloc kf");
|
||||||
free(stats);
|
ret = 1;
|
||||||
return 1;
|
goto out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sctx.stats = stats;
|
sctx.stats = stats;
|
||||||
|
|
@ -786,6 +796,7 @@ static int run_single(const struct cli *cli)
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&attr);
|
pthread_attr_destroy(&attr);
|
||||||
|
out:
|
||||||
free(kf);
|
free(kf);
|
||||||
free(stats);
|
free(stats);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -800,9 +811,12 @@ int main(int argc, char **argv)
|
||||||
pr = parse_cli(argc, argv, &cli);
|
pr = parse_cli(argc, argv, &cli);
|
||||||
if (pr < 0) {
|
if (pr < 0) {
|
||||||
ret = 1;
|
ret = 1;
|
||||||
} else if (pr == 0) {
|
goto out;
|
||||||
|
}
|
||||||
|
if (pr == 0) {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
} else {
|
goto out;
|
||||||
|
}
|
||||||
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0)
|
if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0)
|
||||||
perror("mlockall (ignore if unprivileged)");
|
perror("mlockall (ignore if unprivileged)");
|
||||||
|
|
||||||
|
|
@ -813,8 +827,7 @@ int main(int argc, char **argv)
|
||||||
ret = run_parallel(&cli);
|
ret = run_parallel(&cli);
|
||||||
else
|
else
|
||||||
ret = run_single(&cli);
|
ret = run_single(&cli);
|
||||||
}
|
out:
|
||||||
|
|
||||||
free(cli.follower_paths);
|
free(cli.follower_paths);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue