Skip to content

Commit 25d7abb

Browse files
authored
llama : fixed rlimit error message (#888)
1 parent 018f227 commit 25d7abb

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

llama_util.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#if defined(_POSIX_MAPPED_FILES)
2222
#include <sys/mman.h>
2323
#endif
24+
#if defined(_POSIX_MEMLOCK_RANGE)
25+
#include <sys/resource.h>
26+
#endif
2427
#endif
2528
#endif
2629

@@ -303,8 +306,18 @@ struct llama_mlock {
303306
if (!mlock(addr, size)) {
304307
return true;
305308
} else {
306-
fprintf(stderr, "warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n" MLOCK_SUGGESTION,
307-
size, this->size, std::strerror(errno));
309+
char* errmsg = std::strerror(errno);
310+
bool suggest = (errno == ENOMEM);
311+
312+
// Check if the resource limit is fine after all
313+
struct rlimit lock_limit;
314+
if (suggest && getrlimit(RLIMIT_MEMLOCK, &lock_limit))
315+
suggest = false;
316+
if (suggest && (lock_limit.rlim_max > lock_limit.rlim_cur + size))
317+
suggest = false;
318+
319+
fprintf(stderr, "warning: failed to mlock %zu-byte buffer (after previously locking %zu bytes): %s\n%s",
320+
size, this->size, errmsg, suggest ? MLOCK_SUGGESTION : "");
308321
return false;
309322
}
310323
}

0 commit comments

Comments
 (0)