Skip to content

Commit 9cc4bcd

Browse files
authored
RATIS-2401. Add memory info to VersionInfo. (#1343)
1 parent 2bbff44 commit 9cc4bcd

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

ratis-common/src/main/java/org/apache/ratis/util/VersionInfo.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.Map;
2929
import java.util.Objects;
3030
import java.util.Properties;
31+
import java.util.SortedMap;
32+
import java.util.TreeMap;
33+
import java.util.function.BiConsumer;
3134
import java.util.function.Consumer;
3235

3336
/**
@@ -58,14 +61,18 @@ static SoftwareInfo parse(String key) {
5861

5962
private enum RuntimeInfo {
6063
// the ordering is the output ordering
61-
JAVA, USER;
64+
JAVA, USER, MEMORY;
6265

6366
static final InfoMap<RuntimeInfo> MAP;
6467

6568
static {
6669
final EnumMap<RuntimeInfo, String> map = new EnumMap<>(RuntimeInfo.class);
6770
final Properties properties = System.getProperties();
6871
map.put(JAVA, properties.getProperty("java.vm.name") + " " + properties.getProperty("java.runtime.version"));
72+
final Runtime r = Runtime.getRuntime();
73+
map.put(MEMORY, "max: " + TraditionalBinaryPrefix.long2String(r.maxMemory())
74+
+ ", total: " + TraditionalBinaryPrefix.long2String(r.totalMemory())
75+
+ ", free: " + TraditionalBinaryPrefix.long2String(r.freeMemory()));
6976
map.put(USER, properties.getProperty("user.name"));
7077
MAP = new InfoMap<>(map);
7178
}
@@ -143,7 +150,17 @@ public void printStartupMessages(Object name, Consumer<String> log) {
143150
}
144151
}
145152

153+
static void printSystemProperties(BiConsumer<String, Object> out) {
154+
final SortedMap<String, Object> sortedMap = new TreeMap<>();
155+
for(Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
156+
sortedMap.put(e.getKey().toString(), e.getValue());
157+
}
158+
sortedMap.forEach(out);
159+
}
160+
146161
public static void main(String[] args) {
162+
printSystemProperties((key, value) -> System.out.printf("%-40s = %s%n", key, value));
163+
147164
VersionInfo.load(VersionInfo.class).printStartupMessages(":", System.out::println);
148165
}
149166
}

0 commit comments

Comments
 (0)