sl@0: #!/usr/local/bin/perl sl@0: # sl@0: # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: # All rights reserved. sl@0: # This component and the accompanying materials are made available sl@0: # under the terms of "Eclipse Public License v1.0" sl@0: # which accompanies this distribution, and is available sl@0: # at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: # sl@0: # Initial Contributors: sl@0: # Nokia Corporation - initial contribution. sl@0: # sl@0: # Contributors: sl@0: # sl@0: # Description: sl@0: # This script parses trace data produced by OST from FBS, using the the FBSCLI, sl@0: # FBSERV and Symbian BTrace Hooks OST dictionaries, to produce a CSV output of sl@0: # the amount of FBS resources used per-thread over a user-definable time sl@0: # granularity, since the start of the trace. sl@0: # sl@0: # To use, enable SYMBIAN_KERNEL_THREAD_IDENTIFICATION trace group in Symbian sl@0: # BTrace Hooks OST dictionary, GRAPHICS_RESOURCE_MANAGEMENT_SEMANTICS in FBSERV sl@0: # OST dictionary, and GRAPHICS_RESOURCE_MANAGEMENT_SEMANTICS, sl@0: # GRAPHICS_RESOURCE_MANAGEMENT_FUNCTIONS and GRAPHICS_CONTROL_FUNCTIONS in sl@0: # FBSCLI OST dictionary. Once tracing is gathered, save trace output as ascii sl@0: # and run this script against it. The resulting file can then be imported into sl@0: # a spreadsheet application to be visually processed. sl@0: # sl@0: # KNOWN DEFECTS: sl@0: # Once the log time goes beyond midnight, snapshots will stop being taken. sl@0: # sl@0: sl@0: use strict; sl@0: sl@0: # Sanity checking of the command line parameters... sl@0: if ($#ARGV == -1 || $ARGV[0] eq "help" || $ARGV[0] eq "/?") sl@0: { sl@0: print "\nusage: $0 filename [-h]\n"; sl@0: print "where\n"; sl@0: print " -h : Specifies the heartbeat in millisecs (default=10000)\n"; sl@0: exit; sl@0: } sl@0: sl@0: sl@0: ## Modifiable constants... sl@0: my $CSV_DELIMITER = ','; sl@0: sl@0: # Time after start to take first snapshot, in millisecs sl@0: my $firstHeartBeatTimeMS = 1000; sl@0: sl@0: # Default heartbeat in millisecs if none specified. sl@0: my $heartBeatMS = 10000; sl@0: sl@0: sl@0: ## sl@0: ## Internal structures... sl@0: ## sl@0: my $heartBeatCount = 0; sl@0: my $nextHeartBeatMS = -1; sl@0: sl@0: # Hash of FbsSessions to thread IDs. sl@0: my %SessionThreadMap = (); sl@0: sl@0: # A hash of thread names to the fbs resource count. sl@0: my %fbsResourcesPerThread = (); sl@0: sl@0: # Array of the above hashes, one hash per heartbeat. sl@0: my @arrayOfSnapshots; sl@0: sl@0: # Hashes of thread and process names to IDs. sl@0: my %ThreadNames; sl@0: my %ProcessNames; sl@0: sl@0: sl@0: ## sl@0: ## Command line options parsing... sl@0: ## First arg is assumed to be the filename. sl@0: ## sl@0: for my $i (1..$#ARGV) sl@0: { sl@0: my $cma = $ARGV[$i]; sl@0: if ($cma =~ m/-h(\d*)/) sl@0: { sl@0: $heartBeatMS = $1; sl@0: } sl@0: else sl@0: { sl@0: print "Unrecognised parameter: $cma , ignoring...\n"; sl@0: } sl@0: } sl@0: sl@0: ## Read from the file. sl@0: ## Read the log into an array line by line. sl@0: my $TRACE_FILENAME = $ARGV[0]; sl@0: open(INPUT_FILE, $TRACE_FILENAME) or die $!; sl@0: my @traceLines = ; sl@0: sl@0: sl@0: ## sl@0: ## Parse each line sequentially... sl@0: ## sl@0: foreach my $line (@traceLines) sl@0: { sl@0: my $timeFromMidnightMS; sl@0: sl@0: ## sl@0: ## If this line is about a new process, make a note of the name and the sl@0: ## associated process id, so that FbsSessions can be mapped to their sl@0: ## thread by name. sl@0: ## sl@0: if ($line =~ /^.*Thread:Process name assigned;NThread:(.*);DProcess:(.*);Name:(.*)$/i) sl@0: { sl@0: my $threadId = $1; sl@0: my $processId = $2; sl@0: my $processName = $3; sl@0: $ProcessNames{$processId} = $processName ; sl@0: } sl@0: sl@0: ## sl@0: ## If this line is about a new process, make a note of the name and the sl@0: ## associated process id, so that FbsSessions can be mapped to their sl@0: ## thread by name when the csv is generated. sl@0: ## sl@0: if (($line =~ /^.*Thread:Thread created;NThread:(.*);DProcess:(.*);Name:(.*)$/i) || sl@0: ($line =~ /^.*Thread:Thread name assigned;NThread:(.*);DProcess:(.*);Name:(.*)$/i)) sl@0: { sl@0: my $threadId = $1; sl@0: my $processId = $2; sl@0: my $threadName = $3; sl@0: my $fullThreadName = $ProcessNames{$processId} . ":" . $threadName; sl@0: $ThreadNames{$threadId} = $fullThreadName; sl@0: } sl@0: sl@0: ## sl@0: ## Determine timestamp. If this time is beyond the heartbeat, sl@0: ## take a snapshot and sl@0: ## sl@0: if ($line =~ /^(\d\d):(\d\d):(\d\d)\.(\d{3})/) sl@0: { sl@0: $timeFromMidnightMS = ((($1 * 3600) + ($2 * 60) + $3) * 1000) + $4; sl@0: # Set up the time for the first snapshot. sl@0: if ($nextHeartBeatMS == -1) sl@0: { sl@0: $nextHeartBeatMS = $timeFromMidnightMS + $firstHeartBeatTimeMS; sl@0: } sl@0: } sl@0: sl@0: ## sl@0: ## If heartbeat reached, take snapshot of bmp memory per thread sl@0: ## and set next heartbeat time. sl@0: ## sl@0: while ($timeFromMidnightMS >= $nextHeartBeatMS) sl@0: { sl@0: $nextHeartBeatMS += $heartBeatMS; sl@0: # take a snapshot of the current bitmap memory usage per thread sl@0: while ((my $thread, my $fbsResourceCount) = each(%fbsResourcesPerThread)) sl@0: { sl@0: $arrayOfSnapshots[$heartBeatCount]{$thread} = $fbsResourceCount; sl@0: } sl@0: $heartBeatCount++; sl@0: } sl@0: sl@0: ## FBS Client-side traces. sl@0: if ($line =~ m/\tFBSCLI: /) sl@0: { sl@0: ## sl@0: ## If this line is an FBSCLI trace, and it contains iSSH then sl@0: ## it gives a chance to map a client thread ID to a session handle. sl@0: ## sl@0: if ( $line =~ m/iSSH=(\w*);.*Thread ID:(.*)$/) sl@0: { sl@0: my $ServerSessionHandle = $1; sl@0: my $thread = $2; sl@0: if ($thread ne "0x00000000") sl@0: { sl@0: $SessionThreadMap{$ServerSessionHandle} = $thread; sl@0: } sl@0: } sl@0: } sl@0: sl@0: ## sl@0: ## FBS Server-side traces. sl@0: ## sl@0: if ($line =~ m/\tFBSERV: /) sl@0: { sl@0: ## The line must have a s= parameter to be useful - the session server handle. sl@0: ## Any FBSERV line without this is not considered for parsing. sl@0: if ($line =~ m/; iSSH=(\w*);/) sl@0: { sl@0: my $FbsSessionHandle = $1; sl@0: my $thread = "Unknown Thread [Session=$FbsSessionHandle]"; sl@0: if (defined($SessionThreadMap{$FbsSessionHandle})) sl@0: { sl@0: $thread = $SessionThreadMap{$FbsSessionHandle}; sl@0: } sl@0: if ($line =~ m/; rc=(\d+);/) sl@0: { sl@0: my $resourceCount = $1; sl@0: if ($resourceCount == 0) sl@0: { sl@0: $resourceCount = ''; sl@0: } sl@0: $fbsResourcesPerThread{$thread} = $resourceCount; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: close (INPUT_FILE); sl@0: sl@0: sl@0: ## sl@0: ## Make a map of unique threads across all snapshots sl@0: ## This is so only one occurrence of each thread will appear sl@0: ## in the csv file. sl@0: ## sl@0: my %uniqueThreads = (); sl@0: for my $i (0..$#arrayOfSnapshots) sl@0: { sl@0: for my $thread (keys %{$arrayOfSnapshots[$i]}) sl@0: { sl@0: $uniqueThreads{$thread} = 1; sl@0: } sl@0: } sl@0: sl@0: ## sl@0: ## Start writing to file. sl@0: ## First row, which contains the heartbeat number column headings... sl@0: ## sl@0: my $OUTPUT_FILENAME = sprintf("%s.csv", $TRACE_FILENAME); sl@0: open(OUTPUT_FILE,">$OUTPUT_FILENAME") or die $!; sl@0: print OUTPUT_FILE "Session$CSV_DELIMITER"; sl@0: for my $i (0..$heartBeatCount) sl@0: { sl@0: print OUTPUT_FILE "$i$CSV_DELIMITER"; sl@0: } sl@0: sl@0: ## sl@0: ## For each subsequent row, print the first thread and the sl@0: ## memory at each snapshot... sl@0: ## sl@0: print OUTPUT_FILE "\n"; sl@0: while ((my $thread, my $dummy) = each(%uniqueThreads)) sl@0: { sl@0: # Resolve the thread to its full name... sl@0: print OUTPUT_FILE "$thread"; sl@0: if (defined($ThreadNames{$thread}) ) sl@0: { sl@0: my $threadName = $ThreadNames{$thread}; sl@0: print OUTPUT_FILE ":$threadName"; sl@0: } sl@0: print OUTPUT_FILE "$CSV_DELIMITER"; sl@0: sl@0: # print the memory use per thread, for each snapshot... sl@0: for my $i (0..$#arrayOfSnapshots) sl@0: { sl@0: my %snapshot = %{$arrayOfSnapshots[$i]}; sl@0: while ((my $snapshotThread, my $fbsResourceCount) = each(%snapshot)) sl@0: { sl@0: if ($snapshotThread eq $thread) sl@0: { sl@0: print OUTPUT_FILE "$fbsResourceCount"; sl@0: } sl@0: } sl@0: print OUTPUT_FILE "$CSV_DELIMITER"; sl@0: } sl@0: print OUTPUT_FILE "\n"; sl@0: } sl@0: close (OUTPUT_FILE); sl@0: