test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java
author sgehwolf
Fri, 12 Jul 2019 19:37:25 +0200
changeset 55746 709913d8ace9
parent 55484 1cd4d287839b
child 57535 51286afcbf49
permissions -rw-r--r--
8227642: [TESTBUG] Make docker tests podman compatible Reviewed-by: mseledtsov, iignatyev
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;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    29
import java.nio.file.Files;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    30
import java.nio.file.Path;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    31
import java.nio.file.Paths;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    32
import java.util.Arrays;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    33
import java.util.HashMap;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    34
import java.util.HashSet;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    35
import java.util.Map;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    36
import java.util.Scanner;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    37
import java.util.Set;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    38
import java.util.stream.Collectors;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    39
import java.util.stream.IntStream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    40
import java.util.stream.LongStream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    41
import java.util.stream.Stream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    42
import jdk.internal.platform.Metrics;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    43
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    44
public class MetricsTester {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    45
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    46
    private static final double ERROR_MARGIN = 0.1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    47
    private static long unlimited_minimum = 0x7FFFFFFFFF000000L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    48
    long startSysVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    49
    long startUserVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    50
    long startUsage;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    51
    long startPerCpu[];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    52
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    53
    enum SubSystem {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    54
        MEMORY("memory"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    55
        CPUSET("cpuset"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    56
        CPU("cpu"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    57
        CPUACCT("cpuacct"),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    58
        BLKIO("blkio");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    59
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    60
        private String value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    61
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    62
        SubSystem(String value) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    63
            this.value = value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    64
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    65
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    66
        public String value() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    67
            return value;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    68
        }
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
    private static final Set<String> allowedSubSystems =
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    72
            Stream.of(SubSystem.values()).map(SubSystem::value).collect(Collectors.toSet());
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    73
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    74
    private static final Map<String, String[]> subSystemPaths = new HashMap<>();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    75
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    76
    private static void setPath(String[] line) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    77
        String cgroupPath = line[2];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    78
        String[] subSystems = line[1].split(",");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    79
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    80
        for (String subSystem : subSystems) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    81
            if (allowedSubSystems.contains(subSystem)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    82
                String[] paths = subSystemPaths.get(subSystem);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    83
                String finalPath = "";
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    84
                String root = paths[0];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    85
                String mountPoint = paths[1];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    86
                if (root != null && cgroupPath != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    87
                    if (root.equals("/")) {
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
    88
                        if (!cgroupPath.equals("/")) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    89
                            finalPath = mountPoint + cgroupPath;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    90
                        } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    91
                            finalPath = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    92
                        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    93
                    } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    94
                        if (root.equals(cgroupPath)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    95
                            finalPath = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    96
                        } else {
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
    97
                            if (cgroupPath.startsWith(root)) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    98
                                if (cgroupPath.length() > root.length()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    99
                                    String cgroupSubstr = cgroupPath.substring(root.length());
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   100
                                    finalPath = mountPoint + cgroupSubstr;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   101
                                }
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
                }
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 51162
diff changeset
   106
                subSystemPaths.put(subSystem, new String[]{finalPath, mountPoint});
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   107
            }
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
    private static void createSubsystems(String[] line) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   112
        if (line.length < 5) return;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   113
        Path p = Paths.get(line[4]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   114
        String subsystemName = p.getFileName().toString();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   115
        if (subsystemName != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   116
            for (String subSystem : subsystemName.split(",")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   117
                if (allowedSubSystems.contains(subSystem)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   118
                    subSystemPaths.put(subSystem, new String[]{line[3], line[4]});
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
        }
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
    public void setup() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   125
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   126
        // Initialize CPU usage metrics before we do any testing.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   127
        startSysVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   128
        startUserVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   129
        startUsage = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   130
        startPerCpu = metrics.getPerCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   131
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   132
        try {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   133
            Stream<String> lines = Files.lines(Paths.get("/proc/self/mountinfo"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   134
            lines.filter(line -> line.contains(" - cgroup cgroup "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   135
                    .map(line -> line.split(" "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   136
                    .forEach(MetricsTester::createSubsystems);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   137
            lines.close();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   138
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   139
            lines = Files.lines(Paths.get("/proc/self/cgroup"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   140
            lines.map(line -> line.split(":"))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   141
                    .filter(line -> (line.length >= 3))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   142
                    .forEach(MetricsTester::setPath);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   143
            lines.close();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   144
        } catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   145
        }
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
    private static String getFileContents(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   149
        String fname = subSystemPaths.get(subSystem.value())[0] + File.separator + fileName;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   150
        try {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   151
            return new Scanner(new File(fname)).useDelimiter("\\Z").next();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   152
        } 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
   153
            System.err.println("Unable to open : " + fname);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   154
            return "";
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   155
        }
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
    private static long getLongValueFromFile(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   159
        String data = getFileContents(subSystem, fileName);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   160
        return data.isEmpty() ? 0L : Long.parseLong(data);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   161
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   162
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   163
    private static long getLongValueFromFile(SubSystem subSystem, String metric, String subMetric) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   164
        String stats = getFileContents(subSystem, metric);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   165
        String[] tokens = stats.split("[\\r\\n]+");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   166
        for (int i = 0; i < tokens.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   167
            if (tokens[i].startsWith(subMetric)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   168
                return Long.parseLong(tokens[i].split("\\s+")[1]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   169
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   170
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   171
        return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   172
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   173
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   174
    private static double getDoubleValueFromFile(SubSystem subSystem, String fileName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   175
        String data = getFileContents(subSystem, fileName);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   176
        return data.isEmpty() ? 0.0 : Double.parseDouble(data);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   177
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   178
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   179
    private boolean compareWithErrorMargin(long oldVal, long newVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   180
        return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   181
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   182
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   183
    private boolean compareWithErrorMargin(double oldVal, double newVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   184
        return Math.abs(oldVal - newVal) <= Math.abs(oldVal * ERROR_MARGIN);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   185
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   186
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   187
    private static void fail(SubSystem system, String metric, long oldVal, long testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   188
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   189
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   190
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   191
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   192
    private static void fail(SubSystem system, String metric, String oldVal, String testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   193
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   194
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   195
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   196
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   197
    private static void fail(SubSystem system, String metric, double oldVal, double testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   198
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   199
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
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 static void fail(SubSystem system, String metric, boolean oldVal, boolean testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   203
        throw new RuntimeException("Test failed for - " + system.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   204
                + metric + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   205
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   206
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   207
    private static void warn(SubSystem system, String metric, long oldVal, long testVal) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   208
        System.err.println("Warning - " + system.value + ":" + metric
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   209
                + ", expected [" + oldVal + "], got [" + testVal + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   210
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   211
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   212
    public void testMemorySubsystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   213
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   214
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   215
        // User Memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   216
        long oldVal = metrics.getMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   217
        long newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   218
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   219
            fail(SubSystem.MEMORY, "memory.failcnt", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   220
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   221
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   222
        oldVal = metrics.getMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   223
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   224
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   225
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   226
            fail(SubSystem.MEMORY, "memory.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   227
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   228
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   229
        oldVal = metrics.getMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   230
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   231
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   232
            fail(SubSystem.MEMORY, "memory.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   233
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   234
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   235
        oldVal = metrics.getMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   236
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.usage_in_bytes");
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.usage_in_bytes", 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
        // Kernel memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   242
        oldVal = metrics.getKernelMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   243
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.failcnt");
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.kmem.failcnt", 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.getKernelMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   249
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   250
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   251
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   252
            fail(SubSystem.MEMORY, "memory.kmem.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   253
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   254
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   255
        oldVal = metrics.getKernelMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   256
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   257
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   258
            fail(SubSystem.MEMORY, "memory.kmem.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   259
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   260
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   261
        oldVal = metrics.getKernelMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   262
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.usage_in_bytes");
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.usage_in_bytes", 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
        //TCP Memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   268
        oldVal = metrics.getTcpMemoryFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   269
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.failcnt");
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.tcp.failcnt", 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.getTcpMemoryLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   275
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   276
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   277
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   278
            fail(SubSystem.MEMORY, "memory.kmem.tcp.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   279
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   280
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   281
        oldVal = metrics.getTcpMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   282
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   283
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   284
            fail(SubSystem.MEMORY, "memory.kmem.tcp.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   285
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   286
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   287
        oldVal = metrics.getTcpMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   288
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.kmem.tcp.usage_in_bytes");
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.usage_in_bytes", 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
        //  Memory and Swap
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   294
        oldVal = metrics.getMemoryAndSwapFailCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   295
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.failcnt");
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.memsw.failcnt", 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.getMemoryAndSwapLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   301
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   302
        newVal = newVal > unlimited_minimum ? -1L : newVal;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   303
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   304
            fail(SubSystem.MEMORY, "memory.memsw.limit_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   305
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   306
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   307
        oldVal = metrics.getMemoryAndSwapMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   308
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   309
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   310
            fail(SubSystem.MEMORY, "memory.memsw.max_usage_in_bytes", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   311
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   312
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   313
        oldVal = metrics.getMemoryAndSwapUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   314
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.memsw.usage_in_bytes");
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.usage_in_bytes", 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.getMemorySoftLimit();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   320
        newVal = getLongValueFromFile(SubSystem.MEMORY, "memory.soft_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.soft_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
        boolean oomKillEnabled = metrics.isMemoryOOMKillEnabled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   327
        boolean newOomKillEnabled = getLongValueFromFile(SubSystem.MEMORY,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   328
                "memory.oom_control", "oom_kill_disable") == 0L ? true : false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   329
        if (oomKillEnabled != newOomKillEnabled) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   330
            throw new RuntimeException("Test failed for - " + SubSystem.MEMORY.value + ":"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   331
                    + "memory.oom_control:oom_kill_disable" + ", expected ["
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   332
                    + oomKillEnabled + "], got [" + newOomKillEnabled + "]");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   333
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   334
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   335
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   336
    public void testCpuAccounting() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   337
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   338
        long oldVal = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   339
        long newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.usage");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   340
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   341
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   342
            warn(SubSystem.CPUACCT, "cpuacct.usage", 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
        Long[] newVals = Stream.of(getFileContents(SubSystem.CPUACCT, "cpuacct.usage_percpu")
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   346
                .split("\\s+"))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   347
                .map(Long::parseLong)
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   348
                .toArray(Long[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   349
        Long[] oldVals = LongStream.of(metrics.getPerCpuUsage()).boxed().toArray(Long[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   350
        for (int i = 0; i < oldVals.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   351
            if (!compareWithErrorMargin(oldVals[i], newVals[i])) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   352
                warn(SubSystem.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
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
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   356
        oldVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   357
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.stat", "user");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   358
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   359
            warn(SubSystem.CPUACCT, "cpuacct.usage - user", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   360
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   361
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   362
        oldVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   363
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpuacct.stat", "system");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   364
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   365
            warn(SubSystem.CPUACCT, "cpuacct.usage - system", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   366
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   367
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   368
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   369
    public void testCpuSchedulingMetrics() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   370
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   371
        long oldVal = metrics.getCpuPeriod();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   372
        long newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.cfs_period_us");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   373
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   374
            fail(SubSystem.CPUACCT, "cpu.cfs_period_us", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   375
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   376
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   377
        oldVal = metrics.getCpuQuota();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   378
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.cfs_quota_us");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   379
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   380
            fail(SubSystem.CPUACCT, "cpu.cfs_quota_us", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   381
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   382
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   383
        oldVal = metrics.getCpuShares();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   384
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.shares");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   385
        if (newVal == 0 || newVal == 1024) newVal = -1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   386
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   387
            fail(SubSystem.CPUACCT, "cpu.shares", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   388
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   389
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   390
        oldVal = metrics.getCpuNumPeriods();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   391
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "nr_periods");
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.stat - nr_periods", 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.getCpuNumThrottled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   397
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "nr_throttled");
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.stat - nr_throttled", 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.getCpuThrottledTime();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   403
        newVal = getLongValueFromFile(SubSystem.CPUACCT, "cpu.stat", "throttled_time");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   404
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   405
            fail(SubSystem.CPUACCT, "cpu.stat - throttled_time", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   406
        }
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
    public void testCpuSets() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   410
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   411
        Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   412
        Arrays.sort(oldVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   413
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   414
        String cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.cpus");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   415
        // Parse range string in the format 1,2-6,7
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   416
        Integer[] newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   417
            if (a.contains("-")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   418
                String[] range = a.split("-");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   419
                return IntStream.rangeClosed(Integer.parseInt(range[0]),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   420
                        Integer.parseInt(range[1])).boxed();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   421
            } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   422
                return Stream.of(Integer.parseInt(a));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   423
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   424
        }).toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   425
        Arrays.sort(newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   426
        if (Arrays.compare(oldVal, newVal) != 0) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   427
            fail(SubSystem.CPUSET, "cpuset.cpus", Arrays.toString(oldVal),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   428
                Arrays.toString(newVal));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   429
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   430
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   431
        int [] cpuSets = metrics.getEffectiveCpuSetCpus();
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   432
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   433
        // 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
   434
        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
   435
            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
   436
            Arrays.sort(oldVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   437
            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
   438
            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
   439
                if (a.contains("-")) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   440
                    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
   441
                    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
   442
                            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
   443
                } else {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   444
                    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
   445
                }
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   446
            }).toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   447
            Arrays.sort(newVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   448
            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
   449
                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
   450
                        Arrays.toString(newVal));
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   451
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   452
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   453
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   454
        oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   455
        Arrays.sort(oldVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   456
        cpusstr = getFileContents(SubSystem.CPUSET, "cpuset.mems");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   457
        newVal = Stream.of(cpusstr.split(",")).flatMap(a -> {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   458
            if (a.contains("-")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   459
                String[] range = a.split("-");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   460
                return IntStream.rangeClosed(Integer.parseInt(range[0]),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   461
                        Integer.parseInt(range[1])).boxed();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   462
            } else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   463
                return Stream.of(Integer.parseInt(a));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   464
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   465
        }).toArray(Integer[]::new);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   466
        Arrays.sort(newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   467
        if (Arrays.compare(oldVal, newVal) != 0) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   468
            fail(SubSystem.CPUSET, "cpuset.mems", Arrays.toString(oldVal),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   469
                    Arrays.toString(newVal));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   470
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   471
51162
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   472
        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
   473
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   474
        // 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
   475
        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
   476
            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
   477
            Arrays.sort(oldVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   478
            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
   479
            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
   480
                if (a.contains("-")) {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   481
                    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
   482
                    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
   483
                            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
   484
                } else {
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   485
                    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
   486
                }
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   487
            }).toArray(Integer[]::new);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   488
            Arrays.sort(newVal);
52f96cca600f 8206456: [TESTBUG] docker jtreg tests fail on systems without cpuset.effective_cpus / cpuset.effective_mem
bobv
parents: 50545
diff changeset
   489
            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
   490
                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
   491
                        Arrays.toString(newVal));
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   492
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   493
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   494
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   495
        double oldValue = metrics.getCpuSetMemoryPressure();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   496
        double newValue = getDoubleValueFromFile(SubSystem.CPUSET, "cpuset.memory_pressure");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   497
        if (!compareWithErrorMargin(oldValue, newValue)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   498
            fail(SubSystem.CPUSET, "cpuset.memory_pressure", oldValue, newValue);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   499
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   500
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   501
        boolean oldV = metrics.isCpuSetMemoryPressureEnabled();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   502
        boolean newV = getLongValueFromFile(SubSystem.CPUSET,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   503
                "cpuset.memory_pressure_enabled") == 1 ? true : false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   504
        if (oldV != newV) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   505
            fail(SubSystem.CPUSET, "cpuset.memory_pressure_enabled", oldV, newV);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   506
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   507
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   508
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   509
    public void testBlkIO() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   510
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   511
            long oldVal = metrics.getBlkIOServiceCount();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   512
        long newVal = getLongValueFromFile(SubSystem.BLKIO,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   513
                "blkio.throttle.io_service_bytes", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   514
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   515
            fail(SubSystem.BLKIO, "blkio.throttle.io_service_bytes - Total",
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   516
                    oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   517
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   518
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   519
        oldVal = metrics.getBlkIOServiced();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   520
        newVal = getLongValueFromFile(SubSystem.BLKIO, "blkio.throttle.io_serviced", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   521
        if (!compareWithErrorMargin(oldVal, newVal)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   522
            fail(SubSystem.BLKIO, "blkio.throttle.io_serviced - Total", oldVal, newVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   523
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   524
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   525
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   526
    public void testCpuConsumption() throws IOException, InterruptedException {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   527
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   528
        // make system call
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   529
        long newSysVal = metrics.getCpuSystemUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   530
        long newUserVal = metrics.getCpuUserUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   531
        long newUsage = metrics.getCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   532
        long[] newPerCpu = metrics.getPerCpuUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   533
55746
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   534
        // system/user CPU usage counters may be slowly increasing.
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   535
        // allow for equal values for a pass
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   536
        if (newSysVal < startSysVal) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   537
            fail(SubSystem.CPU, "getCpuSystemUsage", newSysVal, startSysVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   538
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   539
55746
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   540
        // system/user CPU usage counters may be slowly increasing.
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   541
        // allow for equal values for a pass
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   542
        if (newUserVal < startUserVal) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   543
            fail(SubSystem.CPU, "getCpuUserUsage", newUserVal, startUserVal);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   544
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   546
        if (newUsage <= startUsage) {
55746
709913d8ace9 8227642: [TESTBUG] Make docker tests podman compatible
sgehwolf
parents: 55484
diff changeset
   547
            fail(SubSystem.CPU, "getCpuUsage", newUsage, startUsage);
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   548
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   549
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   550
        boolean success = false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   551
        for (int i = 0; i < startPerCpu.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   552
            if (newPerCpu[i] > startPerCpu[i]) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   553
                success = true;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   554
                break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   555
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   556
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   557
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   558
        if(!success) fail(SubSystem.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   559
                Arrays.toString(startPerCpu));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   560
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   561
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   562
    public void testMemoryUsage() throws Exception {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   563
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   564
        long memoryMaxUsage = metrics.getMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   565
        long memoryUsage = metrics.getMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   566
55484
1cd4d287839b 8224502: [TESTBUG] JDK docker test TestSystemMetrics.java fails with access issues and OOM
bobv
parents: 54813
diff changeset
   567
        byte[] bb = new byte[64*1024*1024]; // 64M
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   568
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   569
        long newMemoryMaxUsage = metrics.getMemoryMaxUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   570
        long newMemoryUsage = metrics.getMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   571
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   572
        if(newMemoryMaxUsage < memoryMaxUsage) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   573
            fail(SubSystem.MEMORY, "getMemoryMaxUsage", newMemoryMaxUsage,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   574
                    memoryMaxUsage);
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(newMemoryUsage < memoryUsage) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   578
            fail(SubSystem.MEMORY, "getMemoryUsage", newMemoryUsage, memoryUsage);
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
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   582
    public static void main(String[] args) throws Exception {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   583
        // If cgroups is not configured, report success
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   584
        Metrics metrics = Metrics.systemMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   585
        if (metrics == null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   586
            System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   587
            return;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   588
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   589
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   590
        MetricsTester metricsTester = new MetricsTester();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   591
        metricsTester.setup();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   592
        metricsTester.testCpuAccounting();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   593
        metricsTester.testCpuSchedulingMetrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   594
        metricsTester.testCpuSets();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   595
        metricsTester.testMemorySubsystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   596
        metricsTester.testBlkIO();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   597
        metricsTester.testCpuConsumption();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   598
        metricsTester.testMemoryUsage();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   599
        System.out.println("TEST PASSED!!!");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   600
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   601
}