User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
using-jp2-to-instrument-programs-based-on-apache-felix [2011/03/02 04:37]
weiwu created
using-jp2-to-instrument-programs-based-on-apache-felix [2017/09/06 01:54] (current)
Line 1: Line 1:
-JP2 is a Java Profiler developed by Binder et al. ((W. Binder and J. Hulaas, "Using bytecode instruction counting as portable cpu consumption metric",​ Electronic Notes In Theoretical Computer Science, vol. 153, pp. 57-77, May 2006.)). It can be used to count the executed bytecode number to estimate CPU consumption of a Java program.+====== Background ======
 +JP2 is an excellent and easy-to-use Java Profiler developed by Binder et al.((W. Binder and J. Hulaas, "Using bytecode instruction counting as portable cpu consumption metric",​ Electronic Notes In Theoretical Computer Science, vol. 153, pp. 57-77, May 2006.)). It can be used to count the numbers of executed bytecode instructions to estimate the CPU consumption of a Java program. The basics of calling JP2 are as follows:
 +java -Xmx2g \
 +    -javaagent:​$JP2_HOME/​lib/​javaagent.jar \
 +    -Xbootclasspath/​p:​$JP2_HOME/​lib/​Thread_JP2.jar:​$JP2_HOME/​lib/​asm-all-3.3.jar:​$JP2_HOME/​lib/​jp2.jar \
 +    -Dch.usi.dag.jp2.Output="​program.output.gz"​ \
 +    -Dch.usi.dag.jp2.InstrumentedClasses="​ProgramInstrumented"​ \
 +    -Dch.usi.dag.jp2.UninstrumentedClasses="​ProgramUninstrumented"​ \
 +    -cp YourClasspath YourMainClass ​
 +====== Felix and SIP ======
 +While the previous code works well and allows instrumenting programs in most situations, we faced some difficulties when instrumenting SIP communications((http://​​)). SIP Communicator is a Java VoIP and instant messaging client using Felix. Felix((http://​​site/​index.html)) is the Apache implementation of the OSGi specifications,​ which defines a dynamic service deployment framework. The OSGi specifications((http://​​)) defines a framework on which service implementations or //bundles// are plugged and managed by the framework, independently of the underlying running JVM (and its classpath). The framework handles bundle discovery, dependencies,​ requests, and responses. Each bundle communicates only with/​through the framework. ​
 +We wanted to count the numbers of executed bytecode instructions of SIP communicator using JP2. When instrumenting SIP Communicator,​ we ran into several problems, which we fixed as follows.
 +==== Not Instrumenting Felix ====
 +Exclude Felix classes from being instrumented,​ so Felix can run without having to know about JP2. We added:
 +<​code>​!className.startsWith("​org/​apache/​felix"​)</​code> ​
 +to method:
 +<​code>​byte[] transform(ClassLoader loader, String className, Class<?>​ redefiningClass,​ ProtectionDomain domain, byte[] uninstrumentedBytes)</​code> ​
 +in class:
 +<​code>​ ch.usi.dag.jp2.agent.SimpleTransformer </​code>​
 +==== Telling Felix where to find JP2 for the Instrumented Bundles ====
 +Add a bundle which contains all the classes of JP2 into the build.xml of SIP:
 + <target name="​bundle-jp2">​
 + <jar compress="​true"​ destfile="​${bundles.dest}/​bundle-jp2.jar"​ filesetmanifest="​skip">​
 + <​zipfileset src="​${lib.noinst}/​jp2/​asm-all-3.3.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​bcel-5.2.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​javaagent.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​jp2.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​jre-tool.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​Thread_JP2.jar"​ prefix=""​ />
 + <​zipfileset src="​${lib.noinst}/​jp2/​thread-tool.jar"​ prefix=""​ />
 + <​manifest>​
 + <​attribute name="​Export-Package"​ value="​ch.usi.dag.jp2.agent,​ch.usi.dag.jp2.rewriting,​ch.usi.dag.jp2.runtime,​,​"​ />
 + <​attribute name="​Bundle-Name"​ value="​JP2"​ />
 + <​attribute name="​Bundle-Description"​ value="​Java Instrument Library."​ />
 + <​attribute name="​Bundle-Version"​ value="​1.0"​ />
 + <​attribute name="​System-Bundle"​ value="​yes"​ />
 + </​manifest>​
 + </​jar>​
 + </​target>​
 +Add the JP2 bundle to​
 +<​code>​ \
 + ​reference:​file:​lib/​bundle/​bundle-jp2.jar
 +==== Telling SIP Communicator Bundles about JP2 ====
 +  * Add all the packages of JP2 to the Import-Package attribute of the MANIFEST.MF of all other bundles.
 +  * Set org.osgi.framework.bootdelegation to JP2 classes in the execute script. The original JP2 jars in -XBootclasspath are still needed by -javaagent.
 +javabin=`which java`
 +COMMAND="​$javabin -Xmx2g \
 +    -javaagent:​$LIBPATH/​javaagent.jar \
 +    -Xbootclasspath/​p:​$LIBPATH/​Thread_JP2.jar:​$LIBPATH/​asm-all-3.3.jar:​$LIBPATH/​jp2.jar \
 +    -Dch.usi.dag.jp2.Output="​sip.output.gz"​ \
 +    -Dch.usi.dag.jp2.InstrumentedClasses="​sipInstrumented"​ \
 +    -Dch.usi.dag.jp2.UninstrumentedClasses="​sipUninstrumented"​ \
 +    -Dch.usi.dag.jp2.InsterestedPackage="​"​ -classpath $CLASSPATH -Dorg.osgi.framework.bootdelegation=ch.usi.* \
 +    -Djna.library.path=$LIBPATH/​native​$FELIX_CONFIG -Djava.util.logging.config.file=$LOG_CONFIG \   
 +# set add LIBPATH to LD_LIBRARY_PATH for any sc natives (e.g. jmf .so's)
 +# make LD_PRELOAD use libaoss so that java doesn'​t hog on the audio device.
 +export LD_PRELOAD=/​usr/​lib/​
 +# create .sip-commnicator/​log in home or otherwise java.util.logging will freak
 +mkdir -p $HOME/​.sip-communicator/​log
 +cd $SCDIR
 +exec $COMMAND $*
 +Run this script to execute the instrumented SIP communicator.
using-jp2-to-instrument-programs-based-on-apache-felix.1299040672.txt.gz ยท Last modified: 2017/09/06 01:54 (external edit)