Basic utility to check the availability and permissions of cgroups.
    This will log some warnings for the user if necessary.
    On some systems, daemons such as cgrulesengd might interfere with the cgroups
    of a process soon after it was started. Thus this function starts a process,
    waits a configurable amount of time, and check whether the cgroups have been changed.
    @param wait: a non-negative int that is interpreted as seconds to wait during the check
    @raise SystemExit: if cgroups are not usable

def check_cgroup_availability(wait=1):
    """
    Basic utility to check the availability and permissions of cgroups.
    This will log some warnings for the user if necessary.
    On some systems, daemons such as cgrulesengd might interfere with the cgroups
    of a process soon after it was started. Thus this function starts a process,
    waits a configurable amount of time, and check whether the cgroups have been changed.
    @param wait: a non-negative int that is interpreted as seconds to wait during the check
    @raise SystemExit: if cgroups are not usable
    """
    logging.basicConfig(format="%(levelname)s: %(message)s")
    runexecutor = RunExecutor()
    my_cgroups = runexecutor.cgroups

    if not (CPUACCT in my_cgroups and
            CPUSET in my_cgroups and
            # FREEZER in my_cgroups and # For now, we do not require freezer
            MEMORY in my_cgroups):
        sys.exit(1)

    with tempfile.NamedTemporaryFile(mode='rt') as tmp:
        runexecutor.execute_run(['sh', '-c', 'sleep {0}; cat /proc/self/cgroup'.format(wait)], tmp.name,
                                memlimit=1024*1024, # set memlimit to force check for swapaccount
                                # set cores and memory_nodes to force usage of CPUSET
                                cores=util.parse_int_list(my_cgroups.get_value(CPUSET, 'cpus')),
                                memory_nodes=my_cgroups.read_allowed_memory_banks())
        lines = []
        for line in tmp:
            line = line.strip()
            if line and not line == "sh -c 'sleep {0}; cat /proc/self/cgroup'".format(wait) \
                    and not all(c == '-' for c in line):
                lines.append(line)
    task_cgroups = find_my_cgroups(lines)

    fail = False
    for subsystem in CPUACCT, CPUSET, MEMORY, FREEZER:
        if subsystem in my_cgroups:
            if not task_cgroups[subsystem].startswith(os.path.join(my_cgroups[subsystem], 'benchmark_')):
                logging.warning('Task was in cgroup %s for subsystem %s, '
                                'which is not the expected sub-cgroup of %s. '
                                'Maybe some other program is interfering with cgroup management?',
                                task_cgroups[subsystem], subsystem, my_cgroups[subsystem])
                fail = True
    if fail:
        sys.exit(1)