[libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats

Eli Qiao posted 12 patches 8 years, 11 months ago
There is a newer version of this series
[libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats
Posted by Eli Qiao 8 years, 11 months ago
Add new virsh command line `nodecachestats` to expose the cache usage
on a node.

Signed-off-by: Eli Qiao <liyong.qiao@intel.com>
---
 src/libvirt_private.syms |  3 ++-
 src/qemu/qemu_driver.c   | 12 ++++++++++
 src/util/virresctrl.c    | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virresctrl.h    |  8 +++++++
 tools/virsh-host.c       | 49 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 85b9e94..bb285a9 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2316,13 +2316,14 @@ virRandomInt;
 
 # util/virresctrl.h
 virResCtrlAvailable;
+virResCtrlCacheGetStats;
 virResCtrlGet;
 virResCtrlInit;
 virResCtrlSetCacheBanks;
 virResCtrlTypeFromString;
 virResCtrlTypeToString;
 virResCtrlUpdate;
-#
+
 
 # util/virrotatingfile.h
 virRotatingFileReaderConsume;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 1e3ed1a..ea6a7f2 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -18262,6 +18262,17 @@ qemuNodeGetCPUStats(virConnectPtr conn,
     return virHostCPUGetStats(cpuNum, params, nparams, flags);
 }
 
+static int
+qemuNodeGetCacheStats(virConnectPtr conn,
+                    virNodeCacheStatsPtr params,
+                    int *nparams,
+                    unsigned int flags)
+{
+    if (virNodeGetCacheStatsEnsureACL(conn) < 0)
+        return -1;
+
+    return virResCtrlCacheGetStats(params, nparams, flags);
+}
 
 static int
 qemuNodeGetMemoryStats(virConnectPtr conn,
@@ -20295,6 +20306,7 @@ static virHypervisorDriver qemuHypervisorDriver = {
     .domainMemoryPeek = qemuDomainMemoryPeek, /* 0.4.4 */
     .domainGetBlockInfo = qemuDomainGetBlockInfo, /* 0.8.1 */
     .nodeGetCPUStats = qemuNodeGetCPUStats, /* 0.9.3 */
+    .nodeGetCacheStats = qemuNodeGetCacheStats, /* 3.0.0 */
     .nodeGetMemoryStats = qemuNodeGetMemoryStats, /* 0.9.3 */
     .nodeGetCellsFreeMemory = qemuNodeGetCellsFreeMemory, /* 0.4.4 */
     .nodeGetFreeMemory = qemuNodeGetFreeMemory, /* 0.4.4 */
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index eea9aea..03bec2e 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -1018,3 +1018,65 @@ virResCtrlGet(int type)
 {
     return &resctrlall[type];
 }
+
+int virResCtrlCacheGetStats(virNodeCacheStatsPtr params,
+                            int *nparams,
+                            unsigned int flags)
+{
+    virCheckFlags(0, -1);
+    size_t i, j, k;
+    char *value;
+    int rc = -1;
+    int lockfd;
+
+    if (*nparams == 0) {
+        for (i = 0; i < VIR_RDT_RESOURCE_LAST; i++) {
+            if (VIR_RESCTRL_ENABLED(i))
+                *nparams += resctrlall[i].num_banks;
+        }
+    }
+    if (params == NULL)
+        return 0;
+
+    if ((lockfd = open(RESCTRL_DIR, O_RDONLY)) < 0)
+        goto cleanup;
+
+    if (VIR_RESCTRL_LOCK(lockfd, LOCK_SH) < 0) {
+        virReportSystemError(errno, _("Unable to lock '%s'"), RESCTRL_DIR);
+        goto cleanup;
+    }
+    if (virResCtrlScan() < 0) {
+        VIR_ERROR(_("Failed to scan resctrl domain dir"));
+        goto cleanup;
+    }
+
+    virResCtrlRefreshSchemata();
+
+    if ((rc = virResCtrlFlushDomainToSysfs(domainall.domains)) < 0)
+        goto cleanup;
+
+    k = 0;
+
+    for (i = 0; i < VIR_RDT_RESOURCE_LAST; i++) {
+        if (VIR_RESCTRL_ENABLED(i)) {
+            for (j = 0; j < resctrlall[i].num_banks; j++) {
+
+                if (virAsprintf(&value, "%s.%zu",
+                                resctrlall[i].name, j) < 0)
+                    goto cleanup;
+
+                if (virStrcpyStatic((&params[k])->field, value) == NULL)
+                    goto cleanup;
+
+                (&params[k++])->value = resctrlall[i].cache_banks[j].cache_left;
+            }
+        }
+    }
+
+    rc = 0;
+
+ cleanup:
+    VIR_FREE(value);
+    VIR_RESCTRL_UNLOCK(lockfd);
+    return rc;
+}
diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h
index 303d915..ba3ed6e 100644
--- a/src/util/virresctrl.h
+++ b/src/util/virresctrl.h
@@ -80,9 +80,17 @@ struct _virResCtrl {
 };
 
 bool virResCtrlAvailable(void);
+
 int virResCtrlInit(void);
+
 virResCtrlPtr virResCtrlGet(int);
+
 int virResCtrlSetCacheBanks(virDomainCachetunePtr,
         unsigned char *, pid_t *, int);
+
 int virResCtrlUpdate(unsigned char *);
+
+int virResCtrlCacheGetStats(virNodeCacheStatsPtr params,
+                            int *nparams,
+                            unsigned int flags);
 #endif
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 24ebde2..c90bd2e 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -946,6 +946,49 @@ cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(params);
     return ret;
 }
+/* "nodecachestats" command
+ */
+static const vshCmdInfo info_nodecachestats[] = {
+    {.name = "help",
+     .data = N_("Prints cache stats of the node.")
+    },
+    {.name = "desc",
+     .data = N_("Returns cache stats of the node, in kilobytes.")
+    },
+    {.name = NULL}
+};
+
+static bool
+cmdNodeCacheStats(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
+{
+    virshControlPtr priv = ctl->privData;
+    virNodeCacheStatsPtr params;
+    int nparams = 0;
+    size_t i;
+    bool ret = false;
+
+    if (virNodeGetCacheStats(priv->conn, NULL, &nparams, 0) != 0) {
+        vshError(ctl, "%s",
+                 _("Unable to get number of cache stats"));
+        return false;
+    }
+    if (nparams == 0) {
+        /* nothing to output */
+        return true;
+    }
+
+    params = vshCalloc(ctl, nparams, sizeof(*params));
+    if (virNodeGetCacheStats(priv->conn, params, &nparams, 0) != 0) {
+        vshError(ctl, "%s", _("Unable to get node cache stats"));
+        goto cleanup;
+    }
+
+    for (i = 0; i < nparams; i++)
+        vshPrint(ctl, "%s: %llu KiB\n", params[i].field, params[i].value);
+
+ cleanup:
+    return ret;
+}
 
 /*
  * "nodesuspend" command
@@ -1455,6 +1498,12 @@ const vshCmdDef hostAndHypervisorCmds[] = {
      .info = info_nodememstats,
      .flags = 0
     },
+    {.name = "nodecachestats",
+     .handler = cmdNodeCacheStats,
+     .opts = NULL,
+     .info = info_nodecachestats,
+     .flags = 0
+    },
     {.name = "nodesuspend",
      .handler = cmdNodeSuspend,
      .opts = opts_node_suspend,
-- 
1.9.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats
Posted by Marcelo Tosatti 8 years, 11 months ago
On Mon, Feb 27, 2017 at 03:22:56PM +0800, Eli Qiao wrote:
> Add new virsh command line `nodecachestats` to expose the cache usage
> on a node.

Testing:

Setup: one guest with 3072 bytes reserved for it.

Test-1: start libvirt with free cache.
resctrlfs default: L3:0=fffff;1=fffff

# LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ ./virshnodegetcachestats
ret=0 nparams=10
L3.0: 13824
L3.1: 13824

Result: correct.

Test-2: start libvirt with a manual reservation in place 
(sharing space with default reservation, which is a
valid scenario).

./schemata: L3:0=fffff;1=fffff
./manualres/schemata: L3:0=f;1=f

How many bytes free there are to allocate? 
Mask 0xffff0 is free. Bytes = 12288.

After start libvirt daemon:
Value of resctrlfs files:
./schemata: L3:0=fffff;1=fffff
./manualres/schemata: L3:0=f;1=f

Run cmd:
# LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats 
ret=0 nparams=10
L3.0: 0
L3.1: 0

./schemata: L3:0=3;1=3
./manualres/schemata: L3:0=f;1=f

Result: incorrect.

Test-3: start libvirt with a manual reservation in place
(not sharing space with default reservation):

./schemata: L3:0=ffff0;1=ffff0
./manualres/schemata: L3:0=f;1=f

How many bytes free there are to allocate? 
Mask 0xffff0 is free. Bytes = 12288.

./schemata: L3:0=ffff0;1=ffff0
./manualres/schemata: L3:0=f;1=f

# LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats 
ret=0 nparams=10
L3.0: 0
L3.1: 0

So the interface is very buggy, please fix it (and execute the tests 
above to make sure it is fixed).

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats
Posted by Eli Qiao 8 years, 11 months ago

Best regards  
- Eli

天涯无处不重逢
a leaf duckweed belongs to the sea, where not to meet in life  

2017-03-03 10:24 GMT+08:00 Marcelo Tosatti <mtosatti@redhat.com (mailto:mtosatti@redhat.com)>:
> On Mon, Feb 27, 2017 at 03:22:56PM +0800, Eli Qiao wrote:
> > Add new virsh command line `nodecachestats` to expose the cache usage
> > on a node.
>  
> Testing:
>  
> Setup: one guest with 3072 bytes reserved for it.

hi Marcelo

Thanks for the testing.  

I improved default schemata calculating from V10, and V10 is on the top of master branch.

>  
> Test-1: start libvirt with free cache.
> resctrlfs default: L3:0=fffff;1=fffff
>  
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ ./virshnodegetcachestats
> ret=0 nparams=10
> L3.0: 13824
> L3.1: 13824
>  
> Result: correct.
>  
> Test-2: start libvirt with a manual reservation in place
> (sharing space with default reservation, which is a
> valid scenario).
>  
> ./schemata: L3:0=fffff;1=fffff
> ./manualres/schemata: L3:0=f;1=f
>  
> How many bytes free there are to allocate?
> Mask 0xffff0 is free. Bytes = 12288.
>  
> After start libvirt daemon:
> Value of resctrlfs files:
> ./schemata: L3:0=fffff;1=fffff
> ./manualres/schemata: L3:0=f;1=f
>  
> Run cmd:
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats
> ret=0 nparams=10
> L3.0: 0
> L3.1: 0
>  
> ./schemata: L3:0=3;1=3
> ./manualres/schemata: L3:0=f;1=f1
>  
> Result: incorrect.
>  
> Test-3: start libvirt with a manual reservation in place
> (not sharing space with default reservation):
>  
> ./schemata: L3:0=ffff0;1=ffff0
> ./manualres/schemata: L3:0=f;1=f
>  
> How many bytes free there are to allocate?
> Mask 0xffff0 is free. Bytes = 12288.
>  
> ./schemata: L3:0=ffff0;1=ffff0
> ./manualres/schemata: L3:0=f;1=f
>  
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats
> ret=0 nparams=10
> L3.0: 0
> L3.1: 0
>  
> So the interface is very buggy, please fix it (and execute the tests
> above to make sure it is fixed).
>  


  
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats
Posted by Marcelo Tosatti 8 years, 11 months ago
On Thu, Mar 02, 2017 at 11:24:00PM -0300, Marcelo Tosatti wrote:
> On Mon, Feb 27, 2017 at 03:22:56PM +0800, Eli Qiao wrote:
> > Add new virsh command line `nodecachestats` to expose the cache usage
> > on a node.
> 
> Testing:
> 
> Setup: one guest with 3072 bytes reserved for it.
> 
> Test-1: start libvirt with free cache.
> resctrlfs default: L3:0=fffff;1=fffff
> 
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ ./virshnodegetcachestats
> ret=0 nparams=10
> L3.0: 13824
> L3.1: 13824
> 
> Result: correct.
> 
> Test-2: start libvirt with a manual reservation in place 
> (sharing space with default reservation, which is a
> valid scenario).
> 
> ./schemata: L3:0=fffff;1=fffff
> ./manualres/schemata: L3:0=f;1=f
> 
> How many bytes free there are to allocate? 
> Mask 0xffff0 is free. Bytes = 12288.
> 
> After start libvirt daemon:
> Value of resctrlfs files:
> ./schemata: L3:0=fffff;1=fffff
> ./manualres/schemata: L3:0=f;1=f
> 
> Run cmd:
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats 
> ret=0 nparams=10
> L3.0: 0
> L3.1: 0
> 
> ./schemata: L3:0=3;1=3
> ./manualres/schemata: L3:0=f;1=f
> 
> Result: incorrect.
> 
> Test-3: start libvirt with a manual reservation in place
> (not sharing space with default reservation):
> 
> ./schemata: L3:0=ffff0;1=ffff0
> ./manualres/schemata: L3:0=f;1=f
> 
> How many bytes free there are to allocate? 
> Mask 0xffff0 is free. Bytes = 12288.
> 
> ./schemata: L3:0=ffff0;1=ffff0
> ./manualres/schemata: L3:0=f;1=f
> 
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ /root/git/virshnodegetcachestats 
> ret=0 nparams=10
> L3.0: 0
> L3.1: 0
> 
> So the interface is very buggy, please fix it (and execute the tests 
> above to make sure it is fixed).
> 

Also please rebase the patchset to latest libvirt code.



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATH v9 12/12] Resctrl: Add nodecachestats
Posted by Marcelo Tosatti 8 years, 11 months ago
On Thu, Mar 02, 2017 at 11:24:00PM -0300, Marcelo Tosatti wrote:
> On Mon, Feb 27, 2017 at 03:22:56PM +0800, Eli Qiao wrote:
> > Add new virsh command line `nodecachestats` to expose the cache usage
> > on a node.
> 
> Testing:
> 
> Setup: one guest with 3072 bytes reserved for it.
> 
> Test-1: start libvirt with free cache.
> resctrlfs default: L3:0=fffff;1=fffff
> 
> # LD_LIBRARY_PATH=/root/git/libvirt/src/.libs/ ./virshnodegetcachestats
> ret=0 nparams=10
> L3.0: 13824
> L3.1: 13824

The program does:

	ret = virNodeGetCacheStats(conn, ptr, &nparams, 0);
	fprintf(stdout, "ret=%d nparams=%d\n", ret, nparams);
	w = ptr;
	for (i=0; i<nparams; i++) {
    		printf("%s: %d\n", w->field, w->value);
		w++;
	}

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