From: Pasquale Di Rienzo <phate867@xxxxxxxxx> -fixed some bindings -reworked the CPUStat class introducing a CPUStats class which encaplulates an HashMap and is more user friendly. -added a convenience method Connection.getOverallStats() for retrieving overall stats, instead of passing -1 as cpu number for Connection.getCPUStats(int cpunum) --- src/main/java/org/libvirt/CPUStat.java | 57 -------------- src/main/java/org/libvirt/CPUStats.java | 92 ++++++++++++++++++++++ src/main/java/org/libvirt/Connect.java | 62 ++++++++------- src/main/java/org/libvirt/jna/Libvirt.java | 6 +- src/main/java/org/libvirt/jna/virNodeCPUStats.java | 10 ++- 5 files changed, 133 insertions(+), 94 deletions(-) delete mode 100644 src/main/java/org/libvirt/CPUStat.java create mode 100644 src/main/java/org/libvirt/CPUStats.java diff --git a/src/main/java/org/libvirt/CPUStat.java b/src/main/java/org/libvirt/CPUStat.java deleted file mode 100644 index 527049c..0000000 --- a/src/main/java/org/libvirt/CPUStat.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.libvirt; - -import java.nio.charset.Charset; -import org.libvirt.jna.virNodeCPUStats; -import org.libvirt.jna.virTypedParameter; - -/** - * This class holds a cpu time. - * The tag attribute is a string of either "kernel","user","idle","iowait" - * while the value attribute is the actual time value - * @author Pasquale Di Rienzo - * - */ -public class CPUStat { - public String tag; - public long value; - - private String createStringFromBytes(byte[] b){ - Charset ch = Charset.forName("UTF-8"); - int i = 0; - while ((i<b.length) && (b[i]!=0)) - i++; - - return new String(b,0,i,ch); - } - - public CPUStat(virNodeCPUStats stat){ - tag = createStringFromBytes(stat.field); - value = stat.value.longValue(); - } - - public CPUStat(virTypedParameter stat){ - tag = createStringFromBytes(stat.field); - value = stat.value.l; - } - - public String getTag() { - return tag; - } - - public long getValue() { - return value; - } - - public void setTag(String tag) { - this.tag = tag; - } - - public void setValue(long val) { - this.value = val; - } - - @Override - public String toString() { - return String.format("tag:%s%nval:%d%n", tag, value); - } -} diff --git a/src/main/java/org/libvirt/CPUStats.java b/src/main/java/org/libvirt/CPUStats.java new file mode 100644 index 0000000..71e3b33 --- /dev/null +++ b/src/main/java/org/libvirt/CPUStats.java @@ -0,0 +1,92 @@ +package org.libvirt; + +import java.util.HashMap; +import java.util.Map; + +import org.libvirt.jna.virNodeCPUStats; + +import com.sun.jna.Native; + +/** + * This class holds a cpu time. + * The tag attribute is a string of either "kernel","user","idle","iowait" + * while the value attribute is the actual time value + * @author Pasquale Di Rienzo + * + */ +public class CPUStats { + public enum Type {kernel, user, idle, iowait, intr, utilization}; + private HashMap<Type,Long> values; + + CPUStats(){ + values = new HashMap<Type,Long>(); + } + + void putStat(virNodeCPUStats stat){ + Type t = Type.valueOf(Native.toString(stat.field, "UTF-8")); + values.put(t, stat.value); + } + + /** + * @return the kernel time if present, null otherwise + */ + public Long getKernelTime(){ + return values.get(Type.kernel); + } + + /** + * @return the user time if present, null otherwise + */ + public Long getUserTime(){ + return values.get(Type.user); + } + + /** + * @return the idle time if present, null otherwise + */ + public Long getIdleTime(){ + return values.get(Type.idle); + } + + /** + * @return the I/O time if present, null otherwise + */ + public Long getIoTime(){ + return values.get(Type.iowait); + } + + /** + * @return the intr time if present, null otherwise + */ + public Long getIntrTime(){ + return values.get(Type.intr); + } + + /** + * @return the utilization time if present, null otherwise + */ + public Long getUtilizationTime(){ + return values.get(Type.utilization); + } + + /** + * Gets a generic time stat, good for iterations over the Type enum. If you need + * a specific cpu time just use the convenience methods + * + * @param stat + * @return the time stat corresponding to the passed type, null otherwise + */ + public Long getCpuTimeStat(Type stat){ + return values.get(stat); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for(Map.Entry<Type, Long> entry : values.entrySet()){ + sb.append(String.format("tag:%s%nval:%d%n", entry.getKey(), entry.getValue())); + } + + return sb.toString(); + } +} diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java index d8a4ce2..e2eb9d3 100644 --- a/src/main/java/org/libvirt/Connect.java +++ b/src/main/java/org/libvirt/Connect.java @@ -3,6 +3,7 @@ package org.libvirt; import java.util.UUID; import org.libvirt.CPUStat; +import org.libvirt.CPUStats; import org.libvirt.LibvirtException; import org.libvirt.jna.ConnectionPointer; import org.libvirt.jna.DevicePointer; @@ -219,46 +220,47 @@ public class Connect { * * Note that not all these stats are granted to be retrieved. * - * @param the number of the cpu you want to retrieve stats from. - * passing -1 will make this function retrieve a mean value - * from all cpus the system has. + * @param the number of the cpu you want to retrieve stats from. On a single core cpu pass 0 * - * @param some flags * @return a cpustat object for each cpu * @throws LibvirtException */ - public CPUStat[] getCPUStats(int cpuNumber,long flags) throws LibvirtException{ - CPUStat[] stats = null; - - IntByReference nParams = new IntByReference(); - + public CPUStats getCPUStats(int cpuNumber) throws LibvirtException{ + CPUStats stats = null; + IntByReference nParams = new IntByReference(); + //according to libvirt reference you call this function once passing - //null as param paramether to get the actual stats (kernel,user,io,idle) number into the + //null as param parameter to get the actual stats (kernel,user,io,idle) number into the //nParams reference. Generally this number would be 4, but some systems - //may not give all 4 times, so it is always good to call it. - int result = libvirt.virNodeGetCPUStats( - VCP, cpuNumber, null, nParams, flags); + //may not give all 4 times, so it is always good to call it. + int result = libvirt.virNodeGetCPUStats(VCP, cpuNumber, null, nParams, 0); + processError(result); - processError(result); + //this time we create an array to fit the number of parameters + virNodeCPUStats[] params = new virNodeCPUStats[nParams.getValue()]; - if(result == 0){//dunno if this check is actually needed - - //this time we create an array to fit the number of paramethers - virNodeCPUStats[] params = new virNodeCPUStats[nParams.getValue()]; - //and we pass it to the function - result = libvirt.virNodeGetCPUStats(VCP, cpuNumber , params, nParams, flags); - processError(result); + //and we pass it to the function + result = libvirt.virNodeGetCPUStats(VCP, cpuNumber , params, nParams, 0); + processError(result); - //finally we parse the result in an user friendly class which does - //not expose libvirt's internal paramethers - if(result >= 0){ - stats = new CPUStat[params.length]; - for(int i=0;i<params.length;i++) - stats[i] = new CPUStat(params[i]); - } - } + //finally we parse the result in an user friendly class which does + //not expose libvirt's internal parameters + stats = new CPUStats(); + for( int i=0; i<params.length; i++ ) + stats.putStat(params[i]); - return stats; + return stats; + } + + /** + * For a multiprocessor node returns a CPUStats object containing a mean value + * among each cpu, for each cpu time + * + * @return a CPUStat object containing a mean value for each cpu time + * @throws LibvirtException + */ + public CPUStats getTotalCPUStatistics() throws LibvirtException{ + return getCPUStats(-1); } /** diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java index 658299f..05c1b3f 100644 --- a/src/main/java/org/libvirt/jna/Libvirt.java +++ b/src/main/java/org/libvirt/jna/Libvirt.java @@ -106,7 +106,8 @@ public interface Libvirt extends Library { public static int VIR_UUID_BUFLEN = 16; public static int VIR_UUID_STRING_BUFLEN = (36 + 1); public static int VIR_DOMAIN_SCHED_FIELD_LENGTH = 80; - + public static int NODE_CPU_STATS_FIELD_LENGTH = 80; + // Connection Functions String virConnectBaselineCPU(ConnectionPointer virConnectPtr, String[] xmlCPUs, int ncpus, int flags); @@ -270,8 +271,7 @@ public interface Libvirt extends Library { int virNetworkUndefine(NetworkPointer virConnectPtr); // Node functions - int virNodeGetCPUStats(ConnectionPointer virConnectPtr, int cpuNum, - virNodeCPUStats[] stats,IntByReference nparams, long flags); + int virNodeGetCPUStats(ConnectionPointer virConnectPtr, int cpuNum, virNodeCPUStats[] stats, IntByReference nparams, int flags); int virNodeGetInfo(ConnectionPointer virConnectPtr, virNodeInfo virNodeInfo); int virNodeGetCellsFreeMemory(ConnectionPointer virConnectPtr, LongByReference freeMems, int startCell, int maxCells); diff --git a/src/main/java/org/libvirt/jna/virNodeCPUStats.java b/src/main/java/org/libvirt/jna/virNodeCPUStats.java index a8f2dca..7b128d5 100644 --- a/src/main/java/org/libvirt/jna/virNodeCPUStats.java +++ b/src/main/java/org/libvirt/jna/virNodeCPUStats.java @@ -3,17 +3,19 @@ package org.libvirt.jna; import java.util.Arrays; import java.util.List; +import org.libvirt.jna.Libvirt; + import com.sun.jna.NativeLong; import com.sun.jna.Structure; public class virNodeCPUStats extends Structure{ - public byte[] field = new byte[80]; - public NativeLong value ; + public byte[] field = new byte[Libvirt.NODE_CPU_STATS_FIELD_LENGTH]; + public long value ; - private static final List fields = Arrays.asList( "field", "value"); + private static final List<String> fields = Arrays.asList( "field", "value"); @Override - protected List getFieldOrder() { + protected List<String> getFieldOrder() { return fields; } } -- 1.8.3.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list