use of java.lang.management.MemoryPoolMXBean in project karaf by apache.
the class InfoAction method execute.
@Override
public Object execute() throws Exception {
int maxNameLen;
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
//
// print Karaf informations
//
maxNameLen = 25;
System.out.println("Karaf");
printValue("Karaf version", maxNameLen, System.getProperty("karaf.version"));
printValue("Karaf home", maxNameLen, System.getProperty("karaf.home"));
printValue("Karaf base", maxNameLen, System.getProperty("karaf.base"));
String osgi = getOsgiFramework();
if (osgi != null) {
printValue("OSGi Framework", maxNameLen, osgi);
}
System.out.println();
System.out.println("JVM");
printValue("Java Virtual Machine", maxNameLen, runtime.getVmName() + " version " + runtime.getVmVersion());
printValue("Version", maxNameLen, System.getProperty("java.version"));
printValue("Vendor", maxNameLen, runtime.getVmVendor());
printValue("Pid", maxNameLen, getPid());
printValue("Uptime", maxNameLen, printDuration(runtime.getUptime()));
try {
Class<?> sunOS = Class.forName("com.sun.management.OperatingSystemMXBean");
printValue("Process CPU time", maxNameLen, printDuration(getValueAsLong(sunOS, "getProcessCpuTime") / 1000000));
printValue("Process CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getProcessCpuLoad")));
printValue("System CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getSystemCpuLoad")));
} catch (Throwable t) {
}
try {
Class<?> unixOS = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
printValue("Open file descriptors", maxNameLen, printLong(getValueAsLong(unixOS, "getOpenFileDescriptorCount")));
printValue("Max file descriptors", maxNameLen, printLong(getValueAsLong(unixOS, "getMaxFileDescriptorCount")));
} catch (Throwable t) {
}
printValue("Total compile time", maxNameLen, printDuration(ManagementFactory.getCompilationMXBean().getTotalCompilationTime()));
System.out.println("Threads");
printValue("Live threads", maxNameLen, Integer.toString(threads.getThreadCount()));
printValue("Daemon threads", maxNameLen, Integer.toString(threads.getDaemonThreadCount()));
printValue("Peak", maxNameLen, Integer.toString(threads.getPeakThreadCount()));
printValue("Total started", maxNameLen, Long.toString(threads.getTotalStartedThreadCount()));
System.out.println("Memory");
printValue("Current heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getUsed()));
printValue("Maximum heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getMax()));
printValue("Committed heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getCommitted()));
printValue("Pending objects", maxNameLen, Integer.toString(mem.getObjectPendingFinalizationCount()));
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
String val = "Name = '" + gc.getName() + "', Collections = " + gc.getCollectionCount() + ", Time = " + printDuration(gc.getCollectionTime());
printValue("Garbage collector", maxNameLen, val);
}
if (showMemoryPools) {
List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
System.out.println("Memory Pools");
printValue("Total Memory Pools", maxNameLen, printLong(memoryPools.size()));
String spaces4 = " ";
for (MemoryPoolMXBean pool : memoryPools) {
String name = pool.getName();
MemoryType type = pool.getType();
printValue(spaces4 + "Pool (" + type + ")", maxNameLen, name);
// PeakUsage/CurrentUsage
MemoryUsage peakUsage = pool.getPeakUsage();
MemoryUsage usage = pool.getUsage();
if (usage != null && peakUsage != null) {
long init = peakUsage.getInit();
long used = peakUsage.getUsed();
long committed = peakUsage.getCommitted();
long max = peakUsage.getMax();
System.out.println(spaces4 + spaces4 + "Peak Usage");
printValue(spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
printValue(spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
printValue(spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
printValue(spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
init = usage.getInit();
used = usage.getUsed();
committed = usage.getCommitted();
max = usage.getMax();
System.out.println(spaces4 + spaces4 + "Current Usage");
printValue(spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
printValue(spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
printValue(spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
printValue(spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
}
}
}
System.out.println("Classes");
printValue("Current classes loaded", maxNameLen, printLong(cl.getLoadedClassCount()));
printValue("Total classes loaded", maxNameLen, printLong(cl.getTotalLoadedClassCount()));
printValue("Total classes unloaded", maxNameLen, printLong(cl.getUnloadedClassCount()));
System.out.println("Operating system");
printValue("Name", maxNameLen, os.getName() + " version " + os.getVersion());
printValue("Architecture", maxNameLen, os.getArch());
printValue("Processors", maxNameLen, Integer.toString(os.getAvailableProcessors()));
try {
printValue("Total physical memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getTotalPhysicalMemorySize")));
printValue("Free physical memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getFreePhysicalMemorySize")));
printValue("Committed virtual memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getCommittedVirtualMemorySize")));
printValue("Total swap space", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getTotalSwapSpaceSize")));
printValue("Free swap space", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getFreeSwapSpaceSize")));
} catch (Throwable t) {
}
// Display Information from external information providers.
Map<String, Map<Object, Object>> properties = new HashMap<>();
if (infoProviders != null) {
// dump all properties to Map, KARAF-425
for (InfoProvider provider : infoProviders) {
if (!properties.containsKey(provider.getName())) {
properties.put(provider.getName(), new Properties());
}
properties.get(provider.getName()).putAll(provider.getProperties());
}
List<String> sections = new ArrayList<>(properties.keySet());
Collections.sort(sections);
for (String section : sections) {
List<Object> keys = new ArrayList<>(properties.get(section).keySet());
if (keys.size() > 0) {
System.out.println(section);
keys.sort(Comparator.comparing(String::valueOf));
for (Object key : keys) {
printValue(String.valueOf(key), maxNameLen, String.valueOf(properties.get(section).get(key)));
}
}
}
}
return null;
}
use of java.lang.management.MemoryPoolMXBean in project mkgmap by openstreetmap.
the class Main method endOptions.
public void endOptions(CommandArgs args) {
fileOptions(args);
log.info("Start tile processors");
int threadCount = maxJobs;
int taskCount = futures.size();
Runtime runtime = Runtime.getRuntime();
if (threadPool == null) {
if (threadCount == 0) {
threadCount = 1;
if (taskCount > 2) {
// run one task to see how much memory it uses
log.info("Max Memory: " + runtime.maxMemory());
futures.get(0).run();
long maxMemory = 0;
for (MemoryPoolMXBean mxBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mxBean.getType() == MemoryType.HEAP) {
MemoryUsage memoryUsage = mxBean.getPeakUsage();
log.info("Max: " + memoryUsage.getMax());
log.info("Used: " + memoryUsage.getUsed());
if (memoryUsage.getMax() > maxMemory && memoryUsage.getUsed() != 0) {
maxMemory = memoryUsage.getMax();
threadCount = (int) (memoryUsage.getMax() / memoryUsage.getUsed());
}
}
}
threadCount = Math.max(threadCount, 1);
threadCount = Math.min(threadCount, runtime.availableProcessors());
System.out.println("Setting max-jobs to " + threadCount);
}
}
log.info("Creating thread pool with " + threadCount + " threads");
threadPool = Executors.newFixedThreadPool(threadCount);
}
// process all input files
for (FilenameTask task : futures) {
threadPool.execute(task);
}
List<FilenameTask> filenames = new ArrayList<>();
int numMapFailedExceptions = 0;
if (threadPool != null) {
threadPool.shutdown();
while (!futures.isEmpty()) {
try {
try {
// don't call get() until a job has finished
if (futures.get(0).isDone()) {
FilenameTask future = futures.remove(0);
// Provoke any exceptions by calling get and then
// save the result for later use
future.setFilename(future.get());
filenames.add(future);
} else
Thread.sleep(100);
} catch (ExecutionException e) {
// Re throw the underlying exception
Throwable cause = e.getCause();
if (cause instanceof Exception)
// noinspection ProhibitedExceptionThrown
throw (Exception) cause;
else if (cause instanceof Error)
// noinspection ProhibitedExceptionThrown
throw (Error) cause;
else
throw e;
}
} catch (OutOfMemoryError | ExitException e) {
throw e;
} catch (MapFailedException mfe) {
// System.err.println(mfe.getMessage()); // already printed via log
numMapFailedExceptions++;
setProgramRC(-1);
} catch (Throwable t) {
t.printStackTrace();
if (!args.getProperties().getProperty("keep-going", false)) {
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
}
}
}
System.out.println("Number of MapFailedExceptions: " + numMapFailedExceptions);
if ((taskCount > threadCount + 1) && (maxJobs == 0) && (threadCount < runtime.availableProcessors())) {
System.out.println("To reduce the run time, consider increasing the amnount of memory available for use by mkgmap by using the Java -Xmx flag to set the memory to more than " + 100 * (1 + ((runtime.maxMemory() * runtime.availableProcessors()) / (threadCount * 1024 * 1024 * 100))) + " MB, providing this is less than the amount of physical memory installed.");
}
if (combiners.isEmpty())
return;
boolean hasFiles = false;
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled() || file.getFilename() == null) {
if (args.getProperties().getProperty("keep-going", false))
continue;
else
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
hasFiles = true;
}
if (!hasFiles) {
log.warn("nothing to do for combiners.");
return;
}
log.info("Combining maps");
args.setSort(getSort(args));
// Get them all set up.
for (Combiner c : combiners) c.init(args);
filenames.sort(new Comparator<FilenameTask>() {
public int compare(FilenameTask o1, FilenameTask o2) {
if (!o1.getFilename().endsWith(".img") || !o2.getFilename().endsWith(".img"))
return o1.getFilename().compareTo(o2.getFilename());
// Both end in .img
try {
int id1 = FileInfo.getFileInfo(o1.getFilename()).getHexname();
int id2 = FileInfo.getFileInfo(o2.getFilename()).getHexname();
if (id1 == id2)
return 0;
else if (id1 < id2)
return -1;
else
return 1;
} catch (FileNotFoundException ignored) {
}
return 0;
}
});
// will contain img files for which an additional ovm file was found
HashSet<String> foundOvmFiles = new HashSet<>();
// try OverviewBuilder with special files
if (tdbBuilderAdded) {
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
String fileName = file.getFilename();
if (!fileName.endsWith(".img"))
continue;
fileName = OverviewBuilder.getOverviewImgName(fileName);
log.info(" " + fileName);
FileInfo fileInfo = FileInfo.getFileInfo(fileName);
fileInfo.setArgs(file.getArgs());
// add the real input file
foundOvmFiles.add(file.getFilename());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder)
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException ignored) {
}
}
}
// Tell them about each filename (OverviewBuilder excluded)
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
log.info(" " + file);
FileInfo fileInfo = FileInfo.getFileInfo(file.getFilename());
fileInfo.setArgs(file.getArgs());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder && foundOvmFiles.contains(file.getFilename()))
continue;
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException e) {
throw new MapFailedException("could not open file " + e.getMessage());
}
}
// All done, allow tidy up or file creation to happen
for (Combiner c : combiners) c.onFinish();
if (tdbBuilderAdded && args.getProperties().getProperty("remove-ovm-work-files", false)) {
for (String fName : foundOvmFiles) {
String ovmFile = OverviewBuilder.getOverviewImgName(fName);
log.info("removing " + ovmFile);
new File(ovmFile).delete();
}
}
}
use of java.lang.management.MemoryPoolMXBean in project jvm-profiler by uber-common.
the class CpuAndMemoryProfiler method profile.
@Override
public synchronized void profile() {
Double processCpuLoad = null;
Double systemCpuLoad = null;
Long processCpuTime = null;
AttributeList cpuAttributes = getCpuAttributes();
if (cpuAttributes != null && cpuAttributes.size() > 0) {
Attribute att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_ProcessCpuLoad);
processCpuLoad = (Double) att.getValue();
if (processCpuLoad == Double.NaN) {
processCpuLoad = null;
}
att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_SystemCpuLoad);
systemCpuLoad = (Double) att.getValue();
if (systemCpuLoad == Double.NaN) {
systemCpuLoad = null;
}
att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_ProcessCpuTime);
processCpuTime = (Long) att.getValue();
}
Double heapMemoryTotalUsed = null;
Double heapMemoryCommitted = null;
Double nonHeapMemoryTotalUsed = null;
Double nonHeapMemoryCommitted = null;
if (memoryMXBean != null) {
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
heapMemoryTotalUsed = new Double(memoryUsage.getUsed());
heapMemoryCommitted = new Double(memoryUsage.getCommitted());
memoryUsage = memoryMXBean.getNonHeapMemoryUsage();
nonHeapMemoryTotalUsed = new Double(memoryUsage.getUsed());
nonHeapMemoryCommitted = new Double(memoryUsage.getCommitted());
}
List<Map<String, Object>> gcMetrics = new ArrayList<>();
List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
if (gcMXBeans != null) {
for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
Map<String, Object> gcMap = new HashMap<>();
gcMap.put("name", gcMXBean.getName());
gcMap.put("collectionCount", new Long(gcMXBean.getCollectionCount()));
gcMap.put("collectionTime", new Long(gcMXBean.getCollectionTime()));
gcMetrics.add(gcMap);
}
}
List<Map<String, Object>> memoryPoolsMetrics = new ArrayList<>();
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
Map<String, Object> memoryPoolMap = new HashMap<>();
memoryPoolMap.put("name", pool.getName());
memoryPoolMap.put("type", pool.getType().toString());
memoryPoolMap.put("usageCommitted", pool.getUsage().getCommitted());
memoryPoolMap.put("usageMax", pool.getUsage().getMax());
memoryPoolMap.put("usageUsed", pool.getUsage().getUsed());
memoryPoolMap.put("peakUsageCommitted", pool.getPeakUsage().getCommitted());
memoryPoolMap.put("peakUsageMax", pool.getPeakUsage().getMax());
memoryPoolMap.put("peakUsageUsed", pool.getPeakUsage().getUsed());
memoryPoolsMetrics.add(memoryPoolMap);
}
List<Map<String, Object>> bufferPoolsMetrics = new ArrayList<>();
List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
if (bufferPools != null) {
for (BufferPoolMXBean pool : bufferPools) {
Map<String, Object> bufferPoolMap = new HashMap<>();
bufferPoolMap.put("name", pool.getName());
bufferPoolMap.put("count", new Long(pool.getCount()));
bufferPoolMap.put("memoryUsed", new Long(pool.getMemoryUsed()));
bufferPoolMap.put("totalCapacity", new Long(pool.getTotalCapacity()));
bufferPoolsMetrics.add(bufferPoolMap);
}
}
// See http://man7.org/linux/man-pages/man5/proc.5.html for details about proc status
Map<String, String> procStatus = ProcFileUtils.getProcStatus();
Long procStatusVmRSS = ProcFileUtils.getBytesValue(procStatus, "VmRSS");
Long procStatusVmHWM = ProcFileUtils.getBytesValue(procStatus, "VmHWM");
Map<String, Object> map = new HashMap<String, Object>();
map.put("epochMillis", System.currentTimeMillis());
map.put("name", getProcessName());
map.put("host", getHostName());
map.put("processUuid", getProcessUuid());
map.put("appId", getAppId());
if (getTag() != null) {
map.put("tag", getTag());
}
if (getCluster() != null) {
map.put("cluster", getCluster());
}
if (getRole() != null) {
map.put("role", getRole());
}
map.put("processCpuLoad", processCpuLoad);
map.put("systemCpuLoad", systemCpuLoad);
map.put("processCpuTime", processCpuTime);
map.put("heapMemoryTotalUsed", heapMemoryTotalUsed);
map.put("heapMemoryCommitted", heapMemoryCommitted);
map.put("nonHeapMemoryTotalUsed", nonHeapMemoryTotalUsed);
map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted);
map.put("gc", gcMetrics);
map.put("memoryPools", memoryPoolsMetrics);
map.put("bufferPools", bufferPoolsMetrics);
if (procStatusVmRSS != null) {
map.put("vmRSS", procStatusVmRSS);
}
if (procStatusVmHWM != null) {
map.put("vmHWM", procStatusVmHWM);
}
if (reporter != null) {
reporter.report(PROFILER_NAME, map);
}
}
use of java.lang.management.MemoryPoolMXBean in project jackrabbit-oak by apache.
the class TraverseWithSortStrategy method getMemoryPool.
private static MemoryPoolMXBean getMemoryPool() {
long maxSize = 0;
MemoryPoolMXBean maxPool = null;
for (MemoryPoolMXBean pool : getMemoryPoolMXBeans()) {
if (HEAP == pool.getType() && pool.isCollectionUsageThresholdSupported()) {
// Get usage after a GC, which is more stable, if available
long poolSize = pool.getCollectionUsage().getMax();
// Keep the pool with biggest size, by default it should be Old Gen Space
if (poolSize > maxSize) {
maxPool = pool;
}
}
}
return maxPool;
}
use of java.lang.management.MemoryPoolMXBean in project jackrabbit-oak by apache.
the class TraverseWithSortStrategy method configureMemoryListener.
// ~-------------------------------------< memory management >
private void configureMemoryListener() {
MemoryPoolMXBean pool = getMemoryPool();
if (pool == null) {
log.warn("Unable to setup monitoring of available memory. " + "Would use configured maxMemory limit of {} GB", maxMemory);
useMaxMemory = true;
return;
}
emitter = (NotificationEmitter) getMemoryMXBean();
listener = new MemoryListener();
emitter.addNotificationListener(listener, null, null);
MemoryUsage usage = pool.getCollectionUsage();
long maxMemory = usage.getMax();
long warningThreshold = minMemory * ONE_GB;
if (warningThreshold > maxMemory) {
log.warn("Configured minimum memory {} GB more than available memory ({})." + "Overriding configuration accordingly.", minMemory, humanReadableByteCount(maxMemory));
warningThreshold = maxMemory;
}
log.info("Setting up a listener to monitor pool '{}' and trigger batch save " + "if memory drop below {} GB (max {})", pool.getName(), minMemory, humanReadableByteCount(maxMemory));
pool.setCollectionUsageThreshold(warningThreshold);
checkMemory(usage);
}
Aggregations