こんにちは私はCPU負荷を取得しようとしていますが、%でCPU負荷を取得したいと思います。私はSigar APIを使用するより良い事前正確かつ簡単な方法でパーセントのLinux LinuxをCPUにロードする方法は?
2
A
答えて
2
その中にお返事を
感謝をネット
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
method.setAccessible(true);
if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
Object value;
try {
value = method.invoke(operatingSystemMXBean);
} catch (Exception e) {
value = e;
} // try
System.out.print(method.getName() + " = " + value);
期待を使っ府、このコードを試してみて最も簡単な方法は、それを得るために何を以下のように私は私のコードを持っています異なるメトリックを抽出するために使用することができます。私も自分のアプリケーションのためにこれを使用している、あなたは、タイマーを作成し、以下のリンク
1
を参照し、毎秒すべてのスレッドのCPU時間の合計を取ることができます。
long cpuTime = 0;
for (long id : ManagementFactory.getThreadMXBean().getAllThreadIds())
{
cpuTime += ManagementFactory.getThreadMXBean().getThreadCpuTime (id);
}
CPUパーセンテージは、最後と現在の秒の間の相対的なCPU時間をタイムスタンプの差で割った値になります。ここで
はCpuStats
クラスの簡単な実装例です:
public class CpuStats
{
private final long threadId;
private long lastCpuTime = 0;
private long lastPoll = 0;
/**
* Creates a CpuStats object for a single thread.
* @param threadId The id of the thread to monitor
*
*/
public CpuStats (long threadId)
{
this.threadId = threadId;
lastCpuTime = getTotalTime();
lastPoll = System.nanoTime();
}
/**
* Creates a CpuStatus object for all threads. The supplied statistics affect
* all threads in the current VM.
*/
public CpuStats()
{
threadId = -1;
lastCpuTime = getTotalTime();
lastPoll = System.nanoTime();
}
private long getRelativeTime()
{
long currentCpuTime = getTotalTime();
long ret = currentCpuTime - lastCpuTime;
lastCpuTime = currentCpuTime;
return ret;
}
public double getUsage()
{
long timeBefore = this.lastPoll;
lastPoll = System.nanoTime();
long relTime = getRelativeTime();
return Math.max ((double)relTime/(double)(lastPoll - timeBefore), 0.0);
}
private long getTotalTime()
{
if (threadId == -1)
{
long cpuTime = 0;
for (long id : ManagementFactory.getThreadMXBean().getAllThreadIds())
{
cpuTime += ManagementFactory.getThreadMXBean().getThreadCpuTime (id);
}
return cpuTime;
}
else
{
return ManagementFactory.getThreadMXBean().getThreadCpuTime (threadId);
}
}
}
ただ、定期的にgetUsage()
を取得します。
0
あなたはこのクラスを使用することができます。その後、
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
public class PerformanceMonitor {
static long lastSystemTime = 0;
static long lastProcessCpuTime = 0;
static int availableProcessors = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
public synchronized double getCpuUsage()
{
if (lastSystemTime == 0)
{
baselineCounters();
// return ;
}
long systemTime = System.nanoTime();
long processCpuTime = 0;
if (ManagementFactory.getOperatingSystemMXBean() instanceof com.sun.management.OperatingSystemMXBean)
{
processCpuTime = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
}
double cpuUsage = (double) (processCpuTime - lastProcessCpuTime)/(systemTime - lastSystemTime)*100.0;
lastSystemTime = systemTime;
lastProcessCpuTime = processCpuTime;
return cpuUsage/availableProcessors;
}
private void baselineCounters()
{
lastSystemTime = System.nanoTime();
if (ManagementFactory.getOperatingSystemMXBean() instanceof com.sun.management.OperatingSystemMXBean)
{
lastProcessCpuTime = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
}
}
}
およびコール:
public class Main {
public static PerformanceMonitor monitor = null;
public static void main(String[] args) {
monitor = new PerformanceMonitor();
for(int i=0 ; i<10000 ; i++){
start();
double usage = monitor.getCpuUsage();
if(usage!=0)System.out.println("Current CPU usage in pourcentage : "+usage);
}
}
private static void start() {
int count=0;
for(int i=0 ; i<100000 ; i++){
count=(int) Math.random()*100;
}
}
}
また、他の監視方法を見てみることができ、それが助けいただければ幸いです!
EDIT:(新しいパフォーマンスモニタ)
1
このコードmpstat
を使用して、それが私に正確な結果を与えるだろう
import java.io.*;
public class CpuLoad {
public static void main(String args[]) {
int i=1;
float finalres;
try{
// execute the linux command
Process p=Runtime.getRuntime().exec("mpstat");
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=null;
//read the row corresponding to cpu idle
while((line=in.readLine())!=null && i<4){
i++;
}
String res=line.substring(line.length()-5);
finalres=Float.parseFloat(res);
//convert the idle to cpuload
System.out.println("CPU load:"+(100-finalres)+"%");
}
catch(Exception e){
System.out.println(e);
}
}
}
関連する問題
- 1. LinuxサーバーでC#コードを簡単に実行する方法は?
- 2. Linuxで正しいSSIDパスワードを確認する方法
- 3. linux X11バージョンの確認方法は?
- 4. Zend Framework 2でACLをロードする簡単な方法は?
- 5. Linux上のPythonアプリケーションにSWFコンテンツをロードする方法は?
- 6. LinuxプロセスのRAMとCPU使用率を記録する方法
- 7. GNU/Linuxの世界で最新のGUIアプリケーションを作成する最も簡単な方法は何ですか?
- 8. WindowsのインテルCPU I/Oレジスタに簡単にアクセスする方法
- 9. LinuxマシンでPython 3のキー入力を検出する最も簡単な方法は何ですか?
- 10. Linuxでjavaビットのバージョンを確認する方法は?
- 11. 高いSoftIRQ(si)CPUを簡単にする方法
- 12. Linux上でC++/Qtのスレッドを起動して忘れる最も簡単な方法は何ですか?
- 13. C/Linuxで画像を表示する最も簡単な方法は何ですか?
- 14. どのような方法は、Linuxで
- 15. (簡単な方法)
- 16. 簡単な方法
- 17. 簡単な方法
- 18. 簡単な方法
- 19. このheirarchicalテーブルをフラットテーブルに非正規化する最も簡単で簡単な方法は何ですか?
- 20. 簡単な方法は
- 21. 簡単な方法は、
- 22. Linuxでイーサネットをチェックする方法は?
- 23. Linuxでマルチスレッドハングプロセスをデバッグする方法は?
- 24. ASP.NET MVC3では、通貨とパーセント値をモデルにバインドする最も簡単な方法は何ですか?
- 25. Linuxのファイルアクセス方法
- 26. Linux(ubuntu)マシンからiSeries上のDB2データベースにアクセスする最も簡単な方法は?
- 27. 遅延ロードの単体テストテストの簡単な方法
- 28. 最大のCPU負荷をシミュレートする最も簡単な方法は?
- 29. jsonで簡単に印刷する方法簡単なJavaライブラリ
- 30. ndk(linux)のインストール方法は?
溶液であってもよいです? –
はい、私は他の比較ベースを持っていないと思いますが、私はメモリ管理と結果を使用して、JVMとの比較でかなり正確な例を追加します。それはおそらくCPU使用量も良いです –
しかし、私はこのコードを実行した後に任意の出力を取得していない??? –