Search in sources :

Example 26 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project Lucee by lucee.

the class InstrumentationFactory method loadAgent.

/**
 * Attach and load an agent class.
 *
 * @param log Log used if the agent cannot be loaded.
 * @param agentJar absolute path to the agent jar.
 * @param vmClass VirtualMachine.class from tools.jar.
 */
private static void loadAgent(Config config, Log log, String agentJar, Class<?> vmClass) {
    try {
        // addAttach(config,log);
        // first obtain the PID of the currently-running process
        // ### this relies on the undocumented convention of the
        // RuntimeMXBean's
        // ### name starting with the PID, but there appears to be no other
        // ### way to obtain the current process' id, which we need for
        // ### the attach process
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String pid = runtime.getName();
        if (pid.indexOf("@") != -1)
            pid = pid.substring(0, pid.indexOf("@"));
        log.info("Instrumentation", "pid:" + pid);
        // JDK1.6: now attach to the current VM so we can deploy a new agent
        // ### this is a Sun JVM specific feature; other JVMs may offer
        // ### this feature, but in an implementation-dependent way
        Object vm = vmClass.getMethod("attach", new Class<?>[] { String.class }).invoke(null, new Object[] { pid });
        // now deploy the actual agent, which will wind up calling
        // agentmain()
        vmClass.getMethod("loadAgent", new Class[] { String.class }).invoke(vm, new Object[] { agentJar });
        vmClass.getMethod("detach", new Class[] {}).invoke(vm, new Object[] {});
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        // Log the message from the exception. Don't log the entire
        // stack as this is expected when running on a JDK that doesn't
        // support the Attach API.
        log.log(Log.LEVEL_INFO, "Instrumentation", t);
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 27 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project traccar by tananaev.

the class Log method logSystemInfo.

public static void logSystemInfo() {
    try {
        OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
        Log.info("Operating system" + " name: " + operatingSystemBean.getName() + " version: " + operatingSystemBean.getVersion() + " architecture: " + operatingSystemBean.getArch());
        RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
        Log.info("Java runtime" + " name: " + runtimeBean.getVmName() + " vendor: " + runtimeBean.getVmVendor() + " version: " + runtimeBean.getVmVersion());
        MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
        Log.info("Memory limit" + " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb" + " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb");
        Log.info("Character encoding: " + System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset());
    } catch (Exception error) {
        Log.warning("Failed to get system info");
    }
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) IOException(java.io.IOException)

Example 28 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project Payara by payara.

the class RuntimeInfo method execute.

@Override
public void execute(AdminCommandContext context) {
    report = context.getActionReport();
    report.setActionExitCode(SUCCESS);
    top = report.getTopMessagePart();
    logger = context.getLogger();
    boolean javaEnabledOnCmd = Boolean.parseBoolean(ctx.getArguments().getProperty("-debug"));
    javaConfig = config.getJavaConfig();
    jpdaEnabled = javaEnabledOnCmd || Boolean.parseBoolean(javaConfig.getDebugEnabled());
    int debugPort = parsePort(javaConfig.getDebugOptions());
    top.addProperty("debug", Boolean.toString(jpdaEnabled));
    top.addProperty("debugPort", Integer.toString(debugPort));
    final OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    top.addProperty("os.arch", osBean.getArch());
    top.addProperty("os.name", osBean.getName());
    top.addProperty("os.version", osBean.getVersion());
    top.addProperty("availableProcessorsCount", "" + osBean.getAvailableProcessors());
    // also if we are not on a sun jdk, we will not return this attribute.
    if (!OS.isAix()) {
        try {
            final Method jm = osBean.getClass().getMethod("getTotalPhysicalMemorySize");
            AccessController.doPrivileged(new PrivilegedExceptionAction() {

                public Object run() throws Exception {
                    if (!jm.isAccessible()) {
                        jm.setAccessible(true);
                    }
                    return null;
                }
            });
            top.addProperty("totalPhysicalMemorySize", "" + jm.invoke(osBean));
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }
    RuntimeMXBean rmxb = ManagementFactory.getRuntimeMXBean();
    top.addProperty("startTimeMillis", "" + rmxb.getStartTime());
    top.addProperty("pid", "" + rmxb.getName());
    checkDtrace();
    setDasName();
    top.addProperty("java.vm.name", System.getProperty("java.vm.name"));
    // setRestartable();
    reportMessage.append(Strings.get("runtime.info.debug", jpdaEnabled ? "enabled" : "not enabled"));
    report.setMessage(reportMessage.toString());
}
Also used : Method(java.lang.reflect.Method) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 29 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project Payara by payara.

the class SummaryReporter method getSummaryReport.

public String getSummaryReport() throws RuntimeException {
    try {
        final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder());
        final OperatingSystemMXBean os = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
        sb.append(getOSInfo(os));
        final RuntimeMXBean rt = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
        sb.append(getArguments(rt));
        sb.append(getVMInfo(rt));
        return (sb.toString(secretProperty));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 30 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean 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;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) HashMap(java.util.HashMap) InfoProvider(org.apache.karaf.shell.commands.info.InfoProvider) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) ArrayList(java.util.ArrayList) RuntimeMXBean(java.lang.management.RuntimeMXBean) Properties(java.util.Properties) MemoryUsage(java.lang.management.MemoryUsage) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) HashMap(java.util.HashMap) Map(java.util.Map) MemoryType(java.lang.management.MemoryType)

Aggregations

RuntimeMXBean (java.lang.management.RuntimeMXBean)164 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)27 IOException (java.io.IOException)17 MemoryMXBean (java.lang.management.MemoryMXBean)16 ThreadMXBean (java.lang.management.ThreadMXBean)13 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 MemoryUsage (java.lang.management.MemoryUsage)8 Method (java.lang.reflect.Method)8 Map (java.util.Map)8 Properties (java.util.Properties)6 InputStream (java.io.InputStream)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 Iterator (java.util.Iterator)5 File (java.io.File)4 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)4 Field (java.lang.reflect.Field)4 UnknownHostException (java.net.UnknownHostException)4