Created
September 12, 2014 15:30
-
-
Save Sythelux/858fbcf539425ba7b797 to your computer and use it in GitHub Desktop.
System/JVM Information provider/formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.management.OperatingSystemMXBean;//TODO: package could be removed in future relaeses | |
import java.io.File; | |
import java.lang.management.ManagementFactory; | |
//import java.lang.management.OperatingSystemMXBean; | |
import java.text.DecimalFormat; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.log4j.Logger; | |
import org.apache.log4j.xml.DOMConfigurator; | |
/** | |
* This Class provides some Methods for easy access of System relevant informations like CPU, RAM, HDD usage. <br /> | |
* <b>Note, that this class uses the package: com.sun.management.OperatingSystemMXBean which is noted as to be removed in the Future:</b> | |
* | |
* @author aschaub | |
*/ | |
public class SystemInformation { | |
private static final Logger Log = Logger.getLogger(SystemInformation.class); | |
private final Runtime runtime = Runtime.getRuntime(); | |
private static String infoSpacer = " / "; | |
private static String newLine = "\n"; | |
private final OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); | |
/** | |
* formats given byte with 1024 exchange ratio. up to exabinary byte (EiB) | |
* | |
* @param size given value, that should be formatted. | |
* @return formatted byte. String like "638.2MiB". will consider regionspecific formatting ("."->"," for german). Negativ values will return "0" | |
*/ | |
public static String formatBinaryByte(long size) { | |
if (size <= 0) { | |
return "0"; | |
} | |
final String[] units = new String[]{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}; //http://physics.nist.gov/cuu/Units/binary.html | |
int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); | |
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + units[digitGroups]; | |
} | |
/** | |
* formats given byte with 1000 exchange ratio. up to exa byte (EiB) | |
* | |
* @param size given value, that should be formatted. | |
* @return formatted byte. String like "638.2MiB". will consider regionspecific formatting ("."->"," for german). Negativ values will return "0" | |
*/ | |
public static String formatDecimalByte(long size) { | |
if (size <= 0) { | |
return "0"; | |
} | |
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB", "PB", "EB"}; //http://physics.nist.gov/cuu/Units/binary.html | |
int digitGroups = (int) (Math.log10(size) / Math.log10(1000)); | |
return new DecimalFormat("#,##0.#").format(size / Math.pow(1000, digitGroups)) + units[digitGroups]; | |
} | |
/** | |
* formats given nanoSeconds up to Seconds. | |
* | |
* @param nanos | |
* @return String like: "100millis" | |
*/ | |
public static String formatNanos(long nanos) { | |
if (nanos <= 0) { | |
return "0"; | |
} | |
final String[] units = new String[]{"nanos", "micros", "millis", "s"}; | |
int digitGroups = (int) (Math.log10(nanos) / Math.log10(1000)); | |
if (digitGroups > 3) { | |
long millis = nanos / 1000000; | |
return String.format("%dd %02dh %02dm %02ds", | |
TimeUnit.MILLISECONDS.toDays(millis), | |
TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)), | |
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), | |
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); | |
} | |
return new DecimalFormat("#,##0.#").format(nanos / Math.pow(1000, digitGroups)) + units[digitGroups]; | |
} | |
/** | |
* | |
* @param floated | |
* @return | |
*/ | |
public static String formatPercentage(double floated) { | |
return Math.round(floated * 100.0) + "%"; | |
} | |
public String getCPUCount() { | |
return getCPUCountRaw() + ""; | |
} | |
public long getCPUCountRaw() { | |
return runtime.availableProcessors(); | |
} | |
public String getProcessCPUTime() { | |
long ret = getProcessCPUTimeRaw(); | |
// long ret =-1; | |
if (ret < 0) { | |
return "not supported"; | |
} | |
return formatNanos(ret); | |
} | |
public long getProcessCPUTimeRaw() { | |
return osBean.getProcessCpuTime(); | |
} | |
public String getSystemCPULoad() { //TODO: package could be removed in future relaeses. | |
double ret = getSystemCPULoadRaw(); | |
// double ret = -1; | |
if (ret < 0) { | |
return "not supported"; | |
} | |
return formatPercentage(ret); | |
} | |
public double getSystemCPULoadRaw(){ | |
return osBean.getSystemLoadAverage(); | |
} | |
public String getProcessCPULoad() { //TODO: package could be removed in future relaeses. | |
double ret = getProcessCPULoadRaw(); | |
// double ret = -1; | |
if (ret < 0) { | |
return "not supported"; | |
} | |
return formatPercentage(ret); | |
} | |
public double getProcessCPULoadRaw(){ | |
return osBean.getProcessCpuLoad(); | |
} | |
public String getSystemCPUFree() { //TODO: package could be removed in future relaeses. | |
double ret = getSystemCPUFreeRaw(); | |
// double ret = -1; | |
if (ret > 1) { | |
return "not supported"; | |
} | |
return formatPercentage(getSystemCPUFreeRaw()); | |
} | |
public double getSystemCPUFreeRaw(){ | |
return 1.0 - osBean.getSystemLoadAverage(); | |
} | |
public String getMemoryFree() { | |
return formatBinaryByte(getMemoryFreeRaw()); | |
} | |
public long getMemoryFreeRaw() { | |
return runtime.freeMemory(); | |
} | |
public String getMemoryUsed() { | |
return formatBinaryByte(getMemoryUsedRaw()); | |
} | |
public long getMemoryUsedRaw() { | |
return runtime.totalMemory() - runtime.freeMemory(); | |
} | |
public String getMemoryMax() { | |
long maxMemory = getMemoryMaxRaw(); | |
return maxMemory == Long.MAX_VALUE ? "no limit" : formatBinaryByte(maxMemory); | |
} | |
public long getMemoryMaxRaw() { | |
return runtime.maxMemory(); | |
} | |
public String getHDDInfo(File... hdds) { | |
StringBuilder sb = new StringBuilder("Count: ").append(hdds.length).append(newLine).append("\t"); | |
for (File file : hdds) { | |
sb.append(getHDDInfo(file).replace(newLine, newLine + "\t")).append(newLine); | |
} | |
return sb.toString(); | |
} | |
public String getHDDInfo(File hdd) { | |
StringBuilder sb = new StringBuilder("absolutePath: ").append(hdd.getAbsolutePath()).append(newLine); | |
sb.append("Free: ").append(getHDDFree(hdd)).append(" (total/usable)").append(newLine); | |
sb.append("Used: ").append(getHDDUsed(hdd)).append(newLine); | |
sb.append("ize: ").append(getHDDTotal(hdd)).append(newLine); | |
return sb.toString(); | |
} | |
public String getHDDFree(File hdd) { | |
return formatBinaryByte(hdd.getFreeSpace()) + infoSpacer + formatBinaryByte(hdd.getUsableSpace()); | |
} | |
public String getHDDUsed(File hdd) { | |
return formatBinaryByte(hdd.getTotalSpace() - hdd.getFreeSpace()); | |
} | |
public String getHDDTotal(File hdd) { | |
long maxMemory = hdd.getTotalSpace(); | |
return maxMemory == Long.MAX_VALUE ? "no limit" : formatBinaryByte(maxMemory); | |
} | |
public String returnAllInformation() { | |
StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append(newLine); | |
sb.append("CPU Core count: ").append(getCPUCount()).append(newLine); | |
sb.append("CPU Load(Processload/Systemload): ").append(getProcessCPULoad()).append(infoSpacer).append(getSystemCPULoad()).append(newLine); | |
sb.append("CPU Time: ").append(getProcessCPUTime()).append(newLine); | |
sb.append("MEM Usage(used/free/max): ").append(getMemoryUsed()).append(infoSpacer).append(getMemoryFree()).append(infoSpacer).append(getMemoryMax()).append(newLine); | |
sb.append("HDDs: ").append(getHDDInfo(File.listRoots())); | |
if (sb.toString().endsWith("\t" + newLine)) { | |
return sb.toString().replace("\t" + newLine, ""); | |
} else if (sb.toString().endsWith(newLine)) { | |
return sb.toString().replace(newLine, ""); | |
} else { | |
return sb.toString(); | |
} | |
} | |
public void logAllInformation() { | |
for (String s : returnAllInformation().split(newLine)) { | |
Log.debug(s); | |
} | |
} | |
public static void setInfoSpacer(String infoSpacer) { | |
SystemInformation.infoSpacer = infoSpacer; | |
} | |
public static void setNewLine(String newLine) { | |
SystemInformation.newLine = newLine; | |
} | |
public static void main(String[] args) throws InterruptedException { | |
DOMConfigurator.configure("log4jdatalog.xml"); | |
SystemInformation si = new SystemInformation(); | |
si.logAllInformation(); | |
int maxloopTime = 60 + 1; | |
long deltaT = 0; | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < maxloopTime; i++) { | |
deltaT = System.currentTimeMillis(); | |
// for (int j = 0; j < 100; j++) { | |
// Thread.sleep(9); | |
sb.append("CPU Load(Processload/Systemload): ").append(si.getProcessCPULoad()).append(infoSpacer).append(si.getSystemCPULoad()).append(newLine); | |
sb.append("CPU Time: ").append(si.getProcessCPUTime()).append(newLine); | |
sb.append("MEM Usage(used/free/max): ").append(si.getMemoryUsed()).append(infoSpacer).append(si.getMemoryFree()).append(infoSpacer).append(si.getMemoryMax()).append(newLine); | |
for (String s : sb.toString().split(newLine)) { | |
Log.debug(s); | |
} | |
sb.delete(0, sb.length()); | |
// } | |
deltaT = Math.max(1000l - (System.currentTimeMillis() - deltaT), 0l); | |
Log.debug("sleeping for " + deltaT + " Milliseconds (" + (i + 1) + "/" + maxloopTime + ", in loop)"); | |
Thread.sleep(deltaT); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment