--- old/test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV2.java 2020-01-09 20:39:22.272148835 +0100 +++ new/test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV2.java 2020-01-09 20:39:22.131148687 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Red Hat Inc. + * Copyright (c) 2020, Red Hat Inc. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -149,10 +149,6 @@ CgroupMetricsTester.fail(UnifiedController.NAME, metric, oldVal, newVal); } - private void fail(String metric, String message) { - throw new RuntimeException(metric + ": " + message); - } - private void warn(String metric, long oldVal, long newVal) { CgroupMetricsTester.warn(UnifiedController.NAME, metric, oldVal, newVal); } @@ -169,9 +165,7 @@ } private void verifyPerCpuNotSupported(long[] perCpuUsage) { - if (!Arrays.equals(perCpuUsage, new long[0])) { - throw new RuntimeException("perCpuUsage expected to be not supported"); - }; + Asserts.assertNull(perCpuUsage, "perCpuUsage expected to be not supported"); } private long getCpuShares(String file) { @@ -212,6 +206,9 @@ private long getCpuValueFromFile(String file, int index) { String maxPeriod = getStringVal(file); + if (maxPeriod == null) { + return UNLIMITED; + } String[] tokens = maxPeriod.split("\\s+"); String val = tokens[index]; if (MAX.equals(val)) { @@ -228,7 +225,6 @@ public void testMemorySubsystem() { Metrics metrics = Metrics.systemMetrics(); - long memoryMaxUsageMin; // User Memory long oldVal = metrics.getMemoryFailCount(); long newVal = getLongValueEntryFromFile("memory.events", "max"); @@ -242,20 +238,12 @@ fail("memory.max", oldVal, newVal); } - memoryMaxUsageMin = oldVal = metrics.getMemoryUsage(); + oldVal = metrics.getMemoryUsage(); newVal = getLongValueFromFile("memory.current"); if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) { fail("memory.current", oldVal, newVal); } - - // Memory max has no knowledge of out of band read to "memory.current" - // above. Fix that by calling it again. - metrics.getMemoryUsage(); - long memoryMax = metrics.getMemoryMaxUsage(); - if (memoryMax < memoryMaxUsageMin || memoryMax < newVal) { - fail("memoryMaxUsage", "old usages [ " + memoryMaxUsageMin + ", " + - newVal + " ] >= " + memoryMax); - } + verifyNotSupported(metrics.getMemoryMaxUsage()); // Kernel memory verifyNotSupported(metrics.getKernelMemoryFailCount()); @@ -267,7 +255,12 @@ verifyNotSupported(metrics.getTcpMemoryFailCount()); verifyNotSupported(metrics.getTcpMemoryLimit()); verifyNotSupported(metrics.getTcpMemoryMaxUsage()); - verifyNotSupported(metrics.getTcpMemoryUsage()); + + oldVal = metrics.getTcpMemoryUsage(); + newVal = getLongValueEntryFromFile("memory.stat", "sock"); + if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) { + fail("memory.stat[sock]", oldVal, newVal); + } // Memory and Swap verifyNotSupported(metrics.getMemoryAndSwapFailCount()); @@ -291,7 +284,7 @@ fail("memory.high", oldVal, newVal); } - Asserts.assertFalse(metrics.isMemoryOOMKillEnabled(), "Not supported"); + Asserts.assertNull(metrics.isMemoryOOMKillEnabled(), "Not supported"); } @Override @@ -362,7 +355,8 @@ @Override public void testCpuSets() { Metrics metrics = Metrics.systemMetrics(); - Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new); + int[] cpus = mapNullToEmpty(metrics.getCpuSetCpus()); + Integer[] oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new); Arrays.sort(oldVal); String cpusstr = getStringVal("cpuset.cpus"); @@ -374,7 +368,8 @@ Arrays.toString(newVal)); } - oldVal = Arrays.stream(metrics.getEffectiveCpuSetCpus()).boxed().toArray(Integer[]::new); + cpus = mapNullToEmpty(metrics.getEffectiveCpuSetCpus()); + oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new); Arrays.sort(oldVal); cpusstr = getStringVal("cpuset.cpus.effective"); newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr); @@ -384,7 +379,8 @@ Arrays.toString(newVal)); } - oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new); + cpus = mapNullToEmpty(metrics.getCpuSetMems()); + oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new); Arrays.sort(oldVal); cpusstr = getStringVal("cpuset.mems"); newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr); @@ -394,7 +390,8 @@ Arrays.toString(newVal)); } - oldVal = Arrays.stream(metrics.getEffectiveCpuSetMems()).boxed().toArray(Integer[]::new); + cpus = mapNullToEmpty(metrics.getEffectiveCpuSetMems()); + oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new); Arrays.sort(oldVal); cpusstr = getStringVal("cpuset.mems.effective"); newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr); @@ -405,7 +402,16 @@ } verifyNotSupported(metrics.getCpuSetMemoryPressure()); - Asserts.assertFalse(metrics.isCpuSetMemoryPressureEnabled(), "Should be not supported"); + Asserts.assertNull(metrics.isCpuSetMemoryPressureEnabled(), "Should be not supported"); + } + + private int[] mapNullToEmpty(int[] cpus) { + if (cpus == null) { + // Not available. For sake of testing continue with an + // empty array. + cpus = new int[0]; + } + return cpus; } @Override @@ -467,7 +473,45 @@ @Override public void testMisc() { - // nothing for cgroups v2 + testIOStat(); } + private void testIOStat() { + Metrics metrics = Metrics.systemMetrics(); + long oldVal = metrics.getBlkIOServiceCount(); + long newVal = getIoStatAccumulate(new String[] { "rios", "wios" }); + if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) { + fail("io.stat->rios/wios: ", oldVal, newVal); + } + + oldVal = metrics.getBlkIOServiced(); + newVal = getIoStatAccumulate(new String[] { "rbytes", "wbytes" }); + if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) { + fail("io.stat->rbytes/wbytes: ", oldVal, newVal); + } + } + + private long getIoStatAccumulate(String[] matchNames) { + try { + return Files.lines(Paths.get(UNIFIED.getPath(), "io.stat")) + .map(line -> { + long accumulator = 0; + String[] tokens = line.split("\\s+"); + for (String t: tokens) { + String[] keyVal = t.split("="); + if (keyVal.length != 2) { + continue; + } + for (String match: matchNames) { + if (match.equals(keyVal[0])) { + accumulator += Long.parseLong(keyVal[1]); + } + } + } + return accumulator; + }).collect(Collectors.summingLong(e -> e)); + } catch (IOException e) { + return Metrics.LONG_RETVAL_NOT_SUPPORTED; + } + } }