<div dir="ltr">That is if we would need to read/process these values too instead of just using them.</div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-15 10:36 GMT+02:00 Pasquale Dir <span dir="ltr"><<a href="mailto:phate867@gmail.com" target="_blank">phate867@gmail.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Working for the other corrections but I disegree with these ones:<div><br></div><div>1. <span style="font-family:arial,sans-serif;font-size:13px">This is an "unsigned long long" in C, so it must be a long, not</span></div>

<span style="font-family:arial,sans-serif;font-size:13px">NativeLong here.</span><div><span style="font-family:arial,sans-serif;font-size:13px">2.</span><span style="font-family:arial,sans-serif;font-size:13px"> </span><span style="font-family:arial,sans-serif;font-size:13px">flags is an "unsigned int" in C, so it</span></div>
<div class="">
<span style="font-family:arial,sans-serif;font-size:13px">must be an "int" in Java</span><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div></div><div><font face="arial, sans-serif">As in java types are signed I'd avoid to allocate the exact same memory, on the contrary I would allocate more space so that an high positive value for an unsigned C type would stay an high positive value in java (who would make it a negative value if space is not enough).</font></div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-14 14:22 GMT+02:00 Claudio Bley <span dir="ltr"><<a href="mailto:cbley@av-test.de" target="_blank">cbley@av-test.de</a>></span>:<div><div class="h5">
<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
At Thu, 10 Apr 2014 17:49:07 +0200,<br>
<div><div><a href="mailto:phate867@gmail.com" target="_blank">phate867@gmail.com</a> wrote:<br>
><br>
> From: Pasquale Di Rienzo <<a href="mailto:phate867@gmail.com" target="_blank">phate867@gmail.com</a>><br>
><br>
> -Added the getNodeCpuStat binding to Libvirt class<br>
> -Added virNodeCPUStats and CPUStat classes to support this binding<br>
> -Added the method getCPUStats to Connect class<br>
> ---<br>
>  AUTHORS                                            |  1 +<br>
>  src/main/java/org/libvirt/CPUStat.java             | 57 ++++++++++++++++++++++<br>
>  src/main/java/org/libvirt/Connect.java             | 53 ++++++++++++++++++++<br>
>  src/main/java/org/libvirt/jna/Libvirt.java         |  5 ++<br>
>  src/main/java/org/libvirt/jna/virNodeCPUStats.java | 19 ++++++++<br>
>  5 files changed, 135 insertions(+)<br>
>  create mode 100644 src/main/java/org/libvirt/CPUStat.java<br>
>  create mode 100644 src/main/java/org/libvirt/jna/virNodeCPUStats.java<br>
><br>
> diff --git a/AUTHORS b/AUTHORS<br>
> index 07809bf..77139e5 100644<br>
> --- a/AUTHORS<br>
> +++ b/AUTHORS<br>
> @@ -16,3 +16,4 @@ Andrea Sansottera <<a href="mailto:andrea.sansottera@gmail.com" target="_blank">andrea.sansottera@gmail.com</a>><br>
>  Stefan Majer <<a href="mailto:stefan.majer@gmail.com" target="_blank">stefan.majer@gmail.com</a>><br>
>  Wido den Hollander <<a href="mailto:wido@widodh.nl" target="_blank">wido@widodh.nl</a>><br>
>  Eric Blake <<a href="mailto:eblake@redhat.com" target="_blank">eblake@redhat.com</a>><br>
> +Pasquale Di Rienzo <<a href="mailto:phate867@gmail.com" target="_blank">phate867@gmail.com</a>><br>
> diff --git a/src/main/java/org/libvirt/CPUStat.java b/src/main/java/org/libvirt/CPUStat.java<br>
> new file mode 100644<br>
> index 0000000..527049c<br>
> --- /dev/null<br>
> +++ b/src/main/java/org/libvirt/CPUStat.java<br>
> @@ -0,0 +1,57 @@<br>
> +package org.libvirt;<br>
> +<br>
> +import java.nio.charset.Charset;<br>
> +import org.libvirt.jna.virNodeCPUStats;<br>
> +import org.libvirt.jna.virTypedParameter;<br>
> +<br>
> +/**<br>
> + * This class holds a cpu time.<br>
> + * The tag attribute is a string of either "kernel","user","idle","iowait"<br>
<br>
</div></div>Spaces after comma.<br>
<br>
But, just mentioning the possible tags in the documentation is not<br>
good enough. We should provide constants (preferably type safe enums)<br>
to the user.<br>
<br>
Furthermore, grepping through the code, there seem to be some<br>
other constants besides the ones you have mentioned:<br>
<br>
#define VIR_NODE_CPU_STATS_KERNEL "kernel"<br>
#define VIR_NODE_CPU_STATS_USER "user"<br>
#define VIR_NODE_CPU_STATS_IDLE "idle"<br>
#define VIR_NODE_CPU_STATS_IOWAIT "iowait"<br>
#define VIR_NODE_CPU_STATS_INTR "intr"<br>
#define VIR_NODE_CPU_STATS_UTILIZATION "utilization"<br>
<br>
>From a users perspective, I think it would be easier to provide an API<br>
like:<br>
<br>
NodeCPUStatistics stats = connection.getNodeCPUStatistics(cpuNum);<br>
<br>
stats.kernelTime()<br>
stats.userTime()<br>
...<br>
<br>
where those methods return a special value if the time is not present,<br>
e.g. "null".<br>
<br>
For upwards compatibility we might provide a getter having a String<br>
parameter to retrieve yet unknown stats. But, I don't anticipate any<br>
additions to the available node CPU stats that often or even at all.<br>
<br>
FWIW, I'd prefer not use use a special parameter value to retrieve<br>
the total node CPU stats, but use a telling method name, like<br>
getTotalCPUStatistics().<br>
<div><br>
> + * while the value attribute is the actual time value<br>
> + * @author Pasquale Di Rienzo<br>
> + *<br>
> + */<br>
> +public class CPUStat {<br>
> +     public String tag;<br>
> +    public long value;<br>
> +<br>
<br>
</div>Wrong indentation. Don't use tabs for indentation.<br>
<div><br>
> +    private String createStringFromBytes(byte[] b){<br>
> +     Charset ch = Charset.forName("UTF-8");<br>
> +     int i = 0;<br>
> +     while ((i<b.length) && (b[i]!=0))<br>
> +             i++;<br>
> +<br>
> +     return new String(b,0,i,ch);<br>
> +    }<br>
<br>
</div>Just use the Native.toString(byte[] b, String enc) method for this<br>
conversion instead of open coding it here.<br>
<br>
I'll look into making such a method available in the Library class.<br>
<br>
> +    public CPUStat(virNodeCPUStats stat){<br>
<br>
Make this constructor package-private.<br>
<div><br>
> +     tag = createStringFromBytes(stat.field);<br>
> +     value = stat.value.longValue();<br>
> +    }<br>
> +<br>
> +    public CPUStat(virTypedParameter stat){<br>
> +     tag = createStringFromBytes(stat.field);<br>
> +             value = stat.value.l;<br>
> +    }<br>
<br>
</div>What's virTypedParameter? You don't define that class in this patch.<br>
<div><div><br>
> +    public String getTag() {<br>
> +        return tag;<br>
> +    }<br>
> +<br>
> +    public long getValue() {<br>
> +        return value;<br>
> +    }<br>
> +<br>
> +    public void setTag(String tag) {<br>
> +        this.tag = tag;<br>
> +    }<br>
> +<br>
> +    public void setValue(long val) {<br>
> +        this.value = val;<br>
> +    }<br>
> +<br>
> +    @Override<br>
> +    public String toString() {<br>
> +        return String.format("tag:%s%nval:%d%n", tag, value);<br>
> +    }<br>
> +}<br>
> diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java<br>
> index fedc60e..d8a4ce2 100644<br>
> --- a/src/main/java/org/libvirt/Connect.java<br>
> +++ b/src/main/java/org/libvirt/Connect.java<br>
> @@ -2,6 +2,8 @@ package org.libvirt;<br>
><br>
>  import java.util.UUID;<br>
><br>
> +import org.libvirt.CPUStat;<br>
> +import org.libvirt.LibvirtException;<br>
>  import org.libvirt.jna.ConnectionPointer;<br>
>  import org.libvirt.jna.DevicePointer;<br>
>  import org.libvirt.jna.DomainPointer;<br>
> @@ -14,6 +16,7 @@ import org.libvirt.jna.StoragePoolPointer;<br>
>  import org.libvirt.jna.StorageVolPointer;<br>
>  import org.libvirt.jna.StreamPointer;<br>
>  import org.libvirt.jna.virConnectAuth;<br>
> +import org.libvirt.jna.virNodeCPUStats;<br>
>  import org.libvirt.jna.virNodeInfo;<br>
><br>
>  import static org.libvirt.Library.libvirt;<br>
> @@ -23,6 +26,7 @@ import static org.libvirt.ErrorHandler.processErrorIfZero;<br>
>  import com.sun.jna.Memory;<br>
>  import com.sun.jna.NativeLong;<br>
>  import com.sun.jna.Pointer;<br>
> +import com.sun.jna.ptr.IntByReference;<br>
>  import com.sun.jna.ptr.LongByReference;<br>
><br>
>  /**<br>
> @@ -207,6 +211,55 @@ public class Connect {<br>
>          }<br>
>          return processError(success);<br>
>      }<br>
> +<br>
> +    /**<br>
> +     * This function returns statistics about the cpu, that is the time<br>
> +     * each cpu is spending in kernel time, user time, io time and idle time.<br>
> +     * Each CPUStat object refers to a particular time.<br>
> +     *<br>
> +     * Note that not all these stats are granted to be retrieved.<br>
> +     *<br>
> +     * @param the number of the cpu you want to retrieve stats from.<br>
> +     * passing -1 will make this function retrieve a mean value<br>
> +     * from all cpus the system has.<br>
> +     *<br>
> +     * @param some flags<br>
<br>
</div></div>As per my comment for the last review, remove the flags parameter.<br>
<div><br>
> +     * @return a cpustat object for each cpu<br>
> +     * @throws LibvirtException<br>
> +     */<br>
> +    public CPUStat[] getCPUStats(int cpuNumber,long flags) throws LibvirtException{<br>
> +     CPUStat[] stats = null;<br>
> +<br>
> +     IntByReference nParams = new IntByReference();<br>
> +<br>
> +     //according to libvirt reference you call this function once passing<br>
> +     //null as param paramether to get the actual stats (kernel,user,io,idle) number into the<br>
> +     //nParams reference. Generally this number would be 4, but some systems<br>
> +     //may not give all 4 times, so it is always good to call it.<br>
<br>
</div>Insert a space after //<br>
<br>
s/paramethers/parameters/<br>
<div><br>
> +     int result = libvirt.virNodeGetCPUStats(<br>
> +                     VCP, cpuNumber, null, nParams, flags);<br>
> +<br>
> +     processError(result);<br>
> +<br>
> +     if(result == 0){//dunno if this check is actually needed<br>
<br>
</div>The check is unnecessary since processError will throw an exception if<br>
result == -1.<br>
<div><br>
> +             //this time we create an array to fit the number of paramethers<br>
<br>
</div>s/paramethers/parameters/<br>
<div><br>
> +             virNodeCPUStats[] params = new virNodeCPUStats[nParams.getValue()];<br>
> +             //and we pass it to the function<br>
> +             result = libvirt.virNodeGetCPUStats(VCP, cpuNumber , params, nParams, flags);<br>
> +             processError(result);<br>
> +<br>
> +             //finally we parse the result in an user friendly class which does<br>
> +             //not expose libvirt's internal paramethers<br>
<br>
</div>Likewise.<br>
<br>
> +             if(result >= 0){<br>
<br>
Space between "if" and "(" and between ")" and "{"<br>
<div><br>
> +                     stats = new CPUStat[params.length];<br>
> +                     for(int i=0;i<params.length;i++)<br>
<br>
</div>Spaces after "for" and after semicolon<br>
<div><br>
> +                                     stats[i] = new CPUStat(params[i]);<br>
> +             }<br>
> +     }<br>
> +<br>
> +     return stats;<br>
> +    }<br>
><br>
<br>
</div>Lots of trailing spaces here.<br>
<div><br>
>      /**<br>
>       * Compares the given CPU description with the host CPU<br>
> diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java<br>
> index 0e4c9fc..658299f 100644<br>
> --- a/src/main/java/org/libvirt/jna/Libvirt.java<br>
> +++ b/src/main/java/org/libvirt/jna/Libvirt.java<br>
> @@ -1,5 +1,8 @@<br>
>  package org.libvirt.jna;<br>
><br>
> +import org.libvirt.jna.ConnectionPointer;<br>
> +import org.libvirt.jna.virNodeCPUStats;<br>
> +<br>
>  import com.sun.jna.Callback;<br>
>  import com.sun.jna.Library;<br>
>  import com.sun.jna.Native;<br>
> @@ -267,6 +270,8 @@ public interface Libvirt extends Library {<br>
>      int virNetworkUndefine(NetworkPointer virConnectPtr);<br>
><br>
>      // Node functions<br>
> +    int virNodeGetCPUStats(ConnectionPointer virConnectPtr, int cpuNum,<br>
> +             virNodeCPUStats[] stats,IntByReference nparams, long flags);<br>
<br>
</div>Replace the tab with spaces. flags is an "unsigned int" in C, so it<br>
must be an "int" in Java.<br>
<div><br>
>      int virNodeGetInfo(ConnectionPointer virConnectPtr, virNodeInfo virNodeInfo);<br>
>      int virNodeGetCellsFreeMemory(ConnectionPointer virConnectPtr, LongByReference freeMems, int startCell,<br>
>              int maxCells);<br>
> diff --git a/src/main/java/org/libvirt/jna/virNodeCPUStats.java b/src/main/java/org/libvirt/jna/virNodeCPUStats.java<br>
> new file mode 100644<br>
> index 0000000..a8f2dca<br>
> --- /dev/null<br>
> +++ b/src/main/java/org/libvirt/jna/virNodeCPUStats.java<br>
> @@ -0,0 +1,19 @@<br>
> +package org.libvirt.jna;<br>
> +<br>
> +import java.util.Arrays;<br>
> +import java.util.List;<br>
> +<br>
> +import com.sun.jna.NativeLong;<br>
> +import com.sun.jna.Structure;<br>
> +<br>
> +public class virNodeCPUStats extends Structure{<br>
> +     public byte[] field = new byte[80];<br>
<br>
</div>It would be better to declare a constant instead of using magic numbers<br>
here => NODE_CPU_STATS_FIELD_LENGTH<br>
<br>
> +    public NativeLong value ;<br>
<br>
This is an "unsigned long long" in C, so it must be a long, not<br>
NativeLong here.<br>
<div><br>
> +    private static final List fields = Arrays.asList( "field", "value");<br>
<br>
</div>Spurious space after opening paren.<br>
<br>
Use a generic type to avoid compiler warnings (cf. 94ec3dc0b78b)<br>
<div><br>
> +    @Override<br>
> +    protected List getFieldOrder() {<br>
> +        return fields;<br>
> +    }<br>
> +}<br>
<br>
</div>Likewise.<br>
<br>
Regards,<br>
Claudio<br>
</blockquote></div></div></div><br></div>
</blockquote></div><br></div>