Separated out g/i/m trace handling.
[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
26#define NBKTS EXCESS_BKT
27
28struct hist_bkt {
29 __u32 dev;
30 int hist[NBKTS * sizeof(int)];
31};
32
fc16a815
AB
33void *unplug_hist_init(__u32 device)
34{
35 struct hist_bkt *hbp;
36
37 if (unplug_hist_name == NULL) return NULL;
38
39 hbp = malloc(sizeof(*hbp));
40 hbp->dev = device;
41 memset(hbp->hist, 0, NBKTS * sizeof(int));
42
43 return hbp;
44}
45
46void unplug_hist_add(struct io *u_iop)
47{
48 struct d_info *dip;
49
50 assert(u_iop->t.pdu_len >= sizeof(__u64));
51
52 dip = __dip_find(u_iop->t.device);
53 if (dip && dip->unplug_hist_handle) {
54 __u64 *val = u_iop->pdu;
5406b971
AB
55 int idx, n_unplugs = be64_to_cpu(*val);
56 struct hist_bkt *hbp = dip->unplug_hist_handle;
57
58 idx = (n_unplugs / BKT_WIDTH);
59 if (idx > MAX_BKT)
60 idx = EXCESS_BKT;
fc16a815
AB
61
62 assert((0 <= idx) && (idx <= EXCESS_BKT));
63 hbp->hist[idx]++;
64 }
65}
66
67void unplug_hist_exit(void *arg)
68{
69 if (arg) {
70 FILE *fp;
71 struct hist_bkt *hbp = arg;
72 int mjr = hbp->dev >> MINORBITS;
73 int mnr = hbp->dev & ((1 << MINORBITS) - 1);
74 char *oname = malloc(strlen(unplug_hist_name) + 32);
75
76 sprintf(oname, "%s_%03d,%03d.dat", unplug_hist_name, mjr, mnr);
77 if ((fp = fopen(oname, "w")) != NULL) {
78 int i;
79
80 for (i = 0; i < NBKTS; i++)
81 fprintf(fp, "%d %d\n", i, hbp->hist[i]);
82 fclose(fp);
83
84 }
85 else
86 perror(oname);
87
88 free(oname);
89 free(hbp);
90 }
91}