1 /*
   2  * Copyright (c) 2017, 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 "utilities/globalDefinitions.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/os.hpp"
  31 #include "logging/log.hpp"
  32 #include "osContainer_linux.hpp"
  33 
  34 /*
  35  * PER_CPU_SHARES has been set to 1024 because CPU shares' quota
  36  * is commonly used in cloud frameworks like Kubernetes[1],
  37  * AWS[2] and Mesos[3] in a similar way. They spawn containers with
  38  * --cpu-shares option values scaled by PER_CPU_SHARES. Thus, we do
  39  * the inverse for determining the number of possible available
  40  * CPUs to the JVM inside a container. See JDK-8216366.
  41  *
  42  * [1] https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-cpu
  43  *     In particular:
  44  *        When using Docker:
  45  *          The spec.containers[].resources.requests.cpu is converted to its core value, which is potentially
  46  *          fractional, and multiplied by 1024. The greater of this number or 2 is used as the value of the
  47  *          --cpu-shares flag in the docker run command.
  48  * [2] https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerDefinition.html
  49  * [3] https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/docker/docker.cpp#L648
  50  *     https://github.com/apache/mesos/blob/3478e344fb77d931f6122980c6e94cd3913c441d/src/slave/containerizer/mesos/isolators/cgroups/constants.hpp#L30
  51  */
  52 #define PER_CPU_SHARES 1024
  53 
  54 bool  OSContainer::_is_initialized   = false;
  55 bool  OSContainer::_is_containerized = false;
  56 julong _unlimited_memory;
  57 
  58 class CgroupSubsystem: CHeapObj<mtInternal> {
  59  friend class OSContainer;
  60 
  61  private:
  62     /* mountinfo contents */
  63     char *_root;
  64     char *_mount_point;
  65 
  66     /* Constructed subsystem directory */
  67     char *_path;
  68 
  69  public:
  70     CgroupSubsystem(char *root, char *mountpoint) {
  71       _root = os::strdup(root);
  72       _mount_point = os::strdup(mountpoint);
  73       _path = NULL;
  74     }
  75 
  76     /*
  77      * Set directory to subsystem specific files based
  78      * on the contents of the mountinfo and cgroup files.
  79      */
  80     void set_subsystem_path(char *cgroup_path) {
  81       char buf[MAXPATHLEN+1];
  82       if (_root != NULL && cgroup_path != NULL) {
  83         if (strcmp(_root, "/") == 0) {
  84           int buflen;
  85           strncpy(buf, _mount_point, MAXPATHLEN);
  86           buf[MAXPATHLEN-1] = '\0';
  87           if (strcmp(cgroup_path,"/") != 0) {
  88             buflen = strlen(buf);
  89             if ((buflen + strlen(cgroup_path)) > (MAXPATHLEN-1)) {
  90               return;
  91             }
  92             strncat(buf, cgroup_path, MAXPATHLEN-buflen);
  93             buf[MAXPATHLEN-1] = '\0';
  94           }
  95           _path = os::strdup(buf);
  96         } else {
  97           if (strcmp(_root, cgroup_path) == 0) {
  98             strncpy(buf, _mount_point, MAXPATHLEN);
  99             buf[MAXPATHLEN-1] = '\0';
 100             _path = os::strdup(buf);
 101           } else {
 102             char *p = strstr(cgroup_path, _root);
 103             if (p != NULL && p == _root) {
 104               if (strlen(cgroup_path) > strlen(_root)) {
 105                 int buflen;
 106                 strncpy(buf, _mount_point, MAXPATHLEN);
 107                 buf[MAXPATHLEN-1] = '\0';
 108                 buflen = strlen(buf);
 109                 if ((buflen + strlen(cgroup_path) - strlen(_root)) > (MAXPATHLEN-1)) {
 110                   return;
 111                 }
 112                 strncat(buf, cgroup_path + strlen(_root), MAXPATHLEN-buflen);
 113                 buf[MAXPATHLEN-1] = '\0';
 114                 _path = os::strdup(buf);
 115               }
 116             }
 117           }
 118         }
 119       }
 120     }
 121 
 122     char *subsystem_path() { return _path; }
 123 };
 124 
 125 class CgroupMemorySubsystem: CgroupSubsystem {
 126  friend class OSContainer;
 127 
 128  private:
 129     /* Some container runtimes set limits via cgroup
 130      * hierarchy. If set to true consider also memory.stat
 131      * file if everything else seems unlimited */
 132     bool _uses_mem_hierarchy;
 133 
 134  public:
 135     CgroupMemorySubsystem(char *root, char *mountpoint) : CgroupSubsystem::CgroupSubsystem(root, mountpoint) {
 136       _uses_mem_hierarchy = false;
 137     }
 138 
 139     bool is_hierarchical() { return _uses_mem_hierarchy; }
 140     void set_hierarchical(bool value) { _uses_mem_hierarchy = value; }
 141 };
 142 
 143 CgroupMemorySubsystem* memory = NULL;
 144 CgroupSubsystem* cpuset = NULL;
 145 CgroupSubsystem* cpu = NULL;
 146 CgroupSubsystem* cpuacct = NULL;
 147 
 148 typedef char * cptr;
 149 
 150 PRAGMA_DIAG_PUSH
 151 PRAGMA_FORMAT_NONLITERAL_IGNORED
 152 template <typename T> int subsystem_file_contents(CgroupSubsystem* c,
 153                                               const char *filename,
 154                                               const char *scan_fmt,
 155                                               T returnval) {
 156   FILE *fp = NULL;
 157   char *p;
 158   char file[MAXPATHLEN+1];
 159   char buf[MAXPATHLEN+1];
 160 
 161   if (c == NULL) {
 162     log_debug(os, container)("subsystem_file_contents: CgroupSubsytem* is NULL");
 163     return OSCONTAINER_ERROR;
 164   }
 165   if (c->subsystem_path() == NULL) {
 166     log_debug(os, container)("subsystem_file_contents: subsystem path is NULL");
 167     return OSCONTAINER_ERROR;
 168   }
 169 
 170   strncpy(file, c->subsystem_path(), MAXPATHLEN);
 171   file[MAXPATHLEN-1] = '\0';
 172   int filelen = strlen(file);
 173   if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
 174     log_debug(os, container)("File path too long %s, %s", file, filename);
 175     return OSCONTAINER_ERROR;
 176   }
 177   strncat(file, filename, MAXPATHLEN-filelen);
 178   log_trace(os, container)("Path to %s is %s", filename, file);
 179   fp = fopen(file, "r");
 180   if (fp != NULL) {
 181     p = fgets(buf, MAXPATHLEN, fp);
 182     if (p != NULL) {
 183       int matched = sscanf(p, scan_fmt, returnval);
 184       if (matched == 1) {
 185         fclose(fp);
 186         return 0;
 187       } else {
 188         log_debug(os, container)("Type %s not found in file %s", scan_fmt, file);
 189       }
 190     } else {
 191       log_debug(os, container)("Empty file %s", file);
 192     }
 193   } else {
 194     log_debug(os, container)("Open of file %s failed, %s", file, os::strerror(errno));
 195   }
 196   if (fp != NULL)
 197     fclose(fp);
 198   return OSCONTAINER_ERROR;
 199 }
 200 PRAGMA_DIAG_POP
 201 
 202 PRAGMA_DIAG_PUSH
 203 PRAGMA_FORMAT_NONLITERAL_IGNORED
 204 template <typename T> int subsystem_file_line_contents(CgroupSubsystem* c,
 205                                               const char *filename,
 206                                               const char *matchline,
 207                                               const char *scan_fmt,
 208                                               T returnval) {
 209   FILE *fp = NULL;
 210   char *p;
 211   char file[MAXPATHLEN+1];
 212   char buf[MAXPATHLEN+1];
 213   char discard[MAXPATHLEN+1];
 214 
 215   if (c == NULL) {
 216     log_debug(os, container)("subsystem_file_contents: CgroupSubsytem* is NULL");
 217     return OSCONTAINER_ERROR;
 218   }
 219   if (c->subsystem_path() == NULL) {
 220     log_debug(os, container)("subsystem_file_contents: subsystem path is NULL");
 221     return OSCONTAINER_ERROR;
 222   }
 223 
 224   strncpy(file, c->subsystem_path(), MAXPATHLEN);
 225   file[MAXPATHLEN-1] = '\0';
 226   int filelen = strlen(file);
 227   if ((filelen + strlen(filename)) > (MAXPATHLEN-1)) {
 228     log_debug(os, container)("File path too long %s, %s", file, filename);
 229     return OSCONTAINER_ERROR;
 230   }
 231   strncat(file, filename, MAXPATHLEN-filelen);
 232   log_trace(os, container)("Path to %s is %s", filename, file);
 233   fp = fopen(file, "r");
 234   if (fp != NULL) {
 235     int err = 0;
 236     while ((p = fgets(buf, MAXPATHLEN, fp)) != NULL) {
 237       if (strstr(p, matchline) != NULL) {
 238         // discard matchline string prefix
 239         int matched = sscanf(p, scan_fmt, discard, returnval);
 240         if (matched == 2) {
 241           fclose(fp);
 242           return 0;
 243         } else {
 244           err = 1;
 245           log_debug(os, container)("Type %s not found in file %s", scan_fmt, file);
 246         }
 247       }
 248     }
 249     if (err == 0) {
 250       log_debug(os, container)("Empty file %s, or no match found for %s", file, matchline);
 251     }
 252   } else {
 253     log_debug(os, container)("Open of file %s failed, %s", file, os::strerror(errno));
 254   }
 255   if (fp != NULL)
 256     fclose(fp);
 257   return OSCONTAINER_ERROR;
 258 }
 259 PRAGMA_DIAG_POP
 260 
 261 #define GET_CONTAINER_INFO(return_type, subsystem, filename,              \
 262                            logstring, scan_fmt, variable)                 \
 263   return_type variable;                                                   \
 264 {                                                                         \
 265   int err;                                                                \
 266   err = subsystem_file_contents(subsystem,                                \
 267                                 filename,                                 \
 268                                 scan_fmt,                                 \
 269                                 &variable);                               \
 270   if (err != 0)                                                           \
 271     return (return_type) OSCONTAINER_ERROR;                               \
 272                                                                           \
 273   log_trace(os, container)(logstring, variable);                          \
 274 }
 275 
 276 #define GET_CONTAINER_INFO_CPTR(return_type, subsystem, filename,         \
 277                                logstring, scan_fmt, variable, bufsize)    \
 278   char variable[bufsize];                                                 \
 279 {                                                                         \
 280   int err;                                                                \
 281   err = subsystem_file_contents(subsystem,                                \
 282                                 filename,                                 \
 283                                 scan_fmt,                                 \
 284                                 variable);                                \
 285   if (err != 0)                                                           \
 286     return (return_type) NULL;                                            \
 287                                                                           \
 288   log_trace(os, container)(logstring, variable);                          \
 289 }
 290 
 291 #define GET_CONTAINER_INFO_LINE(return_type, subsystem, filename,         \
 292                            matchline, logstring, scan_fmt, variable)      \
 293   return_type variable;                                                   \
 294 {                                                                         \
 295   int err;                                                                \
 296   err = subsystem_file_line_contents(subsystem,                           \
 297                                 filename,                                 \
 298                                 matchline,                                \
 299                                 scan_fmt,                                 \
 300                                 &variable);                               \
 301   if (err != 0)                                                           \
 302     return (return_type) OSCONTAINER_ERROR;                               \
 303                                                                           \
 304   log_trace(os, container)(logstring, variable);                          \
 305 }
 306 
 307 /* init
 308  *
 309  * Initialize the container support and determine if
 310  * we are running under cgroup control.
 311  */
 312 void OSContainer::init() {
 313   FILE *mntinfo = NULL;
 314   FILE *cgroup = NULL;
 315   char buf[MAXPATHLEN+1];
 316   char tmproot[MAXPATHLEN+1];
 317   char tmpmount[MAXPATHLEN+1];
 318   char *p;
 319   jlong mem_limit;
 320 
 321   assert(!_is_initialized, "Initializing OSContainer more than once");
 322 
 323   _is_initialized = true;
 324   _is_containerized = false;
 325 
 326   _unlimited_memory = (LONG_MAX / os::vm_page_size()) * os::vm_page_size();
 327 
 328   log_trace(os, container)("OSContainer::init: Initializing Container Support");
 329   if (!UseContainerSupport) {
 330     log_trace(os, container)("Container Support not enabled");
 331     return;
 332   }
 333 
 334   /*
 335    * Find the cgroup mount point for memory and cpuset
 336    * by reading /proc/self/mountinfo
 337    *
 338    * Example for docker:
 339    * 219 214 0:29 /docker/7208cebd00fa5f2e342b1094f7bed87fa25661471a4637118e65f1c995be8a34 /sys/fs/cgroup/memory ro,nosuid,nodev,noexec,relatime - cgroup cgroup rw,memory
 340    *
 341    * Example for host:
 342    * 34 28 0:29 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,memory
 343    */
 344   mntinfo = fopen("/proc/self/mountinfo", "r");
 345   if (mntinfo == NULL) {
 346       log_debug(os, container)("Can't open /proc/self/mountinfo, %s",
 347                                os::strerror(errno));
 348       return;
 349   }
 350 
 351   while ((p = fgets(buf, MAXPATHLEN, mntinfo)) != NULL) {
 352     char tmpcgroups[MAXPATHLEN+1];
 353     char *cptr = tmpcgroups;
 354     char *token;
 355 
 356     // mountinfo format is documented at https://www.kernel.org/doc/Documentation/filesystems/proc.txt
 357     if (sscanf(p, "%*d %*d %*d:%*d %s %s %*[^-]- cgroup %*s %s", tmproot, tmpmount, tmpcgroups) != 3) {
 358       continue;
 359     }
 360     while ((token = strsep(&cptr, ",")) != NULL) {
 361       if (strcmp(token, "memory") == 0) {
 362         memory = new CgroupMemorySubsystem(tmproot, tmpmount);
 363       } else if (strcmp(token, "cpuset") == 0) {
 364         cpuset = new CgroupSubsystem(tmproot, tmpmount);
 365       } else if (strcmp(token, "cpu") == 0) {
 366         cpu = new CgroupSubsystem(tmproot, tmpmount);
 367       } else if (strcmp(token, "cpuacct") == 0) {
 368         cpuacct= new CgroupSubsystem(tmproot, tmpmount);
 369       }
 370     }
 371   }
 372 
 373   fclose(mntinfo);
 374 
 375   if (memory == NULL) {
 376     log_debug(os, container)("Required cgroup memory subsystem not found");
 377     return;
 378   }
 379   if (cpuset == NULL) {
 380     log_debug(os, container)("Required cgroup cpuset subsystem not found");
 381     return;
 382   }
 383   if (cpu == NULL) {
 384     log_debug(os, container)("Required cgroup cpu subsystem not found");
 385     return;
 386   }
 387   if (cpuacct == NULL) {
 388     log_debug(os, container)("Required cgroup cpuacct subsystem not found");
 389     return;
 390   }
 391 
 392   /*
 393    * Read /proc/self/cgroup and map host mount point to
 394    * local one via /proc/self/mountinfo content above
 395    *
 396    * Docker example:
 397    * 5:memory:/docker/6558aed8fc662b194323ceab5b964f69cf36b3e8af877a14b80256e93aecb044
 398    *
 399    * Host example:
 400    * 5:memory:/user.slice
 401    *
 402    * Construct a path to the process specific memory and cpuset
 403    * cgroup directory.
 404    *
 405    * For a container running under Docker from memory example above
 406    * the paths would be:
 407    *
 408    * /sys/fs/cgroup/memory
 409    *
 410    * For a Host from memory example above the path would be:
 411    *
 412    * /sys/fs/cgroup/memory/user.slice
 413    *
 414    */
 415   cgroup = fopen("/proc/self/cgroup", "r");
 416   if (cgroup == NULL) {
 417     log_debug(os, container)("Can't open /proc/self/cgroup, %s",
 418                              os::strerror(errno));
 419     return;
 420   }
 421 
 422   while ((p = fgets(buf, MAXPATHLEN, cgroup)) != NULL) {
 423     char *controllers;
 424     char *token;
 425     char *base;
 426 
 427     /* Skip cgroup number */
 428     strsep(&p, ":");
 429     /* Get controllers and base */
 430     controllers = strsep(&p, ":");
 431     base = strsep(&p, "\n");
 432 
 433     if (controllers == NULL) {
 434       continue;
 435     }
 436 
 437     while ((token = strsep(&controllers, ",")) != NULL) {
 438       if (strcmp(token, "memory") == 0) {
 439         memory->set_subsystem_path(base);
 440         jlong hierarchy = uses_mem_hierarchy();
 441         if (hierarchy > 0) {
 442           memory->set_hierarchical(true);
 443         }
 444       } else if (strcmp(token, "cpuset") == 0) {
 445         cpuset->set_subsystem_path(base);
 446       } else if (strcmp(token, "cpu") == 0) {
 447         cpu->set_subsystem_path(base);
 448       } else if (strcmp(token, "cpuacct") == 0) {
 449         cpuacct->set_subsystem_path(base);
 450       }
 451     }
 452   }
 453 
 454   fclose(cgroup);
 455 
 456   // We need to update the amount of physical memory now that
 457   // command line arguments have been processed.
 458   if ((mem_limit = memory_limit_in_bytes()) > 0) {
 459     os::Linux::set_physical_memory(mem_limit);
 460     log_info(os, container)("Memory Limit is: " JLONG_FORMAT, mem_limit);
 461   }
 462 
 463   _is_containerized = true;
 464 
 465 }
 466 
 467 const char * OSContainer::container_type() {
 468   if (is_containerized()) {
 469     return "cgroupv1";
 470   } else {
 471     return NULL;
 472   }
 473 }
 474 
 475 /* uses_mem_hierarchy
 476  *
 477  * Return whether or not hierarchical cgroup accounting is being
 478  * done.
 479  *
 480  * return:
 481  *    A number > 0 if true, or
 482  *    OSCONTAINER_ERROR for not supported
 483  */
 484 jlong OSContainer::uses_mem_hierarchy() {
 485   GET_CONTAINER_INFO(jlong, memory, "/memory.use_hierarchy",
 486                     "Use Hierarchy is: " JLONG_FORMAT, JLONG_FORMAT, use_hierarchy);
 487   return use_hierarchy;
 488 }
 489 
 490 
 491 /* memory_limit_in_bytes
 492  *
 493  * Return the limit of available memory for this process.
 494  *
 495  * return:
 496  *    memory limit in bytes or
 497  *    -1 for unlimited
 498  *    OSCONTAINER_ERROR for not supported
 499  */
 500 jlong OSContainer::memory_limit_in_bytes() {
 501   GET_CONTAINER_INFO(julong, memory, "/memory.limit_in_bytes",
 502                      "Memory Limit is: " JULONG_FORMAT, JULONG_FORMAT, memlimit);
 503 
 504   if (memlimit >= _unlimited_memory) {
 505     log_trace(os, container)("Non-Hierarchical Memory Limit is: Unlimited");
 506     if (memory->is_hierarchical()) {
 507       const char* matchline = "hierarchical_memory_limit";
 508       char* format = "%s " JULONG_FORMAT;
 509       GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
 510                              "Hierarchical Memory Limit is: " JULONG_FORMAT, format, hier_memlimit)
 511       if (hier_memlimit >= _unlimited_memory) {
 512         log_trace(os, container)("Hierarchical Memory Limit is: Unlimited");
 513       } else {
 514         return (jlong)hier_memlimit;
 515       }
 516     }
 517     return (jlong)-1;
 518   }
 519   else {
 520     return (jlong)memlimit;
 521   }
 522 }
 523 
 524 jlong OSContainer::memory_and_swap_limit_in_bytes() {
 525   GET_CONTAINER_INFO(julong, memory, "/memory.memsw.limit_in_bytes",
 526                      "Memory and Swap Limit is: " JULONG_FORMAT, JULONG_FORMAT, memswlimit);
 527   if (memswlimit >= _unlimited_memory) {
 528     log_trace(os, container)("Non-Hierarchical Memory and Swap Limit is: Unlimited");
 529     if (memory->is_hierarchical()) {
 530       const char* matchline = "hierarchical_memsw_limit";
 531       char* format = "%s " JULONG_FORMAT;
 532       GET_CONTAINER_INFO_LINE(julong, memory, "/memory.stat", matchline,
 533                              "Hierarchical Memory and Swap Limit is : " JULONG_FORMAT, format, hier_memlimit)
 534       if (hier_memlimit >= _unlimited_memory) {
 535         log_trace(os, container)("Hierarchical Memory and Swap Limit is: Unlimited");
 536       } else {
 537         return (jlong)hier_memlimit;
 538       }
 539     }
 540     return (jlong)-1;
 541   } else {
 542     return (jlong)memswlimit;
 543   }
 544 }
 545 
 546 jlong OSContainer::memory_soft_limit_in_bytes() {
 547   GET_CONTAINER_INFO(julong, memory, "/memory.soft_limit_in_bytes",
 548                      "Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
 549   if (memsoftlimit >= _unlimited_memory) {
 550     log_trace(os, container)("Memory Soft Limit is: Unlimited");
 551     return (jlong)-1;
 552   } else {
 553     return (jlong)memsoftlimit;
 554   }
 555 }
 556 
 557 /* memory_usage_in_bytes
 558  *
 559  * Return the amount of used memory for this process.
 560  *
 561  * return:
 562  *    memory usage in bytes or
 563  *    -1 for unlimited
 564  *    OSCONTAINER_ERROR for not supported
 565  */
 566 jlong OSContainer::memory_usage_in_bytes() {
 567   GET_CONTAINER_INFO(jlong, memory, "/memory.usage_in_bytes",
 568                      "Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memusage);
 569   return memusage;
 570 }
 571 
 572 /* memory_max_usage_in_bytes
 573  *
 574  * Return the maximum amount of used memory for this process.
 575  *
 576  * return:
 577  *    max memory usage in bytes or
 578  *    OSCONTAINER_ERROR for not supported
 579  */
 580 jlong OSContainer::memory_max_usage_in_bytes() {
 581   GET_CONTAINER_INFO(jlong, memory, "/memory.max_usage_in_bytes",
 582                      "Maximum Memory Usage is: " JLONG_FORMAT, JLONG_FORMAT, memmaxusage);
 583   return memmaxusage;
 584 }
 585 
 586 /* active_processor_count
 587  *
 588  * Calculate an appropriate number of active processors for the
 589  * VM to use based on these three inputs.
 590  *
 591  * cpu affinity
 592  * cgroup cpu quota & cpu period
 593  * cgroup cpu shares
 594  *
 595  * Algorithm:
 596  *
 597  * Determine the number of available CPUs from sched_getaffinity
 598  *
 599  * If user specified a quota (quota != -1), calculate the number of
 600  * required CPUs by dividing quota by period.
 601  *
 602  * If shares are in effect (shares != -1), calculate the number
 603  * of CPUs required for the shares by dividing the share value
 604  * by PER_CPU_SHARES.
 605  *
 606  * All results of division are rounded up to the next whole number.
 607  *
 608  * If neither shares or quotas have been specified, return the
 609  * number of active processors in the system.
 610  *
 611  * If both shares and quotas have been specified, the results are
 612  * based on the flag PreferContainerQuotaForCPUCount.  If true,
 613  * return the quota value.  If false return the smallest value
 614  * between shares or quotas.
 615  *
 616  * If shares and/or quotas have been specified, the resulting number
 617  * returned will never exceed the number of active processors.
 618  *
 619  * return:
 620  *    number of CPUs
 621  */
 622 int OSContainer::active_processor_count() {
 623   int quota_count = 0, share_count = 0;
 624   int cpu_count, limit_count;
 625   int result;
 626 
 627   cpu_count = limit_count = os::Linux::active_processor_count();
 628   int quota  = cpu_quota();
 629   int period = cpu_period();
 630   int share  = cpu_shares();
 631 
 632   if (quota > -1 && period > 0) {
 633     quota_count = ceilf((float)quota / (float)period);
 634     log_trace(os, container)("CPU Quota count based on quota/period: %d", quota_count);
 635   }
 636   if (share > -1) {
 637     share_count = ceilf((float)share / (float)PER_CPU_SHARES);
 638     log_trace(os, container)("CPU Share count based on shares: %d", share_count);
 639   }
 640 
 641   // If both shares and quotas are setup results depend
 642   // on flag PreferContainerQuotaForCPUCount.
 643   // If true, limit CPU count to quota
 644   // If false, use minimum of shares and quotas
 645   if (quota_count !=0 && share_count != 0) {
 646     if (PreferContainerQuotaForCPUCount) {
 647       limit_count = quota_count;
 648     } else {
 649       limit_count = MIN2(quota_count, share_count);
 650     }
 651   } else if (quota_count != 0) {
 652     limit_count = quota_count;
 653   } else if (share_count != 0) {
 654     limit_count = share_count;
 655   }
 656 
 657   result = MIN2(cpu_count, limit_count);
 658   log_trace(os, container)("OSContainer::active_processor_count: %d", result);
 659   return result;
 660 }
 661 
 662 char * OSContainer::cpu_cpuset_cpus() {
 663   GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.cpus",
 664                      "cpuset.cpus is: %s", "%1023s", cpus, 1024);
 665   return os::strdup(cpus);
 666 }
 667 
 668 char * OSContainer::cpu_cpuset_memory_nodes() {
 669   GET_CONTAINER_INFO_CPTR(cptr, cpuset, "/cpuset.mems",
 670                      "cpuset.mems is: %s", "%1023s", mems, 1024);
 671   return os::strdup(mems);
 672 }
 673 
 674 /* cpu_quota
 675  *
 676  * Return the number of milliseconds per period
 677  * process is guaranteed to run.
 678  *
 679  * return:
 680  *    quota time in milliseconds
 681  *    -1 for no quota
 682  *    OSCONTAINER_ERROR for not supported
 683  */
 684 int OSContainer::cpu_quota() {
 685   GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_quota_us",
 686                      "CPU Quota is: %d", "%d", quota);
 687   return quota;
 688 }
 689 
 690 int OSContainer::cpu_period() {
 691   GET_CONTAINER_INFO(int, cpu, "/cpu.cfs_period_us",
 692                      "CPU Period is: %d", "%d", period);
 693   return period;
 694 }
 695 
 696 /* cpu_shares
 697  *
 698  * Return the amount of cpu shares available to the process
 699  *
 700  * return:
 701  *    Share number (typically a number relative to 1024)
 702  *                 (2048 typically expresses 2 CPUs worth of processing)
 703  *    -1 for no share setup
 704  *    OSCONTAINER_ERROR for not supported
 705  */
 706 int OSContainer::cpu_shares() {
 707   GET_CONTAINER_INFO(int, cpu, "/cpu.shares",
 708                      "CPU Shares is: %d", "%d", shares);
 709   // Convert 1024 to no shares setup
 710   if (shares == 1024) return -1;
 711 
 712   return shares;
 713 }
 714