@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 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; }
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)); } } }
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 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); } }
@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(); }