First public contribution.
3 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 # This component and the accompanying materials are made available
6 # under the terms of "Eclipse Public License v1.0"
7 # which accompanies this distribution, and is available
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
10 # Initial Contributors:
11 # Nokia Corporation - initial contribution.
16 # This script parses trace data produced by OST from FBS, using the the FBSCLI,
17 # FBSERV and Symbian BTrace Hooks OST dictionaries, to produce a CSV output of
18 # the amount of FBS resources used per-thread over a user-definable time
19 # granularity, since the start of the trace.
21 # To use, enable SYMBIAN_KERNEL_THREAD_IDENTIFICATION trace group in Symbian
22 # BTrace Hooks OST dictionary, GRAPHICS_RESOURCE_MANAGEMENT_SEMANTICS in FBSERV
23 # OST dictionary, and GRAPHICS_RESOURCE_MANAGEMENT_SEMANTICS,
24 # GRAPHICS_RESOURCE_MANAGEMENT_FUNCTIONS and GRAPHICS_CONTROL_FUNCTIONS in
25 # FBSCLI OST dictionary. Once tracing is gathered, save trace output as ascii
26 # and run this script against it. The resulting file can then be imported into
27 # a spreadsheet application to be visually processed.
30 # Once the log time goes beyond midnight, snapshots will stop being taken.
35 # Sanity checking of the command line parameters...
36 if ($#ARGV == -1 || $ARGV[0] eq "help" || $ARGV[0] eq "/?")
38 print "\nusage: $0 filename [-h]\n";
40 print " -h : Specifies the heartbeat in millisecs (default=10000)\n";
45 ## Modifiable constants...
46 my $CSV_DELIMITER = ',';
48 # Time after start to take first snapshot, in millisecs
49 my $firstHeartBeatTimeMS = 1000;
51 # Default heartbeat in millisecs if none specified.
52 my $heartBeatMS = 10000;
56 ## Internal structures...
58 my $heartBeatCount = 0;
59 my $nextHeartBeatMS = -1;
61 # Hash of FbsSessions to thread IDs.
62 my %SessionThreadMap = ();
64 # A hash of thread names to the fbs resource count.
65 my %fbsResourcesPerThread = ();
67 # Array of the above hashes, one hash per heartbeat.
70 # Hashes of thread and process names to IDs.
76 ## Command line options parsing...
77 ## First arg is assumed to be the filename.
82 if ($cma =~ m/-h(\d*)/)
88 print "Unrecognised parameter: $cma , ignoring...\n";
92 ## Read from the file.
93 ## Read the log into an array line by line.
94 my $TRACE_FILENAME = $ARGV[0];
95 open(INPUT_FILE, $TRACE_FILENAME) or die $!;
96 my @traceLines = <INPUT_FILE>;
100 ## Parse each line sequentially...
102 foreach my $line (@traceLines)
104 my $timeFromMidnightMS;
107 ## If this line is about a new process, make a note of the name and the
108 ## associated process id, so that FbsSessions can be mapped to their
111 if ($line =~ /^.*Thread:Process name assigned;NThread:(.*);DProcess:(.*);Name:(.*)$/i)
115 my $processName = $3;
116 $ProcessNames{$processId} = $processName ;
120 ## If this line is about a new process, make a note of the name and the
121 ## associated process id, so that FbsSessions can be mapped to their
122 ## thread by name when the csv is generated.
124 if (($line =~ /^.*Thread:Thread created;NThread:(.*);DProcess:(.*);Name:(.*)$/i) ||
125 ($line =~ /^.*Thread:Thread name assigned;NThread:(.*);DProcess:(.*);Name:(.*)$/i))
130 my $fullThreadName = $ProcessNames{$processId} . ":" . $threadName;
131 $ThreadNames{$threadId} = $fullThreadName;
135 ## Determine timestamp. If this time is beyond the heartbeat,
136 ## take a snapshot and
138 if ($line =~ /^(\d\d):(\d\d):(\d\d)\.(\d{3})/)
140 $timeFromMidnightMS = ((($1 * 3600) + ($2 * 60) + $3) * 1000) + $4;
141 # Set up the time for the first snapshot.
142 if ($nextHeartBeatMS == -1)
144 $nextHeartBeatMS = $timeFromMidnightMS + $firstHeartBeatTimeMS;
149 ## If heartbeat reached, take snapshot of bmp memory per thread
150 ## and set next heartbeat time.
152 while ($timeFromMidnightMS >= $nextHeartBeatMS)
154 $nextHeartBeatMS += $heartBeatMS;
155 # take a snapshot of the current bitmap memory usage per thread
156 while ((my $thread, my $fbsResourceCount) = each(%fbsResourcesPerThread))
158 $arrayOfSnapshots[$heartBeatCount]{$thread} = $fbsResourceCount;
163 ## FBS Client-side traces.
164 if ($line =~ m/\tFBSCLI: /)
167 ## If this line is an FBSCLI trace, and it contains iSSH then
168 ## it gives a chance to map a client thread ID to a session handle.
170 if ( $line =~ m/iSSH=(\w*);.*Thread ID:(.*)$/)
172 my $ServerSessionHandle = $1;
174 if ($thread ne "0x00000000")
176 $SessionThreadMap{$ServerSessionHandle} = $thread;
182 ## FBS Server-side traces.
184 if ($line =~ m/\tFBSERV: /)
186 ## The line must have a s= parameter to be useful - the session server handle.
187 ## Any FBSERV line without this is not considered for parsing.
188 if ($line =~ m/; iSSH=(\w*);/)
190 my $FbsSessionHandle = $1;
191 my $thread = "Unknown Thread [Session=$FbsSessionHandle]";
192 if (defined($SessionThreadMap{$FbsSessionHandle}))
194 $thread = $SessionThreadMap{$FbsSessionHandle};
196 if ($line =~ m/; rc=(\d+);/)
198 my $resourceCount = $1;
199 if ($resourceCount == 0)
203 $fbsResourcesPerThread{$thread} = $resourceCount;
213 ## Make a map of unique threads across all snapshots
214 ## This is so only one occurrence of each thread will appear
217 my %uniqueThreads = ();
218 for my $i (0..$#arrayOfSnapshots)
220 for my $thread (keys %{$arrayOfSnapshots[$i]})
222 $uniqueThreads{$thread} = 1;
227 ## Start writing to file.
228 ## First row, which contains the heartbeat number column headings...
230 my $OUTPUT_FILENAME = sprintf("%s.csv", $TRACE_FILENAME);
231 open(OUTPUT_FILE,">$OUTPUT_FILENAME") or die $!;
232 print OUTPUT_FILE "Session$CSV_DELIMITER";
233 for my $i (0..$heartBeatCount)
235 print OUTPUT_FILE "$i$CSV_DELIMITER";
239 ## For each subsequent row, print the first thread and the
240 ## memory at each snapshot...
242 print OUTPUT_FILE "\n";
243 while ((my $thread, my $dummy) = each(%uniqueThreads))
245 # Resolve the thread to its full name...
246 print OUTPUT_FILE "$thread";
247 if (defined($ThreadNames{$thread}) )
249 my $threadName = $ThreadNames{$thread};
250 print OUTPUT_FILE ":$threadName";
252 print OUTPUT_FILE "$CSV_DELIMITER";
254 # print the memory use per thread, for each snapshot...
255 for my $i (0..$#arrayOfSnapshots)
257 my %snapshot = %{$arrayOfSnapshots[$i]};
258 while ((my $snapshotThread, my $fbsResourceCount) = each(%snapshot))
260 if ($snapshotThread eq $thread)
262 print OUTPUT_FILE "$fbsResourceCount";
265 print OUTPUT_FILE "$CSV_DELIMITER";
267 print OUTPUT_FILE "\n";