| 1 |
=encoding utf8 |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
xl-numa-placement - Guest Automatic NUMA Placement in libxl and xl |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
=head2 Rationale |
| 10 |
|
| 11 |
NUMA (which stands for Non-Uniform Memory Access) means that the memory |
| 12 |
accessing times of a program running on a CPU depends on the relative |
| 13 |
distance between that CPU and that memory. In fact, most of the NUMA |
| 14 |
systems are built in such a way that each processor has its local memory, |
| 15 |
on which it can operate very fast. On the other hand, getting and storing |
| 16 |
data from and on remote memory (that is, memory local to some other processor) |
| 17 |
is quite more complex and slow. On these machines, a NUMA node is usually |
| 18 |
defined as a set of processor cores (typically a physical CPU package) and |
| 19 |
the memory directly attached to the set of cores. |
| 20 |
|
| 21 |
NUMA awareness becomes very important as soon as many domains start |
| 22 |
running memory-intensive workloads on a shared host. In fact, the cost |
| 23 |
of accessing non node-local memory locations is very high, and the |
| 24 |
performance degradation is likely to be noticeable. |
| 25 |
|
| 26 |
For more information, have a look at the L<Xen NUMA Introduction|https://wiki.xenproject.org/wiki/Xen_on_NUMA_Machines> |
| 27 |
page on the Wiki. |
| 28 |
|
| 29 |
|
| 30 |
=head2 Xen and NUMA machines: the concept of I<node-affinity> |
| 31 |
|
| 32 |
The Xen hypervisor deals with NUMA machines throughout the concept of |
| 33 |
I<node-affinity>. The node-affinity of a domain is the set of NUMA nodes |
| 34 |
of the host where the memory for the domain is being allocated (mostly, |
| 35 |
at domain creation time). This is, at least in principle, different and |
| 36 |
unrelated with the vCPU (hard and soft, see below) scheduling affinity, |
| 37 |
which instead is the set of pCPUs where the vCPU is allowed (or prefers) |
| 38 |
to run. |
| 39 |
|
| 40 |
Of course, despite the fact that they belong to and affect different |
| 41 |
subsystems, the domain node-affinity and the vCPUs affinity are not |
| 42 |
completely independent. |
| 43 |
In fact, if the domain node-affinity is not explicitly specified by the |
| 44 |
user, via the proper libxl calls or xl config item, it will be computed |
| 45 |
basing on the vCPUs' scheduling affinity. |
| 46 |
|
| 47 |
Notice that, even if the node affinity of a domain may change on-line, |
| 48 |
it is very important to "place" the domain correctly when it is first |
| 49 |
created, as the most of its memory is allocated at that time and can |
| 50 |
not (for now) be moved easily. |
| 51 |
|
| 52 |
|
| 53 |
=head2 Placing via pinning and cpupools |
| 54 |
|
| 55 |
The simplest way of placing a domain on a NUMA node is setting the hard |
| 56 |
scheduling affinity of the domain's vCPUs to the pCPUs of the node. This |
| 57 |
also goes under the name of vCPU pinning, and can be done through the |
| 58 |
"cpus=" option in the config file (more about this below). Another option |
| 59 |
is to pool together the pCPUs spanning the node and put the domain in |
| 60 |
such a I<cpupool> with the "pool=" config option (as documented in our |
| 61 |
L<Wiki|https://wiki.xenproject.org/wiki/Cpupools_Howto>). |
| 62 |
|
| 63 |
In both the above cases, the domain will not be able to execute outside |
| 64 |
the specified set of pCPUs for any reasons, even if all those pCPUs are |
| 65 |
busy doing something else while there are others, idle, pCPUs. |
| 66 |
|
| 67 |
So, when doing this, local memory accesses are 100% guaranteed, but that |
| 68 |
may come at he cost of some load imbalances. |
| 69 |
|
| 70 |
|
| 71 |
=head2 NUMA aware scheduling |
| 72 |
|
| 73 |
If using the credit1 scheduler, and starting from Xen 4.3, the scheduler |
| 74 |
itself always tries to run the domain's vCPUs on one of the nodes in |
| 75 |
its node-affinity. Only if that turns out to be impossible, it will just |
| 76 |
pick any free pCPU. Locality of access is less guaranteed than in the |
| 77 |
pinning case, but that comes along with better chances to exploit all |
| 78 |
the host resources (e.g., the pCPUs). |
| 79 |
|
| 80 |
Starting from Xen 4.5, credit1 supports two forms of affinity: hard and |
| 81 |
soft, both on a per-vCPU basis. This means each vCPU can have its own |
| 82 |
soft affinity, stating where such vCPU prefers to execute on. This is |
| 83 |
less strict than what it (also starting from 4.5) is called hard affinity, |
| 84 |
as the vCPU can potentially run everywhere, it just prefers some pCPUs |
| 85 |
rather than others. |
| 86 |
In Xen 4.5, therefore, NUMA-aware scheduling is achieved by matching the |
| 87 |
soft affinity of the vCPUs of a domain with its node-affinity. |
| 88 |
|
| 89 |
In fact, as it was for 4.3, if all the pCPUs in a vCPU's soft affinity |
| 90 |
are busy, it is possible for the domain to run outside from there. The |
| 91 |
idea is that slower execution (due to remote memory accesses) is still |
| 92 |
better than no execution at all (as it would happen with pinning). For |
| 93 |
this reason, NUMA aware scheduling has the potential of bringing |
| 94 |
substantial performances benefits, although this will depend on the |
| 95 |
workload. |
| 96 |
|
| 97 |
Notice that, for each vCPU, the following three scenarios are possible: |
| 98 |
|
| 99 |
=over |
| 100 |
|
| 101 |
=item * |
| 102 |
|
| 103 |
a vCPU I<is pinned> to some pCPUs and I<does not have> any soft affinity |
| 104 |
In this case, the vCPU is always scheduled on one of the pCPUs to which |
| 105 |
it is pinned, without any specific preference among them. |
| 106 |
|
| 107 |
|
| 108 |
=item * |
| 109 |
|
| 110 |
a vCPU I<has> its own soft affinity and I<is not> pinned to any particular |
| 111 |
pCPU. In this case, the vCPU can run on every pCPU. Nevertheless, the |
| 112 |
scheduler will try to have it running on one of the pCPUs in its soft |
| 113 |
affinity; |
| 114 |
|
| 115 |
|
| 116 |
=item * |
| 117 |
|
| 118 |
a vCPU I<has> its own vCPU soft affinity and I<is also> pinned to some |
| 119 |
pCPUs. In this case, the vCPU is always scheduled on one of the pCPUs |
| 120 |
onto which it is pinned, with, among them, a preference for the ones |
| 121 |
that also forms its soft affinity. In case pinning and soft affinity |
| 122 |
form two disjoint sets of pCPUs, pinning "wins", and the soft affinity |
| 123 |
is just ignored. |
| 124 |
|
| 125 |
|
| 126 |
=back |
| 127 |
|
| 128 |
|
| 129 |
=head2 Guest placement in xl |
| 130 |
|
| 131 |
If using xl for creating and managing guests, it is very easy to ask for |
| 132 |
both manual or automatic placement of them across the host's NUMA nodes. |
| 133 |
|
| 134 |
Note that xm/xend does a very similar thing, the only differences being |
| 135 |
the details of the heuristics adopted for automatic placement (see below), |
| 136 |
and the lack of support (in both xm/xend and the Xen versions where that |
| 137 |
was the default toolstack) for NUMA aware scheduling. |
| 138 |
|
| 139 |
|
| 140 |
=head2 Placing the guest manually |
| 141 |
|
| 142 |
Thanks to the "cpus=" option, it is possible to specify where a domain |
| 143 |
should be created and scheduled on, directly in its config file. This |
| 144 |
affects NUMA placement and memory accesses as, in this case, the |
| 145 |
hypervisor constructs the node-affinity of a VM basing right on its |
| 146 |
vCPU pinning when it is created. |
| 147 |
|
| 148 |
This is very simple and effective, but requires the user/system |
| 149 |
administrator to explicitly specify the pinning for each and every domain, |
| 150 |
or Xen won't be able to guarantee the locality for their memory accesses. |
| 151 |
|
| 152 |
That, of course, also mean the vCPUs of the domain will only be able to |
| 153 |
execute on those same pCPUs. |
| 154 |
|
| 155 |
It is is also possible to have a "cpus_soft=" option in the xl config file, |
| 156 |
to specify the soft affinity for all the vCPUs of the domain. This affects |
| 157 |
the NUMA placement in the following way: |
| 158 |
|
| 159 |
=over |
| 160 |
|
| 161 |
=item * |
| 162 |
|
| 163 |
if only "cpus_soft=" is present, the VM's node-affinity will be equal |
| 164 |
to the nodes to which the pCPUs in the soft affinity mask belong; |
| 165 |
|
| 166 |
|
| 167 |
=item * |
| 168 |
|
| 169 |
if both "cpus_soft=" and "cpus=" are present, the VM's node-affinity |
| 170 |
will be equal to the nodes to which the pCPUs present both in hard and |
| 171 |
soft affinity belong. |
| 172 |
|
| 173 |
|
| 174 |
=back |
| 175 |
|
| 176 |
|
| 177 |
=head2 Placing the guest automatically |
| 178 |
|
| 179 |
If neither "cpus=" nor "cpus_soft=" are present in the config file, libxl |
| 180 |
tries to figure out on its own on which node(s) the domain could fit best. |
| 181 |
If it finds one (some), the domain's node affinity get set to there, |
| 182 |
and both memory allocations and NUMA aware scheduling (for the credit |
| 183 |
scheduler and starting from Xen 4.3) will comply with it. Starting from |
| 184 |
Xen 4.5, this also means that the mask resulting from this "fitting" |
| 185 |
procedure will become the soft affinity of all the vCPUs of the domain. |
| 186 |
|
| 187 |
It is worthwhile noting that optimally fitting a set of VMs on the NUMA |
| 188 |
nodes of an host is an incarnation of the Bin Packing Problem. In fact, |
| 189 |
the various VMs with different memory sizes are the items to be packed, |
| 190 |
and the host nodes are the bins. As such problem is known to be NP-hard, |
| 191 |
we will be using some heuristics. |
| 192 |
|
| 193 |
The first thing to do is find the nodes or the sets of nodes (from now |
| 194 |
on referred to as 'candidates') that have enough free memory and enough |
| 195 |
physical CPUs for accommodating the new domain. The idea is to find a |
| 196 |
spot for the domain with at least as much free memory as it has configured |
| 197 |
to have, and as much pCPUs as it has vCPUs. After that, the actual |
| 198 |
decision on which candidate to pick happens accordingly to the following |
| 199 |
heuristics: |
| 200 |
|
| 201 |
=over |
| 202 |
|
| 203 |
=item * |
| 204 |
|
| 205 |
candidates involving fewer nodes are considered better. In case |
| 206 |
two (or more) candidates span the same number of nodes, |
| 207 |
|
| 208 |
|
| 209 |
=item * |
| 210 |
|
| 211 |
candidates with a smaller number of vCPUs runnable on them (due |
| 212 |
to previous placement and/or plain vCPU pinning) are considered |
| 213 |
better. In case the same number of vCPUs can run on two (or more) |
| 214 |
candidates, |
| 215 |
|
| 216 |
|
| 217 |
=item * |
| 218 |
|
| 219 |
the candidate with with the greatest amount of free memory is |
| 220 |
considered to be the best one. |
| 221 |
|
| 222 |
|
| 223 |
=back |
| 224 |
|
| 225 |
Giving preference to candidates with fewer nodes ensures better |
| 226 |
performance for the guest, as it avoid spreading its memory among |
| 227 |
different nodes. Favoring candidates with fewer vCPUs already runnable |
| 228 |
there ensures a good balance of the overall host load. Finally, if more |
| 229 |
candidates fulfil these criteria, prioritizing the nodes that have the |
| 230 |
largest amounts of free memory helps keeping the memory fragmentation |
| 231 |
small, and maximizes the probability of being able to put more domains |
| 232 |
there. |
| 233 |
|
| 234 |
|
| 235 |
=head2 Guest placement in libxl |
| 236 |
|
| 237 |
xl achieves automatic NUMA placement because that is what libxl does |
| 238 |
by default. No API is provided (yet) for modifying the behaviour of |
| 239 |
the placement algorithm. However, if your program is calling libxl, |
| 240 |
it is possible to set the C<numa_placement> build info key to C<false> |
| 241 |
(it is C<true> by default) with something like the below, to prevent |
| 242 |
any placement from happening: |
| 243 |
|
| 244 |
libxl_defbool_set(&domain_build_info->numa_placement, false); |
| 245 |
|
| 246 |
Also, if C<numa_placement> is set to C<true>, the domain's vCPUs must |
| 247 |
not be pinned (i.e., C<<< domain_build_info->cpumap >>> must have all its |
| 248 |
bits set, as it is by default), or domain creation will fail with |
| 249 |
C<ERROR_INVAL>. |
| 250 |
|
| 251 |
Starting from Xen 4.3, in case automatic placement happens (and is |
| 252 |
successful), it will affect the domain's node-affinity and I<not> its |
| 253 |
vCPU pinning. Namely, the domain's vCPUs will not be pinned to any |
| 254 |
pCPU on the host, but the memory from the domain will come from the |
| 255 |
selected node(s) and the NUMA aware scheduling (if the credit scheduler |
| 256 |
is in use) will try to keep the domain's vCPUs there as much as possible. |
| 257 |
|
| 258 |
Besides than that, looking and/or tweaking the placement algorithm |
| 259 |
search "Automatic NUMA placement" in libxl_internal.h. |
| 260 |
|
| 261 |
Note this may change in future versions of Xen/libxl. |
| 262 |
|
| 263 |
|
| 264 |
=head2 Xen < 4.5 |
| 265 |
|
| 266 |
The concept of vCPU soft affinity has been introduced for the first time |
| 267 |
in Xen 4.5. In 4.3, it is the domain's node-affinity that drives the |
| 268 |
NUMA-aware scheduler. The main difference is soft affinity is per-vCPU, |
| 269 |
and so each vCPU can have its own mask of pCPUs, while node-affinity is |
| 270 |
per-domain, that is the equivalent of having all the vCPUs with the same |
| 271 |
soft affinity. |
| 272 |
|
| 273 |
|
| 274 |
=head2 Xen < 4.3 |
| 275 |
|
| 276 |
As NUMA aware scheduling is a new feature of Xen 4.3, things are a little |
| 277 |
bit different for earlier version of Xen. If no "cpus=" option is specified |
| 278 |
and Xen 4.2 is in use, the automatic placement algorithm still runs, but |
| 279 |
the results is used to I<pin> the vCPUs of the domain to the output node(s). |
| 280 |
This is consistent with what was happening with xm/xend. |
| 281 |
|
| 282 |
On a version of Xen earlier than 4.2, there is not automatic placement at |
| 283 |
all in xl or libxl, and hence no node-affinity, vCPU affinity or pinning |
| 284 |
being introduced/modified. |
| 285 |
|
| 286 |
|
| 287 |
=head2 Limitations |
| 288 |
|
| 289 |
Analyzing various possible placement solutions is what makes the |
| 290 |
algorithm flexible and quite effective. However, that also means |
| 291 |
it won't scale well to systems with arbitrary number of nodes. |
| 292 |
For this reason, automatic placement is disabled (with a warning) |
| 293 |
if it is requested on a host with more than 16 NUMA nodes. |