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.
This commit is contained in:
Johann Schleier-Smith
2017-01-03 12:33:29 -08:00
committed by Robert Nishihara
parent 431bba3c8a
commit b1e76e582e
2 changed files with 35 additions and 1 deletions
+22
View File
@@ -13,8 +13,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/un.h>
#include <getopt.h>
@@ -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);
}