From 0273341f821d8443a3659bf3e69fe4c2cd20eb95 Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Fri, 24 Oct 2014 17:34:23 +0000
Subject: [PATCH] Consolidate stats to Stats_t

---
 .gitattributes       |  1 +
 include/zipr.h       | 22 +-------------------
 include/zipr_stats.h | 29 ++++++++++++++++++++++++++
 src/Makefile         |  2 +-
 src/zipr.cpp         | 41 +++++++++++++------------------------
 src/zipr_stats.cpp   | 48 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 94 insertions(+), 49 deletions(-)
 create mode 100644 src/zipr_stats.cpp

diff --git a/.gitattributes b/.gitattributes
index 2770518a5..7e714e68d 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -29,6 +29,7 @@ src/Makefile -text
 src/main.cpp -text
 src/zipr.cpp -text
 src/zipr_options.cpp -text
+src/zipr_stats.cpp -text
 third_party/ELFIO/elfio-2.2/AUTHORS -text
 third_party/ELFIO/elfio-2.2/COPYING -text
 third_party/ELFIO/elfio-2.2/ChangeLog -text
diff --git a/include/zipr.h b/include/zipr.h
index 108ec2ed1..7cdb1c1cb 100644
--- a/include/zipr.h
+++ b/include/zipr.h
@@ -42,16 +42,7 @@ class Zipr_t
 		Zipr_t(libIRDB::FileIR_t* p_firp, Options_t &p_opts)
 			: m_firp(p_firp), m_opts(p_opts)
 		{ 
-                	total_dollops=0;
-                	total_dollop_space=0;
-                	total_dollop_instructions=0;
-                	total_trampolines=0;
-                	total_2byte_pins=0;
-                	total_5byte_pins=0;
-                	total_tramp_space=0;
-                	total_other_space=0;
-			truncated_dollops=0;
-		};
+ 		};
 
 		void CreateBinaryFile(const std::string &name);
 
@@ -149,17 +140,6 @@ class Zipr_t
 		// way to read elf headers, etc.
 		ELFIO::elfio*    elfiop;
 
-		// stats
-		int total_dollops;
-		int total_dollop_space;
-		int total_dollop_instructions;
-		int total_trampolines;
-		int total_2byte_pins;
-		int total_5byte_pins;
-		int total_tramp_space;
-		int total_other_space;
-		int truncated_dollops;
-
 		// records where we will insert extra bytes into the program.
 		RangeAddress_t start_of_new_space;
 
diff --git a/include/zipr_stats.h b/include/zipr_stats.h
index 938863de7..2dfb27740 100644
--- a/include/zipr_stats.h
+++ b/include/zipr_stats.h
@@ -9,7 +9,36 @@ class Stats_t
 			{
 				Hits[i] = Misses[i] = 0;
 			}
+			total_dollops = 0;
+			total_dollop_space = 0;
+			total_dollop_instructions = 0;
+			truncated_dollops = 0;
+			total_trampolines = 0;
+			total_2byte_pins = 0;
+			total_5byte_pins = 0;
+			total_tramp_space = 0;
+			total_other_space = 0;
 		};
+
+		void PrintStats(Options_t opts, std::ostream &out);
+
+		/*
+		 * General stats tracking.
+		 */
+
+		int total_dollops;
+		int total_dollop_space;
+		int total_dollop_instructions;
+		int truncated_dollops;
+		int total_trampolines;
+		int total_2byte_pins;
+		int total_5byte_pins;
+		int total_tramp_space;
+		int total_other_space;
+
+		/*
+		 * Optimization stats tracking.
+		 */
 		int Hits[Optimizations_t::NumberOfOptimizations];
 		int Misses[Optimizations_t::NumberOfOptimizations];
 		void Hit(Optimizations_t::OptimizationName_t opt) { Hits[opt]++; };
diff --git a/src/Makefile b/src/Makefile
index 8d8275908..e6283f159 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -3,7 +3,7 @@ INC=-I../include -I../third_party/ELFIO/elfio-2.2 -I$(SECURITY_TRANSFORMS_HOME)/
 
 
 
-SRCS=zipr.cpp zipr_options.cpp main.cpp
+SRCS=zipr.cpp zipr_options.cpp zipr_stats.cpp main.cpp
 OBJS=$(subst .cpp,.o, $(SRCS))
 HDRS=../include/*.h 
 EXE=zipr.exe
diff --git a/src/zipr.cpp b/src/zipr.cpp
index b003e3e46..cae6b167a 100644
--- a/src/zipr.cpp
+++ b/src/zipr.cpp
@@ -589,7 +589,7 @@ void Zipr_t::ReservePinnedInstructions()
 			{
 				byte_map[addr+i]=upinsn->GetDataBits()[i];
 				SplitFreeRange(addr+i);
-				total_other_space++;
+				m_stats->total_other_space++;
 			}
 			continue;
 		}
@@ -647,17 +647,17 @@ void Zipr_t::ExpandPinnedInstructions()
 
 			five_byte_pins[up]=addr;
 			two_byte_pins.erase(it++);
-			total_5byte_pins++;
-			total_trampolines++;
+			m_stats->total_5byte_pins++;
+			m_stats->total_trampolines++;
 		}
 		else
 		{
 			++it;
 			if(m_opts.GetVerbose())
 				printf("Found %p can NOT be updated to 5-byte jmp\n", (void*)addr);
-			total_2byte_pins++;
-			total_trampolines++;
-			total_tramp_space+=2;
+			m_stats->total_2byte_pins++;
+			m_stats->total_trampolines++;
+			m_stats->total_tramp_space+=2;
 		}
 	}
 
@@ -812,7 +812,7 @@ void Zipr_t::OptimizePinnedInstructions()
 			if(m_opts.GetVerbose())
 				printf("Converting 5-byte pinned jump at %p-%p to patch to %d:%s\n", 
 				(void*)addr,(void*)(addr+4), uu.GetInstruction()->GetBaseID(), d.CompleteInstr);
-			total_tramp_space+=5;
+			m_stats->total_tramp_space+=5;
 		}
 
 		// remove and move to next pin
@@ -1029,14 +1029,14 @@ void Zipr_t::ProcessUnpinnedInstruction(const UnresolvedUnpinned_t &uu, const Pa
 		patch_list.insert(pair<UnresolvedUnpinned_t,Patch_t>(uu,thepatch));
 		PlopJump(cur_addr);
 		truncated="truncated due to lack of space.";
-		total_tramp_space+=5;
-		truncated_dollops++;
-		total_trampolines++;
+		m_stats->total_tramp_space+=5;
+		m_stats->truncated_dollops++;
+		m_stats->total_trampolines++;
 	}
 
-	total_dollops++;
-	total_dollop_instructions+=insn_count;
-	total_dollop_space+=(cur_addr-fr_start);
+	m_stats->total_dollops++;
+	m_stats->total_dollop_instructions+=insn_count;
+	m_stats->total_dollop_space+=(cur_addr-fr_start);
 
 	if(m_opts.GetVerbose())
 		printf("Ending dollop.  size=%d, %s.  space_remaining=%lld, req'd=%d\n", insn_count, truncated,
@@ -1408,20 +1408,7 @@ void Zipr_t::OutputBinaryFile(const string &name)
 
 void Zipr_t::PrintStats()
 {
-	cout<<"Total dollops: "<<std::dec << total_dollops <<endl;
-	cout<<"Total dollop size: "<<std::dec << total_dollop_space <<endl;
-	cout<<"Total dollop instructions: "<<std::dec << total_dollop_instructions <<endl;
-	cout<<"Truncated dollops: "<<std::dec << truncated_dollops <<endl;
-	cout<<"Ave dollop size: "<<std::dec << (double)total_dollop_space/(double)total_dollops <<endl;
-	cout<<"Ave dollop instructions: "<<std::dec << (double)total_dollop_instructions/(double)total_dollops <<endl;
-	cout<<"Truncated dollop fraction: "<<std::dec << (double)truncated_dollops/(double)total_dollops <<endl;
-
-	cout<<"Total trampolines: "<<std::dec << total_trampolines <<endl;
-	cout<<"Total 2-byte pin trampolines: "<<std::dec << total_2byte_pins <<endl;
-	cout<<"Total 5-byte pin trampolines: "<<std::dec << total_5byte_pins <<endl;
-	cout<<"Total trampoline space pins: "<<std::dec << total_tramp_space <<endl;
-
-	cout<<"Other space: "<<total_other_space<<endl;
+	m_stats->PrintStats(m_opts, cout);
 }
 
 template < typename T > std::string to_hex_string( const T& n )
diff --git a/src/zipr_stats.cpp b/src/zipr_stats.cpp
new file mode 100644
index 000000000..3254ff516
--- /dev/null
+++ b/src/zipr_stats.cpp
@@ -0,0 +1,48 @@
+#include <zipr_all.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <iostream>
+
+using namespace zipr;
+
+static void PrintStat(std::ostream &out, std::string description, int value)
+{
+	out << description << ": " << std::dec << value << std::endl;
+}
+static void PrintStat(std::ostream &out, std::string description, double value)
+{
+	out << description << ": " << std::dec << value << std::endl;
+}
+
+void Stats_t::PrintStats(Options_t opts, std::ostream &out)
+{
+	PrintStat(out, "Total dollops", total_dollops);
+	PrintStat(out, "Total dollop size", total_dollop_space);
+	PrintStat(out, "Total dollop instructions",total_dollop_instructions);
+	PrintStat(out, "Truncated dollops",truncated_dollops);
+	PrintStat(out, "Ave dollop size",
+		(double)total_dollop_space/(double)total_dollops);
+	PrintStat(out, "Ave dollop instructions",
+		(double)total_dollop_instructions/(double)total_dollops);
+	PrintStat(out, "Truncated dollop fraction",
+		(double)truncated_dollops/(double)total_dollops);
+
+	/*
+	 * Optimizations
+	 */
+	if(opts.IsEnabledOptimization(Optimizations_t::OptimizationFallthroughPinned))
+	{
+		PrintStat(out, "Optimization: FallthroughPinned hit rate",
+			(double)Hits[Optimizations_t::OptimizationFallthroughPinned]/
+			(Hits[Optimizations_t::OptimizationFallthroughPinned] +
+			Misses[Optimizations_t::OptimizationFallthroughPinned]));
+	}
+
+	PrintStat(out, "Total trampolines", total_trampolines);
+	PrintStat(out, "Total 2-byte pin trampolines", total_2byte_pins);
+	PrintStat(out, "Total 5-byte pin trampolines", total_5byte_pins);
+	PrintStat(out, "Total trampoline space pins", total_tramp_space);
+	PrintStat(out, "Other space", total_other_space);
+	PrintStat(out, "Total free ranges", total_free_ranges);
+}
-- 
GitLab