summaryrefslogtreecommitdiff
path: root/src/stats.c
blob: 76b8db238363b26d8b1d8165c588d62b52fa7ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* License: BSD-style license
* Copyright: Radek Podgorny <radek@podgorny.cz>,
*/

#include <stdio.h>
#include <string.h>

#include "stats.h"
#include "opts.h"


void stats_init(struct stats_t *s) {
	memset(s, 0, sizeof(struct stats_t));
}

void stats_sprint(struct stats_t *s, char *out) {
	strcpy(out, "");

	sprintf(out+strlen(out), "Bytes read: %u,%03u,%03u,%03u,%03u\n", s->r_t, s->r_g, s->r_m, s->r_k, s->r_b);
	sprintf(out+strlen(out), "Bytes written: %u,%03u,%03u,%03u,%03u\n", s->w_t, s->w_g, s->w_m, s->w_k, s->w_b);
}

void stats_add_read(struct stats_t *s, unsigned int bytes) {
	s->r_b += bytes;

	while (s->r_b >= 1000) { s->r_k++; s->r_b -= 1000; }
	while (s->r_k >= 1000) { s->r_m++; s->r_k -= 1000; }
	while (s->r_m >= 1000) { s->r_g++; s->r_m -= 1000; }
	while (s->r_g >= 1000) { s->r_t++; s->r_g -= 1000; }
}

void stats_add_written(struct stats_t *s, unsigned int bytes) {
	s->w_b += bytes;

	while (s->w_b >= 1000) { s->w_k++; s->w_b -= 1000; }
	while (s->w_k >= 1000) { s->w_m++; s->w_k -= 1000; }
	while (s->w_m >= 1000) { s->w_g++; s->w_m -= 1000; }
	while (s->w_g >= 1000) { s->w_t++; s->w_g -= 1000; }
}