Skip to content

Commit

Permalink
add memory limit
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuenni committed Jan 23, 2025
1 parent 04a1dcd commit 9c69452
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
5 changes: 3 additions & 2 deletions bin/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def run_interactive_testcase(
# Set limits
validator_timeout = config.DEFAULT_INTERACTION_TIMEOUT

memory_limit = get_memory_limit()
timelimit = run.problem.settings.timelimit
timeout = run.problem.settings.timeout
memory = run.problem.limits.memory

# Validator command
def get_validator_command():
Expand Down Expand Up @@ -108,6 +108,7 @@ def get_validator_command():
stderr=subprocess.PIPE if team_error is False else None,
cwd=submission_dir,
timeout=timeout,
memory=memory,
)

# Wait
Expand Down Expand Up @@ -259,7 +260,7 @@ def get_validator_command():
stderr=subprocess.PIPE if team_error is False else None,
cwd=submission_dir,
pipesize=BUFFER_SIZE,
preexec_fn=limit_setter(submission_command, timeout, memory_limit, gid),
preexec_fn=limit_setter(submission_command, timeout, memory, gid),
)
submission_pid = submission.pid

Expand Down
1 change: 1 addition & 0 deletions bin/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def _read_settings(self):
'time_multiplier': 2,
'time_safety_margin': 1.5,
'time_resolution': 1.0,
'memory': 2048,
}

yaml_path = self.path / 'problem.yaml'
Expand Down
2 changes: 1 addition & 1 deletion bin/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _get_language(self, bar: ProgressBar):
'mainclass': mainclass,
'Mainclass': mainclass[0].upper() + mainclass[1:],
# Memory limit in MB.
'memlim': (get_memory_limit() or 1024),
# 'memlim': 2048,
# Out-of-spec variables used by 'manual' and 'Viva' languages.
'build': (
self.tmpdir / 'build' if (self.tmpdir / 'build') in self.input_files else ''
Expand Down
10 changes: 9 additions & 1 deletion bin/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def run(self, in_path, out_path, crop=True, args=[], cwd=None, default_timeout=F
stdout=out_file,
stderr=None if out_file is None else True,
timeout=True if default_timeout else self.problem.settings.timeout,
memory=self.problem.limits.memory,
cwd=cwd,
)
if out_file:
Expand Down Expand Up @@ -513,6 +514,7 @@ def test(self):
stdout=None,
stderr=None,
timeout=self.problem.settings.timeout,
memory=self.problem.limits.memory,
)

assert result.err is None and result.out is None
Expand Down Expand Up @@ -617,7 +619,13 @@ def test_interactive(self):

assert self.run_command is not None
result = exec_command(
self.run_command, crop=False, stdin=r, stdout=None, stderr=None, timeout=None
self.run_command,
crop=False,
stdin=r,
stdout=None,
stderr=None,
timeout=None,
memory=self.problem.limits.memory,
)

assert result.err is None and result.out is None
Expand Down
7 changes: 1 addition & 6 deletions bin/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,6 @@ def build_parser():
type=int,
help='The number of jobs to use. Default: cpu_count()/2.',
)
global_parser.add_argument(
'--memory',
'-m',
help='The maximum amount of memory in MB a subprocess may use. Does not work for Java. Default: 2048.',
)
global_parser.add_argument(
'--api',
help='CCS API endpoint to use, e.g. https://www.domjudge.org/demoweb. Defaults to the value in contest.yaml.',
Expand Down Expand Up @@ -662,7 +657,7 @@ def build_parser():
runparser.add_argument(
'--sanitizer',
action='store_true',
help='Run submissions with additional sanitizer flags (currently only C++). Note that this sets --memory unlimited.',
help='Run submissions with additional sanitizer flags (currently only C++). Note that this removes all memory limits for submissions.',
)

timelimitparser = subparsers.add_parser(
Expand Down
27 changes: 8 additions & 19 deletions bin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,23 +876,6 @@ def tail(string, limit):
return '\n'.join(lines)


# TODO: Move this to Problem.settings and read limits.memory variable from problem.yaml.
# Return memory limit in MB.
def get_memory_limit(kwargs=None) -> Optional[int]:
memory_limit: Optional[int] = 2048 # 2GB
if config.args.sanitizer:
memory_limit = None # disabled
elif config.args.memory:
if config.args.memory != 'unlimited':
memory_limit = int(config.args.memory)
else:
memory_limit = None # disabled
if kwargs and 'memory' in kwargs:
memory_limit = kwargs['memory']
kwargs.pop('memory')
return memory_limit


class ExecStatus(Enum):
ACCEPTED = 1
REJECTED = 2
Expand Down Expand Up @@ -1057,9 +1040,15 @@ def exec_command(
if timeout is not None and math.isinf(timeout):
timeout = None

if (is_windows() or is_wsl()) and 'memory' in kwargs:
memory: Optional[int] = None
if 'memory' in kwargs:
if kwargs['memory'] is not None:
memory = kwargs['memory']
kwargs.pop('memory')

if is_windows() or is_wsl() or config.args.sanitizer:
memory = None

process: Optional[ResourcePopen] = None

def interrupt_handler(sig, frame):
Expand All @@ -1078,7 +1067,7 @@ def interrupt_handler(sig, frame):
if not is_windows() and not is_wsl() and preexec_fn:
process = ResourcePopen(
command,
preexec_fn=limit_setter(command, timeout, get_memory_limit(kwargs)),
preexec_fn=limit_setter(command, timeout, memory),
**kwargs,
)
else:
Expand Down
3 changes: 1 addition & 2 deletions doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ The flags below work for any subcommand:
- `--verbose`/`-v`: Without this, only failing steps are printed to the terminal. With `-v`, progress bars print one line for each processed item. Pass `-v` twice to see all commands that are executed.
- `--contest <directory>`: The directory of the contest to use, if not the current directory. At most one of `--contest` and `--problem` may be used. Useful in CI jobs.
- `--problem <directory>`: The directory of the problem to use, if not the current directory. At most one of `--contest` and `--problem` may be used. Useful in CI jobs.
- `--memory <MB>`/`-m <MB>`: The maximum amount of memory in MB a subprocess (submission/generator/etc.) may use. Does not work for Java. Default: 2048.
- `--no-bar`: Disable showing progress bars. This is useful when running in non-interactive contexts (such as CI jobs) or on platforms/terminals that don't handle the progress bars well.
- `--error`/`-e`: show full output of failing commands using `--error`. The default is to show a short snippet only.
- `--force-build`: Force rebuilding binaries instead of reusing cached version.
Expand Down Expand Up @@ -109,7 +108,7 @@ Use `bt run -v` to show results for all testcases.
- `--table`: Print a table of which testcases were solved by which submissions. May be used to deduplicate testcases that fail the same submissions.
- `--overview`/`-o`: Print a live overview of the received verdicts for all submissions and testcases. If combined with `--no-bar` only the final table is printed.
- `--no-testcase-sanity-checks`: when passed, all sanity checks on the testcases are skipped. You might want to set this in `.bapctools.yaml`.
- `--sanitizer`: when passed, run submissions with additional sanitizer flags (currently only C++). Note that this sets --memory unlimited.
- `--sanitizer`: when passed, run submissions with additional sanitizer flags (currently only C++). Note that this removes all memory limits for submissions.

## `test`

Expand Down

0 comments on commit 9c69452

Please sign in to comment.