[ https://issues.apache.org/jira/browse/FLINK-7812?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16565459#comment-16565459 ]
ASF GitHub Bot commented on FLINK-7812: --------------------------------------- zentol commented on a change in pull request #4801: [FLINK-7812] Log system resources metrics URL: https://github.com/apache/flink/pull/4801#discussion_r206919014 ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/utils/SystemResourcesCounter.java ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.runtime.taskexecutor.utils; + +import org.apache.flink.api.common.time.Time; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; +import oshi.hardware.CentralProcessor.TickType; +import oshi.hardware.HardwareAbstractionLayer; +import oshi.hardware.NetworkIF; + +import javax.annotation.concurrent.ThreadSafe; + +import java.util.concurrent.atomic.AtomicLongArray; +import java.util.concurrent.atomic.AtomicReferenceArray; + +import static org.apache.flink.util.Preconditions.checkState; + +/** + * Daemon thread logging system resources. + * + * To accurately and consistently report CPU and network usage we have to periodically probe + * CPU ticks and network sent/received bytes and then convert those values to CPU usage and + * send/receive byte rates. + */ +@ThreadSafe +public class SystemResourcesCounter extends Thread { + + private static final Logger LOG = LoggerFactory.getLogger(SystemResourcesCounter.class); + + private final long logIntervalMs; + private final SystemInfo systemInfo = new SystemInfo(); + private final HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware(); + + private volatile boolean running = true; + + private long[] previousCpuTicks; + private long[] bytesReceivedPerInterface; + private long[] bytesSentPerInterface; + + private volatile double cpuUser; + private volatile double cpuNice; + private volatile double cpuSys; + private volatile double cpuIdle; + private volatile double cpuIOWait; + private volatile double cpuIrq; + private volatile double cpuSoftIrq; + private volatile double cpuUsage; + + private volatile double cpuLoad1; + private volatile double cpuLoad5; + private volatile double cpuLoad15; + + private AtomicReferenceArray<Double> cpuUsagePerProcessor; + + private final String[] networkInterfaceNames; + + private AtomicLongArray receiveRatePerInterface; + private AtomicLongArray sendRatePerInterface; + + public SystemResourcesCounter(Time logInterval) { + logIntervalMs = logInterval.toMilliseconds(); + checkState(this.logIntervalMs > 0); + + setName(SystemResourcesCounter.class.getSimpleName() + " logging thread"); + + cpuUsagePerProcessor = new AtomicReferenceArray<>(hardwareAbstractionLayer.getProcessor().getLogicalProcessorCount()); + + NetworkIF[] networkIFs = hardwareAbstractionLayer.getNetworkIFs(); + bytesReceivedPerInterface = new long[networkIFs.length]; + bytesSentPerInterface = new long[networkIFs.length]; + receiveRatePerInterface = new AtomicLongArray(networkIFs.length); + sendRatePerInterface = new AtomicLongArray(networkIFs.length); + networkInterfaceNames = new String[networkIFs.length]; + + for (int i = 0; i < networkInterfaceNames.length; i++) { + networkInterfaceNames[i] = networkIFs[i].getName(); + } + } + + @Override + public void run() { + try { + while (running) { + calculateCPUUsage(hardwareAbstractionLayer.getProcessor()); + calculateNetworkUsage(hardwareAbstractionLayer.getNetworkIFs()); + Thread.sleep(logIntervalMs); + } + } catch (InterruptedException e) { + if (running) { + LOG.error("{} has failed", SystemResourcesCounter.class.getSimpleName(), e); Review comment: log as warning instead ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Log system resources as metrics > ------------------------------- > > Key: FLINK-7812 > URL: https://issues.apache.org/jira/browse/FLINK-7812 > Project: Flink > Issue Type: New Feature > Components: Metrics > Reporter: Piotr Nowojski > Assignee: Piotr Nowojski > Priority: Major > Labels: pull-request-available > -- This message was sent by Atlassian JIRA (v7.6.3#76005)