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