Bug 1449983 - The Java thread has the wrong JSON format. r=mstange
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 20 Apr 2018 10:33:26 +0900
changeset 471556 4a75b60fccb6c73dc37e804f38b076dbf9c4c1c4
parent 471555 dd82a78059af3c47e0961a277bf720cac3210121
child 471557 5934ceb7311587a6b096401d4b2058a2d13933d6
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs1449983
milestone61.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 1449983 - The Java thread has the wrong JSON format. r=mstange MozReview-Commit-ID: 93Kw7McCWva
tools/profiler/core/platform.cpp
--- a/tools/profiler/core/platform.cpp
+++ b/tools/profiler/core/platform.cpp
@@ -1661,67 +1661,46 @@ StreamMetaJSCustomObject(PSLockRef aLock
       }
     }
     aWriter.EndArray();
   }
   aWriter.EndObject();
 }
 
 #if defined(GP_OS_android)
-static void
-BuildJavaThreadJSObject(SpliceableJSONWriter& aWriter)
+static UniquePtr<ProfileBuffer>
+CollectJavaThreadProfileData()
 {
-  aWriter.StringProperty("name", "Java Main Thread");
-
-  aWriter.StartArrayProperty("samples");
-  {
-    for (int sampleId = 0; true; sampleId++) {
-      bool firstRun = true;
-      for (int frameId = 0; true; frameId++) {
-        jni::String::LocalRef frameName =
-            java::GeckoJavaSampler::GetFrameName(0, sampleId, frameId);
-
-        // When we run out of frames, we stop looping.
-        if (!frameName) {
-          // If we found at least one frame, we have objects to close.
-          if (!firstRun) {
-            aWriter.EndArray();
-            aWriter.EndObject();
-          }
-          break;
-        }
-        // The first time around, open the sample object and frames array.
-        if (firstRun) {
-          firstRun = false;
-
-          double sampleTime =
-              java::GeckoJavaSampler::GetSampleTime(0, sampleId);
-
-          aWriter.StartObjectElement();
-            aWriter.DoubleProperty("time", sampleTime);
-
-            aWriter.StartArrayProperty("frames");
-        }
-
-        // Add a frame to the sample.
-        aWriter.StartObjectElement();
-        {
-          aWriter.StringProperty("location",
-                                 frameName->ToCString().BeginReading());
-        }
-        aWriter.EndObject();
-      }
-
-      // If we found no frames for this sample, we are done.
-      if (firstRun) {
+  // locked_profiler_start uses sample count is 1000 for Java thread.
+  // This entry size is enough now, but we might have to estimate it
+  // if we can customize it
+  auto buffer = MakeUnique<ProfileBuffer>(1000 * 1000);
+
+  int sampleId = 0;
+  while (true) {
+    double sampleTime = java::GeckoJavaSampler::GetSampleTime(0, sampleId);
+    if (sampleTime == 0.0) {
+      break;
+    }
+
+    buffer->AddThreadIdEntry(0);
+    buffer->AddEntry(ProfileBufferEntry::Time(sampleTime));
+    int frameId = 0;
+    while (true) {
+      jni::String::LocalRef frameName =
+        java::GeckoJavaSampler::GetFrameName(0, sampleId, frameId++);
+      if (!frameName) {
         break;
       }
+      buffer->CollectCodeLocation("", frameName->ToCString().get(), -1,
+                                  Nothing());
     }
+    sampleId++;
   }
-  aWriter.EndArray();
+  return Move(buffer);
 }
 #endif
 
 static void
 locked_profiler_stream_json_for_this_process(PSLockRef aLock,
                                              SpliceableJSONWriter& aWriter,
                                              double aSinceTime,
                                              bool aIsShuttingDown)
@@ -1764,28 +1743,33 @@ locked_profiler_stream_json_for_this_pro
       JSContext* cx =
         registeredThread ? registeredThread->GetJSContext() : nullptr;
       ProfiledThreadData* profiledThreadData = thread.second();
       profiledThreadData->StreamJSON(buffer, cx, aWriter,
                                      CorePS::ProcessStartTime(), aSinceTime);
     }
 
 #if defined(GP_OS_android)
-    if (ActivePS::FeatureJava(aLock)) {
-      java::GeckoJavaSampler::Pause();
-
-      aWriter.Start();
-      {
-        BuildJavaThreadJSObject(aWriter);
-      }
-      aWriter.End();
-
-      java::GeckoJavaSampler::Unpause();
-    }
+  if (ActivePS::FeatureJava(aLock)) {
+     java::GeckoJavaSampler::Pause();
+
+     UniquePtr<ProfileBuffer> javaBuffer = CollectJavaThreadProfileData();
+
+     // Thread id of java Main thread is 0, if we support profiling of other
+     // java thread, we have to get thread id and name via JNI.
+     RefPtr<ThreadInfo> threadInfo =
+       new ThreadInfo("Java Main Thread", 0, false);
+     ProfiledThreadData profiledThreadData(threadInfo, nullptr);
+     profiledThreadData.StreamJSON(*javaBuffer.get(), nullptr, aWriter,
+                                   CorePS::ProcessStartTime(), aSinceTime);
+
+     java::GeckoJavaSampler::Unpause();
+  }
 #endif
+
   }
   aWriter.EndArray();
 
   aWriter.StartArrayProperty("pausedRanges");
   {
     buffer.StreamPausedRangesToJSON(aWriter, aSinceTime);
   }
   aWriter.EndArray();