bootgraph.pl 4.5 KB

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