Java获取Linux系统cpu使用率
原文:http://www.open-open.com/code/view/1426152165201
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.StringTokenizer;
- import org.apache.commons.io.FileSystemUtils;
- public class OSUtils {
- /**
- * 功能:可用磁盘
- * */
- public static int disk() {
- try {
- long total = FileSystemUtils.freeSpaceKb("/home");
- double disk = (double) total / 1024 / 1024;
- return (int) disk;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * 功能:获取Linux系统cpu使用率
- * */
- public static int cpuUsage() {
- try {
- Map<?, ?> map1 = OSUtils.cpuinfo();
- Thread.sleep(5 * 1000);
- Map<?, ?> map2 = OSUtils.cpuinfo();
- long user1 = Long.parseLong(map1.get("user").toString());
- long nice1 = Long.parseLong(map1.get("nice").toString());
- long system1 = Long.parseLong(map1.get("system").toString());
- long idle1 = Long.parseLong(map1.get("idle").toString());
- long user2 = Long.parseLong(map2.get("user").toString());
- long nice2 = Long.parseLong(map2.get("nice").toString());
- long system2 = Long.parseLong(map2.get("system").toString());
- long idle2 = Long.parseLong(map2.get("idle").toString());
- long total1 = user1 + system1 + nice1;
- long total2 = user2 + system2 + nice2;
- float total = total2 - total1;
- long totalIdle1 = user1 + nice1 + system1 + idle1;
- long totalIdle2 = user2 + nice2 + system2 + idle2;
- float totalidle = totalIdle2 - totalIdle1;
- float cpusage = (total / totalidle) * 100;
- return (int) cpusage;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return 0;
- }
- /**
- * 功能:CPU使用信息
- * */
- public static Map<?, ?> cpuinfo() {
- InputStreamReader inputs = null;
- BufferedReader buffer = null;
- Map<String, Object> map = new HashMap<String, Object>();
- try {
- inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
- buffer = new BufferedReader(inputs);
- String line = "";
- while (true) {
- line = buffer.readLine();
- if (line == null) {
- break;
- }
- if (line.startsWith("cpu")) {
- StringTokenizer tokenizer = new StringTokenizer(line);
- List<String> temp = new ArrayList<String>();
- while (tokenizer.hasMoreElements()) {
- String value = tokenizer.nextToken();
- temp.add(value);
- }
- map.put("user", temp.get(1));
- map.put("nice", temp.get(2));
- map.put("system", temp.get(3));
- map.put("idle", temp.get(4));
- map.put("iowait", temp.get(5));
- map.put("irq", temp.get(6));
- map.put("softirq", temp.get(7));
- map.put("stealstolen", temp.get(8));
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- buffer.close();
- inputs.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return map;
- }
- /**
- * 功能:内存使用率
- * */
- public static int memoryUsage() {
- Map<String, Object> map = new HashMap<String, Object>();
- InputStreamReader inputs = null;
- BufferedReader buffer = null;
- try {
- inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
- buffer = new BufferedReader(inputs);
- String line = "";
- while (true) {
- line = buffer.readLine();
- if (line == null)
- break;
- int beginIndex = 0;
- int endIndex = line.indexOf(":");
- if (endIndex != -1) {
- String key = line.substring(beginIndex, endIndex);
- beginIndex = endIndex + 1;
- endIndex = line.length();
- String memory = line.substring(beginIndex, endIndex);
- String value = memory.replace("kB", "").trim();
- map.put(key, value);
- }
- }
- long memTotal = Long.parseLong(map.get("MemTotal").toString());
- long memFree = Long.parseLong(map.get("MemFree").toString());
- long memused = memTotal - memFree;
- long buffers = Long.parseLong(map.get("Buffers").toString());
- long cached = Long.parseLong(map.get("Cached").toString());
- double usage = (double) (memused - buffers - cached) / memTotal * 100;
- return (int) usage;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- buffer.close();
- inputs.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return 0;
- }
- }
Java获取Linux系统cpu使用率的更多相关文章
- C/C++获取Linux系统CPU和内存及硬盘使用情况
需求分析: 不使用Top df free 等命令,利用C/C++获取Linux系统CPU和内存及硬盘使用情况 实现: //通过获取/proc/stat (CPU)和/proc/meminfo(内存 ...
- JAVA如何利用Swiger获取Linux系统电脑配置相关信息
最近开发java应用程序,涉及到获取Linux服务器相关配置的问题,特地网上搜寻了下,采用Swiger包可以直接获取,再次小结一下,以便于以后能方便使用,也便于其他童鞋们学习. 推荐大家参考链接:ht ...
- Java获取Linux和Window系统CPU、内存和磁盘总使用率的情况
这是一个工具类,获取的内容: CPU使用率:得到的是当前CPU的使用情况,这是算出的是两次500毫秒时间差的CPU使用率 内存使用率:[1 - 剩余的物理内存/(总的物理内存+虚拟内存) ] * 1 ...
- java 获取系统信息及CPU的使用率(转)
java 获取系统信息及CPU的使用率 原文:http://kakaluyi.javaeye.com/blog/211492 最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接 ...
- 关于linux系统CPU篇--->CPU使用率升高
1.CPU使用率为单位时间内CPU使用情况的统计,以百分比的方式展示. LINUX作为一个多任务操作系统,将每个CPU的时间划分为很短的时间片,再通过调度器轮流分配给各个任务使用,因此造成多任务同时运 ...
- linux系统CPU,内存,磁盘,网络流量监控脚本
前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...
- 关于linux系统CPU篇--->上下文切换
1.什么是CPU上下文切换? linux是一个多任务操作系统,它支持远大于CPU数量的任务同时运行,当然这些任务实际上并不是真的同时在运行,而是因为系统在很短的时间内,将CPU轮流分配给它们,造成多任 ...
- Linux系统CPU的性能监控及调优
前言: 性能优化是一个老生常谈的话题,典型的性能问题如页面响应慢.接口超时,服务器负载高.并发数低,数据库频繁死锁等.尤其是在“糙快猛”的互联网开发模式大行其道的今天,随着系统访问量的日益增加和代码的 ...
- centOs 查看系统cpu使用率等--top
原文:centOs 查看系统cpu使用率等--top 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CP ...
随机推荐
- python struct.pack方法报错argument for 's' must be a bytes object 解决
参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...
- 关于apache access log 统计的那些事儿
统计APACHE ACCESS.LOG IP访问记录 可以根据自己的需要,统计很多,每个IP访问多少个页面等等! cat access.log-20090904 |awk '{print $3}'|s ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- [JOYOI] 1415 西瓜种植
题目描述 笨笨种了一块西瓜地,但这块西瓜地的种植范围是一条直线的-- 笨笨在一番研究过后,得出了m个结论,这m个结论可以使他收获的西瓜最多. 笨笨的结论是这样的: 从西瓜地B处到E处至少要种植T个西瓜 ...
- 我的Python分析成长之路1
Python是什么? ...
- 转载 js弹出框、对话框、提示框、弹窗总结
转载:https://blog.csdn.net/huileiforever/article/details/9464659 一.JS的三种最常见的对话框 //================== ...
- 【06】sass编译工具(弃)
[06]编译工具(弃) SASS转译工具 除了使用sass命令来转译SASS文件之外,还可以借助第三方工具完成,目前世面上较为流行的转译工具主要有: Compass.app Scout Codekit ...
- luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法
BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...
- luogu3302 [SDOI2013]森林
前置技能:Count on a tree 然后带上一个启发式合并 #include <algorithm> #include <iostream> #include <c ...
- 大数据学习——mapreduce案例join算法
需求: 用mapreduce实现select order.orderid,order.pdtid,pdts.pdt_name,oder.amount from orderjoin pdtson ord ...