1 /*
   2  * Copyright (c) 2018, 2020, 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 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;
  62         }
  63 
  64         public String value() {
  65             return value;
  66         }
  67     }
  68 
  69     private static final Set<String> allowedSubSystems =
  70             Stream.of(Controller.values()).map(Controller::value).collect(Collectors.toSet());
  71 
  72     private static final Map<String, String[]> subSystemPaths = new HashMap<>();
  73 
  74     private static void setPath(String[] line) {
  75         String cgroupPath = line[2];
  76         String[] subSystems = line[1].split(",");
  77 
  78         for (String subSystem : subSystems) {
  79             if (allowedSubSystems.contains(subSystem)) {
  80                 String[] paths = subSystemPaths.get(subSystem);
  81                 String finalPath = "";
  82                 String root = paths[0];
  83                 String mountPoint = paths[1];
  84                 if (root != null && cgroupPath != null) {
  85                     if (root.equals("/")) {
  86                         if (!cgroupPath.equals("/")) {
  87                             finalPath = mountPoint + cgroupPath;
  88                         } else {
  89                             finalPath = mountPoint;
  90                         }
  91                     } else {
  92                         if (root.equals(cgroupPath)) {
  93                             finalPath = mountPoint;
  94                         } else {
  95                             if (cgroupPath.startsWith(root)) {
  96                                 if (cgroupPath.length() > root.length()) {
  97                                     String cgroupSubstr = cgroupPath.substring(root.length());
  98                                     finalPath = mountPoint + cgroupSubstr;
  99                                 }
 100                             }
 101                         }
 102                     }
 103                 }
 104                 subSystemPaths.put(subSystem, new String[]{finalPath, mountPoint});
 105             }
 106         }
 107     }
 108 
 109     private static void createSubsystems(String[] line) {
 110         if (line.length < 5) return;
 111         Path p = Paths.get(line[4]);
 112         String subsystemName = p.getFileName().toString();
 113         if (subsystemName != null) {
 114             for (String subSystem : subsystemName.split(",")) {
 115                 if (allowedSubSystems.contains(subSystem)) {
 116                     subSystemPaths.put(subSystem, new String[]{line[3], line[4]});
 117                 }
 118             }
 119         }
 120     }
 121 
 122     public void setup() {
 123         Metrics metrics = Metrics.systemMetrics();
 124         // Initialize CPU usage metrics before we do any testing.
 125         startSysVal = metrics.getCpuSystemUsage();
 126         startUserVal = metrics.getCpuUserUsage();
 127         startUsage = metrics.getCpuUsage();
 128         startPerCpu = metrics.getPerCpuUsage();
 129         if (startPerCpu == null) {
 130             startPerCpu = new long[0];
 131         }
 132 
 133         try {
 134             Stream<String> lines = Files.lines(Paths.get("/proc/self/mountinfo"));
 135             lines.filter(line -> line.contains(" - cgroup cgroup "))
 136                     .map(line -> line.split(" "))
 137                     .forEach(MetricsTesterCgroupV1::createSubsystems);
 138             lines.close();
 139 
 140             lines = Files.lines(Paths.get("/proc/self/cgroup"));
 141             lines.map(line -> line.split(":"))
 142                     .filter(line -> (line.length >= 3))
 143                     .forEach(MetricsTesterCgroupV1::setPath);
 144             lines.close();
 145         } catch (IOException e) {
 146         }
 147     }
 148 
 149     private static String getFileContents(Controller subSystem, String fileName) {
 150         String fname = subSystemPaths.get(subSystem.value())[0] + File.separator + fileName;
 151         try {
 152             return new Scanner(new File(fname)).useDelimiter("\\Z").next();
 153         } catch (FileNotFoundException e) {
 154             System.err.println("Unable to open : " + fname);
 155             return null;
 156         }
 157     }
 158 
 159     private static long getLongValueFromFile(Controller subSystem, String fileName) {
 160         String data = getFileContents(subSystem, fileName);
 161         return (data == null || data.isEmpty()) ? 0L : convertStringToLong(data);
 162     }
 163 
 164     private static long convertStringToLong(String strval) {
 165         return CgroupMetricsTester.convertStringToLong(strval, Long.MAX_VALUE);
 166     }
 167 
 168     private static long getLongValueFromFile(Controller subSystem, String metric, String subMetric) {
 169         String stats = getFileContents(subSystem, metric);
 170         String[] tokens = stats.split("[\\r\\n]+");
 171         for (int i = 0; i < tokens.length; i++) {
 172             if (tokens[i].startsWith(subMetric)) {
 173                 String strval = tokens[i].split("\\s+")[1];
 174                 return convertStringToLong(strval);
 175             }
 176         }
 177         return 0L;
 178     }
 179 
 180     private static double getDoubleValueFromFile(Controller subSystem, String fileName) {
 181         String data = getFileContents(subSystem, fileName);
 182         return data.isEmpty() ? 0.0 : Double.parseDouble(data);
 183     }
 184 
 185     private static void fail(Controller system, String metric, long oldVal, long testVal) {
 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         }
 227 
 228         oldVal = metrics.getMemoryUsage();
 229         newVal = getLongValueFromFile(Controller.MEMORY, "memory.usage_in_bytes");
 230         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 231             fail(Controller.MEMORY, "memory.usage_in_bytes", oldVal, newVal);
 232         }
 233 
 234         // Kernel memory
 235         oldVal = metrics.getKernelMemoryFailCount();
 236         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.failcnt");
 237         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 238             fail(Controller.MEMORY, "memory.kmem.failcnt", oldVal, newVal);
 239         }
 240 
 241         oldVal = metrics.getKernelMemoryLimit();
 242         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.limit_in_bytes");
 243         newVal = newVal > unlimited_minimum ? -1L : newVal;
 244         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 245             fail(Controller.MEMORY, "memory.kmem.limit_in_bytes", oldVal, newVal);
 246         }
 247 
 248         oldVal = metrics.getKernelMemoryMaxUsage();
 249         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.max_usage_in_bytes");
 250         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 251             fail(Controller.MEMORY, "memory.kmem.max_usage_in_bytes", oldVal, newVal);
 252         }
 253 
 254         oldVal = metrics.getKernelMemoryUsage();
 255         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.usage_in_bytes");
 256         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 257             fail(Controller.MEMORY, "memory.kmem.usage_in_bytes", oldVal, newVal);
 258         }
 259 
 260         //TCP Memory
 261         oldVal = metrics.getTcpMemoryFailCount();
 262         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.tcp.failcnt");
 263         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 264             fail(Controller.MEMORY, "memory.kmem.tcp.failcnt", oldVal, newVal);
 265         }
 266 
 267         oldVal = metrics.getTcpMemoryLimit();
 268         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.tcp.limit_in_bytes");
 269         newVal = newVal > unlimited_minimum ? -1L : newVal;
 270         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 271             fail(Controller.MEMORY, "memory.kmem.tcp.limit_in_bytes", oldVal, newVal);
 272         }
 273 
 274         oldVal = metrics.getTcpMemoryMaxUsage();
 275         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.tcp.max_usage_in_bytes");
 276         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 277             fail(Controller.MEMORY, "memory.kmem.tcp.max_usage_in_bytes", oldVal, newVal);
 278         }
 279 
 280         oldVal = metrics.getTcpMemoryUsage();
 281         newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.tcp.usage_in_bytes");
 282         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 283             fail(Controller.MEMORY, "memory.kmem.tcp.usage_in_bytes", oldVal, newVal);
 284         }
 285 
 286         //  Memory and Swap
 287         oldVal = metrics.getMemoryAndSwapFailCount();
 288         newVal = getLongValueFromFile(Controller.MEMORY, "memory.memsw.failcnt");
 289         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 290             fail(Controller.MEMORY, "memory.memsw.failcnt", oldVal, newVal);
 291         }
 292 
 293         oldVal = metrics.getMemoryAndSwapLimit();
 294         newVal = getLongValueFromFile(Controller.MEMORY, "memory.memsw.limit_in_bytes");
 295         newVal = newVal > unlimited_minimum ? -1L : newVal;
 296         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 297             fail(Controller.MEMORY, "memory.memsw.limit_in_bytes", oldVal, newVal);
 298         }
 299 
 300         oldVal = metrics.getMemoryAndSwapMaxUsage();
 301         newVal = getLongValueFromFile(Controller.MEMORY, "memory.memsw.max_usage_in_bytes");
 302         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 303             fail(Controller.MEMORY, "memory.memsw.max_usage_in_bytes", oldVal, newVal);
 304         }
 305 
 306         oldVal = metrics.getMemoryAndSwapUsage();
 307         newVal = getLongValueFromFile(Controller.MEMORY, "memory.memsw.usage_in_bytes");
 308         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
 309             fail(Controller.MEMORY, "memory.memsw.usage_in_bytes", oldVal, newVal);
 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);
 430             Arrays.sort(newVal);
 431             if (Arrays.compare(oldVal, newVal) != 0) {
 432                 fail(Controller.CPUSET, "cpuset.effective_cpus", Arrays.toString(oldVal),
 433                         Arrays.toString(newVal));
 434             }
 435         }
 436 
 437         oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new);
 438         Arrays.sort(oldVal);
 439         cpusstr = getFileContents(Controller.CPUSET, "cpuset.mems");
 440         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
 441         Arrays.sort(newVal);
 442         if (Arrays.compare(oldVal, newVal) != 0) {
 443             fail(Controller.CPUSET, "cpuset.mems", Arrays.toString(oldVal),
 444                     Arrays.toString(newVal));
 445         }
 446 
 447         int [] cpuSetMems = metrics.getEffectiveCpuSetMems();
 448 
 449         // Skip this test if this metric is not supported on this platform
 450         if (cpuSetMems.length != 0) {
 451             oldVal = Arrays.stream(cpuSetMems).boxed().toArray(Integer[]::new);
 452             Arrays.sort(oldVal);
 453             cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_mems");
 454             newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
 455             Arrays.sort(newVal);
 456             if (Arrays.compare(oldVal, newVal) != 0) {
 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);
 554         }
 555 
 556         if (newMemoryUsage < memoryUsage) {
 557             fail(Controller.MEMORY, "getMemoryUsage", memoryUsage, newMemoryUsage);
 558         }
 559     }
 560 
 561     @Override
 562     public void testMisc() {
 563         testBlkIO();
 564     }
 565 }