180 likes | 281 Views
Readings on Instrumentation, Profiling, and Tracing. IPT. Seminar presentation by Milan Jovic University of Lugano November 30, 2006. BIT: A Tool for Instrumenting Java Bytecodes. Han Bok Lee (University of Colorado, Boulder) Benjamin G. Zorn (University of Colorado, Boulder) USENIX, 1997.
E N D
Readings on Instrumentation, Profiling, and Tracing IPT Seminar presentation by Milan Jovic University of Lugano November 30, 2006
BIT: A Tool for Instrumenting Java Bytecodes Han Bok Lee (University of Colorado, Boulder) Benjamin G. Zorn (University of Colorado, Boulder) USENIX, 1997
What is BIT? • BIT = ATOM for Java bytecode • framework for Java
The ATOM process The BIT process
How does BIT work? • What has to be changed? • instances of BIT classes • new method calls required • new constants and variables needed • Constant pool has to be changed
disassembled while loop statement publicstaticvoid main(String[] args) { int i, j = 10; i = 0; while (j > 0) { if (j % 2 == 0) i++; else System.out.print(j+i); j--; } } public static void main(java.lang.String[]); Signature: ([Ljava/lang/String;)V Code: 0: bipush 10 2: istore_2 3: iconst_0 4: istore_1 5: goto 32 8: iload_2 9: iconst_2 10: irem 11: ifne 20 14: iinc 1, 1 17: goto 29 20: getstatic #16; //Field java/lang/System.out:Ljava/io/PrintStream; 23: iload_2 24: iload_1 25: iadd 26: invokevirtual #22; //Method java/io/PrintStream.print:(I)V 29: iinc 2, -1 32: iload_2 33: ifgt 8 36: return }
disassembled SWITCH statement publicstaticvoid main(String[] args) { int i, j; i = 5; switch (i) { case 0:j=0;break; case 1:j=1;break; case 2:j=2;break; case 3:j=3;break; default : j=4; } System.out.println (j); } public static void main(java.lang.String[]); Code: 0: iconst_5 1: istore_1 2: iload_1 3: tableswitch{ //0 to 3 0: 32; 1: 37; 2: 42; 3: 47; default: 52 } 32: iconst_0 33: istore_2 34: goto 54 37: iconst_1 38: istore_2 39: goto 54 42: iconst_2 43: istore_2 44: goto 54 47: iconst_3 48: istore_2 49: goto 54 52: iconst_4 53: istore_2 54: getstatic #16; //Field java/lang/System.out:Ljava/io/PrintStream; 57: iload_2 58: invokevirtual #22; //Method java/io/PrintStream.println:(I)V 61: return }
How to instrument? Routine ClassInfo addBefore () getBasicBlocks() getInstructions () getMethod () getRoutines () ClassInfo (fileName) write () Instruction addBefore () addAfter () getOpcode () getOrigOffset ()
Instrument for dynamic instruction counting ClassInfo c = new ClassInfo (InstrumentFileName); Vector r = c.getRoutines (); for (Enumeration e = r.elements (); e.has MoreElements ();) { Routine rout = (Routine) e.nextElement (); Vector i = rout.getInstructions (); for (Enumeration e2 = rout.getBasicBlocks().elements ();; b.hasMoreElements ();) { BasicBlock bb = (BasicBlock) e2.nextElement (); bb.addBefore (“ICount”, “count”, new Integer (bb.size())); } } c.write (OutputFileName);
Instrument for branch profiling ClassInfo c = new ClassInfo (InstrumentFileName); Vector r = c.getRoutines (); for (Enumeration e = r.elements (); e.has MoreElements ();) { Routine rout = (Routine) e.nextElement (); Vector i = rout.getInstructions (); for (Enumeration e2 = rout.getBasicBlocks ().elements(); e2.hasMoreElements ();) { BasicBlock bb = (BasicBlock) e2.nextElement (); Instruction inst = (Instruction) i.elementAt(bb.getEndAddress ()); short type = InstructionTable.InstructionTypeTable [inst.getOpcode ()]; if (type == InstructionTable.CONDITIONAL_INSTRUCTION) { inst.addBefore (“BranchPrediction”, “Offset”, new Integer (inst.getOrigOffset ()); inst.addBefore (“BracnhPrediction”, “Branch”, new String (“Branch Outcome”); } } String method = new String (rout.getMethod ()); rout.addBefore (“BranchPrediction”, “EnterMethod”, method); rout.addAfter (“BranchPrediction”, “LeaveMethod”, method); } c.write (OutputFileName);
Analysis code for branch profiling static int pc = 0; public static void Offset (int offset) { pc = offset; } public static void Branch (int brOutcome) { Integer n = new Integer (pc); Branch b = (Branch) branch.get (n); if (b == null) b = new Branch (); if (brOutcome == 0) b.taken++; else b.not_taken++; }
Weakness of Tool? • Slowdown of instrumented program • 23% to 150% • Mostly around 150% • Doesn’t work with exceptions • Calls from instrumentation to analysis code limited to one argument