Make diffprofile (a) python (b) less retarded.

Signed-off-by: Martin J. Bligh <mbligh@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@599 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/tools/diffprofile b/client/tools/diffprofile
index 73f6bfd..6bf2b6b 100755
--- a/client/tools/diffprofile
+++ b/client/tools/diffprofile
@@ -1,28 +1,71 @@
-#!/usr/bin/perl
-#
-# Martin J. Bligh (mbligh@aracnet.com)
+#!/usr/bin/python
+# Copyright Martin J. Bligh (mbligh@google.com)
+# Released under the GPL, v2
 
-$mindiff = 5;
+import os, sys, re
 
-open (FILE1, shift(@ARGV)) || die;
-open (FILE2, shift(@ARGV)) || die;
+results_per_sign = 10
 
-while (<FILE2>) {
-	/\s*(\d*)\s*(\S*)/;
-	$new{$2} = $1;
-	$diff{$2} = $1;
-}
-while (<FILE1>) {
-	/\s*(\d*)\s*(\S*)/;
-	$old{$2} = $1;
-	$diff{$2} -= $1;
-}
-foreach $key (sort bydiff (keys %diff)) {
-	$pct = 0;
-	$pct = 100 * ($diff{$key} / $old{$key}) if ($old{$key} > 0);
-	if ( ($diff{$key} > $mindiff) || ($diff{$key} < 0 - $mindiff) ) {
-		printf "%10s  %6.1f%% %s\n", $diff{$key}, $pct, $key;
-	}
-}
+def parse_lines(filename):
+	results = []
+	start_key = 1
+	for line in open(filename).readlines():
+		try:
+			a = line.split()
+			key = ' '.join(a[start_key:])
+			count = int(a[0])
+			results.append((key, count))
+		except:		# presumably a header line
+			if re.match(r'samples\s*%\s*app name\s*symbol name', line):
+				start_key = 2
+			elif re.match(r'samples\s*%\s*image name\s*app name\s*symbol name', line):
+				start_key = 3
+	return results
 
-sub bydiff { $diff{$b} <=> $diff{$a}; }
+
+# Firstly, suck in both files.
+orig = {}
+new = {}
+diff = {}
+
+for (key, count) in parse_lines(sys.argv[1]):
+	# Oprofile seems to be ... erm ... broken. Keys can appear > once ;-(
+	if orig.has_key(key):
+		orig[key] += count
+	else:
+		orig[key] = count
+	if diff.has_key(key):
+		diff[key] -= count
+	else:
+		diff[key] = -count
+
+for (key, count) in parse_lines(sys.argv[2]):
+	if new.has_key(key):
+		new[key] += count
+	else:
+		new[key] = count
+	if diff.has_key(key):
+		diff[key] += count
+	else:
+		diff[key] = count
+
+if len(orig) < 2* results_per_sign or len(new) < 2 * results_per_sign:
+	sys.exit(1)		# one of the files was blank?
+
+# Now sort and print the diffs.
+def print_key(key):
+	if orig.has_key(key) and orig[key] > 0:
+		pct = (100 * diff[key]) / orig[key]
+	else:
+		pct = 0
+	print "%10d  %6.1f%% %s" % (diff[key], pct, key)
+
+keys = sorted(diff.keys(), key=lambda x : diff[x], reverse = True)
+
+for key in keys[:results_per_sign]:
+	print_key(key)
+
+print "\n...\n"
+
+for key in keys[len(keys)-results_per_sign:]:
+	print_key(key)