TestShrinkAuxiliaryData.java revision 8362:08b5dfe9bcb5
1/*
2 * Copyright (c) 2014, 2015, 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
24import jdk.test.lib.Asserts;
25import jdk.test.lib.OutputAnalyzer;
26import jdk.test.lib.Platform;
27import jdk.test.lib.ProcessTools;
28import jdk.test.lib.Utils;
29import java.io.IOException;
30import java.lang.management.ManagementFactory;
31import java.lang.management.MemoryUsage;
32import java.text.DecimalFormat;
33import java.text.DecimalFormatSymbols;
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.Collections;
37import java.util.LinkedList;
38import java.util.List;
39import sun.misc.Unsafe; // for ADDRESS_SIZE
40import sun.hotspot.WhiteBox;
41
42public class TestShrinkAuxiliaryData {
43
44    private static final int REGION_SIZE = 1024 * 1024;
45
46    private final static String[] initialOpts = new String[]{
47        "-XX:MinHeapFreeRatio=10",
48        "-XX:MaxHeapFreeRatio=11",
49        "-XX:+UseG1GC",
50        "-XX:G1HeapRegionSize=" + REGION_SIZE,
51        "-XX:-ExplicitGCInvokesConcurrent",
52        "-XX:+PrintGCDetails",
53        "-XX:+UnlockDiagnosticVMOptions",
54        "-XX:+WhiteBoxAPI",
55        "-Xbootclasspath/a:.",
56    };
57
58    private final int hotCardTableSize;
59
60    protected TestShrinkAuxiliaryData(int hotCardTableSize) {
61        this.hotCardTableSize = hotCardTableSize;
62    }
63
64    protected void test() throws Exception {
65        ArrayList<String> vmOpts = new ArrayList();
66        Collections.addAll(vmOpts, initialOpts);
67
68        int maxCacheSize = Math.max(0, Math.min(31, getMaxCacheSize()));
69        if (maxCacheSize < hotCardTableSize) {
70            System.out.format("Skiping test for %d cache size due max cache size %d",
71                    hotCardTableSize, maxCacheSize
72            );
73            return;
74        }
75
76        printTestInfo(maxCacheSize);
77
78        vmOpts.add("-XX:G1ConcRSLogCacheSize=" + hotCardTableSize);
79
80        // for 32 bits ObjectAlignmentInBytes is not a option
81        if (Platform.is32bit()) {
82            ArrayList<String> vmOptsWithoutAlign = new ArrayList(vmOpts);
83            vmOptsWithoutAlign.add(ShrinkAuxiliaryDataTest.class.getName());
84            performTest(vmOptsWithoutAlign);
85            return;
86        }
87
88        for (int alignment = 3; alignment <= 8; alignment++) {
89            ArrayList<String> vmOptsWithAlign = new ArrayList(vmOpts);
90            vmOptsWithAlign.add("-XX:ObjectAlignmentInBytes="
91                    + (int) Math.pow(2, alignment));
92            vmOptsWithAlign.add(ShrinkAuxiliaryDataTest.class.getName());
93
94            performTest(vmOptsWithAlign);
95        }
96    }
97
98    private void performTest(List<String> opts) throws Exception {
99        ProcessBuilder pb
100                = ProcessTools.createJavaProcessBuilder(true,
101                        opts.toArray(new String[opts.size()])
102                );
103
104        OutputAnalyzer output = new OutputAnalyzer(pb.start());
105        System.out.println(output.getStdout());
106        System.err.println(output.getStderr());
107        output.shouldHaveExitValue(0);
108    }
109
110    private void printTestInfo(int maxCacheSize) {
111
112        DecimalFormat grouped = new DecimalFormat("000,000");
113        DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
114        formatSymbols.setGroupingSeparator(' ');
115        grouped.setDecimalFormatSymbols(formatSymbols);
116
117        System.out.format(
118                "Test will use %s bytes of memory of %s available%n"
119                + "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
120                + "Max cache size: 2^%d = %s elements%n",
121                grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
122                grouped.format(Runtime.getRuntime().maxMemory()),
123                grouped.format(Runtime.getRuntime().maxMemory()
124                        - ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
125                Unsafe.ADDRESS_SIZE,
126                grouped.format((Runtime.getRuntime().freeMemory()
127                        - ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
128                        / Unsafe.ADDRESS_SIZE),
129                maxCacheSize,
130                grouped.format((int) Math.pow(2, maxCacheSize))
131        );
132    }
133
134    /**
135     * Detects maximum possible size of G1ConcRSLogCacheSize available for
136     * current process based on maximum available process memory size
137     *
138     * @return power of two
139     */
140    private static int getMaxCacheSize() {
141        long availableMemory = Runtime.getRuntime().freeMemory()
142                - ShrinkAuxiliaryDataTest.getMemoryUsedByTest() - 1l;
143        if (availableMemory <= 0) {
144            return 0;
145        }
146
147        long availablePointersCount = availableMemory / Unsafe.ADDRESS_SIZE;
148        return (63 - (int) Long.numberOfLeadingZeros(availablePointersCount));
149    }
150
151    static class ShrinkAuxiliaryDataTest {
152
153        public static void main(String[] args) throws IOException {
154
155            ShrinkAuxiliaryDataTest testCase = new ShrinkAuxiliaryDataTest();
156
157            if (!testCase.checkEnvApplicability()) {
158                return;
159            }
160
161            testCase.test();
162        }
163
164        /**
165         * Checks is this environment suitable to run this test
166         * - memory is enough to decommit (page size is not big)
167         * - RSet cache size is not too big
168         *
169         * @return true if test could run, false if test should be skipped
170         */
171        protected boolean checkEnvApplicability() {
172
173            int pageSize = WhiteBox.getWhiteBox().getVMPageSize();
174            System.out.println( "Page size = " + pageSize
175                    + " region size = " + REGION_SIZE
176                    + " aux data ~= " + (REGION_SIZE * 3 / 100));
177            // If auxdata size will be less than page size it wouldn't decommit.
178            // Auxiliary data size is about ~3.6% of heap size.
179            if (pageSize >= REGION_SIZE * 3 / 100) {
180                System.out.format("Skipping test for too large page size = %d",
181                       pageSize
182                );
183                return false;
184            }
185
186            if (REGION_SIZE * REGIONS_TO_ALLOCATE > Runtime.getRuntime().maxMemory()) {
187                System.out.format("Skipping test for too low available memory. "
188                        + "Need %d, available %d",
189                        REGION_SIZE * REGIONS_TO_ALLOCATE,
190                        Runtime.getRuntime().maxMemory()
191                );
192                return false;
193            }
194
195            return true;
196        }
197
198        class GarbageObject {
199
200            private final List<byte[]> payload = new ArrayList();
201            private final List<GarbageObject> ref = new LinkedList();
202
203            public GarbageObject(int size) {
204                payload.add(new byte[size]);
205            }
206
207            public void addRef(GarbageObject g) {
208                ref.add(g);
209            }
210
211            public void mutate() {
212                if (!payload.isEmpty() && payload.get(0).length > 0) {
213                    payload.get(0)[0] = (byte) (Math.random() * Byte.MAX_VALUE);
214                }
215            }
216        }
217
218        private final List<GarbageObject> garbage = new ArrayList();
219
220        public void test() throws IOException {
221
222            MemoryUsage muFull, muFree, muAuxDataFull, muAuxDataFree;
223            float auxFull, auxFree;
224
225            allocate();
226            link();
227            mutate();
228
229            muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
230            long numUsedRegions = WhiteBox.getWhiteBox().g1NumMaxRegions()
231                    - WhiteBox.getWhiteBox().g1NumFreeRegions();
232            muAuxDataFull = WhiteBox.getWhiteBox().g1AuxiliaryMemoryUsage();
233            auxFull = (float)muAuxDataFull.getUsed() / numUsedRegions;
234
235            System.out.format("Full aux data  ratio= %f, regions max= %d, used= %d\n",
236                    auxFull, WhiteBox.getWhiteBox().g1NumMaxRegions(), numUsedRegions
237            );
238
239            deallocate();
240            System.gc();
241
242            muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
243            muAuxDataFree = WhiteBox.getWhiteBox().g1AuxiliaryMemoryUsage();
244
245            numUsedRegions = WhiteBox.getWhiteBox().g1NumMaxRegions()
246                    - WhiteBox.getWhiteBox().g1NumFreeRegions();
247            auxFree = (float)muAuxDataFree.getUsed() / numUsedRegions;
248
249            System.out.format("Free aux data ratio= %f, regions max= %d, used= %d\n",
250                    auxFree, WhiteBox.getWhiteBox().g1NumMaxRegions(), numUsedRegions
251            );
252
253            Asserts.assertLessThanOrEqual(muFree.getCommitted(), muFull.getCommitted(),
254                    String.format("heap decommit failed - full > free: %d > %d",
255                            muFree.getCommitted(), muFull.getCommitted()
256                    )
257            );
258
259            System.out.format("State               used   committed\n");
260            System.out.format("Full aux data: %10d %10d\n", muAuxDataFull.getUsed(), muAuxDataFull.getCommitted());
261            System.out.format("Free aux data: %10d %10d\n", muAuxDataFree.getUsed(), muAuxDataFree.getCommitted());
262
263            // if decommited check that aux data has same ratio
264            if (muFree.getCommitted() < muFull.getCommitted()) {
265                Asserts.assertLessThanOrEqual(auxFree, auxFull,
266                        String.format("auxiliary data decommit failed - full > free: %f > %f",
267                                auxFree, auxFull
268                        )
269                );
270            }
271        }
272
273        private void allocate() {
274            for (int r = 0; r < REGIONS_TO_ALLOCATE; r++) {
275                for (int i = 0; i < NUM_OBJECTS_PER_REGION; i++) {
276                    GarbageObject g = new GarbageObject(REGION_SIZE
277                            / NUM_OBJECTS_PER_REGION);
278                    garbage.add(g);
279                }
280            }
281        }
282
283        /**
284         * Iterate through all allocated objects, and link to objects in another
285         * regions
286         */
287        private void link() {
288            for (int ig = 0; ig < garbage.size(); ig++) {
289                int regionNumber = ig / NUM_OBJECTS_PER_REGION;
290
291                for (int i = 0; i < NUM_LINKS; i++) {
292                    int regionToLink;
293                    do {
294                        regionToLink = (int) (Math.random() * REGIONS_TO_ALLOCATE);
295                    } while (regionToLink == regionNumber);
296
297                    // get random garbage object from random region
298                    garbage.get(ig).addRef(garbage.get(regionToLink
299                            * NUM_OBJECTS_PER_REGION + (int) (Math.random()
300                            * NUM_OBJECTS_PER_REGION)));
301                }
302            }
303        }
304
305        private void mutate() {
306            for (int ig = 0; ig < garbage.size(); ig++) {
307                garbage.get(ig).mutate();
308            }
309        }
310
311        private void deallocate() {
312            garbage.clear();
313            System.gc();
314        }
315
316        static long getMemoryUsedByTest() {
317            return REGIONS_TO_ALLOCATE * REGION_SIZE;
318        }
319
320        private static final int REGIONS_TO_ALLOCATE = 100;
321        private static final int NUM_OBJECTS_PER_REGION = 10;
322        private static final int NUM_LINKS = 20; // how many links create for each object
323    }
324}
325