blob: 594c55541b1623aacb78eb986356f14383502157 [file] [log] [blame]
Finn Behrens2d1f7c92020-11-23 15:15:33 +01001#!/usr/bin/env perl
Arjan van de Venaa5d9152008-09-13 09:36:06 -07002
3# Copyright 2008, Intel Corporation
4#
5# This file is part of the Linux kernel
6#
7# This program file is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by the
9# Free Software Foundation; version 2 of the License.
10#
11# This program is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14# for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program in a file named COPYING; if not, write to the
18# Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor,
20# Boston, MA 02110-1301 USA
21#
22# Authors:
23# Arjan van de Ven <arjan@linux.intel.com>
24
25
26#
27# This script turns a dmesg output into a SVG graphic that shows which
28# functions take how much time. You can view SVG graphics with various
29# programs, including Inkscape, The Gimp and Firefox.
30#
31#
32# For this script to work, the kernel needs to be compiled with the
33# CONFIG_PRINTK_TIME configuration option enabled, and with
34# "initcall_debug" passed on the kernel command line.
35#
36# usage:
37# dmesg | perl scripts/bootgraph.pl > output.svg
38#
39
Alan Jenkins2a813f82008-10-14 14:18:07 +010040use strict;
Fabian Frederick67554fa2014-04-02 21:39:52 +020041use Getopt::Long;
42my $header = 0;
43
44sub help {
45 my $text = << "EOM";
46Usage:
471) dmesg | perl scripts/bootgraph.pl [OPTION] > output.svg
482) perl scripts/bootgraph.pl -h
49
50Options:
51 -header Insert kernel version and date
52EOM
53 my $std=shift;
54 if ($std == 1) {
55 print STDERR $text;
56 } else {
57 print $text;
58 }
59 exit;
60}
61
62GetOptions(
63 'h|help' =>\&help,
64 'header' =>\$header
65);
Alan Jenkins2a813f82008-10-14 14:18:07 +010066
67my %start;
68my %end;
Arjan van de Vend3f8dde2009-01-10 10:03:05 -080069my %type;
Arjan van de Venaa5d9152008-09-13 09:36:06 -070070my $done = 0;
Arjan van de Venaa5d9152008-09-13 09:36:06 -070071my $maxtime = 0;
Andrew Murrayc4434532011-06-09 22:40:10 +010072my $firsttime = 99999;
Arjan van de Venaa5d9152008-09-13 09:36:06 -070073my $count = 0;
Frederic Weisbeckerddc7a012008-10-04 21:35:48 +020074my %pids;
Arjan van de Vend3f8dde2009-01-10 10:03:05 -080075my %pidctr;
Frederic Weisbeckerddc7a012008-10-04 21:35:48 +020076
Fabian Frederick67554fa2014-04-02 21:39:52 +020077my $headerstep = 20;
78my $xheader = 15;
79my $yheader = 25;
80my $cyheader = 0;
81
Arjan van de Venaa5d9152008-09-13 09:36:06 -070082while (<>) {
83 my $line = $_;
Michael Neuling0bb98e22009-02-15 10:20:30 +010084 if ($line =~ /([0-9\.]+)\] calling ([a-zA-Z0-9\_\.]+)\+/) {
Arjan van de Venaa5d9152008-09-13 09:36:06 -070085 my $func = $2;
86 if ($done == 0) {
87 $start{$func} = $1;
Arjan van de Vend3f8dde2009-01-10 10:03:05 -080088 $type{$func} = 0;
Arjan van de Ven80a398a2008-09-14 15:30:52 -070089 if ($1 < $firsttime) {
90 $firsttime = $1;
91 }
Arjan van de Venaa5d9152008-09-13 09:36:06 -070092 }
Arjan van de Venaa5d9152008-09-13 09:36:06 -070093 if ($line =~ /\@ ([0-9]+)/) {
Frederic Weisbeckerddc7a012008-10-04 21:35:48 +020094 $pids{$func} = $1;
Arjan van de Venaa5d9152008-09-13 09:36:06 -070095 }
96 $count = $count + 1;
97 }
98
Arjan van de Vend3f8dde2009-01-10 10:03:05 -080099 if ($line =~ /([0-9\.]+)\] async_waiting @ ([0-9]+)/) {
100 my $pid = $2;
101 my $func;
102 if (!defined($pidctr{$pid})) {
103 $func = "wait_" . $pid . "_1";
104 $pidctr{$pid} = 1;
105 } else {
106 $pidctr{$pid} = $pidctr{$pid} + 1;
107 $func = "wait_" . $pid . "_" . $pidctr{$pid};
108 }
109 if ($done == 0) {
110 $start{$func} = $1;
111 $type{$func} = 1;
112 if ($1 < $firsttime) {
113 $firsttime = $1;
114 }
115 }
116 $pids{$func} = $pid;
117 $count = $count + 1;
118 }
119
Michael Neuling0bb98e22009-02-15 10:20:30 +0100120 if ($line =~ /([0-9\.]+)\] initcall ([a-zA-Z0-9\_\.]+)\+.*returned/) {
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700121 if ($done == 0) {
122 $end{$2} = $1;
123 $maxtime = $1;
124 }
125 }
Arjan van de Vend3f8dde2009-01-10 10:03:05 -0800126
127 if ($line =~ /([0-9\.]+)\] async_continuing @ ([0-9]+)/) {
128 my $pid = $2;
129 my $func = "wait_" . $pid . "_" . $pidctr{$pid};
130 $end{$func} = $1;
131 $maxtime = $1;
132 }
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700133 if ($line =~ /Write protecting the/) {
134 $done = 1;
135 }
Arjan van de Ven80a398a2008-09-14 15:30:52 -0700136 if ($line =~ /Freeing unused kernel memory/) {
137 $done = 1;
138 }
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700139}
140
141if ($count == 0) {
Stephen Hemmingerd1aaf8c2008-11-13 08:33:00 -0800142 print STDERR <<END;
143No data found in the dmesg. Make sure that 'printk.time=1' and
144'initcall_debug' are passed on the kernel command line.
Stephen Hemmingerd1aaf8c2008-11-13 08:33:00 -0800145END
Fabian Frederick67554fa2014-04-02 21:39:52 +0200146 help(1);
Stephen Hemmingerd1aaf8c2008-11-13 08:33:00 -0800147 exit 1;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700148}
149
150print "<?xml version=\"1.0\" standalone=\"no\"?> \n";
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800151print "<svg width=\"2000\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n";
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700152
Fabian Frederick67554fa2014-04-02 21:39:52 +0200153
154if ($header) {
155 my $version = `uname -a`;
156 my $date = `date`;
157 print "<text transform=\"translate($xheader,$yheader)\">Kernel version: $version</text>\n";
158 $cyheader = $yheader+$headerstep;
159 print "<text transform=\"translate($xheader,$cyheader)\">Date: $date</text>\n";
160}
161
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700162my @styles;
163
164$styles[0] = "fill:rgb(0,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
165$styles[1] = "fill:rgb(0,255,0);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
166$styles[2] = "fill:rgb(255,0,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
167$styles[3] = "fill:rgb(255,255,20);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
168$styles[4] = "fill:rgb(255,0,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
169$styles[5] = "fill:rgb(0,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
170$styles[6] = "fill:rgb(0,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
171$styles[7] = "fill:rgb(0,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
172$styles[8] = "fill:rgb(255,0,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
173$styles[9] = "fill:rgb(255,255,128);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
174$styles[10] = "fill:rgb(255,128,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
175$styles[11] = "fill:rgb(128,255,255);fill-opacity:0.5;stroke-width:1;stroke:rgb(0,0,0)";
176
Arjan van de Vend3f8dde2009-01-10 10:03:05 -0800177my $style_wait = "fill:rgb(128,128,128);fill-opacity:0.5;stroke-width:0;stroke:rgb(0,0,0)";
178
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800179my $mult = 1950.0 / ($maxtime - $firsttime);
180my $threshold2 = ($maxtime - $firsttime) / 120.0;
181my $threshold = $threshold2/10;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700182my $stylecounter = 0;
Frederic Weisbeckerddc7a012008-10-04 21:35:48 +0200183my %rows;
184my $rowscount = 1;
Alan Jenkins06d1cd22008-10-14 14:19:15 +0100185my @initcalls = sort { $start{$a} <=> $start{$b} } keys(%start);
Stephen Hemminger68f96c02008-11-12 10:21:01 -0800186
187foreach my $key (@initcalls) {
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700188 my $duration = $end{$key} - $start{$key};
189
190 if ($duration >= $threshold) {
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800191 my ($s, $s2, $s3, $e, $w, $y, $y2, $style);
Alan Jenkins2a813f82008-10-14 14:18:07 +0100192 my $pid = $pids{$key};
Frederic Weisbecker07d18902008-10-04 21:35:48 +0200193
194 if (!defined($rows{$pid})) {
195 $rows{$pid} = $rowscount;
196 $rowscount = $rowscount + 1;
197 }
Alan Jenkins06d1cd22008-10-14 14:19:15 +0100198 $s = ($start{$key} - $firsttime) * $mult;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700199 $s2 = $s + 6;
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800200 $s3 = $s + 1;
Arjan van de Ven80a398a2008-09-14 15:30:52 -0700201 $e = ($end{$key} - $firsttime) * $mult;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700202 $w = $e - $s;
203
Frederic Weisbeckerddc7a012008-10-04 21:35:48 +0200204 $y = $rows{$pid} * 150;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700205 $y2 = $y + 4;
206
207 $style = $styles[$stylecounter];
208 $stylecounter = $stylecounter + 1;
209 if ($stylecounter > 11) {
210 $stylecounter = 0;
211 };
212
Arjan van de Vend3f8dde2009-01-10 10:03:05 -0800213 if ($type{$key} == 1) {
214 $y = $y + 15;
215 print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"115\" style=\"$style_wait\"/>\n";
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800216 } else {
Arjan van de Vend3f8dde2009-01-10 10:03:05 -0800217 print "<rect x=\"$s\" width=\"$w\" y=\"$y\" height=\"145\" style=\"$style\"/>\n";
218 if ($duration >= $threshold2) {
219 print "<text transform=\"translate($s2,$y2) rotate(90)\">$key</text>\n";
220 } else {
221 print "<text transform=\"translate($s3,$y2) rotate(90)\" font-size=\"3pt\">$key</text>\n";
222 }
Arjan van de Ven40c8c852009-01-04 07:16:38 -0800223 }
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700224 }
225}
226
227
228# print the time line on top
Arjan van de Ven80a398a2008-09-14 15:30:52 -0700229my $time = $firsttime;
230my $step = ($maxtime - $firsttime) / 15;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700231while ($time < $maxtime) {
Alan Jenkins2a813f82008-10-14 14:18:07 +0100232 my $s3 = ($time - $firsttime) * $mult;
Arjan van de Ven80a398a2008-09-14 15:30:52 -0700233 my $tm = int($time * 100) / 100.0;
Alan Jenkins2a813f82008-10-14 14:18:07 +0100234 print "<text transform=\"translate($s3,89) rotate(90)\">$tm</text>\n";
Arjan van de Ven80a398a2008-09-14 15:30:52 -0700235 $time = $time + $step;
Arjan van de Venaa5d9152008-09-13 09:36:06 -0700236}
237
238print "</svg>\n";