[PATCH V4 1/5] Add new public API virDomainGetCPUStats()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



add new API virDomainGetCPUStats() for getting cpu accounting information
per real cpus which is used by a domain.

based on ideas by Lai Jiangshan and Eric Blake.
Proposed API is a bit morified to be able to return max cpu ID.
(max cpu ID !=  cpu num.)

* src/libvirt_public.syms: add API for LIBVIRT_0.9.10
* src/libvirt.c: define virDomainGetCPUStats()
* include/libvirt/libvirt.h.in: add virDomainGetCPUStats() header
* src/driver.h: add driver API
* python/generator.py: add python API (as not implemented)
---
 include/libvirt/libvirt.h.in |    6 ++
 python/generator.py          |    1 +
 src/driver.h                 |    8 +++
 src/libvirt.c                |  136 ++++++++++++++++++++++++++++++++++++++++++
 src/libvirt_public.syms      |    1 +
 5 files changed, 152 insertions(+), 0 deletions(-)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index e99cd00..e8b5116 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -3797,6 +3797,12 @@ int virConnectSetKeepAlive(virConnectPtr conn,
                            int interval,
                            unsigned int count);
 
+int virDomainGetCPUStats(virDomainPtr domain,
+                         virTypedParameterPtr params,
+                         unsigned int nparams,
+                         int start_cpu,
+                         unsigned int ncpus,
+                         unsigned int flags);
 #ifdef __cplusplus
 }
 #endif
diff --git a/python/generator.py b/python/generator.py
index de635dc..9c74427 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -422,6 +422,7 @@ skip_impl = (
     'virDomainGetBlockIoTune',
     'virDomainSetInterfaceParameters',
     'virDomainGetInterfaceParameters',
+    'virDomainGetCPUStats'  # not implemented now.
 )
 
 qemu_skip_impl = (
diff --git a/src/driver.h b/src/driver.h
index df2aa60..0e52770 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -797,6 +797,13 @@ typedef int
     (*virDrvDomainShutdownFlags)(virDomainPtr domain,
                                  unsigned int flags);
 
+typedef int
+    (*virDrvDomainGetCPUStats)(virDomainPtr domain,
+                               virTypedParameterPtr params,
+                               unsigned int nparams,
+                               int start_cpu,
+                               unsigned int ncpus,
+                               unsigned int flags);
 
 /**
  * _virDriver:
@@ -967,6 +974,7 @@ struct _virDriver {
     virDrvNodeSuspendForDuration nodeSuspendForDuration;
     virDrvDomainSetBlockIoTune domainSetBlockIoTune;
     virDrvDomainGetBlockIoTune domainGetBlockIoTune;
+    virDrvDomainGetCPUStats domainGetCPUStats;
 };
 
 typedef int
diff --git a/src/libvirt.c b/src/libvirt.c
index e9d638b..8983f27 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -18017,3 +18017,139 @@ error:
     virDispatchError(dom->conn);
     return -1;
 }
+
+/**
+ * virDomainGetCPUStats:
+ * @domain: domain to query
+ * @params: array to populate on output
+ * @nparams: number of parameters per cpu
+ * @start_cpu: which cpu to start with, or -1 for summary
+ * @ncpus: how many cpus to query
+ * @flags: unused for now
+ *
+ * Get statistics relating to CPU usage attributable to a single
+ * domain (in contrast to the statistics returned by
+ * virNodeGetCPUStats() for all processes on the host).  @dom
+ * must be running (an inactive domain has no attributable cpu
+ * usage).  On input, @params must contain at least @nparams * @ncpus
+ * entries, allocated by the caller.
+ *
+ * Now, @ncpus is limited to be <= 128. If you want to get
+ * values in a host with more cpus, you need to call multiple times.
+ *
+ * @nparams are limited to be <= 16 but maximum params per cpu
+ * provided by will be smaller than this.
+ *
+ *
+ * If @start_cpu is -1, then @ncpus must be 1, and the returned
+ * results reflect the statistics attributable to the entire
+ * domain (such as user and system time for the process as a
+ * whole).  Otherwise, @start_cpu represents which cpu to start
+ * with, and @ncpus represents how many consecutive processors to
+ * query, with statistics attributable per processor (such as
+ * per-cpu usage).
+ *
+ * As a special case, if @params is NULL and @nparams is 0 and
+ * @ncpus is 1, and the return value will be how many
+ * statistics are available for the given @start_cpu.  This number
+ * may be different for @start_cpu of -1 than for any non-negative
+ * value, but will be the same for all non-negative @start_cpu.
+ *
+ * And, if @param is NULL and @ncparam is 0 and @ncpus is 0,
+ * Max cpu id of the node is returned. (considering cpu hotplug,
+ * max cpu id may be different from the number of cpu on a host.)
+ *
+ * For now, @flags is unused, and the statistics all relate to the
+ * usage from the host perspective; the number of cpus available to
+ * query can be determined by the cpus member of the result of
+ * virNodeGetInfo(), even if the domain has had vcpus pinned to only
+ * use a subset of overall host cpus.  It is possible that a future
+ * version will support a flag that queries the cpu usage from the
+ * guest's perspective, using the number of vcpus available to the
+ * guest, found by virDomainGetVcpusFlags().  An individual guest
+ * vcpu cannot be reliably mapped back to a specific host cpu unless
+ * a single-processor vcpu pinning was used, but when @start_cpu is -1,
+ * any difference in usage between a host and guest perspective would
+ * serve as a measure of hypervisor overhead.
+ *
+ * Returns -1 on failure, or the number of statistics that were
+ * populated per cpu on success (this will be less than the total
+ * number of populated @params, unless @ncpus was 1; and may be
+ * less than @nparams).  The populated parameters start at each
+ * stride of @nparams; any unpopulated parameters will be zeroed
+ * on success.  The caller is responsible for freeing any returned
+ * string parameters.
+ *
+ * Note:
+ * Because cpu ids may be discontig, retuned @param array may contain
+ * zero-filled entry in the middle.
+ * All time related values are represented in ns, using UULONG.
+ *
+ * Typical use sequence is below.
+ *
+ * getting total stats: set start_cpu as -1, ncpus 1
+ * virDomainGetCPUStats(dom, NULL, 0, -1, 1, 0) => nparams
+ * VIR_ALLOC_N(params, nparams)
+ * virDomainGetCPUStats(dom, parasm, nparams, -1, 1, 0) => total stats.
+ *
+ * getting percpu stats:
+ * virDomainGetCPUStats(dom, NULL, 0, 0, 0, 0) => max_cpu_id
+ * virDomainGetCPUStats(dom, NULL, 0, 0, 1, 0) => nparams
+ * VIR_ALLOC_N(params, max_cpu_id * nparams)
+ * virDomainGetCPUStats(dom, params, nparams, 0, max_cpu_id, 0) => percpu stats
+ *
+ */
+
+int virDomainGetCPUStats(virDomainPtr domain,
+                         virTypedParameterPtr params,
+                         unsigned int nparams,
+                         int start_cpu,
+                         unsigned int ncpus,
+                         unsigned int flags)
+{
+    virConnectPtr conn;
+
+    VIR_DOMAIN_DEBUG(domain, "nparams=%d, start_cpu=%d, ncpu=%d, flags=%x",
+                     nparams, start_cpu, ncpus, flags);
+    virResetLastError();
+
+    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
+        virLibDomainError(VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    conn = domain->conn;
+    /* start_cpu == -1 is a special case. ncpus must be 1 */
+    if ((start_cpu == -1) && (ncpus != 1)) {
+        virLibDomainError(VIR_ERR_INVALID_ARG, __FUNCTION__);
+        goto error;
+    }
+    /* if params == NULL, nparams must be 0 */
+    if ((params == NULL) && ((nparams != 0))) {
+        virLibDomainError(VIR_ERR_INVALID_ARG, __FUNCTION__);
+        goto error;
+    }
+
+    /* remote protocol doesn't welcome big args in one shot */
+    if ((nparams > 16) || (ncpus > 128)) {
+        virLibDomainError(VIR_ERR_INVALID_ARG, __FUNCTION__);
+        goto error;
+    }
+
+    if (conn->driver->domainGetCPUStats) {
+        int ret;
+
+        ret = conn->driver->domainGetCPUStats(domain, params, nparams,
+                                              start_cpu, ncpus, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibDomainError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(domain->conn);
+    return -1;
+}
diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 1340b0c..7f9c5ab 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -520,6 +520,7 @@ LIBVIRT_0.9.10 {
     global:
         virDomainShutdownFlags;
         virStorageVolWipePattern;
+        virDomainGetCPUStats;
 } LIBVIRT_0.9.9;
 
 # .... define new API here using predicted next version number ....
-- 
1.7.4.1


--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]