1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package gc.g1.humongousObjects;
25
26import jdk.test.lib.Utils;
27import sun.hotspot.WhiteBox;
28
29import java.util.LinkedList;
30import java.util.List;
31import java.util.Random;
32import java.util.stream.Collectors;
33
34/**
35 * @test TestNoAllocationsInHRegions
36 * @summary Checks that no additional allocations are made in humongous regions
37 * @requires vm.gc.G1
38 * @library /test/lib /
39 * @modules java.management java.base/jdk.internal.misc
40 * @build sun.hotspot.WhiteBox
41 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
42 *      sun.hotspot.WhiteBox$WhiteBoxPermission
43 *
44 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
45 *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0
46 *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions10.log
47 *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 10
48 *
49 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
50 *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0
51 *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions50.log
52 *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 50
53 *
54 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
55 *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0
56 *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions70.log
57 *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 70
58 */
59public class TestNoAllocationsInHRegions {
60    private static final WhiteBox WB = WhiteBox.getWhiteBox();
61    private static final Random RND = Utils.getRandomInstance();
62    private static final int G1_REGION_SIZE = WB.g1RegionSize();
63    private static final int[] HUMONGOUS_SIZES = {G1_REGION_SIZE / 2, G1_REGION_SIZE + 1, G1_REGION_SIZE * 2 + 1};
64    private static final int ALLOC_THREAD_COUNT = 5;
65
66    // We fill specified part of heap with humongous objects - we need public static to prevent escape analysis to
67    // collect this field
68    public static LinkedList<byte[]> humongousAllocations = new LinkedList<>();
69
70    private static volatile boolean shouldStop = false;
71    private static volatile Error error = null;
72
73    static class Allocator implements Runnable {
74
75        private final List<byte[]> liveObjects = new LinkedList<>();
76        private int usedMemory = 0;
77        public final Runnable[] actions;
78
79        /**
80         * Maximum size of simple allocation
81         */
82        private static final int MAX_ALLOCATION_SIZE = (int) (G1_REGION_SIZE / 2 * 0.9);
83
84        /**
85         * Maximum size of dead (i.e. one which is made unreachable right after allocation) object
86         */
87        private static final int DEAD_OBJECT_MAX_SIZE = G1_REGION_SIZE / 10;
88
89        public Allocator(int maxAllocationMemory) {
90
91            actions = new Runnable[]{
92                    // Allocation
93                    () -> {
94                        if (maxAllocationMemory - usedMemory != 0) {
95                            int arraySize = RND.nextInt(Math.min(maxAllocationMemory - usedMemory,
96                                    MAX_ALLOCATION_SIZE));
97
98                            if (arraySize != 0) {
99                                byte[] allocation = new byte[arraySize];
100                                liveObjects.add(allocation);
101                                usedMemory += arraySize;
102
103                                // Sanity check
104                                if (WB.g1IsHumongous(allocation)) {
105                                    String errorMessage = String.format("Test Bug: Byte array of size"
106                                                    + " %d is expected to be non-humongous but it is humongous",
107                                            allocation.length);
108
109                                    System.out.println(errorMessage);
110                                    error = new Error(errorMessage);
111                                    shouldStop = true;
112                                }
113
114                                // Test check
115                                if (WB.g1BelongsToHumongousRegion(WB.getObjectAddress(allocation))) {
116                                    String errorMessage = String.format("Non-humongous allocation of byte array of "
117                                            + "length %d and size %d with address %d was made in Humongous Region",
118                                            allocation.length, WB.getObjectSize(allocation),
119                                            WB.getObjectAddress(allocation));
120
121                                    System.out.println(errorMessage);
122                                    error = new Error(errorMessage);
123                                    shouldStop = true;
124                                }
125                            }
126                        }
127                    },
128
129                    // Deallocation
130                    () -> {
131                        if (liveObjects.size() != 0) {
132                            int elementNum = RND.nextInt(liveObjects.size());
133                            int shouldFree = liveObjects.get(elementNum).length;
134                            liveObjects.remove(elementNum);
135                            usedMemory -= shouldFree;
136                        }
137                    },
138
139                    // Dead object allocation
140                    () -> {
141                        int size = RND.nextInt(DEAD_OBJECT_MAX_SIZE);
142                        byte[] deadObject = new byte[size];
143                    },
144
145                    // Check
146                    () -> {
147                        List<byte[]> wrongHumongousAllocations = liveObjects.stream()
148                                .filter(WB::g1IsHumongous)
149                                .collect(Collectors.toList());
150
151                        if (wrongHumongousAllocations.size() > 0) {
152                            wrongHumongousAllocations.stream().forEach(a ->
153                                    System.out.format("Non-humongous allocation of byte array of length %d and"
154                                                    + " size %d with address %d was made in Humongous Region",
155                                            a.length, WB.getObjectSize(a), WB.getObjectAddress(a)));
156                            error = new Error("Some non-humongous allocations were made to humongous region");
157                            shouldStop = true;
158                        }
159                    }
160            };
161        }
162
163        @Override
164        public void run() {
165            while (!shouldStop) {
166                actions[RND.nextInt(actions.length)].run();
167                Thread.yield();
168            }
169        }
170    }
171
172    public static void main(String[] args) {
173        if (args.length != 2) {
174            throw new Error("Test Bug: Expected duration (in seconds) and percent of allocated regions were not "
175                    + "provided as command line argument");
176        }
177
178        // test duration
179        long duration = Integer.parseInt(args[0]) * 1000L;
180        // part of heap preallocated with humongous objects (in percents)
181        int percentOfAllocatedHeap = Integer.parseInt(args[1]);
182
183        long startTime = System.currentTimeMillis();
184
185        long initialFreeRegionsCount = WB.g1NumFreeRegions();
186        int regionsToAllocate = (int) ((double) initialFreeRegionsCount / 100.0 * percentOfAllocatedHeap);
187        long freeRegionLeft = initialFreeRegionsCount - regionsToAllocate;
188
189        System.out.println("Regions to allocate: " + regionsToAllocate + "; regions to left free: " + freeRegionLeft);
190
191        int maxMemoryPerAllocThread = (int) ((Runtime.getRuntime().freeMemory() / 100.0
192                * (100 - percentOfAllocatedHeap)) / ALLOC_THREAD_COUNT * 0.5);
193
194        System.out.println("Using " + maxMemoryPerAllocThread / 1024 + "KB for each of " + ALLOC_THREAD_COUNT
195                + " allocation threads");
196
197        while (WB.g1NumFreeRegions() > freeRegionLeft) {
198            try {
199                humongousAllocations.add(new byte[HUMONGOUS_SIZES[RND.nextInt(HUMONGOUS_SIZES.length)]]);
200            } catch (OutOfMemoryError oom) {
201                //We got OOM trying to fill heap with humongous objects
202                //It probably means that heap is fragmented which is strange since the test logic should avoid it
203                System.out.println("Warning: OOM while allocating humongous objects - it likely means "
204                        + "that heap is fragmented");
205                break;
206            }
207        }
208
209        System.out.println("Initial free regions " + initialFreeRegionsCount + "; Free regions left "
210                + WB.g1NumFreeRegions());
211
212        LinkedList<Thread> threads = new LinkedList<>();
213
214        for (int i = 0; i < ALLOC_THREAD_COUNT; i++) {
215            threads.add(new Thread(new Allocator(maxMemoryPerAllocThread)));
216        }
217
218        threads.stream().forEach(Thread::start);
219
220        while ((System.currentTimeMillis() - startTime < duration) && error == null) {
221            Thread.yield();
222        }
223
224        shouldStop = true;
225        System.out.println("Finished test");
226        if (error != null) {
227            throw error;
228        }
229    }
230}
231