Skip to contentSkip to conversation

Add support for binding guest numa nodes to host numa nodes

PATCH Superseded Wanlong Gao 9 participants 38 messages Jul 4, 2013 – Jul 16, 2013

v4 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add support for binding guest numa nodes to host numa nodes

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

This supports "mem-policy={membind|interleave|preferred},mem-hostnode=[+|!]{all|N-N}" like format.

And patch 8/10 adds a QMP command "set-mpol" to set the memory policy for every guest nodes:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

And patch 9/10 adds a monitor command "set-mpol" whose format like:
set-mpol 0 mem-policy=membind,mem-hostnode=0-1

And with patch 10/10, we can get the current memory policy of each guest node using monitor command "info numa", for example:

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

V1->V2:
change to use QemuOpts in numa options (Paolo)
handle Error in mpol parser (Paolo)
change qmp command format to mem-policy=membind,mem-hostnode=0-1 like (Paolo)
V2->V3:
also handle Error in cpus parser (5/10)
split out common parser from cpus and hostnode parser (Bandan 6/10)
V3-V4:
rebase to request for comments

Bandan Das (1):
NUMA: Support multiple CPU ranges on -numa option

Wanlong Gao (9):
NUMA: Add numa_info structure to contain numa nodes info
NUMA: Add Linux libnuma detection
NUMA: parse guest numa nodes memory policy
NUMA: handle Error in cpus, mpol and hostnode parser
NUMA: split out the common range parser
NUMA: set guest numa nodes memory policy
NUMA: add qmp command set-mpol to set memory policy for NUMA node
NUMA: add hmp command set-mpol
NUMA: show host memory policy info in info numa command

 configure               |  32 ++++++
 cpus.c                  | 143 +++++++++++++++++++++++-
 hmp-commands.hx         |  16 +++
 hmp.c                   |  35 ++++++
 hmp.h                   |   1 +
 hw/i386/pc.c            |   4 +-
 hw/net/eepro100.c       |   1 -
 include/sysemu/sysemu.h |  20 +++-
 monitor.c               |  44 +++++++-
 qapi-schema.json        |  15 +++
 qemu-options.hx         |   3 +-
 qmp-commands.hx         |  35 ++++++
 vl.c                    | 285 +++++++++++++++++++++++++++++++++++-------------
 13 files changed, 553 insertions(+), 81 deletions(-)
Signature · 1 line
1.8.3.1.448.gfb7dfaa
#1 lore raw

PATCH v4 1/10 NUMA: Support multiple CPU ranges on -numa option

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

---
 qemu-options.hx |   3 +-
 vl.c            | 108 ++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 67 insertions(+), 44 deletions(-)
qemu-options.hx +2 -1
index 137a39b..449cf36 100644
@@ -100,7 +100,8 @@ STEXI
 @item -numa @var{opts}
 @findex -numa
 Simulate a multi node NUMA system. If mem and cpus are omitted, resources
-are split equally.
+are split equally. The "-cpus" property may be specified multiple times
+to denote multiple cpus or cpu ranges.
 ETEXI
 
 DEF("add-fd", HAS_ARG, QEMU_OPTION_add_fd,
vl.c +65 -43

146 lines not on this page · raw message

Signature · 1 line
1.8.3.2.634.g7a3187e
#2 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 5 lines · Wanlong Gao

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

Quoted 7 lines

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Quoted 13 lines
---
 qemu-options.hx |   3 +-
 vl.c            | 108 ++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 67 insertions(+), 44 deletions(-)
qemu-options.hx +0 -0
index 137a39b..449cf36 100644

qemu-options.hx STEXI · line 103

Quoted 5 lines
qemu-options.hx +1 -1
@@ -100,7 +100,8 @@ STEXI
 @item -numa @var{opts}
 @findex -numa
 Simulate a multi node NUMA system. If mem and cpus are omitted, resources
-are split equally.
+are split equally. The "-cpus" property may be specified multiple times

The option is not named "-cpus", but just "cpus". And I believe it is normally called an "option", not a "property".

Quoted 132 lines
qemu-options.hx +1 -0
+to denote multiple cpus or cpu ranges.
 ETEXI
 
 DEF("add-fd", HAS_ARG, QEMU_OPTION_add_fd,
vl.c +50 -42
index 6d9fd7d..6f2e17a 100644
@@ -516,6 +516,32 @@ static QemuOptsList qemu_realtime_opts = {
     },
 };
 
+static QemuOptsList qemu_numa_opts = {
+    .name = "numa",
+    .implied_opt_name = "type",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head),
+    .desc = {
+        {
+            .name = "type",
+            .type = QEMU_OPT_STRING,
+            .help = "node type"
+        },{
+            .name = "nodeid",
+            .type = QEMU_OPT_NUMBER,
+            .help = "node ID"
+        },{
+            .name = "mem",
+            .type = QEMU_OPT_SIZE,
+            .help = "memory size"
+        },{
+            .name = "cpus",
+            .type = QEMU_OPT_STRING,
+            .help = "cpu number or range"
+        },
+        { /* end of list */ }
+    },
+};
+
 const char *qemu_get_vm_name(void)
 {
     return qemu_name;
@@ -1349,56 +1375,37 @@ error:
     exit(1);
 }
 
-static void numa_add(const char *optarg)
+
+static int numa_add_cpus(const char *name, const char *value, void *opaque)
 {
-    char option[128];
-    char *endptr;
-    unsigned long long nodenr;
+    int *nodenr = opaque;
 
-    optarg = get_opt_name(option, 128, optarg, ',');
-    if (*optarg == ',') {
-        optarg++;
+    if (!strcmp(name, "cpu")) {
+        numa_node_parse_cpus(*nodenr, value);
     }
-    if (!strcmp(option, "node")) {
-
-        if (nb_numa_nodes >= MAX_NODES) {
-            fprintf(stderr, "qemu: too many NUMA nodes\n");
-            exit(1);
-        }
+    return 0;
+}
 
-        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
-            nodenr = nb_numa_nodes;
-        } else {
-            if (parse_uint_full(option, &nodenr, 10) < 0) {
-                fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
-                exit(1);
-            }
-        }
+static int numa_init_func(QemuOpts *opts, void *opaque)
+{
+    uint64_t nodenr, mem_size;
 
-        if (nodenr >= MAX_NODES) {
-            fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
-            exit(1);
-        }
+    nodenr = qemu_opt_get_number(opts, "nodeid", nb_numa_nodes++);
 
-        if (get_param_value(option, 128, "mem", optarg) == 0) {
-            node_mem[nodenr] = 0;
-        } else {
-            int64_t sval;
-            sval = strtosz(option, &endptr);
-            if (sval < 0 || *endptr) {
-                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
-                exit(1);
-            }
-            node_mem[nodenr] = sval;
-        }
-        if (get_param_value(option, 128, "cpus", optarg) != 0) {
-            numa_node_parse_cpus(nodenr, option);
-        }
-        nb_numa_nodes++;
-    } else {
-        fprintf(stderr, "Invalid -numa option: %s\n", option);
+    if (nodenr >= MAX_NODES) {
+        fprintf(stderr, "qemu: Max number of NUMA nodes reached : %d\n",
+                (int)nodenr);
         exit(1);
     }
+
+    mem_size = qemu_opt_get_size(opts, "mem", 0);
+    node_mem[nodenr] = mem_size;
+
+    if (qemu_opt_foreach(opts, numa_add_cpus, &nodenr, 1) < 0) {
+        return -1;
+    }
+
+    return 0;
 }
 
 static QemuOptsList qemu_smp_opts = {
@@ -2933,6 +2940,7 @@ int main(int argc, char **argv, char **envp)
     qemu_add_opts(&qemu_object_opts);
     qemu_add_opts(&qemu_tpmdev_opts);
     qemu_add_opts(&qemu_realtime_opts);
+    qemu_add_opts(&qemu_numa_opts);
 
     runstate_init();
 
@@ -3119,7 +3127,16 @@ int main(int argc, char **argv, char **envp)
                 }
                 break;

vl.c int main(int argc, char **argv, char **envp) · line 3137

Quoted 10 lines
vl.c +8 -1
             case QEMU_OPTION_numa:
-                numa_add(optarg);
+                olist = qemu_find_opts("numa");
+                opts = qemu_opts_parse(olist, optarg, 1);
+                if (!opts) {
+                    exit(1);
+                }
+                optarg = qemu_opt_get(opts, "type");
+                if (!optarg || strcmp(optarg, "node")) {
+                    fprintf(stderr, "qemu: Incorrect format for numa option\n");

Why not do this inside numa_init_func()?

Quoted 21 lines
vl.c +7 -0
+                    exit(1);
+                }
                 break;
             case QEMU_OPTION_display:
                 display_type = select_display(optarg);
@@ -4195,6 +4212,11 @@ int main(int argc, char **argv, char **envp)
 
     register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
 
+    if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
+                          NULL, 1) != 0) {
+        exit(1);
+    }
+
     if (nb_numa_nodes > 0) {
         int i;
 
Signature · 1 line
1.8.3.2.634.g7a3187e
Signature · 1 line
Eduardo
#3 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 10 lines · Eduardo Habkost
Quoted 9 lines · Wanlong Gao

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Quoted 7 lines
Quoted 3 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 24 lines · Eric Blake
Quoted 10 lines · Eduardo Habkost
Quoted 9 lines · Wanlong Gao

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Quoted 7 lines
Quoted 3 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

Quoted 7 lines

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

I assume the rules above apply only if "new libvirt" gets released before "new qemu", right? Otherwise people won't be able to use latest released libvirt with latest released qemu until "new libvirt" gets released.

Signature · 1 line
Eduardo
#5 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

In reply to Eric Blake
Quoted 24 lines · Eric Blake
Quoted 10 lines · Eduardo Habkost
Quoted 9 lines · Wanlong Gao

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Quoted 7 lines
Quoted 3 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

Quoted 7 lines

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

That's fine for libvirt, but we don't break command line compatibility in QEMU. So this patch needs to change.

Regards,

Anthony Liguori

Quoted 4 lines
Signature · 3 lines
-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 30 lines · Anthony Liguori
Quoted 28 lines · Eric Blake
Quoted 10 lines · Eduardo Habkost
Quoted 9 lines · Wanlong Gao

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Quoted 7 lines
Quoted 3 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

Quoted 6 lines
Quoted 3 lines

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

That's fine for libvirt, but we don't break command line compatibility in QEMU. So this patch needs to change.

But if we follow Paolo's suggestion like:

    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

We already break the command line compatibility.
Why not change it be a really "size" options without default suffix?

Thanks,
Wanlong Gao

Quoted 11 lines

Regards,

Anthony Liguori

Quoted 4 lines
Signature · 3 lines
-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 7 lines · Wanlong Gao
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Quoted 6 lines

But if we follow Paolo's suggestion like:

    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

We already break the command line compatibility.

New command options can have whatever syntax makes sense. The worry here is that if you use the old command line without any new options, the old syntax must behave the same.

Quoted 1 line

Why not change it be a really "size" options without default suffix?

Because users are annoyed when a command line that worked with qemu 1.5 needlessly fails to work with qemu 1.6.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#8 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

In reply to Eric Blake

Il 08/07/2013 21:02, Eric Blake ha scritto:

Quoted 24 lines · Eduardo Habkost
Quoted 10 lines · Wanlong Gao
Quoted 9 lines

From: Bandan Das <[email protected]>

This allows us to use the "cpus" property multiple times to specify multiple cpu (ranges) to the -numa option :

-numa node,cpus=1,cpus=2,cpus=4
or
-numa node,cpus=1-3,cpus=5

Quoted 7 lines
Quoted 3 lines

Note that after this patch, the defalut suffix of "-numa node,mem=N" will no longer be "M". So we must add the suffix "M" like "-numa node,mem=NM" when assigning "N MB" of node memory size.

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

Quoted 7 lines

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

I don't think this is the case, unless you're talking of *very* old libvirt (e.g. pre-QMP).

Paolo

#9 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

Quoted 13 lines · Paolo Bonzini
Quoted 13 lines
Quoted 3 lines

Such an incompatible change is not acceptable, as it would break existing configurations. libvirt doesn't specify any suffix and expects it to always mean "MB".

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

old libvirt -> old qemu
new libvirt -> old qemu
new libvirt -> new qemu

Quoted 6 lines
Quoted 3 lines

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

I don't think this is the case, unless you're talking of *very* old libvirt (e.g. pre-QMP).

As a counter-example, I can recall a case where a qemu release that used just two digits (was that 1.2?) broke operation under older libvirt that assumed versions would always be three digits; but it definitely occurred after 0.15.x which is the point at which libvirt started favoring QMP. That is, we had a case in Fedora where if you upgraded qemu, you HAD to also update libvirt to be able to keep your guests running.

But yes, the goal of having command line compatibility, so that any application using the same command line it always uses will get the same guest, regardless of a qemu upgrade in the meantime, should be our default mode of operation, even if newer apps should prefer newer (better) command line interfaces.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#10 lore raw

Re: [Qemu-devel] [PATCH V4 01/10] NUMA: Support multiple CPU ranges on -numa option

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 15/07/2013 23:33, Eric Blake ha scritto:

Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Newer libvirt can be taught to append 'M' when it detects it is talking to newer qemu. While you have a point that it is annoying to force users to upgrade to a newer libvirt merely because they upgraded qemu, the libvirt point of view is that the following are supported:

old libvirt -> old qemu new libvirt -> old qemu new libvirt -> new qemu

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

but that this combination is always best effort and not required to work:

old libvirt -> new qemu

I don't think this is the case, unless you're talking of *very* old libvirt (e.g. pre-QMP).

Quoted 7 lines

As a counter-example, I can recall a case where a qemu release that used just two digits (was that 1.2?) broke operation under older libvirt that assumed versions would always be three digits; but it definitely occurred after 0.15.x which is the point at which libvirt started favoring QMP. That is, we had a case in Fedora where if you upgraded qemu, you HAD to also update libvirt to be able to keep your guests running.

Right, I remember that now. So, better: "we have some interfaces which are considered API, and old libvirt -> new QEMU should not break for things that use those interfaces". QMP and the command line are definitely one.

The case you mentioned was about -help, if I remember correctly, which was indeed quite brittle (like HMP).

Quoted 5 lines

But yes, the goal of having command line compatibility, so that any application using the same command line it always uses will get the same guest, regardless of a qemu upgrade in the meantime, should be our default mode of operation, even if newer apps should prefer newer (better) command line interfaces.

Yes, the command line *is* part of the API.

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJR5OcaAAoJEBvWZb6bTYbyGQgQAJVrX6z8/0hozhjAz81G7tuX cVmnC5cw+TgfYspf73yoBLBYZPUY/Ydb7WiabKrSfweMFX848WVhQr7rkwp0DVKQ X0WSbEKrVIGRMCjtvEMkzw1fmXintPLsaoxaLqYZs2MFgEsEP1eEG2MT/2JwpFd/ iDkqVmQ9fPxCEm8beoJXN8HV4Mwz5YY5E04tSqCktJzPh9+sGwB4cPy7PPiPjvHK I8nIdLHtOqFs4SwX1ic6HEZbeBE71swxr5QKhSg3/v6MzjZbK9/IU0RBcY69ftek 3fRJV8/hs8mHhfT7LsvB7XCNOxYq8jD1Bzy4oMJ/3LcAOyTLt1QJzFW4yaRSNGBK 6V/pDSWlghefulZu/aZASMh/IyxuCJRJ0uMVUEi20FeaIs96Bq5QBEInN/1JIYdH Qkek7C6dTrP1EdfbZFRa8+RzYEIDL0XmJFce8oicPZLGbhr/Jg1tZAkcUGr9gaeh z9bOTgAI98z29ZSHm4Bb3rb1WWSJY7BBRAgIDDxZuf34wuVUGWEvJOHRkB2iRa87 d6howw9eqWogVNNYHKYoTQCxEaTe7/PB0wXdWX5+AAZ29C0ETPZFOBYVwh9QLuCD V9WxGutqlXohbTgOk8rERHcUMJLlNblJg/i0tOMDU2Me4Uv+nW8UawEWUJClZiZS 5emSnaCr7UwPS9qUz1n1
=LQs8
-----END PGP SIGNATURE-----

#11 lore raw

PATCH v4 2/10 NUMA: Add numa_info structure to contain numa nodes info

In reply to Wanlong Gao

Add the numa_info structure to contain the numa nodes memory, VCPUs information and the future added numa nodes host memory policies.

---
 cpus.c                  |  2 +-
 hw/i386/pc.c            |  4 ++--
 hw/net/eepro100.c       |  1 -
 include/sysemu/sysemu.h |  8 ++++++--
 monitor.c               |  2 +-
 vl.c                    | 24 ++++++++++++------------
 6 files changed, 22 insertions(+), 19 deletions(-)
cpus.c +1 -1
index 20958e5..496d5ce 100644
@@ -1180,7 +1180,7 @@ void set_numa_modes(void)
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu = ENV_GET_CPU(env);
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (test_bit(cpu->cpu_index, node_cpumask[i])) {
+            if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
                 cpu->numa_node = i;
             }
         }
hw/i386/pc.c +2 -2
index 78f92e2..78b5a72 100644
@@ -650,14 +650,14 @@ static FWCfgState *bochs_bios_init(void)
         unsigned int apic_id = x86_cpu_apic_id_from_index(i);
         assert(apic_id < apic_id_limit);
         for (j = 0; j < nb_numa_nodes; j++) {
-            if (test_bit(i, node_cpumask[j])) {
+            if (test_bit(i, numa_info[j].node_cpu)) {
                 numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
                 break;
             }
         }
     }
     for (i = 0; i < nb_numa_nodes; i++) {
-        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
+        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(numa_info[i].node_mem);
     }
     fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
                      (1 + apic_id_limit + nb_numa_nodes) *
hw/net/eepro100.c +0 -1
index dc99ea6..478c688 100644
@@ -105,7 +105,6 @@
 #define PCI_IO_SIZE             64
 #define PCI_FLASH_SIZE          (128 * KiB)
 
-#define BIT(n) (1 << (n))
 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
 
 /* The SCB accepts the following controls for the Tx and Rx units: */
include/sysemu/sysemu.h +6 -2
index 2fb71af..70fd2ed 100644
@@ -9,6 +9,7 @@
 #include "qapi-types.h"
 #include "qemu/notify.h"
 #include "qemu/main-loop.h"
+#include "qemu/bitmap.h"
 
 /* vl.c */
 
@@ -130,8 +131,11 @@ extern QEMUClock *rtc_clock;
 #define MAX_NODES 64
 #define MAX_CPUMASK_BITS 255
 extern int nb_numa_nodes;
-extern uint64_t node_mem[MAX_NODES];
-extern unsigned long *node_cpumask[MAX_NODES];
+struct node_info {
+    uint64_t node_mem;
+    DECLARE_BITMAP(node_cpu, MAX_CPUMASK_BITS);
+};
+extern struct node_info numa_info[MAX_NODES];
 
 #define MAX_OPTION_ROMS 16
 typedef struct QEMUOptionRom {
monitor.c +1 -1
index 9be515c..93ac045 100644
@@ -1820,7 +1820,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         }
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
-            node_mem[i] >> 20);
+            numa_info[i].node_mem >> 20);
     }
 }
 
vl.c +12 -12
index 6f2e17a..5207b8e 100644
@@ -250,8 +250,7 @@ static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
     QTAILQ_HEAD_INITIALIZER(fw_boot_order);
 
 int nb_numa_nodes;
-uint64_t node_mem[MAX_NODES];
-unsigned long *node_cpumask[MAX_NODES];
+struct node_info numa_info[MAX_NODES];
 
 uint8_t qemu_uuid[16];
 
@@ -1367,7 +1366,7 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
         goto error;
     }
 
-    bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+    bitmap_set(numa_info[nodenr].node_cpu, value, endvalue-value+1);
     return;
 
 error:
@@ -1399,7 +1398,7 @@ static int numa_init_func(QemuOpts *opts, void *opaque)
     }
 
     mem_size = qemu_opt_get_size(opts, "mem", 0);
-    node_mem[nodenr] = mem_size;
+    numa_info[nodenr].node_mem = mem_size;
 
     if (qemu_opt_foreach(opts, numa_add_cpus, &nodenr, 1) < 0) {
         return -1;
@@ -2961,8 +2960,8 @@ int main(int argc, char **argv, char **envp)
     translation = BIOS_ATA_TRANSLATION_AUTO;
 
     for (i = 0; i < MAX_NODES; i++) {
-        node_mem[i] = 0;
-        node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
+        numa_info[i].node_mem = 0;
+        bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
     }
 
     nb_numa_nodes = 0;
@@ -4228,7 +4227,7 @@ int main(int argc, char **argv, char **envp)
          * and distribute the available memory equally across all nodes
          */
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (node_mem[i] != 0)
+            if (numa_info[i].node_mem != 0)
                 break;
         }
         if (i == nb_numa_nodes) {
@@ -4238,14 +4237,15 @@ int main(int argc, char **argv, char **envp)
              * the final node gets the rest.
              */
             for (i = 0; i < nb_numa_nodes - 1; i++) {
-                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
-                usedmem += node_mem[i];
+                numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
+                                        ~((1 << 23UL) - 1);
+                usedmem += numa_info[i].node_mem;
             }
-            node_mem[i] = ram_size - usedmem;
+            numa_info[i].node_mem = ram_size - usedmem;
         }
 
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
+            if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
                 break;
             }
         }
@@ -4255,7 +4255,7 @@ int main(int argc, char **argv, char **envp)
          */
         if (i == nb_numa_nodes) {
             for (i = 0; i < max_cpus; i++) {
-                set_bit(i, node_cpumask[i % nb_numa_nodes]);
+                set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
             }
         }
     }
Signature · 1 line
1.8.3.2.634.g7a3187e
Reviewed-by on v4 2/10
#12 lore raw

Re: [Qemu-devel] [PATCH V4 02/10] NUMA: Add numa_info structure to contain numa nodes info

Quoted 6 lines · Wanlong Gao

Add the numa_info structure to contain the numa nodes memory, VCPUs information and the future added numa nodes host memory policies.

Quoted 179 lines
---
 cpus.c                  |  2 +-
 hw/i386/pc.c            |  4 ++--
 hw/net/eepro100.c       |  1 -
 include/sysemu/sysemu.h |  8 ++++++--
 monitor.c               |  2 +-
 vl.c                    | 24 ++++++++++++------------
 6 files changed, 22 insertions(+), 19 deletions(-)
cpus.c +1 -1
index 20958e5..496d5ce 100644
@@ -1180,7 +1180,7 @@ void set_numa_modes(void)
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu = ENV_GET_CPU(env);
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (test_bit(cpu->cpu_index, node_cpumask[i])) {
+            if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
                 cpu->numa_node = i;
             }
         }
hw/i386/pc.c +2 -2
index 78f92e2..78b5a72 100644
@@ -650,14 +650,14 @@ static FWCfgState *bochs_bios_init(void)
         unsigned int apic_id = x86_cpu_apic_id_from_index(i);
         assert(apic_id < apic_id_limit);
         for (j = 0; j < nb_numa_nodes; j++) {
-            if (test_bit(i, node_cpumask[j])) {
+            if (test_bit(i, numa_info[j].node_cpu)) {
                 numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
                 break;
             }
         }
     }
     for (i = 0; i < nb_numa_nodes; i++) {
-        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
+        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(numa_info[i].node_mem);
     }
     fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
                      (1 + apic_id_limit + nb_numa_nodes) *
hw/net/eepro100.c +0 -1
index dc99ea6..478c688 100644
@@ -105,7 +105,6 @@
 #define PCI_IO_SIZE             64
 #define PCI_FLASH_SIZE          (128 * KiB)
 
-#define BIT(n) (1 << (n))
 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
 
 /* The SCB accepts the following controls for the Tx and Rx units: */
include/sysemu/sysemu.h +6 -2
index 2fb71af..70fd2ed 100644
@@ -9,6 +9,7 @@
 #include "qapi-types.h"
 #include "qemu/notify.h"
 #include "qemu/main-loop.h"
+#include "qemu/bitmap.h"
 
 /* vl.c */
 
@@ -130,8 +131,11 @@ extern QEMUClock *rtc_clock;
 #define MAX_NODES 64
 #define MAX_CPUMASK_BITS 255
 extern int nb_numa_nodes;
-extern uint64_t node_mem[MAX_NODES];
-extern unsigned long *node_cpumask[MAX_NODES];
+struct node_info {
+    uint64_t node_mem;
+    DECLARE_BITMAP(node_cpu, MAX_CPUMASK_BITS);
+};
+extern struct node_info numa_info[MAX_NODES];
 
 #define MAX_OPTION_ROMS 16
 typedef struct QEMUOptionRom {
monitor.c +1 -1
index 9be515c..93ac045 100644
@@ -1820,7 +1820,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         }
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
-            node_mem[i] >> 20);
+            numa_info[i].node_mem >> 20);
     }
 }
 
vl.c +12 -12
index 6f2e17a..5207b8e 100644
@@ -250,8 +250,7 @@ static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
     QTAILQ_HEAD_INITIALIZER(fw_boot_order);
 
 int nb_numa_nodes;
-uint64_t node_mem[MAX_NODES];
-unsigned long *node_cpumask[MAX_NODES];
+struct node_info numa_info[MAX_NODES];
 
 uint8_t qemu_uuid[16];
 
@@ -1367,7 +1366,7 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
         goto error;
     }
 
-    bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+    bitmap_set(numa_info[nodenr].node_cpu, value, endvalue-value+1);
     return;
 
 error:
@@ -1399,7 +1398,7 @@ static int numa_init_func(QemuOpts *opts, void *opaque)
     }
 
     mem_size = qemu_opt_get_size(opts, "mem", 0);
-    node_mem[nodenr] = mem_size;
+    numa_info[nodenr].node_mem = mem_size;
 
     if (qemu_opt_foreach(opts, numa_add_cpus, &nodenr, 1) < 0) {
         return -1;
@@ -2961,8 +2960,8 @@ int main(int argc, char **argv, char **envp)
     translation = BIOS_ATA_TRANSLATION_AUTO;
 
     for (i = 0; i < MAX_NODES; i++) {
-        node_mem[i] = 0;
-        node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
+        numa_info[i].node_mem = 0;
+        bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
     }
 
     nb_numa_nodes = 0;
@@ -4228,7 +4227,7 @@ int main(int argc, char **argv, char **envp)
          * and distribute the available memory equally across all nodes
          */
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (node_mem[i] != 0)
+            if (numa_info[i].node_mem != 0)
                 break;
         }
         if (i == nb_numa_nodes) {
@@ -4238,14 +4237,15 @@ int main(int argc, char **argv, char **envp)
              * the final node gets the rest.
              */
             for (i = 0; i < nb_numa_nodes - 1; i++) {
-                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
-                usedmem += node_mem[i];
+                numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
+                                        ~((1 << 23UL) - 1);
+                usedmem += numa_info[i].node_mem;
             }
-            node_mem[i] = ram_size - usedmem;
+            numa_info[i].node_mem = ram_size - usedmem;
         }
 
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
+            if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
                 break;
             }
         }
@@ -4255,7 +4255,7 @@ int main(int argc, char **argv, char **envp)
          */
         if (i == nb_numa_nodes) {
             for (i = 0; i < max_cpus; i++) {
-                set_bit(i, node_cpumask[i % nb_numa_nodes]);
+                set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
             }
         }
     }
Signature · 1 line
1.8.3.2.634.g7a3187e
Signature · 1 line
Eduardo
#13 lore raw

Re: [Qemu-devel] [PATCH V4 02/10] NUMA: Add numa_info structure to contain numa nodes info

Am 05.07.2013 21:32, schrieb Eduardo Habkost:

Quoted 77 lines · Wanlong Gao
Quoted 6 lines

Add the numa_info structure to contain the numa nodes memory, VCPUs information and the future added numa nodes host memory policies.

Quoted 67 lines
---
 cpus.c                  |  2 +-
 hw/i386/pc.c            |  4 ++--
 hw/net/eepro100.c       |  1 -
 include/sysemu/sysemu.h |  8 ++++++--
 monitor.c               |  2 +-
 vl.c                    | 24 ++++++++++++------------
 6 files changed, 22 insertions(+), 19 deletions(-)
cpus.c +1 -1
index 20958e5..496d5ce 100644
@@ -1180,7 +1180,7 @@ void set_numa_modes(void)
     for (env = first_cpu; env != NULL; env = env->next_cpu) {
         cpu = ENV_GET_CPU(env);
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (test_bit(cpu->cpu_index, node_cpumask[i])) {
+            if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) {
                 cpu->numa_node = i;
             }
         }
hw/i386/pc.c +2 -2
index 78f92e2..78b5a72 100644
@@ -650,14 +650,14 @@ static FWCfgState *bochs_bios_init(void)
         unsigned int apic_id = x86_cpu_apic_id_from_index(i);
         assert(apic_id < apic_id_limit);
         for (j = 0; j < nb_numa_nodes; j++) {
-            if (test_bit(i, node_cpumask[j])) {
+            if (test_bit(i, numa_info[j].node_cpu)) {
                 numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
                 break;
             }
         }
     }
     for (i = 0; i < nb_numa_nodes; i++) {
-        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
+        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(numa_info[i].node_mem);
     }
     fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
                      (1 + apic_id_limit + nb_numa_nodes) *
hw/net/eepro100.c +0 -1
index dc99ea6..478c688 100644
@@ -105,7 +105,6 @@
 #define PCI_IO_SIZE             64
 #define PCI_FLASH_SIZE          (128 * KiB)
 
-#define BIT(n) (1 << (n))
 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
 
 /* The SCB accepts the following controls for the Tx and Rx units: */
include/sysemu/sysemu.h +1 -0
index 2fb71af..70fd2ed 100644
@@ -9,6 +9,7 @@
 #include "qapi-types.h"
 #include "qemu/notify.h"
 #include "qemu/main-loop.h"
+#include "qemu/bitmap.h"
 
 /* vl.c */
 
Quoted 7 lines

include/sysemu/sysemu.h extern QEMUClock *rtc_clock; · line 134

Quoted 7 lines
include/sysemu/sysemu.h +1 -2
@@ -130,8 +131,11 @@ extern QEMUClock *rtc_clock;
 #define MAX_NODES 64
 #define MAX_CPUMASK_BITS 255
 extern int nb_numa_nodes;
-extern uint64_t node_mem[MAX_NODES];
-extern unsigned long *node_cpumask[MAX_NODES];
+struct node_info {

NodeInfo

Quoted 3 lines

include/sysemu/sysemu.h extern QEMUClock *rtc_clock; · line 137

Quoted 3 lines
include/sysemu/sysemu.h +3 -0
+    uint64_t node_mem;
+    DECLARE_BITMAP(node_cpu, MAX_CPUMASK_BITS);
+};

Please add a typedef and use that everywhere below.

Quoted 1 line

include/sysemu/sysemu.h extern QEMUClock *rtc_clock; · line 138

Quoted 1 line
include/sysemu/sysemu.h +1 -0
+extern struct node_info numa_info[MAX_NODES];

I wonder if those structs should be QOM Objects instead, so that we can use link<> properties from CPUState. I think Paolo suggested something in that direction?

Regards,
Andreas

Quoted 102 lines
Quoted 101 lines
include/sysemu/sysemu.h +0 -0
 #define MAX_OPTION_ROMS 16
 typedef struct QEMUOptionRom {
monitor.c +1 -1
index 9be515c..93ac045 100644
@@ -1820,7 +1820,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         }
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
-            node_mem[i] >> 20);
+            numa_info[i].node_mem >> 20);
     }
 }
 
vl.c +12 -12
index 6f2e17a..5207b8e 100644
@@ -250,8 +250,7 @@ static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
     QTAILQ_HEAD_INITIALIZER(fw_boot_order);
 
 int nb_numa_nodes;
-uint64_t node_mem[MAX_NODES];
-unsigned long *node_cpumask[MAX_NODES];
+struct node_info numa_info[MAX_NODES];
 
 uint8_t qemu_uuid[16];
 
@@ -1367,7 +1366,7 @@ static void numa_node_parse_cpus(int nodenr, const char *cpus)
         goto error;
     }
 
-    bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
+    bitmap_set(numa_info[nodenr].node_cpu, value, endvalue-value+1);
     return;
 
 error:
@@ -1399,7 +1398,7 @@ static int numa_init_func(QemuOpts *opts, void *opaque)
     }
 
     mem_size = qemu_opt_get_size(opts, "mem", 0);
-    node_mem[nodenr] = mem_size;
+    numa_info[nodenr].node_mem = mem_size;
 
     if (qemu_opt_foreach(opts, numa_add_cpus, &nodenr, 1) < 0) {
         return -1;
@@ -2961,8 +2960,8 @@ int main(int argc, char **argv, char **envp)
     translation = BIOS_ATA_TRANSLATION_AUTO;
 
     for (i = 0; i < MAX_NODES; i++) {
-        node_mem[i] = 0;
-        node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
+        numa_info[i].node_mem = 0;
+        bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
     }
 
     nb_numa_nodes = 0;
@@ -4228,7 +4227,7 @@ int main(int argc, char **argv, char **envp)
          * and distribute the available memory equally across all nodes
          */
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (node_mem[i] != 0)
+            if (numa_info[i].node_mem != 0)
                 break;
         }
         if (i == nb_numa_nodes) {
@@ -4238,14 +4237,15 @@ int main(int argc, char **argv, char **envp)
              * the final node gets the rest.
              */
             for (i = 0; i < nb_numa_nodes - 1; i++) {
-                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
-                usedmem += node_mem[i];
+                numa_info[i].node_mem = (ram_size / nb_numa_nodes) &
+                                        ~((1 << 23UL) - 1);
+                usedmem += numa_info[i].node_mem;
             }
-            node_mem[i] = ram_size - usedmem;
+            numa_info[i].node_mem = ram_size - usedmem;
         }
 
         for (i = 0; i < nb_numa_nodes; i++) {
-            if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
+            if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) {
                 break;
             }
         }
@@ -4255,7 +4255,7 @@ int main(int argc, char **argv, char **envp)
          */
         if (i == nb_numa_nodes) {
             for (i = 0; i < max_cpus; i++) {
-                set_bit(i, node_cpumask[i % nb_numa_nodes]);
+                set_bit(i, numa_info[i % nb_numa_nodes].node_cpu);
             }
         }
     }
Signature · 1 line
1.8.3.2.634.g7a3187e
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#14 lore raw

PATCH v4 3/10 NUMA: Add Linux libnuma detection

In reply to Wanlong Gao

Add detection of libnuma (mostly contained in the numactl package) to the configure script. Can be enabled or disabled on the command line, default is use if available.

---
 configure | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
configure +32 -0
index 0e0adde..9d3b4ce 100755
@@ -242,6 +242,7 @@ gtk=""
 gtkabi="2.0"
 tpm="no"
 libssh2=""
+numa=""
 
 # parse CC options first
 for opt do
@@ -944,6 +945,10 @@ for opt do
   ;;
   --enable-libssh2) libssh2="yes"
   ;;
+  --disable-numa) numa="no"
+  ;;
+  --enable-numa) numa="yes"
+  ;;
   *) echo "ERROR: unknown option $opt"; show_help="yes"
   ;;
   esac
@@ -1158,6 +1163,8 @@ echo "  --gcov=GCOV              use specified gcov [$gcov_tool]"
 echo "  --enable-tpm             enable TPM support"
 echo "  --disable-libssh2        disable ssh block device support"
 echo "  --enable-libssh2         enable ssh block device support"
+echo "  --disable-numa           disable libnuma support"
+echo "  --enable-numa            enable libnuma support"
 echo ""
 echo "NOTE: The object files are built at the place where configure is launched"
 exit 1
@@ -2389,6 +2396,27 @@ EOF
 fi
 
 ##########################################
+# libnuma probe
+
+if test "$numa" != "no" ; then
+  numa=no
+  cat > $TMPC << EOF
+#include <numa.h>
+int main(void) { return numa_available(); }
+EOF
+
+  if compile_prog "" "-lnuma" ; then
+    numa=yes
+    libs_softmmu="-lnuma $libs_softmmu"
+  else
+    if test "$numa" = "yes" ; then
+      feature_not_found "linux NUMA (install numactl?)"
+    fi
+    numa=no
+  fi
+fi
+
+##########################################
 # linux-aio probe
 
 if test "$linux_aio" != "no" ; then
@@ -3557,6 +3585,7 @@ echo "TPM support       $tpm"
 echo "libssh2 support   $libssh2"
 echo "TPM passthrough   $tpm_passthrough"
 echo "QOM debugging     $qom_cast_debug"
+echo "NUMA host support $numa"
 
 if test "$sdl_too_old" = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -3590,6 +3619,9 @@ echo "extra_cflags=$EXTRA_CFLAGS" >> $config_host_mak
 echo "extra_ldflags=$EXTRA_LDFLAGS" >> $config_host_mak
 echo "qemu_localedir=$qemu_localedir" >> $config_host_mak
 echo "libs_softmmu=$libs_softmmu" >> $config_host_mak
+if test "$numa" = "yes"; then
+  echo "CONFIG_NUMA=y" >> $config_host_mak
+fi
 
 echo "ARCH=$ARCH" >> $config_host_mak
 
Signature · 1 line
1.8.3.2.634.g7a3187e
#15 lore raw

PATCH v4 4/10 NUMA: parse guest numa nodes memory policy

In reply to Wanlong Gao

The memory policy setting format is like:
mem-policy={membind|interleave|preferred},mem-hostnode=[+|!]{all|N-N} And we are adding this setting as a suboption of "-numa",
the memory policy then can be set like following:
-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=!1

---
 include/sysemu/sysemu.h |   8 ++++
 vl.c                    | 110 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+)
include/sysemu/sysemu.h +8 -0
index 70fd2ed..993b8e0 100644
@@ -130,10 +130,18 @@ extern QEMUClock *rtc_clock;
 
 #define MAX_NODES 64
 #define MAX_CPUMASK_BITS 255
+#define NODE_HOST_NONE        0x00
+#define NODE_HOST_BIND        0x01
+#define NODE_HOST_INTERLEAVE  0x02
+#define NODE_HOST_PREFERRED   0x03
+#define NODE_HOST_POLICY_MASK 0x03
+#define NODE_HOST_RELATIVE    0x04
 extern int nb_numa_nodes;
 struct node_info {
     uint64_t node_mem;
     DECLARE_BITMAP(node_cpu, MAX_CPUMASK_BITS);
+    DECLARE_BITMAP(host_mem, MAX_CPUMASK_BITS);
+    unsigned int flags;
 };
 extern struct node_info numa_info[MAX_NODES];
 
vl.c +110 -0

140 lines not on this page · raw message

Signature · 1 line
1.8.3.2.634.g7a3187e
#16 lore raw

PATCH v4 5/10 NUMA: handle Error in cpus, mpol and hostnode parser

In reply to Wanlong Gao

As Paolo pointed out that, handle Error in mpol and hostnode parser will make it easier to be used for example in mem-hotplug in the future. And this will be used later in set-mpol QMP command.
Also handle Error in cpus parser to be consistent with others.

---
 include/sysemu/sysemu.h |  4 ++++
 vl.c                    | 42 ++++++++++++++++++++++++++++++++----------
 2 files changed, 36 insertions(+), 10 deletions(-)
include/sysemu/sysemu.h +4 -0
index 993b8e0..0f135fe 100644
@@ -144,6 +144,10 @@ struct node_info {
     unsigned int flags;
 };
 extern struct node_info numa_info[MAX_NODES];
+extern void numa_node_parse_mpol(int nodenr, const char *hostnode,
+                                 Error **errp);
+extern void numa_node_parse_hostnode(int nodenr, const char *hostnode,
+                                     Error **errp);
 
 #define MAX_OPTION_ROMS 16
 typedef struct QEMUOptionRom {
vl.c +32 -10

98 lines not on this page · raw message

Signature · 1 line
1.8.3.2.634.g7a3187e
#17 lore raw

PATCH v4 6/10 NUMA: split out the common range parser

In reply to Wanlong Gao

Since cpus parser and hostnode parser have the common range parser part, split it out to the common range parser to avoid the duplicate code.

---
 vl.c | 89 ++++++++++++++++++++++++++++----------------------------------------
 1 file changed, 37 insertions(+), 52 deletions(-)
vl.c +37 -52
index 38e0d3d..6e86dcf 100644
@@ -1338,47 +1338,55 @@ char *get_boot_devices_list(size_t *size)
     return list;
 }
 
-static void numa_node_parse_cpus(int nodenr, const char *cpus, Error **errp)
+static int numa_node_parse_common(const char *str,
+                                  unsigned long long *value,
+                                  unsigned long long *endvalue)
 {
     char *endptr;
-    unsigned long long value, endvalue;
-
-    /* Empty CPU range strings will be considered valid, they will simply
-     * not set any bit in the CPU bitmap.
-     */
-    if (!*cpus) {
-        return;
+    if (parse_uint(str, value, &endptr, 10) < 0) {
+        return -1;
     }
 
-    if (parse_uint(cpus, &value, &endptr, 10) < 0) {
-        goto error;
-    }
     if (*endptr == '-') {
-        if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
-            goto error;
+        if (parse_uint_full(endptr + 1, endvalue, 10) < 0) {
+           return -1;
         }
     } else if (*endptr == '\0') {
-        endvalue = value;
+        *endvalue = *value;
     } else {
-        goto error;
+        return -1;
     }
 
-    if (endvalue >= MAX_CPUMASK_BITS) {
-        endvalue = MAX_CPUMASK_BITS - 1;
-        fprintf(stderr,
-            "qemu: NUMA: A max of %d VCPUs are supported\n",
-             MAX_CPUMASK_BITS);
+    if (*endvalue >= MAX_CPUMASK_BITS) {
+        *endvalue = MAX_CPUMASK_BITS - 1;
+        fprintf(stderr, "qemu: NUMA: A max number %d is supported\n",
+                MAX_CPUMASK_BITS);
     }
 
-    if (endvalue < value) {
-        goto error;
+    if (*endvalue < *value) {
+        return -1;
     }
 
-    bitmap_set(numa_info[nodenr].node_cpu, value, endvalue-value+1);
-    return;
+    return 0;
+}
 
-error:
-    error_setg(errp, "Invalid NUMA CPU range: %s\n", cpus);
+static void numa_node_parse_cpus(int nodenr, const char *cpus, Error **errp)
+{
+    unsigned long long value, endvalue;
+
+    /* Empty CPU range strings will be considered valid, they will simply
+     * not set any bit in the CPU bitmap.
+     */
+    if (!*cpus) {
+        return;
+    }
+
+    if (numa_node_parse_common(cpus, &value, &endvalue) < 0) {
+        error_setg(errp, "Invalid NUMA CPU range: %s", cpus);
+        return;
+    }
+
+    bitmap_set(numa_info[nodenr].node_cpu, value, endvalue-value+1);
     return;
 }
 
@@ -1403,7 +1411,6 @@ void numa_node_parse_mpol(int nodenr, const char *mpol, Error **errp)
 void numa_node_parse_hostnode(int nodenr, const char *hostnode, Error **errp)
 {
     unsigned long long value, endvalue;
-    char *endptr;
     bool clear = false;
     unsigned long *bm = numa_info[nodenr].host_mem;
 
@@ -1422,27 +1429,9 @@ void numa_node_parse_hostnode(int nodenr, const char *hostnode, Error **errp)
         return;
     }
 
-    if (parse_uint(hostnode, &value, &endptr, 10) < 0)
-        goto error;
-    if (*endptr == '-') {
-        if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
-            goto error;
-        }
-    } else if (*endptr == '\0') {
-        endvalue = value;
-    } else {
-        goto error;
-    }
-
-    if (endvalue >= MAX_CPUMASK_BITS) {
-        endvalue = MAX_CPUMASK_BITS - 1;
-        fprintf(stderr,
-            "qemu: NUMA: A max of %d host nodes are supported\n",
-             MAX_CPUMASK_BITS);
-    }
-
-    if (endvalue < value) {
-        goto error;
+    if (numa_node_parse_common(hostnode, &value, &endvalue) < 0) {
+        error_setg(errp, "Invalid host NUMA ndoes range: %s", hostnode);
+        return;
     }
 
     if (clear)
@@ -1451,10 +1440,6 @@ void numa_node_parse_hostnode(int nodenr, const char *hostnode, Error **errp)
         bitmap_set(bm, value, endvalue - value + 1);
 
     return;
-
-error:
-    error_setg(errp, "Invalid host NUMA nodes range: %s", hostnode);
-    return;
 }
 
 static int numa_add_cpus(const char *name, const char *value, void *opaque)
Signature · 1 line
1.8.3.2.634.g7a3187e
#18 lore raw

PATCH v4 7/10 NUMA: set guest numa nodes memory policy

In reply to Wanlong Gao

Set the guest numa nodes memory policies using the mbind(2) system call node by node.
After this patch, we are able to set guest nodes memory policies through the QEMU options, this arms to solve the guest cross nodes memory access performance issue.
And as you all know, if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

So, we should set the guest nodes memory allocation policies before the pages are really mapped.

---
 cpus.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)
cpus.c +87 -0
index 496d5ce..7240de7 100644
@@ -60,6 +60,15 @@
 
 #endif /* CONFIG_LINUX */
 
+#ifdef CONFIG_NUMA
+#include <numa.h>
+#include <numaif.h>
+#ifndef MPOL_F_RELATIVE_NODES
+#define MPOL_F_RELATIVE_NODES (1 << 14)
+#define MPOL_F_STATIC_NODES   (1 << 15)
+#endif
+#endif
+
 static CPUArchState *next_cpu;
 
 static bool cpu_thread_is_idle(CPUState *cpu)
@@ -1171,6 +1180,75 @@ static void tcg_exec_all(void)
     exit_request = 0;
 }
 
+#ifdef CONFIG_NUMA
+static int node_parse_bind_mode(unsigned int nodeid)
+{
+    int bind_mode;
+
+    switch (numa_info[nodeid].flags & NODE_HOST_POLICY_MASK) {
+    case NODE_HOST_BIND:
+        bind_mode = MPOL_BIND;
+        break;
+    case NODE_HOST_INTERLEAVE:
+        bind_mode = MPOL_INTERLEAVE;
+        break;
+    case NODE_HOST_PREFERRED:
+        bind_mode = MPOL_PREFERRED;
+        break;
+    default:
+        bind_mode = MPOL_DEFAULT;
+        return bind_mode;
+    }
+
+    bind_mode |= (numa_info[nodeid].flags & NODE_HOST_RELATIVE) ?
+        MPOL_F_RELATIVE_NODES : MPOL_F_STATIC_NODES;
+
+    return bind_mode;
+}
+#endif
+
+static int set_node_mpol(unsigned int nodeid)
+{
+#ifdef CONFIG_NUMA
+    void *ram_ptr;
+    RAMBlock *block;
+    ram_addr_t len, ram_offset = 0;
+    int bind_mode;
+    int i;
+
+    QTAILQ_FOREACH(block, &ram_list.blocks, next) {
+        if (!strcmp(block->mr->name, "pc.ram")) {
+            break;
+        }
+    }
+
+    if (block->host == NULL)
+        return -1;
+
+    ram_ptr = block->host;
+    for (i = 0; i < nodeid; i++) {
+        len = numa_info[i].node_mem;
+        ram_offset += len;
+    }
+
+    len = numa_info[i].node_mem;
+    bind_mode = node_parse_bind_mode(i);
+
+    /* This is a workaround for a long standing bug in Linux'
+     * mbind implementation, which cuts off the last specified
+     * node. To stay compatible should this bug be fixed, we
+     * specify one more node and zero this one out.
+     */
+    clear_bit(numa_num_configured_nodes() + 1, numa_info[i].host_mem);
+    if (mbind(ram_ptr + ram_offset, len, bind_mode,
+        numa_info[i].host_mem, numa_num_configured_nodes() + 1, 0)) {
+            perror("mbind");
+            return -1;
+    }
+#endif
+    return 0;
+}
+
 void set_numa_modes(void)
 {
     CPUArchState *env;
@@ -1185,6 +1263,15 @@ void set_numa_modes(void)
             }
         }
     }
+
+#ifdef CONFIG_NUMA
+    for (i = 0; i < nb_numa_nodes; i++) {
+        if (set_node_mpol(i) == -1) {
+            fprintf(stderr,
+                    "qemu: can't set host memory policy for node%d\n", i);
+        }
+    }
+#endif
 }
 
 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Signature · 1 line
1.8.3.2.634.g7a3187e
#19 lore raw

PATCH v4 8/10 NUMA: add qmp command set-mpol to set memory policy for NUMA node

In reply to Wanlong Gao

The QMP command let it be able to set node's memory policy through the QMP protocol. The qmp-shell command is like:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

---
 cpus.c           | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json | 15 +++++++++++++++
 qmp-commands.hx  | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
cpus.c +54 -0
index 7240de7..ff42b9d 100644
@@ -1417,3 +1417,57 @@ void qmp_inject_nmi(Error **errp)
     error_set(errp, QERR_UNSUPPORTED);
 #endif
 }
+
+void qmp_set_mpol(int64_t nodeid, bool has_mpol, const char *mpol,
+                  bool has_hostnode, const char *hostnode, Error **errp)
+{
+    unsigned int flags;
+    DECLARE_BITMAP(host_mem, MAX_CPUMASK_BITS);
+
+    if (nodeid >= nb_numa_nodes) {
+        error_setg(errp, "Only has '%d' NUMA nodes", nb_numa_nodes);
+        return;
+    }
+
+    bitmap_copy(host_mem, numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    flags = numa_info[nodeid].flags;
+
+    numa_info[nodeid].flags = NODE_HOST_NONE;
+    bitmap_zero(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+
+    if (!has_mpol) {
+        if (set_node_mpol(nodeid) == -1) {
+            error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+            goto error;
+        }
+        return;
+    }
+
+    numa_node_parse_mpol(nodeid, mpol, errp);
+    if (error_is_set(errp)) {
+        goto error;
+    }
+
+    if (!has_hostnode) {
+        bitmap_fill(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    }
+
+    if (hostnode) {
+        numa_node_parse_hostnode(nodeid, hostnode, errp);
+        if (error_is_set(errp)) {
+            goto error;
+        }
+    }
+
+    if (set_node_mpol(nodeid) == -1) {
+        error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+        goto error;
+    }
+
+    return;
+
+error:
+    bitmap_copy(numa_info[nodeid].host_mem, host_mem, MAX_CPUMASK_BITS);
+    numa_info[nodeid].flags = flags;
+    return;
+}
qapi-schema.json +15 -0
index 5c32528..0870da2 100644
@@ -3712,3 +3712,18 @@
             '*cpuid-input-ecx': 'int',
             'cpuid-register': 'X86CPURegister32',
             'features': 'int' } }
+
+# @set-mpol:
+#
+# Set the host memory binding policy for guest NUMA node.
+#
+# @nodeid: The node ID of guest NUMA node to set memory policy to.
+#
+# @mem-policy: The memory policy string to set.
+#
+# @mem-hostnode: The host node or node range for memory policy.
+#
+# Since: 1.6.0
+##
+{ 'command': 'set-mpol', 'data': {'nodeid': 'int', '*mem-policy': 'str',
+                                  '*mem-hostnode': 'str'} }
qmp-commands.hx +35 -0
index 362f0e1..ccab51b 100644
@@ -3043,3 +3043,38 @@ Example:
 <- { "return": {} }
 
 EQMP
+
+    {
+        .name      = "set-mpol",
+        .args_type = "nodeid:i,mem-policy:s?,mem-hostnode:s?",
+        .help      = "Set the host memory binding policy for guest NUMA node",
+        .mhandler.cmd_new = qmp_marshal_input_set_mpol,
+    },
+
+SQMP
+set-mpol
+------
+
+Set the host memory binding policy for guest NUMA node
+
+Arguments:
+
+- "nodeid": The nodeid of guest NUMA node to set memory policy to.
+            (json-int)
+- "mem-policy": The memory policy string to set.
+                (json-string, optional)
+- "mem-hostnode": The host nodes contained to mpol.
+                  (json-string, optional)
+
+Example:
+
+-> { "execute": "set-mpol", "arguments": { "nodeid": 0, "mem-policy": "membind",
+                                           "mem-hostnode": "0-1" }}
+<- { "return": {} }
+
+Notes:
+    1. If "mem-policy" is not set, the memory policy of this "nodeid" will be set
+       to "default".
+    2. If "mem-hostnode" is not set, the node mask of this "mpol" will be set
+       to "all".
+EQMP
Signature · 1 line
1.8.3.2.634.g7a3187e
#20 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

Quoted 79 lines · On Thu, 4 Jul 2013 17:53:15 +0800 Wanlong Gao <[email protected]> wrote:

The QMP command let it be able to set node's memory policy through the QMP protocol. The qmp-shell command is like:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

---
 cpus.c           | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json | 15 +++++++++++++++
 qmp-commands.hx  | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
cpus.c +54 -0
index 7240de7..ff42b9d 100644
@@ -1417,3 +1417,57 @@ void qmp_inject_nmi(Error **errp)
     error_set(errp, QERR_UNSUPPORTED);
 #endif
 }
+
+void qmp_set_mpol(int64_t nodeid, bool has_mpol, const char *mpol,
+                  bool has_hostnode, const char *hostnode, Error **errp)
+{
+    unsigned int flags;
+    DECLARE_BITMAP(host_mem, MAX_CPUMASK_BITS);
+
+    if (nodeid >= nb_numa_nodes) {
+        error_setg(errp, "Only has '%d' NUMA nodes", nb_numa_nodes);
+        return;
+    }
+
+    bitmap_copy(host_mem, numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    flags = numa_info[nodeid].flags;
+
+    numa_info[nodeid].flags = NODE_HOST_NONE;
+    bitmap_zero(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+
+    if (!has_mpol) {
+        if (set_node_mpol(nodeid) == -1) {
+            error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+            goto error;
+        }
+        return;
+    }
+
+    numa_node_parse_mpol(nodeid, mpol, errp);
+    if (error_is_set(errp)) {
+        goto error;
+    }
+
+    if (!has_hostnode) {
+        bitmap_fill(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    }
+
+    if (hostnode) {
+        numa_node_parse_hostnode(nodeid, hostnode, errp);
+        if (error_is_set(errp)) {
+            goto error;
+        }
+    }
+
+    if (set_node_mpol(nodeid) == -1) {
+        error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+        goto error;
+    }
+
+    return;
+
+error:
+    bitmap_copy(numa_info[nodeid].host_mem, host_mem, MAX_CPUMASK_BITS);
+    numa_info[nodeid].flags = flags;
+    return;
+}
qapi-schema.json +0 -0
index 5c32528..0870da2 100644
@@ -3712,3 +3712,18 @@
             '*cpuid-input-ecx': 'int',

qapi-schema.json line 3722

Quoted 10 lines
qapi-schema.json +8 -0
             'cpuid-register': 'X86CPURegister32',
             'features': 'int' } }
+
+# @set-mpol:
+#
+# Set the host memory binding policy for guest NUMA node.
+#
+# @nodeid: The node ID of guest NUMA node to set memory policy to.
+#
+# @mem-policy: The memory policy string to set.

Shouldn't this be an enum? Also, optional members have a leading '#optional' string and if a default value is used it should be documented.

qapi-schema.json line 3724

Quoted 2 lines
qapi-schema.json +2 -0
+#
+# @mem-hostnode: The host node or node range for memory policy.

It doesn't seem appropriate to use a string here. Maybe we could use a list with only to values (like [0,2] for 0-2) or maybe a list of nodes if that makes sense (like [0,1,2]).

Quoted 48 lines
qapi-schema.json +5 -0
+#
+# Since: 1.6.0
+##
+{ 'command': 'set-mpol', 'data': {'nodeid': 'int', '*mem-policy': 'str',
+                                  '*mem-hostnode': 'str'} }
qmp-commands.hx +35 -0
index 362f0e1..ccab51b 100644
@@ -3043,3 +3043,38 @@ Example:
 <- { "return": {} }
 
 EQMP
+
+    {
+        .name      = "set-mpol",
+        .args_type = "nodeid:i,mem-policy:s?,mem-hostnode:s?",
+        .help      = "Set the host memory binding policy for guest NUMA node",
+        .mhandler.cmd_new = qmp_marshal_input_set_mpol,
+    },
+
+SQMP
+set-mpol
+------
+
+Set the host memory binding policy for guest NUMA node
+
+Arguments:
+
+- "nodeid": The nodeid of guest NUMA node to set memory policy to.
+            (json-int)
+- "mem-policy": The memory policy string to set.
+                (json-string, optional)
+- "mem-hostnode": The host nodes contained to mpol.
+                  (json-string, optional)
+
+Example:
+
+-> { "execute": "set-mpol", "arguments": { "nodeid": 0, "mem-policy": "membind",
+                                           "mem-hostnode": "0-1" }}
+<- { "return": {} }
+
+Notes:
+    1. If "mem-policy" is not set, the memory policy of this "nodeid" will be set
+       to "default".
+    2. If "mem-hostnode" is not set, the node mask of this "mpol" will be set
+       to "all".
+EQMP
#21 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

Quoted 96 lines · On Mon, 8 Jul 2013 14:25:14 -0400 Luiz Capitulino <[email protected]>…
Quoted 79 lines · On Thu, 4 Jul 2013 17:53:15 +0800 Wanlong Gao <[email protected]> wrote:

The QMP command let it be able to set node's memory policy through the QMP protocol. The qmp-shell command is like:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

---
 cpus.c           | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json | 15 +++++++++++++++
 qmp-commands.hx  | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+)
cpus.c +54 -0
index 7240de7..ff42b9d 100644
@@ -1417,3 +1417,57 @@ void qmp_inject_nmi(Error **errp)
     error_set(errp, QERR_UNSUPPORTED);
 #endif
 }
+
+void qmp_set_mpol(int64_t nodeid, bool has_mpol, const char *mpol,
+                  bool has_hostnode, const char *hostnode, Error **errp)
+{
+    unsigned int flags;
+    DECLARE_BITMAP(host_mem, MAX_CPUMASK_BITS);
+
+    if (nodeid >= nb_numa_nodes) {
+        error_setg(errp, "Only has '%d' NUMA nodes", nb_numa_nodes);
+        return;
+    }
+
+    bitmap_copy(host_mem, numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    flags = numa_info[nodeid].flags;
+
+    numa_info[nodeid].flags = NODE_HOST_NONE;
+    bitmap_zero(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+
+    if (!has_mpol) {
+        if (set_node_mpol(nodeid) == -1) {
+            error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+            goto error;
+        }
+        return;
+    }
+
+    numa_node_parse_mpol(nodeid, mpol, errp);
+    if (error_is_set(errp)) {
+        goto error;
+    }
+
+    if (!has_hostnode) {
+        bitmap_fill(numa_info[nodeid].host_mem, MAX_CPUMASK_BITS);
+    }
+
+    if (hostnode) {
+        numa_node_parse_hostnode(nodeid, hostnode, errp);
+        if (error_is_set(errp)) {
+            goto error;
+        }
+    }
+
+    if (set_node_mpol(nodeid) == -1) {
+        error_setg(errp, "Failed to set memory policy for node%lu", nodeid);
+        goto error;
+    }
+
+    return;
+
+error:
+    bitmap_copy(numa_info[nodeid].host_mem, host_mem, MAX_CPUMASK_BITS);
+    numa_info[nodeid].flags = flags;
+    return;
+}
qapi-schema.json +0 -0
index 5c32528..0870da2 100644
@@ -3712,3 +3712,18 @@
             '*cpuid-input-ecx': 'int',

qapi-schema.json line 3722

Quoted 10 lines
qapi-schema.json +8 -0
             'cpuid-register': 'X86CPURegister32',
             'features': 'int' } }
+
+# @set-mpol:
+#
+# Set the host memory binding policy for guest NUMA node.
+#
+# @nodeid: The node ID of guest NUMA node to set memory policy to.
+#
+# @mem-policy: The memory policy string to set.

Shouldn't this be an enum? Also, optional members have a leading '#optional' string and if a default value is used it should be documented.

Quoted 6 lines

qapi-schema.json line 3724

Quoted 2 lines
qapi-schema.json +2 -0
+#
+# @mem-hostnode: The host node or node range for memory policy.

It doesn't seem appropriate to use a string here. Maybe we could use a list with only to values (like [0,2] for 0-2) or maybe a list of nodes if that makes sense (like [0,1,2]).

I forgot to bike-shed on the naming. I'd suggest the following:

- s/set-mpol/set-numa-policy
- s/mem-policy/policy
- s/mem-hostnode/host-nodes

Quoted 50 lines
Quoted 49 lines
qapi-schema.json +5 -0
+#
+# Since: 1.6.0
+##
+{ 'command': 'set-mpol', 'data': {'nodeid': 'int', '*mem-policy': 'str',
+                                  '*mem-hostnode': 'str'} }
qmp-commands.hx +35 -0
index 362f0e1..ccab51b 100644
@@ -3043,3 +3043,38 @@ Example:
 <- { "return": {} }
 
 EQMP
+
+    {
+        .name      = "set-mpol",
+        .args_type = "nodeid:i,mem-policy:s?,mem-hostnode:s?",
+        .help      = "Set the host memory binding policy for guest NUMA node",
+        .mhandler.cmd_new = qmp_marshal_input_set_mpol,
+    },
+
+SQMP
+set-mpol
+------
+
+Set the host memory binding policy for guest NUMA node
+
+Arguments:
+
+- "nodeid": The nodeid of guest NUMA node to set memory policy to.
+            (json-int)
+- "mem-policy": The memory policy string to set.
+                (json-string, optional)
+- "mem-hostnode": The host nodes contained to mpol.
+                  (json-string, optional)
+
+Example:
+
+-> { "execute": "set-mpol", "arguments": { "nodeid": 0, "mem-policy": "membind",
+                                           "mem-hostnode": "0-1" }}
+<- { "return": {} }
+
+Notes:
+    1. If "mem-policy" is not set, the memory policy of this "nodeid" will be set
+       to "default".
+    2. If "mem-hostnode" is not set, the node mask of this "mpol" will be set
+       to "all".
+EQMP
#22 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

Am 08.07.2013 20:34, schrieb Luiz Capitulino:

Quoted 5 lines

I forgot to bike-shed on the naming. I'd suggest the following:

- s/set-mpol/set-numa-policy
- s/mem-policy/policy
- s/mem-hostnode/host-nodes

I had suggested s/set-mpol/set-memory-policy/g on the previous round (but didn't get any kind of response).

http://patchwork.ozlabs.org/patch/253699/

I'm fine with anything readable and following our conventions though.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#23 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

Quoted 7 lines · On Mon, 08 Jul 2013 20:50:46 +0200 Andreas Färber <[email protected]> wrote:

Am 08.07.2013 20:34, schrieb Luiz Capitulino:

Quoted 5 lines

I forgot to bike-shed on the naming. I'd suggest the following:

- s/set-mpol/set-numa-policy
- s/mem-policy/policy
- s/mem-hostnode/host-nodes

Quoted 6 lines

I had suggested s/set-mpol/set-memory-policy/g on the previous round (but didn't get any kind of response).

http://patchwork.ozlabs.org/patch/253699/

I'm fine with anything readable and following our conventions though.

I'm fine with your suggestion too.

#24 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

In reply to Luiz Capitulino
Quoted 10 lines · Luiz Capitulino
Quoted 8 lines

+

qapi-schema.json line 3722

Quoted 7 lines
qapi-schema.json +7 -0
+# @set-mpol:
+#
+# Set the host memory binding policy for guest NUMA node.
+#
+# @nodeid: The node ID of guest NUMA node to set memory policy to.
+#
+# @mem-policy: The memory policy string to set.

Shouldn't this be an enum? Also, optional members have a leading '#optional' string and if a default value is used it should be documented.

Thank you, will fix in V5.

Quoted 6 lines
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+#
+# @mem-hostnode: The host node or node range for memory policy.

It doesn't seem appropriate to use a string here. Maybe we could use a list with only to values (like [0,2] for 0-2) or maybe a list of nodes if that makes sense (like [0,1,2]).

I wonder if not using string, how to support "+" and "!" ?

Thanks,
Wanlong Gao

#25 lore raw

Re: [Qemu-devel] [PATCH V4 08/10] NUMA: add qmp command set-mpol to set memory policy for NUMA node

In reply to Wanlong Gao
Quoted 1 line · Wanlong Gao

The QMP command let it be able to set node's memory policy

s/let it be able/allows users/

Quoted 5 lines

through the QMP protocol. The qmp-shell command is like:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

---

Just an interface review:

qapi-schema.json line 3716

Quoted 7 lines

+++ b/qapi-schema.json

qapi-schema.json +2 -0
@@ -3712,3 +3712,18 @@
             '*cpuid-input-ecx': 'int',
             'cpuid-register': 'X86CPURegister32',
             'features': 'int' } }
+
+# @set-mpol:

I agree with other requests in this thread to make the name closer to English words (set-memory-policy). Also, I hate write-only interfaces; what is the corresponding query-* command that lets me learn the policy that is currently in effect? I'm expecting that this series either modifies an existing command or adds a new query command as the counterpart to this set command.

qapi-schema.json line 3729

Quoted 13 lines
qapi-schema.json +13 -0
+#
+# Set the host memory binding policy for guest NUMA node.
+#
+# @nodeid: The node ID of guest NUMA node to set memory policy to.
+#
+# @mem-policy: The memory policy string to set.
+#
+# @mem-hostnode: The host node or node range for memory policy.
+#
+# Since: 1.6.0
+##
+{ 'command': 'set-mpol', 'data': {'nodeid': 'int', '*mem-policy': 'str',
+                                  '*mem-hostnode': 'str'} }

Make mem-policy an enum, not an open-coded string. Also, make mem-hostnode an array of nodes - a general rule of thumb is that if the receiver (here, qemu) has to do further parsing of the data (such as scraping out integer vs. dash to decide if it is one node or a range), then the JSON was too high-level. Using ['int'] instead of 'str' will let the data be available already parsed into an integer list by the visitor code, so you aren't having to write your own ad hoc parser on the receiving end.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#26 lore raw

PATCH v4 9/10 NUMA: add hmp command set-mpol

In reply to Wanlong Gao

Add hmp command set-mpol to set host memory policy for a guest NUMA node. Then we can also set node's memory policy using the monitor command like:
(qemu) set-mpol 0 mem-policy=membind,mem-hostnode=0-1

---
 hmp-commands.hx | 16 ++++++++++++++++
 hmp.c           | 35 +++++++++++++++++++++++++++++++++++
 hmp.h           |  1 +
 3 files changed, 52 insertions(+)
hmp-commands.hx +16 -0
index 915b0d1..417b69f 100644
@@ -1567,6 +1567,22 @@ Executes a qemu-io command on the given block device.
 ETEXI
 
     {
+        .name       = "set-mpol",
+        .args_type  = "nodeid:i,args:s?",
+        .params     = "nodeid [args]",
+        .help       = "set host memory policy for a guest NUMA node",
+        .mhandler.cmd = hmp_set_mpol,
+    },
+
+STEXI
+@item set-mpol @var{nodeid} @var{args}
+@findex set-mpol
+
+Set host memory policy for a guest NUMA node
+
+ETEXI
+
+    {
         .name       = "info",
         .args_type  = "item:s?",
         .params     = "[subcommand]",
hmp.c +35 -0
index 2daed43..57a5730 100644
@@ -1482,3 +1482,38 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict)
 
     hmp_handle_error(mon, &err);
 }
+
+void hmp_set_mpol(Monitor *mon, const QDict *qdict)
+{
+    Error *local_err = NULL;
+    bool has_mpol = true;
+    bool has_hostnode = true;
+    const char *mpol = NULL;
+    const char *hostnode = NULL;
+    QemuOpts *opts;
+
+    uint64_t nodeid = qdict_get_int(qdict, "nodeid");
+    const char *args = qdict_get_try_str(qdict, "args");
+
+    if (args == NULL) {
+        has_mpol = false;
+        has_hostnode = false;
+    } else {
+        opts = qemu_opts_parse(qemu_find_opts("numa"), args, 1);
+        if (opts == NULL) {
+            error_setg(&local_err, "Parsing memory policy args failed");
+        } else {
+            mpol = qemu_opt_get(opts, "mem-policy");
+            if (mpol == NULL) {
+                has_mpol = false;
+            }
+            hostnode = qemu_opt_get(opts, "mem-hostnode");
+            if (hostnode == NULL) {
+                has_hostnode = false;
+            }
+        }
+    }
+
+    qmp_set_mpol(nodeid, has_mpol, mpol, has_hostnode, hostnode, &local_err);
+    hmp_handle_error(mon, &local_err);
+}
hmp.h +1 -0
index 56d2e92..81f631b 100644
@@ -86,5 +86,6 @@ void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
 void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
 void hmp_qemu_io(Monitor *mon, const QDict *qdict);
+void hmp_set_mpol(Monitor *mon, const QDict *qdict);
 
 #endif
Signature · 1 line
1.8.3.2.634.g7a3187e
#27 lore raw

Re: [Qemu-devel] [PATCH V4 09/10] NUMA: add hmp command set-mpol

Quoted 57 lines · On Thu, 4 Jul 2013 17:53:16 +0800 Wanlong Gao <[email protected]> wrote:

Add hmp command set-mpol to set host memory policy for a guest NUMA node. Then we can also set node's memory policy using the monitor command like:
(qemu) set-mpol 0 mem-policy=membind,mem-hostnode=0-1

---
 hmp-commands.hx | 16 ++++++++++++++++
 hmp.c           | 35 +++++++++++++++++++++++++++++++++++
 hmp.h           |  1 +
 3 files changed, 52 insertions(+)
hmp-commands.hx +16 -0
index 915b0d1..417b69f 100644
@@ -1567,6 +1567,22 @@ Executes a qemu-io command on the given block device.
 ETEXI
 
     {
+        .name       = "set-mpol",
+        .args_type  = "nodeid:i,args:s?",
+        .params     = "nodeid [args]",
+        .help       = "set host memory policy for a guest NUMA node",
+        .mhandler.cmd = hmp_set_mpol,
+    },
+
+STEXI
+@item set-mpol @var{nodeid} @var{args}
+@findex set-mpol
+
+Set host memory policy for a guest NUMA node
+
+ETEXI
+
+    {
         .name       = "info",
         .args_type  = "item:s?",
         .params     = "[subcommand]",
hmp.c +10 -0
index 2daed43..57a5730 100644
@@ -1482,3 +1482,38 @@ void hmp_qemu_io(Monitor *mon, const QDict *qdict)
 
     hmp_handle_error(mon, &err);
 }
+
+void hmp_set_mpol(Monitor *mon, const QDict *qdict)
+{
+    Error *local_err = NULL;
+    bool has_mpol = true;
+    bool has_hostnode = true;
+    const char *mpol = NULL;
+    const char *hostnode = NULL;
+    QemuOpts *opts;
+

hmp.c void hmp_qemu_io(Monitor *mon, const QDict *qdict) · line 1504

Quoted 10 lines
hmp.c +10 -0
+    uint64_t nodeid = qdict_get_int(qdict, "nodeid");
+    const char *args = qdict_get_try_str(qdict, "args");
+
+    if (args == NULL) {
+        has_mpol = false;
+        has_hostnode = false;
+    } else {
+        opts = qemu_opts_parse(qemu_find_opts("numa"), args, 1);
+        if (opts == NULL) {
+            error_setg(&local_err, "Parsing memory policy args failed");

You're still going to call qmp_set_mpol() if this fails. You can replace error_setg() to a monitor_printf() call and return.

Quoted 26 lines
hmp.c +15 -0
+        } else {
+            mpol = qemu_opt_get(opts, "mem-policy");
+            if (mpol == NULL) {
+                has_mpol = false;
+            }
+            hostnode = qemu_opt_get(opts, "mem-hostnode");
+            if (hostnode == NULL) {
+                has_hostnode = false;
+            }
+        }
+    }
+
+    qmp_set_mpol(nodeid, has_mpol, mpol, has_hostnode, hostnode, &local_err);
+    hmp_handle_error(mon, &local_err);
+}
hmp.h +1 -0
index 56d2e92..81f631b 100644
@@ -86,5 +86,6 @@ void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
 void hmp_chardev_add(Monitor *mon, const QDict *qdict);
 void hmp_chardev_remove(Monitor *mon, const QDict *qdict);
 void hmp_qemu_io(Monitor *mon, const QDict *qdict);
+void hmp_set_mpol(Monitor *mon, const QDict *qdict);
 
 #endif
#28 lore raw

PATCH v4 10/10 NUMA: show host memory policy info in info numa command

In reply to Wanlong Gao

Show host memory policy of nodes in the info numa monitor command. After this patch, the monitor command "info numa" will show the information like following if the host numa support is enabled:

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

---
 monitor.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
monitor.c +42 -0
index 93ac045..a40415d 100644
@@ -74,6 +74,11 @@
 #endif
 #include "hw/lm32/lm32_pic.h"
 
+#ifdef CONFIG_NUMA
+#include <numa.h>
+#include <numaif.h>
+#endif
+
 //#define DEBUG
 //#define DEBUG_COMPLETION
 
@@ -1808,6 +1813,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
     int i;
     CPUArchState *env;
     CPUState *cpu;
+    unsigned long first, next;
 
     monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
     for (i = 0; i < nb_numa_nodes; i++) {
@@ -1821,6 +1827,42 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
             numa_info[i].node_mem >> 20);
+
+#ifdef CONFIG_NUMA
+        monitor_printf(mon, "node %d mempolicy: ", i);
+        switch (numa_info[i].flags & NODE_HOST_POLICY_MASK) {
+        case NODE_HOST_BIND:
+            monitor_printf(mon, "membind=");
+            break;
+        case NODE_HOST_INTERLEAVE:
+            monitor_printf(mon, "interleave=");
+            break;
+        case NODE_HOST_PREFERRED:
+            monitor_printf(mon, "preferred=");
+            break;
+        default:
+            monitor_printf(mon, "default\n");
+            continue;
+        }
+
+        if (numa_info[i].flags & NODE_HOST_RELATIVE)
+            monitor_printf(mon, "+");
+
+        next = first = find_first_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS);
+        monitor_printf(mon, "%lu", first);
+        do {
+            if (next == numa_max_node())
+                break;
+            next = find_next_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS,
+                                 next + 1);
+            if (next > numa_max_node() || next == MAX_CPUMASK_BITS)
+                break;
+
+            monitor_printf(mon, ",%lu", next);
+        } while (true);
+
+        monitor_printf(mon, "\n");
+#endif
     }
 }
 
Signature · 1 line
1.8.3.2.634.g7a3187e
#29 lore raw

Re: [Qemu-devel] [PATCH V4 10/10] NUMA: show host memory policy info in info numa command

Quoted 35 lines · Wanlong Gao

Show host memory policy of nodes in the info numa monitor command. After this patch, the monitor command "info numa" will show the information like following if the host numa support is enabled:

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

---
 monitor.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
monitor.c +5 -0
index 93ac045..a40415d 100644
@@ -74,6 +74,11 @@
 #endif
 #include "hw/lm32/lm32_pic.h"
 
+#ifdef CONFIG_NUMA
+#include <numa.h>
+#include <numaif.h>
+#endif
+
 //#define DEBUG
 //#define DEBUG_COMPLETION
 

monitor.c static void do_info_numa(Monitor *mon, const QDict *qdict) · line 1816

Quoted 4 lines
monitor.c +1 -0
@@ -1808,6 +1813,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
     int i;
     CPUArchState *env;
     CPUState *cpu;
+    unsigned long first, next;

This breaks compilation with --enable-werror and CONFIG_NUMA disabled:

/home/ehabkost/rh/proj/virt/qemu/monitor.c: In function ‘do_info_numa’:
/home/ehabkost/rh/proj/virt/qemu/monitor.c:1816:26: error: unused variable ‘next’ [-Werror=unused-variable] /home/ehabkost/rh/proj/virt/qemu/monitor.c:1816:19: error: unused variable ‘first’ [-Werror=unused-variable]
cc1: all warnings being treated as errors
make[1]: *** [monitor.o] Error 1

Quoted 50 lines
monitor.c +36 -0
     monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
     for (i = 0; i < nb_numa_nodes; i++) {
@@ -1821,6 +1827,42 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
             numa_info[i].node_mem >> 20);
+
+#ifdef CONFIG_NUMA
+        monitor_printf(mon, "node %d mempolicy: ", i);
+        switch (numa_info[i].flags & NODE_HOST_POLICY_MASK) {
+        case NODE_HOST_BIND:
+            monitor_printf(mon, "membind=");
+            break;
+        case NODE_HOST_INTERLEAVE:
+            monitor_printf(mon, "interleave=");
+            break;
+        case NODE_HOST_PREFERRED:
+            monitor_printf(mon, "preferred=");
+            break;
+        default:
+            monitor_printf(mon, "default\n");
+            continue;
+        }
+
+        if (numa_info[i].flags & NODE_HOST_RELATIVE)
+            monitor_printf(mon, "+");
+
+        next = first = find_first_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS);
+        monitor_printf(mon, "%lu", first);
+        do {
+            if (next == numa_max_node())
+                break;
+            next = find_next_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS,
+                                 next + 1);
+            if (next > numa_max_node() || next == MAX_CPUMASK_BITS)
+                break;
+
+            monitor_printf(mon, ",%lu", next);
+        } while (true);
+
+        monitor_printf(mon, "\n");
+#endif
     }
 }
 
Signature · 1 line
1.8.3.2.634.g7a3187e
Signature · 1 line
Eduardo
#30 lore raw

Re: [Qemu-devel] [PATCH V4 10/10] NUMA: show host memory policy info in info numa command

In reply to Wanlong Gao
Quoted 3 lines · On Thu, 4 Jul 2013 17:53:17 +0800 Wanlong Gao <[email protected]> wrote:

Show host memory policy of nodes in the info numa monitor command. After this patch, the monitor command "info numa" will show the information like following if the host numa support is enabled:

As you're adding a QMP command to set the policy, wouldn't it make sense to convert info numa to QMP so that we also have query-numa?

Quoted 82 lines

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

---
 monitor.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
monitor.c +42 -0
index 93ac045..a40415d 100644
@@ -74,6 +74,11 @@
 #endif
 #include "hw/lm32/lm32_pic.h"
 
+#ifdef CONFIG_NUMA
+#include <numa.h>
+#include <numaif.h>
+#endif
+
 //#define DEBUG
 //#define DEBUG_COMPLETION
 
@@ -1808,6 +1813,7 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
     int i;
     CPUArchState *env;
     CPUState *cpu;
+    unsigned long first, next;
 
     monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
     for (i = 0; i < nb_numa_nodes; i++) {
@@ -1821,6 +1827,42 @@ static void do_info_numa(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "\n");
         monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
             numa_info[i].node_mem >> 20);
+
+#ifdef CONFIG_NUMA
+        monitor_printf(mon, "node %d mempolicy: ", i);
+        switch (numa_info[i].flags & NODE_HOST_POLICY_MASK) {
+        case NODE_HOST_BIND:
+            monitor_printf(mon, "membind=");
+            break;
+        case NODE_HOST_INTERLEAVE:
+            monitor_printf(mon, "interleave=");
+            break;
+        case NODE_HOST_PREFERRED:
+            monitor_printf(mon, "preferred=");
+            break;
+        default:
+            monitor_printf(mon, "default\n");
+            continue;
+        }
+
+        if (numa_info[i].flags & NODE_HOST_RELATIVE)
+            monitor_printf(mon, "+");
+
+        next = first = find_first_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS);
+        monitor_printf(mon, "%lu", first);
+        do {
+            if (next == numa_max_node())
+                break;
+            next = find_next_bit(numa_info[i].host_mem, MAX_CPUMASK_BITS,
+                                 next + 1);
+            if (next > numa_max_node() || next == MAX_CPUMASK_BITS)
+                break;
+
+            monitor_printf(mon, ",%lu", next);
+        } while (true);
+
+        monitor_printf(mon, "\n");
+#endif
     }
 }
 
#31 lore raw
In reply to Wanlong Gao

Il 04/07/2013 11:53, Wanlong Gao ha scritto:

Quoted 16 lines

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

Quoted 8 lines

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

Did you see my suggestion to use instead something like this:

    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

With an eye to when we'll support memory hotplug, I think it is better.
It is not hard to implement it using the OptsVisitor; see
14aa0c2de045a6c2fcfadf38c04434fd15909455 for an example of a complex schema described with OptsVistor.

Paolo

#32 lore raw
Quoted 28 lines · Paolo Bonzini

Il 04/07/2013 11:53, Wanlong Gao ha scritto:

Quoted 16 lines

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

Quoted 8 lines

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

Did you see my suggestion to use instead something like this:

Quoted 8 lines
    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

With an eye to when we'll support memory hotplug, I think it is better.
It is not hard to implement it using the OptsVisitor; see
14aa0c2de045a6c2fcfadf38c04434fd15909455 for an example of a complex schema described with OptsVistor.

See also the commit msg of its grandparent, eb7ee2cb, for general notes. The containing series is d195325b^..1a0c0958.

A more recent (and simpler) use is the 8ccbad5c^..0c764a9d sub-series.

Thanks for the reference, Paolo.

Laszlo

#33 lore raw
Quoted 38 lines · Laszlo Ersek
Quoted 28 lines · Paolo Bonzini

Il 04/07/2013 11:53, Wanlong Gao ha scritto:

Quoted 16 lines

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

Quoted 8 lines

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

Did you see my suggestion to use instead something like this:

Quoted 8 lines
    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

With an eye to when we'll support memory hotplug, I think it is better.
It is not hard to implement it using the OptsVisitor; see
14aa0c2de045a6c2fcfadf38c04434fd15909455 for an example of a complex schema described with OptsVistor.

Quoted 4 lines

See also the commit msg of its grandparent, eb7ee2cb, for general notes. The containing series is d195325b^..1a0c0958.

A more recent (and simpler) use is the 8ccbad5c^..0c764a9d sub-series.

Thank you for your references Laszlo, it's very helpful.

Wanlong Gao

Quoted 6 lines

Thanks for the reference, Paolo.

Laszlo

#34 lore raw
In reply to Paolo Bonzini
Quoted 28 lines · Paolo Bonzini

Il 04/07/2013 11:53, Wanlong Gao ha scritto:

Quoted 16 lines

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

Quoted 8 lines

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

Did you see my suggestion to use instead something like this:

Quoted 8 lines
    -numa node,nodeid=0,cpus=0 -numa node,nodeid=1,cpus=1 \
    -numa mem,nodeid=0,size=1G,policy=membind,hostnode=0-1
    -numa mem,nodeid=1,size=2G,policy=interleave,hostnode=1

With an eye to when we'll support memory hotplug, I think it is better.
It is not hard to implement it using the OptsVisitor; see
14aa0c2de045a6c2fcfadf38c04434fd15909455 for an example of a complex schema described with OptsVistor.

OK, got it, thank you.

Wanlong Gao

Quoted 4 lines

Paolo

#35 lore raw
In reply to Wanlong Gao

On Thu, Jul 04, 2013 at 05:53:07PM +0800, Wanlong Gao wrote:
[...]

Quoted 13 lines

Bandan Das (1):
NUMA: Support multiple CPU ranges on -numa option

Wanlong Gao (9):
NUMA: Add numa_info structure to contain numa nodes info
NUMA: Add Linux libnuma detection
NUMA: parse guest numa nodes memory policy
NUMA: handle Error in cpus, mpol and hostnode parser
NUMA: split out the common range parser
NUMA: set guest numa nodes memory policy
NUMA: add qmp command set-mpol to set memory policy for NUMA node
NUMA: add hmp command set-mpol
NUMA: show host memory policy info in info numa command

checkpatch.pl issues:

total: 0 errors, 0 warnings, 155 lines checked

/tmp/numav4-fp/0001-NUMA-Support-multiple-CPU-ranges-on-numa-option.patch has no obvious style problems and is ready for submission.
total: 0 errors, 0 warnings, 129 lines checked

/tmp/numav4-fp/0002-NUMA-Add-numa_info-structure-to-contain-numa-nodes-i.patch has no obvious style problems and is ready for submission.
total: 0 errors, 0 warnings, 68 lines checked

/tmp/numav4-fp/0003-NUMA-Add-Linux-libnuma-detection.patch has no obvious style problems and is ready for submission.
WARNING: braces {} are necessary for all arms of this statement
#106: FILE: vl.c:1424:
+ if (parse_uint(hostnode, &value, &endptr, 10) < 0)
[...]

WARNING: braces {} are necessary for all arms of this statement #129: FILE: vl.c:1447:
+ if (clear)
[...]
+ else
[...]

total: 0 errors, 2 warnings, 158 lines checked

/tmp/numav4-fp/0004-NUMA-parse-guest-numa-nodes-memory-policy.patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
total: 0 errors, 0 warnings, 108 lines checked

/tmp/numav4-fp/0005-NUMA-handle-Error-in-cpus-mpol-and-hostnode-parser.patch has no obvious style problems and is ready for submission.
WARNING: suspect code indent for conditional statements (8, 11)
#47: FILE: vl.c:1351:
+ if (parse_uint_full(endptr + 1, endvalue, 10) < 0) {
+ return -1;

total: 0 errors, 1 warnings, 128 lines checked

/tmp/numav4-fp/0006-NUMA-split-out-the-common-range-parser.patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
WARNING: braces {} are necessary for all arms of this statement
#100: FILE: cpus.c:1225:
+ if (block->host == NULL)
[...]

total: 0 errors, 1 warnings, 105 lines checked

/tmp/numav4-fp/0007-NUMA-set-guest-numa-nodes-memory-policy.patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
total: 0 errors, 0 warnings, 113 lines checked

/tmp/numav4-fp/0008-NUMA-add-qmp-command-set-mpol-to-set-memory-policy-f.patch has no obvious style problems and is ready for submission.
total: 0 errors, 0 warnings, 66 lines checked

/tmp/numav4-fp/0009-NUMA-add-hmp-command-set-mpol.patch has no obvious style problems and is ready for submission.
WARNING: braces {} are necessary for all arms of this statement
#70: FILE: monitor.c:1848:
+ if (numa_info[i].flags & NODE_HOST_RELATIVE)
[...]

WARNING: braces {} are necessary for all arms of this statement #76: FILE: monitor.c:1854:
+ if (next == numa_max_node())
[...]

WARNING: braces {} are necessary for all arms of this statement #80: FILE: monitor.c:1858:
+ if (next > numa_max_node() || next == MAX_CPUMASK_BITS)
[...]

total: 0 errors, 3 warnings, 60 lines checked

/tmp/numav4-fp/0010-NUMA-show-host-memory-policy-info-in-info-numa-comma.patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.

Signature · 1 line
Eduardo
#36 lore raw
In reply to Wanlong Gao

Hi,Wanlong

Quoted 1 line

From the patch discription below, seems that qemu numa only support cpu/memory node binding.

As we know, binding is not the common usage due to VM migration may happen or the load balance would be disabled.
So, do we have any plan of generating virtual numa automatically?

For example, if we create a 16vCPU VM on a 4 8-core node physical box, we can automatically place it to two physical node, not by binding.

Quoted 87 lines · Wanlong Gao

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

This supports "mem-policy={membind|interleave|preferred},mem-hostnode=[+|!]{all|N-N}" like format.

And patch 8/10 adds a QMP command "set-mpol" to set the memory policy for every guest nodes:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

And patch 9/10 adds a monitor command "set-mpol" whose format like:
set-mpol 0 mem-policy=membind,mem-hostnode=0-1

And with patch 10/10, we can get the current memory policy of each guest node using monitor command "info numa", for example:

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

V1->V2:
change to use QemuOpts in numa options (Paolo)
handle Error in mpol parser (Paolo)
change qmp command format to mem-policy=membind,mem-hostnode=0-1 like (Paolo)
V2->V3:
also handle Error in cpus parser (5/10)
split out common parser from cpus and hostnode parser (Bandan 6/10)
V3-V4:
rebase to request for comments

Bandan Das (1):
NUMA: Support multiple CPU ranges on -numa option

Wanlong Gao (9):
NUMA: Add numa_info structure to contain numa nodes info
NUMA: Add Linux libnuma detection
NUMA: parse guest numa nodes memory policy
NUMA: handle Error in cpus, mpol and hostnode parser
NUMA: split out the common range parser
NUMA: set guest numa nodes memory policy
NUMA: add qmp command set-mpol to set memory policy for NUMA node
NUMA: add hmp command set-mpol
NUMA: show host memory policy info in info numa command

 configure               |  32 ++++++
 cpus.c                  | 143 +++++++++++++++++++++++-
 hmp-commands.hx         |  16 +++
 hmp.c                   |  35 ++++++
 hmp.h                   |   1 +
 hw/i386/pc.c            |   4 +-
 hw/net/eepro100.c       |   1 -
 include/sysemu/sysemu.h |  20 +++-
 monitor.c               |  44 +++++++-
 qapi-schema.json        |  15 +++
 qemu-options.hx         |   3 +-
 qmp-commands.hx         |  35 ++++++
 vl.c                    | 285 +++++++++++++++++++++++++++++++++++-------------
 13 files changed, 553 insertions(+), 81 deletions(-)
#37 lore raw
Quoted 9 lines · Peter Huang(Peng)

Hi,Wanlong

From the patch discription below, seems that qemu numa only support cpu/memory node binding. As we know, binding is not the common usage due to VM migration may happen or the load balance would be disabled.
So, do we have any plan of generating virtual numa automatically?

For example, if we create a 16vCPU VM on a 4 8-core node physical box, we can automatically place it to two physical node, not by binding.

Do you mean automatically generating the NUMA topology configuration for the VM, or automatically migrating between physical nodes?

The guest-visible NUMA topology is part of the VM configuration. If automatically creating a config optimized for a specific host is desired, that's a task for other tools (like libvirt, or tools built on top of libvirt).

If you are talking about automatic migration of guest RAM and VCPUs, eventually the kernel may be able to do it efficiently, but we don't even have performance numbers for manually-tuned static binding setups to compare with (because static binding is not possible yet).

Quoted 89 lines · Wanlong Gao
Quoted 87 lines

As you know, QEMU can't direct it's memory allocation now, this may cause guest cross node access performance regression.
And, the worse thing is that if PCI-passthrough is used, direct-attached-device uses DMA transfer between device and qemu process. All pages of the guest will be pinned by get_user_pages().

KVM_ASSIGN_PCI_DEVICE ioctl
kvm_vm_ioctl_assign_device()

    =>kvm_assign_device()
      => kvm_iommu_map_memslots()
        => kvm_iommu_map_pages()
           => kvm_pin_pages()

So, with direct-attached-device, all guest page's page count will be +1 and any page migration will not work. AutoNUMA won't too.

So, we should set the guest nodes memory allocation policy before the pages are really mapped.

According to this patch set, we are able to set guest nodes memory policy like following:

-numa node,nodeid=0,mem=1024,cpus=0,mem-policy=membind,mem-hostnode=0-1 -numa node,nodeid=1,mem=1024,cpus=1,mem-policy=interleave,mem-hostnode=1

This supports "mem-policy={membind|interleave|preferred},mem-hostnode=[+|!]{all|N-N}" like format.

And patch 8/10 adds a QMP command "set-mpol" to set the memory policy for every guest nodes:
set-mpol nodeid=0 mem-policy=membind mem-hostnode=0-1

And patch 9/10 adds a monitor command "set-mpol" whose format like:
set-mpol 0 mem-policy=membind,mem-hostnode=0-1

And with patch 10/10, we can get the current memory policy of each guest node using monitor command "info numa", for example:

(qemu) info numa
2 nodes
node 0 cpus: 0
node 0 size: 1024 MB
node 0 mempolicy: membind=0,1
node 1 cpus: 1
node 1 size: 1024 MB
node 1 mempolicy: interleave=1

V1->V2:
change to use QemuOpts in numa options (Paolo)
handle Error in mpol parser (Paolo)
change qmp command format to mem-policy=membind,mem-hostnode=0-1 like (Paolo)
V2->V3:
also handle Error in cpus parser (5/10)
split out common parser from cpus and hostnode parser (Bandan 6/10)
V3-V4:
rebase to request for comments

Bandan Das (1):
NUMA: Support multiple CPU ranges on -numa option

Wanlong Gao (9):
NUMA: Add numa_info structure to contain numa nodes info
NUMA: Add Linux libnuma detection
NUMA: parse guest numa nodes memory policy
NUMA: handle Error in cpus, mpol and hostnode parser
NUMA: split out the common range parser
NUMA: set guest numa nodes memory policy
NUMA: add qmp command set-mpol to set memory policy for NUMA node
NUMA: add hmp command set-mpol
NUMA: show host memory policy info in info numa command

 configure               |  32 ++++++
 cpus.c                  | 143 +++++++++++++++++++++++-
 hmp-commands.hx         |  16 +++
 hmp.c                   |  35 ++++++
 hmp.h                   |   1 +
 hw/i386/pc.c            |   4 +-
 hw/net/eepro100.c       |   1 -
 include/sysemu/sysemu.h |  20 +++-
 monitor.c               |  44 +++++++-
 qapi-schema.json        |  15 +++
 qemu-options.hx         |   3 +-
 qmp-commands.hx         |  35 ++++++
 vl.c                    | 285 +++++++++++++++++++++++++++++++++++-------------
 13 files changed, 553 insertions(+), 81 deletions(-)
Signature · 1 line
Eduardo