src/hotspot/os/linux/osContainer_linux.cpp
author bobv
Thu, 30 Nov 2017 09:49:45 -0500
changeset 48161 fed0e4b11604
parent 47903 7f22774a5f42
child 48438 efb9f4c91bde
permissions -rw-r--r--
8192154: JVM crashes inside some chroot environments on linux Reviewed-by: dholmes, sgehwolf, glaubitz
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
/*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    34
/*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    35
 * Warning: Some linux distros use 0x7FFFFFFFFFFFF000
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    36
 * and others use 0x7FFFFFFFFFFFFFFF for unlimited.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    37
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    38
#define UNLIMITED_MEM CONST64(0x7FFFFFFFFFFFF000)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    39
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    40
#define PER_CPU_SHARES 1024
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    41
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    42
bool  OSContainer::_is_initialized   = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    43
bool  OSContainer::_is_containerized = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    44
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    45
class CgroupSubsystem: CHeapObj<mtInternal> {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    46
 friend class OSContainer;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    47
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    48
 private:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    49
    /* mountinfo contents */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    50
    char *_root;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    51
    char *_mount_point;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    52
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    53
    /* Constructed subsystem directory */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    54
    char *_path;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    55
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    56
 public:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    57
    CgroupSubsystem(char *root, char *mountpoint) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    58
      _root = os::strdup(root);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    59
      _mount_point = os::strdup(mountpoint);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    60
      _path = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    61
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    62
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    63
    /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    64
     * Set directory to subsystem specific files based
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    65
     * on the contents of the mountinfo and cgroup files.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    66
     */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    67
    void set_subsystem_path(char *cgroup_path) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    68
      char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    69
      if (_root != NULL && cgroup_path != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    70
        if (strcmp(_root, "/") == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    71
          int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    72
          strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    73
          buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    74
          if (strcmp(cgroup_path,"/") != 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    75
            buflen = strlen(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    76
            if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    77
              return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    78
            }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    79
            strncat(buf, cgroup_path, MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    80
            buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    81
          }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    82
          _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    83
        } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    84
          if (strcmp(_root, cgroup_path) == 0) {
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
            _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    88
          } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    89
            char *p = strstr(_root, cgroup_path);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    90
            if (p != NULL && p == _root) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    91
              if (strlen(cgroup_path) > strlen(_root)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    92
                int buflen;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    93
                strncpy(buf, _mount_point, MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    94
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    95
                buflen = strlen(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    96
                if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    97
                  return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    98
                }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
    99
                strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   100
                buf[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   101
                _path = os::strdup(buf);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   102
              }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   103
            }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   104
          }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   105
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   106
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   107
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   108
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   109
    char *subsystem_path() { return _path; }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   110
};
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
CgroupSubsystem* memory = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   113
CgroupSubsystem* cpuset = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   114
CgroupSubsystem* cpu = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   115
CgroupSubsystem* cpuacct = NULL;
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
typedef char * cptr;
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
PRAGMA_DIAG_PUSH
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   120
PRAGMA_FORMAT_NONLITERAL_IGNORED
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   121
template <typename T> int subsystem_file_contents(CgroupSubsystem* c,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   122
                                              const char *filename,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   123
                                              const char *scan_fmt,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   124
                                              T returnval) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   125
  FILE *fp = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   126
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   127
  char file[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   128
  char buf[MAXPATHLEN+1];
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
  if (c != NULL && c->subsystem_path() != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   131
    strncpy(file, c->subsystem_path(), MAXPATHLEN);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   132
    file[MAXPATHLEN-1] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   133
    int filelen = strlen(file);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   134
    if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   135
       log_debug(os, container)("File path too long %s, %s", file, filename);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   136
       return OSCONTAINER_ERROR;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   137
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   138
    strncat(file, filename, MAXPATHLEN-filelen);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   139
    log_trace(os, container)("Path to %s is %s", filename, file);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   140
    fp = fopen(file, "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   141
    if (fp != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   142
      p = fgets(buf, MAXPATHLEN, fp);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   143
      if (p != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   144
        int matched = sscanf(p, scan_fmt, returnval);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   145
        if (matched == 1) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   146
          fclose(fp);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   147
          return 0;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   148
        } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   149
          log_debug(os, container)("Type %s not found in file %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   150
                                     scan_fmt , file);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   151
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   152
      } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   153
        log_debug(os, container)("Empty file %s", file);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   154
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   155
    } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   156
      log_debug(os, container)("Open of file %s failed, %s", file,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   157
                               os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   158
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   159
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   160
  if (fp != NULL)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   161
    fclose(fp);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   162
  return OSCONTAINER_ERROR;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   163
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   164
PRAGMA_DIAG_POP
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   165
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   166
#define GET_CONTAINER_INFO(return_type, subsystem, filename,              \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   167
                           logstring, scan_fmt, variable)                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   168
  return_type variable;                                                   \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   169
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   170
  int err;                                                                \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   171
  err = subsystem_file_contents(subsystem,                                \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   172
                                filename,                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   173
                                scan_fmt,                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   174
                                &variable);                               \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   175
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   176
    return (return_type) OSCONTAINER_ERROR;                               \
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
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   179
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   180
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   181
#define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename,         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   182
                               logstring, scan_fmt, variable, bufsize)    \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   183
  char variable[bufsize];                                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   184
{                                                                         \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   185
  int err;                                                                \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   186
  err = subsystem_file_contents(subsystem,                                \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   187
                                filename,                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   188
                                scan_fmt,                                 \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   189
                                variable);                                \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   190
  if (err != 0)                                                           \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   191
    return (return_type) NULL;                                            \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   192
                                                                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   193
  log_trace(os, container)(logstring, variable);                          \
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   194
}
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
/* init
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
 * Initialize the container support and determine if
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   199
 * we are running under cgroup control.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   200
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   201
void OSContainer::init() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   202
  int mountid;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   203
  int parentid;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   204
  int major;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   205
  int minor;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   206
  FILE *mntinfo = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   207
  FILE *cgroup = NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   208
  char buf[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   209
  char tmproot[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   210
  char tmpmount[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   211
  char tmpbase[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   212
  char *p;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   213
  jlong mem_limit;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   214
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   215
  assert(!_is_initialized, "Initializing OSContainer more than once");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   216
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   217
  _is_initialized = true;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   218
  _is_containerized = false;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   219
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   220
  log_trace(os, container)("OSContainer::init: Initializing Container Support");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   221
  if (!UseContainerSupport) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   222
    log_trace(os, container)("Container Support not enabled");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   223
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   224
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   225
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   226
  /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   227
   * Find the cgroup mount point for memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   228
   * by reading /proc/self/mountinfo
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   229
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   230
   * Example for docker:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   231
   * 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
   232
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   233
   * Example for host:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   234
   * 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
   235
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   236
  mntinfo = fopen("/proc/self/mountinfo", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   237
  if (mntinfo == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   238
      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
   239
                               os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   240
      return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   241
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   242
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   243
  while ( (p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   244
    // 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
   245
    char fstype[MAXPATHLEN+1];
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   246
    fstype[0] = '\0';
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   247
    char *s =  strstr(p, " - ");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   248
    if (s != NULL &&
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   249
        sscanf(s, " - %s", fstype) == 1 &&
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   250
        strcmp(fstype, "cgroup") == 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   251
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   252
      if (strstr(p, "memory") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   253
        int matched = sscanf(p, "%d %d %d:%d %s %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   254
                             &mountid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   255
                             &parentid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   256
                             &major,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   257
                             &minor,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   258
                             tmproot,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   259
                             tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   260
        if (matched == 6) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   261
          memory = new CgroupSubsystem(tmproot, tmpmount);
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
        else
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   264
          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
   265
      } else if (strstr(p, "cpuset") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   266
        int matched = sscanf(p, "%d %d %d:%d %s %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   267
                             &mountid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   268
                             &parentid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   269
                             &major,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   270
                             &minor,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   271
                             tmproot,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   272
                             tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   273
        if (matched == 6) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   274
          cpuset = new CgroupSubsystem(tmproot, tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   275
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   276
        else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   277
          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
   278
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   279
      } else if (strstr(p, "cpu,cpuacct") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   280
        int matched = sscanf(p, "%d %d %d:%d %s %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   281
                             &mountid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   282
                             &parentid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   283
                             &major,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   284
                             &minor,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   285
                             tmproot,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   286
                             tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   287
        if (matched == 6) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   288
          cpu = new CgroupSubsystem(tmproot, tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   289
          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   290
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   291
        else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   292
          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
   293
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   294
      } else if (strstr(p, "cpuacct") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   295
        int matched = sscanf(p, "%d %d %d:%d %s %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   296
                             &mountid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   297
                             &parentid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   298
                             &major,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   299
                             &minor,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   300
                             tmproot,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   301
                             tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   302
        if (matched == 6) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   303
          cpuacct = new CgroupSubsystem(tmproot, tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   304
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   305
        else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   306
          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
   307
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   308
      } else if (strstr(p, "cpu") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   309
        int matched = sscanf(p, "%d %d %d:%d %s %s",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   310
                             &mountid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   311
                             &parentid,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   312
                             &major,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   313
                             &minor,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   314
                             tmproot,
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   315
                             tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   316
        if (matched == 6) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   317
          cpu = new CgroupSubsystem(tmproot, tmpmount);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   318
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   319
        else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   320
          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
   321
        }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   322
      }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   323
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   324
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   325
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   326
  fclose(mntinfo);
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   327
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   328
  if (memory == NULL || cpuset == NULL || cpu == NULL || cpuacct == NULL) {
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   329
    log_debug(os, container)("Required cgroup subsystems not found");
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   330
    return;
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   331
  }
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   332
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   333
  /*
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   334
   * Read /proc/self/cgroup and map host mount point to
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   335
   * local one via /proc/self/mountinfo content above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   336
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   337
   * Docker example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   338
   * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   339
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   340
   * Host example:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   341
   * 5:memory:/user.slice
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
   * Construct a path to the process specific memory and cpuset
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   344
   * cgroup directory.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   345
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   346
   * For a container running under Docker from memory example above
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   347
   * the paths would be:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   348
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   349
   * /sys/fs/cgroup/memory
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   350
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   351
   * 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
   352
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   353
   * /sys/fs/cgroup/memory/user.slice
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   354
   *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   355
   */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   356
  cgroup = fopen("/proc/self/cgroup", "r");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   357
  if (cgroup == NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   358
    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
   359
                             os::strerror(errno));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   360
    return;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   361
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   362
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   363
  while ( (p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   364
    int cgno;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   365
    int matched;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   366
    char *controller;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   367
    char *base;
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
    /* Skip cgroup number */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   370
    strsep(&p, ":");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   371
    /* Get controller and base */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   372
    controller = strsep(&p, ":");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   373
    base = strsep(&p, "\n");
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
    if (controller != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   376
      if (strstr(controller, "memory") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   377
        memory->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   378
      } else if (strstr(controller, "cpuset") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   379
        cpuset->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   380
      } else if (strstr(controller, "cpu,cpuacct") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   381
        cpu->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   382
        cpuacct->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   383
      } else if (strstr(controller, "cpuacct") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   384
        cpuacct->set_subsystem_path(base);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   385
      } else if (strstr(controller, "cpu") != NULL) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   386
        cpu->set_subsystem_path(base);
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
    }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   389
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   390
48161
fed0e4b11604 8192154: JVM crashes inside some chroot environments on linux
bobv
parents: 47903
diff changeset
   391
  fclose(cgroup);
47903
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   392
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   393
  // 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
   394
  // command line arguments have been processed.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   395
  if ((mem_limit = memory_limit_in_bytes()) > 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   396
    os::Linux::set_physical_memory(mem_limit);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   397
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   398
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   399
  _is_containerized = true;
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
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   402
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   403
char * OSContainer::container_type() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   404
  if (is_containerized()) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   405
    return (char *)"cgroupv1";
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   406
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   407
    return NULL;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   408
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   409
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   410
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   411
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   412
/* memory_limit_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   413
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   414
 * Return the limit of available memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   415
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   416
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   417
 *    memory limit in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   418
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   419
 *    OSCONTAINER_ERROR for not supported
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
jlong OSContainer::memory_limit_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   422
  GET_CONTAINER_INFO(jlong, memory, "/memory.limit_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   423
                     "Memory Limit is: " JLONG_FORMAT, JLONG_FORMAT, memlimit);
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
  if (memlimit >= UNLIMITED_MEM) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   426
    log_trace(os, container)("Memory Limit is: Unlimited");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   427
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   428
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   429
  else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   430
    return memlimit;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   431
  }
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
jlong OSContainer::memory_and_swap_limit_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   435
  GET_CONTAINER_INFO(jlong, memory, "/memory.memsw.limit_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   436
                     "Memory and Swap Limit is: " JLONG_FORMAT, JLONG_FORMAT, memswlimit);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   437
  if (memswlimit >= UNLIMITED_MEM) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   438
    log_trace(os, container)("Memory and Swap Limit is: Unlimited");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   439
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   440
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   441
    return memswlimit;
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   445
jlong OSContainer::memory_soft_limit_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   446
  GET_CONTAINER_INFO(jlong, memory, "/memory.soft_limit_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   447
                     "Memory Soft Limit is: " JLONG_FORMAT, JLONG_FORMAT, memsoftlimit);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   448
  if (memsoftlimit >= UNLIMITED_MEM) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   449
    log_trace(os, container)("Memory Soft Limit is: Unlimited");
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   450
    return (jlong)-1;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   451
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   452
    return memsoftlimit;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   453
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   454
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   455
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   456
/* memory_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   457
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   458
 * Return the amount of used memory for this process.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   459
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   460
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   461
 *    memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   462
 *    -1 for unlimited
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   463
 *    OSCONTAINER_ERROR for not supported
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
jlong OSContainer::memory_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   466
  GET_CONTAINER_INFO(jlong, memory, "/memory.usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   467
                     "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   468
  return memusage;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   469
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   470
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   471
/* memory_max_usage_in_bytes
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   472
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   473
 * Return the maximum amount of used memory for this process.
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
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   476
 *    max memory usage in bytes or
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   477
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   478
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   479
jlong OSContainer::memory_max_usage_in_bytes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   480
  GET_CONTAINER_INFO(jlong, memory, "/memory.max_usage_in_bytes",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   481
                     "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   482
  return memmaxusage;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   483
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   484
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   485
/* active_processor_count
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
 * Calculate an appropriate number of active processors for the
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   488
 * VM to use based on these three cgroup options.
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
 * cpu affinity
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   491
 * cpu quota & cpu period
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   492
 * cpu shares
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   493
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   494
 * Algorithm:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   495
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   496
 * Determine the number of available CPUs from sched_getaffinity
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   497
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   498
 * 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
   499
 * required CPUs by dividing quota by period.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   500
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   501
 * If shares are in effect (shares != -1), calculate the number
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   502
 * of cpus required for the shares by dividing the share value
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   503
 * by PER_CPU_SHARES.
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
 * 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
   506
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   507
 * Return the smaller number from the three different settings.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   508
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   509
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   510
 *    number of cpus
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   511
 *    OSCONTAINER_ERROR if failure occured during extract of cpuset info
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   512
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   513
int OSContainer::active_processor_count() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   514
  int cpu_count, share_count, quota_count;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   515
  int share, quota, period;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   516
  int result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   517
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   518
  cpu_count = os::Linux::active_processor_count();
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   519
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   520
  share = cpu_shares();
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   521
  if (share > -1) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   522
    share_count = ceilf((float)share / (float)PER_CPU_SHARES);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   523
    log_trace(os, container)("cpu_share count: %d", share_count);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   524
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   525
    share_count = cpu_count;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   526
  }
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
  quota = cpu_quota();
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   529
  period = cpu_period();
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   530
  if (quota > -1 && period > 0) {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   531
    quota_count = ceilf((float)quota / (float)period);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   532
    log_trace(os, container)("quota_count: %d", quota_count);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   533
  } else {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   534
    quota_count = cpu_count;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   535
  }
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   536
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   537
  result = MIN2(cpu_count, MIN2(share_count, quota_count));
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   538
  log_trace(os, container)("OSContainer::active_processor_count: %d", result);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   539
  return result;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   540
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   541
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   542
char * OSContainer::cpu_cpuset_cpus() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   543
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.cpus",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   544
                     "cpuset.cpus is: %s", "%1023s", cpus, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   545
  return os::strdup(cpus);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   546
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   547
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   548
char * OSContainer::cpu_cpuset_memory_nodes() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   549
  GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.mems",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   550
                     "cpuset.mems is: %s", "%1023s", mems, 1024);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   551
  return os::strdup(mems);
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
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   554
/* cpu_quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   555
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   556
 * Return the number of milliseconds per period
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   557
 * process is guaranteed to run.
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   558
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   559
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   560
 *    quota time in milliseconds
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   561
 *    -1 for no quota
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   562
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   563
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   564
int OSContainer::cpu_quota() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   565
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_quota_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   566
                     "CPU Quota is: %d", "%d", quota);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   567
  return quota;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   568
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   569
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   570
int OSContainer::cpu_period() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   571
  GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_period_us",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   572
                     "CPU Period is: %d", "%d", period);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   573
  return period;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   574
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   575
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   576
/* cpu_shares
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   577
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   578
 * Return the amount of cpu shares available to the process
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   579
 *
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   580
 * return:
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   581
 *    Share number (typically a number relative to 1024)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   582
 *                 (2048 typically expresses 2 CPUs worth of processing)
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   583
 *    -1 for no share setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   584
 *    OSCONTAINER_ERROR for not supported
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   585
 */
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   586
int OSContainer::cpu_shares() {
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   587
  GET_CONTAINER_INFO(int, cpu, "/cpu.shares",
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   588
                     "CPU Shares is: %d", "%d", shares);
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   589
  // Convert 1024 to no shares setup
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   590
  if (shares == 1024) return -1;
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
  return shares;
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   593
}
7f22774a5f42 8146115: Improve docker container detection and resource configuration usage
bobv
parents:
diff changeset
   594