Search in sources :

Example 21 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project Gargoyle by callakrsos.

the class CPUUsageTest method showCPU.

/*
	 * cpu 사용량
	 */
public static void showCPU() {
    OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runbean = ManagementFactory.getRuntimeMXBean();
    long bfprocesstime = osbean.getProcessCpuTime();
    long bfuptime = runbean.getUptime();
    long ncpus = osbean.getAvailableProcessors();
    //		for (int i = 0; i < 1000000; ++i) {
    //			ncpus = osbean.getAvailableProcessors();
    //		}
    long afprocesstime = osbean.getProcessCpuTime();
    long afuptime = runbean.getUptime();
    float cal = (afprocesstime - bfprocesstime) / ((afuptime - bfuptime) * 10000f);
    float usage = Math.min(99f, cal);
    System.out.println("Calculation: " + cal);
    System.out.println("CPU Usage: " + usage);
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(com.sun.management.OperatingSystemMXBean)

Example 22 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project searchcode-server by boyter.

the class StatsService method getUptime.

/**
     * Returns how long the application has been up in seconds, minutes or hours using larger
     * time units where appropriate
     * TODO add in larger time units such as days
     * TODO change to display something such as 3 days 4 hours 45 minutes 23 seconds
     */
public String getUptime() {
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    int upTime = (int) (runtimeBean.getUptime() / 1000);
    int minutes = upTime / 60;
    int hours = minutes / 60;
    if (upTime < 120) {
        return upTime + " seconds";
    }
    if (minutes < 120) {
        return minutes + " minutes";
    }
    return hours + " hours";
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 23 with RuntimeMXBean

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

the class MBeanProcessController 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 24 with RuntimeMXBean

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

the class Banner method print.

/**
   * Print information about this process to the specified stream.
   *
   * @param args possibly null list of command line arguments
   */
static void print(PrintWriter out, String[] args) {
    // fix for 46822
    Map sp = new TreeMap((Properties) System.getProperties().clone());
    int processId = -1;
    final String SEPERATOR = "---------------------------------------------------------------------------";
    try {
        processId = OSProcess.getId();
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
    }
    out.println();
    final String productName = GemFireVersion.getProductName();
    out.println(SEPERATOR);
    out.println("  ");
    out.println("  Licensed to the Apache Software Foundation (ASF) under one or more");
    out.println("  contributor license agreements.  See the NOTICE file distributed with this");
    out.println("  work for additional information regarding copyright ownership.");
    out.println("   ");
    out.println("  The ASF licenses this file to You under the Apache License, Version 2.0");
    out.println("  (the \"License\"); you may not use this file except in compliance with the");
    out.println("  License.  You may obtain a copy of the License at");
    out.println("  ");
    out.println("  http://www.apache.org/licenses/LICENSE-2.0");
    out.println("  ");
    out.println("  Unless required by applicable law or agreed to in writing, software");
    out.println("  distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT");
    out.println("  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the");
    out.println("  License for the specific language governing permissions and limitations");
    out.println("  under the License.");
    out.println("  ");
    out.println(SEPERATOR);
    GemFireVersion.print(out);
    out.println("Communications version: " + Version.CURRENT_ORDINAL);
    out.println("Process ID: " + processId);
    out.println("User: " + sp.get("user.name"));
    sp.remove("user.name");
    sp.remove("os.name");
    sp.remove("os.arch");
    out.println("Current dir: " + sp.get("user.dir"));
    sp.remove("user.dir");
    out.println("Home dir: " + sp.get("user.home"));
    sp.remove("user.home");
    List<String> allArgs = new ArrayList<>();
    {
        RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
        if (runtimeBean != null) {
            // fixes 45353
            allArgs.addAll(runtimeBean.getInputArguments());
        }
    }
    if (args != null && args.length != 0) {
        Collections.addAll(allArgs, args);
    }
    if (!allArgs.isEmpty()) {
        out.println("Command Line Parameters:");
        for (String arg : allArgs) {
            out.println("  " + ArgumentRedactor.redact(arg));
        }
    }
    out.println("Class Path:");
    prettyPrintPath((String) sp.get("java.class.path"), out);
    sp.remove("java.class.path");
    out.println("Library Path:");
    prettyPrintPath((String) sp.get("java.library.path"), out);
    sp.remove("java.library.path");
    if (Boolean.getBoolean(DistributionConfig.GEMFIRE_PREFIX + "disableSystemPropertyLogging")) {
        out.println("System property logging disabled.");
    } else {
        out.println("System Properties:");
        Iterator it = sp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            String key = me.getKey().toString();
            out.println("    " + key + " = " + ArgumentRedactor.redact(String.valueOf(me.getValue())));
        }
        out.println("Log4J 2 Configuration:");
        out.println("    " + LogService.getConfigInformation());
    }
    out.println(SEPERATOR);
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) RuntimeMXBean(java.lang.management.RuntimeMXBean) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 25 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project warn-report by saaavsaaa.

the class TestAgentVMAttacher method test.

// consult: http://ayufox.iteye.com/blog/653214
/*
    * HotSpotDiagnosticMXBean
    * ClassLoadingMXBean
    * CompilationMXBean
    * GarbageCollectorMXBean
    * MemoryManagerMXBean
    * MemoryPoolMXBean
    * OperatingSystemMXBean
    * RuntimeMXBean
    * ThreadMXBeanisolatering
    * */
@Test
public void test() throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
    // args[0]传入的是某个jvm进程的pid
    String targetPid = "9527";
    VirtualMachine virtualmachine = VirtualMachine.attach(targetPid);
    // 让JVM加载jmx Agent
    String javaHome = virtualmachine.getSystemProperties().getProperty("java.home");
    String jmxAgent = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
    virtualmachine.loadAgent(jmxAgent, "com.sun.management.jmxremote");
    // 获得连接地址
    Properties properties = virtualmachine.getAgentProperties();
    String address = (String) properties.get("com.sun.management.jmxremote.localConnectorAddress");
    // Detach
    virtualmachine.detach();
    // 通过jxm address来获取RuntimeMXBean对象,从而得到虚拟机运行时相关信息
    JMXServiceURL url = new JMXServiceURL(address);
    JMXConnector connector = JMXConnectorFactory.connect(url);
    RuntimeMXBean rmxb = ManagementFactory.newPlatformMXBeanProxy(connector.getMBeanServerConnection(), "java.lang:type=Runtime", RuntimeMXBean.class);
    // 得到目标虚拟机占用cpu时间
    System.out.println(rmxb.getInputArguments());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) RuntimeMXBean(java.lang.management.RuntimeMXBean) Properties(java.util.Properties) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.junit.Test)

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