Tejun Heo | 6c29209 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 1 | |
| 2 | Control Group v2 |
| 3 | |
| 4 | October, 2015 Tejun Heo <tj@kernel.org> |
| 5 | |
| 6 | This is the authoritative documentation on the design, interface and |
| 7 | conventions of cgroup v2. It describes all userland-visible aspects |
| 8 | of cgroup including core and specific controller behaviors. All |
| 9 | future changes must be reflected in this document. Documentation for |
W. Trevor King | 9a2ddda | 2016-01-27 13:01:52 -0800 | [diff] [blame] | 10 | v1 is available under Documentation/cgroup-v1/. |
Tejun Heo | 6c29209 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 11 | |
| 12 | CONTENTS |
| 13 | |
| 14 | 1. Introduction |
| 15 | 1-1. Terminology |
| 16 | 1-2. What is cgroup? |
| 17 | 2. Basic Operations |
| 18 | 2-1. Mounting |
| 19 | 2-2. Organizing Processes |
| 20 | 2-3. [Un]populated Notification |
| 21 | 2-4. Controlling Controllers |
| 22 | 2-4-1. Enabling and Disabling |
| 23 | 2-4-2. Top-down Constraint |
| 24 | 2-4-3. No Internal Process Constraint |
| 25 | 2-5. Delegation |
| 26 | 2-5-1. Model of Delegation |
| 27 | 2-5-2. Delegation Containment |
| 28 | 2-6. Guidelines |
| 29 | 2-6-1. Organize Once and Control |
| 30 | 2-6-2. Avoid Name Collisions |
| 31 | 3. Resource Distribution Models |
| 32 | 3-1. Weights |
| 33 | 3-2. Limits |
| 34 | 3-3. Protections |
| 35 | 3-4. Allocations |
| 36 | 4. Interface Files |
| 37 | 4-1. Format |
| 38 | 4-2. Conventions |
| 39 | 4-3. Core Interface Files |
| 40 | 5. Controllers |
| 41 | 5-1. CPU |
| 42 | 5-1-1. CPU Interface Files |
| 43 | 5-2. Memory |
| 44 | 5-2-1. Memory Interface Files |
| 45 | 5-2-2. Usage Guidelines |
| 46 | 5-2-3. Memory Ownership |
| 47 | 5-3. IO |
| 48 | 5-3-1. IO Interface Files |
| 49 | 5-3-2. Writeback |
| 50 | P. Information on Kernel Programming |
| 51 | P-1. Filesystem Support for Writeback |
| 52 | D. Deprecated v1 Core Features |
| 53 | R. Issues with v1 and Rationales for v2 |
| 54 | R-1. Multiple Hierarchies |
| 55 | R-2. Thread Granularity |
| 56 | R-3. Competition Between Inner Nodes and Threads |
| 57 | R-4. Other Interface Issues |
| 58 | R-5. Controller Issues and Remedies |
| 59 | R-5-1. Memory |
| 60 | |
| 61 | |
| 62 | 1. Introduction |
| 63 | |
| 64 | 1-1. Terminology |
| 65 | |
| 66 | "cgroup" stands for "control group" and is never capitalized. The |
| 67 | singular form is used to designate the whole feature and also as a |
| 68 | qualifier as in "cgroup controllers". When explicitly referring to |
| 69 | multiple individual control groups, the plural form "cgroups" is used. |
| 70 | |
| 71 | |
| 72 | 1-2. What is cgroup? |
| 73 | |
| 74 | cgroup is a mechanism to organize processes hierarchically and |
| 75 | distribute system resources along the hierarchy in a controlled and |
| 76 | configurable manner. |
| 77 | |
| 78 | cgroup is largely composed of two parts - the core and controllers. |
| 79 | cgroup core is primarily responsible for hierarchically organizing |
| 80 | processes. A cgroup controller is usually responsible for |
| 81 | distributing a specific type of system resource along the hierarchy |
| 82 | although there are utility controllers which serve purposes other than |
| 83 | resource distribution. |
| 84 | |
| 85 | cgroups form a tree structure and every process in the system belongs |
| 86 | to one and only one cgroup. All threads of a process belong to the |
| 87 | same cgroup. On creation, all processes are put in the cgroup that |
| 88 | the parent process belongs to at the time. A process can be migrated |
| 89 | to another cgroup. Migration of a process doesn't affect already |
| 90 | existing descendant processes. |
| 91 | |
| 92 | Following certain structural constraints, controllers may be enabled or |
| 93 | disabled selectively on a cgroup. All controller behaviors are |
| 94 | hierarchical - if a controller is enabled on a cgroup, it affects all |
| 95 | processes which belong to the cgroups consisting the inclusive |
| 96 | sub-hierarchy of the cgroup. When a controller is enabled on a nested |
| 97 | cgroup, it always restricts the resource distribution further. The |
| 98 | restrictions set closer to the root in the hierarchy can not be |
| 99 | overridden from further away. |
| 100 | |
| 101 | |
| 102 | 2. Basic Operations |
| 103 | |
| 104 | 2-1. Mounting |
| 105 | |
| 106 | Unlike v1, cgroup v2 has only single hierarchy. The cgroup v2 |
| 107 | hierarchy can be mounted with the following mount command. |
| 108 | |
| 109 | # mount -t cgroup2 none $MOUNT_POINT |
| 110 | |
| 111 | cgroup2 filesystem has the magic number 0x63677270 ("cgrp"). All |
| 112 | controllers which support v2 and are not bound to a v1 hierarchy are |
| 113 | automatically bound to the v2 hierarchy and show up at the root. |
| 114 | Controllers which are not in active use in the v2 hierarchy can be |
| 115 | bound to other hierarchies. This allows mixing v2 hierarchy with the |
| 116 | legacy v1 multiple hierarchies in a fully backward compatible way. |
| 117 | |
| 118 | A controller can be moved across hierarchies only after the controller |
| 119 | is no longer referenced in its current hierarchy. Because per-cgroup |
| 120 | controller states are destroyed asynchronously and controllers may |
| 121 | have lingering references, a controller may not show up immediately on |
| 122 | the v2 hierarchy after the final umount of the previous hierarchy. |
| 123 | Similarly, a controller should be fully disabled to be moved out of |
| 124 | the unified hierarchy and it may take some time for the disabled |
| 125 | controller to become available for other hierarchies; furthermore, due |
| 126 | to inter-controller dependencies, other controllers may need to be |
| 127 | disabled too. |
| 128 | |
| 129 | While useful for development and manual configurations, moving |
| 130 | controllers dynamically between the v2 and other hierarchies is |
| 131 | strongly discouraged for production use. It is recommended to decide |
| 132 | the hierarchies and controller associations before starting using the |
| 133 | controllers after system boot. |
| 134 | |
| 135 | |
| 136 | 2-2. Organizing Processes |
| 137 | |
| 138 | Initially, only the root cgroup exists to which all processes belong. |
| 139 | A child cgroup can be created by creating a sub-directory. |
| 140 | |
| 141 | # mkdir $CGROUP_NAME |
| 142 | |
| 143 | A given cgroup may have multiple child cgroups forming a tree |
| 144 | structure. Each cgroup has a read-writable interface file |
| 145 | "cgroup.procs". When read, it lists the PIDs of all processes which |
| 146 | belong to the cgroup one-per-line. The PIDs are not ordered and the |
| 147 | same PID may show up more than once if the process got moved to |
| 148 | another cgroup and then back or the PID got recycled while reading. |
| 149 | |
| 150 | A process can be migrated into a cgroup by writing its PID to the |
| 151 | target cgroup's "cgroup.procs" file. Only one process can be migrated |
| 152 | on a single write(2) call. If a process is composed of multiple |
| 153 | threads, writing the PID of any thread migrates all threads of the |
| 154 | process. |
| 155 | |
| 156 | When a process forks a child process, the new process is born into the |
| 157 | cgroup that the forking process belongs to at the time of the |
| 158 | operation. After exit, a process stays associated with the cgroup |
| 159 | that it belonged to at the time of exit until it's reaped; however, a |
| 160 | zombie process does not appear in "cgroup.procs" and thus can't be |
| 161 | moved to another cgroup. |
| 162 | |
| 163 | A cgroup which doesn't have any children or live processes can be |
| 164 | destroyed by removing the directory. Note that a cgroup which doesn't |
| 165 | have any children and is associated only with zombie processes is |
| 166 | considered empty and can be removed. |
| 167 | |
| 168 | # rmdir $CGROUP_NAME |
| 169 | |
| 170 | "/proc/$PID/cgroup" lists a process's cgroup membership. If legacy |
| 171 | cgroup is in use in the system, this file may contain multiple lines, |
| 172 | one for each hierarchy. The entry for cgroup v2 is always in the |
| 173 | format "0::$PATH". |
| 174 | |
| 175 | # cat /proc/842/cgroup |
| 176 | ... |
| 177 | 0::/test-cgroup/test-cgroup-nested |
| 178 | |
| 179 | If the process becomes a zombie and the cgroup it was associated with |
| 180 | is removed subsequently, " (deleted)" is appended to the path. |
| 181 | |
| 182 | # cat /proc/842/cgroup |
| 183 | ... |
| 184 | 0::/test-cgroup/test-cgroup-nested (deleted) |
| 185 | |
| 186 | |
| 187 | 2-3. [Un]populated Notification |
| 188 | |
| 189 | Each non-root cgroup has a "cgroup.events" file which contains |
| 190 | "populated" field indicating whether the cgroup's sub-hierarchy has |
| 191 | live processes in it. Its value is 0 if there is no live process in |
| 192 | the cgroup and its descendants; otherwise, 1. poll and [id]notify |
| 193 | events are triggered when the value changes. This can be used, for |
| 194 | example, to start a clean-up operation after all processes of a given |
| 195 | sub-hierarchy have exited. The populated state updates and |
| 196 | notifications are recursive. Consider the following sub-hierarchy |
| 197 | where the numbers in the parentheses represent the numbers of processes |
| 198 | in each cgroup. |
| 199 | |
| 200 | A(4) - B(0) - C(1) |
| 201 | \ D(0) |
| 202 | |
| 203 | A, B and C's "populated" fields would be 1 while D's 0. After the one |
| 204 | process in C exits, B and C's "populated" fields would flip to "0" and |
| 205 | file modified events will be generated on the "cgroup.events" files of |
| 206 | both cgroups. |
| 207 | |
| 208 | |
| 209 | 2-4. Controlling Controllers |
| 210 | |
| 211 | 2-4-1. Enabling and Disabling |
| 212 | |
| 213 | Each cgroup has a "cgroup.controllers" file which lists all |
| 214 | controllers available for the cgroup to enable. |
| 215 | |
| 216 | # cat cgroup.controllers |
| 217 | cpu io memory |
| 218 | |
| 219 | No controller is enabled by default. Controllers can be enabled and |
| 220 | disabled by writing to the "cgroup.subtree_control" file. |
| 221 | |
| 222 | # echo "+cpu +memory -io" > cgroup.subtree_control |
| 223 | |
| 224 | Only controllers which are listed in "cgroup.controllers" can be |
| 225 | enabled. When multiple operations are specified as above, either they |
| 226 | all succeed or fail. If multiple operations on the same controller |
| 227 | are specified, the last one is effective. |
| 228 | |
| 229 | Enabling a controller in a cgroup indicates that the distribution of |
| 230 | the target resource across its immediate children will be controlled. |
| 231 | Consider the following sub-hierarchy. The enabled controllers are |
| 232 | listed in parentheses. |
| 233 | |
| 234 | A(cpu,memory) - B(memory) - C() |
| 235 | \ D() |
| 236 | |
| 237 | As A has "cpu" and "memory" enabled, A will control the distribution |
| 238 | of CPU cycles and memory to its children, in this case, B. As B has |
| 239 | "memory" enabled but not "CPU", C and D will compete freely on CPU |
| 240 | cycles but their division of memory available to B will be controlled. |
| 241 | |
| 242 | As a controller regulates the distribution of the target resource to |
| 243 | the cgroup's children, enabling it creates the controller's interface |
| 244 | files in the child cgroups. In the above example, enabling "cpu" on B |
| 245 | would create the "cpu." prefixed controller interface files in C and |
| 246 | D. Likewise, disabling "memory" from B would remove the "memory." |
| 247 | prefixed controller interface files from C and D. This means that the |
| 248 | controller interface files - anything which doesn't start with |
| 249 | "cgroup." are owned by the parent rather than the cgroup itself. |
| 250 | |
| 251 | |
| 252 | 2-4-2. Top-down Constraint |
| 253 | |
| 254 | Resources are distributed top-down and a cgroup can further distribute |
| 255 | a resource only if the resource has been distributed to it from the |
| 256 | parent. This means that all non-root "cgroup.subtree_control" files |
| 257 | can only contain controllers which are enabled in the parent's |
| 258 | "cgroup.subtree_control" file. A controller can be enabled only if |
| 259 | the parent has the controller enabled and a controller can't be |
| 260 | disabled if one or more children have it enabled. |
| 261 | |
| 262 | |
| 263 | 2-4-3. No Internal Process Constraint |
| 264 | |
| 265 | Non-root cgroups can only distribute resources to their children when |
| 266 | they don't have any processes of their own. In other words, only |
| 267 | cgroups which don't contain any processes can have controllers enabled |
| 268 | in their "cgroup.subtree_control" files. |
| 269 | |
| 270 | This guarantees that, when a controller is looking at the part of the |
| 271 | hierarchy which has it enabled, processes are always only on the |
| 272 | leaves. This rules out situations where child cgroups compete against |
| 273 | internal processes of the parent. |
| 274 | |
| 275 | The root cgroup is exempt from this restriction. Root contains |
| 276 | processes and anonymous resource consumption which can't be associated |
| 277 | with any other cgroups and requires special treatment from most |
| 278 | controllers. How resource consumption in the root cgroup is governed |
| 279 | is up to each controller. |
| 280 | |
| 281 | Note that the restriction doesn't get in the way if there is no |
| 282 | enabled controller in the cgroup's "cgroup.subtree_control". This is |
| 283 | important as otherwise it wouldn't be possible to create children of a |
| 284 | populated cgroup. To control resource distribution of a cgroup, the |
| 285 | cgroup must create children and transfer all its processes to the |
| 286 | children before enabling controllers in its "cgroup.subtree_control" |
| 287 | file. |
| 288 | |
| 289 | |
| 290 | 2-5. Delegation |
| 291 | |
| 292 | 2-5-1. Model of Delegation |
| 293 | |
| 294 | A cgroup can be delegated to a less privileged user by granting write |
| 295 | access of the directory and its "cgroup.procs" file to the user. Note |
| 296 | that resource control interface files in a given directory control the |
| 297 | distribution of the parent's resources and thus must not be delegated |
| 298 | along with the directory. |
| 299 | |
| 300 | Once delegated, the user can build sub-hierarchy under the directory, |
| 301 | organize processes as it sees fit and further distribute the resources |
| 302 | it received from the parent. The limits and other settings of all |
| 303 | resource controllers are hierarchical and regardless of what happens |
| 304 | in the delegated sub-hierarchy, nothing can escape the resource |
| 305 | restrictions imposed by the parent. |
| 306 | |
| 307 | Currently, cgroup doesn't impose any restrictions on the number of |
| 308 | cgroups in or nesting depth of a delegated sub-hierarchy; however, |
| 309 | this may be limited explicitly in the future. |
| 310 | |
| 311 | |
| 312 | 2-5-2. Delegation Containment |
| 313 | |
| 314 | A delegated sub-hierarchy is contained in the sense that processes |
| 315 | can't be moved into or out of the sub-hierarchy by the delegatee. For |
| 316 | a process with a non-root euid to migrate a target process into a |
| 317 | cgroup by writing its PID to the "cgroup.procs" file, the following |
| 318 | conditions must be met. |
| 319 | |
| 320 | - The writer's euid must match either uid or suid of the target process. |
| 321 | |
| 322 | - The writer must have write access to the "cgroup.procs" file. |
| 323 | |
| 324 | - The writer must have write access to the "cgroup.procs" file of the |
| 325 | common ancestor of the source and destination cgroups. |
| 326 | |
| 327 | The above three constraints ensure that while a delegatee may migrate |
| 328 | processes around freely in the delegated sub-hierarchy it can't pull |
| 329 | in from or push out to outside the sub-hierarchy. |
| 330 | |
| 331 | For an example, let's assume cgroups C0 and C1 have been delegated to |
| 332 | user U0 who created C00, C01 under C0 and C10 under C1 as follows and |
| 333 | all processes under C0 and C1 belong to U0. |
| 334 | |
| 335 | ~~~~~~~~~~~~~ - C0 - C00 |
| 336 | ~ cgroup ~ \ C01 |
| 337 | ~ hierarchy ~ |
| 338 | ~~~~~~~~~~~~~ - C1 - C10 |
| 339 | |
| 340 | Let's also say U0 wants to write the PID of a process which is |
| 341 | currently in C10 into "C00/cgroup.procs". U0 has write access to the |
| 342 | file and uid match on the process; however, the common ancestor of the |
| 343 | source cgroup C10 and the destination cgroup C00 is above the points |
| 344 | of delegation and U0 would not have write access to its "cgroup.procs" |
| 345 | files and thus the write will be denied with -EACCES. |
| 346 | |
| 347 | |
| 348 | 2-6. Guidelines |
| 349 | |
| 350 | 2-6-1. Organize Once and Control |
| 351 | |
| 352 | Migrating a process across cgroups is a relatively expensive operation |
| 353 | and stateful resources such as memory are not moved together with the |
| 354 | process. This is an explicit design decision as there often exist |
| 355 | inherent trade-offs between migration and various hot paths in terms |
| 356 | of synchronization cost. |
| 357 | |
| 358 | As such, migrating processes across cgroups frequently as a means to |
| 359 | apply different resource restrictions is discouraged. A workload |
| 360 | should be assigned to a cgroup according to the system's logical and |
| 361 | resource structure once on start-up. Dynamic adjustments to resource |
| 362 | distribution can be made by changing controller configuration through |
| 363 | the interface files. |
| 364 | |
| 365 | |
| 366 | 2-6-2. Avoid Name Collisions |
| 367 | |
| 368 | Interface files for a cgroup and its children cgroups occupy the same |
| 369 | directory and it is possible to create children cgroups which collide |
| 370 | with interface files. |
| 371 | |
| 372 | All cgroup core interface files are prefixed with "cgroup." and each |
| 373 | controller's interface files are prefixed with the controller name and |
| 374 | a dot. A controller's name is composed of lower case alphabets and |
| 375 | '_'s but never begins with an '_' so it can be used as the prefix |
| 376 | character for collision avoidance. Also, interface file names won't |
| 377 | start or end with terms which are often used in categorizing workloads |
| 378 | such as job, service, slice, unit or workload. |
| 379 | |
| 380 | cgroup doesn't do anything to prevent name collisions and it's the |
| 381 | user's responsibility to avoid them. |
| 382 | |
| 383 | |
| 384 | 3. Resource Distribution Models |
| 385 | |
| 386 | cgroup controllers implement several resource distribution schemes |
| 387 | depending on the resource type and expected use cases. This section |
| 388 | describes major schemes in use along with their expected behaviors. |
| 389 | |
| 390 | |
| 391 | 3-1. Weights |
| 392 | |
| 393 | A parent's resource is distributed by adding up the weights of all |
| 394 | active children and giving each the fraction matching the ratio of its |
| 395 | weight against the sum. As only children which can make use of the |
| 396 | resource at the moment participate in the distribution, this is |
| 397 | work-conserving. Due to the dynamic nature, this model is usually |
| 398 | used for stateless resources. |
| 399 | |
| 400 | All weights are in the range [1, 10000] with the default at 100. This |
| 401 | allows symmetric multiplicative biases in both directions at fine |
| 402 | enough granularity while staying in the intuitive range. |
| 403 | |
| 404 | As long as the weight is in range, all configuration combinations are |
| 405 | valid and there is no reason to reject configuration changes or |
| 406 | process migrations. |
| 407 | |
| 408 | "cpu.weight" proportionally distributes CPU cycles to active children |
| 409 | and is an example of this type. |
| 410 | |
| 411 | |
| 412 | 3-2. Limits |
| 413 | |
| 414 | A child can only consume upto the configured amount of the resource. |
| 415 | Limits can be over-committed - the sum of the limits of children can |
| 416 | exceed the amount of resource available to the parent. |
| 417 | |
| 418 | Limits are in the range [0, max] and defaults to "max", which is noop. |
| 419 | |
| 420 | As limits can be over-committed, all configuration combinations are |
| 421 | valid and there is no reason to reject configuration changes or |
| 422 | process migrations. |
| 423 | |
| 424 | "io.max" limits the maximum BPS and/or IOPS that a cgroup can consume |
| 425 | on an IO device and is an example of this type. |
| 426 | |
| 427 | |
| 428 | 3-3. Protections |
| 429 | |
| 430 | A cgroup is protected to be allocated upto the configured amount of |
| 431 | the resource if the usages of all its ancestors are under their |
| 432 | protected levels. Protections can be hard guarantees or best effort |
| 433 | soft boundaries. Protections can also be over-committed in which case |
| 434 | only upto the amount available to the parent is protected among |
| 435 | children. |
| 436 | |
| 437 | Protections are in the range [0, max] and defaults to 0, which is |
| 438 | noop. |
| 439 | |
| 440 | As protections can be over-committed, all configuration combinations |
| 441 | are valid and there is no reason to reject configuration changes or |
| 442 | process migrations. |
| 443 | |
| 444 | "memory.low" implements best-effort memory protection and is an |
| 445 | example of this type. |
| 446 | |
| 447 | |
| 448 | 3-4. Allocations |
| 449 | |
| 450 | A cgroup is exclusively allocated a certain amount of a finite |
| 451 | resource. Allocations can't be over-committed - the sum of the |
| 452 | allocations of children can not exceed the amount of resource |
| 453 | available to the parent. |
| 454 | |
| 455 | Allocations are in the range [0, max] and defaults to 0, which is no |
| 456 | resource. |
| 457 | |
| 458 | As allocations can't be over-committed, some configuration |
| 459 | combinations are invalid and should be rejected. Also, if the |
| 460 | resource is mandatory for execution of processes, process migrations |
| 461 | may be rejected. |
| 462 | |
| 463 | "cpu.rt.max" hard-allocates realtime slices and is an example of this |
| 464 | type. |
| 465 | |
| 466 | |
| 467 | 4. Interface Files |
| 468 | |
| 469 | 4-1. Format |
| 470 | |
| 471 | All interface files should be in one of the following formats whenever |
| 472 | possible. |
| 473 | |
| 474 | New-line separated values |
| 475 | (when only one value can be written at once) |
| 476 | |
| 477 | VAL0\n |
| 478 | VAL1\n |
| 479 | ... |
| 480 | |
| 481 | Space separated values |
| 482 | (when read-only or multiple values can be written at once) |
| 483 | |
| 484 | VAL0 VAL1 ...\n |
| 485 | |
| 486 | Flat keyed |
| 487 | |
| 488 | KEY0 VAL0\n |
| 489 | KEY1 VAL1\n |
| 490 | ... |
| 491 | |
| 492 | Nested keyed |
| 493 | |
| 494 | KEY0 SUB_KEY0=VAL00 SUB_KEY1=VAL01... |
| 495 | KEY1 SUB_KEY0=VAL10 SUB_KEY1=VAL11... |
| 496 | ... |
| 497 | |
| 498 | For a writable file, the format for writing should generally match |
| 499 | reading; however, controllers may allow omitting later fields or |
| 500 | implement restricted shortcuts for most common use cases. |
| 501 | |
| 502 | For both flat and nested keyed files, only the values for a single key |
| 503 | can be written at a time. For nested keyed files, the sub key pairs |
| 504 | may be specified in any order and not all pairs have to be specified. |
| 505 | |
| 506 | |
| 507 | 4-2. Conventions |
| 508 | |
| 509 | - Settings for a single feature should be contained in a single file. |
| 510 | |
| 511 | - The root cgroup should be exempt from resource control and thus |
| 512 | shouldn't have resource control interface files. Also, |
| 513 | informational files on the root cgroup which end up showing global |
| 514 | information available elsewhere shouldn't exist. |
| 515 | |
| 516 | - If a controller implements weight based resource distribution, its |
| 517 | interface file should be named "weight" and have the range [1, |
| 518 | 10000] with 100 as the default. The values are chosen to allow |
| 519 | enough and symmetric bias in both directions while keeping it |
| 520 | intuitive (the default is 100%). |
| 521 | |
| 522 | - If a controller implements an absolute resource guarantee and/or |
| 523 | limit, the interface files should be named "min" and "max" |
| 524 | respectively. If a controller implements best effort resource |
| 525 | guarantee and/or limit, the interface files should be named "low" |
| 526 | and "high" respectively. |
| 527 | |
| 528 | In the above four control files, the special token "max" should be |
| 529 | used to represent upward infinity for both reading and writing. |
| 530 | |
| 531 | - If a setting has a configurable default value and keyed specific |
| 532 | overrides, the default entry should be keyed with "default" and |
| 533 | appear as the first entry in the file. |
| 534 | |
| 535 | The default value can be updated by writing either "default $VAL" or |
| 536 | "$VAL". |
| 537 | |
| 538 | When writing to update a specific override, "default" can be used as |
| 539 | the value to indicate removal of the override. Override entries |
| 540 | with "default" as the value must not appear when read. |
| 541 | |
| 542 | For example, a setting which is keyed by major:minor device numbers |
| 543 | with integer values may look like the following. |
| 544 | |
| 545 | # cat cgroup-example-interface-file |
| 546 | default 150 |
| 547 | 8:0 300 |
| 548 | |
| 549 | The default value can be updated by |
| 550 | |
| 551 | # echo 125 > cgroup-example-interface-file |
| 552 | |
| 553 | or |
| 554 | |
| 555 | # echo "default 125" > cgroup-example-interface-file |
| 556 | |
| 557 | An override can be set by |
| 558 | |
| 559 | # echo "8:16 170" > cgroup-example-interface-file |
| 560 | |
| 561 | and cleared by |
| 562 | |
| 563 | # echo "8:0 default" > cgroup-example-interface-file |
| 564 | # cat cgroup-example-interface-file |
| 565 | default 125 |
| 566 | 8:16 170 |
| 567 | |
| 568 | - For events which are not very high frequency, an interface file |
| 569 | "events" should be created which lists event key value pairs. |
| 570 | Whenever a notifiable event happens, file modified event should be |
| 571 | generated on the file. |
| 572 | |
| 573 | |
| 574 | 4-3. Core Interface Files |
| 575 | |
| 576 | All cgroup core files are prefixed with "cgroup." |
| 577 | |
| 578 | cgroup.procs |
| 579 | |
| 580 | A read-write new-line separated values file which exists on |
| 581 | all cgroups. |
| 582 | |
| 583 | When read, it lists the PIDs of all processes which belong to |
| 584 | the cgroup one-per-line. The PIDs are not ordered and the |
| 585 | same PID may show up more than once if the process got moved |
| 586 | to another cgroup and then back or the PID got recycled while |
| 587 | reading. |
| 588 | |
| 589 | A PID can be written to migrate the process associated with |
| 590 | the PID to the cgroup. The writer should match all of the |
| 591 | following conditions. |
| 592 | |
| 593 | - Its euid is either root or must match either uid or suid of |
| 594 | the target process. |
| 595 | |
| 596 | - It must have write access to the "cgroup.procs" file. |
| 597 | |
| 598 | - It must have write access to the "cgroup.procs" file of the |
| 599 | common ancestor of the source and destination cgroups. |
| 600 | |
| 601 | When delegating a sub-hierarchy, write access to this file |
| 602 | should be granted along with the containing directory. |
| 603 | |
| 604 | cgroup.controllers |
| 605 | |
| 606 | A read-only space separated values file which exists on all |
| 607 | cgroups. |
| 608 | |
| 609 | It shows space separated list of all controllers available to |
| 610 | the cgroup. The controllers are not ordered. |
| 611 | |
| 612 | cgroup.subtree_control |
| 613 | |
| 614 | A read-write space separated values file which exists on all |
| 615 | cgroups. Starts out empty. |
| 616 | |
| 617 | When read, it shows space separated list of the controllers |
| 618 | which are enabled to control resource distribution from the |
| 619 | cgroup to its children. |
| 620 | |
| 621 | Space separated list of controllers prefixed with '+' or '-' |
| 622 | can be written to enable or disable controllers. A controller |
| 623 | name prefixed with '+' enables the controller and '-' |
| 624 | disables. If a controller appears more than once on the list, |
| 625 | the last one is effective. When multiple enable and disable |
| 626 | operations are specified, either all succeed or all fail. |
| 627 | |
| 628 | cgroup.events |
| 629 | |
| 630 | A read-only flat-keyed file which exists on non-root cgroups. |
| 631 | The following entries are defined. Unless specified |
| 632 | otherwise, a value change in this file generates a file |
| 633 | modified event. |
| 634 | |
| 635 | populated |
| 636 | |
| 637 | 1 if the cgroup or its descendants contains any live |
| 638 | processes; otherwise, 0. |
| 639 | |
| 640 | |
| 641 | 5. Controllers |
| 642 | |
| 643 | 5-1. CPU |
| 644 | |
| 645 | [NOTE: The interface for the cpu controller hasn't been merged yet] |
| 646 | |
| 647 | The "cpu" controllers regulates distribution of CPU cycles. This |
| 648 | controller implements weight and absolute bandwidth limit models for |
| 649 | normal scheduling policy and absolute bandwidth allocation model for |
| 650 | realtime scheduling policy. |
| 651 | |
| 652 | |
| 653 | 5-1-1. CPU Interface Files |
| 654 | |
| 655 | All time durations are in microseconds. |
| 656 | |
| 657 | cpu.stat |
| 658 | |
| 659 | A read-only flat-keyed file which exists on non-root cgroups. |
| 660 | |
| 661 | It reports the following six stats. |
| 662 | |
| 663 | usage_usec |
| 664 | user_usec |
| 665 | system_usec |
| 666 | nr_periods |
| 667 | nr_throttled |
| 668 | throttled_usec |
| 669 | |
| 670 | cpu.weight |
| 671 | |
| 672 | A read-write single value file which exists on non-root |
| 673 | cgroups. The default is "100". |
| 674 | |
| 675 | The weight in the range [1, 10000]. |
| 676 | |
| 677 | cpu.max |
| 678 | |
| 679 | A read-write two value file which exists on non-root cgroups. |
| 680 | The default is "max 100000". |
| 681 | |
| 682 | The maximum bandwidth limit. It's in the following format. |
| 683 | |
| 684 | $MAX $PERIOD |
| 685 | |
| 686 | which indicates that the group may consume upto $MAX in each |
| 687 | $PERIOD duration. "max" for $MAX indicates no limit. If only |
| 688 | one number is written, $MAX is updated. |
| 689 | |
| 690 | cpu.rt.max |
| 691 | |
| 692 | [NOTE: The semantics of this file is still under discussion and the |
| 693 | interface hasn't been merged yet] |
| 694 | |
| 695 | A read-write two value file which exists on all cgroups. |
| 696 | The default is "0 100000". |
| 697 | |
| 698 | The maximum realtime runtime allocation. Over-committing |
| 699 | configurations are disallowed and process migrations are |
| 700 | rejected if not enough bandwidth is available. It's in the |
| 701 | following format. |
| 702 | |
| 703 | $MAX $PERIOD |
| 704 | |
| 705 | which indicates that the group may consume upto $MAX in each |
| 706 | $PERIOD duration. If only one number is written, $MAX is |
| 707 | updated. |
| 708 | |
| 709 | |
| 710 | 5-2. Memory |
| 711 | |
| 712 | The "memory" controller regulates distribution of memory. Memory is |
| 713 | stateful and implements both limit and protection models. Due to the |
| 714 | intertwining between memory usage and reclaim pressure and the |
| 715 | stateful nature of memory, the distribution model is relatively |
| 716 | complex. |
| 717 | |
| 718 | While not completely water-tight, all major memory usages by a given |
| 719 | cgroup are tracked so that the total memory consumption can be |
| 720 | accounted and controlled to a reasonable extent. Currently, the |
| 721 | following types of memory usages are tracked. |
| 722 | |
| 723 | - Userland memory - page cache and anonymous memory. |
| 724 | |
| 725 | - Kernel data structures such as dentries and inodes. |
| 726 | |
| 727 | - TCP socket buffers. |
| 728 | |
| 729 | The above list may expand in the future for better coverage. |
| 730 | |
| 731 | |
| 732 | 5-2-1. Memory Interface Files |
| 733 | |
| 734 | All memory amounts are in bytes. If a value which is not aligned to |
| 735 | PAGE_SIZE is written, the value may be rounded up to the closest |
| 736 | PAGE_SIZE multiple when read back. |
| 737 | |
| 738 | memory.current |
| 739 | |
| 740 | A read-only single value file which exists on non-root |
| 741 | cgroups. |
| 742 | |
| 743 | The total amount of memory currently being used by the cgroup |
| 744 | and its descendants. |
| 745 | |
| 746 | memory.low |
| 747 | |
| 748 | A read-write single value file which exists on non-root |
| 749 | cgroups. The default is "0". |
| 750 | |
| 751 | Best-effort memory protection. If the memory usages of a |
| 752 | cgroup and all its ancestors are below their low boundaries, |
| 753 | the cgroup's memory won't be reclaimed unless memory can be |
| 754 | reclaimed from unprotected cgroups. |
| 755 | |
| 756 | Putting more memory than generally available under this |
| 757 | protection is discouraged. |
| 758 | |
| 759 | memory.high |
| 760 | |
| 761 | A read-write single value file which exists on non-root |
| 762 | cgroups. The default is "max". |
| 763 | |
| 764 | Memory usage throttle limit. This is the main mechanism to |
| 765 | control memory usage of a cgroup. If a cgroup's usage goes |
| 766 | over the high boundary, the processes of the cgroup are |
| 767 | throttled and put under heavy reclaim pressure. |
| 768 | |
| 769 | Going over the high limit never invokes the OOM killer and |
| 770 | under extreme conditions the limit may be breached. |
| 771 | |
| 772 | memory.max |
| 773 | |
| 774 | A read-write single value file which exists on non-root |
| 775 | cgroups. The default is "max". |
| 776 | |
| 777 | Memory usage hard limit. This is the final protection |
| 778 | mechanism. If a cgroup's memory usage reaches this limit and |
| 779 | can't be reduced, the OOM killer is invoked in the cgroup. |
| 780 | Under certain circumstances, the usage may go over the limit |
| 781 | temporarily. |
| 782 | |
| 783 | This is the ultimate protection mechanism. As long as the |
| 784 | high limit is used and monitored properly, this limit's |
| 785 | utility is limited to providing the final safety net. |
| 786 | |
| 787 | memory.events |
| 788 | |
| 789 | A read-only flat-keyed file which exists on non-root cgroups. |
| 790 | The following entries are defined. Unless specified |
| 791 | otherwise, a value change in this file generates a file |
| 792 | modified event. |
| 793 | |
| 794 | low |
| 795 | |
| 796 | The number of times the cgroup is reclaimed due to |
| 797 | high memory pressure even though its usage is under |
| 798 | the low boundary. This usually indicates that the low |
| 799 | boundary is over-committed. |
| 800 | |
| 801 | high |
| 802 | |
| 803 | The number of times processes of the cgroup are |
| 804 | throttled and routed to perform direct memory reclaim |
| 805 | because the high memory boundary was exceeded. For a |
| 806 | cgroup whose memory usage is capped by the high limit |
| 807 | rather than global memory pressure, this event's |
| 808 | occurrences are expected. |
| 809 | |
| 810 | max |
| 811 | |
| 812 | The number of times the cgroup's memory usage was |
| 813 | about to go over the max boundary. If direct reclaim |
| 814 | fails to bring it down, the OOM killer is invoked. |
| 815 | |
| 816 | oom |
| 817 | |
| 818 | The number of times the OOM killer has been invoked in |
| 819 | the cgroup. This may not exactly match the number of |
| 820 | processes killed but should generally be close. |
| 821 | |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 822 | memory.stat |
| 823 | |
| 824 | A read-only flat-keyed file which exists on non-root cgroups. |
| 825 | |
| 826 | This breaks down the cgroup's memory footprint into different |
| 827 | types of memory, type-specific details, and other information |
| 828 | on the state and past events of the memory management system. |
| 829 | |
| 830 | All memory amounts are in bytes. |
| 831 | |
| 832 | The entries are ordered to be human readable, and new entries |
| 833 | can show up in the middle. Don't rely on items remaining in a |
| 834 | fixed position; use the keys to look up specific values! |
| 835 | |
| 836 | anon |
| 837 | |
| 838 | Amount of memory used in anonymous mappings such as |
| 839 | brk(), sbrk(), and mmap(MAP_ANONYMOUS) |
| 840 | |
| 841 | file |
| 842 | |
| 843 | Amount of memory used to cache filesystem data, |
| 844 | including tmpfs and shared memory. |
| 845 | |
Vladimir Davydov | 27ee57c | 2016-03-17 14:17:35 -0700 | [diff] [blame^] | 846 | slab |
| 847 | |
| 848 | Amount of memory used for storing in-kernel data |
| 849 | structures. |
| 850 | |
Johannes Weiner | 4758e19 | 2016-02-02 16:57:41 -0800 | [diff] [blame] | 851 | sock |
| 852 | |
| 853 | Amount of memory used in network transmission buffers |
| 854 | |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 855 | file_mapped |
| 856 | |
| 857 | Amount of cached filesystem data mapped with mmap() |
| 858 | |
| 859 | file_dirty |
| 860 | |
| 861 | Amount of cached filesystem data that was modified but |
| 862 | not yet written back to disk |
| 863 | |
| 864 | file_writeback |
| 865 | |
| 866 | Amount of cached filesystem data that was modified and |
| 867 | is currently being written back to disk |
| 868 | |
| 869 | inactive_anon |
| 870 | active_anon |
| 871 | inactive_file |
| 872 | active_file |
| 873 | unevictable |
| 874 | |
| 875 | Amount of memory, swap-backed and filesystem-backed, |
| 876 | on the internal memory management lists used by the |
| 877 | page reclaim algorithm |
| 878 | |
Vladimir Davydov | 27ee57c | 2016-03-17 14:17:35 -0700 | [diff] [blame^] | 879 | slab_reclaimable |
| 880 | |
| 881 | Part of "slab" that might be reclaimed, such as |
| 882 | dentries and inodes. |
| 883 | |
| 884 | slab_unreclaimable |
| 885 | |
| 886 | Part of "slab" that cannot be reclaimed on memory |
| 887 | pressure. |
| 888 | |
Johannes Weiner | 587d9f7 | 2016-01-20 15:03:19 -0800 | [diff] [blame] | 889 | pgfault |
| 890 | |
| 891 | Total number of page faults incurred |
| 892 | |
| 893 | pgmajfault |
| 894 | |
| 895 | Number of major page faults incurred |
| 896 | |
Vladimir Davydov | 3e24b19 | 2016-01-20 15:03:13 -0800 | [diff] [blame] | 897 | memory.swap.current |
| 898 | |
| 899 | A read-only single value file which exists on non-root |
| 900 | cgroups. |
| 901 | |
| 902 | The total amount of swap currently being used by the cgroup |
| 903 | and its descendants. |
| 904 | |
| 905 | memory.swap.max |
| 906 | |
| 907 | A read-write single value file which exists on non-root |
| 908 | cgroups. The default is "max". |
| 909 | |
| 910 | Swap usage hard limit. If a cgroup's swap usage reaches this |
| 911 | limit, anonymous meomry of the cgroup will not be swapped out. |
| 912 | |
Tejun Heo | 6c29209 | 2015-11-16 11:13:34 -0500 | [diff] [blame] | 913 | |
| 914 | 5-2-2. General Usage |
| 915 | |
| 916 | "memory.high" is the main mechanism to control memory usage. |
| 917 | Over-committing on high limit (sum of high limits > available memory) |
| 918 | and letting global memory pressure to distribute memory according to |
| 919 | usage is a viable strategy. |
| 920 | |
| 921 | Because breach of the high limit doesn't trigger the OOM killer but |
| 922 | throttles the offending cgroup, a management agent has ample |
| 923 | opportunities to monitor and take appropriate actions such as granting |
| 924 | more memory or terminating the workload. |
| 925 | |
| 926 | Determining whether a cgroup has enough memory is not trivial as |
| 927 | memory usage doesn't indicate whether the workload can benefit from |
| 928 | more memory. For example, a workload which writes data received from |
| 929 | network to a file can use all available memory but can also operate as |
| 930 | performant with a small amount of memory. A measure of memory |
| 931 | pressure - how much the workload is being impacted due to lack of |
| 932 | memory - is necessary to determine whether a workload needs more |
| 933 | memory; unfortunately, memory pressure monitoring mechanism isn't |
| 934 | implemented yet. |
| 935 | |
| 936 | |
| 937 | 5-2-3. Memory Ownership |
| 938 | |
| 939 | A memory area is charged to the cgroup which instantiated it and stays |
| 940 | charged to the cgroup until the area is released. Migrating a process |
| 941 | to a different cgroup doesn't move the memory usages that it |
| 942 | instantiated while in the previous cgroup to the new cgroup. |
| 943 | |
| 944 | A memory area may be used by processes belonging to different cgroups. |
| 945 | To which cgroup the area will be charged is in-deterministic; however, |
| 946 | over time, the memory area is likely to end up in a cgroup which has |
| 947 | enough memory allowance to avoid high reclaim pressure. |
| 948 | |
| 949 | If a cgroup sweeps a considerable amount of memory which is expected |
| 950 | to be accessed repeatedly by other cgroups, it may make sense to use |
| 951 | POSIX_FADV_DONTNEED to relinquish the ownership of memory areas |
| 952 | belonging to the affected files to ensure correct memory ownership. |
| 953 | |
| 954 | |
| 955 | 5-3. IO |
| 956 | |
| 957 | The "io" controller regulates the distribution of IO resources. This |
| 958 | controller implements both weight based and absolute bandwidth or IOPS |
| 959 | limit distribution; however, weight based distribution is available |
| 960 | only if cfq-iosched is in use and neither scheme is available for |
| 961 | blk-mq devices. |
| 962 | |
| 963 | |
| 964 | 5-3-1. IO Interface Files |
| 965 | |
| 966 | io.stat |
| 967 | |
| 968 | A read-only nested-keyed file which exists on non-root |
| 969 | cgroups. |
| 970 | |
| 971 | Lines are keyed by $MAJ:$MIN device numbers and not ordered. |
| 972 | The following nested keys are defined. |
| 973 | |
| 974 | rbytes Bytes read |
| 975 | wbytes Bytes written |
| 976 | rios Number of read IOs |
| 977 | wios Number of write IOs |
| 978 | |
| 979 | An example read output follows. |
| 980 | |
| 981 | 8:16 rbytes=1459200 wbytes=314773504 rios=192 wios=353 |
| 982 | 8:0 rbytes=90430464 wbytes=299008000 rios=8950 wios=1252 |
| 983 | |
| 984 | io.weight |
| 985 | |
| 986 | A read-write flat-keyed file which exists on non-root cgroups. |
| 987 | The default is "default 100". |
| 988 | |
| 989 | The first line is the default weight applied to devices |
| 990 | without specific override. The rest are overrides keyed by |
| 991 | $MAJ:$MIN device numbers and not ordered. The weights are in |
| 992 | the range [1, 10000] and specifies the relative amount IO time |
| 993 | the cgroup can use in relation to its siblings. |
| 994 | |
| 995 | The default weight can be updated by writing either "default |
| 996 | $WEIGHT" or simply "$WEIGHT". Overrides can be set by writing |
| 997 | "$MAJ:$MIN $WEIGHT" and unset by writing "$MAJ:$MIN default". |
| 998 | |
| 999 | An example read output follows. |
| 1000 | |
| 1001 | default 100 |
| 1002 | 8:16 200 |
| 1003 | 8:0 50 |
| 1004 | |
| 1005 | io.max |
| 1006 | |
| 1007 | A read-write nested-keyed file which exists on non-root |
| 1008 | cgroups. |
| 1009 | |
| 1010 | BPS and IOPS based IO limit. Lines are keyed by $MAJ:$MIN |
| 1011 | device numbers and not ordered. The following nested keys are |
| 1012 | defined. |
| 1013 | |
| 1014 | rbps Max read bytes per second |
| 1015 | wbps Max write bytes per second |
| 1016 | riops Max read IO operations per second |
| 1017 | wiops Max write IO operations per second |
| 1018 | |
| 1019 | When writing, any number of nested key-value pairs can be |
| 1020 | specified in any order. "max" can be specified as the value |
| 1021 | to remove a specific limit. If the same key is specified |
| 1022 | multiple times, the outcome is undefined. |
| 1023 | |
| 1024 | BPS and IOPS are measured in each IO direction and IOs are |
| 1025 | delayed if limit is reached. Temporary bursts are allowed. |
| 1026 | |
| 1027 | Setting read limit at 2M BPS and write at 120 IOPS for 8:16. |
| 1028 | |
| 1029 | echo "8:16 rbps=2097152 wiops=120" > io.max |
| 1030 | |
| 1031 | Reading returns the following. |
| 1032 | |
| 1033 | 8:16 rbps=2097152 wbps=max riops=max wiops=120 |
| 1034 | |
| 1035 | Write IOPS limit can be removed by writing the following. |
| 1036 | |
| 1037 | echo "8:16 wiops=max" > io.max |
| 1038 | |
| 1039 | Reading now returns the following. |
| 1040 | |
| 1041 | 8:16 rbps=2097152 wbps=max riops=max wiops=max |
| 1042 | |
| 1043 | |
| 1044 | 5-3-2. Writeback |
| 1045 | |
| 1046 | Page cache is dirtied through buffered writes and shared mmaps and |
| 1047 | written asynchronously to the backing filesystem by the writeback |
| 1048 | mechanism. Writeback sits between the memory and IO domains and |
| 1049 | regulates the proportion of dirty memory by balancing dirtying and |
| 1050 | write IOs. |
| 1051 | |
| 1052 | The io controller, in conjunction with the memory controller, |
| 1053 | implements control of page cache writeback IOs. The memory controller |
| 1054 | defines the memory domain that dirty memory ratio is calculated and |
| 1055 | maintained for and the io controller defines the io domain which |
| 1056 | writes out dirty pages for the memory domain. Both system-wide and |
| 1057 | per-cgroup dirty memory states are examined and the more restrictive |
| 1058 | of the two is enforced. |
| 1059 | |
| 1060 | cgroup writeback requires explicit support from the underlying |
| 1061 | filesystem. Currently, cgroup writeback is implemented on ext2, ext4 |
| 1062 | and btrfs. On other filesystems, all writeback IOs are attributed to |
| 1063 | the root cgroup. |
| 1064 | |
| 1065 | There are inherent differences in memory and writeback management |
| 1066 | which affects how cgroup ownership is tracked. Memory is tracked per |
| 1067 | page while writeback per inode. For the purpose of writeback, an |
| 1068 | inode is assigned to a cgroup and all IO requests to write dirty pages |
| 1069 | from the inode are attributed to that cgroup. |
| 1070 | |
| 1071 | As cgroup ownership for memory is tracked per page, there can be pages |
| 1072 | which are associated with different cgroups than the one the inode is |
| 1073 | associated with. These are called foreign pages. The writeback |
| 1074 | constantly keeps track of foreign pages and, if a particular foreign |
| 1075 | cgroup becomes the majority over a certain period of time, switches |
| 1076 | the ownership of the inode to that cgroup. |
| 1077 | |
| 1078 | While this model is enough for most use cases where a given inode is |
| 1079 | mostly dirtied by a single cgroup even when the main writing cgroup |
| 1080 | changes over time, use cases where multiple cgroups write to a single |
| 1081 | inode simultaneously are not supported well. In such circumstances, a |
| 1082 | significant portion of IOs are likely to be attributed incorrectly. |
| 1083 | As memory controller assigns page ownership on the first use and |
| 1084 | doesn't update it until the page is released, even if writeback |
| 1085 | strictly follows page ownership, multiple cgroups dirtying overlapping |
| 1086 | areas wouldn't work as expected. It's recommended to avoid such usage |
| 1087 | patterns. |
| 1088 | |
| 1089 | The sysctl knobs which affect writeback behavior are applied to cgroup |
| 1090 | writeback as follows. |
| 1091 | |
| 1092 | vm.dirty_background_ratio |
| 1093 | vm.dirty_ratio |
| 1094 | |
| 1095 | These ratios apply the same to cgroup writeback with the |
| 1096 | amount of available memory capped by limits imposed by the |
| 1097 | memory controller and system-wide clean memory. |
| 1098 | |
| 1099 | vm.dirty_background_bytes |
| 1100 | vm.dirty_bytes |
| 1101 | |
| 1102 | For cgroup writeback, this is calculated into ratio against |
| 1103 | total available memory and applied the same way as |
| 1104 | vm.dirty[_background]_ratio. |
| 1105 | |
| 1106 | |
| 1107 | P. Information on Kernel Programming |
| 1108 | |
| 1109 | This section contains kernel programming information in the areas |
| 1110 | where interacting with cgroup is necessary. cgroup core and |
| 1111 | controllers are not covered. |
| 1112 | |
| 1113 | |
| 1114 | P-1. Filesystem Support for Writeback |
| 1115 | |
| 1116 | A filesystem can support cgroup writeback by updating |
| 1117 | address_space_operations->writepage[s]() to annotate bio's using the |
| 1118 | following two functions. |
| 1119 | |
| 1120 | wbc_init_bio(@wbc, @bio) |
| 1121 | |
| 1122 | Should be called for each bio carrying writeback data and |
| 1123 | associates the bio with the inode's owner cgroup. Can be |
| 1124 | called anytime between bio allocation and submission. |
| 1125 | |
| 1126 | wbc_account_io(@wbc, @page, @bytes) |
| 1127 | |
| 1128 | Should be called for each data segment being written out. |
| 1129 | While this function doesn't care exactly when it's called |
| 1130 | during the writeback session, it's the easiest and most |
| 1131 | natural to call it as data segments are added to a bio. |
| 1132 | |
| 1133 | With writeback bio's annotated, cgroup support can be enabled per |
| 1134 | super_block by setting SB_I_CGROUPWB in ->s_iflags. This allows for |
| 1135 | selective disabling of cgroup writeback support which is helpful when |
| 1136 | certain filesystem features, e.g. journaled data mode, are |
| 1137 | incompatible. |
| 1138 | |
| 1139 | wbc_init_bio() binds the specified bio to its cgroup. Depending on |
| 1140 | the configuration, the bio may be executed at a lower priority and if |
| 1141 | the writeback session is holding shared resources, e.g. a journal |
| 1142 | entry, may lead to priority inversion. There is no one easy solution |
| 1143 | for the problem. Filesystems can try to work around specific problem |
| 1144 | cases by skipping wbc_init_bio() or using bio_associate_blkcg() |
| 1145 | directly. |
| 1146 | |
| 1147 | |
| 1148 | D. Deprecated v1 Core Features |
| 1149 | |
| 1150 | - Multiple hierarchies including named ones are not supported. |
| 1151 | |
| 1152 | - All mount options and remounting are not supported. |
| 1153 | |
| 1154 | - The "tasks" file is removed and "cgroup.procs" is not sorted. |
| 1155 | |
| 1156 | - "cgroup.clone_children" is removed. |
| 1157 | |
| 1158 | - /proc/cgroups is meaningless for v2. Use "cgroup.controllers" file |
| 1159 | at the root instead. |
| 1160 | |
| 1161 | |
| 1162 | R. Issues with v1 and Rationales for v2 |
| 1163 | |
| 1164 | R-1. Multiple Hierarchies |
| 1165 | |
| 1166 | cgroup v1 allowed an arbitrary number of hierarchies and each |
| 1167 | hierarchy could host any number of controllers. While this seemed to |
| 1168 | provide a high level of flexibility, it wasn't useful in practice. |
| 1169 | |
| 1170 | For example, as there is only one instance of each controller, utility |
| 1171 | type controllers such as freezer which can be useful in all |
| 1172 | hierarchies could only be used in one. The issue is exacerbated by |
| 1173 | the fact that controllers couldn't be moved to another hierarchy once |
| 1174 | hierarchies were populated. Another issue was that all controllers |
| 1175 | bound to a hierarchy were forced to have exactly the same view of the |
| 1176 | hierarchy. It wasn't possible to vary the granularity depending on |
| 1177 | the specific controller. |
| 1178 | |
| 1179 | In practice, these issues heavily limited which controllers could be |
| 1180 | put on the same hierarchy and most configurations resorted to putting |
| 1181 | each controller on its own hierarchy. Only closely related ones, such |
| 1182 | as the cpu and cpuacct controllers, made sense to be put on the same |
| 1183 | hierarchy. This often meant that userland ended up managing multiple |
| 1184 | similar hierarchies repeating the same steps on each hierarchy |
| 1185 | whenever a hierarchy management operation was necessary. |
| 1186 | |
| 1187 | Furthermore, support for multiple hierarchies came at a steep cost. |
| 1188 | It greatly complicated cgroup core implementation but more importantly |
| 1189 | the support for multiple hierarchies restricted how cgroup could be |
| 1190 | used in general and what controllers was able to do. |
| 1191 | |
| 1192 | There was no limit on how many hierarchies there might be, which meant |
| 1193 | that a thread's cgroup membership couldn't be described in finite |
| 1194 | length. The key might contain any number of entries and was unlimited |
| 1195 | in length, which made it highly awkward to manipulate and led to |
| 1196 | addition of controllers which existed only to identify membership, |
| 1197 | which in turn exacerbated the original problem of proliferating number |
| 1198 | of hierarchies. |
| 1199 | |
| 1200 | Also, as a controller couldn't have any expectation regarding the |
| 1201 | topologies of hierarchies other controllers might be on, each |
| 1202 | controller had to assume that all other controllers were attached to |
| 1203 | completely orthogonal hierarchies. This made it impossible, or at |
| 1204 | least very cumbersome, for controllers to cooperate with each other. |
| 1205 | |
| 1206 | In most use cases, putting controllers on hierarchies which are |
| 1207 | completely orthogonal to each other isn't necessary. What usually is |
| 1208 | called for is the ability to have differing levels of granularity |
| 1209 | depending on the specific controller. In other words, hierarchy may |
| 1210 | be collapsed from leaf towards root when viewed from specific |
| 1211 | controllers. For example, a given configuration might not care about |
| 1212 | how memory is distributed beyond a certain level while still wanting |
| 1213 | to control how CPU cycles are distributed. |
| 1214 | |
| 1215 | |
| 1216 | R-2. Thread Granularity |
| 1217 | |
| 1218 | cgroup v1 allowed threads of a process to belong to different cgroups. |
| 1219 | This didn't make sense for some controllers and those controllers |
| 1220 | ended up implementing different ways to ignore such situations but |
| 1221 | much more importantly it blurred the line between API exposed to |
| 1222 | individual applications and system management interface. |
| 1223 | |
| 1224 | Generally, in-process knowledge is available only to the process |
| 1225 | itself; thus, unlike service-level organization of processes, |
| 1226 | categorizing threads of a process requires active participation from |
| 1227 | the application which owns the target process. |
| 1228 | |
| 1229 | cgroup v1 had an ambiguously defined delegation model which got abused |
| 1230 | in combination with thread granularity. cgroups were delegated to |
| 1231 | individual applications so that they can create and manage their own |
| 1232 | sub-hierarchies and control resource distributions along them. This |
| 1233 | effectively raised cgroup to the status of a syscall-like API exposed |
| 1234 | to lay programs. |
| 1235 | |
| 1236 | First of all, cgroup has a fundamentally inadequate interface to be |
| 1237 | exposed this way. For a process to access its own knobs, it has to |
| 1238 | extract the path on the target hierarchy from /proc/self/cgroup, |
| 1239 | construct the path by appending the name of the knob to the path, open |
| 1240 | and then read and/or write to it. This is not only extremely clunky |
| 1241 | and unusual but also inherently racy. There is no conventional way to |
| 1242 | define transaction across the required steps and nothing can guarantee |
| 1243 | that the process would actually be operating on its own sub-hierarchy. |
| 1244 | |
| 1245 | cgroup controllers implemented a number of knobs which would never be |
| 1246 | accepted as public APIs because they were just adding control knobs to |
| 1247 | system-management pseudo filesystem. cgroup ended up with interface |
| 1248 | knobs which were not properly abstracted or refined and directly |
| 1249 | revealed kernel internal details. These knobs got exposed to |
| 1250 | individual applications through the ill-defined delegation mechanism |
| 1251 | effectively abusing cgroup as a shortcut to implementing public APIs |
| 1252 | without going through the required scrutiny. |
| 1253 | |
| 1254 | This was painful for both userland and kernel. Userland ended up with |
| 1255 | misbehaving and poorly abstracted interfaces and kernel exposing and |
| 1256 | locked into constructs inadvertently. |
| 1257 | |
| 1258 | |
| 1259 | R-3. Competition Between Inner Nodes and Threads |
| 1260 | |
| 1261 | cgroup v1 allowed threads to be in any cgroups which created an |
| 1262 | interesting problem where threads belonging to a parent cgroup and its |
| 1263 | children cgroups competed for resources. This was nasty as two |
| 1264 | different types of entities competed and there was no obvious way to |
| 1265 | settle it. Different controllers did different things. |
| 1266 | |
| 1267 | The cpu controller considered threads and cgroups as equivalents and |
| 1268 | mapped nice levels to cgroup weights. This worked for some cases but |
| 1269 | fell flat when children wanted to be allocated specific ratios of CPU |
| 1270 | cycles and the number of internal threads fluctuated - the ratios |
| 1271 | constantly changed as the number of competing entities fluctuated. |
| 1272 | There also were other issues. The mapping from nice level to weight |
| 1273 | wasn't obvious or universal, and there were various other knobs which |
| 1274 | simply weren't available for threads. |
| 1275 | |
| 1276 | The io controller implicitly created a hidden leaf node for each |
| 1277 | cgroup to host the threads. The hidden leaf had its own copies of all |
| 1278 | the knobs with "leaf_" prefixed. While this allowed equivalent |
| 1279 | control over internal threads, it was with serious drawbacks. It |
| 1280 | always added an extra layer of nesting which wouldn't be necessary |
| 1281 | otherwise, made the interface messy and significantly complicated the |
| 1282 | implementation. |
| 1283 | |
| 1284 | The memory controller didn't have a way to control what happened |
| 1285 | between internal tasks and child cgroups and the behavior was not |
| 1286 | clearly defined. There were attempts to add ad-hoc behaviors and |
| 1287 | knobs to tailor the behavior to specific workloads which would have |
| 1288 | led to problems extremely difficult to resolve in the long term. |
| 1289 | |
| 1290 | Multiple controllers struggled with internal tasks and came up with |
| 1291 | different ways to deal with it; unfortunately, all the approaches were |
| 1292 | severely flawed and, furthermore, the widely different behaviors |
| 1293 | made cgroup as a whole highly inconsistent. |
| 1294 | |
| 1295 | This clearly is a problem which needs to be addressed from cgroup core |
| 1296 | in a uniform way. |
| 1297 | |
| 1298 | |
| 1299 | R-4. Other Interface Issues |
| 1300 | |
| 1301 | cgroup v1 grew without oversight and developed a large number of |
| 1302 | idiosyncrasies and inconsistencies. One issue on the cgroup core side |
| 1303 | was how an empty cgroup was notified - a userland helper binary was |
| 1304 | forked and executed for each event. The event delivery wasn't |
| 1305 | recursive or delegatable. The limitations of the mechanism also led |
| 1306 | to in-kernel event delivery filtering mechanism further complicating |
| 1307 | the interface. |
| 1308 | |
| 1309 | Controller interfaces were problematic too. An extreme example is |
| 1310 | controllers completely ignoring hierarchical organization and treating |
| 1311 | all cgroups as if they were all located directly under the root |
| 1312 | cgroup. Some controllers exposed a large amount of inconsistent |
| 1313 | implementation details to userland. |
| 1314 | |
| 1315 | There also was no consistency across controllers. When a new cgroup |
| 1316 | was created, some controllers defaulted to not imposing extra |
| 1317 | restrictions while others disallowed any resource usage until |
| 1318 | explicitly configured. Configuration knobs for the same type of |
| 1319 | control used widely differing naming schemes and formats. Statistics |
| 1320 | and information knobs were named arbitrarily and used different |
| 1321 | formats and units even in the same controller. |
| 1322 | |
| 1323 | cgroup v2 establishes common conventions where appropriate and updates |
| 1324 | controllers so that they expose minimal and consistent interfaces. |
| 1325 | |
| 1326 | |
| 1327 | R-5. Controller Issues and Remedies |
| 1328 | |
| 1329 | R-5-1. Memory |
| 1330 | |
| 1331 | The original lower boundary, the soft limit, is defined as a limit |
| 1332 | that is per default unset. As a result, the set of cgroups that |
| 1333 | global reclaim prefers is opt-in, rather than opt-out. The costs for |
| 1334 | optimizing these mostly negative lookups are so high that the |
| 1335 | implementation, despite its enormous size, does not even provide the |
| 1336 | basic desirable behavior. First off, the soft limit has no |
| 1337 | hierarchical meaning. All configured groups are organized in a global |
| 1338 | rbtree and treated like equal peers, regardless where they are located |
| 1339 | in the hierarchy. This makes subtree delegation impossible. Second, |
| 1340 | the soft limit reclaim pass is so aggressive that it not just |
| 1341 | introduces high allocation latencies into the system, but also impacts |
| 1342 | system performance due to overreclaim, to the point where the feature |
| 1343 | becomes self-defeating. |
| 1344 | |
| 1345 | The memory.low boundary on the other hand is a top-down allocated |
| 1346 | reserve. A cgroup enjoys reclaim protection when it and all its |
| 1347 | ancestors are below their low boundaries, which makes delegation of |
| 1348 | subtrees possible. Secondly, new cgroups have no reserve per default |
| 1349 | and in the common case most cgroups are eligible for the preferred |
| 1350 | reclaim pass. This allows the new low boundary to be efficiently |
| 1351 | implemented with just a minor addition to the generic reclaim code, |
| 1352 | without the need for out-of-band data structures and reclaim passes. |
| 1353 | Because the generic reclaim code considers all cgroups except for the |
| 1354 | ones running low in the preferred first reclaim pass, overreclaim of |
| 1355 | individual groups is eliminated as well, resulting in much better |
| 1356 | overall workload performance. |
| 1357 | |
| 1358 | The original high boundary, the hard limit, is defined as a strict |
| 1359 | limit that can not budge, even if the OOM killer has to be called. |
| 1360 | But this generally goes against the goal of making the most out of the |
| 1361 | available memory. The memory consumption of workloads varies during |
| 1362 | runtime, and that requires users to overcommit. But doing that with a |
| 1363 | strict upper limit requires either a fairly accurate prediction of the |
| 1364 | working set size or adding slack to the limit. Since working set size |
| 1365 | estimation is hard and error prone, and getting it wrong results in |
| 1366 | OOM kills, most users tend to err on the side of a looser limit and |
| 1367 | end up wasting precious resources. |
| 1368 | |
| 1369 | The memory.high boundary on the other hand can be set much more |
| 1370 | conservatively. When hit, it throttles allocations by forcing them |
| 1371 | into direct reclaim to work off the excess, but it never invokes the |
| 1372 | OOM killer. As a result, a high boundary that is chosen too |
| 1373 | aggressively will not terminate the processes, but instead it will |
| 1374 | lead to gradual performance degradation. The user can monitor this |
| 1375 | and make corrections until the minimal memory footprint that still |
| 1376 | gives acceptable performance is found. |
| 1377 | |
| 1378 | In extreme cases, with many concurrent allocations and a complete |
| 1379 | breakdown of reclaim progress within the group, the high boundary can |
| 1380 | be exceeded. But even then it's mostly better to satisfy the |
| 1381 | allocation from the slack available in other groups or the rest of the |
| 1382 | system than killing the group. Otherwise, memory.max is there to |
| 1383 | limit this type of spillover and ultimately contain buggy or even |
| 1384 | malicious applications. |
Vladimir Davydov | 3e24b19 | 2016-01-20 15:03:13 -0800 | [diff] [blame] | 1385 | |
| 1386 | The combined memory+swap accounting and limiting is replaced by real |
| 1387 | control over swap space. |
| 1388 | |
| 1389 | The main argument for a combined memory+swap facility in the original |
| 1390 | cgroup design was that global or parental pressure would always be |
| 1391 | able to swap all anonymous memory of a child group, regardless of the |
| 1392 | child's own (possibly untrusted) configuration. However, untrusted |
| 1393 | groups can sabotage swapping by other means - such as referencing its |
| 1394 | anonymous memory in a tight loop - and an admin can not assume full |
| 1395 | swappability when overcommitting untrusted jobs. |
| 1396 | |
| 1397 | For trusted jobs, on the other hand, a combined counter is not an |
| 1398 | intuitive userspace interface, and it flies in the face of the idea |
| 1399 | that cgroup controllers should account and limit specific physical |
| 1400 | resources. Swap space is a resource like all others in the system, |
| 1401 | and that's why unified hierarchy allows distributing it separately. |