I am trying to collect performance metrics for CPU using the below code. the code is running without error but the performance metrics values i got from the code is completely different when it is compared with the values from top command
Code: Select all
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.virtualbox_5_0.Holder;
import org.virtualbox_5_0.IMachine;
import org.virtualbox_5_0.IPerformanceCollector;
import org.virtualbox_5_0.IUnknown;
import org.virtualbox_5_0.IVirtualBox;
import org.virtualbox_5_0.VBoxException;
import org.virtualbox_5_0.VirtualBoxManager;
public class VmIPerformanceCollector {
IVirtualBox vbox;
IMachine machine;
IPerformanceCollector oCollector;
VirtualBoxManager mgr;
String machinename;
long scale;
double Cpuutilization;
public VmIPerformanceCollector(String machinename){
this.machinename=machinename;
}
@SuppressWarnings("finally")
public boolean ConnecttoVirtualbox(){
boolean connectsucess=false;
mgr = VirtualBoxManager.createInstance(null);
try{
mgr.connect(vm.getVboxsrvUrl(), null, null);
vbox=mgr.getVBox();
machine=vbox.findMachine(machinename);
}catch (VBoxException e){
e.printStackTrace();
}finally{
return connectsucess;
}
}
public void setup(long Period, long Count){
oCollector = vbox.getPerformanceCollector();
oCollector.setupMetrics(Arrays.asList(new String[]{"CPU/*:avg"}),
new ArrayList<IUnknown>(),
Period, Count);
}
public void CollectCpuUtilization(long time) throws InterruptedException{
Holder<List<String>> names = new Holder<List<String>> ();
Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
Holder<List<String>> units = new Holder<List<String>>();
Holder<List<Long>> scales = new Holder<List<Long>>();
Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
Holder<List<Long>> indices = new Holder<List<Long>>();
Holder<List<Long>> lengths = new Holder<List<Long>>();
oCollector = vbox.getPerformanceCollector();
List<Integer> values = oCollector.queryMetricsData(Arrays.asList(new String[]{"CPU/*:avg"}),
Arrays.asList(new IUnknown[]{machine}),
names, objects, units, scales,
sequenceNumbers, indices, lengths);
Cpuutilization=0;
System.out.println();
for (int i=0; i < names.value.size(); i++){
scale=scales.value.get(i);
System.out.println(names.value.get(i));
for (int j = 0; j < lengths.value.get(i); j++)
{
long val= values.get(indices.value.get(i).intValue() + j).longValue();
System.out.println(val);
Long val2=val;
}
}
System.out.println("------------------");
disconnect();
}
public void disconnect(){
try {
mgr.disconnect();
} catch (VBoxException e) {
e.printStackTrace();
safeprinterr.safeprintErr(e.getMessage());
safePrintln.safeprintln("Cannot disconnect, start webserver first!");
}
mgr.cleanup();
}
}
Code: Select all
public class testcpuperformance {
public static void main(String[] args) throws InterruptedException{
String VmName="VmLab11";
VmIPerformanceCollector vmper=new VmIPerformanceCollector(VmName);
vmper.ConnecttoVirtualbox();
vmper.setup(new Long(1), new Long(3));
vmper.disconnect();
for(int i=1; i<100; i++){
Thread.sleep(2000);
vmper.ConnecttoVirtualbox();
vmper.CollectCpuUtilization(3);
}
}
}
Code: Select all
CPU/Load/User
250
375
CPU/Load/Kernel
250
250
------------------
CPU/Load/User
375
250
125
CPU/Load/Kernel
250
125
250
------------------
i am using
Centos 6.7
Virtualbox 5.0.12
Language Java (jdk 1.7)
any help will be appreciated