Java Code Examples for com.sun.management.OperatingSystemMXBean#getProcessCpuLoad()

The following examples show how to use com.sun.management.OperatingSystemMXBean#getProcessCpuLoad() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetProcessCpuLoad.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 2
Source File: GetProcessCpuLoad.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 3
Source File: GetProcessCpuLoad.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 4
Source File: GetProcessCpuLoad.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 5
Source File: GetProcessCpuLoad.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 6
Source File: GetProcessCpuLoad.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 7
Source File: GetProcessCpuLoad.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 8
Source File: GetProcessCpuLoad.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 9
Source File: GetProcessCpuLoad.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 10
Source File: GetProcessCpuLoad.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 11
Source File: GetProcessCpuLoad.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 12
Source File: GetProcessCpuLoad.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 13
Source File: GetProcessCpuLoad.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] argv) throws Exception {
    OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
        ManagementFactory.getOperatingSystemMXBean();
    double load;
    for(int i=0; i<10; i++) {
        load = mbean.getProcessCpuLoad();
        if((load<0.0 || load>1.0) && load != -1.0) {
            throw new RuntimeException("getProcessCpuLoad() returns " + load
                   +   " which is not in the [0.0,1.0] interval");
        }
        try {
            Thread.sleep(200);
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 14
Source File: HostInfo.java    From vi with Apache License 2.0 5 votes vote down vote up
@Override
public void refresh() {
    OperatingSystemMXBean bean= (OperatingSystemMXBean ) ManagementFactory.getOperatingSystemMXBean();
    maxFileDescriptorCount = invoke(bean,"getMaxFileDescriptorCount");
    openFileDescriptorCount = invoke(bean, "getOpenFileDescriptorCount");
    PhysicalMemoryAvaliable = bean.getFreePhysicalMemorySize();
    PhysicalMemoryTotal = bean.getTotalPhysicalMemorySize();
    systemCpuLoad = (float) bean.getSystemCpuLoad();
    processCpuTime = bean.getProcessCpuTime();
    processCpuLoad = bean.getProcessCpuLoad();

    File[] roots = File.listRoots();
    long diskAvaliable=0,diskTotal=0;
    for(File file:roots){
        diskAvaliable += file.getFreeSpace();
        diskTotal += file.getTotalSpace();
    }
    DiskAvaliable = diskAvaliable;
    DiskTotal = diskTotal;
    if(isLinux()) {
        try {
            availableMem =(LinuxInfoUtil.getAvailableMemKB()*1024l);
            cpuLoadAverages = LinuxInfoUtil.getCpuLoadAverage();
        } catch (Throwable e) {
            logger.warn("get linux info failed!");
        }
    }

}
 
Example 15
Source File: IndexAdminController.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequestMapping(value = "/console", method = RequestMethod.GET)
public String console(Model model) {
	// 查询当天新增话题
    model.addAttribute("topic_count", topicService.countToday());
    // 查询当天新增评论
    model.addAttribute("comment_count", replyService.count());
    // 查询当天新增用户
    model.addAttribute("user_count", userService.countToday());
    //查询当天新增节点
    model.addAttribute("node_count", nodeService.countToday());
    // 获取操作系统的名字
    model.addAttribute("os_name", System.getProperty("os.name"));
    // 内存
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean();
    // 总的物理内存(G)
    float totalMemorySize = (float) osmxb.getTotalPhysicalMemorySize() / kb / kb / kb;
    
    //已使用的物理内存(G)
    float usedMemory = (float) (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb / kb /kb;
    // 获取系统cpu负载
    double systemCpuLoad = osmxb.getSystemCpuLoad();
    // 获取jvm线程负载
    double processCpuLoad = osmxb.getProcessCpuLoad();
    
    DecimalFormat df = new DecimalFormat("0.0");
    model.addAttribute("totalMemorySize", df.format(totalMemorySize));
    model.addAttribute("usedMemory", df.format(usedMemory));
    model.addAttribute("systemCpuLoad", df.format(systemCpuLoad));
    model.addAttribute("processCpuLoad", df.format(processCpuLoad));

	return "/admin/console";
}
 
Example 16
Source File: IndexAdminController.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model model) {
	// 查询当天新增话题
    model.addAttribute("topic_count", topicService.countToday());
    // 查询当天新增评论
    model.addAttribute("comment_count", replyService.count());
    // 查询当天新增用户
    model.addAttribute("user_count", userService.countToday());
    //查询当天新增节点
    model.addAttribute("node_count", nodeService.countToday());
    // 获取操作系统的名字
    model.addAttribute("os_name", System.getProperty("os.name"));
    // 内存
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean();
    // 总的物理内存(G)
    float totalMemorySize = (float) osmxb.getTotalPhysicalMemorySize() / kb / kb / kb;
    
    //已使用的物理内存(G)
    float usedMemory = (float) (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb / kb /kb;
    // 获取系统cpu负载
    double systemCpuLoad = osmxb.getSystemCpuLoad();
    // 获取jvm线程负载
    double processCpuLoad = osmxb.getProcessCpuLoad();
    
    DecimalFormat df = new DecimalFormat("0.0");
    model.addAttribute("totalMemorySize", df.format(totalMemorySize));
    model.addAttribute("usedMemory", df.format(usedMemory));
    model.addAttribute("systemCpuLoad", df.format(systemCpuLoad));
    model.addAttribute("processCpuLoad", df.format(processCpuLoad));

	return "/admin/index";
}
 
Example 17
Source File: IndexAdminController.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
@RequiresUser
@GetMapping({"/", "/index"})
public String index(Model model) {
    // 查询当天新增话题
    model.addAttribute("topic_count", topicService.countToday());
    // 查询当天新增标签
    model.addAttribute("tag_count", tagService.countToday());
    // 查询当天新增评论
    model.addAttribute("comment_count", commentService.countToday());
    // 查询当天新增用户
    model.addAttribute("user_count", userService.countToday());

    // 获取操作系统的名字
    model.addAttribute("os_name", System.getProperty("os.name"));

    // 内存
    int kb = 1024;
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    // 总的物理内存
    long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
    //已使用的物理内存
    long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
    // 获取系统cpu负载
    double systemCpuLoad = osmxb.getSystemCpuLoad();
    // 获取jvm线程负载
    double processCpuLoad = osmxb.getProcessCpuLoad();

    model.addAttribute("totalMemorySize", totalMemorySize);
    model.addAttribute("usedMemory", usedMemory);
    model.addAttribute("systemCpuLoad", systemCpuLoad);
    model.addAttribute("processCpuLoad", processCpuLoad);

    return "admin/index";
}
 
Example 18
Source File: PerformanceStatus.java    From vi with Apache License 2.0 4 votes vote down vote up
public PerformanceStatus() {
    OperatingSystemMXBean bean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();

    MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
    heapMemoryUsage= memBean.getHeapMemoryUsage();
    nonHeapMemoryUsage = memBean.getNonHeapMemoryUsage();

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    currentThreadCount=threadBean.getThreadCount();
    daemonThreadCount= threadBean.getDaemonThreadCount();
    beanCreatedThreadCount= threadBean.getTotalStartedThreadCount();
    peakThreadCount = threadBean.getPeakThreadCount();

    ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    loadedClassCount=classLoadingBean.getLoadedClassCount();
    totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
    unloadedClassCount=classLoadingBean.getUnloadedClassCount();
    committedVirtualMemorySize = (bean.getCommittedVirtualMemorySize());
    freePhysicalMemorySize =(bean.getFreePhysicalMemorySize());
    totalPhysicalMemorySize =(bean.getTotalPhysicalMemorySize());

    freeSwapSpaceSize =(bean.getFreeSwapSpaceSize());
    totalSwapSpaceSize =(bean.getTotalSwapSpaceSize());
    processCpuTime =(bean.getProcessCpuTime());
    availableProcessors =bean.getAvailableProcessors();
    processCpuLoad =bean.getProcessCpuLoad();

    systemCpuLoad =bean.getSystemCpuLoad();
    systemLoadAverage =bean.getSystemLoadAverage();
    appStartUpTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(runtimeBean.getStartTime()));
    runtime = (new Date().getTime() - runtimeBean.getStartTime())/1000;
    os = bean.getName()+" "+bean.getVersion();

    if(HostInfo.isLinux()){
        try {
            availableMemory = (LinuxInfoUtil.getAvailableMemKB()*1024l);
        } catch (Throwable ignored) {
        }
    }

    File[] roots = File.listRoots();
    for(File file:roots){
        rootFiles.add(new RootFile(file.getAbsolutePath(),file.getTotalSpace(),file.getFreeSpace()));
    }
    getGCStatus();
}
 
Example 19
Source File: JmxCollector.java    From promagent with Apache License 2.0 4 votes vote down vote up
private MetricFamilySamples collectOperatingSystemMetrics() {
    OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    MetricFamilySamples.Sample cpuLoad = new MetricFamilySamples.Sample("process_cpu_load", new ArrayList<>(), new ArrayList<>(), operatingSystemMXBean.getProcessCpuLoad());
    return new MetricFamilySamples(cpuLoad.name, Type.GAUGE, "recent cpu usage for the whole system", Arrays.asList(cpuLoad));
}
 
Example 20
Source File: WindowsSystemCommander.java    From Jpom with MIT License 4 votes vote down vote up
/**
     * 将windows的tasklist转为集合
     *
     * @param header 是否包含投信息
     * @param result 进程信息
     * @return jsonArray
     */
    private static List<ProcessModel> formatWindowsProcess(String result, boolean header) {
        List<String> list = StrSpliter.splitTrim(result, StrUtil.LF, true);
        if (list.isEmpty()) {
            return null;
        }
        List<ProcessModel> processModels = new ArrayList<>();
        ProcessModel processModel;
        for (int i = header ? 2 : 0, len = list.size(); i < len; i++) {
            String param = list.get(i);
            List<String> memList = StrSpliter.splitTrim(param, StrUtil.SPACE, true);
            processModel = new ProcessModel();
            int pid = Convert.toInt(memList.get(1), 0);
            processModel.setPid(pid);
            //
            String name = memList.get(0);
            processModel.setCommand(name);
            //使用内存 kb
            String mem = memList.get(4).replace(",", "");
            long aLong = Convert.toLong(mem, 0L);
//            FileUtil.readableFileSize()
            processModel.setRes(aLong / 1024 + " MB");
            String status = memList.get(6);
            processModel.setStatus(formatStatus(status));
            //
            processModel.setUser(memList.get(7));
            processModel.setTime(memList.get(8));

            try {
                OperatingSystemMXBean operatingSystemMXBean = JvmUtil.getOperatingSystemMXBean(memList.get(1));
                if (operatingSystemMXBean != null) {
                    //最近jvm cpu使用率
                    double processCpuLoad = operatingSystemMXBean.getProcessCpuLoad() * 100;
                    if (processCpuLoad <= 0) {
                        processCpuLoad = 0;
                    }
                    processModel.setCpu(String.format("%.2f", processCpuLoad) + "%");
                    //服务器总内存
                    long totalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize();
                    BigDecimal total = new BigDecimal(totalMemorySize / 1024);
                    // 进程
                    double v = new BigDecimal(aLong).divide(total, 4, BigDecimal.ROUND_HALF_UP).doubleValue() * 100;
                    processModel.setMem(String.format("%.2f", v) + "%");
                }
            } catch (Exception ignored) {

            }
            processModels.add(processModel);
        }
        return processModels;
    }