Reviewers: alph, loislo,

Description:
Save heap object tracking data in heap snapshot

Every time embedder calls v8::HeapProfiler::GetHeapStats we store next
unuassigned heap object id and timestamp of the request. This patch serializes
all that data into heap snapshot so that embedder can restore allocation
timeline.

BUG=chromium:467222
LOG=Y

Please review this at https://codereview.chromium.org/1019813004/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+56, -7 lines):
  M src/heap-snapshot-generator.h
  M src/heap-snapshot-generator.cc


Index: src/heap-snapshot-generator.cc
diff --git a/src/heap-snapshot-generator.cc b/src/heap-snapshot-generator.cc
index 5864ad9b43fb9e82cf55b8e35a0bd0a3749d3ee7..ce9d705cd5e5730a2424a5ac87963474bd623191 100644
--- a/src/heap-snapshot-generator.cc
+++ b/src/heap-snapshot-generator.cc
@@ -2743,6 +2743,11 @@ void HeapSnapshotJSONSerializer::SerializeImpl() {
   if (writer_->aborted()) return;
   writer_->AddString("],\n");

+  writer_->AddString("\"samples\":[");
+  SerializeSamples();
+  if (writer_->aborted()) return;
+  writer_->AddString("],\n");
+
   writer_->AddString("\"strings\":[");
   SerializeStrings();
   if (writer_->aborted()) return;
@@ -2939,7 +2944,10 @@ void HeapSnapshotJSONSerializer::SerializeSnapshot() {
         JSON_S("function_info_index") ","
         JSON_S("count") ","
         JSON_S("size") ","
-        JSON_S("children"))));
+        JSON_S("children")) ","
+    JSON_S("sample_fields") ":" JSON_A(
+        JSON_S("timstamp_ms") ","
+        JSON_S("next_object_id"))));
 #undef JSON_S
 #undef JSON_O
 #undef JSON_A
@@ -2954,6 +2962,10 @@ void HeapSnapshotJSONSerializer::SerializeSnapshot() {
     count = tracker->function_info_list().length();
   }
   writer_->AddNumber(count);
+  writer_->AddString(",\"sample_count\":");
+  const List<HeapObjectsMap::TimeInterval>& samples =
+      snapshot_->profiler()->heap_object_map()->samples();
+  writer_->AddNumber(samples.length());
 }


@@ -3057,6 +3069,38 @@ void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() {
 }


+void HeapSnapshotJSONSerializer::SerializeSamples() {
+  const List<HeapObjectsMap::TimeInterval>& samples =
+      snapshot_->profiler()->heap_object_map()->samples();
+  if (samples.is_empty()) return;
+  base::TimeTicks start_time = samples[0].timestamp;
+  // The buffer needs space for 2 unsigned ints, 2 commas, \n and \0
+  const int kBufferSize =
+      2 * MaxDecimalDigitsIn<sizeof(
+              base::TimeDelta().InMilliseconds())>::kUnsigned  // NOLINT
+      +
+      2 + 1 + 1;
+  EmbeddedVector<char, kBufferSize> buffer;
+  bool first_entry = true;
+  for (int i = 0; i < samples.length(); i++) {
+    HeapObjectsMap::TimeInterval& sample = samples[i];
+    int buffer_pos = 0;
+    if (first_entry) {
+      first_entry = false;
+    } else {
+      buffer[buffer_pos++] = ',';
+    }
+    base::TimeDelta time_delta = sample.timestamp - start_time;
+    buffer_pos = utoa(time_delta.InMilliseconds(), buffer, buffer_pos);
+    buffer[buffer_pos++] = ',';
+    buffer_pos = utoa(sample.id, buffer, buffer_pos);
+    buffer[buffer_pos++] = '\n';
+    buffer[buffer_pos++] = '\0';
+    writer_->AddString(buffer.start());
+  }
+}
+
+
 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) {
   writer_->AddCharacter('\n');
   writer_->AddCharacter('\"');
Index: src/heap-snapshot-generator.h
diff --git a/src/heap-snapshot-generator.h b/src/heap-snapshot-generator.h
index 55d36ce946af66d4a1f6d9c94c2e72b0d724ac5a..6bb549ae8b39561245156e3ed23be1de5cce80bd 100644
--- a/src/heap-snapshot-generator.h
+++ b/src/heap-snapshot-generator.h
@@ -194,6 +194,15 @@ class HeapSnapshot {

 class HeapObjectsMap {
  public:
+  struct TimeInterval {
+    explicit TimeInterval(SnapshotObjectId id)
+        : id(id), size(0), count(0), timestamp(base::TimeTicks::Now()) {}
+    SnapshotObjectId id;
+    uint32_t size;
+    uint32_t count;
+    base::TimeTicks timestamp;
+  };
+
   explicit HeapObjectsMap(Heap* heap);

   Heap* heap() const { return heap_; }
@@ -210,6 +219,7 @@ class HeapObjectsMap {

   void StopHeapObjectsTracking();
   SnapshotObjectId PushHeapObjectsStats(OutputStream* stream);
+  const List<TimeInterval>& samples() const { return time_intervals_; }
   size_t GetUsedMemorySize() const;

   SnapshotObjectId GenerateId(v8::RetainedObjectInfo* info);
@@ -236,12 +246,6 @@ class HeapObjectsMap {
     unsigned int size;
     bool accessed;
   };
-  struct TimeInterval {
- explicit TimeInterval(SnapshotObjectId id) : id(id), size(0), count(0) { }
-    SnapshotObjectId id;
-    uint32_t size;
-    uint32_t count;
-  };

   SnapshotObjectId next_id_;
   HashMap entries_map_;
@@ -584,6 +588,7 @@ class HeapSnapshotJSONSerializer {
   void SerializeTraceTree();
   void SerializeTraceNode(AllocationTraceNode* node);
   void SerializeTraceNodeInfos();
+  void SerializeSamples();
   void SerializeString(const unsigned char* s);
   void SerializeStrings();



--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to