Blame tc/tc_estimator.c

Packit d3f73b
/*
Packit d3f73b
 * tc_core.c		TC core library.
Packit d3f73b
 *
Packit d3f73b
 *		This program is free software; you can redistribute it and/or
Packit d3f73b
 *		modify it under the terms of the GNU General Public License
Packit d3f73b
 *		as published by the Free Software Foundation; either version
Packit d3f73b
 *		2 of the License, or (at your option) any later version.
Packit d3f73b
 *
Packit d3f73b
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Packit d3f73b
 *
Packit d3f73b
 */
Packit d3f73b
Packit d3f73b
#include <stdio.h>
Packit d3f73b
#include <stdlib.h>
Packit d3f73b
#include <unistd.h>
Packit d3f73b
#include <fcntl.h>
Packit d3f73b
#include <math.h>
Packit d3f73b
#include <sys/socket.h>
Packit d3f73b
#include <netinet/in.h>
Packit d3f73b
#include <arpa/inet.h>
Packit d3f73b
#include <string.h>
Packit d3f73b
Packit d3f73b
#include "utils.h"
Packit d3f73b
#include "tc_core.h"
Packit d3f73b
Packit d3f73b
int tc_setup_estimator(unsigned int A, unsigned int time_const, struct tc_estimator *est)
Packit d3f73b
{
Packit d3f73b
	for (est->interval = 0; est->interval <= 5; est->interval++) {
Packit d3f73b
		if (A <= (1<<est->interval)*(TIME_UNITS_PER_SEC/4))
Packit d3f73b
			break;
Packit d3f73b
	}
Packit d3f73b
	if (est->interval > 5)
Packit d3f73b
		return -1;
Packit d3f73b
	est->interval -= 2;
Packit d3f73b
	for (est->ewma_log = 1; est->ewma_log < 32; est->ewma_log++) {
Packit d3f73b
		double w = 1.0 - 1.0/(1<<est->ewma_log);
Packit d3f73b
Packit d3f73b
		if (A/(-log(w)) > time_const)
Packit d3f73b
			break;
Packit d3f73b
	}
Packit d3f73b
	est->ewma_log--;
Packit d3f73b
	if (est->ewma_log == 0 || est->ewma_log >= 31)
Packit d3f73b
		return -1;
Packit d3f73b
	return 0;
Packit d3f73b
}