test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57943 72bc9a29fd7e
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     1
/*
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50545
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
package jdk.test.lib.containers.cgroup;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    25
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    26
import java.io.File;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    27
import java.io.FileNotFoundException;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    28
import java.io.IOException;
57647
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
    29
import java.math.BigInteger;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    30
import java.nio.file.Files;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    31
import java.nio.file.Path;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    32
import java.nio.file.Paths;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    33
import java.util.Arrays;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    34
import java.util.HashMap;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    35
import java.util.HashSet;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    36
import java.util.Map;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    37
import java.util.Scanner;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    38
import java.util.Set;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    39
import java.util.stream.Collectors;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    40
import java.util.stream.IntStream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    41
import java.util.stream.LongStream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    42
import java.util.stream.Stream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    43
import jdk.internal.platform.Metrics;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    44
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    45
public class MetricsTester {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    46
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    47
    private static final double ERROR_MARGIN = 0.1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    48
    private static long unlimited_minimum = 0x7FFFFFFFFF000000L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    49
    long startSysVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    50
    long startUserVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    51
    long startUsage;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    52
    long startPerCpu[];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    53
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    54
    enum SubSystem {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    55
        MEMORY("memory"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    56
        CPUSET("cpuset"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    57
        CPU("cpu"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    58
        CPUACCT("cpuacct"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    59
        BLKIO("blkio");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    60
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    61
        private String value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    62
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    63
        SubSystem(String value) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    64
            this.value = value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    65
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    66
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    67
        public String value() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    68
            return value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    69
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    70
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    71
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    72
    private static final Set<String> allowedSubSystems =
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    73
            Stream.of(SubSystem.values()).map(SubSystem::value).collect(Collectors.toSet());
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    74
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    75
    private static final Map<String, String[]> subSystemPaths = new HashMap<>();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    76
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    77
    private static void setPath(String[] line) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    78
        String cgroupPath = line[2];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    79
        String[] subSystems = line[1].split(",");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    80
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    81
        for (String subSystem : subSystems) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    82
            if (allowedSubSystems.contains(subSystem)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    83
                String[] paths = subSystemPaths.get(subSystem);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    84
                String finalPath = "";
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    85
                String root = paths[0];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    86
                String mountPoint = paths[1];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    87
                if (root != null && cgroupPath != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    88
                    if (root.equals("/")) {
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
    89
                        if (!cgroupPath.equals("/")) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    90
                            finalPath = mountPoint + cgroupPath;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    91
                        } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    92
                            finalPath = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    93
                        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    94
                    } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    95
                        if (root.equals(cgroupPath)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    96
                            finalPath = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    97
                        } else {
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
    98
                            if (cgroupPath.startsWith(root)) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    99
                                if (cgroupPath.length() > root.length()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   100
                                    String cgroupSubstr = cgroupPath.substring(root.length());
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   101
                                    finalPath = mountPoint + cgroupSubstr;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   102
                                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   103
                            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   104
                        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   105
                    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   106
                }
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
   107
                subSystemPaths.put(subSystem, new String[]{finalPath, mountPoint});
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   108
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   109
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   110
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   111
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   112
    private static void createSubsystems(String[] line) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   113
        if (line.length < 5) return;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   114
        Path p = Paths.get(line[4]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   115
        String subsystemName = p.getFileName().toString();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   116
        if (subsystemName != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   117
            for (String subSystem : subsystemName.split(",")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   118
                if (allowedSubSystems.contains(subSystem)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   119
                    subSystemPaths.put(subSystem, new String[]{line[3], line[4]});
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   120
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   121
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   122
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   123
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   124
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   125
    public void setup() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   126
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   127
        // Initialize CPU usage metrics before we do any testing.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   128
        startSysVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   129
        startUserVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   130
        startUsage = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   131
        startPerCpu = metrics.getPerCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   132
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   133
        try {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   134
            Stream<String> lines = Files.lines(Paths.get("/proc/self/mountinfo"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   135
            lines.filter(line -> line.contains(" - cgroup cgroup "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   136
                    .map(line -> line.split(" "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   137
                    .forEach(MetricsTester::createSubsystems);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   138
            lines.close();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   139
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   140
            lines = Files.lines(Paths.get("/proc/self/cgroup"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   141
            lines.map(line -> line.split(":"))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   142
                    .filter(line -> (line.length >= 3))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   143
                    .forEach(MetricsTester::setPath);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   144
            lines.close();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   145
        } catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   146
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   147
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   148
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   149
    private static String getFileContents(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   150
        String fname = subSystemPaths.get(subSystem.value())[0] + File.separator + fileName;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   151
        try {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   152
            return new Scanner(new File(fname)).useDelimiter("\\Z").next();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   153
        } catch (FileNotFoundException e) {
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   154
            System.err.println("Unable to open : " + fname);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   155
            return "";
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   156
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   157
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   158
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   159
    private static long getLongValueFromFile(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   160
        String data = getFileContents(subSystem, fileName);
57647
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   161
        return data.isEmpty() ? 0L : convertStringToLong(data);
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   162
    }
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   163
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   164
    private static long convertStringToLong(String strval) {
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   165
        long retval = 0;
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   166
        if (strval == null) return 0L;
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   167
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   168
        try {
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   169
            retval = Long.parseLong(strval);
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   170
        } catch (NumberFormatException e) {
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   171
            // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   172
            // In this case, return Long.MAX_VALUE
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   173
            BigInteger b = new BigInteger(strval);
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   174
            if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   175
                return Long.MAX_VALUE;
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   176
            }
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   177
        }
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   178
        return retval;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   179
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   180
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   181
    private static long getLongValueFromFile(SubSystem subSystem, String metric, String subMetric) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   182
        String stats = getFileContents(subSystem, metric);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   183
        String[] tokens = stats.split("[\\r\\n]+");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   184
        for (int i = 0; i < tokens.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   185
            if (tokens[i].startsWith(subMetric)) {
57647
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   186
                String strval = tokens[i].split("\\s+")[1];
bcc706c719d7 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 55484
diff changeset
   187
                return convertStringToLong(strval);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   188
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   189
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   190
        return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   191
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   192
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   193
    private static double getDoubleValueFromFile(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   194
        String data = getFileContents(subSystem, fileName);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   195
        return data.isEmpty() ? 0.0 : Double.parseDouble(data);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   196
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   197
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   198
    private boolean compareWithErrorMargin(long oldVal, long newVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   199
        return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   200
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   201
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   202
    private boolean compareWithErrorMargin(double oldVal, double newVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   203
        return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   204
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   205
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   206
    private static void fail(SubSystem system, String metric, long oldVal, long testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   207
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   208
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   209
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   210
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   211
    private static void fail(SubSystem system, String metric, String oldVal, String testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   212
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   213
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   214
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   215
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   216
    private static void fail(SubSystem system, String metric, double oldVal, double testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   217
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   218
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   219
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   220
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   221
    private static void fail(SubSystem system, String metric, boolean oldVal, boolean testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   222
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   223
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   224
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   225
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   226
    private static void warn(SubSystem system, String metric, long oldVal, long testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   227
        System.err.println("Warning - " + system.value + ":" + metric
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   228
                + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   229
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   230
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   231
    public void testMemorySubsystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   232
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   233
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   234
        // User Memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   235
        long oldVal = metrics.getMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   236
        long newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   237
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   238
            fail(SubSystem.MEMORY, "memory.failcnt", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   239
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   240
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   241
        oldVal = metrics.getMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   242
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   243
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   244
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   245
            fail(SubSystem.MEMORY, "memory.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   246
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   247
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   248
        oldVal = metrics.getMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   249
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   250
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   251
            fail(SubSystem.MEMORY, "memory.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   252
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   253
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   254
        oldVal = metrics.getMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   255
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   256
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   257
            fail(SubSystem.MEMORY, "memory.usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   258
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   259
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   260
        // Kernel memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   261
        oldVal = metrics.getKernelMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   262
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   263
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   264
            fail(SubSystem.MEMORY, "memory.kmem.failcnt", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   265
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   266
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   267
        oldVal = metrics.getKernelMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   268
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   269
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   270
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   271
            fail(SubSystem.MEMORY, "memory.kmem.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   272
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   273
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   274
        oldVal = metrics.getKernelMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   275
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   276
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   277
            fail(SubSystem.MEMORY, "memory.kmem.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   278
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   279
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   280
        oldVal = metrics.getKernelMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   281
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   282
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   283
            fail(SubSystem.MEMORY, "memory.kmem.usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   284
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   285
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   286
        //TCP Memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   287
        oldVal = metrics.getTcpMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   288
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   289
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   290
            fail(SubSystem.MEMORY, "memory.kmem.tcp.failcnt", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   291
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   292
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   293
        oldVal = metrics.getTcpMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   294
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   295
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   296
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   297
            fail(SubSystem.MEMORY, "memory.kmem.tcp.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   298
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   299
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   300
        oldVal = metrics.getTcpMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   301
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   302
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   303
            fail(SubSystem.MEMORY, "memory.kmem.tcp.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   304
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   305
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   306
        oldVal = metrics.getTcpMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   307
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   308
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   309
            fail(SubSystem.MEMORY, "memory.kmem.tcp.usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   310
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   311
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   312
        //  Memory and Swap
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   313
        oldVal = metrics.getMemoryAndSwapFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   314
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   315
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   316
            fail(SubSystem.MEMORY, "memory.memsw.failcnt", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   317
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   318
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   319
        oldVal = metrics.getMemoryAndSwapLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   320
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   321
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   322
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   323
            fail(SubSystem.MEMORY, "memory.memsw.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   324
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   325
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   326
        oldVal = metrics.getMemoryAndSwapMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   327
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   328
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   329
            fail(SubSystem.MEMORY, "memory.memsw.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   330
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   331
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   332
        oldVal = metrics.getMemoryAndSwapUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   333
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   334
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   335
            fail(SubSystem.MEMORY, "memory.memsw.usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   336
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   337
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   338
        oldVal = metrics.getMemorySoftLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   339
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.soft_limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   340
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   341
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   342
            fail(SubSystem.MEMORY, "memory.soft_limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   343
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   344
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   345
        boolean oomKillEnabled = metrics.isMemoryOOMKillEnabled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   346
        boolean newOomKillEnabled = getLongValueFromFile(SubSystem.MEMORY,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   347
                "memory.oom_control", "oom_kill_disable") == 0L ? true : false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   348
        if (oomKillEnabled != newOomKillEnabled) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   349
            throw new RuntimeException("Test failed for - " + SubSystem.MEMORY.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   350
                    + "memory.oom_control:oom_kill_disable" + ", expected ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   351
                    + oomKillEnabled + "], got [" + newOomKillEnabled + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   352
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   353
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   354
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   355
    public void testCpuAccounting() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   356
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   357
        long oldVal = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   358
        long newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.usage");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   359
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   360
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   361
            warn(SubSystem.CPUACCT, "cpuacct.usage", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   362
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   363
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   364
        Long[] newVals = Stream.of(getFileContents(SubSystem.CPUACCT, "cpuacct.usage_percpu")
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   365
                .split("\\s+"))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   366
                .map(Long::parseLong)
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   367
                .toArray(Long[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   368
        Long[] oldVals = LongStream.of(metrics.getPerCpuUsage()).boxed().toArray(Long[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   369
        for (int i = 0; i < oldVals.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   370
            if (!compareWithErrorMargin(oldVals[i], newVals[i])) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   371
                warn(SubSystem.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   372
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   373
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   374
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   375
        oldVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   376
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.stat", "user");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   377
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   378
            warn(SubSystem.CPUACCT, "cpuacct.usage - user", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   379
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   380
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   381
        oldVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   382
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.stat", "system");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   383
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   384
            warn(SubSystem.CPUACCT, "cpuacct.usage - system", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   385
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   386
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   387
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   388
    public void testCpuSchedulingMetrics() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   389
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   390
        long oldVal = metrics.getCpuPeriod();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   391
        long newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.cfs_period_us");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   392
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   393
            fail(SubSystem.CPUACCT, "cpu.cfs_period_us", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   394
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   395
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   396
        oldVal = metrics.getCpuQuota();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   397
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.cfs_quota_us");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   398
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   399
            fail(SubSystem.CPUACCT, "cpu.cfs_quota_us", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   400
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   401
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   402
        oldVal = metrics.getCpuShares();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   403
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.shares");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   404
        if (newVal == 0 || newVal == 1024) newVal = -1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   405
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   406
            fail(SubSystem.CPUACCT, "cpu.shares", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   407
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   408
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   409
        oldVal = metrics.getCpuNumPeriods();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   410
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "nr_periods");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   411
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   412
            fail(SubSystem.CPUACCT, "cpu.stat - nr_periods", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   413
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   414
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   415
        oldVal = metrics.getCpuNumThrottled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   416
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "nr_throttled");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   417
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   418
            fail(SubSystem.CPUACCT, "cpu.stat - nr_throttled", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   419
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   420
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   421
        oldVal = metrics.getCpuThrottledTime();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   422
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "throttled_time");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   423
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   424
            fail(SubSystem.CPUACCT, "cpu.stat - throttled_time", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   425
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   426
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   427
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   428
    public void testCpuSets() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   429
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   430
        Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   431
        Arrays.sort(oldVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   432
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   433
        String cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.cpus");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   434
        // Parse range string in the format 1,2-6,7
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   435
        Integer[] newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   436
            if (a.contains("-")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   437
                String[] range = a.split("-");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   438
                return IntStream.rangeClosed(Integer.parseInt(range[0]),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   439
                        Integer.parseInt(range[1])).boxed();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   440
            } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   441
                return Stream.of(Integer.parseInt(a));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   442
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   443
        }).toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   444
        Arrays.sort(newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   445
        if (Arrays.compare(oldVal, newVal) != 0) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   446
            fail(SubSystem.CPUSET, "cpuset.cpus", Arrays.toString(oldVal),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   447
                Arrays.toString(newVal));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   448
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   449
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   450
        int [] cpuSets = metrics.getEffectiveCpuSetCpus();
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   451
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   452
        // Skip this test if this metric is supported on this platform
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   453
        if (cpuSets.length != 0) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   454
            oldVal = Arrays.stream(cpuSets).boxed().toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   455
            Arrays.sort(oldVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   456
            cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.effective_cpus");
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   457
            newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   458
                if (a.contains("-")) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   459
                    String[] range = a.split("-");
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   460
                    return IntStream.rangeClosed(Integer.parseInt(range[0]),
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   461
                            Integer.parseInt(range[1])).boxed();
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   462
                } else {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   463
                    return Stream.of(Integer.parseInt(a));
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   464
                }
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   465
            }).toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   466
            Arrays.sort(newVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   467
            if (Arrays.compare(oldVal, newVal) != 0) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   468
                fail(SubSystem.CPUSET, "cpuset.effective_cpus", Arrays.toString(oldVal),
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   469
                        Arrays.toString(newVal));
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   470
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   471
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   472
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   473
        oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   474
        Arrays.sort(oldVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   475
        cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.mems");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   476
        newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   477
            if (a.contains("-")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   478
                String[] range = a.split("-");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   479
                return IntStream.rangeClosed(Integer.parseInt(range[0]),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   480
                        Integer.parseInt(range[1])).boxed();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   481
            } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   482
                return Stream.of(Integer.parseInt(a));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   483
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   484
        }).toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   485
        Arrays.sort(newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   486
        if (Arrays.compare(oldVal, newVal) != 0) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   487
            fail(SubSystem.CPUSET, "cpuset.mems", Arrays.toString(oldVal),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   488
                    Arrays.toString(newVal));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   489
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   490
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   491
        int [] cpuSetMems = metrics.getEffectiveCpuSetMems();
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   492
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   493
        // Skip this test if this metric is supported on this platform
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   494
        if (cpuSetMems.length != 0) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   495
            oldVal = Arrays.stream(cpuSetMems).boxed().toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   496
            Arrays.sort(oldVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   497
            cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.effective_mems");
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   498
            newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   499
                if (a.contains("-")) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   500
                    String[] range = a.split("-");
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   501
                    return IntStream.rangeClosed(Integer.parseInt(range[0]),
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   502
                            Integer.parseInt(range[1])).boxed();
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   503
                } else {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   504
                    return Stream.of(Integer.parseInt(a));
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   505
                }
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   506
            }).toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   507
            Arrays.sort(newVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   508
            if (Arrays.compare(oldVal, newVal) != 0) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   509
                fail(SubSystem.CPUSET, "cpuset.effective_mems", Arrays.toString(oldVal),
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   510
                        Arrays.toString(newVal));
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   511
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   512
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   513
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   514
        double oldValue = metrics.getCpuSetMemoryPressure();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   515
        double newValue = getDoubleValueFromFile(SubSystem.CPUSET, "cpuset.memory_pressure");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   516
        if (!compareWithErrorMargin(oldValue, newValue)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   517
            fail(SubSystem.CPUSET, "cpuset.memory_pressure", oldValue, newValue);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   518
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   519
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   520
        boolean oldV = metrics.isCpuSetMemoryPressureEnabled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   521
        boolean newV = getLongValueFromFile(SubSystem.CPUSET,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   522
                "cpuset.memory_pressure_enabled") == 1 ? true : false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   523
        if (oldV != newV) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   524
            fail(SubSystem.CPUSET, "cpuset.memory_pressure_enabled", oldV, newV);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   525
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   526
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   527
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   528
    public void testBlkIO() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   529
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   530
            long oldVal = metrics.getBlkIOServiceCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   531
        long newVal = getLongValueFromFile(SubSystem.BLKIO,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   532
                "blkio.throttle.io_service_bytes", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   533
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   534
            fail(SubSystem.BLKIO, "blkio.throttle.io_service_bytes - Total",
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   535
                    oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   536
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   537
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   538
        oldVal = metrics.getBlkIOServiced();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   539
        newVal = getLongValueFromFile(SubSystem.BLKIO, "blkio.throttle.io_serviced", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   540
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   541
            fail(SubSystem.BLKIO, "blkio.throttle.io_serviced - Total", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   542
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   543
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   544
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   545
    public void testCpuConsumption() throws IOException, InterruptedException {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   546
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   547
        // make system call
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   548
        long newSysVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   549
        long newUserVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   550
        long newUsage = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   551
        long[] newPerCpu = metrics.getPerCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   552
57675
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   553
        // system/user CPU usage counters may be slowly increasing.
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   554
        // allow for equal values for a pass
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   555
        if (newSysVal < startSysVal) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   556
            fail(SubSystem.CPU, "getCpuSystemUsage", newSysVal, startSysVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   557
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   558
57675
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   559
        // system/user CPU usage counters may be slowly increasing.
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   560
        // allow for equal values for a pass
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   561
        if (newUserVal < startUserVal) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   562
            fail(SubSystem.CPU, "getCpuUserUsage", newUserVal, startUserVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   563
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   564
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   565
        if (newUsage <= startUsage) {
57675
830619e8936c 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 57647
diff changeset
   566
            fail(SubSystem.CPU, "getCpuUsage", newUsage, startUsage);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   567
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   568
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   569
        boolean success = false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   570
        for (int i = 0; i < startPerCpu.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   571
            if (newPerCpu[i] > startPerCpu[i]) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   572
                success = true;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   573
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   574
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   575
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   576
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   577
        if(!success) fail(SubSystem.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   578
                Arrays.toString(startPerCpu));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   579
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   580
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   581
    public void testMemoryUsage() throws Exception {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   582
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   583
        long memoryMaxUsage = metrics.getMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   584
        long memoryUsage = metrics.getMemoryUsage();
57943
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   585
        long newMemoryMaxUsage = 0, newMemoryUsage = 0;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   586
57943
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   587
        // allocate memory in a loop and check more than once for new values
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   588
        // otherwise we might see seldom the effect of decreasing new memory values
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   589
        // e.g. because the system could free up memory
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   590
        byte[][] bytes = new byte[32][];
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   591
        for (int i = 0; i < 32; i++) {
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   592
            bytes[i] = new byte[8*1024*1024];
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   593
            newMemoryUsage = metrics.getMemoryUsage();
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   594
            if (newMemoryUsage > memoryUsage) {
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   595
                break;
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   596
            }
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   597
        }
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   598
        newMemoryMaxUsage = metrics.getMemoryMaxUsage();
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   599
57943
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   600
        if (newMemoryMaxUsage < memoryMaxUsage) {
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   601
            fail(SubSystem.MEMORY, "getMemoryMaxUsage", memoryMaxUsage,
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   602
                    newMemoryMaxUsage);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   603
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   604
57943
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   605
        if (newMemoryUsage < memoryUsage) {
72bc9a29fd7e 8229284: jdk/internal/platform/cgroup/TestCgroupMetrics.java fails for - memory:getMemoryUsage
mbaesken
parents: 57678
diff changeset
   606
            fail(SubSystem.MEMORY, "getMemoryUsage", memoryUsage, newMemoryUsage);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   607
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   608
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   609
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   610
    public static void main(String[] args) throws Exception {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   611
        // If cgroups is not configured, report success
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   612
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   613
        if (metrics == null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   614
            System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   615
            return;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   616
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   617
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   618
        MetricsTester metricsTester = new MetricsTester();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   619
        metricsTester.setup();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   620
        metricsTester.testCpuAccounting();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   621
        metricsTester.testCpuSchedulingMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   622
        metricsTester.testCpuSets();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   623
        metricsTester.testMemorySubsystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   624
        metricsTester.testBlkIO();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   625
        metricsTester.testCpuConsumption();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   626
        metricsTester.testMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   627
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   628
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   629
}