tizen 2.3.1 release
[framework/web/mobile/wrt.git] / src / profiling / script / utils / analyse.pl
1 #!/usr/bin/env perl
2 # Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License");
5 #    you may not use this file except in compliance with the License.
6 #    You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS,
12 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #    See the License for the specific language governing permissions and
14 #    limitations under the License.
15 #
16
17 use strict;
18 use warnings;
19 use diagnostics;
20
21 my ($regular_out_file, $range_out_file) = @ARGV;
22
23 print "Script requires two arguments,
24 which are file names to store the output\n
25 Regular points will be stored in first file,
26 ranges in second one.\n" and exit unless (defined $regular_out_file and defined $range_out_file);
27
28
29 print "Starting analysis\n";
30 my @lines = <STDIN>;
31
32 my @packets;
33 foreach my $line (@lines)
34 {
35     chomp($line);
36     my @splitted = split(/#/, $line);
37     my $prefix = $splitted[0];
38     my $name = $splitted[1];
39     my $time = $splitted[2];
40     my $description = $splitted[3];
41     my %packet;
42     $packet{time} = $time;
43     $packet{prefix} = $prefix;
44     $packet{name} = $name;
45     $packet{description} = $description;
46     push @packets, \%packet;
47 }
48
49 @packets = sort { $$a{time} <=> $$b{time} } @packets;
50
51 my $starttime;
52 my %tmps;
53 my @ranges;
54
55 foreach my $packet (@packets)
56 {
57     my $name = $$packet{name};
58     my $prefix = $$packet{prefix};
59
60     if (not defined $starttime)
61     {
62         if (defined $name and $name eq "Profiling_Started")
63         {
64             $starttime = $$packet{time};
65         }
66         else
67         {
68             print "***** No Profiling Start Point Found *****\n";
69             exit();
70         }
71     }
72     $$packet{time} = $$packet{time} - $starttime;
73
74     if ($prefix eq "start")
75     {
76         if (exists($tmps{$name}))
77         {
78             print "***** Doubled start point found $name *****\n";
79             print "[$name]\n";
80             exit();
81         }
82         $tmps{$name} = \%$packet;
83     }
84     elsif ($prefix eq "stop")
85     {
86         if (not exists($tmps{$name}))
87         {
88             print "***** No start point found *****\n";
89             exit();
90         }
91         my %tmp = %{$tmps{$name}};
92         my %newpacket = %$packet;
93         $newpacket{time} = $newpacket{time} - $tmp{time};
94         delete ($tmps{$name});
95         my $found;
96         foreach my $p (@ranges)
97         {
98             if ($$p{name} eq $newpacket{name})
99             {
100                 $$p{time} += $newpacket{time};
101                 $found = 1;
102             }
103         }
104         if (not defined($found))
105         {
106             push(@ranges, \%newpacket);
107         }
108     }
109 }
110
111 open REGULAR_OUT_FILE, ">", $regular_out_file or die $!;
112
113 print REGULAR_OUT_FILE "name,prefix,timestamp[µs],timespan[µs]\n";
114 my $lastTime;
115 foreach my $point (@packets)
116 {
117     if (not defined $lastTime)
118     {
119         $lastTime = $$point{time};
120     }
121     my $toPrint = sprintf("%s-%s,%lu,%lu\n", $$point{name}, $$point{prefix}, $$point{time}, $$point{time} - $lastTime);
122     print REGULAR_OUT_FILE $toPrint;
123     $lastTime = $$point{time};
124 }
125 close REGULAR_OUT_FILE;
126
127 open RANGE_OUT_FILE, ">", $range_out_file or die $!;
128 print RANGE_OUT_FILE "range name,timespan[µs]\n";
129 foreach my $range (@ranges)
130 {
131     my $res = sprintf("%s,%lu\n", $$range{name}, $$range{time});
132     print RANGE_OUT_FILE $res;
133 }
134 close RANGE_OUT_FILE;
135