Skip to content

Commit

Permalink
cpu memory limits test: Work in progress.
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Szulik <pawel.szulik@intel.com>
  • Loading branch information
Paweł Szulik committed Dec 2, 2022
1 parent 046bc5c commit c4be935
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
6 changes: 3 additions & 3 deletions gprofiler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,15 @@ def parse_cmd_args() -> configargparse.Namespace:
default=DEFAULT_MEMORY_LIMIT,
dest="memory_limit",
type=int,
help="Limit on the memory used by gProfiler. Units are bytes and the default is %DEFAULT_MEMORY_LIMIT."
help=f"Limit on the memory used by gProfiler. Units are bytes and the default is '{DEFAULT_MEMORY_LIMIT}'."
)

parser.add_argument(
"--limit-cpu",
default=DEFAULT_CPU_LIMIT,
dest="cpu_limit",
type=float,
help="Limit on the cpu used by gProfiler. Units are cores and the default is %DEFAULT_CPU_LIMIT."
help=f"Limit on the cpu used by gProfiler. Units are cores and the default is '{DEFAULT_CPU_LIMIT}'."
)

parser.add_argument(
Expand Down Expand Up @@ -790,7 +790,7 @@ def main() -> None:
# TODO(Creatone): Check the containerized scenario.
if args.cgroups_changes and get_run_mode() not in ("k8s", "container"):
logger.info(f"Trying to set resource limits, cpu='{args.cpu_limit}' "
f"cores and memory='{args.memory_limit >> 20}' MB.")
f"cores and memory='{args.memory_limit >> 20:.2f}' MB.")
try:
set_limits(args.cpu_limit, args.memory_limit)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion gprofiler/usage_loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from granulate_utils.linux.cgroups.cgroup import find_v1_hierarchies, find_v2_hierarchy


# TODO(Creatone): Move it to granulate-utils.
# TODO(Creatone): Move it to granulate-utils. Consider change.
def _obtain_cgroup_controller_path(cgroup: str, controller: str) -> str:
cgroup_v1_hierarchies = find_v1_hierarchies()
if len(cgroup_v1_hierarchies) != 1:
Expand Down
59 changes: 59 additions & 0 deletions tests/test_cgroups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# Copyright (c) Granulate. All rights reserved.
# Licensed under the AGPL3 License. See LICENSE.md in the project root for license information.
#
import os
import subprocess
from pathlib import Path
from subprocess import Popen
from typing import List

import pytest
from docker import DockerClient
from docker.models.images import Image

from tests.utils import run_privileged_container, _print_process_output


def test_cgroup_limit_container(
docker_client: DockerClient,
gprofiler_docker_image: Image,
output_directory: Path,
) -> None:
logs = run_privileged_container(docker_client, gprofiler_docker_image,
command=['-v', '--limit-cpu', '0.5', '--limit-memory', '1048576', '-o',
str(output_directory)])

limit_log = "Trying to set resource limits, cpu='0.5' cores and memory='1024.00' MB."

assert limit_log not in logs


def test_cgroup_limit_privileged_executable(
gprofiler_exe: Path,
output_directory: Path,
) -> None:
os.mkdir(output_directory)

command = (
['sudo', str(gprofiler_exe), '-v', '--limit-cpu', '0.5',
'--limit-memory', str((1 << 30)), '-o', str(output_directory), "-d", "5",
"--no-java", "--no-python", "--no-php", "--no-ruby", "--no-nodejs", "--no-dotnet"]
)

popen = Popen(command, stdout=subprocess.PIPE)
assert popen.wait() == 0
stdout, _ = popen.communicate()
logs = stdout.decode("utf-8").splitlines()
limit_log = "Trying to set resource limits, cpu='0.5' cores and memory='1024.00' MB."

present = False
for line in logs:
if limit_log in line:
present = True
assert present


# Not implemented yet.
def test_cgroup_try_limit_no_privileged_executable():
assert False

0 comments on commit c4be935

Please sign in to comment.