Fix: tests: stderr not correctly redirected
[lttng-tools.git] / tests / utils / babelstats.pl
CommitLineData
d8865498
CB
1#!/usr/bin/perl
2
3# Copyright (C) - 2012 Christian Babeux <christian.babeux@efficios.com>
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License, version 2 only, as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12# more details.
13#
14# You should have received a copy of the GNU General Public License along with
15# this program; if not, write to the Free Software Foundation, Inc., 51
16# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18use strict;
19use warnings;
20
21use Getopt::Long;
22
23my $opt_tracepoint;
24
25GetOptions('tracepoint=s' => \$opt_tracepoint)
26 or die("Invalid command-line option\n");
27
28defined($opt_tracepoint)
29 or die("Missing tracepoint, use --tracepoint <name>");
30
31# Parse an array string.
32# The format is as follow: [ [index] = value, ... ]
33sub parse_array
34{
35 my ($arr_str) = @_;
36 my @array = ();
37
38 # Strip leading and ending brackets, remove whitespace
39 $arr_str =~ s/^\[//;
40 $arr_str =~ s/\]$//;
41 $arr_str =~ s/\s//g;
42
43 my @entries = split(',', $arr_str);
44
45 foreach my $entry (@entries) {
46 if ($entry =~ /^\[(\d+)\]=(\d+)$/) {
47 my $index = $1;
48 my $value = $2;
49 splice @array, $index, 0, $value;
50 }
51 }
52
53 return \@array;
54}
55
56# Parse fields values.
57# Format can either be a name = array or a name = value pair.
58sub parse_fields
59{
60 my ($fields_str) = @_;
61 my %fields_hash;
62
63 my $field_name = '[\w\d_]+';
64 my $field_value = '[\w\d_\\\*"]+';
65 my $array = '\[(?:\s\[\d+\]\s=\s\d+,)*\s\[\d+\]\s=\s\d+\s\]';
66
67 # Split the various fields
68 my @fields = ($fields_str =~ /$field_name\s=\s(?:$array|$field_value)/g);
69
70 foreach my $field (@fields) {
71 if ($field =~ /($field_name)\s=\s($array)/) {
72 my $name = $1;
73 my $value = parse_array($2);
74 $fields_hash{$name} = $value;
75 }
76
77 if ($field =~ /($field_name)\s=\s($field_value)/) {
78 my $name = $1;
79 my $value = $2;
80 $fields_hash{$name} = $value;
81 }
82 }
83
84 return \%fields_hash;
85}
86
87# Using an event array, merge all the fields
88# of a particular tracepoint.
89sub merge_fields
90{
91 my ($events_ref) = @_;
92 my %merged;
93
94 foreach my $event (@{$events_ref}) {
95 my $tp_provider = $event->{'tp_provider'};
96 my $tp_name = $event->{'tp_name'};
97 my $tracepoint = "$tp_provider:$tp_name";
98
99 foreach my $key (keys %{$event->{'fields'}}) {
100 my $val = $event->{'fields'}->{$key};
101
102 # TODO: Merge of array is not implemented.
103 next if (ref($val) eq 'ARRAY');
104 $merged{$tracepoint}{$key}{$val} = undef;
105 }
106 }
107
108 return \%merged;
109}
110
111# Print the minimum and maximum of each fields
112# for a particular tracepoint.
113sub print_fields_stats
114{
115 my ($merged_ref, $tracepoint) = @_;
116
117 return unless ($tracepoint && exists $merged_ref->{$tracepoint});
118
119 foreach my $field (keys %{$merged_ref->{$tracepoint}}) {
120 my @sorted;
d9c3a893 121 my @val = keys %{$merged_ref->{$tracepoint}->{$field}};
d8865498
CB
122
123 if ($val[0] =~ /^\d+$/) {
124 # Sort numerically
125 @sorted = sort { $a <=> $b } @val;
126 } elsif ($val[0] =~ /^0x[\da-f]+$/i) {
127 # Convert the hex values and sort numerically
128 @sorted = sort { hex($a) <=> hex($b) } @val;
129 } else {
130 # Fallback, alphabetical sort
131 @sorted = sort { lc($a) cmp lc($b) } @val;
132 }
133
134 my $min = $sorted[0];
135 my $max = $sorted[-1];
136
137 print "$field $min $max\n";
138 }
139}
140
141my @events;
142
143while (<>)
144{
145 my $timestamp = '\[(.*)\]';
146 my $elapsed = '\((.*)\)';
147 my $hostname = '.*';
148 my $pname = '.*';
815535ae 149 my $pinfo = '.*';
d8865498
CB
150 my $pid = '\d+';
151 my $tp_provider = '.*';
152 my $tp_name = '.*';
153 my $cpu_info = '{\scpu_id\s=\s(\d+)\s\}';
154 my $fields = '{(.*)}';
155
156 # Parse babeltrace text output format
815535ae 157 if (/$timestamp\s$elapsed\s($pinfo)\s($tp_provider):($tp_name):\s$cpu_info,\s$fields/) {
d8865498 158 my %event_hash;
d8865498
CB
159 $event_hash{'timestamp'} = $1;
160 $event_hash{'elapsed'} = $2;
815535ae
CB
161 $event_hash{'pinfo'} = $3;
162
163# my @split_pinfo = split(':', $3);
164# $event_hash{'hostname'} = $split_pinfo[0];
165# $event_hash{'pname'} = defined($split_pinfo[1]) ? $split_pinfo[1] : undef;
166# $event_hash{'pid'} = defined($split_pinfo[2]) ? $split_pinfo[2] : undef;
167
168 $event_hash{'tp_provider'} = $4;
169 $event_hash{'tp_name'} = $5;
170 $event_hash{'cpu_id'} = $6;
171 $event_hash{'fields'} = parse_fields($7);
d8865498
CB
172
173 push @events, \%event_hash;
174 }
175}
176
177my %merged_fields = %{merge_fields(\@{events})};
178print_fields_stats(\%merged_fields, $opt_tracepoint);
This page took 0.0349 seconds and 4 git commands to generate.