aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorapaz <aarpazdera@gmail.com>2023-04-21 13:48:06 -0500
committerGitHub <noreply@github.com>2023-04-21 21:48:06 +0300
commit25d7abbd1f73582b7e0fdc422a936e8541c0780b (patch)
tree22447d741c80fd6334b0742e4efb72b6dcadd0fc
parent018f2279f5fe3ef743bd8254b23ea8f0efae7e73 (diff)
llama : fixed rlimit error message (#888)
-rwxr-xr-xllama_util.h17
1 files changed, 15 insertions, 2 deletions
diff --git a/llama_util.h b/llama_util.h
index eba1465..acb207e 100755
--- a/llama_util.h
+++ b/llama_util.h
@@ -21,6 +21,9 @@
#if defined(_POSIX_MAPPED_FILES)
#include <sys/mman.h>
#endif
+ #if defined(_POSIX_MEMLOCK_RANGE)
+ #include <sys/resource.h>
+ #endif
#endif
#endif
@@ -303,8 +306,18 @@ struct llama_mlock {
if (!mlock(addr, size)) {
return true;
} else {
- fprintf(stderr, "warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n" MLOCK_SUGGESTION,
- size, this->size, std::strerror(errno));
+ char* errmsg = std::strerror(errno);
+ bool suggest = (errno == ENOMEM);
+
+ // Check if the resource limit is fine after all
+ struct rlimit lock_limit;
+ if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit))
+ suggest = false;
+ if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size))
+ suggest = false;
+
+ fprintf(stderr, "warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
+ size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
return false;
}
}