hotspot/test/serviceability/tmtools/jstat/GarbageProducerTest.java
author mchernov
Fri, 03 Feb 2017 15:45:57 +0300
changeset 43933 85ee551f3948
permissions -rw-r--r--
8170737: Not enough old space utilisation Reviewed-by: dfazunen, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43933
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     1
/*
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     4
 *
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     8
 *
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    13
 * accompanied this code).
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    14
 *
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    18
 *
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    21
 * questions.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    22
 */
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    23
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    24
import java.lang.management.ManagementFactory;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    25
import utils.GarbageProducer;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    26
import common.TmTool;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    27
import utils.JstatResults;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    28
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    29
/**
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    30
 * Base class for jstat testing which uses GarbageProducer to allocate garbage.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    31
 */
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    32
public class GarbageProducerTest {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    33
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    34
    // Iterations of measurement to get consistent value of counters and jstat.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    35
    private final static int ITERATIONS = 10;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    36
    private final static float TARGET_MEMORY_USAGE = 0.7f;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    37
    private final static float MEASUREMENT_TOLERANCE = 0.05f;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    38
    private final GarbageProducer garbageProducer;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    39
    private final TmTool<? extends JstatResults> jstatTool;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    40
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    41
    public GarbageProducerTest(TmTool<? extends JstatResults> tool) {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    42
        garbageProducer = new GarbageProducer(TARGET_MEMORY_USAGE);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    43
        // We will be running jstat tool
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    44
        jstatTool = tool;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    45
    }
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    46
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    47
    public void run() throws Exception {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    48
        // Run once and get the  results asserting that they are reasonable
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    49
        JstatResults measurement1 = jstatTool.measure();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    50
        measurement1.assertConsistency();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    51
        // Eat metaspace and heap then run the tool again and get the results  asserting that they are reasonable
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    52
        System.gc();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    53
        garbageProducer.allocateMetaspaceAndHeap();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    54
        // Collect garbage. Also update VM statistics
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    55
        System.gc();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    56
        int i = 0;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    57
        long collectionCountBefore = getCollectionCount();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    58
        JstatResults measurement2 = jstatTool.measure();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    59
        do {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    60
            System.out.println("Measurement #" + i);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    61
            long currentCounter = getCollectionCount();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    62
            // Check if GC cycle occured during measurement
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    63
            if (currentCounter == collectionCountBefore) {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    64
                measurement2.assertConsistency();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    65
                checkOldGenMeasurement(measurement2);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    66
                return;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    67
            } else {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    68
                System.out.println("GC happened during measurement.");
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    69
            }
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    70
            collectionCountBefore = getCollectionCount();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    71
            measurement2 = jstatTool.measure();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    72
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    73
        } while (i++ < ITERATIONS);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    74
        // Checking will be performed without consistency guarantee.
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    75
        checkOldGenMeasurement(measurement2);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    76
    }
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    77
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    78
    private void checkOldGenMeasurement(JstatResults measurement2) {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    79
        float oldGenAllocationRatio = garbageProducer.getOldGenAllocationRatio() - MEASUREMENT_TOLERANCE;
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    80
        // Assert that space has been utilized accordingly
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    81
        JstatResults.assertSpaceUtilization(measurement2, TARGET_MEMORY_USAGE, oldGenAllocationRatio);
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    82
    }
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    83
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    84
    private static long getCollectionCount() {
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    85
        return ManagementFactory.getGarbageCollectorMXBeans().stream()
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    86
                .mapToLong(b -> b.getCollectionCount())
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    87
                .sum();
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    88
    }
85ee551f3948 8170737: Not enough old space utilisation
mchernov
parents:
diff changeset
    89
}