Codebase list libvirt / debian/3.0.0-4+deb9u2 docs / cgroups.html
debian/3.0.0-4+deb9u2

Tree @debian/3.0.0-4+deb9u2 (Download .tar.gz)

cgroups.html @debian/3.0.0-4+deb9u2raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
        This file is autogenerated from cgroups.html.in
        Do not edit this file. Changes will be lost.
      -->
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="main.css" />
    <link rel="SHORTCUT ICON" href="32favicon.png" />
    <title>libvirt: Control Groups Resource Management</title>
    <meta name="description" content="libvirt, virtualization, virtualization API" />
  </head>
  <body>
    <div id="body">
      <div id="content">
        <h1>Control Groups Resource Management</h1>
        <ul><li>
            <a href="#requiredControllers">Required controllers</a>
          </li><li>
            <a href="#currentLayout">Current cgroups layout</a>
            <ul><li>
                <a href="#currentLayoutSystemd">Systemd cgroups integration</a>
                <ul><li>
                    <a href="#systemdScope">Systemd scope naming</a>
                  </li><li>
                    <a href="#systemdSlice">Systemd slice naming</a>
                  </li><li>
                    <a href="#systemdLayout">Systemd cgroup layout</a>
                  </li></ul>
              </li><li>
                <a href="#currentLayoutGeneric">Non-systemd cgroups layout</a>
              </li></ul>
          </li><li>
            <a href="#customPartiton">Using custom partitions</a>
            <ul><li>
                <a href="#createSystemd">Creating custom partitions (systemd)</a>
              </li><li>
                <a href="#createNonSystemd">Creating custom partitions (non-systemd)</a>
              </li></ul>
          </li><li>
            <a href="#resourceAPIs">Resource management APIs/commands</a>
          </li><li>
            <a href="#legacyLayout">Legacy cgroups layout</a>
          </li></ul>
        <p>
      The QEMU and LXC drivers make use of the Linux "Control Groups" facility
      for applying resource management to their virtual machines and containers.
    </p>
        <h2>
          <a name="requiredControllers" shape="rect" id="requiredControllers">Required controllers</a>
          <a class="headerlink" href="#requiredControllers" title="Permalink to this headline"></a>
        </h2>
        <p>
      The control groups filesystem supports multiple "controllers". By default
      the init system (such as systemd) should mount all controllers compiled
      into the kernel at <code>/sys/fs/cgroup/$CONTROLLER-NAME</code>. Libvirt
      will never attempt to mount any controllers itself, merely detect where
      they are mounted.
    </p>
        <p>
      The QEMU driver is capable of using the <code>cpuset</code>,
      <code>cpu</code>, <code>memory</code>, <code>blkio</code> and
      <code>devices</code> controllers. None of them are compulsory.
      If any controller is not mounted, the resource management APIs
      which use it will cease to operate. It is possible to explicitly
      turn off use of a controller, even when mounted, via the
      <code>/etc/libvirt/qemu.conf</code> configuration file.
    </p>
        <p>
      The LXC driver is capable of using the <code>cpuset</code>,
      <code>cpu</code>, <code>cpuacct</code>, <code>freezer</code>,
      <code>memory</code>, <code>blkio</code> and <code>devices</code>
      controllers. The <code>cpuacct</code>, <code>devices</code>
      and <code>memory</code> controllers are compulsory. Without
      them mounted, no containers can be started. If any of the
      other controllers are not mounted, the resource management APIs
      which use them will cease to operate.
    </p>
        <h2>
          <a name="currentLayout" shape="rect" id="currentLayout">Current cgroups layout</a>
          <a class="headerlink" href="#currentLayout" title="Permalink to this headline"></a>
        </h2>
        <p>
      As of libvirt 1.0.5 or later, the cgroups layout created by libvirt has been
      simplified, in order to facilitate the setup of resource control policies by
      administrators / management applications. The new layout is based on the concepts
      of "partitions" and "consumers". A "consumer" is a cgroup which holds the
      processes for a single virtual machine or container. A "partition" is a cgroup
      which does not contain any processes, but can have resource controls applied.
      A "partition" will have zero or more child directories which may be either
      "consumer" or "partition".
    </p>
        <p>
      As of libvirt 1.1.1 or later, the cgroups layout will have some slight
      differences when running on a host with systemd 205 or later. The overall
      tree structure is the same, but there are some differences in the naming
      conventions for the cgroup directories. Thus the following docs split
      in two, one describing systemd hosts and the other non-systemd hosts.
    </p>
        <h3>
          <a name="currentLayoutSystemd" shape="rect" id="currentLayoutSystemd">Systemd cgroups integration</a>
          <a class="headerlink" href="#currentLayoutSystemd" title="Permalink to this headline"></a>
        </h3>
        <p>
      On hosts which use systemd, each consumer maps to a systemd scope unit,
      while partitions map to a system slice unit.
    </p>
        <h4>
          <a name="systemdScope" shape="rect" id="systemdScope">Systemd scope naming</a>
          <a class="headerlink" href="#systemdScope" title="Permalink to this headline"></a>
        </h4>
        <p>
      The systemd convention is for the scope name of virtual machines / containers
      to be of the general format <code>machine-$NAME.scope</code>. Libvirt forms the
      <code>$NAME</code> part of this by concatenating the driver type with the name
      of the guest, and then escaping any systemd reserved characters.
      So for a guest <code>demo</code> running under the <code>lxc</code> driver,
      we get a <code>$NAME</code> of <code>lxc-demo</code> which when escaped is
      <code>lxc\x2ddemo</code>. So the complete scope name is <code>machine-lxc\x2ddemo.scope</code>.
      The scope names map directly to the cgroup directory names.
    </p>
        <h4>
          <a name="systemdSlice" shape="rect" id="systemdSlice">Systemd slice naming</a>
          <a class="headerlink" href="#systemdSlice" title="Permalink to this headline"></a>
        </h4>
        <p>
      The systemd convention for slice naming is that a slice should include the
      name of all of its parents prepended on its own name. So for a libvirt
      partition <code>/machine/engineering/testing</code>, the slice name will
      be <code>machine-engineering-testing.slice</code>. Again the slice names
      map directly to the cgroup directory names. Systemd creates three top level
      slices by default, <code>system.slice</code> <code>user.slice</code> and
      <code>machine.slice</code>. All virtual machines or containers created
      by libvirt will be associated with <code>machine.slice</code> by default.
    </p>
        <h4>
          <a name="systemdLayout" shape="rect" id="systemdLayout">Systemd cgroup layout</a>
          <a class="headerlink" href="#systemdLayout" title="Permalink to this headline"></a>
        </h4>
        <p>
      Given this, a possible systemd cgroups layout involving 3 qemu guests,
      3 lxc containers and 3 custom child slices, would be:
    </p>
        <pre xml:space="preserve">
$ROOT
  |
  +- system.slice
  |   |
  |   +- libvirtd.service
  |
  +- machine.slice
      |
      +- machine-qemu\x2dvm1.scope
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- machine-qemu\x2dvm2.scope
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- machine-qemu\x2dvm3.scope
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- machine-engineering.slice
      |   |
      |   +- machine-engineering-testing.slice
      |   |   |
      |   |   +- machine-lxc\x2dcontainer1.scope
      |   |
      |   +- machine-engineering-production.slice
      |       |
      |       +- machine-lxc\x2dcontainer2.scope
      |
      +- machine-marketing.slice
          |
          +- machine-lxc\x2dcontainer3.scope
    </pre>
        <h3>
          <a name="currentLayoutGeneric" shape="rect" id="currentLayoutGeneric">Non-systemd cgroups layout</a>
          <a class="headerlink" href="#currentLayoutGeneric" title="Permalink to this headline"></a>
        </h3>
        <p>
      On hosts which do not use systemd, each consumer has a corresponding cgroup
      named <code>$VMNAME.libvirt-{qemu,lxc}</code>. Each consumer is associated
      with exactly one partition, which also have a corresponding cgroup usually
      named <code>$PARTNAME.partition</code>. The exceptions to this naming rule
      are the three top level default partitions, named <code>/system</code> (for
      system services), <code>/user</code> (for user login sessions) and
      <code>/machine</code> (for virtual machines and containers). By default
      every consumer will of course be associated with the <code>/machine</code>
      partition.
    </p>
        <p>
      Given this, a possible systemd cgroups layout involving 3 qemu guests,
      3 lxc containers and 2 custom child slices, would be:
    </p>
        <pre xml:space="preserve">
$ROOT
  |
  +- system
  |   |
  |   +- libvirtd.service
  |
  +- machine
      |
      +- vm1.libvirt-qemu
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- vm2.libvirt-qemu
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- vm3.libvirt-qemu
      |   |
      |   +- emulator
      |   +- vcpu0
      |   +- vcpu1
      |
      +- engineering.partition
      |   |
      |   +- testing.partition
      |   |   |
      |   |   +- container1.libvirt-lxc
      |   |
      |   +- production.partition
      |       |
      |       +- container2.libvirt-lxc
      |
      +- marketing.partition
          |
          +- container3.libvirt-lxc
    </pre>
        <h2>
          <a name="customPartiton" shape="rect" id="customPartiton">Using custom partitions</a>
          <a class="headerlink" href="#customPartiton" title="Permalink to this headline"></a>
        </h2>
        <p>
      If there is a need to apply resource constraints to groups of
      virtual machines or containers, then the single default
      partition <code>/machine</code> may not be sufficiently
      flexible. The administrator may wish to sub-divide the
      default partition, for example into "testing" and "production"
      partitions, and then assign each guest to a specific
      sub-partition. This is achieved via a small element addition
      to the guest domain XML config, just below the main <code>domain</code>
      element
    </p>
        <pre xml:space="preserve">
...
&lt;resource&gt;
  &lt;partition&gt;/machine/production&lt;/partition&gt;
&lt;/resource&gt;
...
    </pre>
        <p>
      Note that the partition names in the guest XML are using a
      generic naming format, not the low level naming convention
      required by the underlying host OS. That is, you should not include
      any of the <code>.partition</code> or <code>.slice</code>
      suffixes in the XML config. Given a partition name
      <code>/machine/production</code>, libvirt will automatically
      apply the platform specific translation required to get
      <code>/machine/production.partition</code> (non-systemd)
      or <code>/machine.slice/machine-production.slice</code>
      (systemd) as the underlying cgroup name
    </p>
        <p>
      Libvirt will not auto-create the cgroups directory to back
      this partition. In the future, libvirt / virsh will provide
      APIs / commands to create custom partitions, but currently
      this is left as an exercise for the administrator.
    </p>
        <p>
      <strong>Note:</strong> the ability to place guests in custom
      partitions is only available with libvirt &gt;= 1.0.5, using
      the new cgroup layout. The legacy cgroups layout described
      later in this document did not support customization per guest.
    </p>
        <h3>
          <a name="createSystemd" shape="rect" id="createSystemd">Creating custom partitions (systemd)</a>
          <a class="headerlink" href="#createSystemd" title="Permalink to this headline"></a>
        </h3>
        <p>
      Given the XML config above, the admin on a systemd based host would
      need to create a unit file <code>/etc/systemd/system/machine-production.slice</code>
    </p>
        <pre xml:space="preserve">
# cat &gt; /etc/systemd/system/machine-testing.slice &lt;&lt;EOF
[Unit]
Description=VM testing slice
Before=slices.target
Wants=machine.slice
EOF
# systemctl start machine-testing.slice
    </pre>
        <h3>
          <a name="createNonSystemd" shape="rect" id="createNonSystemd">Creating custom partitions (non-systemd)</a>
          <a class="headerlink" href="#createNonSystemd" title="Permalink to this headline"></a>
        </h3>
        <p>
      Given the XML config above, the admin on a non-systemd based host
      would need to create a cgroup named '/machine/production.partition'
    </p>
        <pre xml:space="preserve">
# cd /sys/fs/cgroup
# for i in blkio cpu,cpuacct cpuset devices freezer memory net_cls perf_event
  do
    mkdir $i/machine/production.partition
  done
# for i in cpuset.cpus  cpuset.mems
  do
    cat cpuset/machine/$i &gt; cpuset/machine/production.partition/$i
  done
</pre>
        <h2>
          <a name="resourceAPIs" shape="rect" id="resourceAPIs">Resource management APIs/commands</a>
          <a class="headerlink" href="#resourceAPIs" title="Permalink to this headline"></a>
        </h2>
        <p>
      Since libvirt aims to provide an API which is portable across
      hypervisors, the concept of cgroups is not exposed directly
      in the API or XML configuration. It is considered to be an
      internal implementation detail. Instead libvirt provides a
      set of APIs for applying resource controls, which are then
      mapped to corresponding cgroup tunables
    </p>
        <h3>Scheduler tuning</h3>
        <p>
     Parameters from the "cpu" controller are exposed via the
     <code>schedinfo</code> command in virsh.
    </p>
        <pre xml:space="preserve">
# virsh schedinfo demo
Scheduler      : posix
cpu_shares     : 1024
vcpu_period    : 100000
vcpu_quota     : -1
emulator_period: 100000
emulator_quota : -1</pre>
        <h3>Block I/O tuning</h3>
        <p>
     Parameters from the "blkio" controller are exposed via the
     <code>bkliotune</code> command in virsh.
    </p>
        <pre xml:space="preserve">
# virsh blkiotune demo
weight         : 500
device_weight  : </pre>
        <h3>Memory tuning</h3>
        <p>
     Parameters from the "memory" controller are exposed via the
     <code>memtune</code> command in virsh.
    </p>
        <pre xml:space="preserve">
# virsh memtune demo
hard_limit     : 580192
soft_limit     : unlimited
swap_hard_limit: unlimited
    </pre>
        <h3>Network tuning</h3>
        <p>
      The <code>net_cls</code> is not currently used. Instead traffic
      filter policies are set directly against individual virtual
      network interfaces.
    </p>
        <h2>
          <a name="legacyLayout" shape="rect" id="legacyLayout">Legacy cgroups layout</a>
          <a class="headerlink" href="#legacyLayout" title="Permalink to this headline"></a>
        </h2>
        <p>
      Prior to libvirt 1.0.5, the cgroups layout created by libvirt was different
      from that described above, and did not allow for administrator customization.
      Libvirt used a fixed, 3-level hierarchy <code>libvirt/{qemu,lxc}/$VMNAME</code>
      which was rooted at the point in the hierarchy where libvirtd itself was
      located. So if libvirtd was placed at <code>/system/libvirtd.service</code>
      by systemd, the groups for each virtual machine / container would be located
      at <code>/system/libvirtd.service/libvirt/{qemu,lxc}/$VMNAME</code>. In addition
      to this, the QEMU drivers further child groups for each vCPU thread and the
      emulator thread(s). This leads to a hierarchy that looked like
    </p>
        <pre xml:space="preserve">
$ROOT
  |
  +- system
      |
      +- libvirtd.service
           |
           +- libvirt
               |
               +- qemu
               |   |
               |   +- vm1
               |   |   |
               |   |   +- emulator
               |   |   +- vcpu0
               |   |   +- vcpu1
               |   |
               |   +- vm2
               |   |   |
               |   |   +- emulator
               |   |   +- vcpu0
               |   |   +- vcpu1
               |   |
               |   +- vm3
               |       |
               |       +- emulator
               |       +- vcpu0
               |       +- vcpu1
               |
               +- lxc
                   |
                   +- container1
                   |
                   +- container2
                   |
                   +- container3
    </pre>
        <p>
      Although current releases are much improved, historically the use of deep
      hierarchies has had a significant negative impact on the kernel scalability.
      The legacy libvirt cgroups layout highlighted these problems, to the detriment
      of the performance of virtual machines and containers.
    </p>
      </div>
    </div>
    <div id="nav">
      <div id="home">
        <a href="index.html">Home</a>
      </div>
      <div id="jumplinks">
        <ul><li>
            <a href="downloads.html">Download</a>
          </li><li>
            <a href="contribute.html">Contribute</a>
          </li><li>
            <a href="docs.html">Learn</a>
          </li></ul>
      </div>
      <div id="search">
        <form action="search.php" enctype="application/x-www-form-urlencoded" method="get"><div>
            <input name="query" type="text" size="12" value="" />
            <input name="submit" type="submit" value="Go" />
          </div></form>
      </div>
    </div>
    <div id="footer">
      <div id="contact">
        <h3>Contact</h3>
        <ul><li>
            <a href="contact.html#email">email</a>
          </li><li>
            <a href="contact.html#irc">irc</a>
          </li></ul>
      </div>
      <div id="community">
        <h3>Community</h3>
        <ul><li>
            <a href="https://twitter.com/hashtag/libvirt">twitter</a>
          </li><li>
            <a href="https://plus.google.com/communities/109522598353007505282">google+</a>
          </li><li>
            <a href="http://stackoverflow.com/questions/tagged/libvirt">stackoverflow</a>
          </li><li>
            <a href="http://serverfault.com/questions/tagged/libvirt">serverfault</a>
          </li></ul>
      </div>
      <div id="conduct">
            Participants in the libvirt project agree to abide by <a href="governance.html#codeofconduct">the project code of conduct</a></div>
      <br class="clear" />
    </div>
  </body>
</html>