Skip to content

Commit

Permalink
Including psutil & setproctitle (ray-project#7031)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijrsvt authored Feb 5, 2020
1 parent 93ed86f commit 0826f95
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/python/ray/core
/python/ray/pyarrow_files/
/python/ray/pickle5_files/
/python/ray/thirdparty_files/
/python/ray/jars/
/python/build
/python/dist
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ matrix:
- sphinx-build -W -b html -d _build/doctrees source _build/html
- cd ..
# Run Python linting, ignore dict vs {} (C408), others are defaults
- flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/ --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
- flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/,python/ray/thirdparty_files --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
- ./ci/travis/format.sh --all
# Make sure that the README is formatted properly.
- cd python
Expand Down
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ if [[ "$PYTHON_VERSION" == "3.5" || "$PYTHON_VERSION" == "3.6" || "$PYTHON_VERSI
popd
fi


"$PYTHON_EXECUTABLE" -m pip install -q psutil setproctitle \
--target="$ROOT_DIR/python/ray/thirdparty_files"

export PYTHON3_BIN_PATH="$PYTHON_EXECUTABLE"
export PYTHON2_BIN_PATH="$PYTHON_EXECUTABLE"

Expand Down
114 changes: 114 additions & 0 deletions ci/travis/check_import_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import argparse
import re
import sys
import tempfile
from pathlib import Path

exit_with_error = False


def check_import(file):
check_to_lines = {
"import ray": -1,
"import psutil": -1,
"import setproctitle": -1
}

with open(file) as f:
for i, line in enumerate(f):
for check in check_to_lines.keys():
if re.match(r"\s+" + check + r"(\s|$)", line):
check_to_lines[check] = i

for import_lib in ["import psutil", "import setproctitle"]:
if check_to_lines[import_lib] != -1:
import_psutil_line = check_to_lines[import_lib]
import_ray_line = check_to_lines["import ray"]
if import_ray_line == -1 or import_ray_line > import_psutil_line:
print(
"{}:{}".format(str(file), import_psutil_line + 1),
"{} without explicit import ray before it.".format(
import_lib))
global exit_with_error
exit_with_error = True


# Run the test with pytest file_name
def test_check_import():
global exit_with_error
_, path = tempfile.mkstemp()

with open(path, "w") as f:
f.write("""
import psutil
import ray
""")
check_import(path)
assert exit_with_error
exit_with_error = False

with open(path, "w") as f:
f.write("""
import psutil
""")
check_import(path)
assert exit_with_error
exit_with_error = False

with open(path, "w") as f:
f.write("""
import random_lib
""")
check_import(path)
assert not exit_with_error
exit_with_error = False

with open(path, "w") as f:
f.write("""
import setproctitle
import ray
""")
check_import(path)
assert exit_with_error
exit_with_error = False

with open(path, "w") as f:
f.write("""
import ray
import psutil
""")
check_import(path)
assert not exit_with_error
exit_with_error = False


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("path", help="File path to check. e.g. '.' or './src'")
parser.add_argument(
"-s", "--skip", action="append", help="Skip certian directory")
args = parser.parse_args()

file_path = Path(args.path)
if file_path.is_dir():
all_py_files = file_path.rglob("*.py")
else:
all_py_files = [file_path]

if args.skip is not None:
filtered_py_files = []
for py_file in all_py_files:
should_skip = False
for skip_dir in args.skip:
if str(py_file).startswith(skip_dir):
should_skip = True
if not should_skip:
filtered_py_files.append(py_file)
all_py_files = filtered_py_files

for py_file in all_py_files:
check_import(py_file)

if exit_with_error:
print("check import ordering failed")
sys.exit(1)
10 changes: 8 additions & 2 deletions ci/travis/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ YAPF_EXCLUDES=(
'--exclude' 'python/build/*'
'--exclude' 'python/ray/pyarrow_files/*'
'--exclude' 'python/ray/core/src/ray/gcs/*'
'--exclude' 'python/ray/thirdparty_files/*'
)

# Format specified files
Expand All @@ -104,14 +105,14 @@ format_changed() {
yapf --in-place "${YAPF_EXCLUDES[@]}" "${YAPF_FLAGS[@]}"
if which flake8 >/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/ --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/,python/ray/thirdparty_files/ --ignore=C408,E121,E123,E126,E226,E24,E704,W503,W504,W605
fi
fi

if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' &>/dev/null; then
if which flake8 >/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' | xargs -P 5 \
flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/ --ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605
flake8 --inline-quotes '"' --no-avoid-escape --exclude=python/ray/core/generated/,streaming/python/generated,doc/source/conf.py,python/ray/cloudpickle/,python/ray/thirdparty_files/ --ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605
fi
fi

Expand Down Expand Up @@ -141,6 +142,11 @@ else
format_changed
fi

# Ensure import ordering
# Make sure that for every import psutil; import setpproctitle
# There's a import ray above it.
python ci/travis/check_import_order.py . -s ci -s python/ray/pyarrow_files

if ! git diff --quiet &>/dev/null; then
echo 'Reformatted changed files. Please review and stage the changes.'
echo 'Files updated:'
Expand Down
3 changes: 3 additions & 0 deletions ci/travis/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ if [[ "$PYTHON" == "3.5" ]] || [[ "$MAC_WHEELS" == "1" ]]; then
source $HOME/.nvm/nvm.sh
nvm install node
fi

pip install -q psutil setproctitle \
--target="$ROOT_DIR/../../python/ray/thirdparty_files"
5 changes: 5 additions & 0 deletions python/ray/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
os.path.abspath(os.path.dirname(__file__)), "pickle5_files")
sys.path.insert(0, pickle5_path)

# Importing psutil & setproctitle. Must be before ray._raylet is initialized.
thirdparty_files = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "thirdparty_files")
sys.path.insert(0, thirdparty_files)

# Expose ray ABI symbols which may be dependent by other shared
# libraries such as _streaming.so. See BUILD.bazel:_raylet
so_path = os.path.join(dirname(__file__), "_raylet.so")
Expand Down
6 changes: 1 addition & 5 deletions python/ray/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import colorama

try:
import setproctitle
except ImportError:
setproctitle = None

import ray
import setproctitle


class RayError(Exception):
Expand Down
10 changes: 3 additions & 7 deletions python/ray/memory_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import sys
import time

try:
import psutil
except ImportError:
psutil = None
# Import ray before psutil will make sure we use psutil's bundled version
import ray # noqa F401
import psutil # noqa E402

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -105,9 +104,6 @@ def set_heap_limit(self, worker_name, limit_bytes):
self.worker_name = worker_name

def raise_if_low_memory(self):
if psutil is None:
return # nothing we can do

if time.time() - self.last_checked > self.check_interval:
if "RAY_DEBUG_DISABLE_MEMORY_MONITOR" in os.environ:
return # escape hatch, not intended for user use
Expand Down
9 changes: 2 additions & 7 deletions python/ray/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
import subprocess
from concurrent import futures

try:
import psutil
except ImportError:
print("The reporter requires psutil to run.")
import sys
sys.exit(1)

import ray
import psutil
import ray.ray_constants as ray_constants
import ray.services
import ray.utils
Expand Down
15 changes: 1 addition & 14 deletions python/ray/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# Ray modules
import ray
import ray.ray_constants as ray_constants
import psutil

# True if processes are run in the valgrind profiler.
RUN_RAYLET_PROFILER = False
Expand Down Expand Up @@ -91,11 +92,6 @@ def include_java_from_redis(redis_client):


def find_redis_address_or_die():
try:
import psutil
except ImportError:
raise ImportError(
"Please install `psutil` to automatically detect the Ray cluster.")
pids = psutil.pids()
redis_addresses = set()
for pid in pids:
Expand Down Expand Up @@ -998,13 +994,6 @@ def start_reporter(redis_address,
if redis_password:
command += ["--redis-password", redis_password]

try:
import psutil # noqa: F401
except ImportError:
logger.warning("Failed to start the reporter. The reporter requires "
"'pip install psutil'.")
return None

process_info = start_ray_process(
command,
ray_constants.PROCESS_TYPE_REPORTER,
Expand Down Expand Up @@ -1066,8 +1055,6 @@ def start_dashboard(require_webui,
webui_dependencies_present = True
try:
import aiohttp # noqa: F401
import psutil # noqa: F401
import setproctitle # noqa: F401
import grpc # noqa: F401
except ImportError:
webui_dependencies_present = False
Expand Down
2 changes: 1 addition & 1 deletion python/ray/tests/test_advanced_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import glob
import logging
import os
import setproctitle
import shutil
import json
import sys
Expand All @@ -20,6 +19,7 @@
import ray.ray_constants as ray_constants
import ray.cluster_utils
import ray.test_utils
import setproctitle

from ray.test_utils import RayTestTimeoutException

Expand Down
1 change: 1 addition & 0 deletions python/ray/tune/progress_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def report(self, trials, *sys_info):

def memory_debug_str():
try:
import ray # noqa F401
import psutil
total_gb = psutil.virtual_memory().total / (1024**3)
used_gb = total_gb - psutil.virtual_memory().available / (1024**3)
Expand Down
6 changes: 1 addition & 5 deletions python/ray/tune/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@

import numpy as np
import ray
import psutil

logger = logging.getLogger(__name__)

try:
import psutil
except ImportError:
psutil = None

try:
import GPUtil
except ImportError:
Expand Down
26 changes: 4 additions & 22 deletions python/ray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import time
import uuid

import ray
import ray.gcs_utils
import ray.ray_constants as ray_constants
import psutil

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -438,12 +440,7 @@ def get_system_memory():
docker_limit = int(f.read())

# Use psutil if it is available.
psutil_memory_in_bytes = None
try:
import psutil
psutil_memory_in_bytes = psutil.virtual_memory().total
except ImportError:
pass
psutil_memory_in_bytes = psutil.virtual_memory().total

if psutil_memory_in_bytes is not None:
memory_in_bytes = psutil_memory_in_bytes
Expand All @@ -468,22 +465,7 @@ def estimate_available_memory():
The total amount of available memory in bytes. It may be an
overestimate if psutil is not installed.
"""

# Use psutil if it is available.
try:
import psutil
return psutil.virtual_memory().available
except ImportError:
pass

# Handle Linux.
if sys.platform == "linux" or sys.platform == "linux2":
bytes_in_kilobyte = 1024
return (
vmstat("total memory") - vmstat("used memory")) * bytes_in_kilobyte

# Give up
return get_system_memory()
return psutil.virtual_memory().available


def get_shared_memory_bytes():
Expand Down
Loading

0 comments on commit 0826f95

Please sign in to comment.