src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java
author mbaesken
Thu, 25 Jul 2019 12:15:27 +0200
changeset 57535 51286afcbf49
parent 54814 207b7bf04f49
permissions -rw-r--r--
8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes) Reviewed-by: dholmes, sgehwolf
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: 54577
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.  Oracle designates this
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    10
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    15
 * accompanied this code).
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    16
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    20
 *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    23
 * questions.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    24
 */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    25
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    26
package jdk.internal.platform.cgroupv1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    27
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    28
import java.io.BufferedReader;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    29
import java.io.IOException;
50989
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
    30
import java.math.BigInteger;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    31
import java.nio.file.Files;
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.ArrayList;
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
    34
import java.util.List;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    35
import java.util.Optional;
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
    36
import java.util.function.Function;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    37
import java.util.stream.Stream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    38
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    39
public class SubSystem {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    40
    String root;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    41
    String mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    42
    String path;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    43
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    44
    public SubSystem(String root, String mountPoint) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    45
        this.root = root;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    46
        this.mountPoint = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    47
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    48
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    49
    public void setPath(String cgroupPath) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    50
        if (root != null && cgroupPath != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    51
            if (root.equals("/")) {
54176
2786541e4f91 8220579: [Containers] SubSystem.java out of sync with osContainer_linux.cpp
sgehwolf
parents: 50989
diff changeset
    52
                if (!cgroupPath.equals("/")) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    53
                    path = mountPoint + cgroupPath;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    54
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    55
                else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    56
                    path = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    57
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    58
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    59
            else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    60
                if (root.equals(cgroupPath)) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    61
                    path = mountPoint;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    62
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    63
                else {
54813
0fe908af327e 8221340: [TESTBUG] TestCgroupMetrics.java fails after fix for JDK-8219562
bobv
parents: 54577
diff changeset
    64
                    if (cgroupPath.startsWith(root)) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    65
                        if (cgroupPath.length() > root.length()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    66
                            String cgroupSubstr = cgroupPath.substring(root.length());
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    67
                            path = mountPoint + cgroupSubstr;
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
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    72
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    73
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    74
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    75
    public String path() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    76
        return path;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    77
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    78
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    79
    /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    80
     * getSubSystemStringValue
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    81
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    82
     * Return the first line of the file "parm" argument from the subsystem.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    83
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    84
     * TODO:  Consider using weak references for caching BufferedReader object.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    85
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    86
     * @param subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    87
     * @param parm
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    88
     * @return Returns the contents of the file specified by param.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    89
     */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    90
    public static String getStringValue(SubSystem subsystem, String parm) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    91
        if (subsystem == null) return null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    92
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    93
        try(BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(subsystem.path(), parm))) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    94
            String line = bufferedReader.readLine();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    95
            return line;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    96
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    97
        catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    98
            return null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    99
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   100
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   101
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   102
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   103
    public static long getLongValueMatchingLine(SubSystem subsystem,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   104
                                                     String param,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   105
                                                     String match,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   106
                                                     Function<String, Long> conversion) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   107
        long retval = Metrics.unlimited_minimum + 1; // default unlimited
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   108
        try {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   109
            List<String> lines = Files.readAllLines(Paths.get(subsystem.path(), param));
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   110
            for (String line: lines) {
54814
207b7bf04f49 8222533: jtreg test jdk/internal/platform/cgroup/TestCgroupMetrics.java fails on SLES12.3 linux ppc64le machine
bobv
parents: 54813
diff changeset
   111
                if (line.startsWith(match)) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   112
                    retval = conversion.apply(line);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   113
                    break;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   114
                }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   115
            }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   116
        } catch (IOException e) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   117
            // Ignore. Default is unlimited.
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   118
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   119
        return retval;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   120
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   121
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   122
    public static long getLongValue(SubSystem subsystem, String parm) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   123
        String strval = getStringValue(subsystem, parm);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   124
        return convertStringToLong(strval);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   125
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   126
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   127
    public static long convertStringToLong(String strval) {
50989
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   128
        long retval = 0;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   129
        if (strval == null) return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   130
50989
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   131
        try {
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   132
            retval = Long.parseLong(strval);
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   133
        } catch (NumberFormatException e) {
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   134
            // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
57535
51286afcbf49 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
mbaesken
parents: 54814
diff changeset
   135
            // In this case, return Long.MAX_VALUE
50989
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   136
            BigInteger b = new BigInteger(strval);
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   137
            if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   138
                return Long.MAX_VALUE;
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   139
            }
3bc865cc2122 8206243: java -XshowSettings fails if memory.limit_in_bytes overflows LONG.max
stuefe
parents: 50545
diff changeset
   140
        }
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   141
        return retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   142
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   143
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   144
    public static double getDoubleValue(SubSystem subsystem, String parm) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   145
        String strval = getStringValue(subsystem, parm);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   146
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   147
        if (strval == null) return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   148
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   149
        double retval = Double.parseDouble(strval);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   150
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   151
        return retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   152
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   153
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   154
    /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   155
     * getSubSystemlongEntry
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   156
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   157
     * Return the long value from the line containing the string "entryname"
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   158
     * within file "parm" in the "subsystem".
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   159
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   160
     * TODO:  Consider using weak references for caching BufferedReader object.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   161
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   162
     * @param subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   163
     * @param parm
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   164
     * @param entryname
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   165
     * @return long value
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   166
     */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   167
    public static long getLongEntry(SubSystem subsystem, String parm, String entryname) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   168
        String val = null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   169
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   170
        if (subsystem == null) return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   171
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   172
        try (Stream<String> lines = Files.lines(Paths.get(subsystem.path(), parm))) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   173
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   174
            Optional<String> result = lines.map(line -> line.split(" "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   175
                                           .filter(line -> (line.length == 2 &&
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   176
                                                   line[0].equals(entryname)))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   177
                                           .map(line -> line[1])
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   178
                                           .findFirst();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   179
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   180
            return result.isPresent() ? Long.parseLong(result.get()) : 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   181
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   182
        catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   183
            return 0L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   184
        }
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
    public static int getIntValue(SubSystem subsystem, String parm) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   188
        String val = getStringValue(subsystem, parm);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   189
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   190
        if (val == null) return 0;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   191
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   192
        return Integer.parseInt(val);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   193
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   194
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   195
    /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   196
     * StringRangeToIntArray
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   197
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   198
     * Convert a string in the form of  1,3-4,6 to an array of
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   199
     * integers containing all the numbers in the range.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   200
     *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   201
     * @param range
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   202
     * @return int[] containing a sorted list of processors or memory nodes
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   203
     */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   204
    public static int[] StringRangeToIntArray(String range) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   205
        int[] ints = new int[0];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   206
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   207
        if (range == null) return ints;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   208
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   209
        ArrayList<Integer> results = new ArrayList<>();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   210
        String strs[] = range.split(",");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   211
        for (String str : strs) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   212
            if (str.contains("-")) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   213
                String lohi[] = str.split("-");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   214
                // validate format
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   215
                if (lohi.length != 2) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   216
                    continue;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   217
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   218
                int lo = Integer.parseInt(lohi[0]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   219
                int hi = Integer.parseInt(lohi[1]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   220
                for (int i = lo; i <= hi; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   221
                    results.add(i);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   222
                }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   223
            }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   224
            else {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   225
                results.add(Integer.parseInt(str));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   226
            }
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
        // sort results
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   230
        results.sort(null);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   231
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   232
        // convert ArrayList to primitive int array
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   233
        ints = new int[results.size()];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   234
        int i = 0;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   235
        for (Integer n : results) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   236
            ints[i++] = n;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   237
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   238
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   239
        return ints;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   240
    }
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   241
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   242
    public static class MemorySubSystem extends SubSystem {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   243
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   244
        private boolean hierarchical;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   245
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   246
        public MemorySubSystem(String root, String mountPoint) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   247
            super(root, mountPoint);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   248
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   249
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   250
        boolean isHierarchical() {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   251
            return hierarchical;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   252
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   253
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   254
        void setHierarchical(boolean hierarchical) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   255
            this.hierarchical = hierarchical;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   256
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   257
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54176
diff changeset
   258
    }
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   259
}