pax_global_header00006660000000000000000000000064117226274510014521gustar00rootroot0000000000000052 comment=d6918c8832793b4205ed3bfede78c2f915c23385 blktrace-1.0.5/000077500000000000000000000000001172262745100133135ustar00rootroot00000000000000blktrace-1.0.5/.gitignore000066400000000000000000000002351172262745100153030ustar00rootroot00000000000000.depend *.o blkparse blktrace blkrawverify blkiomon btreplay/btrecord btreplay/btreplay doc/.depend verify_blkparse btt/btt btt/doc/btt.pdf doc/blktrace.pdf blktrace-1.0.5/COPYING000066400000000000000000000431311172262745100143500ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. blktrace-1.0.5/Makefile000066400000000000000000000037101172262745100147540ustar00rootroot00000000000000CC = gcc CFLAGS = -Wall -O2 -g -W ALL_CFLAGS = $(CFLAGS) -D_GNU_SOURCE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 PROGS = blkparse blktrace verify_blkparse blkrawverify blkiomon LIBS = -lpthread SCRIPTS = btrace ALL = $(PROGS) $(SCRIPTS) btt/btt btreplay/btrecord btreplay/btreplay \ btt/bno_plot.py all: $(ALL) btt/btt: $(MAKE) -C btt btreplay/btrecord: $(MAKE) -C btreplay btreplay/btreplay: $(MAKE) -C btreplay %.o: %.c $(CC) -o $*.o -c $(ALL_CFLAGS) $< blkparse: blkparse.o blkparse_fmt.o rbtree.o act_mask.o $(CC) $(ALL_CFLAGS) -o $@ $(filter %.o,$^) blktrace: blktrace.o act_mask.o $(CC) $(ALL_CFLAGS) -o $@ $(filter %.o,$^) $(LIBS) verify_blkparse: verify_blkparse.o $(CC) $(ALL_CFLAGS) -o $@ $(filter %.o,$^) blkrawverify: blkrawverify.o $(CC) $(ALL_CFLAGS) -o $@ $(filter %.o,$^) blkiomon: blkiomon.o rbtree.o $(CC) $(ALL_CFLAGS) -o $@ $(filter %.o,$^) $(LIBS) -lrt $(PROGS): | depend docs: $(MAKE) -C doc all $(MAKE) -C btt docs $(MAKE) -C btreplay docs docsclean: $(MAKE) -C doc clean $(MAKE) -C btt clean $(MAKE) -C btreplay clean depend: @$(CC) -MM $(ALL_CFLAGS) *.c 1> .depend INSTALL = install prefix = /usr/local bindir = $(prefix)/bin mandir = $(prefix)/man RPMBUILD = rpmbuild TAR = tar export prefix INSTALL TAR dist: btrace.spec git-tar-tree HEAD btrace-1.0 > btrace-1.0.tar @mkdir -p btrace-1.0 @cp btrace.spec btrace-1.0 $(TAR) rf btrace-1.0.tar btrace-1.0/btrace.spec @rm -rf btrace-1.0 @bzip2 btrace-1.0.tar rpm: dist $(RPMBUILD) -ta btrace-1.0.tar.bz2 clean: docsclean -rm -f *.o $(PROGS) .depend btrace-1.0.tar.bz2 $(MAKE) -C btt clean $(MAKE) -C btreplay clean install: all $(INSTALL) -m 755 -d $(DESTDIR)$(bindir) $(INSTALL) -m 755 -d $(DESTDIR)$(mandir)/man1 $(INSTALL) -m 755 -d $(DESTDIR)$(mandir)/man8 $(INSTALL) -m 755 $(ALL) $(DESTDIR)$(bindir) $(INSTALL) -m 644 doc/*.1 $(DESTDIR)$(mandir)/man1 $(INSTALL) -m 644 doc/*.8 $(DESTDIR)$(mandir)/man8 ifneq ($(wildcard .depend),) include .depend endif blktrace-1.0.5/README000066400000000000000000000116461172262745100142030ustar00rootroot00000000000000Block IO Tracing ---------------- Written by Jens Axboe (initial version and kernel support), Alan D. Brunelle (threading and splitup into two seperate programs), Nathan Scott (bug fixes, process names, multiple devices) Also thanks to Tom Zanussi for good input and patches. Requirements ------------ blktrace was integrated into the mainline kernel between 2.6.16 and 2.6.17-rc1. The target trace needs to run on a kernel at least that new. git://git.kernel.dk/blktrace.git If you don't have git, you can get hourly snapshots from: http://brick.kernel.dk/snaps/ The snapshots include the full git object database as well. kernel.org has excessively long mirror times, so if you have git installed, you can pull the master tree from: git://git.kernel.dk/blktrace.git For browsing the repo over http and viewing history etc, you can direct your browser to: http://git.kernel.dk/ Usage ----- $ blktrace -d [ -r debug_path ] [ -o output ] [ -k ] [ -w time ] [ -a action ] [ -A action mask ] -d Use specified device. May also be given last after options. -r Path to mounted debugfs, defaults to /sys/kernel/debug. -o File(s) to send output to. -D Directory to prepend to output file names. -k Kill running trace. -w Stop after defined time, in seconds. -a Only trace specific actions (use more -a options to add actions). Available actions are: READ WRITE BARRIER SYNC QUEUE REQUEUE ISSUE COMPLETE FS PC -A Give the trace mask directly as a number. -b Sub buffer size in KiB. -n Number of sub buffers. -l Run in network listen mode (blktrace server) -h Run in network client mode, connecting to the given host -p Network port to use (default 8462) -s Disable network client use of sendfile() to transfer data -V Print program version info. $ blkparse -i [ -o ] [ -b rb_batch ] [ -s ] [ -t ] [ -q ] [ -w start:stop ] [ -f output format ] [ -F format spec ] [ -d ] -i Input file containing trace data, or '-' for stdin. -D Directory to prepend to input file names. -o Output file. If not given, output is stdout. -b stdin read batching. -s Show per-program io statistics. -h Hash processes by name, not pid. -t Track individual ios. Will tell you the time a request took to get queued, to get dispatched, and to get completed. -q Quiet. Don't display any stats at the end of the trace. -w Only parse data between the given time interval in seconds. If 'start' isn't given, blkparse defaults the start time to 0. -d Dump sorted data in binary format -f Output format. Customize the output format. The format field identifiers are: %a - Action %c - CPU ID %C - Task command (process) name %d - Direction (r/w) %D - Device number %e - Error number %M - Major %m - Minor %N - Number of bytes %n - Number of sectors %p - PID %P - PDU %s - Sequence number %S - Sector number %t - Time (wallclock - nanoseconds) %T - Time (wallclock - seconds) %u - Time (processing - microseconds) %U - Unplug depth -F Format specification. The individual specifiers are: A - Remap B - Bounce C - Complete D - Issue M - Back merge F - Front merge G - Get request I - Insert P - Plug Q - Queue R - Requeue S - Sleep requests T - Unplug timer U - Unplug IO W - Bounce X - Split -v More verbose for marginal errors. -V Print program version info. $ verify_blkparse filename Verifies an output file from blkparse. All it does is check if the events in the file are correctly time ordered. If an entry is found that isn't ordered, it's dumped to stdout. $ blkrawverify [...] The blkrawverify utility can be used to verify data retrieved via blktrace. It will check for valid event formats, forward progressing sequence numbers and time stamps, also does reasonable checks for other potential issues within invidividual events. Errors found will be tracked in .verify.out. If you want to do live tracing, you can pipe the data between blktrace and blkparse: % blktrace -d -o - | blkparse -i - This has a small risk of displaying some traces a little out of sync, since it will do batch sorts of input events. Similarly, you can do traces over the network. The network 'server' must run: % blktrace -l to listen to incoming blktrace connections, while the client should use % blktrace -d /dev/sda -h to connect and transfer data over the network. Documentation ------------- A users guide is distributed with the source. It is in latex, a 'make docs' will build a PDF in doc/. You need tetex and latex installed to build the document. Resources --------- vger hosts a mailing list dedicated to btrace discussion and development. The list is called linux-btrace@vger.kernel.org, subscribe by sending a mail to majordomo@vger.kernel.org with 'subscribe linux-btrace' in the mail body. 2006-09-05, Jens Axboe blktrace-1.0.5/act_mask.c000066400000000000000000000021531172262745100152420ustar00rootroot00000000000000#include #include "blktrace.h" #define DECLARE_MASK_MAP(mask) { BLK_TC_##mask, #mask, "BLK_TC_"#mask } #define COMPARE_MASK_MAP(mmp, str) \ (!strcasecmp((mmp)->short_form, (str)) || \ !strcasecmp((mmp)->long_form, (str))) struct mask_map { int mask; char *short_form; char *long_form; }; static struct mask_map mask_maps[] = { DECLARE_MASK_MAP(READ), DECLARE_MASK_MAP(WRITE), DECLARE_MASK_MAP(FLUSH), DECLARE_MASK_MAP(SYNC), DECLARE_MASK_MAP(QUEUE), DECLARE_MASK_MAP(REQUEUE), DECLARE_MASK_MAP(ISSUE), DECLARE_MASK_MAP(COMPLETE), DECLARE_MASK_MAP(FS), DECLARE_MASK_MAP(PC), DECLARE_MASK_MAP(NOTIFY), DECLARE_MASK_MAP(AHEAD), DECLARE_MASK_MAP(META), DECLARE_MASK_MAP(DISCARD), DECLARE_MASK_MAP(DRV_DATA), DECLARE_MASK_MAP(FUA), }; int find_mask_map(char *string) { unsigned int i; for (i = 0; i < sizeof(mask_maps)/sizeof(mask_maps[0]); i++) if (COMPARE_MASK_MAP(&mask_maps[i], string)) return mask_maps[i].mask; return -1; } int valid_act_opt(int x) { return (1 <= x) && (x < (1 << BLK_TC_SHIFT)); } blktrace-1.0.5/barrier.h000066400000000000000000000025221172262745100151130ustar00rootroot00000000000000#ifndef BARRIER_H #define BARRIER_H #if defined(__ia64__) #define store_barrier() asm volatile ("mf" ::: "memory") #elif defined(__x86_64__) #define store_barrier() asm volatile("sfence" ::: "memory") #elif defined(__i386__) #define store_barrier() asm volatile ("": : :"memory") #elif defined(__ppc__) || defined(__powerpc__) #define store_barrier() asm volatile ("eieio" : : : "memory") #elif defined(__s390__) || defined(__s390x__) #define store_barrier() asm volatile ("bcr 15,0" : : : "memory") #elif defined(__alpha__) #define store_barrier() asm volatile("wmb": : :"memory") #elif defined(__hppa__) #define store_barrier() asm volatile("":::"memory") #elif defined(__sparc__) #define store_barrier() asm volatile("":::"memory") #elif defined(__m68000__) || defined(__m68k__) || defined(mc68000) || defined(_M_M68K) #define store_barrier() asm volatile("":::"memory") #elif defined(__mips__) /* also mipsel */ #define store_barrier() do { } while(0) #elif defined(__arm__) /* taken from linux/arch/arm/kernel/entry-armv.S, thanks to pbrook! */ typedef void (__kernel_dmb_t)(void); #define __kernel_dmb (*(__kernel_dmb_t *)0xffff0fa0) #define store_barrier() __kernel_dmb() #else #error Define store_barrier() for your CPU #endif #endif blktrace-1.0.5/blkiomon.c000066400000000000000000000414711172262745100153000ustar00rootroot00000000000000/* * I/O monitor based on block queue trace data * * Copyright IBM Corp. 2008 * * Author(s): Martin Peschke * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "blktrace.h" #include "rbtree.h" #include "jhash.h" #include "blkiomon.h" struct trace { struct blk_io_trace bit; struct rb_node node; struct trace *next; long sequence; }; struct rb_search { struct rb_node **node_ptr; struct rb_node *parent; }; struct dstat_msg { long mtype; struct blkiomon_stat stat; }; struct dstat { struct dstat_msg msg; struct rb_node node; struct dstat *next; }; struct output { char *fn; FILE *fp; char *buf; int pipe; }; static char blkiomon_version[] = "0.3"; static FILE *ifp; static int interval = -1; static struct trace *vacant_traces_list = NULL; static int vacant_traces = 0; #define TRACE_HASH_SIZE 128 struct trace *thash[TRACE_HASH_SIZE] = {}; static struct dstat *vacant_dstats_list = NULL; static struct rb_root dstat_tree[2] = { RB_ROOT, RB_ROOT }; static struct dstat *dstat_list[2] = {}; static int dstat_curr = 0; static struct output drvdata, human, binary, debug; static char *msg_q_name = NULL; static int msg_q_id = -1, msg_q = -1; static long msg_id = -1; static pthread_t interval_thread; static pthread_mutex_t dstat_mutex = PTHREAD_MUTEX_INITIALIZER; int data_is_native = -1; static int up = 1; /* debugging */ static long leftover = 0, driverdata = 0, match = 0, mismatch = 0, sequence = 0; static void dump_bit(struct trace *t, const char *descr) { struct blk_io_trace *bit = &t->bit; if (!debug.fn) return; fprintf(debug.fp, "--- %s ---\n", descr); fprintf(debug.fp, "magic %16d\n", bit->magic); fprintf(debug.fp, "sequence %16d\n", bit->sequence); fprintf(debug.fp, "time %16ld\n", (unsigned long)bit->time); fprintf(debug.fp, "sector %16ld\n", (unsigned long)bit->sector); fprintf(debug.fp, "bytes %16d\n", bit->bytes); fprintf(debug.fp, "action %16x\n", bit->action); fprintf(debug.fp, "pid %16d\n", bit->pid); fprintf(debug.fp, "device %16d\n", bit->device); fprintf(debug.fp, "cpu %16d\n", bit->cpu); fprintf(debug.fp, "error %16d\n", bit->error); fprintf(debug.fp, "pdu_len %16d\n", bit->pdu_len); fprintf(debug.fp, "order %16ld\n", t->sequence); } static void dump_bits(struct trace *t1, struct trace *t2, const char *descr) { struct blk_io_trace *bit1 = &t1->bit; struct blk_io_trace *bit2 = &t2->bit; if (!debug.fn) return; fprintf(debug.fp, "--- %s ---\n", descr); fprintf(debug.fp, "magic %16d %16d\n", bit1->magic, bit2->magic); fprintf(debug.fp, "sequence %16d %16d\n", bit1->sequence, bit2->sequence); fprintf(debug.fp, "time %16ld %16ld\n", (unsigned long)bit1->time, (unsigned long)bit2->time); fprintf(debug.fp, "sector %16ld %16ld\n", (unsigned long)bit1->sector, (unsigned long)bit2->sector); fprintf(debug.fp, "bytes %16d %16d\n", bit1->bytes, bit2->bytes); fprintf(debug.fp, "action %16x %16x\n", bit1->action, bit2->action); fprintf(debug.fp, "pid %16d %16d\n", bit1->pid, bit2->pid); fprintf(debug.fp, "device %16d %16d\n", bit1->device, bit2->device); fprintf(debug.fp, "cpu %16d %16d\n", bit1->cpu, bit2->cpu); fprintf(debug.fp, "error %16d %16d\n", bit1->error, bit2->error); fprintf(debug.fp, "pdu_len %16d %16d\n", bit1->pdu_len, bit2->pdu_len); fprintf(debug.fp, "order %16ld %16ld\n", t1->sequence, t2->sequence); } static struct dstat *blkiomon_alloc_dstat(void) { struct dstat *dstat; if (vacant_dstats_list) { dstat = vacant_dstats_list; vacant_dstats_list = dstat->next; } else dstat = malloc(sizeof(*dstat)); if (!dstat) { fprintf(stderr, "blkiomon: could not allocate device statistic"); return NULL; } blkiomon_stat_init(&dstat->msg.stat); return dstat; } static struct dstat *blkiomon_find_dstat(struct rb_search *search, __u32 device) { struct rb_node **p = &(dstat_tree[dstat_curr].rb_node); struct rb_node *parent = NULL; struct dstat *dstat; while (*p) { parent = *p; dstat = rb_entry(parent, struct dstat, node); if (dstat->msg.stat.device < device) p = &(*p)->rb_left; else if (dstat->msg.stat.device > device) p = &(*p)->rb_right; else return dstat; } search->node_ptr = p; search->parent = parent; return NULL; } static struct dstat *blkiomon_get_dstat(__u32 device) { struct dstat *dstat; struct rb_search search; pthread_mutex_lock(&dstat_mutex); dstat = blkiomon_find_dstat(&search, device); if (dstat) goto out; dstat = blkiomon_alloc_dstat(); if (!dstat) goto out; dstat->msg.stat.device = device; rb_link_node(&dstat->node, search.parent, search.node_ptr); rb_insert_color(&dstat->node, &dstat_tree[dstat_curr]); dstat->next = dstat_list[dstat_curr]; dstat_list[dstat_curr] = dstat; out: pthread_mutex_unlock(&dstat_mutex); return dstat; } static int blkiomon_output_msg_q(struct dstat *dstat) { if (!msg_q_name) return 0; dstat->msg.mtype = msg_id; return msgsnd(msg_q, &dstat->msg, sizeof(struct blkiomon_stat), 0); } static int blkiomon_output_binary(struct dstat *dstat) { struct blkiomon_stat *p = &dstat->msg.stat; if (!binary.fn) return 0; if (fwrite(p, sizeof(*p), 1, binary.fp) != 1) goto failed; if (binary.pipe && fflush(binary.fp)) goto failed; return 0; failed: fprintf(stderr, "blkiomon: could not write to %s\n", binary.fn); fclose(binary.fp); binary.fn = NULL; return 1; } static struct dstat *blkiomon_output(struct dstat *head, struct timespec *ts) { struct dstat *dstat, *tail = NULL; for (dstat = head; dstat; dstat = dstat->next) { dstat->msg.stat.time = ts->tv_sec; blkiomon_stat_print(human.fp, &dstat->msg.stat); blkiomon_stat_to_be(&dstat->msg.stat); blkiomon_output_binary(dstat); blkiomon_output_msg_q(dstat); tail = dstat; } return tail; } static void *blkiomon_interval(void *data) { struct timespec wake, r; struct dstat *head, *tail; int finished; clock_gettime(CLOCK_REALTIME, &wake); while (1) { wake.tv_sec += interval; if (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake, &r)) { fprintf(stderr, "blkiomon: interrupted sleep"); continue; } /* grab tree and make data gatherer build up another tree */ pthread_mutex_lock(&dstat_mutex); finished = dstat_curr; dstat_curr = dstat_curr ? 0 : 1; pthread_mutex_unlock(&dstat_mutex); head = dstat_list[finished]; if (!head) continue; dstat_list[finished] = NULL; dstat_tree[finished] = RB_ROOT; tail = blkiomon_output(head, &wake); pthread_mutex_lock(&dstat_mutex); tail->next = vacant_dstats_list; vacant_dstats_list = head; pthread_mutex_unlock(&dstat_mutex); } return data; } #define BLK_DATADIR(a) (((a) >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE)) static int blkiomon_account(struct blk_io_trace *bit_d, struct blk_io_trace *bit_c) { struct dstat *dstat; struct blkiomon_stat *p; __u64 d2c = (bit_c->time - bit_d->time) / 1000; /* ns -> us */ __u32 size = bit_d->bytes; __u64 thrput = size * 1000 / d2c; dstat = blkiomon_get_dstat(bit_d->device); if (!dstat) return 1; p = &dstat->msg.stat; if (BLK_DATADIR(bit_c->action) & BLK_TC_READ) { minmax_account(&p->thrput_r, thrput); minmax_account(&p->size_r, size); minmax_account(&p->d2c_r, d2c); } else if (BLK_DATADIR(bit_c->action) & BLK_TC_WRITE) { minmax_account(&p->thrput_w, thrput); minmax_account(&p->size_w, size); minmax_account(&p->d2c_w, d2c); } else p->bidir++; histlog2_account(p->size_hist, size, &size_hist); histlog2_account(p->d2c_hist, d2c, &d2c_hist); return 0; } static struct trace *blkiomon_alloc_trace(void) { struct trace *t = vacant_traces_list; if (t) { vacant_traces_list = t->next; vacant_traces--; } else t = malloc(sizeof(*t)); memset(t, 0, sizeof(*t)); return t; } static void blkiomon_free_trace(struct trace *t) { if (vacant_traces < 256) { t->next = vacant_traces_list; vacant_traces_list = t; vacant_traces++; } else free(t); } static int action(int a) { int bits = BLK_TC_WRITE | BLK_TC_READ | BLK_TC_FS | BLK_TC_PC; return a & (BLK_TC_ACT(bits)); } static void blkiomon_store_trace(struct trace *t) { int i = t->bit.sector % TRACE_HASH_SIZE; t->next = thash[i]; thash[i] = t; } static struct trace *blkiomon_fetch_trace(struct blk_io_trace *bit) { int i = bit->sector % TRACE_HASH_SIZE; struct trace *t, *prev = NULL; for (t = thash[i]; t; t = t->next) { if (t->bit.device == bit->device && t->bit.sector == bit->sector && action(t->bit.action) == action(bit->action)) { if (prev) prev->next = t->next; else thash[i] = t->next; return t; } prev = t; } return NULL; } static struct trace *blkiomon_do_trace(struct trace *t) { struct trace *t_stored, *t_old, *t_young; /* store trace if there is no match yet */ t_stored = blkiomon_fetch_trace(&t->bit); if (!t_stored) { blkiomon_store_trace(t); return blkiomon_alloc_trace(); } /* figure out older trace and younger trace */ if (t_stored->bit.time < t->bit.time) { t_old = t_stored; t_young = t; } else { t_old = t; t_young = t_stored; } /* we need an older D trace and a younger C trace */ if (t_old->bit.action & BLK_TC_ACT(BLK_TC_ISSUE) && t_young->bit.action & BLK_TC_ACT(BLK_TC_COMPLETE)) { /* matching D and C traces - update statistics */ match++; blkiomon_account(&t_old->bit, &t_young->bit); blkiomon_free_trace(t_stored); return t; } /* no matching D and C traces - keep more recent trace */ dump_bits(t_old, t_young, "mismatch"); mismatch++; blkiomon_store_trace(t_young); return t_old; } static int blkiomon_dump_drvdata(struct blk_io_trace *bit, void *pdu_buf) { if (!drvdata.fn) return 0; if (fwrite(bit, sizeof(*bit), 1, drvdata.fp) != 1) goto failed; if (fwrite(pdu_buf, bit->pdu_len, 1, drvdata.fp) != 1) goto failed; if (drvdata.pipe && fflush(drvdata.fp)) goto failed; return 0; failed: fprintf(stderr, "blkiomon: could not write to %s\n", drvdata.fn); fclose(drvdata.fp); drvdata.fn = NULL; return 1; } static int blkiomon_do_fifo(void) { struct trace *t; struct blk_io_trace *bit; void *pdu_buf = NULL; t = blkiomon_alloc_trace(); if (!t) return 1; bit = &t->bit; while (up) { if (fread(bit, sizeof(*bit), 1, ifp) != 1) { if (!feof(ifp)) fprintf(stderr, "blkiomon: could not read trace"); break; } if (ferror(ifp)) { clearerr(ifp); fprintf(stderr, "blkiomon: error while reading trace"); break; } if (data_is_native == -1 && check_data_endianness(bit->magic)) { fprintf(stderr, "blkiomon: endianess problem\n"); break; } /* endianess */ trace_to_cpu(bit); if (verify_trace(bit)) { fprintf(stderr, "blkiomon: bad trace\n"); break; } /* read additional trace payload */ if (bit->pdu_len) { pdu_buf = realloc(pdu_buf, bit->pdu_len); if (fread(pdu_buf, bit->pdu_len, 1, ifp) != 1) { clearerr(ifp); fprintf(stderr, "blkiomon: could not read payload\n"); break; } } t->sequence = sequence++; /* forward low-level device driver trace to other tool */ if (bit->action & BLK_TC_ACT(BLK_TC_DRV_DATA)) { driverdata++; if (blkiomon_dump_drvdata(bit, pdu_buf)) { fprintf(stderr, "blkiomon: could not send trace\n"); break; } continue; } if (!(bit->action & BLK_TC_ACT(BLK_TC_ISSUE | BLK_TC_COMPLETE))) continue; /* try to find matching trace and update statistics */ t = blkiomon_do_trace(t); if (!t) { fprintf(stderr, "blkiomon: could not alloc trace\n"); break; } bit = &t->bit; /* t and bit will be recycled for next incoming trace */ } blkiomon_free_trace(t); free(pdu_buf); return 0; } static int blkiomon_open_output(struct output *out) { int mode, vbuf_size; if (!out->fn) return 0; if (!strcmp(out->fn, "-")) { out->fp = fdopen(STDOUT_FILENO, "w"); mode = _IOLBF; vbuf_size = 4096; out->pipe = 1; } else { out->fp = fopen(out->fn, "w"); mode = _IOFBF; vbuf_size = 128 * 1024; out->pipe = 0; } if (!out->fp) goto failed; out->buf = malloc(128 * 1024); if (setvbuf(out->fp, out->buf, mode, vbuf_size)) goto failed; return 0; failed: fprintf(stderr, "blkiomon: could not write to %s\n", out->fn); out->fn = NULL; free(out->buf); return 1; } static int blkiomon_open_msg_q(void) { key_t key; if (!msg_q_name) return 0; if (!msg_q_id || msg_id <= 0) return 1; key = ftok(msg_q_name, msg_q_id); if (key == -1) return 1; while (up) { msg_q = msgget(key, S_IRWXU); if (msg_q >= 0) break; } return (msg_q >= 0 ? 0 : -1); } static void blkiomon_debug(void) { int i; struct trace *t; if (!debug.fn) return; for (i = 0; i < TRACE_HASH_SIZE; i++) for (t = thash[i]; t; t = t->next) { dump_bit(t, "leftover"); leftover++; } fprintf(debug.fp, "%ld leftover, %ld match, %ld mismatch, " "%ld driverdata, %ld overall\n", leftover, match, mismatch, driverdata, sequence); } #define S_OPTS "b:d:D:h:I:Q:q:m:V" static char usage_str[] = "\n\nblkiomon " \ "-I | --interval=\n" \ "[ -h | --human-readable= ]\n" \ "[ -b | --binary= ]\n" \ "[ -d | --dump-lldd= ]\n" \ "[ -D | --debug= ]\n" \ "[ -Q | --msg-queue=]\n" \ "[ -q | --msg-queue-id=]\n" \ "[ -m | --msg-id=]\n" \ "[ -V | --version ]\n\n" \ "\t-I Sample interval.\n" \ "\t-h Human-readable output file.\n" \ "\t-b Binary output file.\n" \ "\t-d Output file for data emitted by low level device driver.\n" \ "\t-D Output file for debugging data.\n" \ "\t-Qqm Output to message queue using given ID for messages.\n" \ "\t-V Print program version.\n\n"; static struct option l_opts[] = { { .name = "human-readable", .has_arg = required_argument, .flag = NULL, .val = 'h' }, { .name = "binary", .has_arg = required_argument, .flag = NULL, .val = 'b' }, { .name = "dump-lldd", .has_arg = required_argument, .flag = NULL, .val = 'd' }, { .name = "debug", .has_arg = required_argument, .flag = NULL, .val = 'D' }, { .name = "interval", .has_arg = required_argument, .flag = NULL, .val = 'I' }, { .name = "msg-queue", .has_arg = required_argument, .flag = NULL, .val = 'Q' }, { .name = "msg-queue-id", .has_arg = required_argument, .flag = NULL, .val = 'q' }, { .name = "msg-id", .has_arg = required_argument, .flag = NULL, .val = 'm' }, { .name = "version", .has_arg = no_argument, .flag = NULL, .val = 'V' }, { .name = NULL, } }; static void blkiomon_signal(int signal) { fprintf(stderr, "blkiomon: terminated by signal\n"); up = signal & 0; } int main(int argc, char *argv[]) { int c; signal(SIGALRM, blkiomon_signal); signal(SIGINT, blkiomon_signal); signal(SIGTERM, blkiomon_signal); signal(SIGQUIT, blkiomon_signal); while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) { switch (c) { case 'h': human.fn = optarg; break; case 'b': binary.fn = optarg; break; case 'd': drvdata.fn = optarg; break; case 'D': debug.fn = optarg; break; case 'I': interval = atoi(optarg); break; case 'Q': msg_q_name = optarg; break; case 'q': msg_q_id = atoi(optarg); break; case 'm': msg_id = atoi(optarg); break; case 'V': printf("%s version %s\n", argv[0], blkiomon_version); return 0; default: fprintf(stderr, "Usage: %s", usage_str); return 1; } } if (interval <= 0) { fprintf(stderr, "Usage: %s", usage_str); return 1; } ifp = fdopen(STDIN_FILENO, "r"); if (!ifp) { perror("blkiomon: could not open stdin for reading"); return 1; } if (blkiomon_open_output(&human)) return 1; if (blkiomon_open_output(&binary)) return 1; if (blkiomon_open_output(&drvdata)) return 1; if (blkiomon_open_output(&debug)) return 1; if (blkiomon_open_msg_q()) return 1; if (pthread_create(&interval_thread, NULL, blkiomon_interval, NULL)) { fprintf(stderr, "blkiomon: could not create thread"); return 1; } blkiomon_do_fifo(); blkiomon_debug(); return 0; } blktrace-1.0.5/blkiomon.h000066400000000000000000000067761172262745100153160ustar00rootroot00000000000000/* * I/O monitor based on block queue trace data * * Copyright IBM Corp. 2008 * * Author(s): Martin Peschke * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef BLKIOMON_H #define BLKIOMON_H #include #include "stats.h" #include "blktrace.h" #define BLKIOMON_SIZE_BUCKETS 16 #define BLKIOMON_D2C_BUCKETS 25 struct blkiomon_stat { __u64 time; __u32 size_hist[BLKIOMON_SIZE_BUCKETS]; __u32 d2c_hist[BLKIOMON_D2C_BUCKETS]; __u32 device; struct minmax size_r; struct minmax size_w; struct minmax d2c_r; struct minmax d2c_w; struct minmax thrput_r; struct minmax thrput_w; __u64 bidir; }; static struct histlog2 size_hist = { .first = 0, .delta = 1024, .num = BLKIOMON_SIZE_BUCKETS }; static struct histlog2 d2c_hist = { .first = 0, .delta = 8, .num = BLKIOMON_D2C_BUCKETS }; static inline void blkiomon_stat_init(struct blkiomon_stat *bstat) { memset(bstat, 0, sizeof(*bstat)); minmax_init(&bstat->size_r); minmax_init(&bstat->size_w); minmax_init(&bstat->d2c_r); minmax_init(&bstat->d2c_w); minmax_init(&bstat->thrput_r); minmax_init(&bstat->thrput_w); } static inline void blkiomon_stat_to_be(struct blkiomon_stat *bstat) { histlog2_to_be(bstat->size_hist, &size_hist); histlog2_to_be(bstat->d2c_hist, &d2c_hist); minmax_to_be(&bstat->size_r); minmax_to_be(&bstat->size_w); minmax_to_be(&bstat->d2c_r); minmax_to_be(&bstat->d2c_w); minmax_to_be(&bstat->thrput_r); minmax_to_be(&bstat->thrput_w); bstat->bidir = cpu_to_be64(bstat->bidir); bstat->time = cpu_to_be64(bstat->time); bstat->device = cpu_to_be32(bstat->device); } static inline void blkiomon_stat_merge(struct blkiomon_stat *dst, struct blkiomon_stat *src) { histlog2_merge(&size_hist, dst->size_hist, src->size_hist); histlog2_merge(&d2c_hist, dst->d2c_hist, src->d2c_hist); minmax_merge(&dst->size_r, &src->size_r); minmax_merge(&dst->size_w, &src->size_w); minmax_merge(&dst->d2c_r, &src->d2c_r); minmax_merge(&dst->d2c_w, &src->d2c_w); minmax_merge(&dst->thrput_r, &src->thrput_r); minmax_merge(&dst->thrput_w, &src->thrput_w); dst->bidir += src->bidir; } static inline void blkiomon_stat_print(FILE *fp, struct blkiomon_stat *p) { if (!fp) return; fprintf(fp, "\ntime: %s", ctime((void *)&p->time)); fprintf(fp, "device: %d,%d\n", MAJOR(p->device), MINOR(p->device)); minmax_print(fp, "sizes read (bytes)", &p->size_r); minmax_print(fp, "sizes write (bytes)", &p->size_w); minmax_print(fp, "d2c read (usec)", &p->d2c_r); minmax_print(fp, "d2c write (usec)", &p->d2c_w); minmax_print(fp, "throughput read (bytes/msec)", &p->thrput_r); minmax_print(fp, "throughput write (bytes/msec)", &p->thrput_w); histlog2_print(fp, "sizes histogram (bytes)", p->size_hist, &size_hist); histlog2_print(fp, "d2c histogram (usec)", p->d2c_hist, &d2c_hist); fprintf(fp, "bidirectional requests: %ld\n", (unsigned long)p->bidir); } #endif blktrace-1.0.5/blkparse.c000066400000000000000000001741761172262745100153020ustar00rootroot00000000000000/* * block queue tracing parse application * * Copyright (C) 2005 Jens Axboe * Copyright (C) 2006 Jens Axboe * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include #include #include #include #include #include #include #include #include #include #include #include "blktrace.h" #include "rbtree.h" #include "jhash.h" static char blkparse_version[] = "1.0.5"; struct skip_info { unsigned long start, end; struct skip_info *prev, *next; }; struct per_dev_info { dev_t dev; char *name; int backwards; unsigned long long events; unsigned long long first_reported_time; unsigned long long last_reported_time; unsigned long long last_read_time; struct io_stats io_stats; unsigned long skips; unsigned long long seq_skips; unsigned int max_depth[2]; unsigned int cur_depth[2]; struct rb_root rb_track; int nfiles; int ncpus; unsigned long *cpu_map; unsigned int cpu_map_max; struct per_cpu_info *cpus; }; /* * some duplicated effort here, we can unify this hash and the ppi hash later */ struct process_pid_map { pid_t pid; char comm[16]; struct process_pid_map *hash_next, *list_next; }; #define PPM_HASH_SHIFT (8) #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT) #define PPM_HASH_MASK (PPM_HASH_SIZE - 1) static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE]; struct per_process_info { struct process_pid_map *ppm; struct io_stats io_stats; struct per_process_info *hash_next, *list_next; int more_than_one; /* * individual io stats */ unsigned long long longest_allocation_wait[2]; unsigned long long longest_dispatch_wait[2]; unsigned long long longest_completion_wait[2]; }; #define PPI_HASH_SHIFT (8) #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT) #define PPI_HASH_MASK (PPI_HASH_SIZE - 1) static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE]; static struct per_process_info *ppi_list; static int ppi_list_entries; static struct option l_opts[] = { { .name = "act-mask", .has_arg = required_argument, .flag = NULL, .val = 'a' }, { .name = "set-mask", .has_arg = required_argument, .flag = NULL, .val = 'A' }, { .name = "batch", .has_arg = required_argument, .flag = NULL, .val = 'b' }, { .name = "input-directory", .has_arg = required_argument, .flag = NULL, .val = 'D' }, { .name = "dump-binary", .has_arg = required_argument, .flag = NULL, .val = 'd' }, { .name = "format", .has_arg = required_argument, .flag = NULL, .val = 'f' }, { .name = "format-spec", .has_arg = required_argument, .flag = NULL, .val = 'F' }, { .name = "hash-by-name", .has_arg = no_argument, .flag = NULL, .val = 'h' }, { .name = "input", .has_arg = required_argument, .flag = NULL, .val = 'i' }, { .name = "no-msgs", .has_arg = no_argument, .flag = NULL, .val = 'M' }, { .name = "output", .has_arg = required_argument, .flag = NULL, .val = 'o' }, { .name = "no-text-output", .has_arg = no_argument, .flag = NULL, .val = 'O' }, { .name = "quiet", .has_arg = no_argument, .flag = NULL, .val = 'q' }, { .name = "per-program-stats", .has_arg = no_argument, .flag = NULL, .val = 's' }, { .name = "track-ios", .has_arg = no_argument, .flag = NULL, .val = 't' }, { .name = "stopwatch", .has_arg = required_argument, .flag = NULL, .val = 'w' }, { .name = "verbose", .has_arg = no_argument, .flag = NULL, .val = 'v' }, { .name = "version", .has_arg = no_argument, .flag = NULL, .val = 'V' }, { .name = NULL, } }; /* * for sorting the displayed output */ struct trace { struct blk_io_trace *bit; struct rb_node rb_node; struct trace *next; unsigned long read_sequence; }; static struct rb_root rb_sort_root; static unsigned long rb_sort_entries; static struct trace *trace_list; /* * allocation cache */ static struct blk_io_trace *bit_alloc_list; static struct trace *t_alloc_list; /* * for tracking individual ios */ struct io_track { struct rb_node rb_node; struct process_pid_map *ppm; __u64 sector; unsigned long long allocation_time; unsigned long long queue_time; unsigned long long dispatch_time; unsigned long long completion_time; }; static int ndevices; static struct per_dev_info *devices; static char *get_dev_name(struct per_dev_info *, char *, int); static int trace_rb_insert_last(struct per_dev_info *, struct trace *); FILE *ofp = NULL; static char *output_name; static char *input_dir; static unsigned long long genesis_time; static unsigned long long last_allowed_time; static unsigned long long stopwatch_start; /* start from zero by default */ static unsigned long long stopwatch_end = -1ULL; /* "infinity" */ static unsigned long read_sequence; static int per_process_stats; static int per_device_and_cpu_stats = 1; static int track_ios; static int ppi_hash_by_pid = 1; static int verbose; static unsigned int act_mask = -1U; static int stats_printed; static int bin_output_msgs = 1; int data_is_native = -1; static FILE *dump_fp; static char *dump_binary; static unsigned int t_alloc_cache; static unsigned int bit_alloc_cache; #define RB_BATCH_DEFAULT (512) static unsigned int rb_batch = RB_BATCH_DEFAULT; static int pipeline; static char *pipename; static int text_output = 1; #define is_done() (*(volatile int *)(&done)) static volatile int done; struct timespec abs_start_time; static unsigned long long start_timestamp; static int have_drv_data = 0; #define JHASH_RANDOM (0x3af5f2ee) #define CPUS_PER_LONG (8 * sizeof(unsigned long)) #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG) #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1)) static void output_binary(void *buf, int len) { if (dump_binary) { size_t n = fwrite(buf, len, 1, dump_fp); if (n != 1) { perror(dump_binary); fclose(dump_fp); dump_binary = NULL; } } } static void resize_cpu_info(struct per_dev_info *pdi, int cpu) { struct per_cpu_info *cpus = pdi->cpus; int ncpus = pdi->ncpus; int new_count = cpu + 1; int new_space, size; char *new_start; size = new_count * sizeof(struct per_cpu_info); cpus = realloc(cpus, size); if (!cpus) { char name[20]; fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n", get_dev_name(pdi, name, sizeof(name)), size); exit(1); } new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info)); new_space = (new_count - ncpus) * sizeof(struct per_cpu_info); memset(new_start, 0, new_space); pdi->ncpus = new_count; pdi->cpus = cpus; for (new_count = 0; new_count < pdi->ncpus; new_count++) { struct per_cpu_info *pci = &pdi->cpus[new_count]; if (!pci->fd) { pci->fd = -1; memset(&pci->rb_last, 0, sizeof(pci->rb_last)); pci->rb_last_entries = 0; pci->last_sequence = -1; } } } static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu) { struct per_cpu_info *pci; if (cpu >= pdi->ncpus) resize_cpu_info(pdi, cpu); pci = &pdi->cpus[cpu]; pci->cpu = cpu; return pci; } static int resize_devices(char *name) { int size = (ndevices + 1) * sizeof(struct per_dev_info); devices = realloc(devices, size); if (!devices) { fprintf(stderr, "Out of memory, device %s (%d)\n", name, size); return 1; } memset(&devices[ndevices], 0, sizeof(struct per_dev_info)); devices[ndevices].name = name; ndevices++; return 0; } static struct per_dev_info *get_dev_info(dev_t dev) { struct per_dev_info *pdi; int i; for (i = 0; i < ndevices; i++) { if (!devices[i].dev) devices[i].dev = dev; if (devices[i].dev == dev) return &devices[i]; } if (resize_devices(NULL)) return NULL; pdi = &devices[ndevices - 1]; pdi->dev = dev; pdi->first_reported_time = 0; pdi->last_read_time = 0; return pdi; } static void insert_skip(struct per_cpu_info *pci, unsigned long start, unsigned long end) { struct skip_info *sip; for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { if (end == (sip->start - 1)) { sip->start = start; return; } else if (start == (sip->end + 1)) { sip->end = end; return; } } sip = malloc(sizeof(struct skip_info)); sip->start = start; sip->end = end; sip->prev = sip->next = NULL; if (pci->skips_tail == NULL) pci->skips_head = pci->skips_tail = sip; else { sip->prev = pci->skips_tail; pci->skips_tail->next = sip; pci->skips_tail = sip; } } static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip) { if (sip->prev == NULL) { if (sip->next == NULL) pci->skips_head = pci->skips_tail = NULL; else { pci->skips_head = sip->next; sip->next->prev = NULL; } } else if (sip->next == NULL) { pci->skips_tail = sip->prev; sip->prev->next = NULL; } else { sip->prev->next = sip->next; sip->next->prev = sip->prev; } sip->prev = sip->next = NULL; free(sip); } #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end)) static int check_current_skips(struct per_cpu_info *pci, unsigned long seq) { struct skip_info *sip; for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) { if (IN_SKIP(sip, seq)) { if (sip->start == seq) { if (sip->end == seq) remove_sip(pci, sip); else sip->start += 1; } else if (sip->end == seq) sip->end -= 1; else { sip->end = seq - 1; insert_skip(pci, seq + 1, sip->end); } return 1; } } return 0; } static void collect_pdi_skips(struct per_dev_info *pdi) { struct skip_info *sip; int cpu; pdi->skips = 0; pdi->seq_skips = 0; for (cpu = 0; cpu < pdi->ncpus; cpu++) { struct per_cpu_info *pci = &pdi->cpus[cpu]; for (sip = pci->skips_head; sip != NULL; sip = sip->next) { pdi->skips++; pdi->seq_skips += (sip->end - sip->start + 1); if (verbose) fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n", MAJOR(pdi->dev), MINOR(pdi->dev), sip->start, sip->end); } } } static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu) { if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) { int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1); unsigned long *map = malloc(new_max / sizeof(long)); memset(map, 0, new_max / sizeof(long)); if (pdi->cpu_map) { memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long)); free(pdi->cpu_map); } pdi->cpu_map = map; pdi->cpu_map_max = new_max; } pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); } static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu) { pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu)); } static inline int cpu_is_online(struct per_dev_info *pdi, int cpu) { return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0; } static inline int ppm_hash_pid(pid_t pid) { return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK; } static struct process_pid_map *find_ppm(pid_t pid) { const int hash_idx = ppm_hash_pid(pid); struct process_pid_map *ppm; ppm = ppm_hash_table[hash_idx]; while (ppm) { if (ppm->pid == pid) return ppm; ppm = ppm->hash_next; } return NULL; } static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name) { const int hash_idx = ppm_hash_pid(pid); struct process_pid_map *ppm; ppm = find_ppm(pid); if (!ppm) { ppm = malloc(sizeof(*ppm)); memset(ppm, 0, sizeof(*ppm)); ppm->pid = pid; memset(ppm->comm, 0, sizeof(ppm->comm)); strncpy(ppm->comm, name, sizeof(ppm->comm)); ppm->comm[sizeof(ppm->comm) - 1] = '\0'; ppm->hash_next = ppm_hash_table[hash_idx]; ppm_hash_table[hash_idx] = ppm; } return ppm; } static void handle_notify(struct blk_io_trace *bit) { void *payload = (caddr_t) bit + sizeof(*bit); __u32 two32[2]; switch (bit->action) { case BLK_TN_PROCESS: add_ppm_hash(bit->pid, payload); break; case BLK_TN_TIMESTAMP: if (bit->pdu_len != sizeof(two32)) return; memcpy(two32, payload, sizeof(two32)); if (!data_is_native) { two32[0] = be32_to_cpu(two32[0]); two32[1] = be32_to_cpu(two32[1]); } start_timestamp = bit->time; abs_start_time.tv_sec = two32[0]; abs_start_time.tv_nsec = two32[1]; if (abs_start_time.tv_nsec < 0) { abs_start_time.tv_sec--; abs_start_time.tv_nsec += 1000000000; } break; case BLK_TN_MESSAGE: if (bit->pdu_len > 0) { char msg[bit->pdu_len+1]; memcpy(msg, (char *)payload, bit->pdu_len); msg[bit->pdu_len] = '\0'; fprintf(ofp, "%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n", MAJOR(bit->device), MINOR(bit->device), bit->cpu, "0", (int) SECONDS(bit->time), (unsigned long) NANO_SECONDS(bit->time), 0, "m", "N", msg); } break; default: /* Ignore unknown notify events */ ; } } char *find_process_name(pid_t pid) { struct process_pid_map *ppm = find_ppm(pid); if (ppm) return ppm->comm; return NULL; } static inline int ppi_hash_pid(pid_t pid) { return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK; } static inline int ppi_hash_name(const char *name) { return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK; } static inline int ppi_hash(struct per_process_info *ppi) { struct process_pid_map *ppm = ppi->ppm; if (ppi_hash_by_pid) return ppi_hash_pid(ppm->pid); return ppi_hash_name(ppm->comm); } static inline void add_ppi_to_hash(struct per_process_info *ppi) { const int hash_idx = ppi_hash(ppi); ppi->hash_next = ppi_hash_table[hash_idx]; ppi_hash_table[hash_idx] = ppi; } static inline void add_ppi_to_list(struct per_process_info *ppi) { ppi->list_next = ppi_list; ppi_list = ppi; ppi_list_entries++; } static struct per_process_info *find_ppi_by_name(char *name) { const int hash_idx = ppi_hash_name(name); struct per_process_info *ppi; ppi = ppi_hash_table[hash_idx]; while (ppi) { struct process_pid_map *ppm = ppi->ppm; if (!strcmp(ppm->comm, name)) return ppi; ppi = ppi->hash_next; } return NULL; } static struct per_process_info *find_ppi_by_pid(pid_t pid) { const int hash_idx = ppi_hash_pid(pid); struct per_process_info *ppi; ppi = ppi_hash_table[hash_idx]; while (ppi) { struct process_pid_map *ppm = ppi->ppm; if (ppm->pid == pid) return ppi; ppi = ppi->hash_next; } return NULL; } static struct per_process_info *find_ppi(pid_t pid) { struct per_process_info *ppi; char *name; if (ppi_hash_by_pid) return find_ppi_by_pid(pid); name = find_process_name(pid); if (!name) return NULL; ppi = find_ppi_by_name(name); if (ppi && ppi->ppm->pid != pid) ppi->more_than_one = 1; return ppi; } /* * struct trace and blktrace allocation cache, we do potentially * millions of mallocs for these structures while only using at most * a few thousand at the time */ static inline void t_free(struct trace *t) { if (t_alloc_cache < 1024) { t->next = t_alloc_list; t_alloc_list = t; t_alloc_cache++; } else free(t); } static inline struct trace *t_alloc(void) { struct trace *t = t_alloc_list; if (t) { t_alloc_list = t->next; t_alloc_cache--; return t; } return malloc(sizeof(*t)); } static inline void bit_free(struct blk_io_trace *bit) { if (bit_alloc_cache < 1024 && !bit->pdu_len) { /* * abuse a 64-bit field for a next pointer for the free item */ bit->time = (__u64) (unsigned long) bit_alloc_list; bit_alloc_list = (struct blk_io_trace *) bit; bit_alloc_cache++; } else free(bit); } static inline struct blk_io_trace *bit_alloc(void) { struct blk_io_trace *bit = bit_alloc_list; if (bit) { bit_alloc_list = (struct blk_io_trace *) (unsigned long) \ bit->time; bit_alloc_cache--; return bit; } return malloc(sizeof(*bit)); } static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t) { struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); rb_erase(&t->rb_node, &pci->rb_last); pci->rb_last_entries--; bit_free(t->bit); t_free(t); } static void put_trace(struct per_dev_info *pdi, struct trace *t) { rb_erase(&t->rb_node, &rb_sort_root); rb_sort_entries--; trace_rb_insert_last(pdi, t); } static inline int trace_rb_insert(struct trace *t, struct rb_root *root) { struct rb_node **p = &root->rb_node; struct rb_node *parent = NULL; struct trace *__t; while (*p) { parent = *p; __t = rb_entry(parent, struct trace, rb_node); if (t->bit->time < __t->bit->time) p = &(*p)->rb_left; else if (t->bit->time > __t->bit->time) p = &(*p)->rb_right; else if (t->bit->device < __t->bit->device) p = &(*p)->rb_left; else if (t->bit->device > __t->bit->device) p = &(*p)->rb_right; else if (t->bit->sequence < __t->bit->sequence) p = &(*p)->rb_left; else /* >= sequence */ p = &(*p)->rb_right; } rb_link_node(&t->rb_node, parent, p); rb_insert_color(&t->rb_node, root); return 0; } static inline int trace_rb_insert_sort(struct trace *t) { if (!trace_rb_insert(t, &rb_sort_root)) { rb_sort_entries++; return 0; } return 1; } static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t) { struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu); if (trace_rb_insert(t, &pci->rb_last)) return 1; pci->rb_last_entries++; if (pci->rb_last_entries > rb_batch * pdi->nfiles) { struct rb_node *n = rb_first(&pci->rb_last); t = rb_entry(n, struct trace, rb_node); __put_trace_last(pdi, t); } return 0; } static struct trace *trace_rb_find(dev_t device, unsigned long sequence, struct rb_root *root, int order) { struct rb_node *n = root->rb_node; struct rb_node *prev = NULL; struct trace *__t; while (n) { __t = rb_entry(n, struct trace, rb_node); prev = n; if (device < __t->bit->device) n = n->rb_left; else if (device > __t->bit->device) n = n->rb_right; else if (sequence < __t->bit->sequence) n = n->rb_left; else if (sequence > __t->bit->sequence) n = n->rb_right; else return __t; } /* * hack - the list may not be sequence ordered because some * events don't have sequence and time matched. so we end up * being a little off in the rb lookup here, because we don't * know the time we are looking for. compensate by browsing * a little ahead from the last entry to find the match */ if (order && prev) { int max = 5; while (((n = rb_next(prev)) != NULL) && max--) { __t = rb_entry(n, struct trace, rb_node); if (__t->bit->device == device && __t->bit->sequence == sequence) return __t; prev = n; } } return NULL; } static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi, struct per_cpu_info *pci, unsigned long seq) { return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0); } static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot) { struct rb_node **p = &pdi->rb_track.rb_node; struct rb_node *parent = NULL; struct io_track *__iot; while (*p) { parent = *p; __iot = rb_entry(parent, struct io_track, rb_node); if (iot->sector < __iot->sector) p = &(*p)->rb_left; else if (iot->sector > __iot->sector) p = &(*p)->rb_right; else { fprintf(stderr, "sector alias (%Lu) on device %d,%d!\n", (unsigned long long) iot->sector, MAJOR(pdi->dev), MINOR(pdi->dev)); return 1; } } rb_link_node(&iot->rb_node, parent, p); rb_insert_color(&iot->rb_node, &pdi->rb_track); return 0; } static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector) { struct rb_node *n = pdi->rb_track.rb_node; struct io_track *__iot; while (n) { __iot = rb_entry(n, struct io_track, rb_node); if (sector < __iot->sector) n = n->rb_left; else if (sector > __iot->sector) n = n->rb_right; else return __iot; } return NULL; } static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid, __u64 sector) { struct io_track *iot; iot = __find_track(pdi, sector); if (!iot) { iot = malloc(sizeof(*iot)); iot->ppm = find_ppm(pid); if (!iot->ppm) iot->ppm = add_ppm_hash(pid, "unknown"); iot->sector = sector; track_rb_insert(pdi, iot); } return iot; } static void log_track_frontmerge(struct per_dev_info *pdi, struct blk_io_trace *t) { struct io_track *iot; if (!track_ios) return; iot = __find_track(pdi, t->sector + t_sec(t)); if (!iot) { if (verbose) fprintf(stderr, "merge not found for (%d,%d): %llu\n", MAJOR(pdi->dev), MINOR(pdi->dev), (unsigned long long) t->sector + t_sec(t)); return; } rb_erase(&iot->rb_node, &pdi->rb_track); iot->sector -= t_sec(t); track_rb_insert(pdi, iot); } static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t) { struct io_track *iot; if (!track_ios) return; iot = find_track(pdi, t->pid, t->sector); iot->allocation_time = t->time; } static inline int is_remapper(struct per_dev_info *pdi) { int major = MAJOR(pdi->dev); return (major == 253 || major == 9); } /* * for md/dm setups, the interesting cycle is Q -> C. So track queueing * time here, as dispatch time */ static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t) { struct io_track *iot; if (!track_ios) return; if (!is_remapper(pdi)) return; iot = find_track(pdi, t->pid, t->sector); iot->dispatch_time = t->time; } /* * return time between rq allocation and insertion */ static unsigned long long log_track_insert(struct per_dev_info *pdi, struct blk_io_trace *t) { unsigned long long elapsed; struct io_track *iot; if (!track_ios) return -1; iot = find_track(pdi, t->pid, t->sector); iot->queue_time = t->time; if (!iot->allocation_time) return -1; elapsed = iot->queue_time - iot->allocation_time; if (per_process_stats) { struct per_process_info *ppi = find_ppi(iot->ppm->pid); int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; if (ppi && elapsed > ppi->longest_allocation_wait[w]) ppi->longest_allocation_wait[w] = elapsed; } return elapsed; } /* * return time between queue and issue */ static unsigned long long log_track_issue(struct per_dev_info *pdi, struct blk_io_trace *t) { unsigned long long elapsed; struct io_track *iot; if (!track_ios) return -1; if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0) return -1; iot = __find_track(pdi, t->sector); if (!iot) { if (verbose) fprintf(stderr, "issue not found for (%d,%d): %llu\n", MAJOR(pdi->dev), MINOR(pdi->dev), (unsigned long long) t->sector); return -1; } iot->dispatch_time = t->time; elapsed = iot->dispatch_time - iot->queue_time; if (per_process_stats) { struct per_process_info *ppi = find_ppi(iot->ppm->pid); int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; if (ppi && elapsed > ppi->longest_dispatch_wait[w]) ppi->longest_dispatch_wait[w] = elapsed; } return elapsed; } /* * return time between dispatch and complete */ static unsigned long long log_track_complete(struct per_dev_info *pdi, struct blk_io_trace *t) { unsigned long long elapsed; struct io_track *iot; if (!track_ios) return -1; iot = __find_track(pdi, t->sector); if (!iot) { if (verbose) fprintf(stderr,"complete not found for (%d,%d): %llu\n", MAJOR(pdi->dev), MINOR(pdi->dev), (unsigned long long) t->sector); return -1; } iot->completion_time = t->time; elapsed = iot->completion_time - iot->dispatch_time; if (per_process_stats) { struct per_process_info *ppi = find_ppi(iot->ppm->pid); int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; if (ppi && elapsed > ppi->longest_completion_wait[w]) ppi->longest_completion_wait[w] = elapsed; } /* * kill the trace, we don't need it after completion */ rb_erase(&iot->rb_node, &pdi->rb_track); free(iot); return elapsed; } static struct io_stats *find_process_io_stats(pid_t pid) { struct per_process_info *ppi = find_ppi(pid); if (!ppi) { ppi = malloc(sizeof(*ppi)); memset(ppi, 0, sizeof(*ppi)); ppi->ppm = find_ppm(pid); if (!ppi->ppm) ppi->ppm = add_ppm_hash(pid, "unknown"); add_ppi_to_hash(ppi); add_ppi_to_list(ppi); } return &ppi->io_stats; } static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size) { if (pdi->name) snprintf(buffer, size, "%s", pdi->name); else snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev)); return buffer; } static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit) { unsigned long long this = bit->time; unsigned long long last = pdi->last_reported_time; pdi->backwards = (this < last) ? 'B' : ' '; pdi->last_reported_time = this; } static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t, int rw) { if (rw) { ios->mwrites++; ios->mwrite_kb += t_kb(t); } else { ios->mreads++; ios->mread_kb += t_kb(t); } } static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_m(&pci->io_stats, t, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_m(ios, t, rw); } } static inline void __account_pc_queue(struct io_stats *ios, struct blk_io_trace *t, int rw) { if (rw) { ios->qwrites_pc++; ios->qwrite_kb_pc += t_kb(t); } else { ios->qreads_pc++; ios->qread_kb += t_kb(t); } } static inline void account_pc_queue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_pc_queue(&pci->io_stats, t, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_pc_queue(ios, t, rw); } } static inline void __account_pc_issue(struct io_stats *ios, int rw, unsigned int bytes) { if (rw) { ios->iwrites_pc++; ios->iwrite_kb_pc += bytes >> 10; } else { ios->ireads_pc++; ios->iread_kb_pc += bytes >> 10; } } static inline void account_pc_issue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_pc_issue(&pci->io_stats, rw, t->bytes); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_pc_issue(ios, rw, t->bytes); } } static inline void __account_pc_requeue(struct io_stats *ios, struct blk_io_trace *t, int rw) { if (rw) { ios->wrqueue_pc++; ios->iwrite_kb_pc -= t_kb(t); } else { ios->rrqueue_pc++; ios->iread_kb_pc -= t_kb(t); } } static inline void account_pc_requeue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_pc_requeue(&pci->io_stats, t, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_pc_requeue(ios, t, rw); } } static inline void __account_pc_c(struct io_stats *ios, int rw) { if (rw) ios->cwrites_pc++; else ios->creads_pc++; } static inline void account_pc_c(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_pc_c(&pci->io_stats, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_pc_c(ios, rw); } } static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t, int rw) { if (rw) { ios->qwrites++; ios->qwrite_kb += t_kb(t); } else { ios->qreads++; ios->qread_kb += t_kb(t); } } static inline void account_queue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_queue(&pci->io_stats, t, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_queue(ios, t, rw); } } static inline void __account_c(struct io_stats *ios, int rw, int bytes) { if (rw) { ios->cwrites++; ios->cwrite_kb += bytes >> 10; } else { ios->creads++; ios->cread_kb += bytes >> 10; } } static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci, int rw, int bytes) { __account_c(&pci->io_stats, rw, bytes); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_c(ios, rw, bytes); } } static inline void __account_issue(struct io_stats *ios, int rw, unsigned int bytes) { if (rw) { ios->iwrites++; ios->iwrite_kb += bytes >> 10; } else { ios->ireads++; ios->iread_kb += bytes >> 10; } } static inline void account_issue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_issue(&pci->io_stats, rw, t->bytes); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_issue(ios, rw, t->bytes); } } static inline void __account_unplug(struct io_stats *ios, int timer) { if (timer) ios->timer_unplugs++; else ios->io_unplugs++; } static inline void account_unplug(struct blk_io_trace *t, struct per_cpu_info *pci, int timer) { __account_unplug(&pci->io_stats, timer); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_unplug(ios, timer); } } static inline void __account_requeue(struct io_stats *ios, struct blk_io_trace *t, int rw) { if (rw) { ios->wrqueue++; ios->iwrite_kb -= t_kb(t); } else { ios->rrqueue++; ios->iread_kb -= t_kb(t); } } static inline void account_requeue(struct blk_io_trace *t, struct per_cpu_info *pci, int rw) { __account_requeue(&pci->io_stats, t, rw); if (per_process_stats) { struct io_stats *ios = find_process_io_stats(t->pid); __account_requeue(ios, t, rw); } } static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL); } static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL); } static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, -1, 0, NULL); } static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL); } static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { if (act[0] == 'F') log_track_frontmerge(pdi, t); process_fmt(act, pci, t, -1ULL, 0, NULL); } static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, -1ULL, 0, NULL); } static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, -1ULL, 0, NULL); } static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, -1ULL, 0, NULL); } static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { process_fmt(act, pci, t, -1ULL, 0, NULL); } static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act) { unsigned char *buf = (unsigned char *) t + sizeof(*t); process_fmt(act, pci, t, -1ULL, t->pdu_len, buf); } static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi, struct per_cpu_info *pci) { int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; int act = t->action & 0xffff; switch (act) { case __BLK_TA_QUEUE: log_generic(pci, t, "Q"); account_pc_queue(t, pci, w); break; case __BLK_TA_GETRQ: log_generic(pci, t, "G"); break; case __BLK_TA_SLEEPRQ: log_generic(pci, t, "S"); break; case __BLK_TA_REQUEUE: /* * can happen if we miss traces, don't let it go * below zero */ if (pdi->cur_depth[w]) pdi->cur_depth[w]--; account_pc_requeue(t, pci, w); log_generic(pci, t, "R"); break; case __BLK_TA_ISSUE: account_pc_issue(t, pci, w); pdi->cur_depth[w]++; if (pdi->cur_depth[w] > pdi->max_depth[w]) pdi->max_depth[w] = pdi->cur_depth[w]; log_pc(pci, t, "D"); break; case __BLK_TA_COMPLETE: if (pdi->cur_depth[w]) pdi->cur_depth[w]--; log_pc(pci, t, "C"); account_pc_c(t, pci, w); break; case __BLK_TA_INSERT: log_pc(pci, t, "I"); break; default: fprintf(stderr, "Bad pc action %x\n", act); break; } } static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi, struct per_cpu_info *pci) { int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; int act = t->action & 0xffff; switch (act) { case __BLK_TA_QUEUE: log_track_queue(pdi, t); account_queue(t, pci, w); log_queue(pci, t, "Q"); break; case __BLK_TA_INSERT: log_insert(pdi, pci, t, "I"); break; case __BLK_TA_BACKMERGE: account_m(t, pci, w); log_merge(pdi, pci, t, "M"); break; case __BLK_TA_FRONTMERGE: account_m(t, pci, w); log_merge(pdi, pci, t, "F"); break; case __BLK_TA_GETRQ: log_track_getrq(pdi, t); log_generic(pci, t, "G"); break; case __BLK_TA_SLEEPRQ: log_generic(pci, t, "S"); break; case __BLK_TA_REQUEUE: /* * can happen if we miss traces, don't let it go * below zero */ if (pdi->cur_depth[w]) pdi->cur_depth[w]--; account_requeue(t, pci, w); log_queue(pci, t, "R"); break; case __BLK_TA_ISSUE: account_issue(t, pci, w); pdi->cur_depth[w]++; if (pdi->cur_depth[w] > pdi->max_depth[w]) pdi->max_depth[w] = pdi->cur_depth[w]; log_issue(pdi, pci, t, "D"); break; case __BLK_TA_COMPLETE: if (pdi->cur_depth[w]) pdi->cur_depth[w]--; account_c(t, pci, w, t->bytes); log_complete(pdi, pci, t, "C"); break; case __BLK_TA_PLUG: log_action(pci, t, "P"); break; case __BLK_TA_UNPLUG_IO: account_unplug(t, pci, 0); log_unplug(pci, t, "U"); break; case __BLK_TA_UNPLUG_TIMER: account_unplug(t, pci, 1); log_unplug(pci, t, "UT"); break; case __BLK_TA_SPLIT: log_split(pci, t, "X"); break; case __BLK_TA_BOUNCE: log_generic(pci, t, "B"); break; case __BLK_TA_REMAP: log_generic(pci, t, "A"); break; case __BLK_TA_DRV_DATA: have_drv_data = 1; /* dump to binary file only */ break; default: fprintf(stderr, "Bad fs action %x\n", t->action); break; } } static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci, struct per_dev_info *pdi) { if (text_output) { if (t->action == BLK_TN_MESSAGE) handle_notify(t); else if (t->action & BLK_TC_ACT(BLK_TC_PC)) dump_trace_pc(t, pdi, pci); else dump_trace_fs(t, pdi, pci); } if (!pdi->events) pdi->first_reported_time = t->time; pdi->events++; if (bin_output_msgs || !(t->action & BLK_TC_ACT(BLK_TC_NOTIFY) && t->action == BLK_TN_MESSAGE)) output_binary(t, sizeof(*t) + t->pdu_len); } /* * print in a proper way, not too small and not too big. if more than * 1000,000K, turn into M and so on */ static char *size_cnv(char *dst, unsigned long long num, int in_kb) { char suff[] = { '\0', 'K', 'M', 'G', 'P' }; unsigned int i = 0; if (in_kb) i++; while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) { i++; num /= 1000; } sprintf(dst, "%'8Lu%c", num, suff[i]); return dst; } static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios, char *msg) { static char x[256], y[256]; fprintf(ofp, "%s\n", msg); fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1)); fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1)); fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1)); fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1)); fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0)); fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0)); fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1)); fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1)); fprintf(ofp, " Read Merges: %s, %siB\t", size_cnv(x, ios->mreads, 0), size_cnv(y, ios->mread_kb, 1)); fprintf(ofp, " Write Merges: %s, %siB\n", size_cnv(x, ios->mwrites, 0), size_cnv(y, ios->mwrite_kb, 1)); if (pdi) { fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' '); fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]); } if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc || ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) { fprintf(ofp, " PC Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads_pc, 0), size_cnv(y, ios->qread_kb_pc, 1)); fprintf(ofp, " PC Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites_pc, 0), size_cnv(y, ios->qwrite_kb_pc, 1)); fprintf(ofp, " PC Read Disp.: %s, %siB\t", size_cnv(x, ios->ireads_pc, 0), size_cnv(y, ios->iread_kb_pc, 1)); fprintf(ofp, " PC Write Disp.: %s, %siB\n", size_cnv(x, ios->iwrites_pc, 0), size_cnv(y, ios->iwrite_kb_pc, 1)); fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0)); fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0)); fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0)); fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites_pc, 0)); } fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' '); fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs); } static void dump_wait_stats(struct per_process_info *ppi) { unsigned long rawait = ppi->longest_allocation_wait[0] / 1000; unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000; unsigned long rcwait = ppi->longest_completion_wait[0] / 1000; unsigned long wawait = ppi->longest_allocation_wait[1] / 1000; unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000; unsigned long wcwait = ppi->longest_completion_wait[1] / 1000; fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' '); fprintf(ofp, " Allocation wait: %'8lu\n", wawait); fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' '); fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait); fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' '); fprintf(ofp, " Completion wait: %'8lu\n", wcwait); } static int ppi_name_compare(const void *p1, const void *p2) { struct per_process_info *ppi1 = *((struct per_process_info **) p1); struct per_process_info *ppi2 = *((struct per_process_info **) p2); int res; res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm); if (!res) res = ppi1->ppm->pid > ppi2->ppm->pid; return res; } static void sort_process_list(void) { struct per_process_info **ppis; struct per_process_info *ppi; int i = 0; ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *)); ppi = ppi_list; while (ppi) { ppis[i++] = ppi; ppi = ppi->list_next; } qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare); i = ppi_list_entries - 1; ppi_list = NULL; while (i >= 0) { ppi = ppis[i]; ppi->list_next = ppi_list; ppi_list = ppi; i--; } free(ppis); } static void show_process_stats(void) { struct per_process_info *ppi; sort_process_list(); ppi = ppi_list; while (ppi) { struct process_pid_map *ppm = ppi->ppm; char name[64]; if (ppi->more_than_one) sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid); else sprintf(name, "%s (%u)", ppm->comm, ppm->pid); dump_io_stats(NULL, &ppi->io_stats, name); dump_wait_stats(ppi); ppi = ppi->list_next; } fprintf(ofp, "\n"); } static void show_device_and_cpu_stats(void) { struct per_dev_info *pdi; struct per_cpu_info *pci; struct io_stats total, *ios; unsigned long long rrate, wrate, msec; int i, j, pci_events; char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3]; char name[32]; double ratio; for (pdi = devices, i = 0; i < ndevices; i++, pdi++) { memset(&total, 0, sizeof(total)); pci_events = 0; if (i > 0) fprintf(ofp, "\n"); for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) { if (!pci->nelems) continue; ios = &pci->io_stats; total.qreads += ios->qreads; total.qwrites += ios->qwrites; total.creads += ios->creads; total.cwrites += ios->cwrites; total.mreads += ios->mreads; total.mwrites += ios->mwrites; total.ireads += ios->ireads; total.iwrites += ios->iwrites; total.rrqueue += ios->rrqueue; total.wrqueue += ios->wrqueue; total.qread_kb += ios->qread_kb; total.qwrite_kb += ios->qwrite_kb; total.cread_kb += ios->cread_kb; total.cwrite_kb += ios->cwrite_kb; total.iread_kb += ios->iread_kb; total.iwrite_kb += ios->iwrite_kb; total.mread_kb += ios->mread_kb; total.mwrite_kb += ios->mwrite_kb; total.qreads_pc += ios->qreads_pc; total.qwrites_pc += ios->qwrites_pc; total.creads_pc += ios->creads_pc; total.cwrites_pc += ios->cwrites_pc; total.ireads_pc += ios->ireads_pc; total.iwrites_pc += ios->iwrites_pc; total.rrqueue_pc += ios->rrqueue_pc; total.wrqueue_pc += ios->wrqueue_pc; total.qread_kb_pc += ios->qread_kb_pc; total.qwrite_kb_pc += ios->qwrite_kb_pc; total.iread_kb_pc += ios->iread_kb_pc; total.iwrite_kb_pc += ios->iwrite_kb_pc; total.timer_unplugs += ios->timer_unplugs; total.io_unplugs += ios->io_unplugs; snprintf(line, sizeof(line) - 1, "CPU%d (%s):", j, get_dev_name(pdi, name, sizeof(name))); dump_io_stats(pdi, ios, line); pci_events++; } if (pci_events > 1) { fprintf(ofp, "\n"); snprintf(line, sizeof(line) - 1, "Total (%s):", get_dev_name(pdi, name, sizeof(name))); dump_io_stats(NULL, &total, line); } wrate = rrate = 0; msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000; if (msec) { rrate = 1000 * total.cread_kb / msec; wrate = 1000 * total.cwrite_kb / msec; } fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n", rrate, wrate); fprintf(ofp, "Events (%s): %'Lu entries\n", get_dev_name(pdi, line, sizeof(line)), pdi->events); collect_pdi_skips(pdi); if (!pdi->skips && !pdi->events) ratio = 0.0; else ratio = 100.0 * ((double)pdi->seq_skips / (double)(pdi->events + pdi->seq_skips)); fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n", pdi->skips, pdi->seq_skips, ratio); } } static void find_genesis(void) { struct trace *t = trace_list; genesis_time = -1ULL; while (t != NULL) { if (t->bit->time < genesis_time) genesis_time = t->bit->time; t = t->next; } /* The time stamp record will usually be the first * record in the trace, but not always. */ if (start_timestamp && start_timestamp != genesis_time) { long delta = genesis_time - start_timestamp; abs_start_time.tv_sec += SECONDS(delta); abs_start_time.tv_nsec += NANO_SECONDS(delta); if (abs_start_time.tv_nsec < 0) { abs_start_time.tv_nsec += 1000000000; abs_start_time.tv_sec -= 1; } else if (abs_start_time.tv_nsec > 1000000000) { abs_start_time.tv_nsec -= 1000000000; abs_start_time.tv_sec += 1; } } } static inline int check_stopwatch(struct blk_io_trace *bit) { if (bit->time < stopwatch_end && bit->time >= stopwatch_start) return 0; return 1; } /* * return youngest entry read */ static int sort_entries(unsigned long long *youngest) { struct per_dev_info *pdi = NULL; struct per_cpu_info *pci = NULL; struct trace *t; if (!genesis_time) find_genesis(); *youngest = 0; while ((t = trace_list) != NULL) { struct blk_io_trace *bit = t->bit; trace_list = t->next; bit->time -= genesis_time; if (bit->time < *youngest || !*youngest) *youngest = bit->time; if (!pdi || pdi->dev != bit->device) { pdi = get_dev_info(bit->device); pci = NULL; } if (!pci || pci->cpu != bit->cpu) pci = get_cpu_info(pdi, bit->cpu); if (bit->sequence < pci->smallest_seq_read) pci->smallest_seq_read = bit->sequence; if (check_stopwatch(bit)) { bit_free(bit); t_free(t); continue; } if (trace_rb_insert_sort(t)) return -1; } return 0; } /* * to continue, we must have traces from all online cpus in the tree */ static int check_cpu_map(struct per_dev_info *pdi) { unsigned long *cpu_map; struct rb_node *n; struct trace *__t; unsigned int i; int ret, cpu; /* * create a map of the cpus we have traces for */ cpu_map = malloc(pdi->cpu_map_max / sizeof(long)); memset(cpu_map, 0, sizeof(*cpu_map)); n = rb_first(&rb_sort_root); while (n) { __t = rb_entry(n, struct trace, rb_node); cpu = __t->bit->cpu; cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu)); n = rb_next(n); } /* * we can't continue if pdi->cpu_map has entries set that we don't * have in the sort rbtree. the opposite is not a problem, though */ ret = 0; for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) { if (pdi->cpu_map[i] & ~(cpu_map[i])) { ret = 1; break; } } free(cpu_map); return ret; } static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force) { struct blk_io_trace *bit = t->bit; unsigned long expected_sequence; struct per_cpu_info *pci; struct trace *__t; pci = get_cpu_info(pdi, bit->cpu); expected_sequence = pci->last_sequence + 1; if (!expected_sequence) { /* * 1 should be the first entry, just allow it */ if (bit->sequence == 1) return 0; if (bit->sequence == pci->smallest_seq_read) return 0; return check_cpu_map(pdi); } if (bit->sequence == expected_sequence) return 0; /* * we may not have seen that sequence yet. if we are not doing * the final run, break and wait for more entries. */ if (expected_sequence < pci->smallest_seq_read) { __t = trace_rb_find_last(pdi, pci, expected_sequence); if (!__t) goto skip; __put_trace_last(pdi, __t); return 0; } else if (!force) { return 1; } else { skip: if (check_current_skips(pci, bit->sequence)) return 0; if (expected_sequence < bit->sequence) insert_skip(pci, expected_sequence, bit->sequence - 1); return 0; } } static void show_entries_rb(int force) { struct per_dev_info *pdi = NULL; struct per_cpu_info *pci = NULL; struct blk_io_trace *bit; struct rb_node *n; struct trace *t; while ((n = rb_first(&rb_sort_root)) != NULL) { if (is_done() && !force && !pipeline) break; t = rb_entry(n, struct trace, rb_node); bit = t->bit; if (read_sequence - t->read_sequence < 1 && !force) break; if (!pdi || pdi->dev != bit->device) { pdi = get_dev_info(bit->device); pci = NULL; } if (!pdi) { fprintf(stderr, "Unknown device ID? (%d,%d)\n", MAJOR(bit->device), MINOR(bit->device)); break; } if (!(bit->action == BLK_TN_MESSAGE) && check_sequence(pdi, t, force)) break; if (!force && bit->time > last_allowed_time) break; check_time(pdi, bit); if (!pci || pci->cpu != bit->cpu) pci = get_cpu_info(pdi, bit->cpu); if (!(bit->action == BLK_TN_MESSAGE)) pci->last_sequence = bit->sequence; pci->nelems++; if (bit->action & (act_mask << BLK_TC_SHIFT)) dump_trace(bit, pci, pdi); put_trace(pdi, t); } } static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock) { int ret, bytes_left, fl; void *p; if (block != *fdblock) { fl = fcntl(fd, F_GETFL); if (!block) { *fdblock = 0; fcntl(fd, F_SETFL, fl | O_NONBLOCK); } else { *fdblock = 1; fcntl(fd, F_SETFL, fl & ~O_NONBLOCK); } } bytes_left = bytes; p = buffer; while (bytes_left > 0) { ret = read(fd, p, bytes_left); if (!ret) return 1; else if (ret < 0) { if (errno != EAGAIN) { perror("read"); return -1; } /* * never do partial reads. we can return if we * didn't read anything and we should not block, * otherwise wait for data */ if ((bytes_left == bytes) && !block) return 1; usleep(10); continue; } else { p += ret; bytes_left -= ret; } } return 0; } static inline __u16 get_pdulen(struct blk_io_trace *bit) { if (data_is_native) return bit->pdu_len; return __bswap_16(bit->pdu_len); } static inline __u32 get_magic(struct blk_io_trace *bit) { if (data_is_native) return bit->magic; return __bswap_32(bit->magic); } static int read_events(int fd, int always_block, int *fdblock) { struct per_dev_info *pdi = NULL; unsigned int events = 0; while (!is_done() && events < rb_batch) { struct blk_io_trace *bit; struct trace *t; int pdu_len, should_block, ret; __u32 magic; bit = bit_alloc(); should_block = !events || always_block; ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock); if (ret) { bit_free(bit); if (!events && ret < 0) events = ret; break; } /* * look at first trace to check whether we need to convert * data in the future */ if (data_is_native == -1 && check_data_endianness(bit->magic)) break; magic = get_magic(bit); if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { fprintf(stderr, "Bad magic %x\n", magic); break; } pdu_len = get_pdulen(bit); if (pdu_len) { void *ptr = realloc(bit, sizeof(*bit) + pdu_len); if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) { bit_free(ptr); break; } bit = ptr; } trace_to_cpu(bit); if (verify_trace(bit)) { bit_free(bit); continue; } /* * not a real trace, so grab and handle it here */ if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) { handle_notify(bit); output_binary(bit, sizeof(*bit) + bit->pdu_len); continue; } t = t_alloc(); memset(t, 0, sizeof(*t)); t->bit = bit; t->read_sequence = read_sequence; t->next = trace_list; trace_list = t; if (!pdi || pdi->dev != bit->device) pdi = get_dev_info(bit->device); if (bit->time > pdi->last_read_time) pdi->last_read_time = bit->time; events++; } return events; } /* * Managing input streams */ struct ms_stream { struct ms_stream *next; struct trace *first, *last; struct per_dev_info *pdi; unsigned int cpu; }; #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff)) struct ms_stream *ms_head; struct ms_stream *ms_hash[256]; static void ms_sort(struct ms_stream *msp); static int ms_prime(struct ms_stream *msp); static inline struct trace *ms_peek(struct ms_stream *msp) { return (msp == NULL) ? NULL : msp->first; } static inline __u64 ms_peek_time(struct ms_stream *msp) { return ms_peek(msp)->bit->time; } static inline void ms_resort(struct ms_stream *msp) { if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) { ms_head = msp->next; msp->next = NULL; ms_sort(msp); } } static inline void ms_deq(struct ms_stream *msp) { msp->first = msp->first->next; if (!msp->first) { msp->last = NULL; if (!ms_prime(msp)) { ms_head = msp->next; msp->next = NULL; return; } } ms_resort(msp); } static void ms_sort(struct ms_stream *msp) { __u64 msp_t = ms_peek_time(msp); struct ms_stream *this_msp = ms_head; if (this_msp == NULL) ms_head = msp; else if (msp_t < ms_peek_time(this_msp)) { msp->next = this_msp; ms_head = msp; } else { while (this_msp->next && ms_peek_time(this_msp->next) < msp_t) this_msp = this_msp->next; msp->next = this_msp->next; this_msp->next = msp; } } static int ms_prime(struct ms_stream *msp) { __u32 magic; unsigned int i; struct trace *t; struct per_dev_info *pdi = msp->pdi; struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu); struct blk_io_trace *bit = NULL; int ret, pdu_len, ndone = 0; for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) { bit = bit_alloc(); ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock); if (ret) goto err; if (data_is_native == -1 && check_data_endianness(bit->magic)) goto err; magic = get_magic(bit); if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { fprintf(stderr, "Bad magic %x\n", magic); goto err; } pdu_len = get_pdulen(bit); if (pdu_len) { void *ptr = realloc(bit, sizeof(*bit) + pdu_len); ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len, 1, &pci->fdblock); if (ret) { free(ptr); bit = NULL; goto err; } bit = ptr; } trace_to_cpu(bit); if (verify_trace(bit)) goto err; if (bit->cpu != pci->cpu) { fprintf(stderr, "cpu %d trace info has error cpu %d\n", pci->cpu, bit->cpu); continue; } if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) { handle_notify(bit); output_binary(bit, sizeof(*bit) + bit->pdu_len); bit_free(bit); i -= 1; continue; } if (bit->time > pdi->last_read_time) pdi->last_read_time = bit->time; t = t_alloc(); memset(t, 0, sizeof(*t)); t->bit = bit; if (msp->first == NULL) msp->first = msp->last = t; else { msp->last->next = t; msp->last = t; } ndone++; } return ndone; err: if (bit) bit_free(bit); cpu_mark_offline(pdi, pci->cpu); close(pci->fd); pci->fd = -1; return ndone; } static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu) { struct ms_stream *msp = malloc(sizeof(*msp)); msp->next = NULL; msp->first = msp->last = NULL; msp->pdi = pdi; msp->cpu = cpu; if (ms_prime(msp)) ms_sort(msp); return msp; } static int setup_file(struct per_dev_info *pdi, int cpu) { int len = 0; struct stat st; char *p, *dname; struct per_cpu_info *pci = get_cpu_info(pdi, cpu); pci->cpu = cpu; pci->fdblock = -1; p = strdup(pdi->name); dname = dirname(p); if (strcmp(dname, ".")) { input_dir = dname; p = strdup(pdi->name); strcpy(pdi->name, basename(p)); } free(p); if (input_dir) len = sprintf(pci->fname, "%s/", input_dir); snprintf(pci->fname + len, sizeof(pci->fname)-1-len, "%s.blktrace.%d", pdi->name, pci->cpu); if (stat(pci->fname, &st) < 0) return 0; if (!st.st_size) return 1; pci->fd = open(pci->fname, O_RDONLY); if (pci->fd < 0) { perror(pci->fname); return 0; } printf("Input file %s added\n", pci->fname); cpu_mark_online(pdi, pci->cpu); pdi->nfiles++; ms_alloc(pdi, pci->cpu); return 1; } static int handle(struct ms_stream *msp) { struct trace *t; struct per_dev_info *pdi; struct per_cpu_info *pci; struct blk_io_trace *bit; t = ms_peek(msp); bit = t->bit; pdi = msp->pdi; pci = get_cpu_info(pdi, msp->cpu); pci->nelems++; bit->time -= genesis_time; if (t->bit->time > stopwatch_end) return 0; pdi->last_reported_time = bit->time; if ((bit->action & (act_mask << BLK_TC_SHIFT))&& t->bit->time >= stopwatch_start) dump_trace(bit, pci, pdi); ms_deq(msp); if (text_output) trace_rb_insert_last(pdi, t); else { bit_free(t->bit); t_free(t); } return 1; } /* * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X * is given, then strip back down to 'foo' to avoid missing files. */ static int name_fixup(char *name) { char *b; if (!name) return 1; b = strstr(name, ".blktrace."); if (b) *b = '\0'; return 0; } static int do_file(void) { int i, cpu, ret; struct per_dev_info *pdi; /* * first prepare all files for reading */ for (i = 0; i < ndevices; i++) { pdi = &devices[i]; ret = name_fixup(pdi->name); if (ret) return ret; for (cpu = 0; setup_file(pdi, cpu); cpu++) ; if (!cpu) { fprintf(stderr,"No input files found for %s\n", pdi->name); return 1; } } /* * Get the initial time stamp */ if (ms_head) genesis_time = ms_peek_time(ms_head); /* * Keep processing traces while any are left */ while (!is_done() && ms_head && handle(ms_head)) ; return 0; } static void do_pipe(int fd) { unsigned long long youngest; int events, fdblock; last_allowed_time = -1ULL; fdblock = -1; while ((events = read_events(fd, 0, &fdblock)) > 0) { read_sequence++; #if 0 smallest_seq_read = -1U; #endif if (sort_entries(&youngest)) break; if (youngest > stopwatch_end) break; show_entries_rb(0); } if (rb_sort_entries) show_entries_rb(1); } static int do_fifo(void) { int fd; if (!strcmp(pipename, "-")) fd = dup(STDIN_FILENO); else fd = open(pipename, O_RDONLY); if (fd == -1) { perror("dup stdin"); return -1; } do_pipe(fd); close(fd); return 0; } static void show_stats(void) { if (!ofp) return; if (stats_printed) return; stats_printed = 1; if (per_process_stats) show_process_stats(); if (per_device_and_cpu_stats) show_device_and_cpu_stats(); fflush(ofp); } static void handle_sigint(__attribute__((__unused__)) int sig) { done = 1; } /* * Extract start and duration times from a string, allowing * us to specify a time interval of interest within a trace. * Format: "duration" (start is zero) or "start:duration". */ static int find_stopwatch_interval(char *string) { double value; char *sp; value = strtod(string, &sp); if (sp == string) { fprintf(stderr,"Invalid stopwatch timer: %s\n", string); return 1; } if (*sp == ':') { stopwatch_start = DOUBLE_TO_NANO_ULL(value); string = sp + 1; value = strtod(string, &sp); if (sp == string || *sp != '\0') { fprintf(stderr,"Invalid stopwatch duration time: %s\n", string); return 1; } } else if (*sp != '\0') { fprintf(stderr,"Invalid stopwatch start timer: %s\n", string); return 1; } stopwatch_end = DOUBLE_TO_NANO_ULL(value); if (stopwatch_end <= stopwatch_start) { fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n", stopwatch_start, stopwatch_end); return 1; } return 0; } static int is_pipe(const char *str) { struct stat st; if (!strcmp(str, "-")) return 1; if (!stat(str, &st) && S_ISFIFO(st.st_mode)) return 1; return 0; } #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vVM" static char usage_str[] = "\n\n" \ "-i | --input=\n" \ "[ -a | --act-mask= ]\n" \ "[ -A | --set-mask= ]\n" \ "[ -b | --batch= ]\n" \ "[ -d | --dump-binary= ]\n" \ "[ -D | --input-directory= ]\n" \ "[ -f | --format= ]\n" \ "[ -F | --format-spec= ]\n" \ "[ -h | --hash-by-name ]\n" \ "[ -o | --output= ]\n" \ "[ -O | --no-text-output ]\n" \ "[ -q | --quiet ]\n" \ "[ -s | --per-program-stats ]\n" \ "[ -t | --track-ios ]\n" \ "[ -w