Skip to content

Commit 8272dff

Browse files
committed
better Java/system reporting
1 parent 9164e39 commit 8272dff

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.opengrok.indexer.util.Executor;
8585
import org.opengrok.indexer.util.HostUtil;
8686
import org.opengrok.indexer.util.OptionParser;
87+
import org.opengrok.indexer.util.RuntimeUtil;
8788
import org.opengrok.indexer.util.Statistics;
8889

8990
import static org.opengrok.indexer.util.RuntimeUtil.checkJavaVersion;
@@ -372,8 +373,9 @@ public static void main(String[] argv) {
372373
}
373374
}
374375

375-
LOGGER.log(Level.INFO, "Indexer version {0} ({1}) running on Java {2}",
376-
new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()});
376+
LOGGER.log(Level.INFO, "Indexer version {0} ({1}) running on Java {2} with properties: {3}",
377+
new Object[]{Info.getVersion(), Info.getRevision(), RuntimeUtil.getJavaVersion(),
378+
RuntimeUtil.getJavaProperties()});
377379

378380
checkJavaVersion();
379381

opengrok-indexer/src/main/java/org/opengrok/indexer/util/RuntimeUtil.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323
package org.opengrok.indexer.util;
2424

25+
2526
public class RuntimeUtil {
2627
private RuntimeUtil() {
2728
// private for static
@@ -45,4 +46,43 @@ public static void checkJavaVersion() throws RuntimeException {
4546
majorVersion, JAVA_VERSION_MIN, JAVA_VERSION_MAX));
4647
}
4748
}
49+
50+
/**
51+
* @return string representing Java version and some properties
52+
*/
53+
public static String getJavaVersion() {
54+
StringBuilder stringBuilder = new StringBuilder();
55+
stringBuilder.append("version: ").append(Runtime.version());
56+
stringBuilder.append(", name: ").append(System.getProperty("java.vm.name"));
57+
stringBuilder.append(", vendor: ").append(System.getProperty("java.vendor"));
58+
stringBuilder.append(", arch: ").append(System.getProperty("os.arch"));
59+
return stringBuilder.toString();
60+
}
61+
62+
private static String bytesToHumanReadable(long size) {
63+
if (size < 0) {
64+
throw new IllegalArgumentException("Invalid size: " + size);
65+
}
66+
if (size < 1024) {
67+
return size + " bytes";
68+
}
69+
String units = " KMGTPE";
70+
int idx = 0;
71+
float value = size;
72+
while (value > 1024) {
73+
value /= 1024;
74+
idx++;
75+
}
76+
return String.format("%.1f %siB", value, units.substring(idx, idx + 1));
77+
}
78+
79+
/**
80+
* @return string representing Java properties of interest
81+
*/
82+
public static String getJavaProperties() {
83+
StringBuilder stringBuilder = new StringBuilder();
84+
stringBuilder.append("ncpu: ").append(Runtime.getRuntime().availableProcessors());
85+
stringBuilder.append(", maxMemory: ").append(bytesToHumanReadable(Runtime.getRuntime().maxMemory()));
86+
return stringBuilder.toString();
87+
}
4888
}

opengrok-web/src/main/java/org/opengrok/web/WebappListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.opengrok.indexer.index.IndexCheck;
4141
import org.opengrok.indexer.index.IndexCheckException;
4242
import org.opengrok.indexer.logger.LoggerFactory;
43+
import org.opengrok.indexer.util.RuntimeUtil;
4344
import org.opengrok.indexer.web.SearchHelper;
4445
import org.opengrok.web.api.ApiTaskManager;
4546
import org.opengrok.web.api.v1.controller.ConfigurationController;
@@ -79,8 +80,9 @@ public void contextInitialized(final ServletContextEvent servletContextEvent) {
7980
ServletContext context = servletContextEvent.getServletContext();
8081
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
8182

82-
LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1}) on Java {2}",
83-
new Object[]{Info.getVersion(), Info.getRevision(), Runtime.version()});
83+
LOGGER.log(Level.INFO, "Starting webapp with version {0} ({1}) on Java {2} with properties {3}",
84+
new Object[]{Info.getVersion(), Info.getRevision(), RuntimeUtil.getJavaVersion(),
85+
RuntimeUtil.getJavaProperties()});
8486

8587
checkJavaVersion();
8688

0 commit comments

Comments
 (0)