Search in sources :

Example 41 with RuntimeMXBean

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

the class UtilAll method getPid.

public static int getPid() {
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    // format: "pid@hostname"
    String name = runtime.getName();
    try {
        return Integer.parseInt(name.substring(0, name.indexOf('@')));
    } catch (Exception e) {
        return -1;
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 42 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project BroadleafCommerce by BroadleafCommerce.

the class InstrumentationRuntimeFactory method loadAgent.

private static void loadAgent(String agentJar, Class<?> vmClass) {
    try {
        // 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.contains("@"))
            pid = pid.substring(0, pid.indexOf("@"));
        // 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, pid);
        vmClass.getMethod("loadAgent", new Class[] { String.class }).invoke(vm, agentJar);
        vmClass.getMethod("detach", new Class[] {}).invoke(vm);
    } catch (Throwable t) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Problem loading the agent", t);
        }
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 43 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project openolat by klemens.

the class ThreadInfosManager method updateTimeSeries.

private void updateTimeSeries() {
    ThreadMXBean threadProxy = ManagementFactory.getThreadMXBean();
    RuntimeMXBean runtimeProxy = ManagementFactory.getRuntimeMXBean();
    ThreadInfo[] tis = threadProxy.dumpAllThreads(false, false);
    List<String> currentThreadNames = new ArrayList<String>();
    Set<Long> currentThreadIds = new HashSet<Long>();
    for (ThreadInfo ti : tis) {
        Long threadId = new Long(ti.getThreadId());
        if (threadMap.containsKey(threadId)) {
            ThreadView threadVO = threadMap.get(threadId);
            threadVO.setState(ti.getThreadState());
        } else {
            ThreadView threadVO = new ThreadView();
            threadVO.setId(threadId);
            threadVO.setName(ti.getThreadName());
            threadVO.setState(ti.getThreadState());
            threadMap.put(threadId, threadVO);
        }
        currentThreadIds.add(threadId);
    }
    WorkThreadInformations.currentThreadNames(currentThreadNames);
    for (ThreadView threadVO : threadMap.values()) {
        threadVO.setPrevCpuTime(Math.max(0, threadVO.getCpuTime()));
        threadVO.setCpuTime(Math.max(0, threadProxy.getThreadCpuTime(threadVO.getId())));
    }
    long upTime = runtimeProxy.getUptime();
    if (prevUpTime > 0L && upTime > prevUpTime) {
        // elapsedTime is in ms
        long elapsedTime = upTime - prevUpTime;
        for (ThreadView threadVO : threadMap.values()) {
            // elapsedCpu is in ns
            long elapsedCpu = threadVO.getCpuTime() - threadVO.getPrevCpuTime();
            // cpuUsage could go higher than 100% because elapsedTime
            // and elapsedCpu are not fetched simultaneously. Limit to
            // 99% to avoid Chart showing a scale from 0% to 200%.
            float cpuUsage = Math.min(99f, elapsedCpu / (elapsedTime * 1000000F));
            threadVO.setCpuUsage(cpuUsage);
            threadVO.setCpuUsagePercent(percentFormat.format(cpuUsage));
            if (cpuUsage > 0.8) {
                threadVO.setWarningCounter(threadVO.getWarningCounter() + 1);
                if (threadVO.getWarningCounter() >= 2) {
                    String currentWork = WorkThreadInformations.get(threadVO.getName());
                    if (currentWork == null) {
                        currentWork = "unkown";
                    }
                    log.info("High usage on thread:" + threadVO + " because thread work at: " + currentWork);
                }
            } else {
                threadVO.setWarningCounter(0);
            }
        }
    }
    prevUpTime = upTime;
    // clean-up closed threads
    for (Iterator<Map.Entry<Long, ThreadView>> it = threadMap.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<Long, ThreadView> entry = it.next();
        if (!currentThreadIds.contains(entry.getKey())) {
            it.remove();
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ArrayList(java.util.ArrayList) RuntimeMXBean(java.lang.management.RuntimeMXBean) ThreadInfo(java.lang.management.ThreadInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ThreadView(org.olat.admin.sysinfo.model.ThreadView) HashSet(java.util.HashSet)

Example 44 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project xian by happyyangyuan.

the class JavaPIDUtil method initPID.

private static void initPID() {
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    String name = runtime.getName();
    System.out.println("Java process name: " + name);
    PROCESS_NAME = name;
    int index = name.indexOf("@");
    if (index != -1) {
        PID = Integer.parseInt(name.substring(0, index));
        System.out.println("Java process id: " + PID);
        hostname = name.substring(index + 1);
        System.out.println("hostname: " + hostname);
    } else {
        throw new RuntimeException("Failed to obtain the java process id.");
    }
}
Also used : RuntimeMXBean(java.lang.management.RuntimeMXBean)

Example 45 with RuntimeMXBean

use of java.lang.management.RuntimeMXBean in project Saber-Bot by notem.

the class StatsCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    JDA.ShardInfo info = event.getJDA().getShardInfo();
    Runtime rt = Runtime.getRuntime();
    RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
    String msg = "```python\n" + "\"Database\"\n" + "      Entries: " + Main.getDBDriver().getEventCollection().count() + "\n" + "    Schedules: " + Main.getDBDriver().getScheduleCollection().count() + "\n" + "       Guilds: " + Main.getDBDriver().getGuildCollection().count() + "\n" + "\n\"Shard\"\n" + "      ShardId: " + info.getShardId() + "/" + info.getShardTotal() + "\n" + "       Guilds: " + event.getJDA().getGuilds().size() + "\n" + "        Users: " + event.getJDA().getUsers().size() + "\n" + "ResponseTotal: " + event.getJDA().getResponseTotal() + "\n" + "\n\"Application\"\n" + " Memory-total: " + rt.totalMemory() / 1024 / 1024 + " MB\n" + "       -free : " + rt.freeMemory() / 1024 / 1024 + " MB\n" + "       -max  : " + rt.maxMemory() / 1024 / 1024 + " MB\n" + "      Threads: " + Thread.activeCount() + "\n" + "       Uptime: " + rb.getUptime() / 1000 / 60 + " minute(s)" + "```";
    if (event.isFromType(ChannelType.PRIVATE)) {
        MessageUtilities.sendPrivateMsg(msg, event.getAuthor(), null);
    } else {
        MessageUtilities.sendMsg(msg, event.getTextChannel(), null);
    }
}
Also used : JDA(net.dv8tion.jda.core.JDA) RuntimeMXBean(java.lang.management.RuntimeMXBean)

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