bootgraph.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. my %start, %end;
  37. my $done = 0;
  38. my $maxtime = 0;
  39. my $firsttime = 100;
  40. my $count = 0;
  41. my %pids;
  42. while (<>) {
  43. my $line = $_;
  44. if ($line =~ /([0-9\.]+)\] calling ([a-zA-Z0-9\_]+)\+/) {
  45. my $func = $2;
  46. if ($done == 0) {
  47. $start{$func} = $1;
  48. if ($1 < $firsttime) {
  49. $firsttime = $1;
  50. }
  51. }
  52. if ($line =~ /\@ ([0-9]+)/) {
  53. $pids{$func} = $1;
  54. }
  55. $count = $count + 1;
  56. }
  57. if ($line =~ /([0-9\.]+)\] initcall ([a-zA-Z0-9\_]+)\+.*returned/) {
  58. if ($done == 0) {
  59. $end{$2} = $1;
  60. $maxtime = $1;
  61. }
  62. }
  63. if ($line =~ /Write protecting the/) {
  64. $done = 1;
  65. }
  66. if ($line =~ /Freeing unused kernel memory/) {
  67. $done = 1;
  68. }
  69. }
  70. if ($count == 0) {
  71. print "No data found in the dmesg. Make sure that 'printk.time=1' and\n";
  72. print "'initcall_debug' are passed on the kernel command line.\n\n";
  73. print "Usage: \n";
  74. print " dmesg | perl scripts/bootgraph.pl > output.svg\n\n";
  75. exit;
  76. }
  77. print "<?xml version=\"1.0\" standalone=\"no\"?> \n";
  78. print "<svg width=\"1000\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
  79. my @styles;
  80. $styles[0] = "fill:rgb(0,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  81. $styles[1] = "fill:rgb(0,255,0);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  82. $styles[2] = "fill:rgb(255,0,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  83. $styles[3] = "fill:rgb(255,255,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  84. $styles[4] = "fill:rgb(255,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  85. $styles[5] = "fill:rgb(0,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  86. $styles[6] = "fill:rgb(0,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  87. $styles[7] = "fill:rgb(0,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  88. $styles[8] = "fill:rgb(255,0,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  89. $styles[9] = "fill:rgb(255,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  90. $styles[10] = "fill:rgb(255,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  91. $styles[11] = "fill:rgb(128,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
  92. my $mult = 950.0 / ($maxtime - $firsttime);
  93. my $threshold = ($maxtime - $firsttime) / 60.0;
  94. my $stylecounter = 0;
  95. my %rows;
  96. my $rowscount = 1;
  97. while (($key,$value) = each %start) {
  98. my $duration = $end{$key} - $start{$key};
  99. if ($duration >= $threshold) {
  100. my $s, $s2, $e, $y;
  101. $pid = $pids{$key};
  102. if (!defined($rows{$pid})) {
  103. $rows{$pid} = $rowscount;
  104. $rowscount = $rowscount + 1;
  105. }
  106. $s = ($value - $firsttime) * $mult;
  107. $s2 = $s + 6;
  108. $e = ($end{$key} - $firsttime) * $mult;
  109. $w = $e - $s;
  110. $y = $rows{$pid} * 150;
  111. $y2 = $y + 4;
  112. $style = $styles[$stylecounter];
  113. $stylecounter = $stylecounter + 1;
  114. if ($stylecounter > 11) {
  115. $stylecounter = 0;
  116. };
  117. print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"145\" style=\"$style\"/>\n";
  118. print "<text transform=\"translate($s2,$y2) rotate(90)\">$key</text>\n";
  119. }
  120. }
  121. # print the time line on top
  122. my $time = $firsttime;
  123. my $step = ($maxtime - $firsttime) / 15;
  124. while ($time < $maxtime) {
  125. my $s2 = ($time - $firsttime) * $mult;
  126. my $tm = int($time * 100) / 100.0;
  127. print "<text transform=\"translate($s2,89) rotate(90)\">$tm</text>\n";
  128. $time = $time + $step;
  129. }
  130. print "</svg>\n";