Search code examples
linuxkernelcgroups

What does hierarchy support do in cgroup?


cgroup has 2 options for memory control:

  • memory.use_hierarchy = 1 : use hierarchy
  • memory.use_hierarchy = 0 : use flat hierarchy, according to this answer

What is the difference between the two? I think, hierarchy is hierarchy, what does "flat" mean here?

Edit: I read the cgroup documentation here explained the hierarchical support:

In the diagram above, with hierarchical accounting enabled, all memory usage of e, is accounted to its ancestors up until the root (i.e, c and root), that has memory.use_hierarchy enabled. If one of the ancestors goes over its limit, the reclaim algorithm reclaims from the tasks in the ancestor and the children of the ancestor.

  • What does it mean by accounted to its ancestors up until the root?
  • How can ancestors can go over its limit, since its children capacities are allocated that sum to the capacity of the ancestor?
  • Why are tasks reclaimed in both ancestor and children? I thought that tasks are only allocated in the children and those tasks automatically belong to ancestor. i.e. "WWW Browsing" has 20% capacity; its children are "Professors" and "Students" that have 15% and 5% respectively. The maximum can never exceed 20%. Why is there a case in the document stated that:

If one of the ancestors goes over its limit, the reclaim algorithm reclaims from the tasks in the ancestor and the children of the ancestor.


Solution

  • Hierarchical here means that there are memory cgroups nested within cgroups. eg. You can create a parent cgroup P and it can have a child cgroup C. There can be processes in P (say p1 and p2) and in C (c1, c2). With use_hierarchy=1, memory stats at P would show the total of C's usage and usage by all processes in P. It will also account for any tmpfs in P.

    If cgroup P goes over limit, it can reclaim memory from p1, p2, and child cgroup C. If C goes over limit, it will reclaim from c1 and c2.

    I think the point that is unclear in documentation is that there can be tasks that are directly under P and not under one of its children cgroups.