From b1e76e582eb29562b91e22943613e681fa4283fc Mon Sep 17 00:00:00 2001 From: Johann Schleier-Smith Date: Tue, 3 Jan 2017 15:33:29 -0500 Subject: [PATCH] Check /dev/shm on Linux (#174) * check available shared memory when starting object store * exit with error if not enough shared memory available for object store * Some comments and formatting. --- lib/python/ray/services.py | 14 +++++++++++++- src/plasma/plasma_store.c | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/python/ray/services.py b/lib/python/ray/services.py index df93f49c7..1abb24b93 100644 --- a/lib/python/ray/services.py +++ b/lib/python/ray/services.py @@ -254,8 +254,20 @@ def start_objstore(node_ip_address, redis_address, cleanup=True, redirect_output # memory. To not overflow it, we set the plasma memory limit to 0.4 times # the size of the physical memory. objstore_memory = int(system_memory * 0.4) + # Compare the requested memory size to the memory available in /dev/shm. + shm_fd = os.open("/dev/shm", os.O_RDONLY) + try: + shm_fs_stats = os.fstatvfs(shm_fd) + # The value shm_fs_stats.f_bsize is the block size and the value + # shm_fs_stats.f_bavail is the number of available blocks. + shm_avail = shm_fs_stats.f_bsize * shm_fs_stats.f_bavail + if objstore_memory > shm_avail: + print("Warning: Reducing object store memory because /dev/shm has only {} bytes available. You may be able to free up space by deleting files in /dev/shm. If you are inside a Docker container, you may need to pass an argument with the flag '--shm-size' to 'docker run'.".format(shm_avail)) + objstore_memory = int(shm_avail * 0.8) + finally: + os.close(shm_fd) else: - objstore_memory = int(system_memory * 0.75) + objstore_memory = int(system_memory * 0.8) # Start the Plasma store. plasma_store_name, p1 = plasma.start_plasma_store(plasma_store_memory=objstore_memory, use_profiler=RUN_PLASMA_STORE_PROFILER, redirect_output=redirect_output) # Start the plasma manager. diff --git a/src/plasma/plasma_store.c b/src/plasma/plasma_store.c index d8f3e90f2..d9c034e5a 100644 --- a/src/plasma/plasma_store.c +++ b/src/plasma/plasma_store.c @@ -13,8 +13,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -664,6 +666,26 @@ int main(int argc, char *argv[]) { if (system_memory == -1) { LOG_FATAL("please specify the amount of system memory with -m switch"); } +#ifdef __linux__ + /* On Linux, check that the amount of memory available in /dev/shm is large + * enough to accommodate the request. If it isn't, then fail. */ + int shm_fd = open("/dev/shm", O_RDONLY); + struct statvfs shm_vfs_stats; + fstatvfs(shm_fd, &shm_vfs_stats); + /* The value shm_vfs_stats.f_bsize is the block size, and the value + * shm_vfs_stats.f_bavail is the number of available blocks. */ + int64_t shm_mem_avail = shm_vfs_stats.f_bsize * shm_vfs_stats.f_bavail; + close(shm_fd); + if (system_memory > shm_mem_avail) { + LOG_FATAL( + "System memory request exceeds memory available in /dev/shm. The " + "request is for %" PRId64 " bytes, and the amount available is %" PRId64 + " bytes. You may be able to free up space by deleting files in " + "/dev/shm. If you are inside a Docker container, you may need to pass " + "an argument with the flag '--shm-size' to 'docker run'.", + system_memory, shm_mem_avail); + } +#endif LOG_DEBUG("starting server listening on %s", socket_name); start_server(socket_name, system_memory); }