< prev index next >

test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV1.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
~


  22  */
  23 
  24 package jdk.test.lib.containers.cgroup;
  25 
  26 import java.io.File;
  27 import java.io.FileNotFoundException;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Arrays;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.Scanner;
  36 import java.util.Set;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.LongStream;
  39 import java.util.stream.Stream;
  40 
  41 import jdk.internal.platform.Metrics;

  42 
  43 public class MetricsTesterCgroupV1 implements CgroupMetricsTester {
  44 
  45     private static long unlimited_minimum = 0x7FFFFFFFFF000000L;
  46     long startSysVal;
  47     long startUserVal;
  48     long startUsage;
  49     long startPerCpu[];
  50 
  51     enum Controller {
  52         MEMORY("memory"),
  53         CPUSET("cpuset"),
  54         CPU("cpu"),
  55         CPUACCT("cpuacct"),
  56         BLKIO("blkio");
  57 
  58         private String value;
  59 
  60         Controller(String value) {
  61             this.value = value;


 186         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 187     }
 188 
 189     private static void fail(Controller system, String metric, String oldVal, String testVal) {
 190         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 191     }
 192 
 193     private static void fail(Controller system, String metric, double oldVal, double testVal) {
 194         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 195     }
 196 
 197     private static void fail(Controller system, String metric, boolean oldVal, boolean testVal) {
 198         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 199     }
 200 
 201     private static void warn(Controller system, String metric, long oldVal, long testVal) {
 202         CgroupMetricsTester.warn(system.value, metric, oldVal, testVal);
 203     }
 204 
 205     public void testMemorySubsystem() {
 206         Metrics metrics = Metrics.systemMetrics();
 207 
 208         // User Memory
 209         long oldVal = metrics.getMemoryFailCount();
 210         long newVal = getLongValueFromFile(Controller.MEMORY, "memory.failcnt");
 211         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 212             fail(Controller.MEMORY, "memory.failcnt", oldVal, newVal);
 213         }
 214 
 215         oldVal = metrics.getMemoryLimit();
 216         newVal = getLongValueFromFile(Controller.MEMORY, "memory.limit_in_bytes");
 217         newVal = newVal > unlimited_minimum ? -1L : newVal;
 218         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 219             fail(Controller.MEMORY, "memory.limit_in_bytes", oldVal, newVal);
 220         }
 221 
 222         oldVal = metrics.getMemoryMaxUsage();
 223         newVal = getLongValueFromFile(Controller.MEMORY, "memory.max_usage_in_bytes");
 224         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 225             fail(Controller.MEMORY, "memory.max_usage_in_bytes", oldVal, newVal);
 226         }


 310         }
 311 
 312         oldVal = metrics.getMemorySoftLimit();
 313         newVal = getLongValueFromFile(Controller.MEMORY, "memory.soft_limit_in_bytes");
 314         newVal = newVal > unlimited_minimum ? -1L : newVal;
 315         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 316             fail(Controller.MEMORY, "memory.soft_limit_in_bytes", oldVal, newVal);
 317         }
 318 
 319         boolean oomKillEnabled = metrics.isMemoryOOMKillEnabled();
 320         boolean newOomKillEnabled = getLongValueFromFile(Controller.MEMORY,
 321                 "memory.oom_control", "oom_kill_disable") == 0L ? true : false;
 322         if (oomKillEnabled != newOomKillEnabled) {
 323             throw new RuntimeException("Test failed for - " + Controller.MEMORY.value + ":"
 324                     + "memory.oom_control:oom_kill_disable" + ", expected ["
 325                     + oomKillEnabled + "], got [" + newOomKillEnabled + "]");
 326         }
 327     }
 328 
 329     public void testCpuAccounting() {
 330         Metrics metrics = Metrics.systemMetrics();
 331         long oldVal = metrics.getCpuUsage();
 332         long newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.usage");
 333 
 334         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 335             warn(Controller.CPUACCT, "cpuacct.usage", oldVal, newVal);
 336         }
 337 
 338         String newValsStr = getFileContents(Controller.CPUACCT, "cpuacct.usage_percpu");
 339         Long[] newVals = new Long[0];
 340         if (newValsStr != null) {
 341             newVals = Stream.of(newValsStr
 342                 .split("\\s+"))
 343                 .map(Long::parseLong)
 344                 .toArray(Long[]::new);
 345         }
 346         long[] oldValsPrim = metrics.getPerCpuUsage();
 347         Long[] oldVals = LongStream.of(oldValsPrim == null ? new long[0] : oldValsPrim)
 348                                     .boxed().toArray(Long[]::new);
 349         for (int i = 0; i < oldVals.length; i++) {
 350             if (!CgroupMetricsTester.compareWithErrorMargin(oldVals[i], newVals[i])) {
 351                 warn(Controller.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
 352             }
 353         }
 354 
 355         oldVal = metrics.getCpuUserUsage();
 356         newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.stat", "user");
 357         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 358             warn(Controller.CPUACCT, "cpuacct.usage - user", oldVal, newVal);
 359         }
 360 
 361         oldVal = metrics.getCpuSystemUsage();
 362         newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.stat", "system");
 363         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 364             warn(Controller.CPUACCT, "cpuacct.usage - system", oldVal, newVal);
 365         }
 366     }
 367 
 368     public void testCpuSchedulingMetrics() {
 369         Metrics metrics = Metrics.systemMetrics();
 370         long oldVal = metrics.getCpuPeriod();
 371         long newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.cfs_period_us");
 372         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 373             fail(Controller.CPUACCT, "cpu.cfs_period_us", oldVal, newVal);
 374         }
 375 
 376         oldVal = metrics.getCpuQuota();
 377         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.cfs_quota_us");
 378         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 379             fail(Controller.CPUACCT, "cpu.cfs_quota_us", oldVal, newVal);
 380         }
 381 
 382         oldVal = metrics.getCpuShares();
 383         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.shares");
 384         if (newVal == 0 || newVal == 1024) newVal = -1;
 385         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 386             fail(Controller.CPUACCT, "cpu.shares", oldVal, newVal);
 387         }
 388 
 389         oldVal = metrics.getCpuNumPeriods();
 390         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "nr_periods");
 391         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 392             fail(Controller.CPUACCT, "cpu.stat - nr_periods", oldVal, newVal);
 393         }
 394 
 395         oldVal = metrics.getCpuNumThrottled();
 396         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "nr_throttled");
 397         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 398             fail(Controller.CPUACCT, "cpu.stat - nr_throttled", oldVal, newVal);
 399         }
 400 
 401         oldVal = metrics.getCpuThrottledTime();
 402         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "throttled_time");
 403         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 404             fail(Controller.CPUACCT, "cpu.stat - throttled_time", oldVal, newVal);
 405         }
 406     }
 407 
 408     public void testCpuSets() {
 409         Metrics metrics = Metrics.systemMetrics();
 410         Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
 411         Arrays.sort(oldVal);
 412 
 413         String cpusstr = getFileContents(Controller.CPUSET, "cpuset.cpus");
 414         // Parse range string in the format 1,2-6,7
 415         Integer[] newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
 416         Arrays.sort(newVal);
 417         if (Arrays.compare(oldVal, newVal) != 0) {
 418             fail(Controller.CPUSET, "cpuset.cpus", Arrays.toString(oldVal),
 419                 Arrays.toString(newVal));
 420         }
 421 
 422         int [] cpuSets = metrics.getEffectiveCpuSetCpus();
 423 
 424         // Skip this test if this metric is not supported on this platform
 425         if (cpuSets.length != 0) {
 426             oldVal = Arrays.stream(cpuSets).boxed().toArray(Integer[]::new);
 427             Arrays.sort(oldVal);
 428             cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_cpus");
 429             newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);


 457                 fail(Controller.CPUSET, "cpuset.effective_mems", Arrays.toString(oldVal),
 458                         Arrays.toString(newVal));
 459             }
 460         }
 461 
 462         double oldValue = metrics.getCpuSetMemoryPressure();
 463         double newValue = getDoubleValueFromFile(Controller.CPUSET, "cpuset.memory_pressure");
 464         if (!CgroupMetricsTester.compareWithErrorMargin(oldValue, newValue)) {
 465             fail(Controller.CPUSET, "cpuset.memory_pressure", oldValue, newValue);
 466         }
 467 
 468         boolean oldV = metrics.isCpuSetMemoryPressureEnabled();
 469         boolean newV = getLongValueFromFile(Controller.CPUSET,
 470                 "cpuset.memory_pressure_enabled") == 1 ? true : false;
 471         if (oldV != newV) {
 472             fail(Controller.CPUSET, "cpuset.memory_pressure_enabled", oldV, newV);
 473         }
 474     }
 475 
 476     private void testBlkIO() {
 477         Metrics metrics = Metrics.systemMetrics();
 478             long oldVal = metrics.getBlkIOServiceCount();
 479         long newVal = getLongValueFromFile(Controller.BLKIO,
 480                 "blkio.throttle.io_service_bytes", "Total");
 481         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 482             fail(Controller.BLKIO, "blkio.throttle.io_service_bytes - Total",
 483                     oldVal, newVal);
 484         }
 485 
 486         oldVal = metrics.getBlkIOServiced();
 487         newVal = getLongValueFromFile(Controller.BLKIO, "blkio.throttle.io_serviced", "Total");
 488         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 489             fail(Controller.BLKIO, "blkio.throttle.io_serviced - Total", oldVal, newVal);
 490         }
 491     }
 492 
 493     public void testCpuConsumption() throws IOException, InterruptedException {
 494         Metrics metrics = Metrics.systemMetrics();
 495         // make system call
 496         long newSysVal = metrics.getCpuSystemUsage();
 497         long newUserVal = metrics.getCpuUserUsage();
 498         long newUsage = metrics.getCpuUsage();
 499         long[] newPerCpu = metrics.getPerCpuUsage();
 500         if (newPerCpu == null) {
 501             newPerCpu = new long[0];
 502         }
 503 
 504         // system/user CPU usage counters may be slowly increasing.
 505         // allow for equal values for a pass
 506         if (newSysVal < startSysVal) {
 507             fail(Controller.CPU, "getCpuSystemUsage", newSysVal, startSysVal);
 508         }
 509 
 510         // system/user CPU usage counters may be slowly increasing.
 511         // allow for equal values for a pass
 512         if (newUserVal < startUserVal) {
 513             fail(Controller.CPU, "getCpuUserUsage", newUserVal, startUserVal);
 514         }
 515 
 516         if (newUsage <= startUsage) {
 517             fail(Controller.CPU, "getCpuUsage", newUsage, startUsage);
 518         }
 519 
 520         boolean success = false;
 521         for (int i = 0; i < startPerCpu.length; i++) {
 522             if (newPerCpu[i] > startPerCpu[i]) {
 523                 success = true;
 524                 break;
 525             }
 526         }
 527 
 528         if(!success) fail(Controller.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
 529                 Arrays.toString(startPerCpu));
 530     }
 531 
 532     public void testMemoryUsage() throws Exception {
 533         Metrics metrics = Metrics.systemMetrics();
 534         long memoryMaxUsage = metrics.getMemoryMaxUsage();
 535         long memoryUsage = metrics.getMemoryUsage();
 536         long newMemoryMaxUsage = 0, newMemoryUsage = 0;
 537 
 538         // allocate memory in a loop and check more than once for new values
 539         // otherwise we might see seldom the effect of decreasing new memory values
 540         // e.g. because the system could free up memory
 541         byte[][] bytes = new byte[32][];
 542         for (int i = 0; i < 32; i++) {
 543             bytes[i] = new byte[8*1024*1024];
 544             newMemoryUsage = metrics.getMemoryUsage();
 545             if (newMemoryUsage > memoryUsage) {
 546                 break;
 547             }
 548         }
 549         newMemoryMaxUsage = metrics.getMemoryMaxUsage();
 550 
 551         if (newMemoryMaxUsage < memoryMaxUsage) {
 552             fail(Controller.MEMORY, "getMemoryMaxUsage", memoryMaxUsage,
 553                     newMemoryMaxUsage);


  22  */
  23 
  24 package jdk.test.lib.containers.cgroup;
  25 
  26 import java.io.File;
  27 import java.io.FileNotFoundException;
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Arrays;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.Scanner;
  36 import java.util.Set;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.LongStream;
  39 import java.util.stream.Stream;
  40 
  41 import jdk.internal.platform.Metrics;
  42 import jdk.internal.platform.MetricsCgroupV1;
  43 
  44 public class MetricsTesterCgroupV1 implements CgroupMetricsTester {
  45 
  46     private static long unlimited_minimum = 0x7FFFFFFFFF000000L;
  47     long startSysVal;
  48     long startUserVal;
  49     long startUsage;
  50     long startPerCpu[];
  51 
  52     enum Controller {
  53         MEMORY("memory"),
  54         CPUSET("cpuset"),
  55         CPU("cpu"),
  56         CPUACCT("cpuacct"),
  57         BLKIO("blkio");
  58 
  59         private String value;
  60 
  61         Controller(String value) {
  62             this.value = value;


 187         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 188     }
 189 
 190     private static void fail(Controller system, String metric, String oldVal, String testVal) {
 191         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 192     }
 193 
 194     private static void fail(Controller system, String metric, double oldVal, double testVal) {
 195         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 196     }
 197 
 198     private static void fail(Controller system, String metric, boolean oldVal, boolean testVal) {
 199         CgroupMetricsTester.fail(system.value, metric, oldVal, testVal);
 200     }
 201 
 202     private static void warn(Controller system, String metric, long oldVal, long testVal) {
 203         CgroupMetricsTester.warn(system.value, metric, oldVal, testVal);
 204     }
 205 
 206     public void testMemorySubsystem() {
 207         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 208 
 209         // User Memory
 210         long oldVal = metrics.getMemoryFailCount();
 211         long newVal = getLongValueFromFile(Controller.MEMORY, "memory.failcnt");
 212         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 213             fail(Controller.MEMORY, "memory.failcnt", oldVal, newVal);
 214         }
 215 
 216         oldVal = metrics.getMemoryLimit();
 217         newVal = getLongValueFromFile(Controller.MEMORY, "memory.limit_in_bytes");
 218         newVal = newVal > unlimited_minimum ? -1L : newVal;
 219         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 220             fail(Controller.MEMORY, "memory.limit_in_bytes", oldVal, newVal);
 221         }
 222 
 223         oldVal = metrics.getMemoryMaxUsage();
 224         newVal = getLongValueFromFile(Controller.MEMORY, "memory.max_usage_in_bytes");
 225         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 226             fail(Controller.MEMORY, "memory.max_usage_in_bytes", oldVal, newVal);
 227         }


 311         }
 312 
 313         oldVal = metrics.getMemorySoftLimit();
 314         newVal = getLongValueFromFile(Controller.MEMORY, "memory.soft_limit_in_bytes");
 315         newVal = newVal > unlimited_minimum ? -1L : newVal;
 316         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 317             fail(Controller.MEMORY, "memory.soft_limit_in_bytes", oldVal, newVal);
 318         }
 319 
 320         boolean oomKillEnabled = metrics.isMemoryOOMKillEnabled();
 321         boolean newOomKillEnabled = getLongValueFromFile(Controller.MEMORY,
 322                 "memory.oom_control", "oom_kill_disable") == 0L ? true : false;
 323         if (oomKillEnabled != newOomKillEnabled) {
 324             throw new RuntimeException("Test failed for - " + Controller.MEMORY.value + ":"
 325                     + "memory.oom_control:oom_kill_disable" + ", expected ["
 326                     + oomKillEnabled + "], got [" + newOomKillEnabled + "]");
 327         }
 328     }
 329 
 330     public void testCpuAccounting() {
 331         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 332         long oldVal = metrics.getCpuUsage();
 333         long newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.usage");
 334 
 335         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 336             warn(Controller.CPUACCT, "cpuacct.usage", oldVal, newVal);
 337         }
 338 
 339         String newValsStr = getFileContents(Controller.CPUACCT, "cpuacct.usage_percpu");
 340         Long[] newVals = new Long[0];
 341         if (newValsStr != null) {
 342             newVals = Stream.of(newValsStr
 343                 .split("\\s+"))
 344                 .map(Long::parseLong)
 345                 .toArray(Long[]::new);
 346         }
 347         long[] oldValsPrim = metrics.getPerCpuUsage();
 348         Long[] oldVals = LongStream.of(oldValsPrim == null ? new long[0] : oldValsPrim)
 349                                     .boxed().toArray(Long[]::new);
 350         for (int i = 0; i < oldVals.length; i++) {
 351             if (!CgroupMetricsTester.compareWithErrorMargin(oldVals[i], newVals[i])) {
 352                 warn(Controller.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
 353             }
 354         }
 355 
 356         oldVal = metrics.getCpuUserUsage();
 357         newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.stat", "user");
 358         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 359             warn(Controller.CPUACCT, "cpuacct.usage - user", oldVal, newVal);
 360         }
 361 
 362         oldVal = metrics.getCpuSystemUsage();
 363         newVal = getLongValueFromFile(Controller.CPUACCT, "cpuacct.stat", "system");
 364         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 365             warn(Controller.CPUACCT, "cpuacct.usage - system", oldVal, newVal);
 366         }
 367     }
 368 
 369     public void testCpuSchedulingMetrics() {
 370         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 371         long oldVal = metrics.getCpuPeriod();
 372         long newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.cfs_period_us");
 373         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 374             fail(Controller.CPUACCT, "cpu.cfs_period_us", oldVal, newVal);
 375         }
 376 
 377         oldVal = metrics.getCpuQuota();
 378         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.cfs_quota_us");
 379         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 380             fail(Controller.CPUACCT, "cpu.cfs_quota_us", oldVal, newVal);
 381         }
 382 
 383         oldVal = metrics.getCpuShares();
 384         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.shares");
 385         if (newVal == 0 || newVal == 1024) newVal = -1;
 386         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 387             fail(Controller.CPUACCT, "cpu.shares", oldVal, newVal);
 388         }
 389 
 390         oldVal = metrics.getCpuNumPeriods();
 391         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "nr_periods");
 392         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 393             fail(Controller.CPUACCT, "cpu.stat - nr_periods", oldVal, newVal);
 394         }
 395 
 396         oldVal = metrics.getCpuNumThrottled();
 397         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "nr_throttled");
 398         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 399             fail(Controller.CPUACCT, "cpu.stat - nr_throttled", oldVal, newVal);
 400         }
 401 
 402         oldVal = metrics.getCpuThrottledTime();
 403         newVal = getLongValueFromFile(Controller.CPUACCT, "cpu.stat", "throttled_time");
 404         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 405             fail(Controller.CPUACCT, "cpu.stat - throttled_time", oldVal, newVal);
 406         }
 407     }
 408 
 409     public void testCpuSets() {
 410         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 411         Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
 412         Arrays.sort(oldVal);
 413 
 414         String cpusstr = getFileContents(Controller.CPUSET, "cpuset.cpus");
 415         // Parse range string in the format 1,2-6,7
 416         Integer[] newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
 417         Arrays.sort(newVal);
 418         if (Arrays.compare(oldVal, newVal) != 0) {
 419             fail(Controller.CPUSET, "cpuset.cpus", Arrays.toString(oldVal),
 420                 Arrays.toString(newVal));
 421         }
 422 
 423         int [] cpuSets = metrics.getEffectiveCpuSetCpus();
 424 
 425         // Skip this test if this metric is not supported on this platform
 426         if (cpuSets.length != 0) {
 427             oldVal = Arrays.stream(cpuSets).boxed().toArray(Integer[]::new);
 428             Arrays.sort(oldVal);
 429             cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_cpus");
 430             newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);


 458                 fail(Controller.CPUSET, "cpuset.effective_mems", Arrays.toString(oldVal),
 459                         Arrays.toString(newVal));
 460             }
 461         }
 462 
 463         double oldValue = metrics.getCpuSetMemoryPressure();
 464         double newValue = getDoubleValueFromFile(Controller.CPUSET, "cpuset.memory_pressure");
 465         if (!CgroupMetricsTester.compareWithErrorMargin(oldValue, newValue)) {
 466             fail(Controller.CPUSET, "cpuset.memory_pressure", oldValue, newValue);
 467         }
 468 
 469         boolean oldV = metrics.isCpuSetMemoryPressureEnabled();
 470         boolean newV = getLongValueFromFile(Controller.CPUSET,
 471                 "cpuset.memory_pressure_enabled") == 1 ? true : false;
 472         if (oldV != newV) {
 473             fail(Controller.CPUSET, "cpuset.memory_pressure_enabled", oldV, newV);
 474         }
 475     }
 476 
 477     private void testBlkIO() {
 478         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 479             long oldVal = metrics.getBlkIOServiceCount();
 480         long newVal = getLongValueFromFile(Controller.BLKIO,
 481                 "blkio.throttle.io_service_bytes", "Total");
 482         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 483             fail(Controller.BLKIO, "blkio.throttle.io_service_bytes - Total",
 484                     oldVal, newVal);
 485         }
 486 
 487         oldVal = metrics.getBlkIOServiced();
 488         newVal = getLongValueFromFile(Controller.BLKIO, "blkio.throttle.io_serviced", "Total");
 489         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 490             fail(Controller.BLKIO, "blkio.throttle.io_serviced - Total", oldVal, newVal);
 491         }
 492     }
 493 
 494     public void testCpuConsumption() throws IOException, InterruptedException {
 495         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 496         // make system call
 497         long newSysVal = metrics.getCpuSystemUsage();
 498         long newUserVal = metrics.getCpuUserUsage();
 499         long newUsage = metrics.getCpuUsage();
 500         long[] newPerCpu = metrics.getPerCpuUsage();
 501         if (newPerCpu == null) {
 502             newPerCpu = new long[0];
 503         }
 504 
 505         // system/user CPU usage counters may be slowly increasing.
 506         // allow for equal values for a pass
 507         if (newSysVal < startSysVal) {
 508             fail(Controller.CPU, "getCpuSystemUsage", newSysVal, startSysVal);
 509         }
 510 
 511         // system/user CPU usage counters may be slowly increasing.
 512         // allow for equal values for a pass
 513         if (newUserVal < startUserVal) {
 514             fail(Controller.CPU, "getCpuUserUsage", newUserVal, startUserVal);
 515         }
 516 
 517         if (newUsage <= startUsage) {
 518             fail(Controller.CPU, "getCpuUsage", newUsage, startUsage);
 519         }
 520 
 521         boolean success = false;
 522         for (int i = 0; i < startPerCpu.length; i++) {
 523             if (newPerCpu[i] > startPerCpu[i]) {
 524                 success = true;
 525                 break;
 526             }
 527         }
 528 
 529         if(!success) fail(Controller.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
 530                 Arrays.toString(startPerCpu));
 531     }
 532 
 533     public void testMemoryUsage() throws Exception {
 534         MetricsCgroupV1 metrics = (MetricsCgroupV1)Metrics.systemMetrics();
 535         long memoryMaxUsage = metrics.getMemoryMaxUsage();
 536         long memoryUsage = metrics.getMemoryUsage();
 537         long newMemoryMaxUsage = 0, newMemoryUsage = 0;
 538 
 539         // allocate memory in a loop and check more than once for new values
 540         // otherwise we might see seldom the effect of decreasing new memory values
 541         // e.g. because the system could free up memory
 542         byte[][] bytes = new byte[32][];
 543         for (int i = 0; i < 32; i++) {
 544             bytes[i] = new byte[8*1024*1024];
 545             newMemoryUsage = metrics.getMemoryUsage();
 546             if (newMemoryUsage > memoryUsage) {
 547                 break;
 548             }
 549         }
 550         newMemoryMaxUsage = metrics.getMemoryMaxUsage();
 551 
 552         if (newMemoryMaxUsage < memoryMaxUsage) {
 553             fail(Controller.MEMORY, "getMemoryMaxUsage", memoryMaxUsage,
 554                     newMemoryMaxUsage);
< prev index next >