Bug 406518 - migrate OT/Equinox to the standard OSGi WeavingHook
- profiling
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBinding.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBinding.java
index ae40b02..c64986a 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBinding.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBinding.java
@@ -31,6 +31,7 @@
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.objectteams.internal.osgi.weaving.Util.ProfileKind;
 import org.eclipse.objectteams.otequinox.ActivationKind;
 import org.objectteams.Team;
 import org.osgi.framework.Bundle;
@@ -286,6 +287,8 @@
 	 * and collect affected base classes into the teamBindings.
 	 */
 	public synchronized void scanTeamClasses(Bundle bundle) {
+		long time = 0;
+		if (Util.PROFILE) time= System.nanoTime();
 		ClassScanner scanner = new ClassScanner();
 		for (@SuppressWarnings("null")@NonNull TeamBinding team : getAllTeamBindings()) {
 			if (team.hasScannedBases) continue; // not a surprise for members of equivalentSet
@@ -303,6 +306,7 @@
 			}
 		}
 		this.hasScannedTeams = true;
+		if (Util.PROFILE) Util.profile(time, ProfileKind.Scan, bundle.getSymbolicName());
 	}
 
 	private List<TeamBinding> getAllTeamBindings() {
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
index d0aea73..c9980ed 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
@@ -29,6 +29,7 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.objectteams.internal.osgi.weaving.Util.ProfileKind;
 import org.eclipse.objectteams.otequinox.TransformerPlugin;
 import org.eclipse.objectteams.otre.jplis.ObjectTeamsTransformer;
 import org.osgi.framework.Bundle;
@@ -140,11 +141,16 @@
 				byte[] bytes = wovenClass.getBytes();
 				try {
 					log(IStatus.OK, "About to transform class "+className);
+					long time = 0;
+					if (Util.PROFILE) time= System.nanoTime();
 					byte[] newBytes = transformer.transform(bundleWiring.getBundle(),
 										className, classBeingRedefined, protectionDomain, bytes);
 					if (newBytes != bytes && !Arrays.equals(newBytes, bytes)) {
+						if (Util.PROFILE) Util.profile(time, ProfileKind.Transformation, className);
 						log(IStatus.INFO, "Transformation performed on "+className);
 						wovenClass.setBytes(newBytes);
+					} else {
+						if (Util.PROFILE) Util.profile(time, ProfileKind.NoTransformation, className);
 					}
 				} catch (IllegalClassFormatException e) {
 					log(e, "Failed to transform class "+className);
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/TeamLoader.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/TeamLoader.java
index 56525b1..adf0cd1 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/TeamLoader.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/TeamLoader.java
@@ -28,6 +28,7 @@
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.objectteams.internal.osgi.weaving.AspectBinding.TeamBinding;
+import org.eclipse.objectteams.internal.osgi.weaving.Util.ProfileKind;
 import org.eclipse.objectteams.otequinox.ActivationKind;
 import org.eclipse.objectteams.otequinox.TransformerPlugin;
 import org.objectteams.Team;
@@ -159,6 +160,9 @@
 		}
 
 		try {
+			long time = 0;
+			if (Util.PROFILE) time= System.nanoTime();
+
 			@SuppressWarnings("null")@NonNull Team instance = team.teamClass.newInstance();
 			TransformerPlugin.registerTeamInstance(instance);
 			log(IStatus.INFO, "Instantiated team "+teamName);
@@ -177,6 +181,7 @@
 				default:
 					break;
 				}
+				if (Util.PROFILE) Util.profile(time, ProfileKind.Activation, teamName);
 			} catch (Throwable t) {
 				// application errors during activation
 				log(t, "Failed to activate team "+teamName);
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/Util.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/Util.java
index 3086cec..a4a0c35 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/Util.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/Util.java
@@ -20,6 +20,7 @@
  **********************************************************************/
 package org.eclipse.objectteams.internal.osgi.weaving;
 
+import static org.eclipse.objectteams.otequinox.TransformerPlugin.doLog;
 import static org.eclipse.core.runtime.IStatus.*;
 
 public class Util 
@@ -47,18 +48,18 @@
 	}
 
 	/** Profiling data: */
-	enum ProfileKind { BaseTransformation, AspectTransformation, SuperClassFetching }
+	enum ProfileKind { Transformation, NoTransformation, Activation, Scan }
 	private static long[] profileTimes= new long[ProfileKind.values().length];
 	private static long systemStartTime= System.nanoTime();
 
 	@SuppressWarnings("nls")
-	public static void profile(long startTime, ProfileKind kind, String msg, Logger logger) 
+	public static void profile(long startTime, ProfileKind kind, String msg) 
 	{
 		long now= System.nanoTime();
-		long delta= (now-startTime) / getActiveCount();
+		long delta= (now-startTime);// / getActiveCount();
 		long total= (profileTimes[kind.ordinal()]+= delta);
 		msg = msg.substring(msg.lastIndexOf('.')+1);
-		logger.doLog(INFO, "Profile "+kind.name()+": "+m(delta)+"("+m(total)+"/"+m(now-systemStartTime)+") ["+msg+"]");
+		doLog(INFO, "Profile "+kind.name()+": "+m(delta)+"("+m(total)+"/"+m(now-systemStartTime)+") ["+msg+"]");
 	}
 	// nano-to milli conversion
 	private static double m(long l) {
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
index d33a64a..2871c68 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
@@ -45,7 +45,7 @@
 	}
 
 	@SuppressWarnings("restriction")
-	private void acquireLog(BundleContext bundleContext) {
+	private static void acquireLog(BundleContext bundleContext) {
 		try {
 			TransformerPlugin.log = org.eclipse.core.internal.runtime.InternalPlatform.getDefault().getLog(bundleContext.getBundle());
 		} catch (NullPointerException npe) {
@@ -104,8 +104,10 @@
 			doLog(status, msg);
 	}
 
-	private static void doLog(int status, String msg) {
+	public static void doLog(int status, String msg) {
 		msg = "OT/Equinox: "+msg;
+// this seems to cause java.lang.NoClassDefFoundError: org/eclipse/ui/statushandlers/StatusAdapter etc.
+//		if (log == null) acquireLog(context);
 		if (log != null) {
 			log.log(new Status(status, TRANSFORMER_PLUGIN_ID, msg));
 		} else {