Debian Codegraph @

scripts/plot_histogram.pl

raw
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2024 Linutronix GmbH
4 # Author Kurt Kanzenbach <[email protected]>
5 #
6 # SPDX-License-Identifier: BSD-2-Clause
7 #
8
9 use strict;
10 use warnings;
11
12 my ($hist, $min, $max);
13 my ($tsn_high, $tsn_low, $rtc, $rta, $dcp, $lldp, $udp_high, $udp_low, $g2);
14
15 sub write_data_file
16 {
17 my ($file) = @_;
18 my ($fh, $line, @lines, $rtt);
19
20 $tsn_high = $tsn_low = $rtc = $rta = $dcp = $lldp = $udp_high = $udp_low = $g2 = 0;
21 $max = 0;
22 $min = 1e9;
23 open($fh, "<", $file) or die("Failed to open file '$file': $!");
24 while ($line = <$fh>) {
25 my ($c1, $c2, $c3, $c4, $c5, $c6, $c7, $c8, $c9);
26
27 push(@lines, $line) if $line =~ /^\d+:/;
28
29 if (($rtt) = $line =~ /^(\d+):/) {
30 $max = $rtt if $rtt > $max;
31 $min = $rtt if $rtt < $min;
32 }
33
34 if (($c1, $c2, $c3, $c4, $c5, $c6, $c7, $c8, $c9) =
35 $line =~ /^\d+:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
36 $tsn_high = 1 if $c1 != 0;
37 $tsn_low = 1 if $c2 != 0;
38 $rtc = 1 if $c3 != 0;
39 $rta = 1 if $c4 != 0;
40 $dcp = 1 if $c5 != 0;
41 $lldp = 1 if $c6 != 0;
42 $udp_high = 1 if $c7 != 0;
43 $udp_low = 1 if $c8 != 0;
44 $g2 = 1 if $c9 != 0;
45 }
46 }
47 close($fh);
48
49 open($fh, ">", "data.txt") or die;
50 print $fh $_ for @lines;
51 close($fh);
52 }
53
54 sub plot
55 {
56 my ($file) = @_;
57 my ($fh);
58
59 $file =~ s/\.txt$//;
60 $file .= ".png";
61
62 $min =~ s/^0+//;
63 $max =~ s/^0+//;
64
65 $min -= 100;
66 $max += 100;
67
68 open($fh, ">", "plot.txt");
69 print $fh "set terminal png size 1920,1080 enhanced font 'sans-serif,20'\n";
70 print $fh "set output '$file';\n";
71 print $fh "set title 'Testbench latency plot';\n";
72 print $fh "set xlabel 'Latency [us]';\n";
73 print $fh "set ylabel 'Number of latency samples';\n";
74 print $fh "set ytics right;\n";
75 print $fh "set logscale y;\n";
76 print $fh "set xrange [$min:$max];\n";
77 print $fh "set yrange [0.8:*];\n";
78 print $fh "set grid back;\n";
79 print $fh "plot ";
80 print $fh "'data.txt' using 1:2 with fsteps lw 3 title 'TsnHigh', " if $tsn_high;
81 print $fh "'data.txt' using 1:3 with fsteps lw 3 title 'TsnLow', " if $tsn_low;
82 print $fh "'data.txt' using 1:4 with fsteps lw 3 title 'Rtc', " if $rtc;
83 print $fh "'data.txt' using 1:5 with fsteps lw 3 title 'Rta', " if $rta;
84 print $fh "'data.txt' using 1:6 with fsteps lw 3 title 'Dcp', " if $dcp;
85 print $fh "'data.txt' using 1:7 with fsteps lw 3 title 'Lldp', " if $lldp;
86 print $fh "'data.txt' using 1:8 with fsteps lw 3 title 'UdpHigh', " if $udp_high;
87 print $fh "'data.txt' using 1:9 with fsteps lw 3 title 'UdpLow', " if $udp_low;
88 print $fh "'data.txt' using 1:10 with fsteps lw 3 title 'GenericL2', " if $g2;
89 print $fh ";\n";
90 close($fh);
91
92 `gnuplot plot.txt`;
93 die("Gnuplot failed!") if $?;
94 }
95
96 sub cleanup
97 {
98 `rm -f data.txt`;
99 `rm -f plot.txt`;
100 }
101
102 sub print_usage
103 {
104 select(STDERR);
105 print "usage: plot_histogram.pl <histogram_file>\n";
106 exit 0;
107 }
108
109 $hist = $ARGV[0];
110 print_usage unless $hist;
111
112 write_data_file($hist);
113 plot($hist);
114 cleanup;
115
116 exit 0;