Merge branch 'master' of ssh://git.kernel.dk/data/git/blktrace
[blktrace.git] / btt / trace.c
CommitLineData
63eba147
JA
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
095181f2
JA
23int dump_level;
24LIST_HEAD(retries);
63eba147 25
d76c5b81 26void __dump_iop(FILE *ofp, struct io *iop, int extra_nl)
63eba147 27{
d76c5b81
AB
28 fprintf(ofp, "%5d.%09lu %3d,%-3d %c %10llu+%-4u\n",
29 (int)SECONDS(iop->t.time),
30 (unsigned long)NANO_SECONDS(iop->t.time),
31 MAJOR(iop->t.device), MINOR(iop->t.device), type2c(iop->type),
32 (unsigned long long)iop->t.sector, t_sec(&iop->t));
33 if (extra_nl) fprintf(ofp, "\n");
63eba147
JA
34}
35
d76c5b81 36void __dump_iop2(FILE *ofp, struct io *a_iop, struct io *l_iop)
63eba147 37{
d76c5b81
AB
38 fprintf(ofp, "%5d.%09lu %3d,%-3d %c %10llu+%-4u <- (%3d,%-3d) %10llu\n",
39 (int)SECONDS(a_iop->t.time),
40 (unsigned long)NANO_SECONDS(a_iop->t.time),
41 MAJOR(a_iop->t.device), MINOR(a_iop->t.device),
42 type2c(a_iop->type), (unsigned long long)a_iop->t.sector,
43 t_sec(&a_iop->t), MAJOR(l_iop->t.device),
44 MINOR(l_iop->t.device), (unsigned long long)l_iop->t.sector);
63eba147
JA
45}
46
d76c5b81 47void release_iops(struct list_head *rmhd)
63eba147 48{
095181f2
JA
49 struct io *x_iop;
50 struct list_head *p, *q;
51
d76c5b81 52 list_for_each_safe(p, q, rmhd) {
095181f2
JA
53 x_iop = list_entry(p, struct io, f_head);
54 LIST_DEL(&x_iop->f_head);
55 io_release(x_iop);
56 }
63eba147
JA
57}
58
d76c5b81 59void do_retries(__u64 now)
63eba147 60{
095181f2
JA
61 struct io *iop;
62 struct list_head *p, *q;
63
64 list_for_each_safe(p, q, &retries) {
65 iop = list_entry(p, struct io, retry);
d76c5b81
AB
66 ASSERT(iop->type == IOP_C);
67
095181f2 68 // iop could be gone after call...
d76c5b81 69 retry_complete(iop, now);
63eba147 70 }
63eba147
JA
71}
72
d76c5b81
AB
73static inline int retry_check_time(__u64 t)
74{
75 return next_retry_check && (t > next_retry_check);
76}
77
095181f2 78static void __add_trace(struct io *iop)
63eba147 79{
6eb42155 80 time_t now = time(NULL);
d76c5b81
AB
81 __u64 tstamp = iop->t.time;
82 int run_retry = retry_check_time(iop->t.time);
63eba147 83
6eb42155 84 n_traces++;
21e47d90 85 iostat_check_time(iop->t.time);
6eb42155
ADB
86
87 if (verbose && ((now - last_vtrace) > 0)) {
d76c5b81
AB
88#if defined(DEBUG)
89 printf("%10lu t\tretries=|%10d|\ttree size=|%10d|\r",
90 n_traces, list_len(&retries), rb_tree_size);
91#else
6eb42155 92 printf("%10lu t\r", n_traces);
d76c5b81 93#endif
63eba147 94 if ((n_traces % 1000000) == 0) printf("\n");
6eb42155
ADB
95 fflush(stdout);
96 last_vtrace = now;
63eba147
JA
97 }
98
99 switch (iop->t.action & 0xffff) {
095181f2
JA
100 case __BLK_TA_QUEUE: trace_queue(iop); break;
101 case __BLK_TA_REMAP: trace_remap(iop); break;
6dee1e20 102 case __BLK_TA_GETRQ: trace_insert(iop); break;
095181f2
JA
103 case __BLK_TA_BACKMERGE: trace_merge(iop); break;
104 case __BLK_TA_FRONTMERGE: trace_merge(iop); break;
105 case __BLK_TA_REQUEUE: trace_requeue(iop); break;
106 case __BLK_TA_ISSUE: trace_issue(iop); break;
107 case __BLK_TA_COMPLETE: trace_complete(iop); break;
108 default:
109 io_release(iop);
110 return;
63eba147 111 }
095181f2 112
d76c5b81
AB
113 if (run_retry && !list_empty(&retries)) {
114 do_retries(tstamp);
115 bump_retry(tstamp);
116 }
63eba147
JA
117}
118
119void add_trace(struct io *iop)
120{
121 if (iop->t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
d76c5b81
AB
122 if (iop->t.pid == 0)
123 add_process(0, "kernel");
124 else {
125 char *slash = strchr(iop->pdu, '/');
126 if (slash)
127 *slash = '\0';
128
129 add_process(iop->t.pid, iop->pdu);
130 }
6eb42155 131 io_release(iop);
63eba147 132 }
f8d501e1 133 else if (iop->t.action & BLK_TC_ACT(BLK_TC_PC))
6eb42155 134 io_release(iop);
001b2633
JA
135 else {
136 if (time_bounded) {
137 if (BIT_TIME(iop->t.time) < t_astart) {
138 io_release(iop);
139 return;
140 }
141 else if (BIT_TIME(iop->t.time) > t_aend) {
142 io_release(iop);
143 done = 1;
144 return;
145 }
146 }
63eba147 147 __add_trace(iop);
001b2633 148 }
63eba147 149}