src/hotspot/os/linux/osContainer_linux.cpp
author sgehwolf
Tue, 12 Mar 2019 10:43:27 +0100
changeset 54577 1c242c2d037f
parent 54193 d5da034032e9
child 54810 258170da6d3a
permissions -rw-r--r--
8217338: [Containers] Improve systemd slice memory limit support Summary: Use hierachical memory limit in addition to memory_limits_in_bytes Reviewed-by: bobv, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     1
/*
54191
da7f6755ba71 8219562: Line of code in osContainer_linux.cpp L102 appears unreachable
bobv
parents: 53200
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     4
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     8
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    13
 * accompanied this code).
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    14
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    18
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    21
 * questions.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    22
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    23
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    24
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    25
#include <string.h>
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    26
#include <math.h>
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    27
#include <errno.h>
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    28
#include "utilities/globalDefinitions.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    29
#include "memory/allocation.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    30
#include "runtime/os.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    31
#include "logging/log.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    32
#include "osContainer_linux.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    33
53200
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    34
/*
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    35
 * PER_CPU_SHARES has been set to 1024 because CPU shares' quota
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    36
 * is commonly used in cloud frameworks like Kubernetes[1],
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    37
 * AWS[2] and Mesos[3] in a similar way. They spawn containers with
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    38
 * --cpu-shares option values scaled by PER_CPU_SHARES. Thus, we do
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    39
 * the inverse for determining the number of possible available
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    40
 * CPUs to the JVM inside a container. See JDK-8216366.
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    41
 *
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    42
 * [1] https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    43
 *     In particular:
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    44
 *        When using Docker:
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    45
 *          The spec.containers[].resources.requests.cpu is converted to its core value, which is potentially
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    46
 *          fractional, and multiplied by 1024. The greater of this number or 2 is used as the value of the
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    47
 *          --cpu-shares flag in the docker run command.
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    48
 * [2] https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    49
 * [3] https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/docker/docker.cpp#L648
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    50
 *     https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/slave/containerizer/mesos/isolators/cgroups/constants.hpp#L30
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    51
 */
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    52
#define PER_CPU_SHARES 1024
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    53
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    54
bool  OSContainer::_is_initialized   = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    55
bool  OSContainer::_is_containerized = false;
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
    56
julong _unlimited_memory;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    57
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    58
class CgroupSubsystem: CHeapObj<mtInternal> {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    59
 friend class OSContainer;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    60
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    61
 private:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    62
    /* mountinfo contents */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    63
    char *_root;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    64
    char *_mount_point;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    65
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    66
    /* Constructed subsystem directory */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    67
    char *_path;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    68
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    69
 public:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    70
    CgroupSubsystem(char *root, char *mountpoint) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    71
      _root = os::strdup(root);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    72
      _mount_point = os::strdup(mountpoint);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    73
      _path = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    74
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    75
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    76
    /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    77
     * Set directory to subsystem specific files based
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    78
     * on the contents of the mountinfo and cgroup files.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    79
     */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    80
    void set_subsystem_path(char *cgroup_path) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    81
      char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    82
      if (_root != NULL && cgroup_path != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    83
        if (strcmp(_root, "/") == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    84
          int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    85
          strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    86
          buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    87
          if (strcmp(cgroup_path,"/") != 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    88
            buflen = strlen(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    89
            if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    90
              return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    91
            }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    92
            strncat(buf, cgroup_path, MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    93
            buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    94
          }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    95
          _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    96
        } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    97
          if (strcmp(_root, cgroup_path) == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    98
            strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    99
            buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   100
            _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   101
          } else {
54191
da7f6755ba71 8219562: Line of code in osContainer_linux.cpp L102 appears unreachable
bobv
parents: 53200
diff changeset
   102
            char *p = strstr(cgroup_path, _root);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   103
            if (p != NULL && p == _root) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   104
              if (strlen(cgroup_path) > strlen(_root)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   105
                int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   106
                strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   107
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   108
                buflen = strlen(buf);
54191
da7f6755ba71 8219562: Line of code in osContainer_linux.cpp L102 appears unreachable
bobv
parents: 53200
diff changeset
   109
                if ((buflen + strlen(cgroup_path) - strlen(_root)) > (MAXPATHLEN-1)) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   110
                  return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   111
                }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   112
                strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   113
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   114
                _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   115
              }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   116
            }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   117
          }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   118
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   119
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   120
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   121
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   122
    char *subsystem_path() { return _path; }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   123
};
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   124
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   125
class CgroupMemorySubsystem: CgroupSubsystem {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   126
 friend class OSContainer;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   127
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   128
 private:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   129
    /* Some container runtimes set limits via cgroup
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   130
     * hierarchy. If set to true consider also memory.stat
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   131
     * file if everything else seems unlimited */
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   132
    bool _uses_mem_hierarchy;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   133
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   134
 public:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   135
    CgroupMemorySubsystem(char *root, char *mountpoint) : CgroupSubsystem::CgroupSubsystem(root, mountpoint) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   136
      _uses_mem_hierarchy = false;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   137
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   138
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   139
    bool is_hierarchical() { return _uses_mem_hierarchy; }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   140
    void set_hierarchical(bool value) { _uses_mem_hierarchy = value; }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   141
};
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   142
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   143
CgroupMemorySubsystem* memory = NULL;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   144
CgroupSubsystem* cpuset = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   145
CgroupSubsystem* cpu = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   146
CgroupSubsystem* cpuacct = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   147
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   148
typedef char * cptr;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   149
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   150
PRAGMA_DIAG_PUSH
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   151
PRAGMA_FORMAT_NONLITERAL_IGNORED
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   152
template <typename T> int subsystem_file_line_contents(CgroupSubsystem* c,
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   153
                                              const char *filename,
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   154
                                              const char *matchline,
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   155
                                              const char *scan_fmt,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   156
                                              T returnval) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   157
  FILE *fp = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   158
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   159
  char file[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   160
  char buf[MAXPATHLEN+1];
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   161
  char discard[MAXPATHLEN+1];
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   162
  bool found_match = false;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   163
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   164
  if (c == NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   165
    log_debug(os, container)("subsystem_file_line_contents: CgroupSubsytem* is NULL");
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   166
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   167
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   168
  if (c->subsystem_path() == NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   169
    log_debug(os, container)("subsystem_file_line_contents: subsystem path is NULL");
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   170
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   171
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   172
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   173
  strncpy(file, c->subsystem_path(), MAXPATHLEN);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   174
  file[MAXPATHLEN-1] = '\0';
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   175
  int filelen = strlen(file);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   176
  if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   177
    log_debug(os, container)("File path too long %s, %s", file, filename);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   178
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   179
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   180
  strncat(file, filename, MAXPATHLEN-filelen);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   181
  log_trace(os, container)("Path to %s is %s", filename, file);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   182
  fp = fopen(file, "r");
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   183
  if (fp != NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   184
    int err = 0;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   185
    while ((p = fgets(buf, MAXPATHLEN, fp)) != NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   186
      found_match = false;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   187
      if (matchline == NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   188
        // single-line file case
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   189
        int matched = sscanf(p, scan_fmt, returnval);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   190
        found_match = (matched == 1);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   191
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   192
        // multi-line file case
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   193
        if (strstr(p, matchline) != NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   194
          // discard matchline string prefix
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   195
          int matched = sscanf(p, scan_fmt, discard, returnval);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   196
          found_match = (matched == 2);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   197
        } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   198
          continue; // substring not found
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   199
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   200
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   201
      if (found_match) {
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   202
        fclose(fp);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   203
        return 0;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   204
      } else {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   205
        err = 1;
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   206
        log_debug(os, container)("Type %s not found in file %s", scan_fmt, file);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   207
      }
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   208
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   209
    if (err == 0) {
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   210
      log_debug(os, container)("Empty file %s", file);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   211
    }
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   212
  } else {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   213
    log_debug(os, container)("Open of file %s failed, %s", file, os::strerror(errno));
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   214
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   215
  if (fp != NULL)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   216
    fclose(fp);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   217
  return OSCONTAINER_ERROR;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   218
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   219
PRAGMA_DIAG_POP
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   220
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   221
#define GET_CONTAINER_INFO(return_type, subsystem, filename,              \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   222
                           logstring, scan_fmt, variable)                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   223
  return_type variable;                                                   \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   224
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   225
  int err;                                                                \
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   226
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   227
                                     filename,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   228
                                     NULL,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   229
                                     scan_fmt,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   230
                                     &variable);                          \
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   231
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   232
    return (return_type) OSCONTAINER_ERROR;                               \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   233
                                                                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   234
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   235
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   236
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   237
#define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename,         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   238
                               logstring, scan_fmt, variable, bufsize)    \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   239
  char variable[bufsize];                                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   240
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   241
  int err;                                                                \
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   242
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   243
                                     filename,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   244
                                     NULL,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   245
                                     scan_fmt,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   246
                                     variable);                           \
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   247
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   248
    return (return_type) NULL;                                            \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   249
                                                                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   250
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   251
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   252
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   253
#define GET_CONTAINER_INFO_LINE(return_type, subsystem, filename,         \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   254
                           matchline, logstring, scan_fmt, variable)      \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   255
  return_type variable;                                                   \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   256
{                                                                         \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   257
  int err;                                                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   258
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   259
                                filename,                                 \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   260
                                matchline,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   261
                                scan_fmt,                                 \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   262
                                &variable);                               \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   263
  if (err != 0)                                                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   264
    return (return_type) OSCONTAINER_ERROR;                               \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   265
                                                                          \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   266
  log_trace(os, container)(logstring, variable);                          \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   267
}
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   268
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   269
/* init
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   270
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   271
 * Initialize the container support and determine if
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   272
 * we are running under cgroup control.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   273
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   274
void OSContainer::init() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   275
  FILE *mntinfo = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   276
  FILE *cgroup = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   277
  char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   278
  char tmproot[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   279
  char tmpmount[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   280
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   281
  jlong mem_limit;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   282
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   283
  assert(!_is_initialized, "Initializing OSContainer more than once");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   284
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   285
  _is_initialized = true;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   286
  _is_containerized = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   287
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   288
  _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   289
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   290
  log_trace(os, container)("OSContainer::init: Initializing Container Support");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   291
  if (!UseContainerSupport) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   292
    log_trace(os, container)("Container Support not enabled");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   293
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   294
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   295
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   296
  /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   297
   * Find the cgroup mount point for memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   298
   * by reading /proc/self/mountinfo
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   299
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   300
   * Example for docker:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   301
   * 219 214 0:29 /docker/7208cebd00fa5f2e342b1094f7bed87fa25661471a4637118e65f1c995be8a34 /sys/fs/cgroup/memory ro,nosuid,nodev,noexec,relatime - cgroup cgroup rw,memory
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   302
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   303
   * Example for host:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   304
   * 34 28 0:29 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,memory
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   305
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   306
  mntinfo = fopen("/proc/self/mountinfo", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   307
  if (mntinfo == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   308
      log_debug(os, container)("Can't open /proc/self/mountinfo, %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   309
                               os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   310
      return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   311
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   312
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   313
  while ((p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   314
    char tmpcgroups[MAXPATHLEN+1];
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   315
    char *cptr = tmpcgroups;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   316
    char *token;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   317
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   318
    // mountinfo format is documented at https://www.kernel.org/doc/Documentation/filesystems/proc.txt
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   319
    if (sscanf(p, "%*d %*d %*d:%*d %s %s %*[^-]- cgroup %*s %s", tmproot, tmpmount, tmpcgroups) != 3) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   320
      continue;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   321
    }
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   322
    while ((token = strsep(&cptr, ",")) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   323
      if (strcmp(token, "memory") == 0) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   324
        memory = new CgroupMemorySubsystem(tmproot, tmpmount);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   325
      } else if (strcmp(token, "cpuset") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   326
        cpuset = new CgroupSubsystem(tmproot, tmpmount);
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   327
      } else if (strcmp(token, "cpu") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   328
        cpu = new CgroupSubsystem(tmproot, tmpmount);
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   329
      } else if (strcmp(token, "cpuacct") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   330
        cpuacct= new CgroupSubsystem(tmproot, tmpmount);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   331
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   332
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   333
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   334
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   335
  fclose(mntinfo);
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   336
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   337
  if (memory == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   338
    log_debug(os, container)("Required cgroup memory subsystem not found");
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   339
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   340
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   341
  if (cpuset == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   342
    log_debug(os, container)("Required cgroup cpuset subsystem not found");
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   343
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   344
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   345
  if (cpu == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   346
    log_debug(os, container)("Required cgroup cpu subsystem not found");
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   347
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   348
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   349
  if (cpuacct == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   350
    log_debug(os, container)("Required cgroup cpuacct subsystem not found");
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   351
    return;
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   352
  }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   353
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   354
  /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   355
   * Read /proc/self/cgroup and map host mount point to
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   356
   * local one via /proc/self/mountinfo content above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   357
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   358
   * Docker example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   359
   * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   360
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   361
   * Host example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   362
   * 5:memory:/user.slice
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   363
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   364
   * Construct a path to the process specific memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   365
   * cgroup directory.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   366
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   367
   * For a container running under Docker from memory example above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   368
   * the paths would be:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   369
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   370
   * /sys/fs/cgroup/memory
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   371
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   372
   * For a Host from memory example above the path would be:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   373
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   374
   * /sys/fs/cgroup/memory/user.slice
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   375
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   376
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   377
  cgroup = fopen("/proc/self/cgroup", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   378
  if (cgroup == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   379
    log_debug(os, container)("Can't open /proc/self/cgroup, %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   380
                             os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   381
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   382
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   383
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   384
  while ((p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   385
    char *controllers;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   386
    char *token;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   387
    char *base;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   388
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   389
    /* Skip cgroup number */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   390
    strsep(&p, ":");
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   391
    /* Get controllers and base */
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   392
    controllers = strsep(&p, ":");
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   393
    base = strsep(&p, "\n");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   394
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   395
    if (controllers == NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   396
      continue;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   397
    }
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   398
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   399
    while ((token = strsep(&controllers, ",")) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   400
      if (strcmp(token, "memory") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   401
        memory->set_subsystem_path(base);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   402
        jlong hierarchy = uses_mem_hierarchy();
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   403
        if (hierarchy > 0) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   404
          memory->set_hierarchical(true);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   405
        }
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   406
      } else if (strcmp(token, "cpuset") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   407
        cpuset->set_subsystem_path(base);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   408
      } else if (strcmp(token, "cpu") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   409
        cpu->set_subsystem_path(base);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   410
      } else if (strcmp(token, "cpuacct") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   411
        cpuacct->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   412
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   413
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   414
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   415
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   416
  fclose(cgroup);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   417
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   418
  // We need to update the amount of physical memory now that
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   419
  // command line arguments have been processed.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   420
  if ((mem_limit = memory_limit_in_bytes()) > 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   421
    os::Linux::set_physical_memory(mem_limit);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   422
    log_info(os, container)("Memory Limit is: " JLONG_FORMAT, mem_limit);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   423
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   424
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   425
  _is_containerized = true;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   426
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   427
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   428
49175
6a5decfc5574 8197408: Bad pointer comparison and small cleanup in os_linux.cpp
rehn
parents: 49058
diff changeset
   429
const char * OSContainer::container_type() {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   430
  if (is_containerized()) {
49175
6a5decfc5574 8197408: Bad pointer comparison and small cleanup in os_linux.cpp
rehn
parents: 49058
diff changeset
   431
    return "cgroupv1";
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   432
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   433
    return NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   434
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   435
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   436
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   437
/* uses_mem_hierarchy
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   438
 *
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   439
 * Return whether or not hierarchical cgroup accounting is being
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   440
 * done.
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   441
 *
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   442
 * return:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   443
 *    A number > 0 if true, or
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   444
 *    OSCONTAINER_ERROR for not supported
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   445
 */
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   446
jlong OSContainer::uses_mem_hierarchy() {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   447
  GET_CONTAINER_INFO(jlong, memory, "/memory.use_hierarchy",
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   448
                    "Use Hierarchy is: " JLONG_FORMAT, JLONG_FORMAT, use_hierarchy);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   449
  return use_hierarchy;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   450
}
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   451
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   452
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   453
/* memory_limit_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   454
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   455
 * Return the limit of available memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   456
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   457
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   458
 *    memory limit in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   459
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   460
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   461
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   462
jlong OSContainer::memory_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   463
  GET_CONTAINER_INFO(julong, memory, "/memory.limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   464
                     "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   465
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   466
  if (memlimit >= _unlimited_memory) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   467
    log_trace(os, container)("Non-Hierarchical Memory Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   468
    if (memory->is_hierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   469
      const char* matchline = "hierarchical_memory_limit";
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   470
      char* format = "%s " JULONG_FORMAT;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   471
      GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   472
                             "Hierarchical Memory Limit is: " JULONG_FORMAT, format, hier_memlimit)
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   473
      if (hier_memlimit >= _unlimited_memory) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   474
        log_trace(os, container)("Hierarchical Memory Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   475
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   476
        return (jlong)hier_memlimit;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   477
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   478
    }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   479
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   480
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   481
  else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   482
    return (jlong)memlimit;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   483
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   484
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   485
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   486
jlong OSContainer::memory_and_swap_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   487
  GET_CONTAINER_INFO(julong, memory, "/memory.memsw.limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   488
                     "Memory and Swap Limit is: " JULONG_FORMAT, JULONG_FORMAT, memswlimit);
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   489
  if (memswlimit >= _unlimited_memory) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   490
    log_trace(os, container)("Non-Hierarchical Memory and Swap Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   491
    if (memory->is_hierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   492
      const char* matchline = "hierarchical_memsw_limit";
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   493
      char* format = "%s " JULONG_FORMAT;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   494
      GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   495
                             "Hierarchical Memory and Swap Limit is : " JULONG_FORMAT, format, hier_memlimit)
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   496
      if (hier_memlimit >= _unlimited_memory) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   497
        log_trace(os, container)("Hierarchical Memory and Swap Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   498
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   499
        return (jlong)hier_memlimit;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   500
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   501
    }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   502
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   503
  } else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   504
    return (jlong)memswlimit;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   505
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   506
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   507
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   508
jlong OSContainer::memory_soft_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   509
  GET_CONTAINER_INFO(julong, memory, "/memory.soft_limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   510
                     "Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   511
  if (memsoftlimit >= _unlimited_memory) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   512
    log_trace(os, container)("Memory Soft Limit is: Unlimited");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   513
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   514
  } else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   515
    return (jlong)memsoftlimit;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   516
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   517
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   518
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   519
/* memory_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   520
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   521
 * Return the amount of used memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   522
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   523
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   524
 *    memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   525
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   526
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   527
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   528
jlong OSContainer::memory_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   529
  GET_CONTAINER_INFO(jlong, memory, "/memory.usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   530
                     "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   531
  return memusage;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   532
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   533
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   534
/* memory_max_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   535
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   536
 * Return the maximum amount of used memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   537
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   538
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   539
 *    max memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   540
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   541
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   542
jlong OSContainer::memory_max_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   543
  GET_CONTAINER_INFO(jlong, memory, "/memory.max_usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   544
                     "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   545
  return memmaxusage;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   546
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   547
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   548
/* active_processor_count
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   549
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   550
 * Calculate an appropriate number of active processors for the
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   551
 * VM to use based on these three inputs.
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   552
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   553
 * cpu affinity
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   554
 * cgroup cpu quota & cpu period
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   555
 * cgroup cpu shares
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   556
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   557
 * Algorithm:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   558
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   559
 * Determine the number of available CPUs from sched_getaffinity
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   560
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   561
 * If user specified a quota (quota != -1), calculate the number of
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   562
 * required CPUs by dividing quota by period.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   563
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   564
 * If shares are in effect (shares != -1), calculate the number
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   565
 * of CPUs required for the shares by dividing the share value
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   566
 * by PER_CPU_SHARES.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   567
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   568
 * All results of division are rounded up to the next whole number.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   569
 *
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   570
 * If neither shares or quotas have been specified, return the
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   571
 * number of active processors in the system.
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   572
 *
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   573
 * If both shares and quotas have been specified, the results are
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   574
 * based on the flag PreferContainerQuotaForCPUCount.  If true,
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   575
 * return the quota value.  If false return the smallest value
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   576
 * between shares or quotas.
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   577
 *
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   578
 * If shares and/or quotas have been specified, the resulting number
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   579
 * returned will never exceed the number of active processors.
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   580
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   581
 * return:
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   582
 *    number of CPUs
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   583
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   584
int OSContainer::active_processor_count() {
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   585
  int quota_count = 0, share_count = 0;
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   586
  int cpu_count, limit_count;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   587
  int result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   588
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   589
  cpu_count = limit_count = os::Linux::active_processor_count();
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   590
  int quota  = cpu_quota();
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   591
  int period = cpu_period();
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   592
  int share  = cpu_shares();
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   593
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   594
  if (quota > -1 && period > 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   595
    quota_count = ceilf((float)quota / (float)period);
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   596
    log_trace(os, container)("CPU Quota count based on quota/period: %d", quota_count);
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   597
  }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   598
  if (share > -1) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   599
    share_count = ceilf((float)share / (float)PER_CPU_SHARES);
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   600
    log_trace(os, container)("CPU Share count based on shares: %d", share_count);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   601
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   602
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   603
  // If both shares and quotas are setup results depend
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   604
  // on flag PreferContainerQuotaForCPUCount.
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   605
  // If true, limit CPU count to quota
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   606
  // If false, use minimum of shares and quotas
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   607
  if (quota_count !=0 && share_count != 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   608
    if (PreferContainerQuotaForCPUCount) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   609
      limit_count = quota_count;
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   610
    } else {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   611
      limit_count = MIN2(quota_count, share_count);
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   612
    }
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   613
  } else if (quota_count != 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   614
    limit_count = quota_count;
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   615
  } else if (share_count != 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   616
    limit_count = share_count;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   617
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   618
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   619
  result = MIN2(cpu_count, limit_count);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   620
  log_trace(os, container)("OSContainer::active_processor_count: %d", result);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   621
  return result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   622
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   623
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   624
char * OSContainer::cpu_cpuset_cpus() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   625
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.cpus",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   626
                     "cpuset.cpus is: %s", "%1023s", cpus, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   627
  return os::strdup(cpus);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   628
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   629
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   630
char * OSContainer::cpu_cpuset_memory_nodes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   631
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.mems",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   632
                     "cpuset.mems is: %s", "%1023s", mems, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   633
  return os::strdup(mems);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   634
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   635
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   636
/* cpu_quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   637
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   638
 * Return the number of milliseconds per period
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   639
 * process is guaranteed to run.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   640
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   641
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   642
 *    quota time in milliseconds
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   643
 *    -1 for no quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   644
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   645
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   646
int OSContainer::cpu_quota() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   647
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_quota_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   648
                     "CPU Quota is: %d", "%d", quota);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   649
  return quota;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   650
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   651
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   652
int OSContainer::cpu_period() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   653
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_period_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   654
                     "CPU Period is: %d", "%d", period);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   655
  return period;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   656
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   657
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   658
/* cpu_shares
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   659
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   660
 * Return the amount of cpu shares available to the process
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   661
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   662
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   663
 *    Share number (typically a number relative to 1024)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   664
 *                 (2048 typically expresses 2 CPUs worth of processing)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   665
 *    -1 for no share setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   666
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   667
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   668
int OSContainer::cpu_shares() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   669
  GET_CONTAINER_INFO(int, cpu, "/cpu.shares",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   670
                     "CPU Shares is: %d", "%d", shares);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   671
  // Convert 1024 to no shares setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   672
  if (shares == 1024) return -1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   673
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   674
  return shares;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   675
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   676