Bug 1449983 - The Java thread has the wrong JSON format. r?mstange draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Fri, 20 Apr 2018 10:33:26 +0900
changeset 786415 5be0676a5708d3ef510cfcc49dbf7a0c12473c49
parent 785383 ea3555cf12afff38370e7a697db81f181e15dbf6
push id107452
push userbmo:m_kato@ga2.so-net.ne.jp
push dateMon, 23 Apr 2018 07:14:47 +0000
reviewersmstange
bugs1449983
milestone61.0a1
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();