Search in sources :

Example 66 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project geode by apache.

the class LocalProcessController method checkPidMatches.

/**
   * Ensures that the other process identifies itself by the same pid used by this stopper to
   * connect to that process. NOT USED EXCEPT IN TEST.
   * 
   * @return true if the pid matches
   * 
   * @throws IllegalStateException if the other process identifies itself by a different pid
   * @throws IOException if a communication problem occurred when accessing the
   *         MBeanServerConnection
   * @throws PidUnavailableException if parsing the pid from the RuntimeMXBean name fails
   */
boolean checkPidMatches() throws IllegalStateException, IOException, PidUnavailableException {
    final RuntimeMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(this.server, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    final int remotePid = ProcessUtils.identifyPid(proxy.getName());
    if (remotePid != this.pid) {
        throw new IllegalStateException("Process has different pid '" + remotePid + "' than expected pid '" + this.pid + "'");
    } else {
        return true;
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 67 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project jdk8u_jdk by JetBrains.

the class GetSystemProperties method runTest.

private static void runTest() throws Exception {
    RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean();
    // Print all system properties
    Map<String, String> props = mbean.getSystemProperties();
    printProperties(props);
    // Add new system properties
    System.setProperty(KEY1, VALUE1);
    System.setProperty(KEY2, VALUE2);
    Map<String, String> props1 = mbean.getSystemProperties();
    String value1 = props1.get(KEY1);
    if (value1 == null || !value1.equals(VALUE1)) {
        throw new RuntimeException(KEY1 + " property found" + " with value = " + value1 + " but expected to be " + VALUE1);
    }
    String value2 = props1.get(KEY2);
    if (value2 == null || !value2.equals(VALUE2)) {
        throw new RuntimeException(KEY2 + " property found" + " with value = " + value2 + " but expected to be " + VALUE2);
    }
    String value3 = props1.get(KEY3);
    if (value3 != null) {
        throw new RuntimeException(KEY3 + " property found" + " but should not exist");
    }
    // Add new system properties but are not Strings
    Properties sp = System.getProperties();
    sp.put(KEY3, VALUE3);
    sp.put(KEY4, VALUE4);
    Map<String, String> props2 = mbean.getSystemProperties();
    // same as the one before adding KEY3 and KEY4
    if (!props1.equals(props2)) {
        throw new RuntimeException("Two copies of system properties " + "are expected to be equal");
    }
    System.out.println("Test passed.");
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 68 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project symmetric-ds by JumpMind.

the class SnapshotUtil method writeRuntimeStats.

protected static void writeRuntimeStats(ISymmetricEngine engine, File tmpDir) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(tmpDir, "runtime-stats.properties"));
        Properties runtimeProperties = new Properties() {

            private static final long serialVersionUID = 1L;

            public synchronized Enumeration<Object> keys() {
                return Collections.enumeration(new TreeSet<Object>(super.keySet()));
            }
        };
        DataSource dataSource = engine.getDatabasePlatform().getDataSource();
        if (dataSource instanceof BasicDataSource) {
            BasicDataSource dbcp = (BasicDataSource) dataSource;
            runtimeProperties.setProperty("connections.idle", String.valueOf(dbcp.getNumIdle()));
            runtimeProperties.setProperty("connections.used", String.valueOf(dbcp.getNumActive()));
            runtimeProperties.setProperty("connections.max", String.valueOf(dbcp.getMaxActive()));
        }
        Runtime rt = Runtime.getRuntime();
        runtimeProperties.setProperty("memory.free", String.valueOf(rt.freeMemory()));
        runtimeProperties.setProperty("memory.used", String.valueOf(rt.totalMemory() - rt.freeMemory()));
        runtimeProperties.setProperty("memory.max", String.valueOf(rt.maxMemory()));
        List<MemoryPoolMXBean> memoryPools = new ArrayList<MemoryPoolMXBean>(ManagementFactory.getMemoryPoolMXBeans());
        long usedHeapMemory = 0;
        for (MemoryPoolMXBean memoryPool : memoryPools) {
            if (memoryPool.getType().equals(MemoryType.HEAP)) {
                MemoryUsage memoryUsage = memoryPool.getCollectionUsage();
                runtimeProperties.setProperty("memory.heap." + memoryPool.getName().toLowerCase().replaceAll(" ", "."), Long.toString(memoryUsage.getUsed()));
                usedHeapMemory += memoryUsage.getUsed();
            }
        }
        runtimeProperties.setProperty("memory.heap.total", Long.toString(usedHeapMemory));
        OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        runtimeProperties.setProperty("os.name", System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ")");
        runtimeProperties.setProperty("os.processors", String.valueOf(osBean.getAvailableProcessors()));
        runtimeProperties.setProperty("os.load.average", String.valueOf(osBean.getSystemLoadAverage()));
        runtimeProperties.setProperty("engine.is.started", Boolean.toString(engine.isStarted()));
        runtimeProperties.setProperty("engine.last.restart", engine.getLastRestartTime().toString());
        runtimeProperties.setProperty("time.server", new Date().toString());
        runtimeProperties.setProperty("time.database", new Date(engine.getSymmetricDialect().getDatabaseTime()).toString());
        runtimeProperties.setProperty("batch.unrouted.data.count", Long.toString(engine.getRouterService().getUnroutedDataCount()));
        runtimeProperties.setProperty("batch.outgoing.errors.count", Long.toString(engine.getOutgoingBatchService().countOutgoingBatchesInError()));
        runtimeProperties.setProperty("batch.outgoing.tosend.count", Long.toString(engine.getOutgoingBatchService().countOutgoingBatchesUnsent()));
        runtimeProperties.setProperty("batch.incoming.errors.count", Long.toString(engine.getIncomingBatchService().countIncomingBatchesInError()));
        List<DataGap> gaps = engine.getDataService().findDataGapsByStatus(DataGap.Status.GP);
        runtimeProperties.setProperty("data.gap.count", Long.toString(gaps.size()));
        if (gaps.size() > 0) {
            runtimeProperties.setProperty("data.gap.start.id", Long.toString(gaps.get(0).getStartId()));
            runtimeProperties.setProperty("data.gap.end.id", Long.toString(gaps.get(gaps.size() - 1).getEndId()));
        }
        runtimeProperties.setProperty("data.id.min", Long.toString(engine.getDataService().findMinDataId()));
        runtimeProperties.setProperty("data.id.max", Long.toString(engine.getDataService().findMaxDataId()));
        runtimeProperties.put("jvm.title", Runtime.class.getPackage().getImplementationTitle());
        runtimeProperties.put("jvm.vendor", Runtime.class.getPackage().getImplementationVendor());
        runtimeProperties.put("jvm.version", Runtime.class.getPackage().getImplementationVersion());
        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
        List<String> arguments = runtimeMxBean.getInputArguments();
        runtimeProperties.setProperty("jvm.arguments", arguments.toString());
        runtimeProperties.store(fos, "runtime-stats.properties");
    } catch (Exception e) {
        log.warn("Failed to export runtime-stats information", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}
Also used : ArrayList(java.util.ArrayList) RuntimeMXBean(java.lang.management.RuntimeMXBean) Properties(java.util.Properties) MemoryUsage(java.lang.management.MemoryUsage) Date(java.util.Date) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DataGap(org.jumpmind.symmetric.model.DataGap) FileOutputStream(java.io.FileOutputStream) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) File(java.io.File) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 69 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project karaf by apache.

the class InstanceHelper method writePid.

private static void writePid(String pidFile) {
    try {
        if (pidFile != null) {
            RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();
            String processName = rtb.getName();
            Pattern pattern = Pattern.compile("^([0-9]+)@.+$", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(processName);
            if (matcher.matches()) {
                int pid = Integer.parseInt(matcher.group(1));
                Writer w = new OutputStreamWriter(new FileOutputStream(pidFile));
                w.write(Integer.toString(pid));
                w.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RuntimeMXBean(java.lang.management.RuntimeMXBean) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 70 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project asterixdb by apache.

the class GetNodeDetailsJSONWork method getCCDetails.

private ObjectNode getCCDetails() {
    ObjectNode o = om.createObjectNode();
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    if (includeConfig) {
        o.put("os_name", osMXBean.getName());
        o.put("arch", osMXBean.getArch());
        o.put("os_version", osMXBean.getVersion());
        o.put("num_processors", osMXBean.getAvailableProcessors());
        o.put("vm_name", runtimeMXBean.getVmName());
        o.put("vm_version", runtimeMXBean.getVmVersion());
        o.put("vm_vendor", runtimeMXBean.getVmVendor());
        o.putPOJO("classpath", runtimeMXBean.getClassPath().split(File.pathSeparator));
        o.putPOJO("library_path", runtimeMXBean.getLibraryPath().split(File.pathSeparator));
        o.putPOJO("boot_classpath", runtimeMXBean.getBootClassPath().split(File.pathSeparator));
        o.putPOJO("input_arguments", runtimeMXBean.getInputArguments());
        o.putPOJO("system_properties", runtimeMXBean.getSystemProperties());
        o.put("pid", PidHelper.getPid());
    }
    if (includeStats) {
        MemoryUsage heapUsage = memoryMXBean.getHeapMemoryUsage();
        MemoryUsage nonheapUsage = memoryMXBean.getNonHeapMemoryUsage();
        List<ObjectNode> gcs = new ArrayList<>();
        for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
            ObjectNode gc = om.createObjectNode();
            gc.put("name", gcMXBean.getName());
            gc.put("collection-time", gcMXBean.getCollectionTime());
            gc.put("collection-count", gcMXBean.getCollectionCount());
            gcs.add(gc);
        }
        o.putPOJO("gcs", gcs);
        o.put("date", new Date().toString());
        o.put("heap_init_size", heapUsage.getInit());
        o.put("heap_used_size", heapUsage.getUsed());
        o.put("heap_committed_size", heapUsage.getCommitted());
        o.put("heap_max_size", heapUsage.getMax());
        o.put("nonheap_init_size", nonheapUsage.getInit());
        o.put("nonheap_used_size", nonheapUsage.getUsed());
        o.put("nonheap_committed_size", nonheapUsage.getCommitted());
        o.put("nonheap_max_size", nonheapUsage.getMax());
        o.put("thread_count", threadMXBean.getThreadCount());
        o.put("peak_thread_count", threadMXBean.getPeakThreadCount());
        o.put("started_thread_count", threadMXBean.getTotalStartedThreadCount());
        o.put("system_load_average", osMXBean.getSystemLoadAverage());
    }
    return o;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) ArrayList(java.util.ArrayList) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Date(java.util.Date)

Aggregations

RuntimeMXBean (java.lang.management.RuntimeMXBean)92 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)16 MemoryMXBean (java.lang.management.MemoryMXBean)11 IOException (java.io.IOException)9 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)9 ThreadMXBean (java.lang.management.ThreadMXBean)8 MemoryUsage (java.lang.management.MemoryUsage)6 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 InputStream (java.io.InputStream)4 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)4 Field (java.lang.reflect.Field)4 Date (java.util.Date)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3