jdk

Форк
0
/
TestFullGCCount.java 
103 строки · 3.9 Кб
1
/*
2
 * Copyright (c) 2011, 2021, 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

24
package gc;
25

26
/**
27
 * @test TestFullGCCount.java
28
 * @bug 7072527
29
 * @summary JMM GC counters overcount in some cases
30
 * @comment Shenandoah has "ExplicitGCInvokesConcurrent" on by default
31
 * @requires !(vm.gc == "Shenandoah" & vm.opt.ExplicitGCInvokesConcurrent != false)
32
 * @comment G1 has separate counters for STW Full GC and concurrent GC.
33
 * @requires !(vm.gc.G1 & vm.opt.ExplicitGCInvokesConcurrent == true)
34
 * @requires vm.gc != "Z"
35
 * @modules java.management
36
 * @run main/othervm -Xlog:gc gc.TestFullGCCount
37
 */
38

39
import java.lang.management.GarbageCollectorMXBean;
40
import java.lang.management.ManagementFactory;
41
import java.util.ArrayList;
42
import java.util.HashMap;
43
import java.util.List;
44

45
/*
46
 * Originally for a specific failure in CMS[[keep]], this test now monitors all
47
 * collectors for double-counting of collections.
48
 */
49
public class TestFullGCCount {
50

51
    static List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
52

53
    public static void main(String[] args) {
54
        int iterations = 20;
55
        boolean failed = false;
56
        String errorMessage = "";
57
        HashMap<String, List<Long>> counts = new HashMap<>();
58

59
        // Prime the collection of count lists for all collectors.
60
        for (int i = 0; i < collectors.size(); i++) {
61
            GarbageCollectorMXBean collector = collectors.get(i);
62
            counts.put(collector.getName(), new ArrayList<>(iterations));
63
        }
64

65
        // Perform some gc, record collector counts.
66
        for (int i = 0; i < iterations; i++) {
67
            System.gc();
68
            addCollectionCount(counts, i);
69
        }
70

71
        // Check the increments:
72
        //   Old gen collectors should increase by one,
73
        //   New collectors may or may not increase.
74
        //   Any increase >=2 is unexpected.
75
        for (String collector : counts.keySet()) {
76
            System.out.println("Checking: " + collector);
77

78
            for (int i = 0; i < iterations - 1; i++) {
79
                List<Long> theseCounts = counts.get(collector);
80
                long a = theseCounts.get(i);
81
                long b = theseCounts.get(i + 1);
82
                if (b - a >= 2) {
83
                    failed = true;
84
                    errorMessage += "Collector '" + collector + "' has increment " + (b - a) +
85
                                    " at iteration " + i + "\n";
86
                }
87
            }
88
        }
89
        if (failed) {
90
            System.err.println(errorMessage);
91
            throw new RuntimeException("FAILED: System.gc collections miscounted.");
92
        }
93
        System.out.println("Passed.");
94
    }
95

96
    private static void addCollectionCount(HashMap<String, List<Long>> counts, int iteration) {
97
        for (int i = 0; i < collectors.size(); i++) {
98
            GarbageCollectorMXBean collector = collectors.get(i);
99
            List<Long> thisList = counts.get(collector.getName());
100
            thisList.add(collector.getCollectionCount());
101
        }
102
    }
103
}
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.