File: lowmem001.java

package info (click to toggle)
openjdk-11 11.0.22%2B7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,236 kB
  • sloc: java: 5,174,754; xml: 1,192,262; cpp: 1,133,036; ansic: 461,316; javascript: 162,554; sh: 16,859; objc: 13,683; python: 4,753; asm: 3,570; makefile: 2,894; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (211 lines) | stat: -rw-r--r-- 7,396 bytes parent folder | download | duplicates (11)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (c) 2003, 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.
 *
 * 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 nsk.monitoring.stress.lowmem;

import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryType;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import nsk.share.Log;
import nsk.share.TestFailure;
import nsk.share.gc.GC;
import nsk.share.gc.ThreadedGCTest;
import nsk.share.gc.gp.GarbageProducer;
import nsk.share.gc.gp.array.ByteArrayProducer;
import nsk.share.gc.gp.classload.GeneratedClassProducer;
import nsk.monitoring.share.*;
import nsk.share.test.ExecutionController;

public class lowmem001 extends ThreadedGCTest {

    // The max heap usage after whih we free memory and restart
    static final int MAX_HEAP_USAGE = 70;
    // isOOM is used to stop allocation and free resources
    // immediately after first OOME
    // really it could be only if we didn't check usage in time
    static AtomicBoolean isOOM = new AtomicBoolean(false);
    static ArgumentHandler argHandler;
    MemoryMonitor monitor;

    public static void main(String[] args) {
        argHandler = new ArgumentHandler(args);
        GC.runTest(new lowmem001(), args);
    }

    @Override
    public void run() {
        Log log = new Log(System.out, true);
        // System.err is duplicated into buffer
        // it should be empty
        MyStream stream = new MyStream(System.err);
        System.setErr(new PrintStream(stream));

        monitor = Monitor.getMemoryMonitor(log, argHandler);
        try {
            monitor.enableMonitoring();
            monitor.updateThresholds();
            super.run();
            monitor.disableMonitoring();
        } catch (Exception e) {
            throw new TestFailure(e);
        }
        if (isOOM.get() == true) {
            log.display("The OOME happened during test");
            // We control memory at 70 %
            // each time when we want to eat 512 bytes
            // if we got OOME it is really ugly
            throw new TestFailure("OOME should not happened.");
        }
        if (!monitor.getPassedStatus()) {
            throw new TestFailure("MemoryMonitor fails. See log.");
        }
        if (!stream.isEmpty()) {
            String string = stream.getString();
            if (string.contains("java.lang.OutOfMemoryError")) {
                log.display("WARNING: The System.err contains OutOfMemory.");
                // the OOME is not error
                log.complain(string);
                return;
            }
            log.complain(string);
            throw new TestFailure("Error stream is not empty.");
        }

    }

    @Override
    protected Runnable createRunnable(int i) {
        String memory = argHandler.getTestedMemory();
        if (memory.equals(MemoryMonitor.HEAP_TYPE)) {
            return new HeapStresser();
        }
        if (memory.equals(MemoryMonitor.NONHEAP_TYPE)) {
            return new ClassStresser();
        }
        // mixed type
        return i % 2 == 0 ? new HeapStresser() : new ClassStresser();
    }

    /*
     * Simple ClassLoader is used for non-heap stressing
     * should be revised after permgen removal
     */
    class ClassStresser extends Thread {

        @Override
        public void run() {
            ExecutionController stresser = getExecutionController();
            GeneratedClassProducer gp = new GeneratedClassProducer();
            while (stresser.continueExecution()) {
                try {
                    gp.create(0);
                } catch (OutOfMemoryError e) {
                    // drop 'gc', reset Thresholds and start new iteration
                    monitor.resetThresholds(MemoryType.NON_HEAP);
                    return;
                }
            }
        }
    };

    class HeapStresser extends Thread {

        final long chunkSize = 512;
        List storage;
        GarbageProducer gp = new ByteArrayProducer();


        @Override
        public void run() {
            storage = new LinkedList();
            ExecutionController stresser = getExecutionController();
            MemoryMXBean bean = ManagementFactory.getMemoryMXBean();

            while (stresser.continueExecution()) {
                try {
                   storage.add(gp.create(chunkSize + new Object().hashCode() % 31));
                   storage.add(gp.create(chunkSize));
                   storage.remove(0);
                    if (isOOM.get() == true || !stresser.continueExecution()) {
                        stresser.finish();
                        storage = null;
                        return;
                    }
                    if (Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    // If memory is low free resources and restart
                    if (bean.getHeapMemoryUsage().getUsed()
                            > bean.getHeapMemoryUsage().getMax() * MAX_HEAP_USAGE / 100) {
                        storage = new LinkedList();
                        monitor.resetThresholds(MemoryType.HEAP);
                    }
                } catch (OutOfMemoryError e) {
                    // Let finish, the managment/memorymonitor could be
                    // corrupted after OOME
                    storage = null;
                    isOOM.set(true);
                    stresser.finish();
                    return;
                }
            }
        }
    }

    static class MyStream extends OutputStream {

        PrintStream err;

        public MyStream(PrintStream err) {
            this.err = err;
        }
        private final static int SIZE = 100000;
        private char[] value = new char[SIZE];
        private int count = 0;

        // No additional memory allocation during write
        @Override
        public synchronized void write(int b) {
            if (count < SIZE) {
                value[count++] = (char) b;
            }
            try {
                err.write(b);
            } catch (OutOfMemoryError oome) {
                isOOM.set(true);
            }
        }

        public String getString() {
            return new String(value, 0, count);
        }

        public boolean isEmpty() {
            return count == 0;
        }
    }
}