Skip to contentSkip to conversation

Instruction counting instrumentation for ARM + initial patch

Qemu-devel Timo Töyry 5 participants 6 messages May 19, 2009 – May 25, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi,

The attached patch implements instruction counting instrumentation for ARM usermode linux. Currently the patch is a working draft but we intend to clean it up for possible inclusion. We'd appreciate any comments. Below is a more detailed description.

Motivation:
For some applications it is important to know what complex instructions are used by the application. Also the data of used instructions could be used to optimize the platform CPU in embedded systems, e.g., choosing the most appropriate CPU for a specific task.

Implementation:
The instruction counters are implemented as extra guest CPU registers, see target-arm/cpu.h:CPUARMState. The counter incrementation is done by modifying decoder to generate TCG code to increment the counters. The values of the counters are printed at guest exit (syscall exit_group & al). The implementation is for arm-linux-user, but we suppose this would be easily ported to other guest architectures, too.

Patch status:
The attached draft patch applies to 0.10.4. Currently it supports armv6, vfp and thumb instructions. We'll probably add support for neon (if we get permissions from ARM).

Todo:
- configure-support to enable/disable this feature at compile time
- extract instrumentation code from decoders, to better support re-use in other architectures
- selectable target (console/log-file) for outputting the values of the counters

Usage:
The instruction counting is enabled with command-line parameter -instrcount. When the guest application exits the counter values are printed to stderr. Example: qemu-arm -instrcount <some-arm-linux-binary>

This work is sponsored by Nokia / Maemo development team.

Regards,
Timo

Signature · 2 lines
Timo Töyry
Embedded Software Group / Helsinki University of Technology
Makefile.target +3 -1
Index: Makefile.target
===================================================================
@@ -208,8 +208,10 @@
 LIBOBJS+=s390-dis.o
 endif
 
+# instrumentation support
+LIBOBJS+=instrumentation.o
+
 # libqemu
-
 libqemu.a: $(LIBOBJS)
 
 translate.o: translate.c cpu.h
syscall.c +63 -17

195 lines not on this page · raw message

main.c +18 -13
Index: linux-user/main.c
===================================================================
@@ -35,6 +35,8 @@
 
 #include "envlist.h"
 
+#include "instrumentation.h"
+
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
 char *exec_path;
@@ -1945,7 +1947,7 @@
 {
     int trapnr, ret;
     target_siginfo_t info;
-    
+
     while (1) {
         trapnr = cpu_cris_exec (env);
         switch (trapnr) {
@@ -1963,13 +1965,13 @@
 	  /* just indicate that signals should be handled asap */
 	  break;
         case EXCP_BREAK:
-            ret = do_syscall(env, 
-                             env->regs[9], 
-                             env->regs[10], 
-                             env->regs[11], 
-                             env->regs[12], 
-                             env->regs[13], 
-                             env->pregs[7], 
+            ret = do_syscall(env,
+                             env->regs[9],
+                             env->regs[10],
+                             env->regs[11],
+                             env->regs[12],
+                             env->regs[13],
+                             env->pregs[7],
                              env->pregs[11]);
             env->regs[10] = ret;
             break;
@@ -2199,6 +2201,8 @@
            "-p pagesize  set the host page size to 'pagesize'\n"
            "-strace      log system calls\n"
            "\n"
+           "Other:\n"
+           "-instrcount	Count instructions\n"
            "Environment variables:\n"
            "QEMU_STRACE       Print system calls and arguments similar to the\n"
            "                  'strace' program.  Enable by setting to any value.\n"
@@ -2222,7 +2226,7 @@
 void init_task_state(TaskState *ts)
 {
     int i;
- 
+
     ts->used = 1;
     ts->first_free = ts->sigqueue_table;
     for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
@@ -2230,7 +2234,7 @@
     }
     ts->sigqueue_table[i].next = NULL;
 }
- 
+
 int main(int argc, char **argv, char **envp)
 {
     const char *filename;
@@ -2341,8 +2345,9 @@
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
         } else if (!strcmp(r, "strace")) {
             do_strace = 1;
-        } else
-        {
+        } else if (!strcmp(r, "instrcount")) {
+        	instrumentation_count_instructions = 1;
+        } else {
             usage();
         }
     }
@@ -2667,7 +2672,7 @@
 	    env->regs[12] = regs->r12;
 	    env->regs[13] = regs->r13;
 	    env->regs[14] = info->start_stack;
-	    env->regs[15] = regs->acr;	    
+	    env->regs[15] = regs->acr;
 	    env->pc = regs->erp;
     }
 #else
Makefile +4 -0
Index: Makefile
===================================================================
@@ -179,6 +179,10 @@
 # USER_OBJS is code used by qemu userspace emulation
 USER_OBJS=cutils.o  cache-utils.o
 
+# instruction count instrumentation
+USER_OBJS+=instrumentation.o
+
+
 libqemu_user.a: $(USER_OBJS)
 
 ######################################################################
instrumentation.c +9 -0
Index: instrumentation.c
===================================================================
@@ -0,0 +1,9 @@
+/*
+ * instrument.c
+ *
+ *  Created on: May 14, 2009
+ *      Author: ttoyry
+ */
+
+#include "instrumentation.h"
+unsigned int instrumentation_count_instructions = 0;
instrumentation.h +16 -0
Index: instrumentation.h
===================================================================
@@ -0,0 +1,16 @@
+/*
+ * instrumentation.h
+ *
+ * Author: Timo Toyry
+ */
+
+#ifndef INSTRUMENTATION_H
+#define INSTRUMENTATION_H
+
+/*
+ * 0 to disable (default)
+ * nonzero to enable
+ */
+extern unsigned int instrumentation_count_instructions;
+
+#endif /* INSTRUMENTATION_H */
cpu.h +338 -0

354 lines not on this page · raw message

translate.c +743 -45

1,935 lines not on this page · raw message

#1 lore raw
Quoted 6 lines · Timo Töyry

Hi,

The attached patch implements instruction counting instrumentation for ARM usermode linux. Currently the patch is a working draft but we intend to clean it up for possible inclusion. We'd appreciate any comments. Below is a more detailed description.

Ewww. This looks pretty much impossible to maintain.
If you really want to do this I recommend dumping an execution trace, and having a third party utility do the counting after the fact.

Paul

#2 lore raw
Quoted 11 lines · Paul Brook
Quoted 6 lines · Timo Töyry

Hi,

The attached patch implements instruction counting instrumentation for ARM usermode linux. Currently the patch is a working draft but we intend to clean it up for possible inclusion. We'd appreciate any comments. Below is a more detailed description.

Ewww. This looks pretty much impossible to maintain.
If you really want to do this I recommend dumping an execution trace, and having a third party utility do the counting after the fact.

My advice would be similar to Paul's: for my instruction counter, instead of counting instructions, I count translation blocks. I can either output a TB trace (which can be huge, but useful to do trace analysis) or an execution count for each TB. As a postpass you can do whatever you want. The benefits, on top of being easier to maintain, are that it's more flexible, it has a very small impact on QEMU execution time, and it can be trivially ported to any front end. The problem is to play nicely with TB flushes :-)

HTH,

Laurent

#3 lore raw

I wonder if a simplistic stats gathering frame work could be added to Qemu.

The problem is there currently are at least 3 users of Qemu:

  1.  People who want fast simulation
  2.  People who are doing virtualization
  3.  People trying to do instrumentation/research

Unfortunately those three groups have conflicting interests.

The main problem is that adding instrumentation infrastructure will either slow down the common case, or else introduce lots of #ifdefs all over the code. Neither is very attractive.

It would be nice if maybe a limited instrumentation architecture could
be put into qemu, that could be configured out. It would save the various researchers the problem of everyone re-implementing it differently.

It would be nice to have:
1. A way to dump an instruction trace (address, length (for CISC), and opcode, CPU# for multi-thread)
2. A way to dump memory traces (address, length, possibly the value loaded/stored, CPU# for multi-thread)
3. A way to dump basic-block entry/exit

Many of the various research metrics can be gained from these stats.
#1 and #2 are enough for cache simulators.
#1 (if post-processed) is enough to get a frequency plot for instruction
count and type.
#1 can be used to extrapolate branch-taken statistics for branch
predictors
#3 Can be used for basic block vectors, or to get faster instruction
counts

I'm not sure if it's possible to do this in a non-intrusive way that would be acceptable to the other users of Qemu though.

Pin manages to have their null plugin run very fast; at least one of the Spec2k binaries runs faster translated than it does natively.

Vince

#4 lore raw
Quoted 8 lines · Vince Weaver

I wonder if a simplistic stats gathering frame work could be added to Qemu.

The problem is there currently are at least 3 users of Qemu:
 1.  People who want fast simulation
 2.  People who are doing virtualization
 3.  People trying to do instrumentation/research

Quoted 5 lines

Unfortunately those three groups have conflicting interests.

The main problem is that adding instrumentation infrastructure will either slow down the common case, or else introduce lots of #ifdefs all over the code.  Neither is very attractive.

I don't think adding command-line enabled options will slow down the standard translation in a measurable way, provided, for instance, it isn't being checked before running every translated block. If it's checked before/after translating a block, then it shouldn't effect performance.

Quoted 11 lines

It would be nice if maybe a limited instrumentation architecture could be put into qemu, that could be configured out.  It would save the various researchers the problem of everyone re-implementing it differently.

It would be nice to have:
 1. A way to dump an instruction trace (address, length (for CISC), and opcode, CPU# for multi-thread)
 2. A way to dump memory traces (address, length, possibly the value loaded/stored, CPU# for multi-thread)
 3. A way to dump basic-block entry/exit

Quoted 8 lines

Many of the various research metrics can be gained from these stats. #1 and #2 are enough for cache simulators.
 #1 (if post-processed) is enough to get a frequency plot for instruction count and type.
 #1 can be used to extrapolate branch-taken statistics for branch predictors
 #3 Can be used for basic block vectors, or to get faster instruction counts

You don't need to generate an instruction trace as I said in my previous mail. For user mode applications, a TB trace is enough (of course there are some fine points that can cause trouble to derive the instruction trace from a TB trace such as dynamically generated code, or TB flushing) to derive an instruction trace.

As an example, my TB counter requires <30% more time to run one of the SPEC 2k tests, while a full TB trace (binary format

Quoted 1 line

2.7 GB) going to a file doubles the run-time, which is very

acceptable. Of course, you then need to process the output using other programs.

Getting memory traces would be more intrusive and would certainly slow down simulation significantly.

Quoted 2 lines

Pin manages to have their null plugin run very fast; at least one of the Spec2k binaries runs faster translated than it does natively.

Too bad they only support x86 now and are not open source. Anyway Pin is not the only binary tool that can make programs faster, Diablo (LTO) also was able to speed up ARM programs.

Laurent

#5 lore raw
Quoted 10 lines · Laurent Desnogues
Quoted 3 lines · Vince Weaver

The main problem is that adding instrumentation infrastructure will either slow down the common case, or else introduce lots of #ifdefs all over the code. Neither is very attractive.

I don't think adding command-line enabled options will slow down the standard translation in a measurable way, provided, for instance, it isn't being checked before running every translated block. If it's checked before/after translating a block, then it shouldn't effect performance.

I tried to measure the performance difference between vanilla Qemu and Qemu with this patch but without the command-line switch. As suggested above, I couldn't measure the difference. However, to disable this feature compile-time, I think that it should be enough to:
1. define macro instr_count_inc which conveniently eliminates all function calls to instr_count_inc() and instr_count_inc_init().
2. insert some #ifdefs to disable the framework code (e.g. #ifdef in CPUARMState to remove counters)

For my small set of workloads, I've measured around 10%..40% overhead when instruction counting is enabled and this is definitely acceptable for us. Your mileage may vary.

Quoted 3 lines
Quoted 3 lines

It would be nice if maybe a limited instrumentation architecture could be put into qemu, that could be configured out. It would save the various researchers the problem of everyone re-implementing it differently.

I think this is something that many software developers would be interested in, too. E.g., getting proper cache utilization etc. BTW, for on-line cache simulation, wouldn't it be enough to instrument memory accesses at TCG level (e.g., tcg_gen_ld8u_i32, ...)?

Quoted 5 lines

You don't need to generate an instruction trace as I said in my previous mail. For user mode applications, a TB trace is enough (of course there are some fine points that can cause trouble to derive the instruction trace from a TB trace such as dynamically generated code, or TB flushing) to derive an instruction trace.

We considered this when designing the patch. However, we decided to start with the current implementation due to concerns of dynamically generated code, as you pointed out. And then there's the system emulation. Anyways, the most important thing would be to have the instr_count_inc():s in the decoders. It shouldn't be too hard to change the implementation later with at most trivial modifications to the decoders.

Sami Kiminki
Embedded Software Group / Helsinki University of Technology