Bug 1189881 - Move GeckoJavaSampler::getProfilerTime out of AndroidJNI.cpp. r=jchen
authorDylan Roeh <droeh@mozilla.com>
Thu, 17 Sep 2015 16:17:26 -0500
changeset 296019 e866e023dfacb82581f2c2e1fb2fbb62771b7d68
parent 296018 a565f782ec8799bc85eac3b999bf1d21814c2069
child 296020 11c096c410a73a9d23637bc73c15557802e5f409
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1189881
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1189881 - Move GeckoJavaSampler::getProfilerTime out of AndroidJNI.cpp. r=jchen
mobile/android/base/GeckoJavaSampler.java
tools/profiler/core/platform.cpp
widget/android/AndroidJNI.cpp
widget/android/GeneratedJNINatives.h
widget/android/GeneratedJNIWrappers.cpp
widget/android/GeneratedJNIWrappers.h
widget/android/moz.build
--- a/mobile/android/base/GeckoJavaSampler.java
+++ b/mobile/android/base/GeckoJavaSampler.java
@@ -17,16 +17,17 @@ import java.util.Set;
 public class GeckoJavaSampler {
     private static final String LOGTAG = "JavaSampler";
     private static Thread sSamplingThread;
     private static SamplingThread sSamplingRunnable;
     private static Thread sMainThread;
 
     // Use the same timer primitive as the profiler
     // to get a perfect sample syncing.
+    @WrapForJNI
     private static native double getProfilerTime();
 
     private static class Sample {
         public Frame[] mFrames;
         public double mTime;
         public long mJavaTime; // non-zero if Android system time is used
         public Sample(StackTraceElement[] aStack) {
             mFrames = new Frame[aStack.length];
@@ -203,11 +204,8 @@ public class GeckoJavaSampler {
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             sSamplingThread = null;
             sSamplingRunnable = null;
         }
     }
 }
-
-
-
--- a/tools/profiler/core/platform.cpp
+++ b/tools/profiler/core/platform.cpp
@@ -30,24 +30,44 @@
 #include "nsThreadUtils.h"
 #endif
 #include "ProfilerMarkers.h"
 
 #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
   #include "AndroidBridge.h"
 #endif
 
+#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
+#include "GeneratedJNINatives.h"
+#endif
+
 #ifndef SPS_STANDALONE
 #if defined(SPS_PLAT_amd64_linux) || defined(SPS_PLAT_x86_linux)
 # define USE_LUL_STACKWALK
 # include "lul/LulMain.h"
 # include "lul/platform-linux-lul.h"
 #endif
 #endif
 
+#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
+class GeckoJavaSampler : public widget::GeckoJavaSampler::Natives<GeckoJavaSampler>
+{
+private:
+  GeckoJavaSampler();
+
+public:
+  static double GetProfilerTime() {
+    if (!profiler_is_active()) {
+      return 0.0;
+    }
+    return profiler_time();
+  };
+};
+#endif
+
 mozilla::ThreadLocal<PseudoStack *> tlsPseudoStack;
 mozilla::ThreadLocal<GeckoSampler *> tlsTicker;
 mozilla::ThreadLocal<void *> tlsStackTop;
 // We need to track whether we've been initialized otherwise
 // we end up using tlsStack without initializing it.
 // Because tlsStack is totally opaque to us we can't reuse
 // it as the flag itself.
 bool stack_key_initialized;
@@ -468,16 +488,22 @@ void mozilla_sampler_init(void* stackTop
 
   // platform specific initialization
   OS::Startup();
 
 #ifndef SPS_STANDALONE
   set_stderr_callback(mozilla_sampler_log);
 #endif
 
+#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
+  if (mozilla::jni::IsAvailable()) {
+    GeckoJavaSampler::Init();
+  }
+#endif
+
   // We can't open pref so we use an environment variable
   // to know if we should trigger the profiler on startup
   // NOTE: Default
   const char *val = getenv("MOZ_PROFILER_STARTUP");
   if (!val || !*val) {
     return;
   }
 
--- a/widget/android/AndroidJNI.cpp
+++ b/widget/android/AndroidJNI.cpp
@@ -675,31 +675,31 @@ Java_org_mozilla_gecko_GeckoAppShell_com
     return nsWindow::ComputeRenderIntegrity();
 }
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_notifyFilePickerResult(JNIEnv* jenv, jclass, jstring filePath, jlong callback)
 {
     class NotifyFilePickerResultRunnable : public nsRunnable {
     public:
-        NotifyFilePickerResultRunnable(nsString& fileDir, long callback) : 
+        NotifyFilePickerResultRunnable(nsString& fileDir, long callback) :
             mFileDir(fileDir), mCallback(callback) {}
 
         NS_IMETHODIMP Run() {
             nsFilePickerCallback* handler = (nsFilePickerCallback*)mCallback;
             handler->handleResult(mFileDir);
             handler->Release();
             return NS_OK;
         }
     private:
         nsString mFileDir;
         long mCallback;
     };
     nsString path = nsJNIString(filePath, jenv);
-    
+
     nsCOMPtr<nsIRunnable> runnable =
         new NotifyFilePickerResultRunnable(path, (long)callback);
     NS_DispatchToMainThread(runnable);
 }
 
 #define MAX_LOCK_ATTEMPTS 10
 
 static bool LockWindowWithRetry(void* window, unsigned char** bits, int* width, int* height, int* format, int* stride)
@@ -762,17 +762,17 @@ Java_org_mozilla_gecko_GeckoAppShell_get
     dstHeight = mozilla::RoundUpPow2(srcHeight);
     dstSize = dstWidth * dstHeight * bpp;
 
     bitsCopy = (unsigned char*)malloc(dstSize);
     bzero(bitsCopy, dstSize);
     for (int i = 0; i < srcHeight; i++) {
         memcpy(bitsCopy + ((dstHeight - i - 1) * dstWidth * bpp), bits + (i * srcStride * bpp), srcStride * bpp);
     }
-    
+
     if (!jSurfaceBitsClass) {
         jSurfaceBitsClass = (jclass)jenv->NewGlobalRef(jenv->FindClass("org/mozilla/gecko/SurfaceBits"));
         jSurfaceBitsCtor = jenv->GetMethodID(jSurfaceBitsClass, "<init>", "()V");
 
         jSurfaceBitsWidth = jenv->GetFieldID(jSurfaceBitsClass, "width", "I");
         jSurfaceBitsHeight = jenv->GetFieldID(jSurfaceBitsClass, "height", "I");
         jSurfaceBitsFormat = jenv->GetFieldID(jSurfaceBitsClass, "format", "I");
         jSurfaceBitsBuffer = jenv->GetFieldID(jSurfaceBitsClass, "buffer", "Ljava/nio/ByteBuffer;");
@@ -847,25 +847,16 @@ Java_org_mozilla_gecko_GeckoAppShell_onS
 }
 
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_dispatchMemoryPressure(JNIEnv* jenv, jclass)
 {
     NS_DispatchMemoryPressure(MemPressure_New);
 }
 
-NS_EXPORT jdouble JNICALL
-Java_org_mozilla_gecko_GeckoJavaSampler_getProfilerTime(JNIEnv *jenv, jclass jc)
-{
-  if (!profiler_is_active()) {
-    return 0.0;
-  }
-  return profiler_time();
-}
-
 NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_gfx_NativePanZoomController_abortAnimation(JNIEnv* env, jobject instance)
 {
     APZCTreeManager *controller = nsWindow::GetAPZCTreeManager();
     if (controller) {
         // TODO: Pass in correct values for presShellId and viewId.
         controller->CancelAnimation(ScrollableLayerGuid(nsWindow::RootLayerTreeId(), 0, 0));
     }
--- a/widget/android/GeneratedJNINatives.h
+++ b/widget/android/GeneratedJNINatives.h
@@ -32,16 +32,31 @@ public:
                 ::template Wrap<&Impl::RequestNativeStack>)
     };
 };
 
 template<class Impl>
 constexpr JNINativeMethod ANRReporter::Natives<Impl>::methods[];
 
 template<class Impl>
+class GeckoJavaSampler::Natives : public mozilla::jni::NativeImpl<GeckoJavaSampler, Impl>
+{
+public:
+    static constexpr JNINativeMethod methods[] = {
+
+        mozilla::jni::MakeNativeMethod<GeckoJavaSampler::GetProfilerTime_t>(
+                mozilla::jni::NativeStub<GeckoJavaSampler::GetProfilerTime_t, Impl>
+                ::template Wrap<&Impl::GetProfilerTime>)
+    };
+};
+
+template<class Impl>
+constexpr JNINativeMethod GeckoJavaSampler::Natives<Impl>::methods[];
+
+template<class Impl>
 class GeckoThread::Natives : public mozilla::jni::NativeImpl<GeckoThread, Impl>
 {
 public:
     static constexpr JNINativeMethod methods[] = {
 
         mozilla::jni::MakeNativeMethod<GeckoThread::SpeculativeConnect_t>(
                 mozilla::jni::NativeStub<GeckoThread::SpeculativeConnect_t, Impl>
                 ::template Wrap<&Impl::SpeculativeConnect>)
--- a/widget/android/GeneratedJNIWrappers.cpp
+++ b/widget/android/GeneratedJNIWrappers.cpp
@@ -710,16 +710,19 @@ constexpr char GeckoJavaSampler::name[];
 constexpr char GeckoJavaSampler::GetFrameNameJavaProfilingWrapper_t::name[];
 constexpr char GeckoJavaSampler::GetFrameNameJavaProfilingWrapper_t::signature[];
 
 auto GeckoJavaSampler::GetFrameNameJavaProfilingWrapper(int32_t a0, int32_t a1, int32_t a2) -> mozilla::jni::String::LocalRef
 {
     return mozilla::jni::Method<GetFrameNameJavaProfilingWrapper_t>::Call(nullptr, nullptr, a0, a1, a2);
 }
 
+constexpr char GeckoJavaSampler::GetProfilerTime_t::name[];
+constexpr char GeckoJavaSampler::GetProfilerTime_t::signature[];
+
 constexpr char GeckoJavaSampler::GetSampleTimeJavaProfiling_t::name[];
 constexpr char GeckoJavaSampler::GetSampleTimeJavaProfiling_t::signature[];
 
 auto GeckoJavaSampler::GetSampleTimeJavaProfiling(int32_t a0, int32_t a1) -> double
 {
     return mozilla::jni::Method<GetSampleTimeJavaProfiling_t>::Call(nullptr, nullptr, a0, a1);
 }
 
--- a/widget/android/GeneratedJNIWrappers.h
+++ b/widget/android/GeneratedJNIWrappers.h
@@ -1690,16 +1690,31 @@ public:
         static const bool isMultithreaded = true;
         static const mozilla::jni::ExceptionMode exceptionMode =
                 mozilla::jni::ExceptionMode::ABORT;
     };
 
     static auto GetFrameNameJavaProfilingWrapper(int32_t, int32_t, int32_t) -> mozilla::jni::String::LocalRef;
 
 public:
+    struct GetProfilerTime_t {
+        typedef GeckoJavaSampler Owner;
+        typedef double ReturnType;
+        typedef double SetterType;
+        typedef mozilla::jni::Args<> Args;
+        static constexpr char name[] = "getProfilerTime";
+        static constexpr char signature[] =
+                "()D";
+        static const bool isStatic = true;
+        static const bool isMultithreaded = false;
+        static const mozilla::jni::ExceptionMode exceptionMode =
+                mozilla::jni::ExceptionMode::ABORT;
+    };
+
+public:
     struct GetSampleTimeJavaProfiling_t {
         typedef GeckoJavaSampler Owner;
         typedef double ReturnType;
         typedef double SetterType;
         typedef mozilla::jni::Args<
                 int32_t,
                 int32_t> Args;
         static constexpr char name[] = "getSampleTime";
@@ -1796,16 +1811,18 @@ public:
         static const bool isStatic = true;
         static const bool isMultithreaded = true;
         static const mozilla::jni::ExceptionMode exceptionMode =
                 mozilla::jni::ExceptionMode::ABORT;
     };
 
     static auto UnpauseJavaProfiling() -> void;
 
+public:
+    template<class Impl> class Natives;
 };
 
 class GeckoThread : public mozilla::jni::Class<GeckoThread>
 {
 public:
     typedef mozilla::jni::Ref<GeckoThread> Ref;
     typedef mozilla::jni::LocalRef<GeckoThread> LocalRef;
     typedef mozilla::jni::GlobalRef<GeckoThread> GlobalRef;
--- a/widget/android/moz.build
+++ b/widget/android/moz.build
@@ -14,16 +14,17 @@ XPIDL_SOURCES += [
 ]
 
 XPIDL_MODULE = 'widget_android'
 
 EXPORTS += [
     'AndroidBridge.h',
     'AndroidJavaWrappers.h',
     'AndroidJNIWrapper.h',
+    'GeneratedJNINatives.h',
     'GeneratedJNIWrappers.h',
 ]
 
 UNIFIED_SOURCES += [
     'AndroidBridge.cpp',
     'AndroidContentController.cpp',
     'AndroidDirectTexture.cpp',
     'AndroidGraphicBuffer.cpp',