gatherer.c 1.06 KB
Newer Older
1
#include <config.h>
2 3
#include <pthread.h>

4
#include "knot/stat/stat-common.h"
Lubos Slovak's avatar
Lubos Slovak committed
5
#include "common/slab/malloc.h"
6
#include "knot/stat/gatherer.h"
7

8
gatherer_t *new_gatherer()
9
{
10
	gatherer_t *ret;
11

12 13 14 15
	if ((ret = malloc(sizeof(gatherer_t))) == NULL) {
		ERR_ALLOC_FAILED;
		return NULL;
	}
16

17
	pthread_mutex_init(&ret->mutex_read, NULL);
18

19 20 21 22
	for (int i = 0; i < FREQ_BUFFER_SIZE; i++) {
		ret->freq_array[i] = 0;
		ret->flow_array[i] = NULL;
	}
23

24 25 26
	ret->qps = 0.0;
	ret->udp_qps = 0.0;
	ret->tcp_qps = 0.0;
27

28 29 30 31
	/*  currently disabled */
	/*  ret->mean_latency = 0.0;
	    ret->udp_mean_latency = 0.0;
	    ret->tcp_mean_latency = 0.0;
32

33 34
	    ret->udp_latency = 0;
	    ret->tcp_latency = 0; */
35

36 37
	ret->udp_queries = 0;
	ret->tcp_queries = 0;
38

39
	return ret;
40 41
}

42
void gatherer_free(gatherer_t *gath)
43
{
44 45 46 47 48 49
	for (int i = 0; i < FREQ_BUFFER_SIZE; i++) {
		if (gath->flow_array[i] != NULL) {
			free(gath->flow_array[i]->addr);
			free(gath->flow_array[i]);
		}
	}
50

51
	pthread_mutex_destroy(&(gath->mutex_read));
52

53
	pthread_cancel(gath->sleeper_thread);
54

55
	pthread_join((gath->sleeper_thread), NULL);
56

57 58
	free(gath);
}