Skip to content

Commit

Permalink
增加GPU信息
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Jan 18, 2018
1 parent c030515 commit 5af18db
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 16 deletions.
99 changes: 84 additions & 15 deletions src/main/java/monitorx/monitorxethminer/ReportSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import monitorx.monitorxethminer.statusReport.Metric;
import monitorx.monitorxethminer.statusReport.NodeStatus;
import monitorx.monitorxethminer.statusReport.NodeStatusUpload;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,9 +13,13 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author qianlifeng
Expand All @@ -27,6 +32,9 @@ public class ReportSchedule {
@Autowired
EthMinerService ethMinerService;

Pattern GPUTemperaturePattern = Pattern.compile("GPU Temperature:(.*)C");
Pattern GPULoadPattern = Pattern.compile("GPU Load:(.*) %");

private Date lastUploadDate;

@Value("${code}")
Expand All @@ -35,21 +43,17 @@ public class ReportSchedule {
@Value("${url}")
private String url;

@Scheduled(fixedDelay = 1000)
public void doNothing() {
Date lastTailDate = ethMinerService.getLastTailDate();
if (lastTailDate != null && (lastUploadDate == null || lastUploadDate.compareTo(lastTailDate) != 0)) {
if (ethMinerService.getLastTailMh() != null) {
lastUploadDate = lastTailDate;
@Value("${gpuFolder}")
private String gpuFolder;

upload(lastTailDate, ethMinerService.getLastTailMh());
}
}
@Scheduled(fixedDelay = 3000)
public void report() {
upload();
}

private void upload(Date lastTailDate, Integer lastTailMh) {
private void upload() {
try {
logger.info("reporting to monitorx: {} {}", lastTailMh, lastTailDate);
logger.info("reporting to monitorx");
NodeStatusUpload statusUpload = new NodeStatusUpload();
NodeStatus status = new NodeStatus();
statusUpload.setNodeCode(code);
Expand All @@ -62,12 +66,77 @@ private void upload(Date lastTailDate, Integer lastTailMh) {
Metric currentHandlingOrders = new Metric();
currentHandlingOrders.setTitle("实时算力");
currentHandlingOrders.setType("number");
currentHandlingOrders.setValue(lastTailMh.toString());
metrics.add(currentHandlingOrders);
Date lastTailDate = ethMinerService.getLastTailDate();
Integer lastTailMh = ethMinerService.getLastTailMh();
if (lastTailDate != null && (lastUploadDate == null || lastUploadDate.compareTo(lastTailDate) != 0)) {
if (ethMinerService.getLastTailMh() != null) {
lastUploadDate = lastTailDate;
currentHandlingOrders.setValue(lastTailMh.toString());
}
} else {
currentHandlingOrders.setValue("0");
}

Metric gpuMetric = new Metric();
List<Map<String, String>> gpuInfo = getGPUInfo();
gpuMetric.setTitle("GPU信息");
gpuMetric.setType("text");
StringBuilder sb = new StringBuilder();
sb.append("<table class='table table-bordered'>");
sb.append(" <tr>");
sb.append(" <th width='60'>序号</th>");
sb.append(" <th>温度</th>");
sb.append(" <th>负载</th>");
sb.append(" </tr>");
for (Map<String, String> gpu : gpuInfo) {
sb.append(" <tr>");
sb.append(" <td>" + gpu.get("index") + "</td>");
sb.append(" <td>" + gpu.get("temperature") + "</td>");
sb.append(" <td>" + gpu.get("load") + "</td>");
sb.append(" </tr>");
}
sb.append("</table>");

gpuMetric.setValue(sb.toString());
gpuMetric.setContext(JSON.toJSONString(gpuInfo));
metrics.add(gpuMetric);

HTTPUtil.sendBodyPost(url, JSON.toJSONString(statusUpload));
} catch (IOException e) {
logger.error("upload to monitorx error: " + e.getMessage(), e);
}
}

private List<Map<String, String>> getGPUInfo() {
List<Map<String, String>> info = new ArrayList<>();
if (StringUtils.isNotEmpty(gpuFolder)) {
for (int i = 1; i < 7; i++) {
Path path = Paths.get(gpuFolder, i + "", "amdgpu_pm_info");
try {
String content = new String(Files.readAllBytes(path));
Matcher temperatureMatcher = GPUTemperaturePattern.matcher(content);
Matcher loadMatcher = GPULoadPattern.matcher(content);
if (temperatureMatcher.find() && loadMatcher.find()) {
Map<String, String> infoMap = new HashMap<>();
String temperature = temperatureMatcher.group(1).trim();
String load = loadMatcher.group(1).trim();

infoMap.put("index", i + "");
infoMap.put("temperature", temperature + " C");
infoMap.put("load", load + "%");
info.add(infoMap);
} else {
logger.info("didn't find");
}
} catch (NoSuchFileException e) {
break;
} catch (IOException e) {
logger.error("read gpu info failed, path={}", path.toString(), e);
}
}
}

return info;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
log: ./nohup.out
wallet: 0x68950Ea934f9D4E3FF889e06369ec29b52c0123A
code: ethminer
url: http://localhost:8080/api/status/upload/
url: http://localhost:8080/api/status/upload/
gpuFolder: /app/dri

0 comments on commit 5af18db

Please sign in to comment.