btt/unplug_hist: fix bad memset
[blktrace.git] / btt / unplug_hist.c
CommitLineData
fc16a815
AB
1/*
2 * blktrace output analysis: generate a timeline & gather statistics
3 *
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21#include "globals.h"
22
23#define BKT_WIDTH 5
24#define MAX_BKT 19
25#define EXCESS_BKT 20
5683cbf6 26#define NBKTS (EXCESS_BKT + 1)
fc16a815
AB
27
28struct hist_bkt {
a155ab98 29 struct d_info *dip;
fc16a815
AB
30 int hist[NBKTS * sizeof(int)];
31};
32
a155ab98 33void *unplug_hist_alloc(struct d_info *dip)
fc16a815
AB
34{
35 struct hist_bkt *hbp;
36
d1422556
JA
37 if (unplug_hist_name == NULL)
38 return NULL;
fc16a815 39
d1422556 40 hbp = calloc(1, sizeof(*hbp));
a155ab98 41 hbp->dip = dip;
fc16a815
AB
42
43 return hbp;
44}
45
46void unplug_hist_add(struct io *u_iop)
47{
48 struct d_info *dip;
49
fc16a815 50 dip = __dip_find(u_iop->t.device);
c053af42 51 if (dip && dip->up_hist_handle) {
fc16a815 52 __u64 *val = u_iop->pdu;
5406b971 53 int idx, n_unplugs = be64_to_cpu(*val);
c053af42 54 struct hist_bkt *hbp = dip->up_hist_handle;
5406b971
AB
55
56 idx = (n_unplugs / BKT_WIDTH);
5683cbf6 57 if (idx > EXCESS_BKT)
5406b971 58 idx = EXCESS_BKT;
fc16a815 59
fc16a815
AB
60 hbp->hist[idx]++;
61 }
62}
63
c053af42 64void unplug_hist_free(void *arg)
fc16a815
AB
65{
66 if (arg) {
67 FILE *fp;
68 struct hist_bkt *hbp = arg;
a155ab98
AB
69 size_t tlen = strlen(unplug_hist_name)
70 + strlen(hbp->dip->dip_name) + 32;
71 char *oname = malloc(tlen);
fc16a815 72
a155ab98
AB
73 sprintf(oname, "%s_%s.dat", unplug_hist_name,
74 hbp->dip->dip_name);
8f34184f 75 if ((fp = my_fopen(oname, "w")) != NULL) {
fc16a815
AB
76 int i;
77
78 for (i = 0; i < NBKTS; i++)
79 fprintf(fp, "%d %d\n", i, hbp->hist[i]);
80 fclose(fp);
81
c053af42 82 } else
fc16a815
AB
83 perror(oname);
84
85 free(oname);
86 free(hbp);
87 }
88}