-
Notifications
You must be signed in to change notification settings - Fork 4
/
evtchart.pl
executable file
·304 lines (274 loc) · 10.8 KB
/
evtchart.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/perl -w
#
# evtchart.pl Convert an evt trace log into a SVG image (Gantt chart).
#
# Input data has the format:
#
# TIME(ns) EVENT ID ID_NAME PARENT_ID EXTRAS
#
# EVENT describes the event type, and is one of the following strings:
#
# start beginning of the event
# end end of the event
# change event changed; eg, new ID name
#
# Eg, for process event tracing the fields can be:
#
# EVENT = "start", for fork()
# EVENT = "change", for exec()
# EVENT = "end", for exit()
# ID = PID
# ID_NAME = execname
# PARENT_ID = PPID
#
# SEE ALSO:
# procevt.d: a DTrace script to trace process execution as an evt log.
#
# HISTORY
#
# Gantt charts have been used for visualizing process execution for many years.
# See the Linux boot chart project: # http://www.bootchart.org/
# and the DTrace Python boot chart:
# http://alexeremin.blogspot.com/2009/01/boot-chart-with-help-of-dtrace-and.html
# both of which inspired this work.
#
# Copyright 2012 Joyent, Inc. All rights reserved.
# Copyright 2012 Brendan Gregg. All rights reserved.
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# 10-Sep-2011 Brendan Gregg Created this.
use strict;
use POSIX qw(ceil);
$| = 1;
# tunables
my $outfile = "chart.svg";
my $fonttype = "Verdana";
my $imagewidth = 900; # max width, pixels
my $eventheight = 13; # height is dynamic
my $dotrunc = 1; # compress short events
# internals
my $fontsize = 8; # base text size
my $ypad = $fontsize * 2 + 30; # pad top, include title and labels
my $xpadl = 10; # pad left, include arrows
my $xpadr = 50; # pad right, include titles
my $timezero = "";
my $timemax = 0;
my %Data;
my %Events;
# SVG functions
{ package SVG;
sub new {
my $class = shift;
my $self = {};
bless ($self, $class);
return $self;
}
sub header {
my ($self, $w, $h) = @_;
$self->{svg} .= <<SVG;
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" width="$w" height="$h" onload="init(evt)" viewBox="0 0 $w $h" xmlns="http://www.w3.org/2000/svg" >
SVG
}
sub defs {
my $self = shift;
$self->{svg} .= <<SVG;
<defs >
<linearGradient id="background" >
<stop stop-color="#C5D5A9" offset="5%" />
<stop stop-color="#eeeeee" offset="95%" />
</linearGradient>
</defs>
SVG
}
sub colorAllocate {
my ($self, $r, $g, $b) = @_;
return "rgb($r,$g,$b)";
}
sub filledRectangle {
my ($self, $x1, $y1, $x2, $y2, $fill, $stroke, $r) = @_;
my $w = $x2 - $x1;
my $h = $y2 - $y1;
$r = defined $r ? $r : 0;
$self->{svg} .= qq/<rect x="$x1" y="$y1" width="$w" height="$h" fill="$fill" stroke="$stroke" rx="$r" ry="$r" \/>\n/;
}
sub line {
my ($self, $x1, $y1, $x2, $y2, $line) = @_;
$self->{svg} .= qq/<line x1="$x1" y1="$y1" x2="$x2" y2="$y2" stroke="$line" stroke-width="1" \/>\n/;
}
sub stringTTF {
my ($self, $color, $font, $size, $angle, $x, $y, $str) = @_;
$self->{svg} .= qq/<text text-anchor="left" x="$x" y="$y" font-size="$size" font-family="$font" fill="$color" >$str<\/text>\n/;
}
sub setStyle {
}
sub svg {
my $self = shift;
$self->{svg} .= "</svg>\n";
return $self->{svg};
}
1;
}
print "Timestamp sorting...\n";
while (<>) {
chomp;
my ($time, $data) = split(' ', $_, 2);
next unless defined $time and $time =~ /^\d+$/;
while (defined $Data{$time}) {
# time(ns) should generally be unique; tweak when not
$time += 0.01;
}
$Data{$time} = $data;
}
# Main
print "Parsing...\n";
foreach my $time (sort { $a <=> $b } keys %Data) {
$timezero = $time if $timezero eq "";
$timemax = $time if $time > $timemax;
# TIME(ns) EVENT ID ID_NAME PARENT_ID EXTRAS
my ($event, $id, $name, $parentid, $rest) = split ' ', $Data{$time};
# change events can rename
$Events{$id}->{name} = $name if $event eq "change";
# first parent takes precedent (birth)
$Events{$id}->{parent} = $parentid unless
defined $Events{$id}->{parent};
if ($event eq "start") {
$Events{$id}->{stime} = $time;
} elsif ($event eq "change" and !defined $Events{$id}->{stime}) {
# This should have been seen by now (time ordered),
# unless it began before tracing.
$Events{$id}->{stime} = $timezero;
} elsif ($event eq "end") {
$Events{$id}->{etime} = $time;
unless (defined $Events{$id}->{stime}) {
$Events{$id}->{stime} = $timezero;
}
}
}
my $secmax = ceil(($timemax - $timezero) / 1000000000);
my $widthpersec = (($imagewidth - $xpadl - $xpadr)) / $secmax;
my $widthperns = $widthpersec / 1000000000;
print "Calculating height...\n";
my $y1;
my $y2 = $ypad;
my $lastx = 0;
my $toosmallpx = 4;
my $toorecentpx = 8;
foreach my $id (sort { $Events{$a}->{stime} <=> $Events{$b}->{stime} } keys %Events) {
my $x1 = $xpadl + int($widthperns * ($Events{$id}->{stime} - $timezero));
my $x2 = defined $Events{$id}->{etime} ? $xpadl +
int($widthperns * ($Events{$id}->{etime} - $timezero)) : $imagewidth + 10;
# truncate event if short duration and near last event, by making its
# height 1 pixel and dropping the text. this compresses shell script loops.
my $height = $eventheight;
if ($dotrunc && (($x2 - $x1) < $toosmallpx && ($x2 - $lastx) < $toorecentpx)) {
$height = 1;
$Events{$id}->{trunc} = 1;
$lastx = $x2;
$x2++;
} else {
$lastx = $x2;
}
$y1 = $y2;
$y2 = $y1 + $height;
$Events{$id}->{x1} = $x1;
$Events{$id}->{y1} = $y1;
$Events{$id}->{x2} = $x2;
$Events{$id}->{y2} = $y2;
}
print "Creating canvas...\n";
my $events = scalar keys %Events;
my $imageheight = $y2 + 2;
my $im = SVG->new();
$im->header($imagewidth, $imageheight);
my ($white, $black, $vvdgrey, $vdgrey, $dgrey, $grey, $lgrey, $red, $green, $blue) = (
$im->colorAllocate(255, 255, 255),
$im->colorAllocate(0, 0, 0),
$im->colorAllocate(40, 40, 40),
$im->colorAllocate(160, 160, 160),
$im->colorAllocate(200, 200, 200),
$im->colorAllocate(225, 225, 225),
$im->colorAllocate(235, 235, 235),
$im->colorAllocate(240, 180, 180),
$im->colorAllocate(160, 220, 160),
$im->colorAllocate(180, 180, 240)
);
$im->stringTTF($black, $fonttype, $fontsize + 3, 0.0, 20, $fontsize + 6, "Event Time Chart") || die $@;
print "Drawing grid...\n";
for (my $i = 0; $i <= $secmax; $i++) {
# Draw dark grey lines every 5 secs.
# Optionally, draw grey lines every 1 and light grey lines every 0.1 sec;
# these only appear if there is more than 10 pixels per target interval.
my $columnspace = 10;
my $x = $xpadl + int($i * $widthpersec);
if (($i % 5) == 0) {
$im->line($x, $ypad - 5, $x, $imageheight, $dgrey);
} elsif ($widthpersec > $columnspace) {
$im->line($x, $ypad - 5, $x, $imageheight, $grey);
}
if ($widthpersec > $columnspace * 10) {
for (my $j = 1; $j < 10; $j++) {
my $xj = $x + int($j * $widthpersec / 10);
$im->line($xj, $ypad - 3, $xj, $imageheight, $lgrey);
}
}
# Draw second labels on 5 sec lines, and 1 sec lines if 0.1 is present.
if (($i % 5) == 0 || $widthpersec > $columnspace * 10) {
$im->stringTTF($vvdgrey, $fonttype, $fontsize, 0.0, $x - 6, $ypad - 10,
"${i}s") || die $@;
}
}
print "Drawing events...\n";
$y2 = $ypad;
$lastx = 0;
foreach my $id (sort { $Events{$a}->{stime} <=> $Events{$b}->{stime} } keys %Events) {
my $x1 = $Events{$id}->{x1};
my $y1 = $Events{$id}->{y1};
my $x2 = $Events{$id}->{x2};
my $y2 = $Events{$id}->{y2};
# draw box and text
my $border = defined $Events{$id}->{trunc} ? "$dgrey" : "$vdgrey";
$im->filledRectangle($x1, $y1, $x2, $y2, $lgrey, $border, 4);
unless (defined $Events{$id}->{trunc}) {
my $name = defined $Events{$id}->{name} ? $Events{$id}->{name} : "";
$im->stringTTF($vvdgrey, $fonttype, $fontsize, 0.0, $x1 + 5, $y1 + 2 + $fontsize, $name) || die $@;
}
}
print "Drawing ancestory...\n";
$im->setStyle($blue, $blue, "transparent", "transparent");
foreach my $id (sort { $Events{$a}->{stime} <=> $Events{$b}->{stime} } keys %Events) {
next if defined $Events{$id}->{trunc};
next unless defined $Events{$id}->{parent};
my $p_id = $Events{$id}->{parent};
next unless defined $Events{$p_id};
my $y1 = $Events{$p_id}->{y1} + $eventheight / 2;
my $y2 = $Events{$id}->{y1} + $eventheight / 2;
my $x1 = $Events{$p_id}->{x1};
my $x2 = $Events{$id}->{x1};
$im->line($x1 - 5, $y1, $x1 - 1, $y1, $blue);
$im->line($x1 - 5, $y2, $x2 - 1, $y2, $blue);
$im->line($x1 - 5, $y1, $x1 - 5, $y2, $blue);
}
print "Writing $outfile...\n";
open FILE, ">$outfile" or die "writing $outfile: $!";
print FILE $im->svg;
close FILE;