8197425: Liveset information for Old Object sample event
authoregahlin
Thu, 28 Jun 2018 15:13:39 +0200
changeset 50880 e1117321adaf
parent 50879 d90c3cbf13df
child 50881 a21cad3fa448
8197425: Liveset information for Old Object sample event Reviewed-by: mgronlun, ehelin
src/hotspot/share/jfr/leakprofiler/emitEventOperation.cpp
src/hotspot/share/jfr/leakprofiler/sampling/objectSample.hpp
src/hotspot/share/jfr/leakprofiler/sampling/objectSampler.cpp
src/hotspot/share/jfr/metadata/metadata.xml
test/jdk/jdk/jfr/event/oldobject/TestLastKnownHeapUsage.java
--- a/src/hotspot/share/jfr/leakprofiler/emitEventOperation.cpp	Thu Jun 28 15:06:55 2018 +0200
+++ b/src/hotspot/share/jfr/leakprofiler/emitEventOperation.cpp	Thu Jun 28 15:13:39 2018 +0200
@@ -216,6 +216,7 @@
   e.set_starttime(GranularTimer::start_time());
   e.set_endtime(GranularTimer::end_time());
   e.set_allocationTime(sample->allocation_time());
+  e.set_lastKnownHeapUsage(sample->heap_used_at_last_gc());
   e.set_object(object_id);
   e.set_arrayElements(array_size(*object_addr));
   e.set_root(gc_root_id);
--- a/src/hotspot/share/jfr/leakprofiler/sampling/objectSample.hpp	Thu Jun 28 15:06:55 2018 +0200
+++ b/src/hotspot/share/jfr/leakprofiler/sampling/objectSample.hpp	Thu Jun 28 15:13:39 2018 +0200
@@ -53,6 +53,7 @@
   int _index;
   size_t _span;
   size_t _allocated;
+  size_t _heap_used_at_last_gc;
   unsigned int _stack_trace_hash;
   bool _dead;
 
@@ -88,6 +89,7 @@
                    _index(0),
                    _span(0),
                    _allocated(0),
+                   _heap_used_at_last_gc(0),
                    _stack_trace_hash(0),
                    _dead(false) {}
 
@@ -164,6 +166,14 @@
     _allocation_time = Ticks(time.value());
   }
 
+  void set_heap_used_at_last_gc(size_t heap_used) {
+    _heap_used_at_last_gc = heap_used;
+  }
+
+  size_t heap_used_at_last_gc() const {
+    return _heap_used_at_last_gc;
+  }
+
   bool has_stack_trace() const {
     return stack_trace_id() != 0;
   }
--- a/src/hotspot/share/jfr/leakprofiler/sampling/objectSampler.cpp	Thu Jun 28 15:06:55 2018 +0200
+++ b/src/hotspot/share/jfr/leakprofiler/sampling/objectSampler.cpp	Thu Jun 28 15:13:39 2018 +0200
@@ -32,9 +32,10 @@
 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
 #include "jfr/support/jfrThreadLocal.hpp"
 #include "jfr/utilities/jfrTryLock.hpp"
+#include "logging/log.hpp"
+#include "memory/universe.hpp"
 #include "oops/oop.inline.hpp"
 #include "runtime/thread.hpp"
-#include "logging/log.hpp"
 
 ObjectSampler::ObjectSampler(size_t size) :
   _priority_queue(new SamplePriorityQueue(size)),
@@ -73,8 +74,6 @@
     thread->jfr_thread_local()->set_cached_stack_trace_id(stack_trace_id, stack_trace_hash);
   }
 
-  const JfrTicks allocation_time = JfrTicks::now();
-
   JfrTryLock tryLock(&_tryLock);
   if (!tryLock.has_lock()) {
     log_trace(jfr, oldobject, sampling)("Skipping old object sample due to lock contention");
@@ -114,7 +113,8 @@
   sample->set_span(allocated);
   sample->set_object((oop)obj);
   sample->set_allocated(allocated);
-  sample->set_allocation_time(allocation_time);
+  sample->set_allocation_time(JfrTicks::now());
+  sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc());
   _priority_queue->push(sample);
 }
 
--- a/src/hotspot/share/jfr/metadata/metadata.xml	Thu Jun 28 15:06:55 2018 +0200
+++ b/src/hotspot/share/jfr/metadata/metadata.xml	Thu Jun 28 15:13:39 2018 +0200
@@ -577,6 +577,7 @@
   <Event name="OldObjectSample" category="Java Application" label="Old Object Sample" description="A potential memory leak" stackTrace="true" thread="true"
     startTime="false" cutoff="true">
     <Field type="Ticks" name="allocationTime" label="Allocation Time" />
+    <Field type="ulong" contentType="bytes" name="lastKnownHeapUsage" label="Last Known Heap Usage" />
     <Field type="OldObject" name="object" label="Object" />
     <Field type="int" name="arrayElements" label="Array Elements" description="If the object is an array, the number of elements, or -1 if it is not an array" />
     <Field type="OldObjectGcRoot" name="root" label="GC Root" />
@@ -1132,4 +1133,4 @@
   <XmlContentType name="percentage" annotationType="jdk.jfr.Percentage" />
   <XmlContentType name="millis" annotationType="jdk.jfr.Timespan" annotationValue="MILLISECONDS" />
 
-</Metadata>
\ No newline at end of file
+</Metadata>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/event/oldobject/TestLastKnownHeapUsage.java	Thu Jun 28 15:13:39 2018 +0200
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.jfr.event.oldobject;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+import jdk.jfr.Recording;
+import jdk.jfr.consumer.RecordedEvent;
+import jdk.jfr.consumer.RecordingFile;
+import jdk.jfr.internal.test.WhiteBox;
+import jdk.test.lib.jfr.EventNames;
+import jdk.test.lib.jfr.Events;
+
+/*
+ * @test
+ * @key jfr
+ * @requires vm.gc == "null"
+ * @library /test/lib /test/jdk
+ * @modules jdk.jfr/jdk.jfr.internal.test
+ * @run main/othervm -XX:TLABSize=2k -XX:-FastTLABRefill jdk.jfr.event.oldobject.TestLastKnownHeapUsage
+ */
+public class TestLastKnownHeapUsage {
+
+    public final static List<Object> heap = new ArrayList<>();
+
+    public static void main(String[] args) throws Exception {
+        WhiteBox.setWriteAllObjectSamples(true);
+        long last = 0;
+        for (int i = 1; i <= 5; i++) {
+            long heapUsage = recordOldObjects(i);
+            System.out.println("Recording: " + i + " heapUsage=" + heapUsage);
+            if (heapUsage < last) {
+                throw new Exception("Unexpect decrease of heap usage");
+            }
+        }
+    }
+
+    private static long recordOldObjects(int index) throws IOException {
+        try (Recording r = new Recording()) {
+            r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
+            r.start();
+            System.gc();
+            for (int i = 0; i < 20; i++) {
+                heap.add(new byte[1_000_000]);
+            }
+            System.gc();
+            r.stop();
+            Path p = Paths.get("recording-" + index + ".jfr");
+            r.dump(p);
+            List<RecordedEvent> events = RecordingFile.readAllEvents(p);
+            Events.hasEvents(events);
+            long max = 0;
+            for (RecordedEvent e : RecordingFile.readAllEvents(p)) {
+                long heapUsage = Events.assertField(e, "lastKnownHeapUsage").atLeast(0L).below(1_000_000_000L).getValue();
+                max = Math.max(max, heapUsage);
+            }
+            return max;
+        }
+    }
+}