bootgraph.pl 4.6 KB

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