@Override public Optional<ContainerStats> getContainerStats(ContainerName containerName) { try { DockerStatsCallback statsCallback = dockerClient.statsCmd(containerName.asString()).exec(new DockerStatsCallback()); statsCallback.awaitCompletion(5, TimeUnit.SECONDS); return statsCallback.stats.map(stats -> new ContainerStats( stats.getNetworks(), stats.getCpuStats(), stats.getMemoryStats(), stats.getBlkioStats())); } catch (NotFoundException ignored) { return Optional.empty(); } catch (RuntimeException | InterruptedException e) { numberOfDockerDaemonFails.add(); throw new DockerException("Failed to get stats for container '" + containerName.asString() + "'", e); } }
public static CubeStatistics updateStats(Statistics statistics) { CubeStatistics stats = new CubeStatistics(); Map<String, Long> blkio = extractIORW(statistics.getBlkioStats()); Map<String, Long> memory = extractMemoryStats(statistics.getMemoryStats(), "usage", "max_usage", "limit"); stats.setIoBytesRead(blkio.get("io_bytes_read")); stats.setIoBytesWrite(blkio.get("io_bytes_write")); stats.setMaxUsage(memory.get("max_usage")); stats.setUsage(memory.get("usage")); stats.setLimit(memory.get("limit")); stats.setNetworks(extractNetworksStats(statistics.getNetworks())); return stats; }
/** * set in {@link MicroserviceStatus} cpu usage and memory usage of given {@link Container} * * @param containerId - id of {@link Container} */ public void setUsage(String containerId) { DockerUtil docker = DockerUtil.getInstance(); if (docker.isContainerRunning(containerId)) { Optional<Statistics> statisticsBefore = docker.getContainerStats(containerId); try { Thread.sleep(200); } catch (InterruptedException exp) { LoggingService.logWarning(MODULE_NAME, exp.getMessage()); } Optional<Statistics> statisticsAfter = docker.getContainerStats(containerId); if (statisticsBefore.isPresent() && statisticsAfter.isPresent()) { Map<String, Object> usageBefore = statisticsBefore.get().getCpuStats(); float totalUsageBefore = extractTotalUsage(usageBefore); float systemCpuUsageBefore = extractSystemCpuUsage(usageBefore); Map<String, Object> usageAfter = statisticsAfter.get().getCpuStats(); float totalUsageAfter = extractTotalUsage(usageAfter); float systemCpuUsageAfter = extractSystemCpuUsage(usageAfter); setCpuUsage(Math.abs(1000f * ((totalUsageAfter - totalUsageBefore) / (systemCpuUsageAfter - systemCpuUsageBefore)))); Map<String, Object> memoryUsage = statisticsAfter.get().getMemoryStats(); setMemoryUsage(extractMemoryUsage(memoryUsage)); } } }
@Override protected double[] getMem() { double[] out = { 0, 0 }; Statistics stats = docker.getStatistics(containerId); MemoryStatsConfig memoryStats = stats.getMemoryStats(); long usage = memoryStats.getUsage(); long limit = memoryStats.getLimit(); double memPercent = usage / limit * 100; out[0] = usage; out[1] = memPercent; return out; }
@Override @SuppressWarnings("unchecked") protected NetInfo getNetInfo() { NetInfo netInfo = new NetInfo(); Statistics stats = docker.getStatistics(containerId); Map<String, StatisticNetworksConfig> networksStats = stats.getNetworks(); for (String key : networksStats.keySet()) { Map<String, Object> iface = (Map<String, Object>) networksStats.get(key); int rxBytes = (Integer) iface.get("rx_bytes"); int txBytes = (Integer) iface.get("tx_bytes"); netInfo.putNetInfo(key, rxBytes, txBytes); } return netInfo; }
@Override @SuppressWarnings("unchecked") protected double getCpuUsage() { double cpuUsage = 0; Statistics stats = docker.getStatistics(containerId); CpuStatsConfig cpuStats = stats.getCpuStats(); if (cpuStats != null) { Map<String, Object> cpuUsageMap = (Map<String, Object>) cpuStats.getCpuUsage(); long systemUsage = Long.parseLong(cpuStats.getSystemCpuUsage().toString()); long totalUsage = Long.parseLong(cpuUsageMap.get("total_usage").toString()); List<Object> perCpuUsage = (List<Object>) cpuUsageMap.get("percpu_usage"); if (previousCpu != -1 && previousSystem != -1) { // Using same formula than // https://github.com/docker/docker/blob/master/api/client/stats.go float cpuDelta = totalUsage - previousCpu; float systemDelta = systemUsage - previousSystem; if (cpuDelta > 0 && systemDelta > 0) { cpuUsage = cpuDelta / systemDelta * perCpuUsage.size() * 100; } } previousCpu = totalUsage; previousSystem = systemUsage; } return cpuUsage; }
private void configureCube() throws IOException { cubeRegistry = new LocalCubeRegistry(); when(cube.getId()).thenReturn(CUBE_ID); cubeRegistry.addCube(cube); when(statistics.getNetworks()).thenReturn(getNetworks()); when(statistics.getMemoryStats()).thenReturn(getMemory()); when(statistics.getBlkioStats()).thenReturn(getIOStats()); when(dockerClientExecutor.statsContainer(CUBE_ID)).thenReturn(statistics); }
@Override protected double[] getMem() { double[] out = { 0, 0 }; Statistics stats = docker.getStatistics(containerId); MemoryStatsConfig memoryStats = stats.getMemoryStats(); long usage = memoryStats.getUsage(); long limit = memoryStats.getLimit(); double memPercent = usage / limit * 100; out[0] = usage; out[1] = memPercent; return out; }
@Override @SuppressWarnings("unchecked") protected NetInfo getNetInfo() { NetInfo netInfo = new NetInfo(); Statistics stats = docker.getStatistics(containerId); Map<String, StatisticNetworksConfig> networksStats = stats.getNetworks(); for (String key : networksStats.keySet()) { Map<String, Object> iface = (Map<String, Object>) networksStats.get(key); int rxBytes = (Integer) iface.get("rx_bytes"); int txBytes = (Integer) iface.get("tx_bytes"); netInfo.putNetInfo(key, rxBytes, txBytes); } return netInfo; }
@Override @SuppressWarnings("unchecked") protected double getCpuUsage() { double cpuUsage = 0; Statistics stats = docker.getStatistics(containerId); CpuStatsConfig cpuStats = stats.getCpuStats(); if (cpuStats != null) { Map<String, Object> cpuUsageMap = (Map<String, Object>) cpuStats.getCpuUsage(); long systemUsage = Long.parseLong(cpuStats.getSystemCpuUsage().toString()); long totalUsage = Long.parseLong(cpuUsageMap.get("total_usage").toString()); List<Object> perCpuUsage = (List<Object>) cpuUsageMap.get("percpu_usage"); if (previousCpu != -1 && previousSystem != -1) { // Using same formula than // https://github.com/docker/docker/blob/master/api/client/stats.go float cpuDelta = totalUsage - previousCpu; float systemDelta = systemUsage - previousSystem; if (cpuDelta > 0 && systemDelta > 0) { cpuUsage = cpuDelta / systemDelta * perCpuUsage.size() * 100; } } previousCpu = totalUsage; previousSystem = systemUsage; } return cpuUsage; }
@Override public void onNext(Statistics stats) { Map<String, Object> memStat = stats.getMemoryStats(); Map<String, Object> blkioStat = stats.getBlkioStats(); Map<String, Object> netStat = stats.getNetworks(); double cpuPercent = 0.0; if (this.preStats != null) { cpuPercent = calculateCpuPercentUnix(this.preStats, stats); } int[] netIO = calculateNetwork(netStat); int[] memUsageLimit = calculateMemory(memStat); int[] blockIo = calculateBlock(blkioStat); logHost.log(Level.INFO, "\n%s(%s)\nCPU %4.3f%% | MEM %4.3f%% (%d KB / %d KB) | NET %d KB / %d KB | BLOCK %d KB / %d KB", this.containerId, this.containerUri, cpuPercent, (memUsageLimit[0] * 1.0 / memUsageLimit[1] * 1.0) * 100, memUsageLimit[0] / 1024, memUsageLimit[1] / 1024, netIO[0] / 1024, netIO[1] / 1024, blockIo[0] / 1024, blockIo[1] / 1024); this.preStats = stats; this.countDownLatch.countDown(); }
/** * reference: {@Link https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go} * @return */ public static double calculateCpuPercentUnix(Statistics preCpuStats, Statistics curCpuStats) { Map<String, Object> preCpuStat = preCpuStats.getCpuStats(); long preSystemUsage = (Long) preCpuStat.get(SYSTEM_CPU_USAGE); Map<String, Object> preCpuUsage = (Map) preCpuStat.get(CPU_USAGE); long preTotalUsage = (Long) preCpuUsage.get(TOTAL_USAGE); int preCpuCount = (Integer) preCpuStat.get(ONLINE_CPUS); Map<String, Object> curCpuStat = curCpuStats.getCpuStats(); long curSystemUsage = (Long) curCpuStat.get(SYSTEM_CPU_USAGE); Map<String, Object> curCpuUsage = (Map) curCpuStat.get(CPU_USAGE); long curTotalUsage = (Long) curCpuUsage.get(TOTAL_USAGE); int curCpuCount = (Integer) curCpuStat.get(ONLINE_CPUS); double cpuPercent = 0.0; if (curCpuCount != preCpuCount) { logHost.log(Level.INFO, String.format("cpu count miss match pre: %d, cur: %d", preCpuCount, curCpuCount)); return cpuPercent; } double cpuDelta = curTotalUsage - preTotalUsage; double systemDelta = curSystemUsage - preSystemUsage; if (systemDelta > 0.0 && cpuDelta > 0.0) { cpuPercent = (cpuDelta / systemDelta) * curCpuCount * 100.0; } return cpuPercent; }