--- old/src/hotspot/share/gc/shared/collectedHeap.hpp 2017-11-21 12:05:10.550586152 -0800 +++ new/src/hotspot/share/gc/shared/collectedHeap.hpp 2017-11-21 12:05:10.250587183 -0800 @@ -146,7 +146,7 @@ static HeapWord* allocate_from_tlab_slow(Klass* klass, Thread* thread, size_t size); // Sample the allocation via HeapMonitoring. - // overflowed_words represents the number of HeapWords that went passed the + // overflowed_words represents the number of HeapWords that went past the // sampling boundary. This is used to fix the next sampling rate. static void sample_allocation(Thread* thread, HeapWord* obj, size_t size, size_t overflowed_words = 0); --- old/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp 2017-11-21 12:05:11.522582813 -0800 +++ new/src/hotspot/share/gc/shared/threadLocalAllocBuffer.hpp 2017-11-21 12:05:11.222583844 -0800 @@ -94,7 +94,6 @@ size_t remaining() { return end() == NULL ? 0 : pointer_delta(hard_end(), top()); } // Obtain the actual end of the TLAB. - HeapWord* hard_end(); void set_sample_end(); // Make parsable and release it. @@ -132,6 +131,7 @@ static size_t max_size_in_bytes() { return max_size() * BytesPerWord; } static void set_max_size(size_t max_size) { _max_size = max_size; } + HeapWord* hard_end(); HeapWord* start() const { return _start; } HeapWord* end() const { return _end; } HeapWord* top() const { return _top; } --- old/src/hotspot/share/runtime/heapMonitoring.hpp 2017-11-21 12:05:12.510579419 -0800 +++ new/src/hotspot/share/runtime/heapMonitoring.hpp 2017-11-21 12:05:12.198580491 -0800 @@ -48,10 +48,10 @@ // This is the lrand64 generator. static inline uint64_t next_random(uint64_t rnd) { const uint64_t PrngMult = 0x5DEECE66DLL; - const uint64_t prng_add = 0xB; - const uint64_t prng_mod_power = 48; - const uint64_t prng_mod_mask = right_n_bits(prng_mod_power); - return (PrngMult * rnd + prng_add) & prng_mod_mask; + const uint64_t PrngAdd = 0xB; + const uint64_t PrngModPower = 48; + const uint64_t PrngModMask = right_n_bits(PrngModPower); + return (PrngMult * rnd + PrngAdd) & PrngModMask; } static inline double fast_log2(const double & d) { --- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java 2017-11-21 12:05:13.418576299 -0800 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java 2017-11-21 12:05:13.098577398 -0800 @@ -55,6 +55,7 @@ public native static void disableSampling(); public native static boolean areSamplingStatisticsZero(); + public native static boolean statsHaveExpectedNumberSamples(int expected, int percentError); /** Do the frames provided exist in live, recent garbage, and frequent garbage. */ public native static boolean framesExistEverywhere(Frame[] frames); @@ -70,11 +71,11 @@ List frames = new ArrayList(); if (depth > 1) { createStackDepth(depth - 1, frames); - frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 72)); + frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 73)); } else { actuallyAllocate(frames); - frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 118)); - frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 75)); + frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 119)); + frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 76)); } return frames; } @@ -88,17 +89,17 @@ int sum = 0; List frames = new ArrayList(); allocate(frames); - frames.add(new Frame("allocate", "()Ljava/util/List;", "HeapMonitor.java", 90)); + frames.add(new Frame("allocate", "()Ljava/util/List;", "HeapMonitor.java", 91)); return frames; } private static void createStackDepth(int depth, List frames) { if (depth > 1) { createStackDepth(depth - 1, frames); - frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 97)); + frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 98)); } else { allocate(frames); - frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 100)); + frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 101)); } } @@ -107,8 +108,8 @@ for (int j = 0; j < 1000; j++) { sum += actuallyAllocate(frames); } - frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 118)); - frames.add(new Frame("allocate", "(Ljava/util/List;)V", "HeapMonitor.java", 108)); + frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 119)); + frames.add(new Frame("allocate", "(Ljava/util/List;)V", "HeapMonitor.java", 109)); } private static int actuallyAllocate(List frames) { --- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitor.c 2017-11-21 12:05:14.382572987 -0800 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitor.c 2017-11-21 12:05:14.090573990 -0800 @@ -560,14 +560,16 @@ } JNIEXPORT jboolean JNICALL -Java_MyPackage_HeapMonitorStatCorrectnessTest_statsHaveExpectedNumberSamples(JNIEnv *env, - jclass cls, - int expected, - int percent_error) { +Java_MyPackage_HeapMonitor_statsHaveExpectedNumberSamples(JNIEnv *env, + jclass cls, + int expected, + int percent_error) { jvmtiHeapSamplingStats stats; check_error((*jvmti)->GetHeapSamplingStats(jvmti, &stats), "Heap Sampling Statistics"); + fprintf(stderr, "Sample data count %ld, rate %ld, rate count %ld\n", + stats.sample_count, stats.sample_rate_accumulation, stats.sample_rate_count); double diff_ratio = (stats.sample_count - expected); diff_ratio = (diff_ratio < 0) ? -diff_ratio : diff_ratio; diff_ratio /= expected; --- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatCorrectnessTest.java 2017-11-21 12:05:15.450569317 -0800 +++ /dev/null 2017-11-14 21:07:17.907421775 -0800 @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2017, Google 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. - * - * 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 MyPackage; - -/** - * @test - * @build Frame HeapMonitor - * @summary Verifies the JVMTI Heap Monitor Statistics - * @compile HeapMonitorStatCorrectnessTest.java - * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatCorrectnessTest - */ - -public class HeapMonitorStatCorrectnessTest { - - // Do 100000 iterations and expect maxIteration / multiplier samples. - private static final int maxIteration = 100000; - private static int array[]; - - private native static boolean statsHaveExpectedNumberSamples(int expected, int percentError); - - private static void allocate(int size) { - for (int j = 0; j < maxIteration; j++) { - array = new int[size]; - } - } - - public static void main(String[] args) { - int sizes[] = {1000, 10000, 100000}; - - for (int currentSize : sizes) { - System.out.println("Testing size " + currentSize); - - // 111 is as good a number as any. - final int samplingMultiplier = 111; - HeapMonitor.enableSampling(samplingMultiplier * currentSize); - - if (!HeapMonitor.areSamplingStatisticsZero()) { - throw new RuntimeException("Statistics should be null to begin with."); - } - - allocate(currentSize); - - // For simplifications, we ignore the array memory usage for array internals (with the array - // sizes requested, it should be a negligible oversight). - // - // That means that with maxIterations, the loop in the method allocate requests: - // maxIterations * currentSize * 4 bytes (4 for integers) - // - // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes. - // - // Therefore, the expected sample number is: - // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize); - double expected = maxIteration; - expected *= 4; - expected /= samplingMultiplier; - - // 10% error ensures a sanity test without becoming flaky. - if (!statsHaveExpectedNumberSamples((int) expected, 10)) { - throw new RuntimeException("Statistics should show about " + expected + " samples."); - } - - HeapMonitor.disableSampling(); - } - } -} --- /dev/null 2017-11-14 21:07:17.907421775 -0800 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatArrayCorrectnessTest.java 2017-11-21 12:05:15.046570705 -0800 @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2017, Google 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. + * + * 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 MyPackage; + +/** + * @test + * @build Frame HeapMonitor + * @summary Verifies the JVMTI Heap Monitor Statistics + * @compile HeapMonitorStatArrayCorrectnessTest.java + * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatArrayCorrectnessTest + */ + +public class HeapMonitorStatArrayCorrectnessTest { + + // Do 100000 iterations and expect maxIteration / multiplier samples. + private static final int maxIteration = 100000; + private static int array[]; + + private static void allocate(int size) { + for (int j = 0; j < maxIteration; j++) { + array = new int[size]; + } + } + + public static void main(String[] args) { + int sizes[] = {1000, 10000, 100000}; + + for (int currentSize : sizes) { + System.out.println("Testing size " + currentSize); + + // 111 is as good a number as any. + final int samplingMultiplier = 111; + HeapMonitor.enableSampling(samplingMultiplier * currentSize); + + if (!HeapMonitor.areSamplingStatisticsZero()) { + throw new RuntimeException("Statistics should be null to begin with."); + } + + allocate(currentSize); + + // For simplifications, we ignore the array memory usage for array internals (with the array + // sizes requested, it should be a negligible oversight). + // + // That means that with maxIterations, the loop in the method allocate requests: + // maxIterations * currentSize * 4 bytes (4 for integers) + // + // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes. + // + // Therefore, the expected sample number is: + // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize); + double expected = maxIteration; + expected *= 4; + expected /= samplingMultiplier; + + // 10% error ensures a sanity test without becoming flaky. + if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) { + throw new RuntimeException("Statistics should show about " + expected + " samples."); + } + + HeapMonitor.disableSampling(); + } + } +} --- old/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatCorrectnessTest.java 2017-11-21 12:05:16.390566087 -0800 +++ /dev/null 2017-11-14 21:07:17.907421775 -0800 @@ -1,86 +0,0 @@ -/* - * Copyright (c) 2017, Google 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. - * - * 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 MyPackage; - -/** - * @test - * @build Frame HeapMonitor - * @summary Verifies the JVMTI Heap Monitor Statistics - * @compile HeapMonitorStatCorrectnessTest.java - * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatCorrectnessTest - */ - -public class HeapMonitorStatCorrectnessTest { - - // Do 100000 iterations and expect maxIteration / multiplier samples. - private static final int maxIteration = 100000; - private static int array[]; - - private native static boolean statsHaveExpectedNumberSamples(int expected, int percentError); - - private static void allocate(int size) { - for (int j = 0; j < maxIteration; j++) { - array = new int[size]; - } - } - - public static void main(String[] args) { - int sizes[] = {1000, 10000, 100000}; - - for (int currentSize : sizes) { - System.out.println("Testing size " + currentSize); - - // 111 is as good a number as any. - final int samplingMultiplier = 111; - HeapMonitor.enableSampling(samplingMultiplier * currentSize); - - if (!HeapMonitor.areSamplingStatisticsZero()) { - throw new RuntimeException("Statistics should be null to begin with."); - } - - allocate(currentSize); - - // For simplifications, we ignore the array memory usage for array internals (with the array - // sizes requested, it should be a negligible oversight). - // - // That means that with maxIterations, the loop in the method allocate requests: - // maxIterations * currentSize * 4 bytes (4 for integers) - // - // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes. - // - // Therefore, the expected sample number is: - // (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize); - double expected = maxIteration; - expected *= 4; - expected /= samplingMultiplier; - - // 10% error ensures a sanity test without becoming flaky. - if (!statsHaveExpectedNumberSamples((int) expected, 10)) { - throw new RuntimeException("Statistics should show about " + expected + " samples."); - } - - HeapMonitor.disableSampling(); - } - } -} --- /dev/null 2017-11-14 21:07:17.907421775 -0800 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitorStatObjectCorrectnessTest.java 2017-11-21 12:05:16.026567338 -0800 @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2017, Google 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. + * + * 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 MyPackage; + +/** + * @test + * @build Frame HeapMonitor + * @summary Verifies the JVMTI Heap Monitor Statistics + * @compile HeapMonitorStatObjectCorrectnessTest.java + * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatObjectCorrectnessTest + */ + +/** This test is checking the object allocation path works with heap sampling. */ +public class HeapMonitorStatObjectCorrectnessTest { + + // Do 100000 iterations and expect maxIteration / multiplier samples. + private static final int maxIteration = 100000; + private static BigObject obj; + + private native static boolean statsHaveExpectedNumberSamples(int expected, int percentError); + + private static void allocate() { + for (int j = 0; j < maxIteration; j++) { + obj = new BigObject(); + } + } + + public static void main(String[] args) { + final int sizeObject = 1400; + + // 111 is as good a number as any. + final int samplingMultiplier = 111; + HeapMonitor.enableSampling(samplingMultiplier * sizeObject); + + if (!HeapMonitor.areSamplingStatisticsZero()) { + throw new RuntimeException("Statistics should be null to begin with."); + } + + allocate(); + + // For simplifications, the code is allocating: + // (BigObject size) * maxIteration. + // + // We ignore the class memory usage apart from field memory usage for BigObject. BigObject + // allocates 250 long, so 2000 bytes, so whatever is used for the class is negligible. + // + // That means that with maxIterations, the loop in the method allocate requests: + // maxIterations * 2000 bytes. + // + // Via the enable sampling, the code requests a sample every samplingMultiplier * sizeObject bytes. + // + // Therefore, the expected sample number is: + // (maxIterations * sizeObject) / (samplingMultiplier * sizeObject); + // + // Which becomes: + // maxIterations / samplingMultiplier + double expected = maxIteration; + expected /= samplingMultiplier; + + // 10% error ensures a sanity test without becoming flaky. + if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) { + throw new RuntimeException("Statistics should show about " + expected + " samples."); + } + + HeapMonitor.disableSampling(); + } + + /** + * Big class on purpose to just be able to ignore the class memory space overhead. + * + * Class contains 175 long fields, so 175 * 8 = 1400 bytes. + */ + private static class BigObject { + private long a0_0, a0_1, a0_2, a0_3, a0_4, a0_5, a0_6, a0_7, a0_8, a0_9; + private long a1_0, a1_1, a1_2, a1_3, a1_4, a1_5, a1_6, a1_7, a1_8, a1_9; + private long a2_0, a2_1, a2_2, a2_3, a2_4, a2_5, a2_6, a2_7, a2_8, a2_9; + private long a3_0, a3_1, a3_2, a3_3, a3_4, a3_5, a3_6, a3_7, a3_8, a3_9; + private long a4_0, a4_1, a4_2, a4_3, a4_4, a4_5, a4_6, a4_7, a4_8, a4_9; + private long a5_0, a5_1, a5_2, a5_3, a5_4, a5_5, a5_6, a5_7, a5_8, a5_9; + private long a6_0, a6_1, a6_2, a6_3, a6_4, a6_5, a6_6, a6_7, a6_8, a6_9; + private long a7_0, a7_1, a7_2, a7_3, a7_4, a7_5, a7_6, a7_7, a7_8, a7_9; + private long a8_0, a8_1, a8_2, a8_3, a8_4, a8_5, a8_6, a8_7, a8_8, a8_9; + private long a9_0, a9_1, a9_2, a9_3, a9_4, a9_5, a9_6, a9_7, a9_8, a9_9; + private long a10_0, a10_1, a10_2, a10_3, a10_4, a10_5, a10_6, a10_7, a10_8, a10_9; + private long a11_0, a11_1, a11_2, a11_3, a11_4, a11_5, a11_6, a11_7, a11_8, a11_9; + private long a12_0, a12_1, a12_2, a12_3, a12_4, a12_5, a12_6, a12_7, a12_8, a12_9; + private long a13_0, a13_1, a13_2, a13_3, a13_4, a13_5, a13_6, a13_7, a13_8, a13_9; + private long a14_0, a14_1, a14_2, a14_3, a14_4, a14_5, a14_6, a14_7, a14_8, a14_9; + private long a15_0, a15_1, a15_2, a15_3, a15_4, a15_5, a15_6, a15_7, a15_8, a15_9; + private long a16_0, a16_1, a16_2, a16_3, a16_4, a16_5, a16_6, a16_7, a16_8, a16_9; + private long a17_0, a17_1, a17_2, a17_3, a17_4; + } +}