[
https://issues.apache.org/jira/browse/SCB-1044?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16708669#comment-16708669
]
ASF GitHub Bot commented on SCB-1044:
-------------------------------------
wujimin commented on a change in pull request #1012: [SCB-1044]add current
process CPU rate and net packets in the metrics
URL:
https://github.com/apache/servicecomb-java-chassis/pull/1012#discussion_r238653915
##########
File path:
metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/os/CpuMeter.java
##########
@@ -16,92 +16,59 @@
*/
package org.apache.servicecomb.metrics.core.meter.os;
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.util.List;
-import org.apache.commons.io.FileUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.servicecomb.metrics.core.meter.os.cpu.OsCpuUsage;
+import org.apache.servicecomb.metrics.core.meter.os.cpu.ProcessCpuUsage;
+import com.google.common.annotations.VisibleForTesting;
+import com.netflix.spectator.api.BasicTag;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
+import com.netflix.spectator.api.Tag;
public class CpuMeter {
- private static final Logger LOGGER = LoggerFactory.getLogger(CpuMeter.class);
- private double rate;
+ public static final Tag TAG_All = new BasicTag(OsMeter.OS_TYPE,
OsMeter.OS_TYPE_ALL_CPU);
- private long lastTotalTime;
+ public static final Tag TAG_CURRENT = new BasicTag(OsMeter.OS_TYPE,
OsMeter.OS_TYPE_PROCESS_CPU);
- private long lastIdleTime;
+ // read from /proc/stat
+ private OsCpuUsage allCpuUsage;
- private int cpuNum;
-
- private Id id;
+ // read from /proc/{pid}/stat
+ private ProcessCpuUsage processCpuUsage;
public CpuMeter(Id id) {
- this.id = id;
- this.cpuNum = Runtime.getRuntime().availableProcessors();
- refreshCpu();
- rate = 0.0;
- }
-
- public void calcMeasurements(List<Measurement> measurements, long msNow) {
- refreshCpu();
- measurements.add(new Measurement(id, msNow, rate));
- }
-
- /*
- * unit : 1 jiffies = 10ms = 0.01 s
- * more details :
- * http://man7.org/linux/man-pages/man5/proc.5.html
- * cpu 2445171 599297 353967 24490633 11242 0 10780 2993
0 0
- * cpu user nice system idle iowait irq softirq stealstolen
guest guest_nice
- * 0 1 2 3 4 5 6 7 8
- * cpuTotal = user + nice + system + idle + iowait + irq + softirq +
stealstolen
- */
- protected void refreshCpu() {
- try {
- File file = new File("/proc/stat");
- //just use first line
- String cpuStr = FileUtils.readLines(file, StandardCharsets.UTF_8).get(0);
- String[] cpuInfo = cpuStr.trim().split("\\s+");
- long idle = Long.parseLong(cpuInfo[4]);
- long total = 0L;
- for (int i = 1; i <= 8; i++) {
- total += Long.parseLong(cpuInfo[i]);
- }
- //just check, make sure it's safe
- if (total != lastTotalTime) {
- rate = 1.0 - (double) (idle - lastIdleTime) / (total - lastTotalTime);
- rate *= cpuNum;
- }
- lastTotalTime = total;
- lastIdleTime = idle;
- } catch (IOException e) {
- LOGGER.error("Failed to read current cpu info.", e);
- }
- }
+ allCpuUsage = new OsCpuUsage(id.withTag(TAG_All));
+ processCpuUsage = new ProcessCpuUsage(id.withTag(TAG_CURRENT));
- public double getRate() {
- return rate;
+ //must refresh all first
+ update();
+ allCpuUsage.setUsage(0);
+ processCpuUsage.setUsage(0);
}
- public long getLastTotalTime() {
- return lastTotalTime;
+ public void calcMeasurements(List<Measurement> measurements, long msNow) {
+ update();
+ measurements.add(new Measurement(allCpuUsage.getId(), msNow,
allCpuUsage.getUsage()));
+ measurements.add(new Measurement(processCpuUsage.getId(), msNow,
processCpuUsage.getUsage()));
}
- public long getLastIdleTime() {
- return lastIdleTime;
+ @VisibleForTesting
Review comment:
why so many VisibleForTesting?
just check measurements is not enough?
----------------------------------------------------------------
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:
[email protected]
> add current process CPU rate and net packets in the metrics
> ------------------------------------------------------------
>
> Key: SCB-1044
> URL: https://issues.apache.org/jira/browse/SCB-1044
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Java-Chassis
> Reporter: 何一乐
> Assignee: 何一乐
> Priority: Major
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)