Linux下java获取CPU、内存、磁盘IO、网络带宽使用率
一、CPU
使用proc文件系统,"proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。"
从/proc文件系统获取cpu使用情况: cat /proc/stat
在Linux的内核中,有一个全 局变量:Jiffies。 Jiffies代表时间。它的单位随硬件平台的不同而不同。系统里定义了一个常数HZ,代表每秒种最小时间间隔的数目。这样jiffies的单位就是 1/HZ。Intel平台jiffies的单位是1/100秒,这就是系统所能分辨的最小时间间隔了。每个CPU时间片,Jiffies都要加1。 CPU的利用率就是用执行用户态+系统态的Jiffies除以总的Jifffies来表示。
[root@localhost LoadBalanceAlg]# cat /proc/stat
cpu 71095 55513 76751 2545622893 303185 4160 47722 0
cpu0 3855 1134 4284 159122519 3882 0 717 0
cpu1 4236 770 5837 159113370 11291 6 865 0
cpu2 4934 1142 5048 158991321 130622 362 2939 0
cpu3 2320 14774 5177 159111528 1417 8 1138 0
cpu4 2694 405 3086 159071174 56284 235 2477 0
cpu5 1701 886 2560 159129034 1316 2 849 0
cpu6 2937 450 2863 159068480 59183 228 2198 0
cpu7 916 316 2426 159130057 1682 1 933 0
cpu8 2543 50 3509 159122844 4467 1 2911 0
cpu9 4761 827 6296 159118849 4490 8 1086 0
cpu10 8517 4236 9148 159102063 9791 173 2382 0
cpu11 22001 29737 14602 159065992 2583 6 1382 0
cpu12 3453 150 3075 159113794 5387 1162 9276 0
cpu13 2120 424 3403 159126526 2608 7 1199 0
cpu14 2637 65 2663 159107796 6704 1914 14503 0
cpu15 1462 142 2763 159127539 1470 39 2859 0
intr 1636622296 1591605869 4 0 4 4 0 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1005479 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32763528 0 0 0 0 0 0 0 1697776 0 0 0 0 0 0 0 1556158 2 0 0 0 0 0 0 1598011 0 0 0 0 0 0 0 1287622 0 0 0 0 0 0 0 1522517 0 0 0 0 0 0 0 2467360 0 0 0 0 0 0 0 1116999 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 431778894
btime 1363058934
processes 279394
procs_running 1
procs_blocked 0
CPU 以及CPU0、CPU1、CPU2、CPU3每行的每个参数意思(以第一行为例)为:
参数 解释
user (432661) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
nice (13295) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
system (86656) 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
idle (422145968) 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
iowait (171474) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
irq (233) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
softirq (5346) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
CPU时间=user+system+nice+idle+iowait+irq+softirq
“intr”这行给出中断的信息,第一个为自系统启动以来,发生的所有的中断的次数;然后每个数对应一个特定的中断自系统启动以来所发生的次数。
“ctxt”给出了自系统启动以来CPU发生的上下文交换的次数。
“btime”给出了从系统启动到现在为止的时间,单位为秒。
“processes (total_forks) 自系统启动以来所创建的任务的个数目。
“procs_running”:当前运行队列的任务的数目。
“procs_blocked”:当前被阻塞的任务的数目。
public class Runtimeextends Object
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过getRuntime 方法获取当前运行时。
应用程序不能创建自己的 Runtime 类实例。
public abstract class Processextends Object
ProcessBuilder.start() 和Runtime.exec 方法创建一个本机进程,并返回 Process 子类的一个实例,该实例可用来控制进程并获得相关信息。
<span style="font-size:14px;">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter; import org.apache.log4j.Logger; /**
* 采集CPU使用率
*/
public class CpuUsage extends ResourceUsage { private static Logger log = Logger.getLogger(CpuUsage.class);
private static CpuUsage INSTANCE = new CpuUsage(); private CpuUsage(){ } public static CpuUsage getInstance(){
return INSTANCE;
} /**
* Purpose:采集CPU使用率
* @param args
* @return float,CPU使用率,小于1
*/
@Override
public float get() {
log.info("开始收集cpu使用率");
float cpuUsage = 0;
Process pro1,pro2;
Runtime r = Runtime.getRuntime();
try {
String command = "cat /proc/stat";
//第一次采集CPU时间
long startTime = System.currentTimeMillis();
pro1 = r.exec(command);
BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
String line = null;
long idleCpuTime1 = 0, totalCpuTime1 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while((line=in1.readLine()) != null){
if(line.startsWith("cpu")){
line = line.trim();
log.info(line);
String[] temp = line.split("\\s+");
idleCpuTime1 = Long.parseLong(temp[4]);
for(String s : temp){
if(!s.equals("cpu")){
totalCpuTime1 += Long.parseLong(s);
}
}
log.info("IdleCpuTime: " + idleCpuTime1 + ", " + "TotalCpuTime" + totalCpuTime1);
break;
}
}
in1.close();
pro1.destroy();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("CpuUsage休眠时发生InterruptedException. " + e.getMessage());
log.error(sw.toString());
}
//第二次采集CPU时间
long endTime = System.currentTimeMillis();
pro2 = r.exec(command);
BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
long idleCpuTime2 = 0, totalCpuTime2 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while((line=in2.readLine()) != null){
if(line.startsWith("cpu")){
line = line.trim();
log.info(line);
String[] temp = line.split("\\s+");
idleCpuTime2 = Long.parseLong(temp[4]);
for(String s : temp){
if(!s.equals("cpu")){
totalCpuTime2 += Long.parseLong(s);
}
}
log.info("IdleCpuTime: " + idleCpuTime2 + ", " + "TotalCpuTime" + totalCpuTime2);
break;
}
}
if(idleCpuTime1 != 0 && totalCpuTime1 !=0 && idleCpuTime2 != 0 && totalCpuTime2 !=0){
cpuUsage = 1 - (float)(idleCpuTime2 - idleCpuTime1)/(float)(totalCpuTime2 - totalCpuTime1);
log.info("本节点CPU使用率为: " + cpuUsage);
}
in2.close();
pro2.destroy();
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("CpuUsage发生InstantiationException. " + e.getMessage());
log.error(sw.toString());
}
return cpuUsage;
} /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
while(true){
System.out.println(CpuUsage.getInstance().get());
Thread.sleep(5000);
}
}
}</span>
MemFree: 4109964 kB
Buffers: 35728 kB
Cached: 1877960 kB
SwapCached: 159088 kB
Active: 3184176 kB
Inactive: 672132 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 8167348 kB
LowFree: 4109964 kB
SwapTotal: 26738680 kB
SwapFree: 26373632 kB
Dirty: 40 kB
Writeback: 0 kB
AnonPages: 1872416 kB
Mapped: 24928 kB
Slab: 107804 kB
PageTables: 34612 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 30822352 kB
Committed_AS: 5386080 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 276892 kB
VmallocChunk: 34359460287 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
Hugepagesize: 2048 kB
<span style="font-size:14px;">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter; import org.apache.log4j.Logger; /**
* 采集内存使用率
*/
public class MemUsage extends ResourceUsage{ private static Logger log = Logger.getLogger(MemUsage.class);
private static MemUsage INSTANCE = new MemUsage(); private MemUsage(){ } public static MemUsage getInstance(){
return INSTANCE;
} /**
* Purpose:采集内存使用率
* @param args
* @return float,内存使用率,小于1
*/
@Override
public float get() {
log.info("开始收集memory使用率");
float memUsage = 0.0f;
Process pro = null;
Runtime r = Runtime.getRuntime();
try {
String command = "cat /proc/meminfo";
pro = r.exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String line = null;
int count = 0;
long totalMem = 0, freeMem = 0;
while((line=in.readLine()) != null){
log.info(line);
String[] memInfo = line.split("\\s+");
if(memInfo[0].startsWith("MemTotal")){
totalMem = Long.parseLong(memInfo[1]);
}
if(memInfo[0].startsWith("MemFree")){
freeMem = Long.parseLong(memInfo[1]);
}
memUsage = 1- (float)freeMem/(float)totalMem;
log.info("本节点内存使用率为: " + memUsage);
if(++count == 2){
break;
}
}
in.close();
pro.destroy();
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("MemUsage发生InstantiationException. " + e.getMessage());
log.error(sw.toString());
}
return memUsage;
} /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
while(true){
System.out.println(MemUsage.getInstance().get());
Thread.sleep(5000);
}
}
}</span>
Linux 2.6.18-238.el5 (localhost.localdomain) 2013Äê03ÔÂ30ÈÕ
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 0.09 0.28 0.02 0.03 0.92 2.57 60.71 0.00 63.28 3.33 0.02
sda1 0.00 0.00 0.00 0.00 0.00 0.00 24.40 0.00 2.59 2.53 0.00
sda2 0.09 0.28 0.02 0.03 0.92 2.57 60.76 0.00 63.36 3.34 0.02
sdb 0.03 0.72 0.04 0.53 2.57 10.04 22.09 0.01 17.36 5.12 0.29
sdb1 0.03 0.72 0.04 0.53 2.57 10.04 22.09 0.01 17.36 5.12 0.29
dm-0 0.00 0.00 0.07 1.30 2.63 10.40 9.53 0.03 24.95 2.15 0.29
dm-1 0.00 0.00 0.11 0.28 0.86 2.21 8.00 0.12 300.47 0.16 0.01
<span style="font-size:14px;">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter; import org.apache.log4j.Logger; /**
* 采集磁盘IO使用率
*/
public class IoUsage extends ResourceUsage{ private static Logger log = Logger.getLogger(IoUsage.class);
private static IoUsage INSTANCE = new IoUsage(); private IoUsage(){ } public static IoUsage getInstance(){
return INSTANCE;
} /**
* @Purpose:采集磁盘IO使用率
* @param args
* @return float,磁盘IO使用率,小于1
*/
@Override
public float get() {
log.info("开始收集磁盘IO使用率");
float ioUsage = 0.0f;
Process pro = null;
Runtime r = Runtime.getRuntime();
try {
String command = "iostat -d -x";
pro = r.exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String line = null;
int count = 0;
while((line=in.readLine()) != null){
if(++count >= 4){
// log.info(line);
String[] temp = line.split("\\s+");
if(temp.length > 1){
float util = Float.parseFloat(temp[temp.length-1]);
ioUsage = (ioUsage>util)?ioUsage:util;
}
}
}
if(ioUsage > 0){
log.info("本节点磁盘IO使用率为: " + ioUsage);
ioUsage /= 100;
}
in.close();
pro.destroy();
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("IoUsage发生InstantiationException. " + e.getMessage());
log.error(sw.toString());
}
return ioUsage;
} /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
while(true){
System.out.println(IoUsage.getInstance().get());
Thread.sleep(5000);
}
} }</span>
从/proc文件系统获取网络使用情况: cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:1402131426 15109136 0 0 0 0 0 0 1402131426 15109136 0 0 0 0 0 0
eth0:4546168400 42535979 0 0 0 0 0 44 5610190492 8943999 0 0 0 0 0 0
eth1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
eth3: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
__tmp945063435: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
bond0:4546168400 42535979 0 0 0 0 0 44 5610190492 8943999 0 0 0 0 0 0
<span style="font-size:14px;">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter; import org.apache.log4j.Logger; /**
* 采集网络带宽使用率
*/
public class NetUsage extends ResourceUsage { private static Logger log = Logger.getLogger(NetUsage.class);
private static NetUsage INSTANCE = new NetUsage();
private final static float TotalBandwidth = 1000; //网口带宽,Mbps private NetUsage(){ } public static NetUsage getInstance(){
return INSTANCE;
} /**
* @Purpose:采集网络带宽使用率
* @param args
* @return float,网络带宽使用率,小于1
*/
@Override
public float get() {
log.info("开始收集网络带宽使用率");
float netUsage = 0.0f;
Process pro1,pro2;
Runtime r = Runtime.getRuntime();
try {
String command = "cat /proc/net/dev";
//第一次采集流量数据
long startTime = System.currentTimeMillis();
pro1 = r.exec(command);
BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
String line = null;
long inSize1 = 0, outSize1 = 0;
while((line=in1.readLine()) != null){
line = line.trim();
if(line.startsWith("eth0")){
log.info(line);
String[] temp = line.split("\\s+");
inSize1 = Long.parseLong(temp[0].substring(5)); //Receive bytes,单位为Byte
outSize1 = Long.parseLong(temp[8]); //Transmit bytes,单位为Byte
break;
}
}
in1.close();
pro1.destroy();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("NetUsage休眠时发生InterruptedException. " + e.getMessage());
log.error(sw.toString());
}
//第二次采集流量数据
long endTime = System.currentTimeMillis();
pro2 = r.exec(command);
BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
long inSize2 = 0 ,outSize2 = 0;
while((line=in2.readLine()) != null){
line = line.trim();
if(line.startsWith("eth0")){
log.info(line);
String[] temp = line.split("\\s+");
inSize2 = Long.parseLong(temp[0].substring(5));
outSize2 = Long.parseLong(temp[8]);
break;
}
}
if(inSize1 != 0 && outSize1 !=0 && inSize2 != 0 && outSize2 !=0){
float interval = (float)(endTime - startTime)/1000;
//网口传输速度,单位为bps
float curRate = (float)(inSize2 - inSize1 + outSize2 - outSize1)*8/(1000000*interval);
netUsage = curRate/TotalBandwidth;
log.info("本节点网口速度为: " + curRate + "Mbps");
log.info("本节点网络带宽使用率为: " + netUsage);
}
in2.close();
pro2.destroy();
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.error("NetUsage发生InstantiationException. " + e.getMessage());
log.error(sw.toString());
}
return netUsage;
} /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
while(true){
System.out.println(NetUsage.getInstance().get());
Thread.sleep(5000);
}
}
}</span>
Linux下java获取CPU、内存、磁盘IO、网络带宽使用率的更多相关文章
- Linux下java进程CPU占用率高分析方法
Linux下java进程CPU占用率高分析方法 在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况.这种情况发生时,我们怎么去找出原因并解决. 一般解决方法是通过top命令找出消耗资源 ...
- Linux下java进程CPU占用率高分析方法(一)
Linux下java进程CPU占用率高分析方法 在工作当中,肯定会遇到由代码所导致的高CPU耗用以及内存溢出的情况.这种情况发生时,我们怎么去找出原因并解决. 一般解决方法是通过top命令找出消耗资源 ...
- (转)Linux下java进程CPU占用率高-分析方法
Linux下java进程CPU占用率高-分析方法 原文:http://itindex.net/detail/47420-linux-java-%E8%BF%9B%E7%A8%8B?utm_source ...
- 【转】Linux 下取进程占用 cpu/内存 最高的前10个进程
# Linux 下 取进程占用 cpu 最高的前10个进程ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head # linux 下 取进程占用内存 ...
- linux 下取进程占用 cpu/内存 最高的前10个进程
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ;|head linux下获取占用内存资源最多的10个进程,可以使用如下命令组合: ;|head 命令组合解析(针对CPU的,M ...
- Linux下java进程CPU占用率高-分析方法
今天登陆同事的一台gateway 开始以为hive环境登陆不了了,仔细一看看了下是因为机器很卡,我每次等几秒没登陆就ctrl+c了,看了下是有个java进程cpu:340.4% mem:14.6% ...
- 排查linux下java应用cpu占用过高
用于快速排查Java的CPU性能问题(top us值过高),自动查出运行的Java进程中消耗CPU多的线程,并打印出其线程栈,从而确定导致性能问题的方法调用.目前只支持Linux.原因是Mac.Win ...
- Linux下java进程CPU占用率高分析方法(二)
1. 通过 top 命令查看当前系统CPU使用情况,定位CPU使用率超过100%的进程ID:2. 通过 ps aux | grep PID 命令进一步确定具体的线程信息:3. 通过 ps -mp pi ...
- 方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...
随机推荐
- Python获取目录、文件的注意事项
Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...
- chrome控制台支持多行js模式
shift + 回车 是换行 转自: http://zhidao.baidu.com/link?url=MYjGRwvVQYJwnr38VTHPJdzRNtF1COyqpeuAtBYbxFYJcu6p ...
- 基础知识《六》---Java集合类: Set、List、Map、Queue使用场景梳理
本文转载自LittleHann 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E6%8E%92%E5%BA%8F% ...
- HTML表单元素登陆界面
<form action="" method="post" name="myform"><p>E-mail:< ...
- Java动态加载类在功能模块开发中的作用
Java中我们一般会使用new关键字实例化对象然后调用该对象所属类提供的方法来实现相应的功能,比如我们现在有个主类叫Web类这个类中能实现各种方法,比如用户注册.发送邮件等功能,代码如下: /* * ...
- Greedy:Stripes(POJ 1826)
新生物 题目大意:给你一堆数,两两结合,答案为2*sqrt(x1*x2),问组合成一个数时,最小的量? 超级无敌大水题,排序或者用堆都可以,反正就是优先组合大的,让根号一直把大数开根降低整体的大小 # ...
- CentOS6.5安装MySql5.5
最近在CentOS上安装MySql,本来以为yum安装会很简单,但是却花了自己不少时间,所以决定和大家分享下. 首先,安装MySql源! 下载地址:http://dev.mysql.com/downl ...
- String解析
常量池(Constant Pool):指的是在编译期被确定,并被保存在已编译的.class文件中的一些数据.JVM虚拟机为每个被装载的类型维护一个常量池.常量池就是该类型所用到常量的一个有序集和,包括 ...
- 通过百度echarts实现数据图表展示功能
现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...
- MVC自带的校验
一.添加控制器Home和Model数据 public class UserInfo { public int Id { get; set; } [Display(Name="用户名" ...