ptxed: track number of blocks

When using ptxed's --stat option with the block decoder, print the number of
blocks in addition to the number of instructions.

Also add a new option --stat:blocks to select collecting the number of blocks.

Change-Id: I59caf3692667318f23a1887a844042b561a338a5
Signed-off-by: Markus Metzger <markus.t.metzger@intel.com>
diff --git a/ptxed/src/ptxed.c b/ptxed/src/ptxed.c
index 0ddcbd0..8615bad 100644
--- a/ptxed/src/ptxed.c
+++ b/ptxed/src/ptxed.c
@@ -91,7 +91,10 @@
 /* A collection of flags selecting which stats to collect/print. */
 enum ptxed_stats_flag {
 	/* Collect number of instructions. */
-	ptxed_stat_insn		= (1 << 0)
+	ptxed_stat_insn		= (1 << 0),
+
+	/* Collect number of blocks. */
+	ptxed_stat_blocks	= (1 << 1)
 };
 
 /* A collection of statistics. */
@@ -99,6 +102,12 @@
 	/* The number of instructions. */
 	uint64_t insn;
 
+	/* The number of blocks.
+	 *
+	 * This only applies to the block decoder.
+	 */
+	uint64_t blocks;
+
 	/* A collection of flags saying which statistics to collect/print. */
 	uint32_t flags;
 };
@@ -926,8 +935,10 @@
 				 * in decoding some instructions.
 				 */
 				if (block.ninsn) {
-					if (stats)
+					if (stats) {
 						stats->insn += block.ninsn;
+						stats->blocks += 1;
+					}
 
 					if (!options->quiet)
 						print_block(&block, iscache,
@@ -936,8 +947,10 @@
 				break;
 			}
 
-			if (stats)
+			if (stats) {
 				stats->insn += block.ninsn;
+				stats->blocks += 1;
+			}
 
 			if (!options->quiet)
 				print_block(&block, iscache, options, offset);
@@ -993,6 +1006,9 @@
 
 	if (stats->flags & ptxed_stat_insn)
 		printf("insn: %" PRIu64 ".\n", stats->insn);
+
+	if (stats->flags & ptxed_stat_blocks)
+		printf("blocks:\t%" PRIu64 ".\n", stats->blocks);
 }
 
 static int get_arg_uint64(uint64_t *value, const char *option, const char *arg,
@@ -1245,6 +1261,10 @@
 			stats.flags |= ptxed_stat_insn;
 			continue;
 		}
+		if (strcmp(arg, "--stat:blocks") == 0) {
+			stats.flags |= ptxed_stat_blocks;
+			continue;
+		}
 		if (strcmp(arg, "--cpu") == 0) {
 			/* override cpu information before the decoder
 			 * is initialized.
@@ -1358,10 +1378,16 @@
 
 	xed_tables_init();
 
-	/* If we didn't select any statistics, select them all. */
-	if (options.print_stats && !stats.flags)
+	/* If we didn't select any statistics, select them all depending on the
+	 * decoder type.
+	 */
+	if (options.print_stats && !stats.flags) {
 		stats.flags |= ptxed_stat_insn;
 
+		if (decoder.type == pdt_block_decoder)
+			stats.flags |= ptxed_stat_blocks;
+	}
+
 	decode(&decoder, iscache, &options,
 	       options.print_stats ? &stats : NULL);
 
diff --git a/test/src/ptxed-stat.ptt b/test/src/ptxed-block-stat.ptt
similarity index 87%
copy from test/src/ptxed-stat.ptt
copy to test/src/ptxed-block-stat.ptt
index f4f7717..2229afb 100644
--- a/test/src/ptxed-stat.ptt
+++ b/test/src/ptxed-block-stat.ptt
@@ -24,9 +24,9 @@
 ; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 ; POSSIBILITY OF SUCH DAMAGE.
 
-; Test that ptxed counts instructions correctly.
+; Test that ptxed counts instructions and blocks correctly.
 ;
-; opt:ptxed --stat
+; opt:ptxed --block-decoder --stat
 
 org 0x1000
 bits 64
@@ -40,10 +40,9 @@
 l2: nop
 l3: nop
 
-; @pt p4:fup(3: %l4)
-; @pt p5:tip.pgd(0: %l5)
-l4: nop
-l5: hlt
+; @pt p4: fup(1: %l4)
+; @pt p5: tip.pgd(0: %l4)
+l4: hlt
 
 
 ; @pt .exp(ptxed)
@@ -52,12 +51,13 @@
 ;%0l2
 ;%0l3
 ;[disabled]
-;insn: 4.
+;insn:    4.
+;blocks:  1.
 
 ; @pt .exp(ptdump)
 ;%0p0  psb
 ;%0p1  mode.exec  cs.l
-;%0p2  fup        3: %0l0
+;%0p2  fup        3: %?l0
 ;%0p3  psbend
-;%0p4  fup        3: %0l4
-;%0p5  tip.pgd    0: %?l5.0
+;%0p4  fup        1: %?l4.2
+;%0p5  tip.pgd    0: %?l4.0
diff --git a/test/src/ptxed-stat.ptt b/test/src/ptxed-block-stat_blocks.ptt
similarity index 86%
copy from test/src/ptxed-stat.ptt
copy to test/src/ptxed-block-stat_blocks.ptt
index f4f7717..91990cd 100644
--- a/test/src/ptxed-stat.ptt
+++ b/test/src/ptxed-block-stat_blocks.ptt
@@ -24,9 +24,9 @@
 ; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 ; POSSIBILITY OF SUCH DAMAGE.
 
-; Test that ptxed counts instructions correctly.
+; Test that ptxed counts instructions and blocks correctly.
 ;
-; opt:ptxed --stat
+; opt:ptxed --block-decoder --stat --stat:blocks
 
 org 0x1000
 bits 64
@@ -40,10 +40,9 @@
 l2: nop
 l3: nop
 
-; @pt p4:fup(3: %l4)
-; @pt p5:tip.pgd(0: %l5)
-l4: nop
-l5: hlt
+; @pt p4: fup(1: %l4)
+; @pt p5: tip.pgd(0: %l4)
+l4: hlt
 
 
 ; @pt .exp(ptxed)
@@ -52,12 +51,12 @@
 ;%0l2
 ;%0l3
 ;[disabled]
-;insn: 4.
+;blocks:  1.
 
 ; @pt .exp(ptdump)
 ;%0p0  psb
 ;%0p1  mode.exec  cs.l
-;%0p2  fup        3: %0l0
+;%0p2  fup        3: %?l0
 ;%0p3  psbend
-;%0p4  fup        3: %0l4
-;%0p5  tip.pgd    0: %?l5.0
+;%0p4  fup        1: %?l4.2
+;%0p5  tip.pgd    0: %?l4.0
diff --git a/test/src/ptxed-stat.ptt b/test/src/ptxed-insn-stat.ptt
similarity index 98%
rename from test/src/ptxed-stat.ptt
rename to test/src/ptxed-insn-stat.ptt
index f4f7717..39dc32e 100644
--- a/test/src/ptxed-stat.ptt
+++ b/test/src/ptxed-insn-stat.ptt
@@ -26,7 +26,7 @@
 
 ; Test that ptxed counts instructions correctly.
 ;
-; opt:ptxed --stat
+; opt:ptxed --insn-decoder --stat
 
 org 0x1000
 bits 64