1348e89c1fce082aa7467f1bc9ba329ed8503c0f
[blktrace.git] / btt / trace.c
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 static void __add_trace(struct io *iop)
24 {
25         time_t now = time(NULL);
26
27         n_traces++;
28         iostat_check_time(iop->t.time);
29
30         if (verbose && ((now - last_vtrace) > 0)) {
31
32 #       if defined(DEBUG)
33                 printf("%10lu t\ttree size=|%10d|\r", 
34                         n_traces, rb_tree_size);
35 #       else
36                 printf("%10lu t\r", n_traces);
37 #       endif
38
39                 if ((n_traces % 1000000) == 0) printf("\n");
40                 fflush(stdout);
41                 last_vtrace = now;
42         }
43
44         switch (iop->t.action & 0xffff) {
45         case __BLK_TA_QUEUE:            trace_queue(iop); break;
46         case __BLK_TA_REMAP:            trace_remap(iop); break;
47         case __BLK_TA_INSERT:           trace_insert(iop); break;
48         case __BLK_TA_GETRQ:            trace_getrq(iop); break;
49         case __BLK_TA_BACKMERGE:        trace_merge(iop); break;
50         case __BLK_TA_FRONTMERGE:       trace_merge(iop); break;
51         case __BLK_TA_REQUEUE:          trace_requeue(iop); break;
52         case __BLK_TA_ISSUE:            trace_issue(iop); break;
53         case __BLK_TA_COMPLETE:         trace_complete(iop); break;
54         case __BLK_TA_PLUG:             trace_plug(iop); break;
55         case __BLK_TA_UNPLUG_IO:        trace_unplug_io(iop); break;
56         case __BLK_TA_UNPLUG_TIMER:     trace_unplug_timer(iop); break;
57         default:                        
58                 io_release(iop); 
59                 return;
60         }
61 }
62
63 void add_trace(struct io *iop)
64 {
65         if (iop->t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
66                 if (iop->t.action == BLK_TN_PROCESS) {
67                         if (iop->t.pid == 0) 
68                                 add_process(0, "kernel");
69                         else {
70                                 char *slash = strchr(iop->pdu, '/');
71                                 if (slash)
72                                         *slash = '\0';
73
74                                 add_process(iop->t.pid, iop->pdu);
75                         }
76                 }
77                 io_release(iop);
78         }
79         else if (iop->t.action & BLK_TC_ACT(BLK_TC_PC))
80                 io_release(iop);
81         else {
82                 if (time_bounded) {
83                         if (BIT_TIME(iop->t.time) < t_astart) {
84                                 io_release(iop);
85                                 return;
86                         }
87                         else if (BIT_TIME(iop->t.time) > t_aend) {
88                                 io_release(iop);
89                                 done = 1;
90                                 return;
91                         }
92                 }
93                 __add_trace(iop);
94         }
95 }