1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <string.h>
  26 #include <math.h>
  27 #include <errno.h>
  28 #include "cgroupV1Subsystem_linux.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "runtime/globals.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 /*
  36  * Set directory to subsystem specific files based
  37  * on the contents of the mountinfo and cgroup files.
  38  */
  39 void CgroupV1Controller::set_subsystem_path(char *cgroup_path) {
  40   char buf[MAXPATHLEN+1];
  41   if (_root != NULL && cgroup_path != NULL) {
  42     if (strcmp(_root, "/") == 0) {
  43       int buflen;
  44       strncpy(buf, _mount_point, MAXPATHLEN);
  45       buf[MAXPATHLEN-1] = '\0';
  46       if (strcmp(cgroup_path,"/") != 0) {
  47         buflen = strlen(buf);
  48         if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
  49           return;
  50         }
  51         strncat(buf, cgroup_path, MAXPATHLEN-buflen);
  52         buf[MAXPATHLEN-1] = '\0';
  53       }
  54       _path = os::strdup(buf);
  55     } else {
  56       if (strcmp(_root, cgroup_path) == 0) {
  57         strncpy(buf, _mount_point, MAXPATHLEN);
  58         buf[MAXPATHLEN-1] = '\0';
  59         _path = os::strdup(buf);
  60       } else {
  61         char *p = strstr(cgroup_path, _root);
  62         if (p != NULL && p == _root) {
  63           if (strlen(cgroup_path) > strlen(_root)) {
  64             int buflen;
  65             strncpy(buf, _mount_point, MAXPATHLEN);
  66             buf[MAXPATHLEN-1] = '\0';
  67             buflen = strlen(buf);
  68             if ((buflen + strlen(cgroup_path) - strlen(_root)) > (MAXPATHLEN-1)) {
  69               return;
  70             }
  71             strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
  72             buf[MAXPATHLEN-1] = '\0';
  73             _path = os::strdup(buf);
  74           }
  75         }
  76       }
  77     }
  78   }
  79 }
  80 
  81 /* uses_mem_hierarchy
  82  *
  83  * Return whether or not hierarchical cgroup accounting is being
  84  * done.
  85  *
  86  * return:
  87  *    A number > 0 if true, or
  88  *    OSCONTAINER_ERROR for not supported
  89  */
  90 jlong CgroupV1MemoryController::uses_mem_hierarchy() {
  91   GET_CONTAINER_INFO(jlong, this, "/memory.use_hierarchy",
  92                     "Use Hierarchy is: " JLONG_FORMAT, JLONG_FORMAT, use_hierarchy);
  93   return use_hierarchy;
  94 }
  95 
  96 void CgroupV1MemoryController::set_subsystem_path(char *cgroup_path) {
  97   CgroupV1Controller::set_subsystem_path(cgroup_path);
  98   jlong hierarchy = uses_mem_hierarchy();
  99   if (hierarchy > 0) {
 100     set_hierarchical(true);
 101   }
 102 }
 103 
 104 /* memory_limit_in_bytes
 105  *
 106  * Return the limit of available memory for this process.
 107  *
 108  * return:
 109  *    memory limit in bytes or
 110  *    -1 for unlimited
 111  *    OSCONTAINER_ERROR for not supported
 112  */
 113 jlong CgroupV1Subsystem::memory_limit_in_bytes() {
 114   GET_CONTAINER_INFO(julong, _memory, "/memory.limit_in_bytes",
 115                      "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit);
 116 
 117   if (memlimit >= _unlimited_memory) {
 118     log_trace(os, container)("Non-Hierarchical Memory Limit is: Unlimited");
 119     if (_memory->is_hierarchical()) {
 120       const char* matchline = "hierarchical_memory_limit";
 121       const char* format = "%s " JULONG_FORMAT;
 122       GET_CONTAINER_INFO_LINE(julong, _memory, "/memory.stat", matchline,
 123                              "Hierarchical Memory Limit is: " JULONG_FORMAT, format, hier_memlimit)
 124       if (hier_memlimit >= _unlimited_memory) {
 125         log_trace(os, container)("Hierarchical Memory Limit is: Unlimited");
 126       } else {
 127         return (jlong)hier_memlimit;
 128       }
 129     }
 130     return (jlong)-1;
 131   }
 132   else {
 133     return (jlong)memlimit;
 134   }
 135 }
 136 
 137 jlong CgroupV1Subsystem::memory_and_swap_limit_in_bytes() {
 138   GET_CONTAINER_INFO(julong, _memory, "/memory.memsw.limit_in_bytes",
 139                      "Memory and Swap Limit is: " JULONG_FORMAT, JULONG_FORMAT, memswlimit);
 140   if (memswlimit >= _unlimited_memory) {
 141     log_trace(os, container)("Non-Hierarchical Memory and Swap Limit is: Unlimited");
 142     if (_memory->is_hierarchical()) {
 143       const char* matchline = "hierarchical_memsw_limit";
 144       const char* format = "%s " JULONG_FORMAT;
 145       GET_CONTAINER_INFO_LINE(julong, _memory, "/memory.stat", matchline,
 146                              "Hierarchical Memory and Swap Limit is : " JULONG_FORMAT, format, hier_memlimit)
 147       if (hier_memlimit >= _unlimited_memory) {
 148         log_trace(os, container)("Hierarchical Memory and Swap Limit is: Unlimited");
 149       } else {
 150         return (jlong)hier_memlimit;
 151       }
 152     }
 153     return (jlong)-1;
 154   } else {
 155     return (jlong)memswlimit;
 156   }
 157 }
 158 
 159 jlong CgroupV1Subsystem::memory_soft_limit_in_bytes() {
 160   GET_CONTAINER_INFO(julong, _memory, "/memory.soft_limit_in_bytes",
 161                      "Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
 162   if (memsoftlimit >= _unlimited_memory) {
 163     log_trace(os, container)("Memory Soft Limit is: Unlimited");
 164     return (jlong)-1;
 165   } else {
 166     return (jlong)memsoftlimit;
 167   }
 168 }
 169 
 170 /* memory_usage_in_bytes
 171  *
 172  * Return the amount of used memory for this process.
 173  *
 174  * return:
 175  *    memory usage in bytes or
 176  *    -1 for unlimited
 177  *    OSCONTAINER_ERROR for not supported
 178  */
 179 jlong CgroupV1Subsystem::memory_usage_in_bytes() {
 180   GET_CONTAINER_INFO(jlong, _memory, "/memory.usage_in_bytes",
 181                      "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
 182   return memusage;
 183 }
 184 
 185 /* memory_max_usage_in_bytes
 186  *
 187  * Return the maximum amount of used memory for this process.
 188  *
 189  * return:
 190  *    max memory usage in bytes or
 191  *    OSCONTAINER_ERROR for not supported
 192  */
 193 jlong CgroupV1Subsystem::memory_max_usage_in_bytes() {
 194   GET_CONTAINER_INFO(jlong, _memory, "/memory.max_usage_in_bytes",
 195                      "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
 196   return memmaxusage;
 197 }
 198 
 199 char * CgroupV1Subsystem::cpu_cpuset_cpus() {
 200   GET_CONTAINER_INFO_CPTR(cptr, _cpuset, "/cpuset.cpus",
 201                      "cpuset.cpus is: %s", "%1023s", cpus, 1024);
 202   return os::strdup(cpus);
 203 }
 204 
 205 char * CgroupV1Subsystem::cpu_cpuset_memory_nodes() {
 206   GET_CONTAINER_INFO_CPTR(cptr, _cpuset, "/cpuset.mems",
 207                      "cpuset.mems is: %s", "%1023s", mems, 1024);
 208   return os::strdup(mems);
 209 }
 210 
 211 /* cpu_quota
 212  *
 213  * Return the number of milliseconds per period
 214  * process is guaranteed to run.
 215  *
 216  * return:
 217  *    quota time in milliseconds
 218  *    -1 for no quota
 219  *    OSCONTAINER_ERROR for not supported
 220  */
 221 int CgroupV1Subsystem::cpu_quota() {
 222   GET_CONTAINER_INFO(int, _cpu, "/cpu.cfs_quota_us",
 223                      "CPU Quota is: %d", "%d", quota);
 224   return quota;
 225 }
 226 
 227 int CgroupV1Subsystem::cpu_period() {
 228   GET_CONTAINER_INFO(int, _cpu, "/cpu.cfs_period_us",
 229                      "CPU Period is: %d", "%d", period);
 230   return period;
 231 }
 232 
 233 /* cpu_shares
 234  *
 235  * Return the amount of cpu shares available to the process
 236  *
 237  * return:
 238  *    Share number (typically a number relative to 1024)
 239  *                 (2048 typically expresses 2 CPUs worth of processing)
 240  *    -1 for no share setup
 241  *    OSCONTAINER_ERROR for not supported
 242  */
 243 int CgroupV1Subsystem::cpu_shares() {
 244   GET_CONTAINER_INFO(int, _cpu, "/cpu.shares",
 245                      "CPU Shares is: %d", "%d", shares);
 246   // Convert 1024 to no shares setup
 247   if (shares == 1024) return -1;
 248 
 249   return shares;
 250 }