bootgraph.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. my %start;
  38. my %end;
  39. my %type;
  40. my $done = 0;
  41. my $maxtime = 0;
  42. my $firsttime = 100;
  43. my $count = 0;
  44. my %pids;
  45. my %pidctr;
  46. while (<>) {
  47. my $line = $_;
  48. if ($line =~ /([0-9\.]+)\] calling ([a-zA-Z0-9\_\.]+)\+/) {
  49. my $func = $2;
  50. if ($done == 0) {
  51. $start{$func} = $1;
  52. $type{$func} = 0;
  53. if ($1 < $firsttime) {
  54. $firsttime = $1;
  55. }
  56. }
  57. if ($line =~ /\@ ([0-9]+)/) {
  58. $pids{$func} = $1;
  59. }
  60. $count = $count + 1;
  61. }
  62. if ($line =~ /([0-9\.]+)\] async_waiting @ ([0-9]+)/) {
  63. my $pid = $2;
  64. my $func;
  65. if (!defined($pidctr{$pid})) {
  66. $func = "wait_" . $pid . "_1";
  67. $pidctr{$pid} = 1;
  68. } else {
  69. $pidctr{$pid} = $pidctr{$pid} + 1;
  70. $func = "wait_" . $pid . "_" . $pidctr{$pid};
  71. }
  72. if ($done == 0) {
  73. $start{$func} = $1;
  74. $type{$func} = 1;
  75. if ($1 < $firsttime) {
  76. $firsttime = $1;
  77. }
  78. }
  79. $pids{$func} = $pid;
  80. $count = $count + 1;
  81. }
  82. if ($line =~ /([0-9\.]+)\] initcall ([a-zA-Z0-9\_\.]+)\+.*returned/) {
  83. if ($done == 0) {
  84. $end{$2} = $1;
  85. $maxtime = $1;
  86. }
  87. }
  88. if ($line =~ /([0-9\.]+)\] async_continuing @ ([0-9]+)/) {
  89. my $pid = $2;
  90. my $func = "wait_" . $pid . "_" . $pidctr{$pid};
  91. $end{$func} = $1;
  92. $maxtime = $1;
  93. }
  94. if ($line =~ /Write protecting the/) {
  95. $done = 1;
  96. }
  97. if ($line =~ /Freeing unused kernel memory/) {
  98. $done = 1;
  99. }
  100. }
  101. if ($count == 0) {
  102. print STDERR <<END;
  103. No data found in the dmesg. Make sure that 'printk.time=1' and
  104. 'initcall_debug' are passed on the kernel command line.
  105. Usage:
  106. dmesg | perl scripts/bootgraph.pl > output.svg
  107. END
  108. exit 1;
  109. }
  110. print "<?xml version=\"1.0\" standalone=\"no\"?> \n";
  111. print "<svg width=\"2000\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
  112. my @styles;
  113. $styles[0] = "fill:rgb(0,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  114. $styles[1] = "fill:rgb(0,255,0);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  115. $styles[2] = "fill:rgb(255,0,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  116. $styles[3] = "fill:rgb(255,255,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  117. $styles[4] = "fill:rgb(255,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  118. $styles[5] = "fill:rgb(0,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  119. $styles[6] = "fill:rgb(0,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  120. $styles[7] = "fill:rgb(0,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  121. $styles[8] = "fill:rgb(255,0,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  122. $styles[9] = "fill:rgb(255,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  123. $styles[10] = "fill:rgb(255,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  124. $styles[11] = "fill:rgb(128,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  125. my $style_wait = "fill:rgb(128,128,128);fill-opacity:0.5;stroke-width:0;stroke:rgb(0,0,0)";
  126. my $mult = 1950.0 / ($maxtime - $firsttime);
  127. my $threshold2 = ($maxtime - $firsttime) / 120.0;
  128. my $threshold = $threshold2/10;
  129. my $stylecounter = 0;
  130. my %rows;
  131. my $rowscount = 1;
  132. my @initcalls = sort { $start{$a} <=> $start{$b} } keys(%start);
  133. foreach my $key (@initcalls) {
  134. my $duration = $end{$key} - $start{$key};
  135. if ($duration >= $threshold) {
  136. my ($s, $s2, $s3, $e, $w, $y, $y2, $style);
  137. my $pid = $pids{$key};
  138. if (!defined($rows{$pid})) {
  139. $rows{$pid} = $rowscount;
  140. $rowscount = $rowscount + 1;
  141. }
  142. $s = ($start{$key} - $firsttime) * $mult;
  143. $s2 = $s + 6;
  144. $s3 = $s + 1;
  145. $e = ($end{$key} - $firsttime) * $mult;
  146. $w = $e - $s;
  147. $y = $rows{$pid} * 150;
  148. $y2 = $y + 4;
  149. $style = $styles[$stylecounter];
  150. $stylecounter = $stylecounter + 1;
  151. if ($stylecounter > 11) {
  152. $stylecounter = 0;
  153. };
  154. if ($type{$key} == 1) {
  155. $y = $y + 15;
  156. print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"115\" style=\"$style_wait\"/>\n";
  157. } else {
  158. print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"145\" style=\"$style\"/>\n";
  159. if ($duration >= $threshold2) {
  160. print "<text transform=\"translate($s2,$y2) rotate(90)\">$key</text>\n";
  161. } else {
  162. print "<text transform=\"translate($s3,$y2) rotate(90)\" font-size=\"3pt\">$key</text>\n";
  163. }
  164. }
  165. }
  166. }
  167. # print the time line on top
  168. my $time = $firsttime;
  169. my $step = ($maxtime - $firsttime) / 15;
  170. while ($time < $maxtime) {
  171. my $s3 = ($time - $firsttime) * $mult;
  172. my $tm = int($time * 100) / 100.0;
  173. print "<text transform=\"translate($s3,89) rotate(90)\">$tm</text>\n";
  174. $time = $time + $step;
  175. }
  176. print "</svg>\n";