src/java.base/linux/classes/jdk/internal/platform/cgroupv1/Metrics.java
author eosterlund
Thu, 07 Nov 2019 10:28:29 +0000
changeset 58961 5d462d4b7a8b
parent 54577 1c242c2d037f
permissions -rw-r--r--
8233073: Make BitMap accessors more memory ordering friendly Reviewed-by: kbarrett, pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
     1
/*
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54192
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.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.stream.Stream;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    33
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
    34
import jdk.internal.platform.cgroupv1.SubSystem.MemorySubSystem;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
    35
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    36
public class Metrics implements jdk.internal.platform.Metrics {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
    37
    private MemorySubSystem memory;
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    38
    private SubSystem cpu;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    39
    private SubSystem cpuacct;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    40
    private SubSystem cpuset;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    41
    private SubSystem blkio;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    42
    private boolean activeSubSystems;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    43
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    44
    // Values returned larger than this number are unlimited.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    45
    static long unlimited_minimum = 0x7FFFFFFFFF000000L;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    46
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    47
    private static final Metrics INSTANCE = initContainerSubSystems();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    48
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    49
    private static final String PROVIDER_NAME = "cgroupv1";
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    50
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    51
    private Metrics() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    52
        activeSubSystems = false;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    53
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    54
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    55
    public static Metrics getInstance() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    56
        return INSTANCE;
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
    private static Metrics initContainerSubSystems() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    60
        Metrics metrics = new Metrics();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    61
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    62
        /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    63
         * Find the cgroup mount points for subsystems
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    64
         * by reading /proc/self/mountinfo
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    65
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    66
         * Example for docker MemorySubSystem subsystem:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    67
         * 219 214 0:29 /docker/7208cebd00fa5f2e342b1094f7bed87fa25661471a4637118e65f1c995be8a34 /sys/fs/cgroup/MemorySubSystem ro,nosuid,nodev,noexec,relatime - cgroup cgroup rw,MemorySubSystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    68
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    69
         * Example for host:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    70
         * 34 28 0:29 / /sys/fs/cgroup/MemorySubSystem rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,MemorySubSystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    71
         */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    72
        try (Stream<String> lines =
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    73
             Files.lines(Paths.get("/proc/self/mountinfo"))) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    74
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    75
            lines.filter(line -> line.contains(" - cgroup "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    76
                 .map(line -> line.split(" "))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    77
                 .forEach(entry -> createSubSystem(metrics, entry));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    78
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    79
        } catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    80
            return null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    81
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    82
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    83
        /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    84
         * Read /proc/self/cgroup and map host mount point to
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    85
         * local one via /proc/self/mountinfo content above
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    86
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    87
         * Docker example:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    88
         * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    89
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    90
         * Host example:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    91
         * 5:memory:/user.slice
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    92
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    93
         * Construct a path to the process specific memory and cpuset
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    94
         * cgroup directory.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    95
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    96
         * For a container running under Docker from memory example above
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    97
         * the paths would be:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    98
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
    99
         * /sys/fs/cgroup/memory
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   100
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   101
         * For a Host from memory example above the path would be:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   102
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   103
         * /sys/fs/cgroup/memory/user.slice
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   104
         *
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   105
         */
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   106
        try (Stream<String> lines =
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   107
             Files.lines(Paths.get("/proc/self/cgroup"))) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   108
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   109
            lines.map(line -> line.split(":"))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   110
                 .filter(line -> (line.length >= 3))
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   111
                 .forEach(line -> setSubSystemPath(metrics, line));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   112
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   113
        } catch (IOException e) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   114
            return null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   115
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   116
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   117
        // Return Metrics object if we found any subsystems.
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   118
        if (metrics.activeSubSystems()) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   119
            return metrics;
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
        return null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   123
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   124
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   125
    /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   126
     * createSubSystem objects and initialize mount points
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   127
     */
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54192
diff changeset
   128
    private static void createSubSystem(Metrics metric, String[] mountentry) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   129
        if (mountentry.length < 5) return;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   130
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   131
        Path p = Paths.get(mountentry[4]);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54192
diff changeset
   132
        String[] subsystemNames = p.getFileName().toString().split(",");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   133
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54192
diff changeset
   134
        for (String subsystemName: subsystemNames) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   135
            switch (subsystemName) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   136
                case "memory":
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   137
                    metric.setMemorySubSystem(new MemorySubSystem(mountentry[3], mountentry[4]));
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   138
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   139
                case "cpuset":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   140
                    metric.setCpuSetSubSystem(new SubSystem(mountentry[3], mountentry[4]));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   141
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   142
                case "cpuacct":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   143
                    metric.setCpuAcctSubSystem(new SubSystem(mountentry[3], mountentry[4]));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   144
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   145
                case "cpu":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   146
                    metric.setCpuSubSystem(new SubSystem(mountentry[3], mountentry[4]));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   147
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   148
                case "blkio":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   149
                    metric.setBlkIOSubSystem(new SubSystem(mountentry[3], mountentry[4]));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   150
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   151
                default:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   152
                    // Ignore subsystems that we don't support
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   153
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   154
            }
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
    /**
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   159
     * setSubSystemPath based on the contents of /proc/self/cgroup
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   160
     */
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54192
diff changeset
   161
    private static void setSubSystemPath(Metrics metric, String[] entry) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   162
        String controller;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   163
        String base;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   164
        SubSystem subsystem = null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   165
        SubSystem subsystem2 = null;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   166
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   167
        controller = entry[1];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   168
        base = entry[2];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   169
        if (controller != null && base != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   170
            switch (controller) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   171
                case "memory":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   172
                    subsystem = metric.MemorySubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   173
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   174
                case "cpuset":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   175
                    subsystem = metric.CpuSetSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   176
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   177
                case "cpu,cpuacct":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   178
                case "cpuacct,cpu":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   179
                    subsystem = metric.CpuSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   180
                    subsystem2 = metric.CpuAcctSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   181
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   182
                case "cpuacct":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   183
                    subsystem = metric.CpuAcctSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   184
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   185
                case "cpu":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   186
                    subsystem = metric.CpuSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   187
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   188
                case "blkio":
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   189
                    subsystem = metric.BlkIOSubSystem();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   190
                    break;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   191
                // Ignore subsystems that we don't support
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   192
                default:
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   193
                    break;
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
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   197
        if (subsystem != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   198
            subsystem.setPath(base);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   199
            if (subsystem instanceof MemorySubSystem) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   200
                MemorySubSystem memorySubSystem = (MemorySubSystem)subsystem;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   201
                boolean isHierarchial = getHierarchical(memorySubSystem);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   202
                memorySubSystem.setHierarchical(isHierarchial);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   203
            }
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   204
            metric.setActiveSubSystems();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   205
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   206
        if (subsystem2 != null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   207
            subsystem2.setPath(base);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   208
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   209
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   210
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   211
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   212
    private static boolean getHierarchical(MemorySubSystem subsystem) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   213
        long hierarchical = SubSystem.getLongValue(subsystem, "memory.use_hierarchy");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   214
        return hierarchical > 0;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   215
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   216
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   217
    private void setActiveSubSystems() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   218
        activeSubSystems = true;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   219
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   220
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   221
    private boolean activeSubSystems() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   222
        return activeSubSystems;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   223
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   224
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   225
    private void setMemorySubSystem(MemorySubSystem memory) {
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   226
        this.memory = memory;
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
    private void setCpuSubSystem(SubSystem cpu) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   230
        this.cpu = cpu;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   231
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   232
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   233
    private void setCpuAcctSubSystem(SubSystem cpuacct) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   234
        this.cpuacct = cpuacct;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   235
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   236
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   237
    private void setCpuSetSubSystem(SubSystem cpuset) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   238
        this.cpuset = cpuset;
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
    private void setBlkIOSubSystem(SubSystem blkio) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   242
        this.blkio = blkio;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   243
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   244
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   245
    private SubSystem MemorySubSystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   246
        return memory;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   247
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   248
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   249
    private SubSystem CpuSubSystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   250
        return cpu;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   251
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   252
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   253
    private SubSystem CpuAcctSubSystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   254
        return cpuacct;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   255
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   256
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   257
    private SubSystem CpuSetSubSystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   258
        return cpuset;
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
    private SubSystem BlkIOSubSystem() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   262
        return blkio;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   263
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   264
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   265
    public String getProvider() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   266
        return PROVIDER_NAME;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   267
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   268
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   269
    /*****************************************************************
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   270
     * CPU Accounting Subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   271
     ****************************************************************/
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
    public long getCpuUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   275
        return SubSystem.getLongValue(cpuacct, "cpuacct.usage");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   276
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   277
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   278
    public long[] getPerCpuUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   279
        String usagelist = SubSystem.getStringValue(cpuacct, "cpuacct.usage_percpu");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   280
        if (usagelist == null) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   281
            return new long[0];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   282
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   283
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   284
        String list[] = usagelist.split(" ");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   285
        long percpu[] = new long[list.length];
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   286
        for (int i = 0; i < list.length; i++) {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   287
            percpu[i] = Long.parseLong(list[i]);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   288
        }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   289
        return percpu;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   290
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   291
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   292
    public long getCpuUserUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   293
        return SubSystem.getLongEntry(cpuacct, "cpuacct.stat", "user");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   294
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   295
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   296
    public long getCpuSystemUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   297
        return SubSystem.getLongEntry(cpuacct, "cpuacct.stat", "system");
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
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   301
    /*****************************************************************
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   302
     * CPU Subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   303
     ****************************************************************/
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   304
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   305
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   306
    public long getCpuPeriod() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   307
        return SubSystem.getLongValue(cpu, "cpu.cfs_period_us");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   308
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   309
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   310
    public long getCpuQuota() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   311
        return SubSystem.getLongValue(cpu, "cpu.cfs_quota_us");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   312
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   313
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   314
    public long getCpuShares() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   315
        long retval = SubSystem.getLongValue(cpu, "cpu.shares");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   316
        if (retval == 0 || retval == 1024)
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   317
            return -1;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   318
        else
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   319
            return retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   320
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   321
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   322
    public long getCpuNumPeriods() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   323
        return SubSystem.getLongEntry(cpu, "cpu.stat", "nr_periods");
50545
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
    public long getCpuNumThrottled() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   327
        return SubSystem.getLongEntry(cpu, "cpu.stat", "nr_throttled");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   328
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   329
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   330
    public long getCpuThrottledTime() {
54192
d909d0a883c4 8212528: Wrong cgroup subsystem being used for some CPU Container Metrics
bobv
parents: 54040
diff changeset
   331
        return SubSystem.getLongEntry(cpu, "cpu.stat", "throttled_time");
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   332
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   333
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   334
    public long getEffectiveCpuCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   335
        return Runtime.getRuntime().availableProcessors();
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   336
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   337
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   338
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   339
    /*****************************************************************
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   340
     * CPUSet Subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   341
     ****************************************************************/
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   342
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   343
    public int[] getCpuSetCpus() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   344
        return SubSystem.StringRangeToIntArray(SubSystem.getStringValue(cpuset, "cpuset.cpus"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   345
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   346
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   347
    public int[] getEffectiveCpuSetCpus() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   348
        return SubSystem.StringRangeToIntArray(SubSystem.getStringValue(cpuset, "cpuset.effective_cpus"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   349
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   350
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   351
    public int[] getCpuSetMems() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   352
        return SubSystem.StringRangeToIntArray(SubSystem.getStringValue(cpuset, "cpuset.mems"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   353
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   354
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   355
    public int[] getEffectiveCpuSetMems() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   356
        return SubSystem.StringRangeToIntArray(SubSystem.getStringValue(cpuset, "cpuset.effective_mems"));
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   357
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   358
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   359
    public double getCpuSetMemoryPressure() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   360
        return SubSystem.getDoubleValue(cpuset, "cpuset.memory_pressure");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   361
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   362
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   363
    public boolean isCpuSetMemoryPressureEnabled() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   364
        long val = SubSystem.getLongValue(cpuset, "cpuset.memory_pressure_enabled");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   365
        return (val == 1);
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
    /*****************************************************************
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   370
     * Memory Subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   371
     ****************************************************************/
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   372
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   373
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   374
    public long getMemoryFailCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   375
        return SubSystem.getLongValue(memory, "memory.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   376
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   377
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   378
    public long getMemoryLimit() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   379
        long retval = SubSystem.getLongValue(memory, "memory.limit_in_bytes");
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   380
        if (retval > unlimited_minimum) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   381
            if (memory.isHierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   382
                // memory.limit_in_bytes returned unlimited, attempt
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   383
                // hierarchical memory limit
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   384
                String match = "hierarchical_memory_limit";
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   385
                retval = SubSystem.getLongValueMatchingLine(memory,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   386
                                                            "memory.stat",
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   387
                                                            match,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   388
                                                            Metrics::convertHierachicalLimitLine);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   389
            }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   390
        }
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   391
        return retval > unlimited_minimum ? -1L : retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   392
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   393
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   394
    public static long convertHierachicalLimitLine(String line) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   395
        String[] tokens = line.split("\\s");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   396
        if (tokens.length == 2) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   397
            String strVal = tokens[1];
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   398
            return SubSystem.convertStringToLong(strVal);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   399
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   400
        return unlimited_minimum + 1; // unlimited
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   401
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   402
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   403
    public long getMemoryMaxUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   404
        return SubSystem.getLongValue(memory, "memory.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   405
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   406
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   407
    public long getMemoryUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   408
        return SubSystem.getLongValue(memory, "memory.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   409
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   410
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   411
    public long getKernelMemoryFailCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   412
        return SubSystem.getLongValue(memory, "memory.kmem.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   413
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   414
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   415
    public long getKernelMemoryLimit() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   416
        long retval = SubSystem.getLongValue(memory, "memory.kmem.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   417
        return retval > unlimited_minimum ? -1L : retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   418
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   419
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   420
    public long getKernelMemoryMaxUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   421
        return SubSystem.getLongValue(memory, "memory.kmem.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   422
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   423
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   424
    public long getKernelMemoryUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   425
        return SubSystem.getLongValue(memory, "memory.kmem.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   426
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   427
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   428
    public long getTcpMemoryFailCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   429
        return SubSystem.getLongValue(memory, "memory.kmem.tcp.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   430
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   431
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   432
    public long getTcpMemoryLimit() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   433
        long retval =  SubSystem.getLongValue(memory, "memory.kmem.tcp.limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   434
        return retval > unlimited_minimum ? -1L : retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   435
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   436
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   437
    public long getTcpMemoryMaxUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   438
        return SubSystem.getLongValue(memory, "memory.kmem.tcp.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   439
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   440
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   441
    public long getTcpMemoryUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   442
        return SubSystem.getLongValue(memory, "memory.kmem.tcp.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   443
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   444
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   445
    public long getMemoryAndSwapFailCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   446
        return SubSystem.getLongValue(memory, "memory.memsw.failcnt");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   447
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   448
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   449
    public long getMemoryAndSwapLimit() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   450
        long retval = SubSystem.getLongValue(memory, "memory.memsw.limit_in_bytes");
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   451
        if (retval > unlimited_minimum) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   452
            if (memory.isHierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   453
                // memory.memsw.limit_in_bytes returned unlimited, attempt
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   454
                // hierarchical memory limit
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   455
                String match = "hierarchical_memsw_limit";
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   456
                retval = SubSystem.getLongValueMatchingLine(memory,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   457
                                                            "memory.stat",
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   458
                                                            match,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   459
                                                            Metrics::convertHierachicalLimitLine);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   460
            }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   461
        }
50545
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   462
        return retval > unlimited_minimum ? -1L : retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   463
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   464
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   465
    public long getMemoryAndSwapMaxUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   466
        return SubSystem.getLongValue(memory, "memory.memsw.max_usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   467
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   468
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   469
    public long getMemoryAndSwapUsage() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   470
        return SubSystem.getLongValue(memory, "memory.memsw.usage_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   471
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   472
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   473
    public boolean isMemoryOOMKillEnabled() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   474
        long val = SubSystem.getLongEntry(memory, "memory.oom_control", "oom_kill_disable");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   475
        return (val == 0);
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   476
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   477
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   478
    public long getMemorySoftLimit() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   479
        long retval = SubSystem.getLongValue(memory, "memory.soft_limit_in_bytes");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   480
        return retval > unlimited_minimum ? -1L : retval;
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   481
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   482
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   483
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   484
    /*****************************************************************
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   485
     * BlKIO Subsystem
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   486
     ****************************************************************/
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   487
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   488
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   489
    public long getBlkIOServiceCount() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   490
        return SubSystem.getLongEntry(blkio, "blkio.throttle.io_service_bytes", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   491
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   492
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   493
    public long getBlkIOServiced() {
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   494
        return SubSystem.getLongEntry(blkio, "blkio.throttle.io_serviced", "Total");
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   495
    }
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   496
292a4a87c321 8203357: Container Metrics
bobv
parents:
diff changeset
   497
}