Skip to content

Commit

Permalink
Added system resources log
Browse files Browse the repository at this point in the history
  • Loading branch information
AionJayT committed Oct 25, 2019
1 parent 303d0d4 commit ece4d96
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/amity/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<blocks-queue-max>32</blocks-queue-max>
<!-- Display syncing status -->
<show-status>false</show-status>
<!--requires show-status=true; comma separated list of options: [all, peer_states, requests, seeds, leeches, responses, none]-->
<!--requires show-status=true; comma separated list of options: [all, requests, seeds, leeches, responses, systemInfo, none]-->
<show-statistics>none</show-statistics>
<!--Trigger compact when IO time is slow. slow-import and frequency values are in milliseconds-->
<compact enabled="false" slow-import="1000" frequency="600000"></compact>
Expand Down
2 changes: 1 addition & 1 deletion config/custom/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<blocks-queue-max>32</blocks-queue-max>
<!-- Display syncing status -->
<show-status>false</show-status>
<!--requires show-status=true; comma separated list of options: [all, peer_states, requests, seeds, leeches, responses, none]-->
<!--requires show-status=true; comma separated list of options: [all, requests, seeds, leeches, responses, systemInfo, none]-->
<show-statistics>none</show-statistics>
<!--Trigger compact when IO time is slow. slow-import and frequency values are in milliseconds-->
<compact enabled="false" slow-import="1000" frequency="600000"></compact>
Expand Down
2 changes: 1 addition & 1 deletion config/mainnet/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<blocks-queue-max>32</blocks-queue-max>
<!-- Display syncing status -->
<show-status>false</show-status>
<!--requires show-status=true; comma separated list of options: [all, peer_states, requests, seeds, leeches, responses, none]-->
<!--requires show-status=true; comma separated list of options: [all, requests, seeds, leeches, responses, systemInfo, none]-->
<show-statistics>none</show-statistics>
<!--Trigger compact when IO time is slow. slow-import and frequency values are in milliseconds-->
<compact enabled="false" slow-import="1000" frequency="600000"></compact>
Expand Down
3 changes: 2 additions & 1 deletion modAionImpl/src/org/aion/zero/impl/config/StatsType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public enum StatsType {
SEEDS,
LEECHES,
RESPONSES,
SYSTEMINFO,
NONE; // used as default for invalid settings

private static final List<StatsType> allSpecificTypes =
Collections.unmodifiableList(
Arrays.asList(REQUESTS, SEEDS, LEECHES, RESPONSES));
Arrays.asList(REQUESTS, SEEDS, LEECHES, RESPONSES, SYSTEMINFO));

/**
* List of all the specific types of statistics that can be displayed, i.e. excluding the {@link
Expand Down
61 changes: 61 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/sync/SyncStats.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package org.aion.zero.impl.sync;

import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.config.StatsType;
import org.aion.zero.impl.sync.statistics.BlockType;
import org.aion.zero.impl.sync.statistics.RequestStatsTracker;
Expand Down Expand Up @@ -37,6 +48,11 @@ public final class SyncStats {
private final ResponseStatsTracker responseTracker;
private final boolean responseEnabled;

private final boolean systemInfoEnabled;
private static long MB = 1024 * 1024;
private static long GB = MB * 1024;
private File dbDir;

/**
* @param enabled all stats are enabled when {@code true}, all stats are disabled otherwise
* @implNote Enables all statistics.
Expand Down Expand Up @@ -87,6 +103,11 @@ public final class SyncStats {
} else {
responseTracker = null;
}

systemInfoEnabled = showStatistics.contains(StatsType.SYSTEMINFO);
if (systemInfoEnabled) {
dbDir = CfgAion.inst().getDatabaseDir();
}
}

/**
Expand Down Expand Up @@ -304,4 +325,44 @@ public String dumpResponseStats() {
return "";
}
}

/**
* Obtain system info including cpu/mem usage, thread number and the disk free space.
*/
String dumpSystemInfo() {
if (systemInfoEnabled) {
return "System resources: "
+ "CPU load = " + getProcessCpuLoad() +"%"
+ ", JVM used mem = " + String.format("%.1f", ((double)(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MB)) + "MB"
+ ", Thread # = " + Thread.activeCount()
+ ", Disk free space = " + (dbDir == null ? "N/A" : String.format("%.1f", (double) dbDir.getFreeSpace() / GB) + "GB");
} else {
return "";
}
}

private static double getProcessCpuLoad() {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
AttributeList list = null;
try {
ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
list = mbs.getAttributes(name, new String[] {"ProcessCpuLoad"});
} catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException e) {
e.printStackTrace();
}

if (Objects.requireNonNull(list).isEmpty()) {
return Double.NaN;
}

Attribute att = (Attribute) list.get(0);
Double value = (Double) att.getValue();

// usually takes a couple of seconds before we get real values
if (value == -1.0) {
return Double.NaN;
}
// returns a percentage value with 1 decimal point precision
return ((int) (value * 1000) / 10.0);
}
}
7 changes: 7 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/sync/TaskShowStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public void run() {
}
}

if (showStatistics.contains(StatsType.SYSTEMINFO)) {
requestedStats = stats.dumpSystemInfo();
if (!requestedStats.isEmpty()) {
p2pLOG.info(requestedStats);
}
}

try {
Thread.sleep(interval);
} catch (InterruptedException e) {
Expand Down

0 comments on commit ece4d96

Please sign in to comment.