Implement native sigar_proc_cpu_get function
This commit is contained in:
1 parent
171f2e021e
commit
93ef97aaa8
10 files changed
+139
-106
No files matched your search
@@ -319,6 +319,18 @@ my %classes = (
|
||||
plat => '*'
|
||||
},
|
||||
],
|
||||
ProcCpu => [
|
||||
{
|
||||
name => 'percent', type => 'Double',
|
||||
desc => 'Process cpu usage',
|
||||
plat => '*'
|
||||
},
|
||||
{
|
||||
name => 'last_time', type => 'Long',
|
||||
desc => '',
|
||||
plat => '*'
|
||||
},
|
||||
],
|
||||
ProcState => [
|
||||
{
|
||||
name => 'state', type => 'Char',
|
||||
@@ -976,6 +988,14 @@ my %classes = (
|
||||
|
||||
$classes{DirUsage} = $classes{DirStat};
|
||||
|
||||
my(%extends) = (
|
||||
ProcCpu => 'ProcTime',
|
||||
);
|
||||
|
||||
while (my($subclass, $superclass) = each %extends) {
|
||||
push @{ $classes{$subclass} }, @{ $classes{$superclass} };
|
||||
}
|
||||
|
||||
my %cmds = (
|
||||
Mem => {
|
||||
AIX => 'top',
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.hyperic.sigar.ptql.ProcessFinder;
|
||||
*/
|
||||
public class MultiProcCpu extends ProcCpu {
|
||||
|
||||
private long pid;
|
||||
private int nproc = 0;
|
||||
private static Map ptable = new HashMap();
|
||||
|
||||
@@ -99,4 +100,19 @@ public class MultiProcCpu extends ProcCpu {
|
||||
public int getProcesses() {
|
||||
return this.nproc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Pid of the process.
|
||||
*/
|
||||
public int hashCode() {
|
||||
return (int)this.pid;
|
||||
}
|
||||
|
||||
public boolean equals(Object cpu) {
|
||||
if (!(cpu instanceof MultiProcCpu)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((MultiProcCpu)cpu).pid == this.pid;
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
|
||||
* This file is part of SIGAR.
|
||||
*
|
||||
* SIGAR is free software; you can redistribute it and/or modify
|
||||
* it under the terms version 2 of the GNU General Public License as
|
||||
* published by the Free Software Foundation. This program is distributed
|
||||
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
||||
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*/
|
||||
|
||||
package org.hyperic.sigar;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Extend ProcTime to provide process cpu percentage metric.
|
||||
*/
|
||||
public class ProcCpu extends ProcTime {
|
||||
|
||||
private static ProcCpu key = new ProcCpu();
|
||||
private static Map ptable = new HashMap();
|
||||
|
||||
protected long lastTime = 0;
|
||||
protected double percent = 0.0;
|
||||
protected long pid;
|
||||
|
||||
private void getValues(Sigar sigar, long pid)
|
||||
throws SigarException {
|
||||
this.gather(sigar, pid);
|
||||
}
|
||||
|
||||
static synchronized ProcCpu get(Sigar sigar, long pid)
|
||||
throws SigarException {
|
||||
|
||||
ProcCpu cpu;
|
||||
|
||||
key.pid = pid;
|
||||
cpu = (ProcCpu)ptable.get(key);
|
||||
|
||||
if (cpu == null) {
|
||||
cpu = new ProcCpu();
|
||||
cpu.pid = pid;
|
||||
ptable.put(cpu, cpu);
|
||||
}
|
||||
|
||||
long timeNow = System.currentTimeMillis();
|
||||
double diff = timeNow - cpu.lastTime;
|
||||
if (diff == 0) {
|
||||
return cpu; //we were just called within < 1 second ago.
|
||||
}
|
||||
|
||||
cpu.lastTime = timeNow;
|
||||
|
||||
long otime = cpu.total;
|
||||
|
||||
cpu.getValues(sigar, pid);
|
||||
|
||||
if (otime == 0) {
|
||||
//XXX could/should pause first time called.
|
||||
return cpu;
|
||||
}
|
||||
|
||||
cpu.percent = ((cpu.total - otime) / diff);
|
||||
if (cpu.percent >= 1.0) {
|
||||
cpu.percent = 0.99;
|
||||
}
|
||||
|
||||
return cpu;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Process CPU usage percentage.
|
||||
*/
|
||||
public double getPercent() {
|
||||
return this.percent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Pid of the process.
|
||||
*/
|
||||
public int hashCode() {
|
||||
return (int)this.pid;
|
||||
}
|
||||
|
||||
public boolean equals(Object cpu) {
|
||||
if (!(cpu instanceof ProcCpu)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((ProcCpu)cpu).pid == this.pid;
|
||||
}
|
||||
}
|
||||
@@ -472,7 +472,7 @@ public class Sigar implements SigarProxy {
|
||||
* @exception SigarException on failure.
|
||||
*/
|
||||
public ProcCpu getProcCpu(long pid) throws SigarException {
|
||||
return ProcCpu.get(this, pid);
|
||||
return ProcCpu.fetch(this, pid);
|
||||
}
|
||||
|
||||
public ProcCpu getProcCpu(String pid) throws SigarException {
|
||||
|
||||
@@ -58,7 +58,7 @@ public class MultiPs extends SigarCommandBase {
|
||||
MultiProcCpu cpu = this.proxy.getMultiProcCpu(query);
|
||||
println("Number of processes: " + cpu.getProcesses());
|
||||
println("Cpu usage: " + CpuPerc.format(cpu.getPercent()));
|
||||
println("Cpu time: " + Ps.getCpuTime(cpu));
|
||||
println("Cpu time: " + Ps.getCpuTime(cpu.getTotal()));
|
||||
|
||||
ProcMem mem = this.proxy.getMultiProcMem(query);
|
||||
println("Size: " + Sigar.formatSize(mem.getSize()));
|
||||
|
||||
@@ -148,11 +148,15 @@ public class Ps extends SigarCommandBase {
|
||||
println(join(getInfo(this.proxy, pid)));
|
||||
}
|
||||
|
||||
public static String getCpuTime(ProcTime time) {
|
||||
long t = time.getTotal() / 1000;
|
||||
public static String getCpuTime(long total) {
|
||||
long t = total / 1000;
|
||||
return t/60 + ":" + t%60;
|
||||
}
|
||||
|
||||
public static String getCpuTime(ProcTime time) {
|
||||
return getCpuTime(time.getTotal());
|
||||
}
|
||||
|
||||
private static String getStartTime(long time) {
|
||||
if (time == 0) {
|
||||
return "00:00";
|
||||
|
||||
Reference in new issue
Block a user