Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct/cg/fs2.GetStats() improvements #2873

Merged
merged 7 commits into from
Apr 19, 2021

Commits on Mar 31, 2021

  1. libct/cg/fs2/memory: fix swap reporting

    cgroup v1 reports combined mem+swap in stats.MemoryStats.SwapUsage.
    In cgroup v2, swap is separate.
    
    For the sake of compatibility, make v2 report mem+swap as well.
    This also includes Limit.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    b99ca25 View commit details
    Browse the repository at this point in the history
  2. libct/cg/fs[2]/getMemoryData[V2]: optimize

    Existing code ignores ENOENT error in case we're reading data from
    controls that might not be enabled. While this is correct, the code
    can be improved:
    
    1. Check name != "" instead of moduleName != "memory", as these checks
       are equivalent but the new one is faster.
    
    2. It does not make sense to ignore subsequent errors -- if the control
       is not available, we won't hit this codepath.
    
    3. Add a comment explaining why we ignore the error.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    a9c47fe View commit details
    Browse the repository at this point in the history
  3. libct/cg/fs2/memory.Stat: add usage for root cgroup

    There is no memory.{current,max} for the root node in cgroup v2, and
    thus stats for "/sys/fs/cgroup" return an error.
    
    The same thing works with cgroup v1 (as there are
    memory.{usage,limit}_in_bytes files in the root node).
    
    Emulate stats for /sys/fs/cgroup by getting numbers from
    /proc/self/meminfo.
    
    NOTE that both memory.current (in cgroup v2) and memory.usage_in_bytes
    (in cgroup v1) include page cache etc into the number, so we do the same
    when calculating memory usage (as opposed to number reported by "free",
    which excludes page cache and buffers).
    
    [v2: check for cgroup files first, as future kernels might add it]
    [v3: don't subtract cache from mem_used, simplifying the logic]
    
    [Initially, I wanted to avoid parsing yet another /proc file and
    instead mock some numbers using data from memory.stat but was
    unable to come up with formulae that make sense.]
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    9455395 View commit details
    Browse the repository at this point in the history
  4. libct/cg/fs2.Stat: always call statCpu

    Giuseppe found out that cpu.stat for a cgroup is available even if
    the cpu controller is not enabled for it. So, let's call statCpu
    regradress, and ignore ENOENT.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    6121f8b View commit details
    Browse the repository at this point in the history
  5. libct/cg/fs2/getPidsWithoutController: optimize

    It is inefficient to create an associative map for the whole purpose of
    counting the number of elements in it, especially if the elements are
    all unique. It uses more CPU than necessary and creates some work for
    the garbage collector.
    
    The file we read contains PIDs and newlines, and the easiest/fastest way
    to get the number of PIDs is just to count the newlines.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    10f9a98 View commit details
    Browse the repository at this point in the history
  6. libct/cg/fs2.statPids: fall back directly

    When getting pids stats, instead of checking whether the pids controller
    is available, let's use a fall back function in case pids.current does
    not exist. This simplifies the logic in fs2.GetStats.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    85416b8 View commit details
    Browse the repository at this point in the history
  7. libct/cg/fs2.Stat: don't look for available controllers

    Some controllers might still have stats available even if they are
    disabled (this is definitely so for cpu.stat -- see earlier commit).
    
    Some stat methods might implement sensible fallbacks (see previous
    commit for statPids.
    
    In the view of all that, it makes sense to not check if a particular
    controller is available, but rather ignore ENOENT from it.
    
    Signed-off-by: Kir Kolyshkin <[email protected]>
    kolyshkin committed Mar 31, 2021
    Configuration menu
    Copy the full SHA
    0f8d2b6 View commit details
    Browse the repository at this point in the history