test/jdk/com/sun/jdi/JdbStopInNotificationThreadTest.java
author dtitov
Tue, 08 Oct 2019 09:13:08 -0700
changeset 58503 726a3945e934
permissions -rw-r--r--
8170299: Debugger does not stop inside the low memory notifications code Reviewed-by: sspitsyn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58503
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     1
/*
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     4
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     8
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    13
 * accompanied this code).
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    14
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    18
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    21
 * questions.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    22
 */
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    23
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    24
/*
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    25
 * @test
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    26
 * @summary Tests that the breakpoint in the notification listener is hit when the
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    27
 * notification thread is enabled and is not hit when the notification thread is disabled
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    28
 * (the service thread delivers the notifications in this case).
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    29
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    30
 * @library /test/lib
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    31
 * @run compile -g JdbStopInNotificationThreadTest.java
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    32
 * @run main/othervm JdbStopInNotificationThreadTest
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    33
 */
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    34
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    35
import jdk.test.lib.process.OutputAnalyzer;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    36
import lib.jdb.JdbCommand;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    37
import lib.jdb.JdbTest;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    38
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    39
import javax.management.Notification;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    40
import javax.management.NotificationEmitter;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    41
import javax.management.NotificationListener;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    42
import java.lang.management.*;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    43
import java.util.Collection;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    44
import java.util.LinkedList;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    45
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    46
class JdbStopInNotificationThreadTestTarg {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    47
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    48
    private static volatile boolean done = false;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    49
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    50
    private static final MemoryPoolMXBean tenuredGenPool =
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    51
            findTenuredGenPool();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    52
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    53
    public static void main(String[] args) throws Exception {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    54
        test(); // @1 breakpoint
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    55
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    56
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    57
    private static void test() throws Exception {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    58
        setPercentageUsageThreshold(0.1);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    59
        MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    60
        NotificationEmitter emitter = (NotificationEmitter) mbean;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    61
        emitter.addNotificationListener(new NotificationListener() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    62
            public void handleNotification(Notification n, Object hb) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    63
                System.out.println("Notification received:" + n.getType());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    64
                if (n.getType().equals(
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    65
                        MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    66
                    done = true;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    67
                    System.out.println("Notification MEMORY_THRESHOLD_EXCEEDED received:");
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    68
                    long maxMemory = tenuredGenPool.getUsage().getMax();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    69
                    long usedMemory = tenuredGenPool.getUsage().getUsed();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    70
                    System.out.println("Memory usage low!!!"); // @2 breakpoint
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    71
                    double percentageUsed = ((double) usedMemory) / maxMemory;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    72
                    System.out.println("percentageUsed = " + percentageUsed);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    73
                }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    74
            }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    75
        }, null, null);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    76
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    77
        Collection<Object[]> numbers = new LinkedList();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    78
        long counter = 0;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    79
        while (!done) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    80
            numbers.add(new Object[1000]);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    81
            counter++;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    82
            if (counter % 1000 == 0) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    83
                Thread.sleep(100);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    84
            }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    85
        }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    86
        System.out.println("Done");
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    87
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    88
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    89
    private static MemoryPoolMXBean findTenuredGenPool() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    90
        for (MemoryPoolMXBean pool :
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    91
                ManagementFactory.getMemoryPoolMXBeans()) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    92
            if (pool.getType() == MemoryType.HEAP &&
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    93
                    pool.isUsageThresholdSupported()) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    94
                return pool;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    95
            }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    96
        }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    97
        throw new RuntimeException("Could not find tenured space");
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    98
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    99
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   100
    public static void setPercentageUsageThreshold(double percentage) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   101
        if (percentage <= 0.0 || percentage > 1.0) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   102
            throw new IllegalArgumentException("Percentage not in range");
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   103
        }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   104
        System.out.println("Setting threashold for pool " + tenuredGenPool.getName() + " percentage:" + percentage);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   105
        long maxMemory = tenuredGenPool.getUsage().getMax();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   106
        long warningThreshold = (long) (maxMemory * percentage);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   107
        tenuredGenPool.setUsageThreshold(warningThreshold);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   108
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   109
}
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   110
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   111
public class JdbStopInNotificationThreadTest extends JdbTest {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   112
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   113
    private static final String DEBUGGEE_CLASS = JdbStopInNotificationThreadTestTarg.class.getName();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   114
    private static final String PATTERN1_TEMPLATE = "^Breakpoint hit: \"thread=Notification Thread\", " +
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   115
            "JdbStopInNotificationThreadTestTarg\\$1\\.handleNotification\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+System\\.out\\.println\\(\"Memory usage low!!!\"\\);.*";
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   116
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   117
    private JdbStopInNotificationThreadTest() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   118
        super(DEBUGGEE_CLASS);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   119
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   120
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   121
    public static void main(String argv[]) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   122
        new JdbStopInNotificationThreadTest().run();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   123
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   124
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   125
    @Override
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   126
    protected void runCases() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   127
        if (isNotificationThreadDisabled()) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   128
            System.out.println("Notification Thread is disabled. Skipping the test");
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   129
            return;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   130
        }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   131
        int bpLine2 = parseBreakpoints(getTestSourcePath("JdbStopInNotificationThreadTest.java"), 2).get(0);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   132
        jdb.command(JdbCommand.stopAt(DEBUGGEE_CLASS + "$1", bpLine2));
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   133
        String pattern = PATTERN1_TEMPLATE.replaceAll("%LINE_NUMBER", String.valueOf(bpLine2));
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   134
        jdb.command(JdbCommand.cont());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   135
        new OutputAnalyzer(jdb.getJdbOutput()).shouldMatch(pattern);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   136
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   137
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   138
    private boolean isNotificationThreadDisabled() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   139
        int bpLine1 = parseBreakpoints(getTestSourcePath("JdbStopInNotificationThreadTest.java"), 1).get(0);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   140
        jdb.command(JdbCommand.stopAt(DEBUGGEE_CLASS, bpLine1));
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   141
        jdb.command(JdbCommand.run());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   142
        jdb.command(JdbCommand.threads());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   143
        if (new OutputAnalyzer(jdb.getJdbOutput()).getOutput().contains("Notification Thread")) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   144
            return false;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   145
        }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   146
        return true;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   147
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   148
}