test/jdk/jdk/internal/platform/docker/MetricsMemoryTester.java
author bobv
Tue, 03 Jul 2018 10:59:18 -0400
changeset 50972 51e49f77f7eb
parent 50545 292a4a87c321
child 54217 98a01c1344aa
permissions -rw-r--r--
8205928: [TESTBUG] jdk/internal/platform/docker/TestDockerMemoryMetrics Reviewed-by: stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     1
/*
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     4
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     8
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    13
 * accompanied this code).
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    14
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    18
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    21
 * questions.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    22
 */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    23
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    24
import java.util.Arrays;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    25
import jdk.internal.platform.Metrics;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    26
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    27
public class MetricsMemoryTester {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    28
    public static void main(String[] args) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    29
        System.out.println(Arrays.toString(args));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    30
        switch (args[0]) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    31
            case "memory":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    32
                testMemoryLimit(args[1]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    33
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    34
            case "memoryswap":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    35
                testMemoryAndSwapLimit(args[1], args[2]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    36
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    37
            case "kernelmem":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    38
                testKernelMemoryLimit(args[1]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    39
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    40
            case "oomkill":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    41
                testOomKillFlag(Boolean.parseBoolean(args[2]));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    42
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    43
            case "failcount":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    44
                testMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    45
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    46
            case "softlimit":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    47
                testMemorySoftLimit(args[1]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    48
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    49
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    50
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    51
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    52
    private static void testMemoryLimit(String value) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    53
        long limit = getMemoryValue(value);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    54
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    55
        if (limit != Metrics.systemMetrics().getMemoryLimit()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    56
            throw new RuntimeException("Memory limit not equal, expected : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    57
                    + limit + "]" + ", got : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    58
                    + Metrics.systemMetrics().getMemoryLimit() + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    59
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    60
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    61
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    62
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    63
    private static void testMemoryFailCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    64
        long count = Metrics.systemMetrics().getMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    65
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    66
        // Allocate 512M of data
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    67
        long[][] longs = new long[64][];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    68
        for (int i = 1; i <= 64; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    69
            try {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    70
                longs[i] = new long[8 * 1024 * 1024];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    71
            } catch (Error e) { // OOM error
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    72
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    73
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    74
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    75
        if (Metrics.systemMetrics().getMemoryFailCount() <= count) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    76
            throw new RuntimeException("Memory fail count : new : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    77
                    + Metrics.systemMetrics().getMemoryFailCount() + "]"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    78
                    + ", old : [" + count + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    79
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    80
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    81
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    82
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    83
    private static void testMemorySoftLimit(String softLimit) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    84
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    85
        long memorySoftLimit = Metrics.systemMetrics().getMemorySoftLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    86
        long newmemorySoftLimit = getMemoryValue(softLimit);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    87
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    88
        if (newmemorySoftLimit != memorySoftLimit) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    89
            throw new RuntimeException("Memory softlimit not equal, Actual : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    90
                    + newmemorySoftLimit + "]" + ", Expected : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    91
                    + memorySoftLimit + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    92
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    93
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    94
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    95
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    96
    private static void testKernelMemoryLimit(String value) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    97
        long limit = getMemoryValue(value);
50972
51e49f77f7eb 8205928: [TESTBUG] jdk/internal/platform/docker/TestDockerMemoryMetrics
bobv
parents: 50545
diff changeset
    98
        long kmemlimit = Metrics.systemMetrics().getKernelMemoryLimit();
51e49f77f7eb 8205928: [TESTBUG] jdk/internal/platform/docker/TestDockerMemoryMetrics
bobv
parents: 50545
diff changeset
    99
        if (kmemlimit != 0 && limit != kmemlimit) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   100
            throw new RuntimeException("Kernel Memory limit not equal, expected : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   101
                    + limit + "]" + ", got : ["
50972
51e49f77f7eb 8205928: [TESTBUG] jdk/internal/platform/docker/TestDockerMemoryMetrics
bobv
parents: 50545
diff changeset
   102
                    + kmemlimit + "]");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   103
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   104
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   105
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   106
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   107
    private static void testMemoryAndSwapLimit(String memory, String memAndSwap) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   108
        long expectedMem = getMemoryValue(memory);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   109
        long expectedMemAndSwap = getMemoryValue(memAndSwap);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   110
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   111
        if (expectedMem != Metrics.systemMetrics().getMemoryLimit()
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   112
                || expectedMemAndSwap != Metrics.systemMetrics().getMemoryAndSwapLimit()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   113
            System.err.println("Memory and swap limit not equal, expected : ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   114
                    + expectedMem + ", " + expectedMemAndSwap + "]"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   115
                    + ", got : [" + Metrics.systemMetrics().getMemoryLimit()
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   116
                    + ", " + Metrics.systemMetrics().getMemoryAndSwapLimit() + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   117
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   118
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   119
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   120
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   121
    private static long getMemoryValue(String value) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   122
        long result;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   123
        if (value.endsWith("m")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   124
            result = Long.parseLong(value.substring(0, value.length() - 1))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   125
                    * 1024 * 1024;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   126
        } else if (value.endsWith("g")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   127
            result = Long.parseLong(value.substring(0, value.length() - 1))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   128
                    * 1024 * 1024 * 1024;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   129
        } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   130
            result = Long.parseLong(value);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   131
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   132
        return result;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   133
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   134
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   135
    private static void testOomKillFlag(boolean oomKillFlag) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   136
        if (!(oomKillFlag ^ Metrics.systemMetrics().isMemoryOOMKillEnabled())) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   137
            throw new RuntimeException("oomKillFlag error");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   138
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   139
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   140
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   141
}