Commit a77bf432 authored by Sean Solari's avatar Sean Solari
Browse files

refreshed expam_limit interface

parent 1a259417
#!/bin/bash
conda deactivate
if ! [[ "$PWD" =~ expam-public ]]
then
echo "Wrong directory! Move to expam-public!"
......@@ -19,6 +21,9 @@ source .venv/bin/activate
# Print python version in virtual environment.
python3 --version
# Remove previous distribution files.
rm dist/*
# Build package.
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade build
......
......@@ -4,7 +4,6 @@ import json.decoder
import math
import platform
import re
from tkinter import E
import matplotlib.pyplot as plt
import multiprocessing as mp
......
......@@ -2,6 +2,7 @@
import logging
import multiprocessing as mp
import sys
import psutil
import re
import resource
......@@ -9,6 +10,15 @@ import subprocess
import time
def check_free_command():
err = subprocess.run(['free', '-h'], capture_output=True, shell=True).stderr
if err:
return False
else:
return True
def limit(mem_limit):
"""
Defines a wrapper for functions that first sets a resource limit before
......@@ -77,37 +87,35 @@ def sandbox(logging_name, mem_limit, interval):
return inner
def parse(cmd, mem_limit=None, interval=1, out=None):
i = 0
def parse():
from argparse import ArgumentParser, RawTextHelpFormatter, REMAINDER
if len(cmd) > 0 and 'sandbox' in cmd[i]:
i += 1
parser = ArgumentParser(description="::: expam_limit ::: \n\n\tcommand for limiting and logging memory usage.", formatter_class=RawTextHelpFormatter)
parser.add_argument('-m', '--memory', type=int, dest='memory', help="Explicit memory limit in bytes.")
parser.add_argument('-x', '--x', type=float, dest='x', help="Memory limit as a percentage of total system memory.")
parser.add_argument('-t', '--interval', type=float, dest='interval', default=1.0, help="Time between memory logs.")
parser.add_argument('-o', '--out', type=str, dest='out', help="File to log to (default is stdout).")
parser.add_argument('command', nargs=REMAINDER, help="Command to run in limited regime.")
while i < len(cmd):
if cmd[i] in {'--memory', '-m'}:
mem_limit = int(cmd[i + 1])
i += 1
args = parser.parse_args()
elif cmd[i] in {'--interval', '-t'}:
interval = float(cmd[i + 1])
i += 1
# Establish memory limit.
byte_limit = args.memory
perc_limit = None if args.x is None else int(args.x * psutil.virtual_memory().total)
elif cmd[i] in {'--out', '-o'}:
out = cmd[i + 1]
i += 1
if byte_limit is not None and perc_limit is not None:
mem_limit = min(byte_limit, perc_limit)
elif cmd[i] in {'--x', '-x'}:
total_memory = psutil.virtual_memory().total
mem_limit = int(float(cmd[i + 1]) * total_memory)
i += 1
else:
break
i += 1
elif byte_limit is not None:
mem_limit = byte_limit
elif perc_limit is not None:
mem_limit = byte_limit
else:
mem_limit = None
cmd = cmd[i:]
return mem_limit, interval, out, cmd
return mem_limit, args.interval, args.out, args.command
def sandbox_command(data):
......@@ -141,8 +149,13 @@ def sandbox_command(data):
def main():
import sys
sandbox_command(sys.argv)
print(parse())
if not check_free_command():
print("Must run on Linux system (or have `free` command available).")
sys.exit(1)
# sandbox_command()
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment