< prev index next >

src/java.base/linux/classes/jdk/internal/platform/cgroupv1/CgroupV1Subsystem.java

Print this page
@  rev 57734 : Review feedback
|
o  rev 57733 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv, mchung
~
o  rev 56862 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.platform.cgroupv1;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.stream.Stream;
  32 
  33 import jdk.internal.platform.CgroupSubsystem;
  34 import jdk.internal.platform.CgroupSubsystemController;
  35 import jdk.internal.platform.CgroupUtil;
  36 import jdk.internal.platform.Metrics;
  37 
  38 public class CgroupV1Subsystem implements CgroupSubsystem {
  39     private CgroupV1MemorySubSystemController memory;
  40     private CgroupV1SubsystemController cpu;
  41     private CgroupV1SubsystemController cpuacct;
  42     private CgroupV1SubsystemController cpuset;
  43     private CgroupV1SubsystemController blkio;
  44     private boolean activeSubSystems;
  45 
  46     private static final CgroupV1Subsystem INSTANCE = initSubSystem();
  47 
  48     private static final String PROVIDER_NAME = "cgroupv1";
  49 
  50     private CgroupV1Subsystem() {
  51         activeSubSystems = false;
  52     }
  53 
  54     public static CgroupV1Subsystem getInstance() {
  55         return INSTANCE;
  56     }
  57 
  58     private static CgroupV1Subsystem initSubSystem() {


 248     private CgroupV1SubsystemController cpuController() {
 249         return cpu;
 250     }
 251 
 252     private CgroupV1SubsystemController cpuAcctController() {
 253         return cpuacct;
 254     }
 255 
 256     private CgroupV1SubsystemController cpuSetController() {
 257         return cpuset;
 258     }
 259 
 260     private CgroupV1SubsystemController blkIOController() {
 261         return blkio;
 262     }
 263 
 264     private static long getLongValue(CgroupSubsystemController controller,
 265                               String parm) {
 266         return CgroupSubsystemController.getLongValue(controller,
 267                                                       parm,
 268                                                       CgroupV1SubsystemController::convertStringToLong);

 269     }
 270 
 271     public String getProvider() {
 272         return PROVIDER_NAME;
 273     }
 274 
 275     /*****************************************************************
 276      * CPU Accounting Subsystem
 277      ****************************************************************/
 278 
 279 
 280     public long getCpuUsage() {
 281         return getLongValue(cpuacct, "cpuacct.usage");
 282     }
 283 
 284     public long[] getPerCpuUsage() {
 285         String usagelist = CgroupSubsystemController.getStringValue(cpuacct, "cpuacct.usage_percpu");
 286         if (usagelist == null) {
 287             return null;
 288         }
 289 
 290         String list[] = usagelist.split(" ");
 291         long percpu[] = new long[list.length];
 292         for (int i = 0; i < list.length; i++) {
 293             percpu[i] = Long.parseLong(list[i]);
 294         }
 295         return percpu;
 296     }
 297 
 298     public long getCpuUserUsage() {
 299         return CgroupSubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "user");
 300     }
 301 
 302     public long getCpuSystemUsage() {
 303         return CgroupSubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "system");
 304     }
 305 
 306 
 307     /*****************************************************************
 308      * CPU Subsystem
 309      ****************************************************************/
 310 
 311 
 312     public long getCpuPeriod() {
 313         return getLongValue(cpu, "cpu.cfs_period_us");
 314     }
 315 
 316     public long getCpuQuota() {
 317         return getLongValue(cpu, "cpu.cfs_quota_us");
 318     }
 319 
 320     public long getCpuShares() {
 321         long retval = getLongValue(cpu, "cpu.shares");
 322         if (retval == 0 || retval == 1024)
 323             return Metrics.LONG_RETVAL_UNLIMITED;
 324         else
 325             return retval;
 326     }
 327 
 328     public long getCpuNumPeriods() {
 329         return CgroupSubsystemController.getLongEntry(cpu, "cpu.stat", "nr_periods");
 330     }
 331 
 332     public long getCpuNumThrottled() {
 333         return CgroupSubsystemController.getLongEntry(cpu, "cpu.stat", "nr_throttled");
 334     }
 335 
 336     public long getCpuThrottledTime() {
 337         return CgroupSubsystemController.getLongEntry(cpu, "cpu.stat", "throttled_time");
 338     }
 339 
 340     public long getEffectiveCpuCount() {
 341         return Runtime.getRuntime().availableProcessors();
 342     }
 343 
 344 
 345     /*****************************************************************
 346      * CPUSet Subsystem
 347      ****************************************************************/
 348 
 349     public int[] getCpuSetCpus() {
 350         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.cpus"));
 351     }
 352 
 353     public int[] getEffectiveCpuSetCpus() {
 354         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_cpus"));
 355     }
 356 
 357     public int[] getCpuSetMems() {
 358         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.mems"));
 359     }
 360 
 361     public int[] getEffectiveCpuSetMems() {
 362         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_mems"));
 363     }
 364 
 365     public double getCpuSetMemoryPressure() {
 366         return CgroupSubsystemController.getDoubleValue(cpuset, "cpuset.memory_pressure");
 367     }
 368 
 369     public Boolean isCpuSetMemoryPressureEnabled() {
 370         long val = getLongValue(cpuset, "cpuset.memory_pressure_enabled");
 371         return (val == 1);
 372     }
 373 
 374 
 375     /*****************************************************************
 376      * Memory Subsystem
 377      ****************************************************************/
 378 
 379 
 380     public long getMemoryFailCount() {
 381         return getLongValue(memory, "memory.failcnt");
 382     }
 383 
 384     public long getMemoryLimit() {
 385         long retval = getLongValue(memory, "memory.limit_in_bytes");
 386         if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
 387             if (memory.isHierarchical()) {
 388                 // memory.limit_in_bytes returned unlimited, attempt
 389                 // hierarchical memory limit
 390                 String match = "hierarchical_memory_limit";
 391                 retval = CgroupSubsystemController.getLongValueMatchingLine(memory,
 392                                                             "memory.stat",
 393                                                             match,
 394                                                             CgroupV1Subsystem::convertHierachicalLimitLine);
 395             }
 396         }
 397         return CgroupV1SubsystemController.longValOrUnlimited(retval);
 398     }
 399 
 400     public static long convertHierachicalLimitLine(String line) {
 401         String[] tokens = line.split("\\s");
 402         if (tokens.length == 2) {
 403             String strVal = tokens[1];
 404             return CgroupV1SubsystemController.convertStringToLong(strVal);
 405         }
 406         return CgroupV1SubsystemController.UNLIMITED_MIN + 1; // unlimited
 407     }
 408 
 409     public long getMemoryMaxUsage() {
 410         return getLongValue(memory, "memory.max_usage_in_bytes");
 411     }
 412 
 413     public long getMemoryUsage() {
 414         return getLongValue(memory, "memory.usage_in_bytes");
 415     }
 416 
 417     public long getKernelMemoryFailCount() {
 418         return getLongValue(memory, "memory.kmem.failcnt");
 419     }
 420 
 421     public long getKernelMemoryLimit() {
 422         return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.limit_in_bytes"));
 423     }
 424 
 425     public long getKernelMemoryMaxUsage() {
 426         return getLongValue(memory, "memory.kmem.max_usage_in_bytes");
 427     }
 428 


 440 
 441     public long getTcpMemoryMaxUsage() {
 442         return getLongValue(memory, "memory.kmem.tcp.max_usage_in_bytes");
 443     }
 444 
 445     public long getTcpMemoryUsage() {
 446         return getLongValue(memory, "memory.kmem.tcp.usage_in_bytes");
 447     }
 448 
 449     public long getMemoryAndSwapFailCount() {
 450         return getLongValue(memory, "memory.memsw.failcnt");
 451     }
 452 
 453     public long getMemoryAndSwapLimit() {
 454         long retval = getLongValue(memory, "memory.memsw.limit_in_bytes");
 455         if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
 456             if (memory.isHierarchical()) {
 457                 // memory.memsw.limit_in_bytes returned unlimited, attempt
 458                 // hierarchical memory limit
 459                 String match = "hierarchical_memsw_limit";
 460                 retval = CgroupSubsystemController.getLongValueMatchingLine(memory,
 461                                                             "memory.stat",
 462                                                             match,
 463                                                             CgroupV1Subsystem::convertHierachicalLimitLine);
 464             }
 465         }
 466         return CgroupV1SubsystemController.longValOrUnlimited(retval);
 467     }
 468 
 469     public long getMemoryAndSwapMaxUsage() {
 470         return getLongValue(memory, "memory.memsw.max_usage_in_bytes");
 471     }
 472 
 473     public long getMemoryAndSwapUsage() {
 474         return getLongValue(memory, "memory.memsw.usage_in_bytes");
 475     }
 476 
 477     public Boolean isMemoryOOMKillEnabled() {
 478         long val = CgroupSubsystemController.getLongEntry(memory, "memory.oom_control", "oom_kill_disable");
 479         return (val == 0);
 480     }
 481 
 482     public long getMemorySoftLimit() {
 483         return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.soft_limit_in_bytes"));
 484     }
 485 
 486 
 487     /*****************************************************************
 488      * BlKIO Subsystem
 489      ****************************************************************/
 490 
 491 
 492     public long getBlkIOServiceCount() {
 493         return CgroupSubsystemController.getLongEntry(blkio, "blkio.throttle.io_service_bytes", "Total");
 494     }
 495 
 496     public long getBlkIOServiced() {
 497         return CgroupSubsystemController.getLongEntry(blkio, "blkio.throttle.io_serviced", "Total");
 498     }
 499 
 500 }


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.platform.cgroupv1;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.stream.Stream;
  32 
  33 import jdk.internal.platform.CgroupSubsystem;
  34 import jdk.internal.platform.CgroupSubsystemController;
  35 import jdk.internal.platform.CgroupUtil;
  36 import jdk.internal.platform.MetricsCgroupV1;
  37 
  38 public class CgroupV1Subsystem implements CgroupSubsystem, MetricsCgroupV1 {
  39     private CgroupV1MemorySubSystemController memory;
  40     private CgroupV1SubsystemController cpu;
  41     private CgroupV1SubsystemController cpuacct;
  42     private CgroupV1SubsystemController cpuset;
  43     private CgroupV1SubsystemController blkio;
  44     private boolean activeSubSystems;
  45 
  46     private static final CgroupV1Subsystem INSTANCE = initSubSystem();
  47 
  48     private static final String PROVIDER_NAME = "cgroupv1";
  49 
  50     private CgroupV1Subsystem() {
  51         activeSubSystems = false;
  52     }
  53 
  54     public static CgroupV1Subsystem getInstance() {
  55         return INSTANCE;
  56     }
  57 
  58     private static CgroupV1Subsystem initSubSystem() {


 248     private CgroupV1SubsystemController cpuController() {
 249         return cpu;
 250     }
 251 
 252     private CgroupV1SubsystemController cpuAcctController() {
 253         return cpuacct;
 254     }
 255 
 256     private CgroupV1SubsystemController cpuSetController() {
 257         return cpuset;
 258     }
 259 
 260     private CgroupV1SubsystemController blkIOController() {
 261         return blkio;
 262     }
 263 
 264     private static long getLongValue(CgroupSubsystemController controller,
 265                               String parm) {
 266         return CgroupSubsystemController.getLongValue(controller,
 267                                                       parm,
 268                                                       CgroupV1SubsystemController::convertStringToLong,
 269                                                       CgroupSubsystem.LONG_RETVAL_UNLIMITED);
 270     }
 271 
 272     public String getProvider() {
 273         return PROVIDER_NAME;
 274     }
 275 
 276     /*****************************************************************
 277      * CPU Accounting Subsystem
 278      ****************************************************************/
 279 
 280 
 281     public long getCpuUsage() {
 282         return getLongValue(cpuacct, "cpuacct.usage");
 283     }
 284 
 285     public long[] getPerCpuUsage() {
 286         String usagelist = CgroupSubsystemController.getStringValue(cpuacct, "cpuacct.usage_percpu");
 287         if (usagelist == null) {
 288             return null;
 289         }
 290 
 291         String list[] = usagelist.split(" ");
 292         long percpu[] = new long[list.length];
 293         for (int i = 0; i < list.length; i++) {
 294             percpu[i] = Long.parseLong(list[i]);
 295         }
 296         return percpu;
 297     }
 298 
 299     public long getCpuUserUsage() {
 300         return CgroupV1SubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "user");
 301     }
 302 
 303     public long getCpuSystemUsage() {
 304         return CgroupV1SubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "system");
 305     }
 306 
 307 
 308     /*****************************************************************
 309      * CPU Subsystem
 310      ****************************************************************/
 311 
 312 
 313     public long getCpuPeriod() {
 314         return getLongValue(cpu, "cpu.cfs_period_us");
 315     }
 316 
 317     public long getCpuQuota() {
 318         return getLongValue(cpu, "cpu.cfs_quota_us");
 319     }
 320 
 321     public long getCpuShares() {
 322         long retval = getLongValue(cpu, "cpu.shares");
 323         if (retval == 0 || retval == 1024)
 324             return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
 325         else
 326             return retval;
 327     }
 328 
 329     public long getCpuNumPeriods() {
 330         return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "nr_periods");
 331     }
 332 
 333     public long getCpuNumThrottled() {
 334         return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "nr_throttled");
 335     }
 336 
 337     public long getCpuThrottledTime() {
 338         return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "throttled_time");
 339     }
 340 
 341     public long getEffectiveCpuCount() {
 342         return Runtime.getRuntime().availableProcessors();
 343     }
 344 
 345 
 346     /*****************************************************************
 347      * CPUSet Subsystem
 348      ****************************************************************/
 349 
 350     public int[] getCpuSetCpus() {
 351         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.cpus"));
 352     }
 353 
 354     public int[] getEffectiveCpuSetCpus() {
 355         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_cpus"));
 356     }
 357 
 358     public int[] getCpuSetMems() {
 359         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.mems"));
 360     }
 361 
 362     public int[] getEffectiveCpuSetMems() {
 363         return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_mems"));
 364     }
 365 
 366     public double getCpuSetMemoryPressure() {
 367         return CgroupV1SubsystemController.getDoubleValue(cpuset, "cpuset.memory_pressure");
 368     }
 369 
 370     public Boolean isCpuSetMemoryPressureEnabled() {
 371         long val = getLongValue(cpuset, "cpuset.memory_pressure_enabled");
 372         return (val == 1);
 373     }
 374 
 375 
 376     /*****************************************************************
 377      * Memory Subsystem
 378      ****************************************************************/
 379 
 380 
 381     public long getMemoryFailCount() {
 382         return getLongValue(memory, "memory.failcnt");
 383     }
 384 
 385     public long getMemoryLimit() {
 386         long retval = getLongValue(memory, "memory.limit_in_bytes");
 387         if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
 388             if (memory.isHierarchical()) {
 389                 // memory.limit_in_bytes returned unlimited, attempt
 390                 // hierarchical memory limit
 391                 String match = "hierarchical_memory_limit";
 392                 retval = CgroupV1SubsystemController.getLongValueMatchingLine(memory,
 393                                                             "memory.stat",
 394                                                             match);

 395             }
 396         }
 397         return CgroupV1SubsystemController.longValOrUnlimited(retval);
 398     }
 399 









 400     public long getMemoryMaxUsage() {
 401         return getLongValue(memory, "memory.max_usage_in_bytes");
 402     }
 403 
 404     public long getMemoryUsage() {
 405         return getLongValue(memory, "memory.usage_in_bytes");
 406     }
 407 
 408     public long getKernelMemoryFailCount() {
 409         return getLongValue(memory, "memory.kmem.failcnt");
 410     }
 411 
 412     public long getKernelMemoryLimit() {
 413         return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.limit_in_bytes"));
 414     }
 415 
 416     public long getKernelMemoryMaxUsage() {
 417         return getLongValue(memory, "memory.kmem.max_usage_in_bytes");
 418     }
 419 


 431 
 432     public long getTcpMemoryMaxUsage() {
 433         return getLongValue(memory, "memory.kmem.tcp.max_usage_in_bytes");
 434     }
 435 
 436     public long getTcpMemoryUsage() {
 437         return getLongValue(memory, "memory.kmem.tcp.usage_in_bytes");
 438     }
 439 
 440     public long getMemoryAndSwapFailCount() {
 441         return getLongValue(memory, "memory.memsw.failcnt");
 442     }
 443 
 444     public long getMemoryAndSwapLimit() {
 445         long retval = getLongValue(memory, "memory.memsw.limit_in_bytes");
 446         if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
 447             if (memory.isHierarchical()) {
 448                 // memory.memsw.limit_in_bytes returned unlimited, attempt
 449                 // hierarchical memory limit
 450                 String match = "hierarchical_memsw_limit";
 451                 retval = CgroupV1SubsystemController.getLongValueMatchingLine(memory,
 452                                                             "memory.stat",
 453                                                             match);

 454             }
 455         }
 456         return CgroupV1SubsystemController.longValOrUnlimited(retval);
 457     }
 458 
 459     public long getMemoryAndSwapMaxUsage() {
 460         return getLongValue(memory, "memory.memsw.max_usage_in_bytes");
 461     }
 462 
 463     public long getMemoryAndSwapUsage() {
 464         return getLongValue(memory, "memory.memsw.usage_in_bytes");
 465     }
 466 
 467     public Boolean isMemoryOOMKillEnabled() {
 468         long val = CgroupV1SubsystemController.getLongEntry(memory, "memory.oom_control", "oom_kill_disable");
 469         return (val == 0);
 470     }
 471 
 472     public long getMemorySoftLimit() {
 473         return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.soft_limit_in_bytes"));
 474     }
 475 
 476 
 477     /*****************************************************************
 478      * BlKIO Subsystem
 479      ****************************************************************/
 480 
 481 
 482     public long getBlkIOServiceCount() {
 483         return CgroupV1SubsystemController.getLongEntry(blkio, "blkio.throttle.io_service_bytes", "Total");
 484     }
 485 
 486     public long getBlkIOServiced() {
 487         return CgroupV1SubsystemController.getLongEntry(blkio, "blkio.throttle.io_serviced", "Total");
 488     }
 489 
 490 }
< prev index next >