src/hotspot/os/linux/osContainer_linux.cpp
author redestad
Thu, 17 Oct 2019 00:19:02 +0200
changeset 58655 21a92562f0c2
parent 57875 427b38332f20
child 58679 9c3209ff7550
child 58874 c440a6b4e096
permissions -rw-r--r--
8232207: Linux os::available_memory re-reads cgroup configuration on every invocation Reviewed-by: bobv, sgehwolf
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"
57875
427b38332f20 8229836: Remove include of globals.hpp from allocation.hpp
stefank
parents: 54810
diff changeset
    30
#include "runtime/globals.hpp"
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    31
#include "runtime/os.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    32
#include "logging/log.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    33
#include "osContainer_linux.hpp"
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    34
53200
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    35
/*
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    36
 * 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
    37
 * is commonly used in cloud frameworks like Kubernetes[1],
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    38
 * 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
    39
 * --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
    40
 * the inverse for determining the number of possible available
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    41
 * CPUs to the JVM inside a container. See JDK-8216366.
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    42
 *
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    43
 * [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
    44
 *     In particular:
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    45
 *        When using Docker:
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    46
 *          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
    47
 *          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
    48
 *          --cpu-shares flag in the docker run command.
3cdf4d5148a8 8216366: Add rationale to PER_CPU_SHARES define
sgehwolf
parents: 49175
diff changeset
    49
 * [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
    50
 * [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
    51
 *     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
    52
 */
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    53
#define PER_CPU_SHARES 1024
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    54
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    55
bool  OSContainer::_is_initialized   = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    56
bool  OSContainer::_is_containerized = false;
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
    57
julong _unlimited_memory;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    58
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    59
class CgroupSubsystem: CHeapObj<mtInternal> {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    60
 friend class OSContainer;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    61
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    62
 private:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    63
    /* mountinfo contents */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    64
    char *_root;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    65
    char *_mount_point;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    66
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    67
    /* Constructed subsystem directory */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    68
    char *_path;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    69
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    70
 public:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    71
    CgroupSubsystem(char *root, char *mountpoint) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    72
      _root = os::strdup(root);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    73
      _mount_point = os::strdup(mountpoint);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    74
      _path = NULL;
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
    /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    78
     * Set directory to subsystem specific files based
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    79
     * on the contents of the mountinfo and cgroup files.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    80
     */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    81
    void set_subsystem_path(char *cgroup_path) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    82
      char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    83
      if (_root != NULL && cgroup_path != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    84
        if (strcmp(_root, "/") == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    85
          int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    86
          strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    87
          buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    88
          if (strcmp(cgroup_path,"/") != 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    89
            buflen = strlen(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    90
            if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    91
              return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    92
            }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    93
            strncat(buf, cgroup_path, MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    94
            buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    95
          }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    96
          _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    97
        } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    98
          if (strcmp(_root, cgroup_path) == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    99
            strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   100
            buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   101
            _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   102
          } else {
54191
da7f6755ba71 8219562: Line of code in osContainer_linux.cpp L102 appears unreachable
bobv
parents: 53200
diff changeset
   103
            char *p = strstr(cgroup_path, _root);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   104
            if (p != NULL && p == _root) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   105
              if (strlen(cgroup_path) > strlen(_root)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   106
                int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   107
                strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   108
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   109
                buflen = strlen(buf);
54191
da7f6755ba71 8219562: Line of code in osContainer_linux.cpp L102 appears unreachable
bobv
parents: 53200
diff changeset
   110
                if ((buflen + strlen(cgroup_path) - strlen(_root)) > (MAXPATHLEN-1)) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   111
                  return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   112
                }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   113
                strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   114
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   115
                _path = os::strdup(buf);
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   123
    char *subsystem_path() { return _path; }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   124
};
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   125
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   126
class CgroupMemorySubsystem: CgroupSubsystem {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   127
 friend class OSContainer;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   128
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   129
 private:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   130
    /* Some container runtimes set limits via cgroup
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   131
     * hierarchy. If set to true consider also memory.stat
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   132
     * file if everything else seems unlimited */
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   133
    bool _uses_mem_hierarchy;
58655
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   134
    volatile jlong _memory_limit_in_bytes;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   135
    volatile jlong _next_check_counter;
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   136
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   137
 public:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   138
    CgroupMemorySubsystem(char *root, char *mountpoint) : CgroupSubsystem::CgroupSubsystem(root, mountpoint) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   139
      _uses_mem_hierarchy = false;
58655
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   140
      _memory_limit_in_bytes = -1;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   141
      _next_check_counter = min_jlong;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   142
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   143
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   144
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   145
    bool is_hierarchical() { return _uses_mem_hierarchy; }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   146
    void set_hierarchical(bool value) { _uses_mem_hierarchy = value; }
58655
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   147
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   148
    bool should_check_memory_limit() {
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   149
      return os::elapsed_counter() > _next_check_counter;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   150
    }
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   151
    jlong memory_limit_in_bytes() { return _memory_limit_in_bytes; }
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   152
    void set_memory_limit_in_bytes(jlong value) {
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   153
      _memory_limit_in_bytes = value;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   154
      // max memory limit is unlikely to change, but we want to remain
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   155
      // responsive to configuration changes. A very short (20ms) grace time
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   156
      // between re-read avoids excessive overhead during startup without
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   157
      // significantly reducing the VMs ability to promptly react to reduced
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   158
      // memory availability
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   159
      _next_check_counter = os::elapsed_counter() + (NANOSECS_PER_SEC/50);
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   160
    }
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   161
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   162
};
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   163
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   164
CgroupMemorySubsystem* memory = NULL;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   165
CgroupSubsystem* cpuset = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   166
CgroupSubsystem* cpu = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   167
CgroupSubsystem* cpuacct = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   168
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   169
typedef char * cptr;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   170
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   171
PRAGMA_DIAG_PUSH
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   172
PRAGMA_FORMAT_NONLITERAL_IGNORED
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   173
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
   174
                                              const char *filename,
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   175
                                              const char *matchline,
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   176
                                              const char *scan_fmt,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   177
                                              T returnval) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   178
  FILE *fp = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   179
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   180
  char file[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   181
  char buf[MAXPATHLEN+1];
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   182
  char discard[MAXPATHLEN+1];
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   183
  bool found_match = false;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   184
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   185
  if (c == NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   186
    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
   187
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   188
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   189
  if (c->subsystem_path() == NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   190
    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
   191
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   192
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   193
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   194
  strncpy(file, c->subsystem_path(), MAXPATHLEN);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   195
  file[MAXPATHLEN-1] = '\0';
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   196
  int filelen = strlen(file);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   197
  if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   198
    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
   199
    return OSCONTAINER_ERROR;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   200
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   201
  strncat(file, filename, MAXPATHLEN-filelen);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   202
  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
   203
  fp = fopen(file, "r");
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   204
  if (fp != NULL) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   205
    int err = 0;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   206
    while ((p = fgets(buf, MAXPATHLEN, fp)) != NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   207
      found_match = false;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   208
      if (matchline == NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   209
        // single-line file case
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   210
        int matched = sscanf(p, scan_fmt, returnval);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   211
        found_match = (matched == 1);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   212
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   213
        // multi-line file case
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   214
        if (strstr(p, matchline) != NULL) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   215
          // discard matchline string prefix
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   216
          int matched = sscanf(p, scan_fmt, discard, returnval);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   217
          found_match = (matched == 2);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   218
        } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   219
          continue; // substring not found
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   220
        }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   221
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   222
      if (found_match) {
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   223
        fclose(fp);
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   224
        return 0;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   225
      } else {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   226
        err = 1;
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   227
        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
   228
      }
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   229
    }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   230
    if (err == 0) {
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   231
      log_debug(os, container)("Empty file %s", file);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   232
    }
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   233
  } else {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   234
    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
   235
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   236
  if (fp != NULL)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   237
    fclose(fp);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   238
  return OSCONTAINER_ERROR;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   239
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   240
PRAGMA_DIAG_POP
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   241
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   242
#define GET_CONTAINER_INFO(return_type, subsystem, filename,              \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   243
                           logstring, scan_fmt, variable)                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   244
  return_type variable;                                                   \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   245
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   246
  int err;                                                                \
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   247
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   248
                                     filename,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   249
                                     NULL,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   250
                                     scan_fmt,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   251
                                     &variable);                          \
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   252
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   253
    return (return_type) OSCONTAINER_ERROR;                               \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   254
                                                                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   255
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   256
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   257
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   258
#define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename,         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   259
                               logstring, scan_fmt, variable, bufsize)    \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   260
  char variable[bufsize];                                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   261
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   262
  int err;                                                                \
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   263
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   264
                                     filename,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   265
                                     NULL,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   266
                                     scan_fmt,                            \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   267
                                     variable);                           \
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   268
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   269
    return (return_type) NULL;                                            \
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
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   272
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   273
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   274
#define GET_CONTAINER_INFO_LINE(return_type, subsystem, filename,         \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   275
                           matchline, logstring, scan_fmt, variable)      \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   276
  return_type variable;                                                   \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   277
{                                                                         \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   278
  int err;                                                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   279
  err = subsystem_file_line_contents(subsystem,                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   280
                                filename,                                 \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   281
                                matchline,                                \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   282
                                scan_fmt,                                 \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   283
                                &variable);                               \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   284
  if (err != 0)                                                           \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   285
    return (return_type) OSCONTAINER_ERROR;                               \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   286
                                                                          \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   287
  log_trace(os, container)(logstring, variable);                          \
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   288
}
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   289
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   290
/* init
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   291
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   292
 * Initialize the container support and determine if
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   293
 * we are running under cgroup control.
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
void OSContainer::init() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   296
  FILE *mntinfo = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   297
  FILE *cgroup = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   298
  char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   299
  char tmproot[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   300
  char tmpmount[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   301
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   302
  jlong mem_limit;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   303
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   304
  assert(!_is_initialized, "Initializing OSContainer more than once");
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
  _is_initialized = true;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   307
  _is_containerized = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   308
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   309
  _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   310
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   311
  log_trace(os, container)("OSContainer::init: Initializing Container Support");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   312
  if (!UseContainerSupport) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   313
    log_trace(os, container)("Container Support not enabled");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   314
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   315
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   316
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   317
  /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   318
   * Find the cgroup mount point for memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   319
   * by reading /proc/self/mountinfo
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   320
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   321
   * Example for docker:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   322
   * 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
   323
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   324
   * Example for host:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   325
   * 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
   326
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   327
  mntinfo = fopen("/proc/self/mountinfo", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   328
  if (mntinfo == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   329
      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
   330
                               os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   331
      return;
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
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   334
  while ((p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   335
    char tmpcgroups[MAXPATHLEN+1];
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   336
    char *cptr = tmpcgroups;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   337
    char *token;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   338
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   339
    // 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
   340
    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
   341
      continue;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   342
    }
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   343
    while ((token = strsep(&cptr, ",")) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   344
      if (strcmp(token, "memory") == 0) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   345
        memory = new CgroupMemorySubsystem(tmproot, tmpmount);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   346
      } else if (strcmp(token, "cpuset") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   347
        cpuset = new CgroupSubsystem(tmproot, tmpmount);
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   348
      } else if (strcmp(token, "cpu") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   349
        cpu = new CgroupSubsystem(tmproot, tmpmount);
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   350
      } else if (strcmp(token, "cpuacct") == 0) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   351
        cpuacct= new CgroupSubsystem(tmproot, tmpmount);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   352
      }
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
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   356
  fclose(mntinfo);
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   357
48880
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   358
  if (memory == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   359
    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
   360
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   361
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   362
  if (cpuset == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   363
    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
   364
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   365
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   366
  if (cpu == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   367
    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
   368
    return;
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   369
  }
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   370
  if (cpuacct == NULL) {
ee49ac008730 8196062: Enable docker container related tests for linux ppc64le
mbaesken
parents: 48438
diff changeset
   371
    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
   372
    return;
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   373
  }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   374
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
   * Read /proc/self/cgroup and map host mount point to
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   377
   * local one via /proc/self/mountinfo content above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   378
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   379
   * Docker example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   380
   * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   381
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   382
   * Host example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   383
   * 5:memory:/user.slice
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   384
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   385
   * Construct a path to the process specific memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   386
   * cgroup directory.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   387
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   388
   * For a container running under Docker from memory example above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   389
   * the paths would be:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   390
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   391
   * /sys/fs/cgroup/memory
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   392
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   393
   * 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
   394
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   395
   * /sys/fs/cgroup/memory/user.slice
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   396
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   397
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   398
  cgroup = fopen("/proc/self/cgroup", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   399
  if (cgroup == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   400
    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
   401
                             os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   402
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   403
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   404
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   405
  while ((p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   406
    char *controllers;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   407
    char *token;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   408
    char *base;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   409
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   410
    /* Skip cgroup number */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   411
    strsep(&p, ":");
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   412
    /* Get controllers and base */
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   413
    controllers = strsep(&p, ":");
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   414
    base = strsep(&p, "\n");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   415
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   416
    if (controllers == NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   417
      continue;
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   418
    }
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   419
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   420
    while ((token = strsep(&controllers, ",")) != NULL) {
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   421
      if (strcmp(token, "memory") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   422
        memory->set_subsystem_path(base);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   423
        jlong hierarchy = uses_mem_hierarchy();
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   424
        if (hierarchy > 0) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   425
          memory->set_hierarchical(true);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   426
        }
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   427
      } else if (strcmp(token, "cpuset") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   428
        cpuset->set_subsystem_path(base);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   429
      } else if (strcmp(token, "cpu") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   430
        cpu->set_subsystem_path(base);
54193
d5da034032e9 8217766: Container Support doesn't work for some Join Controllers combinations
bobv
parents: 54191
diff changeset
   431
      } else if (strcmp(token, "cpuacct") == 0) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   432
        cpuacct->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   433
      }
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
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   437
  fclose(cgroup);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   438
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   439
  // 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
   440
  // command line arguments have been processed.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   441
  if ((mem_limit = memory_limit_in_bytes()) > 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   442
    os::Linux::set_physical_memory(mem_limit);
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   443
    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
   444
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   445
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   446
  _is_containerized = true;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   447
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   448
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   449
49175
6a5decfc5574 8197408: Bad pointer comparison and small cleanup in os_linux.cpp
rehn
parents: 49058
diff changeset
   450
const char * OSContainer::container_type() {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   451
  if (is_containerized()) {
49175
6a5decfc5574 8197408: Bad pointer comparison and small cleanup in os_linux.cpp
rehn
parents: 49058
diff changeset
   452
    return "cgroupv1";
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   453
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   454
    return NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   455
  }
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
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   458
/* uses_mem_hierarchy
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   459
 *
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   460
 * Return whether or not hierarchical cgroup accounting is being
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   461
 * done.
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   462
 *
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   463
 * return:
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   464
 *    A number > 0 if true, or
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   465
 *    OSCONTAINER_ERROR for not supported
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   466
 */
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   467
jlong OSContainer::uses_mem_hierarchy() {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   468
  GET_CONTAINER_INFO(jlong, memory, "/memory.use_hierarchy",
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   469
                    "Use Hierarchy is: " JLONG_FORMAT, JLONG_FORMAT, use_hierarchy);
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   470
  return use_hierarchy;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   471
}
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   472
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   473
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   474
/* memory_limit_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   475
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   476
 * Return the limit of available memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   477
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   478
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   479
 *    memory limit in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   480
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   481
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   482
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   483
jlong OSContainer::memory_limit_in_bytes() {
58655
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   484
  if (!memory->should_check_memory_limit()) {
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   485
    return memory->memory_limit_in_bytes();
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   486
  }
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   487
  jlong memory_limit = read_memory_limit_in_bytes();
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   488
  // Update CgroupMemorySubsystem to avoid re-reading container settings too often
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   489
  memory->set_memory_limit_in_bytes(memory_limit);
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   490
  return memory_limit;
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   491
}
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   492
21a92562f0c2 8232207: Linux os::available_memory re-reads cgroup configuration on every invocation
redestad
parents: 57875
diff changeset
   493
jlong OSContainer::read_memory_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   494
  GET_CONTAINER_INFO(julong, memory, "/memory.limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   495
                     "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   496
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   497
  if (memlimit >= _unlimited_memory) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   498
    log_trace(os, container)("Non-Hierarchical Memory Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   499
    if (memory->is_hierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   500
      const char* matchline = "hierarchical_memory_limit";
54810
258170da6d3a 8223186: HotSpot compile warnings from GCC 9
ysuenaga
parents: 54577
diff changeset
   501
      const char* format = "%s " JULONG_FORMAT;
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   502
      GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   503
                             "Hierarchical Memory Limit is: " JULONG_FORMAT, format, hier_memlimit)
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   504
      if (hier_memlimit >= _unlimited_memory) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   505
        log_trace(os, container)("Hierarchical Memory Limit is: Unlimited");
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   506
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   507
        return (jlong)hier_memlimit;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   508
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   509
    }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   510
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   511
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   512
  else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   513
    return (jlong)memlimit;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   514
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   515
}
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
jlong OSContainer::memory_and_swap_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   518
  GET_CONTAINER_INFO(julong, memory, "/memory.memsw.limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   519
                     "Memory and Swap Limit is: " JULONG_FORMAT, JULONG_FORMAT, memswlimit);
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   520
  if (memswlimit >= _unlimited_memory) {
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   521
    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
   522
    if (memory->is_hierarchical()) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   523
      const char* matchline = "hierarchical_memsw_limit";
54810
258170da6d3a 8223186: HotSpot compile warnings from GCC 9
ysuenaga
parents: 54577
diff changeset
   524
      const char* format = "%s " JULONG_FORMAT;
54577
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   525
      GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   526
                             "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
   527
      if (hier_memlimit >= _unlimited_memory) {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   528
        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
   529
      } else {
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   530
        return (jlong)hier_memlimit;
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   531
      }
1c242c2d037f 8217338: [Containers] Improve systemd slice memory limit support
sgehwolf
parents: 54193
diff changeset
   532
    }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   533
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   534
  } else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   535
    return (jlong)memswlimit;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   536
  }
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   539
jlong OSContainer::memory_soft_limit_in_bytes() {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   540
  GET_CONTAINER_INFO(julong, memory, "/memory.soft_limit_in_bytes",
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   541
                     "Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   542
  if (memsoftlimit >= _unlimited_memory) {
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   543
    log_trace(os, container)("Memory Soft Limit is: Unlimited");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   544
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   545
  } else {
48438
efb9f4c91bde 8194232: Container memory not properly recognized.
goetz
parents: 48161
diff changeset
   546
    return (jlong)memsoftlimit;
47903
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
}
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
/* memory_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   551
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   552
 * Return the amount of used memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   553
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   554
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   555
 *    memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   556
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   557
 *    OSCONTAINER_ERROR for not supported
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
jlong OSContainer::memory_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   560
  GET_CONTAINER_INFO(jlong, memory, "/memory.usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   561
                     "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   562
  return memusage;
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   565
/* memory_max_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   566
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   567
 * Return the maximum amount of used memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   568
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   569
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   570
 *    max memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   571
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   572
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   573
jlong OSContainer::memory_max_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   574
  GET_CONTAINER_INFO(jlong, memory, "/memory.max_usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   575
                     "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   576
  return memmaxusage;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   577
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   578
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   579
/* active_processor_count
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
 * 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
   582
 * VM to use based on these three inputs.
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
 * cpu affinity
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   585
 * cgroup cpu quota & cpu period
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   586
 * cgroup cpu shares
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   587
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   588
 * Algorithm:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   589
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   590
 * Determine the number of available CPUs from sched_getaffinity
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   591
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   592
 * 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
   593
 * required CPUs by dividing quota by period.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   594
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   595
 * 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
   596
 * 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
   597
 * by PER_CPU_SHARES.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   598
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   599
 * 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
   600
 *
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   601
 * 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
   602
 * 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
   603
 *
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   604
 * 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
   605
 * 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
   606
 * 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
   607
 * between shares or quotas.
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   608
 *
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   609
 * 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
   610
 * returned will never exceed the number of active processors.
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   611
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   612
 * return:
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   613
 *    number of CPUs
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   614
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   615
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
   616
  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
   617
  int cpu_count, limit_count;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   618
  int result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   619
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   620
  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
   621
  int quota  = cpu_quota();
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   622
  int period = cpu_period();
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   623
  int share  = cpu_shares();
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   624
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   625
  if (quota > -1 && period > 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   626
    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
   627
    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
   628
  }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   629
  if (share > -1) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   630
    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
   631
    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
   632
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   633
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   634
  // 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
   635
  // on flag PreferContainerQuotaForCPUCount.
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   636
  // 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
   637
  // 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
   638
  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
   639
    if (PreferContainerQuotaForCPUCount) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   640
      limit_count = quota_count;
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   641
    } else {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   642
      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
   643
    }
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   644
  } else if (quota_count != 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   645
    limit_count = quota_count;
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   646
  } else if (share_count != 0) {
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   647
    limit_count = share_count;
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   648
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   649
49058
15765495db12 8197589: Update CPU count algorithm when both cpu shares and quotas are used
bobv
parents: 48880
diff changeset
   650
  result = MIN2(cpu_count, limit_count);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   651
  log_trace(os, container)("OSContainer::active_processor_count: %d", result);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   652
  return result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   653
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   654
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   655
char * OSContainer::cpu_cpuset_cpus() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   656
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.cpus",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   657
                     "cpuset.cpus is: %s", "%1023s", cpus, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   658
  return os::strdup(cpus);
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   661
char * OSContainer::cpu_cpuset_memory_nodes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   662
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.mems",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   663
                     "cpuset.mems is: %s", "%1023s", mems, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   664
  return os::strdup(mems);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   665
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   666
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   667
/* cpu_quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   668
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   669
 * Return the number of milliseconds per period
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   670
 * process is guaranteed to run.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   671
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   672
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   673
 *    quota time in milliseconds
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   674
 *    -1 for no quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   675
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   676
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   677
int OSContainer::cpu_quota() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   678
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_quota_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   679
                     "CPU Quota is: %d", "%d", quota);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   680
  return quota;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   681
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   682
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   683
int OSContainer::cpu_period() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   684
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_period_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   685
                     "CPU Period is: %d", "%d", period);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   686
  return period;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   687
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   688
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   689
/* cpu_shares
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   690
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   691
 * Return the amount of cpu shares available to the process
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   692
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   693
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   694
 *    Share number (typically a number relative to 1024)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   695
 *                 (2048 typically expresses 2 CPUs worth of processing)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   696
 *    -1 for no share setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   697
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   698
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   699
int OSContainer::cpu_shares() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   700
  GET_CONTAINER_INFO(int, cpu, "/cpu.shares",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   701
                     "CPU Shares is: %d", "%d", shares);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   702
  // Convert 1024 to no shares setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   703
  if (shares == 1024) return -1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   704
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   705
  return shares;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   706
}