summaryrefslogtreecommitdiff
path: root/btt/rstats.c
blob: 71f010bfaa27bfbe6d0fbc170f64e85873996c5d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * blktrace output analysis: generate a timeline & gather statistics
 *
 * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
 *	Alan D. Brunelle (alan.brunelle@hp.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "globals.h"

struct files {
	FILE *fp;
	char *nm;
};

struct rstat {
	struct list_head head;
	struct files files[2];
	unsigned long long ios, nblks;
	long long base_sec;
};

static struct rstat *sys_info;
static LIST_HEAD(rstats);

static int do_open(struct files *fip, char *bn, char *pn)
{
	fip->nm = malloc(sizeof(bn) + 16);
	sprintf(fip->nm, "%s_%s.dat", bn, pn);

	fip->fp = my_fopen(fip->nm, "w");
	if (fip->fp) {
		add_file(fip->fp, fip->nm);
		return 0;
	}

	free(fip->nm);
	return -1;
}

static int init_rsip(struct rstat *rsip, struct d_info *dip)
{
	char *nm = dip ? dip->dip_name : "sys";

	rsip->ios = rsip->nblks = 0;
	if (do_open(&rsip->files[0], nm, "iops_fp") ||
			    do_open(&rsip->files[1], nm, "mbps_fp"))
		return -1;

	list_add_tail(&rsip->head, &rstats);
	return 0;
}

static void rstat_emit(struct rstat *rsip, double cur)
{
	double mbps;

	/*
	 * I/Os per second is easy: just the ios
	 */
	fprintf(rsip->files[0].fp, "%lld %llu\n", rsip->base_sec, rsip->ios);

	/*
	 * MB/s we convert blocks to mb...
	 */
	mbps = ((double)rsip->nblks * 512.0) / (1024.0 * 1024.0);
	fprintf(rsip->files[1].fp, "%lld %lf\n", rsip->base_sec, mbps);

	rsip->base_sec = (unsigned long long)cur;
	rsip->ios = rsip->nblks = 0;
}

static void __add(struct rstat *rsip, double cur, unsigned long long nblks)
{
	if (rsip->base_sec < 0)
		rsip->base_sec = (long long)cur;
	else if (((long long)cur - rsip->base_sec) >= 1)
		rstat_emit(rsip, cur);

	rsip->ios++;
	rsip->nblks += nblks;
}

void *rstat_alloc(struct d_info *dip)
{
	struct rstat *rsip = malloc(sizeof(*rsip));

	if (!init_rsip(rsip, dip))
		return rsip;

	free(rsip);
	return NULL;
}

void rstat_free(void *ptr)
{
	struct rstat *rsip = ptr;

	rstat_emit(rsip, last_t_seen);
	list_del(&rsip->head);
	free(rsip);
}

void rstat_add(void *ptr, double cur, unsigned long long nblks)
{
	if (ptr != NULL)
		__add((struct rstat *)ptr, cur, nblks);
	__add(sys_info, cur, nblks);
}

int rstat_init(void)
{
	sys_info = rstat_alloc(NULL);
	return sys_info != NULL;
}

void rstat_exit(void)
{
	struct list_head *p, *q;

	list_for_each_safe(p, q, &rstats) {
		struct rstat *rsip = list_entry(p, struct rstat, head);
		rstat_free(rsip);
	}
}