diff options
author | Matias Elo <matias.elo@nokia.com> | 2019-02-14 17:19:12 +0200 |
---|---|---|
committer | Petri Savolainen <petri.savolainen@nokia.com> | 2019-02-19 15:59:49 +0200 |
commit | 5ccc27cf77c936460b265c973a531a04b7a74014 (patch) | |
tree | efea8fd67ea03e7cea4864def68fbdb0b294577f | |
parent | 63517b31c273570ead349cf92834a71d13d8470e (diff) |
linux-gen: shm: replace ftruncate() with fallocate()
fallocate() reserves required memory at call time, so out of memory
failures are easier to detect.
Signed-off-by: Matias Elo <matias.elo@nokia.com>
Reviewed-by: Petri Savolainen <petri.savolainen@nokia.com>
-rw-r--r-- | platform/linux-generic/odp_ishm.c | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/platform/linux-generic/odp_ishm.c b/platform/linux-generic/odp_ishm.c index 875f9f9d5..94a14549a 100644 --- a/platform/linux-generic/odp_ishm.c +++ b/platform/linux-generic/odp_ishm.c @@ -256,6 +256,7 @@ static void procsync(void); static int hp_create_file(uint64_t len, const char *filename) { int fd; + int ret; void *addr; if (len <= 0) { @@ -273,10 +274,19 @@ static int hp_create_file(uint64_t len, const char *filename) /* remove file from file system */ unlink(filename); - if (ftruncate(fd, len) == -1) { - ODP_ERR("Could not truncate file: %s\n", strerror(errno)); - close(fd); - return -1; + ret = fallocate(fd, 0, 0, len); + if (ret == -1) { + if (errno == ENOTSUP) { + ODP_DBG("fallocate() not supported\n"); + ret = ftruncate(fd, len); + } + + if (ret == -1) { + ODP_ERR("memory allocation failed: fd=%d, err=%s.\n", + fd, strerror(errno)); + close(fd); + return -1; + } } /* commit huge page */ @@ -637,6 +647,7 @@ static int create_file(int block_index, huge_flag_t huge, uint64_t len, * /mnt/huge */ int oflag = O_RDWR | O_CREAT | O_TRUNC; /* flags for open */ char dir[ISHM_FILENAME_MAXLEN]; + int ret; /* No ishm_block_t for the master single VA memory file */ if (single_va) { @@ -676,12 +687,20 @@ static int create_file(int block_index, huge_flag_t huge, uint64_t len, return -1; } - if (ftruncate(fd, len) == -1) { - ODP_ERR("ftruncate failed: fd=%d, err=%s.\n", - fd, strerror(errno)); - close(fd); - unlink(filename); - return -1; + ret = fallocate(fd, 0, 0, len); + if (ret == -1) { + if (errno == ENOTSUP) { + ODP_DBG("fallocate() not supported\n"); + ret = ftruncate(fd, len); + } + + if (ret == -1) { + ODP_ERR("memory allocation failed: fd=%d, err=%s.\n", + fd, strerror(errno)); + close(fd); + unlink(filename); + return -1; + } } /* No export file is created since this is only for internal use.*/ |