TestEvacuationInfoEvent.java 6.8 KB
Newer Older
A
apetushkov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * Copyright (c) 2013, 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.gc.detailed;

import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.Random;

import jdk.jfr.Recording;
import jdk.jfr.consumer.RecordedEvent;
import jdk.test.lib.Asserts;
import jdk.test.lib.jfr.EventNames;
import jdk.test.lib.jfr.Events;

/**
 * @test
 * @key jfr
 *
 *
 * @library /lib /
 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:-UseFastUnorderedTimeStamps -XX:G1HeapRegionSize=1m -Xmx64m -Xmn16m -XX:+UseG1GC jdk.jfr.event.gc.detailed.TestEvacuationInfoEvent
 */
public class TestEvacuationInfoEvent {
48
    private final static String EVENT_INFO_NAME = EventNames.EvacuationInformation;
A
apetushkov 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    private final static String EVENT_FAILED_NAME = EventNames.EvacuationFailed;

    public static void main(String[] args) throws Throwable {
        final long g1HeapRegionSize = 1024 * 1024;
        Recording recording = new Recording();
        recording.enable(EVENT_INFO_NAME).withThreshold(Duration.ofMillis(0));
        recording.enable(EVENT_FAILED_NAME).withThreshold(Duration.ofMillis(0));
        recording.start();
        allocate();
        recording.stop();

        List<RecordedEvent> events = Events.fromRecording(recording);
        Asserts.assertFalse(events.isEmpty(), "No events found");
        for (RecordedEvent event : events) {
            if (!Events.isEventType(event, EVENT_INFO_NAME)) {
                continue;
            }
            System.out.println("Event: " + event);

            int setRegions = Events.assertField(event, "cSetRegions").atLeast(0).getValue();
            long setUsedAfter = Events.assertField(event, "cSetUsedAfter").atLeast(0L).getValue();
            long setUsedBefore = Events.assertField(event, "cSetUsedBefore").atLeast(setUsedAfter).getValue();
            int allocationRegions = Events.assertField(event, "allocationRegions").atLeast(0).getValue();
72 73
            long allocRegionsUsedBefore = Events.assertField(event, "allocationRegionsUsedBefore").atLeast(0L).getValue();
            long allocRegionsUsedAfter = Events.assertField(event, "allocationRegionsUsedAfter").atLeast(0L).getValue();
A
apetushkov 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            long bytesCopied = Events.assertField(event, "bytesCopied").atLeast(0L).getValue();
            int regionsFreed = Events.assertField(event, "regionsFreed").atLeast(0).getValue();

            Asserts.assertEquals(allocRegionsUsedBefore + bytesCopied, allocRegionsUsedAfter, "allocRegionsUsedBefore + bytesCopied = allocRegionsUsedAfter");
            Asserts.assertGreaterThanOrEqual(setRegions, regionsFreed, "setRegions >= regionsFreed");
            Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * allocationRegions, allocRegionsUsedAfter, "G1HeapRegionSize * allocationRegions >= allocationRegionsUsedAfter");
            Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedAfter, "G1HeapRegionSize * setRegions >= setUsedAfter");
            Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedBefore, "G1HeapRegionSize * setRegions >= setUsedBefore");
            Asserts.assertGreaterThanOrEqual(g1HeapRegionSize, allocRegionsUsedBefore, "G1HeapRegionSize >= allocRegionsUsedBefore");

            int gcId = Events.assertField(event, "gcId").getValue();
            boolean isEvacuationFailed = containsEvacuationFailed(events, gcId);
            if (isEvacuationFailed) {
                Asserts.assertGreaterThan(setUsedAfter, 0L, "EvacuationFailure -> setUsedAfter > 0");
                Asserts.assertGreaterThan(setRegions, regionsFreed, "EvacuationFailure -> setRegions > regionsFreed");
            } else {
                Asserts.assertEquals(setUsedAfter, 0L, "No EvacuationFailure -> setUsedAfter = 0");
                Asserts.assertEquals(setRegions, regionsFreed, "No EvacuationFailure -> setRegions = regionsFreed");
            }
        }
    }

    private static boolean containsEvacuationFailed(List<RecordedEvent> events, int gcId) {
        Optional<RecordedEvent> failedEvent = events.stream()
                                .filter(e -> Events.isEventType(e, EVENT_FAILED_NAME))
                                .filter(e -> gcId == (int)Events.assertField(e, "gcId").getValue())
                                .findAny();
        System.out.println("Failed event: " + (failedEvent.isPresent() ? failedEvent.get() : "None"));
        return failedEvent.isPresent();
    }

    public static DummyObject[] dummys = new DummyObject[6000];

        /**
         * Allocate memory to trigger garbage collections.
         * We want the allocated objects to have different life time, because we want both "young" and "old" objects.
         * This is done by keeping the objects in an array and step the current index by a small random number in the loop.
         * The loop will continue until we have allocated a fixed number of bytes.
         */
        private static void allocate() {
            Random r = new Random(0);
            long bytesToAllocate = 256 * 1024 * 1024;
            int currPos = 0;
            while (bytesToAllocate > 0) {
                int allocSize = 1000 + (r.nextInt(4000));
                bytesToAllocate -= allocSize;
                dummys[currPos] = new DummyObject(allocSize);

                // Skip a few positions to get different duration on the objects.
                currPos = (currPos + r.nextInt(20)) % dummys.length;
            }
            for (int c=0; c<dummys.length; c++) {
                dummys[c] = null;
            }
            System.gc();
        }

        public static class DummyObject {
            public byte[] payload;
            DummyObject(int size) {
            payload = new byte[size];
        }
    }
}