bootgraph.pl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/perl
  2. # Copyright 2008, Intel Corporation
  3. #
  4. # This file is part of the Linux kernel
  5. #
  6. # This program file is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the
  8. # Free Software Foundation; version 2 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program in a file named COPYING; if not, write to the
  17. # Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor,
  19. # Boston, MA 02110-1301 USA
  20. #
  21. # Authors:
  22. # Arjan van de Ven <arjan@linux.intel.com>
  23. #
  24. # This script turns a dmesg output into a SVG graphic that shows which
  25. # functions take how much time. You can view SVG graphics with various
  26. # programs, including Inkscape, The Gimp and Firefox.
  27. #
  28. #
  29. # For this script to work, the kernel needs to be compiled with the
  30. # CONFIG_PRINTK_TIME configuration option enabled, and with
  31. # "initcall_debug" passed on the kernel command line.
  32. #
  33. # usage:
  34. # dmesg | perl scripts/bootgraph.pl > output.svg
  35. #
  36. use strict;
  37. use Getopt::Long;
  38. my $header = 0;
  39. sub help {
  40. my $text = << "EOM";
  41. Usage:
  42. 1) dmesg | perl scripts/bootgraph.pl [OPTION] > output.svg
  43. 2) perl scripts/bootgraph.pl -h
  44. Options:
  45. -header Insert kernel version and date
  46. EOM
  47. my $std=shift;
  48. if ($std == 1) {
  49. print STDERR $text;
  50. } else {
  51. print $text;
  52. }
  53. exit;
  54. }
  55. GetOptions(
  56. 'h|help' =>\&help,
  57. 'header' =>\$header
  58. );
  59. my %start;
  60. my %end;
  61. my %type;
  62. my $done = 0;
  63. my $maxtime = 0;
  64. my $firsttime = 99999;
  65. my $count = 0;
  66. my %pids;
  67. my %pidctr;
  68. my $headerstep = 20;
  69. my $xheader = 15;
  70. my $yheader = 25;
  71. my $cyheader = 0;
  72. while (<>) {
  73. my $line = $_;
  74. if ($line =~ /([0-9\.]+)\] calling ([a-zA-Z0-9\_\.]+)\+/) {
  75. my $func = $2;
  76. if ($done == 0) {
  77. $start{$func} = $1;
  78. $type{$func} = 0;
  79. if ($1 < $firsttime) {
  80. $firsttime = $1;
  81. }
  82. }
  83. if ($line =~ /\@ ([0-9]+)/) {
  84. $pids{$func} = $1;
  85. }
  86. $count = $count + 1;
  87. }
  88. if ($line =~ /([0-9\.]+)\] async_waiting @ ([0-9]+)/) {
  89. my $pid = $2;
  90. my $func;
  91. if (!defined($pidctr{$pid})) {
  92. $func = "wait_" . $pid . "_1";
  93. $pidctr{$pid} = 1;
  94. } else {
  95. $pidctr{$pid} = $pidctr{$pid} + 1;
  96. $func = "wait_" . $pid . "_" . $pidctr{$pid};
  97. }
  98. if ($done == 0) {
  99. $start{$func} = $1;
  100. $type{$func} = 1;
  101. if ($1 < $firsttime) {
  102. $firsttime = $1;
  103. }
  104. }
  105. $pids{$func} = $pid;
  106. $count = $count + 1;
  107. }
  108. if ($line =~ /([0-9\.]+)\] initcall ([a-zA-Z0-9\_\.]+)\+.*returned/) {
  109. if ($done == 0) {
  110. $end{$2} = $1;
  111. $maxtime = $1;
  112. }
  113. }
  114. if ($line =~ /([0-9\.]+)\] async_continuing @ ([0-9]+)/) {
  115. my $pid = $2;
  116. my $func = "wait_" . $pid . "_" . $pidctr{$pid};
  117. $end{$func} = $1;
  118. $maxtime = $1;
  119. }
  120. if ($line =~ /Write protecting the/) {
  121. $done = 1;
  122. }
  123. if ($line =~ /Freeing unused kernel memory/) {
  124. $done = 1;
  125. }
  126. }
  127. if ($count == 0) {
  128. print STDERR <<END;
  129. No data found in the dmesg. Make sure that 'printk.time=1' and
  130. 'initcall_debug' are passed on the kernel command line.
  131. END
  132. help(1);
  133. exit 1;
  134. }
  135. print "<?xml version=\"1.0\" standalone=\"no\"?> \n";
  136. print "<svg width=\"2000\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
  137. if ($header) {
  138. my $version = `uname -a`;
  139. my $date = `date`;
  140. print "<text transform=\"translate($xheader,$yheader)\">Kernel version: $version</text>\n";
  141. $cyheader = $yheader+$headerstep;
  142. print "<text transform=\"translate($xheader,$cyheader)\">Date: $date</text>\n";
  143. }
  144. my @styles;
  145. $styles[0] = "fill:rgb(0,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  146. $styles[1] = "fill:rgb(0,255,0);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  147. $styles[2] = "fill:rgb(255,0,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  148. $styles[3] = "fill:rgb(255,255,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  149. $styles[4] = "fill:rgb(255,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  150. $styles[5] = "fill:rgb(0,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  151. $styles[6] = "fill:rgb(0,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  152. $styles[7] = "fill:rgb(0,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  153. $styles[8] = "fill:rgb(255,0,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  154. $styles[9] = "fill:rgb(255,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  155. $styles[10] = "fill:rgb(255,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  156. $styles[11] = "fill:rgb(128,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  157. my $style_wait = "fill:rgb(128,128,128);fill-opacity:0.5;stroke-width:0;stroke:rgb(0,0,0)";
  158. my $mult = 1950.0 / ($maxtime - $firsttime);
  159. my $threshold2 = ($maxtime - $firsttime) / 120.0;
  160. my $threshold = $threshold2/10;
  161. my $stylecounter = 0;
  162. my %rows;
  163. my $rowscount = 1;
  164. my @initcalls = sort { $start{$a} <=> $start{$b} } keys(%start);
  165. foreach my $key (@initcalls) {
  166. my $duration = $end{$key} - $start{$key};
  167. if ($duration >= $threshold) {
  168. my ($s, $s2, $s3, $e, $w, $y, $y2, $style);
  169. my $pid = $pids{$key};
  170. if (!defined($rows{$pid})) {
  171. $rows{$pid} = $rowscount;
  172. $rowscount = $rowscount + 1;
  173. }
  174. $s = ($start{$key} - $firsttime) * $mult;
  175. $s2 = $s + 6;
  176. $s3 = $s + 1;
  177. $e = ($end{$key} - $firsttime) * $mult;
  178. $w = $e - $s;
  179. $y = $rows{$pid} * 150;
  180. $y2 = $y + 4;
  181. $style = $styles[$stylecounter];
  182. $stylecounter = $stylecounter + 1;
  183. if ($stylecounter > 11) {
  184. $stylecounter = 0;
  185. };
  186. if ($type{$key} == 1) {
  187. $y = $y + 15;
  188. print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"115\" style=\"$style_wait\"/>\n";
  189. } else {
  190. print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"145\" style=\"$style\"/>\n";
  191. if ($duration >= $threshold2) {
  192. print "<text transform=\"translate($s2,$y2) rotate(90)\">$key</text>\n";
  193. } else {
  194. print "<text transform=\"translate($s3,$y2) rotate(90)\" font-size=\"3pt\">$key</text>\n";
  195. }
  196. }
  197. }
  198. }
  199. # print the time line on top
  200. my $time = $firsttime;
  201. my $step = ($maxtime - $firsttime) / 15;
  202. while ($time < $maxtime) {
  203. my $s3 = ($time - $firsttime) * $mult;
  204. my $tm = int($time * 100) / 100.0;
  205. print "<text transform=\"translate($s3,89) rotate(90)\">$tm</text>\n";
  206. $time = $time + $step;
  207. }
  208. print "</svg>\n";