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