[libvirt] [java] [PATCH 1/2] GetNodeCpuStat binding

From: Pasquale Di Rienzo <phate867@gmail.com> -Added the getNodeCpuStat binding to Libvirt class -Added virNodeCPUStats and CPUStat classes to support this binding -Added the method getCPUStats to Connect class --- AUTHORS | 1 + src/main/java/org/libvirt/CPUStat.java | 57 ++++++++++++++++++++++ src/main/java/org/libvirt/Connect.java | 53 ++++++++++++++++++++ src/main/java/org/libvirt/jna/Libvirt.java | 5 ++ src/main/java/org/libvirt/jna/virNodeCPUStats.java | 19 ++++++++ 5 files changed, 135 insertions(+) create mode 100644 src/main/java/org/libvirt/CPUStat.java create mode 100644 src/main/java/org/libvirt/jna/virNodeCPUStats.java diff --git a/AUTHORS b/AUTHORS index 07809bf..77139e5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -16,3 +16,4 @@ Andrea Sansottera <andrea.sansottera@gmail.com> Stefan Majer <stefan.majer@gmail.com> Wido den Hollander <wido@widodh.nl> Eric Blake <eblake@redhat.com> +Pasquale Di Rienzo <phate867@gmail.com> diff --git a/src/main/java/org/libvirt/CPUStat.java b/src/main/java/org/libvirt/CPUStat.java new file mode 100644 index 0000000..527049c --- /dev/null +++ b/src/main/java/org/libvirt/CPUStat.java @@ -0,0 +1,57 @@ +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/Connect.java b/src/main/java/org/libvirt/Connect.java index fedc60e..d8a4ce2 100644 --- a/src/main/java/org/libvirt/Connect.java +++ b/src/main/java/org/libvirt/Connect.java @@ -2,6 +2,8 @@ package org.libvirt; import java.util.UUID; +import org.libvirt.CPUStat; +import org.libvirt.LibvirtException; import org.libvirt.jna.ConnectionPointer; import org.libvirt.jna.DevicePointer; import org.libvirt.jna.DomainPointer; @@ -14,6 +16,7 @@ import org.libvirt.jna.StoragePoolPointer; import org.libvirt.jna.StorageVolPointer; import org.libvirt.jna.StreamPointer; import org.libvirt.jna.virConnectAuth; +import org.libvirt.jna.virNodeCPUStats; import org.libvirt.jna.virNodeInfo; import static org.libvirt.Library.libvirt; @@ -23,6 +26,7 @@ import static org.libvirt.ErrorHandler.processErrorIfZero; import com.sun.jna.Memory; import com.sun.jna.NativeLong; import com.sun.jna.Pointer; +import com.sun.jna.ptr.IntByReference; import com.sun.jna.ptr.LongByReference; /** @@ -207,6 +211,55 @@ public class Connect { } return processError(success); } + + /** + * This function returns statistics about the cpu, that is the time + * each cpu is spending in kernel time, user time, io time and idle time. + * Each CPUStat object refers to a particular time. + * + * 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 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(); + + //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 + //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); + + processError(result); + + 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); + + //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]); + } + } + + return stats; + } /** * Compares the given CPU description with the host CPU diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java index 0e4c9fc..658299f 100644 --- a/src/main/java/org/libvirt/jna/Libvirt.java +++ b/src/main/java/org/libvirt/jna/Libvirt.java @@ -1,5 +1,8 @@ package org.libvirt.jna; +import org.libvirt.jna.ConnectionPointer; +import org.libvirt.jna.virNodeCPUStats; + import com.sun.jna.Callback; import com.sun.jna.Library; import com.sun.jna.Native; @@ -267,6 +270,8 @@ public interface Libvirt extends Library { int virNetworkUndefine(NetworkPointer virConnectPtr); // Node functions + int virNodeGetCPUStats(ConnectionPointer virConnectPtr, int cpuNum, + virNodeCPUStats[] stats,IntByReference nparams, long 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 new file mode 100644 index 0000000..a8f2dca --- /dev/null +++ b/src/main/java/org/libvirt/jna/virNodeCPUStats.java @@ -0,0 +1,19 @@ +package org.libvirt.jna; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.NativeLong; +import com.sun.jna.Structure; + +public class virNodeCPUStats extends Structure{ + public byte[] field = new byte[80]; + public NativeLong value ; + + private static final List fields = Arrays.asList( "field", "value"); + + @Override + protected List getFieldOrder() { + return fields; + } +} -- 1.8.3.2

From: Pasquale Di Rienzo <phate867@gmail.com> -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
participants (1)
-
Pasquale.phate867@gmail.com