Home | History | Annotate | Download | only in btt
      1 /*
      2  * blktrace output analysis: generate a timeline & gather statistics
      3  *
      4  * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle (at) 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 		printf("%10lu t (%6.2lf%%)\r", n_traces, pct_done());
     32 		if ((n_traces % 1000000) == 0) printf("\n");
     33 		fflush(stdout);
     34 		last_vtrace = now;
     35 	}
     36 
     37 	switch (iop->t.action & 0xffff) {
     38 	case __BLK_TA_QUEUE:		trace_queue(iop); break;
     39 	case __BLK_TA_REMAP:		trace_remap(iop); break;
     40 	case __BLK_TA_INSERT:		trace_insert(iop); break;
     41 	case __BLK_TA_GETRQ:		trace_getrq(iop); break;
     42 	case __BLK_TA_BACKMERGE:	trace_merge(iop); break;
     43 	case __BLK_TA_FRONTMERGE:	trace_merge(iop); break;
     44 	case __BLK_TA_REQUEUE:		trace_requeue(iop); break;
     45 	case __BLK_TA_ISSUE:		trace_issue(iop); break;
     46 	case __BLK_TA_COMPLETE:		trace_complete(iop); break;
     47 	case __BLK_TA_PLUG:		trace_plug(iop); break;
     48 	case __BLK_TA_UNPLUG_IO:	trace_unplug_io(iop); break;
     49 	case __BLK_TA_UNPLUG_TIMER:	trace_unplug_timer(iop); break;
     50 	case __BLK_TA_SLEEPRQ:		trace_sleeprq(iop); break;
     51 	default:
     52 		io_release(iop);
     53 		return;
     54 	}
     55 }
     56 
     57 static void trace_message(struct io *iop)
     58 {
     59 	char scratch[15];
     60 	char msg[iop->t.pdu_len + 1];
     61 
     62 	if (!io_setup(iop, IOP_M))
     63 		return;
     64 
     65 	memcpy(msg, iop->pdu, iop->t.pdu_len);
     66 	msg[iop->t.pdu_len] = '\0';
     67 
     68 	fprintf(msgs_ofp, "%s %5d.%09lu %s\n",
     69 		make_dev_hdr(scratch, 15, iop->dip, 1),
     70 		(int)SECONDS(iop->t.time),
     71 		(unsigned long)NANO_SECONDS(iop->t.time), msg);
     72 }
     73 
     74 void add_trace(struct io *iop)
     75 {
     76 	if (iop->t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
     77 		if (iop->t.action == BLK_TN_PROCESS) {
     78 			if (iop->t.pid == 0)
     79 				process_alloc(0, "kernel");
     80 			else {
     81 				char *slash = strchr(iop->pdu, '/');
     82 				if (slash)
     83 					*slash = '\0';
     84 
     85 				process_alloc(iop->t.pid, iop->pdu);
     86 			}
     87 		} else if (iop->t.action == BLK_TN_MESSAGE)
     88 			trace_message(iop);
     89 		io_release(iop);
     90 	} else if (iop->t.action & BLK_TC_ACT(BLK_TC_PC)) {
     91 		io_release(iop);
     92 	} else {
     93 		if (time_bounded) {
     94 			if (BIT_TIME(iop->t.time) < t_astart) {
     95 				io_release(iop);
     96 				return;
     97 			} else if (BIT_TIME(iop->t.time) > t_aend) {
     98 				io_release(iop);
     99 				done = 1;
    100 				return;
    101 			}
    102 		}
    103 		__add_trace(iop);
    104 	}
    105 }
    106