The easiest method to get the CPU usage from a Java class in Linux is to parse the output of the top command. Check out the following code.
import java.io.*;
public class SystemData
{
static String cmdTop = "top -n 2 -b -d 0.2";
// returns user cpu usage
public static double getCpu()
{
double cpu = -1;
try
{
// start up the command in child process
String cmd = cmdTop;
Process child = Runtime.getRuntime().exec(cmd);
// hook up child process output to parent
InputStream lsOut = child.getInputStream();
InputStreamReader r = new InputStreamReader(lsOut);
BufferedReader in = new BufferedReader(r);
// read the child process' output
String line;
int emptyLines = 0;
while(emptyLines<3)
{
line = in.readLine();
if (line.length()<1) emptyLines++;
}
in.readLine();
in.readLine();
line = in.readLine();
System.out.println("Parsing line "+ line);
String delims = "%";
String[] parts = line.split(delims);
System.out.println("Parsing fragment " + parts[0]);
delims =" ";
parts = parts[0].split(delims);
cpu = Double.parseDouble(parts[parts.length-1]);
}
catch (Exception e)
{ // exception thrown
System.out.println("Command failed!");
}
return cpu;
}
}
The parameters from the top command line mean: -b = batch mode, -d 0.2 = take the readings at 0.2 seconds intervals, -n 2 = take 2 readings (the first reading is not correct, from my experience, so we take two).
This code fragment only gets the user CPU percentage. To get the whole CPU percentage, you have to add this value with the system CPU. These values are easy to obtain by changing a few numeric values in the above code. This is left as an exercise for the user. Note that although in general the system CPU and nice CPU percentages are very small, this is not always the case and it is safer to add these values together than to approximate the CPU usage as user CPU only.
How to get CPU usage in Linux from Java
Related Posts:
How to Block a Port in Squid Proxy , Ubuntu LinuxHere I will show you how to block a port using squid proxy server and open this port for a selected user. First you have to open squid configurati… Read More
How to install Graphical Front End for Iptables in Ubuntu /debian Linux : Vuurmuur Vuurmuur is graphical front end for famous firewall software iptables. You can make complex firewall rules in simple steps. Vuurmuur supports traffi… Read More
How to Open, Extract and Convert DAA, ISO and BIN Files in Linux with Free PowerISO for LinuxPowerISO provides a free PowerISO for Linux utility which can extract, list, and convert image files in Linux. The image file formats supported by the… Read More
How to increase downloading Speed with Prozilla Download Accelerator for LinuxWindows Users are pretty lucky as compared to Linux and Mac users. Since for windows you can get any type of freeware application, utility and tools b… Read More
How to Extract rar files in LinuxRAR is a proprietary compression format widely used today. It’s supposedly has 30% higher compression rate when compared with WinZip. If you download … Read More
0 comments:
Post a Comment