Skip to content

Commit

Permalink
Use mincore(2) to queue an madvise(2) call if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira committed Aug 25, 2024
1 parent f9ebc66 commit 557a6e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
check_function_exists(get_current_dir_name LWAN_HAVE_GET_CURRENT_DIR_NAME)
check_symbol_exists(reallocarray stdlib.h LWAN_HAVE_REALLOCARRAY)
check_symbol_exists(eventfd sys/eventfd.h LWAN_HAVE_EVENTFD)
check_symbol_exists(mincore sys/mman.h LWAN_HAVE_MINCORE)
check_function_exists(mempcpy LWAN_HAVE_MEMPCPY)
check_function_exists(memrchr LWAN_HAVE_MEMRCHR)
check_function_exists(pipe2 LWAN_HAVE_PIPE2)
Expand Down
1 change: 1 addition & 0 deletions src/cmake/lwan-build-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#cmakedefine LWAN_HAVE_SYSLOG
#cmakedefine LWAN_HAVE_STPCPY
#cmakedefine LWAN_HAVE_EVENTFD
#cmakedefine LWAN_HAVE_MINCORE

/* Compiler builtins for specific CPU instruction support */
#cmakedefine LWAN_HAVE_BUILTIN_CLZLL
Expand Down
31 changes: 30 additions & 1 deletion src/lib/lwan-mod-serve-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
#include <zstd.h>
#endif

#define MMAP_SIZE_THRESHOLD 16384
#define MINCORE_CALL_THRESHOLD 10
#define MINCORE_VEC_LEN(len) (((len) + PAGE_SIZE - 1) / PAGE_SIZE)

static const struct lwan_key_value deflate_compression_hdr[] = {
{"Content-Encoding", "deflate"}, {}
};
Expand Down Expand Up @@ -113,6 +117,7 @@ struct mmap_cache_data {
#if defined(LWAN_HAVE_ZSTD)
struct lwan_value zstd;
#endif
int mincore_call_threshold;
};

struct sendfile_cache_data {
Expand Down Expand Up @@ -544,6 +549,8 @@ static bool mmap_init(struct file_cache_entry *ce,
ce->mime_type =
lwan_determine_mime_type_for_file_name(full_path + priv->root_path_len);

md->mincore_call_threshold = MINCORE_CALL_THRESHOLD;

return true;
}

Expand Down Expand Up @@ -755,7 +762,7 @@ static const struct cache_funcs *get_funcs(struct serve_files_priv *priv,

/* It's not a directory: choose the fastest way to serve the file
* judging by its size. */
if (st->st_size < 16384)
if (st->st_size < MMAP_SIZE_THRESHOLD)
return &mmap_funcs;

return &sendfile_funcs;
Expand Down Expand Up @@ -1252,6 +1259,28 @@ static enum lwan_http_status mmap_serve(struct lwan_request *request,
return serve_value_ok(request, fce->mime_type, to_serve,
compression_hdr);

#ifdef LWAN_HAVE_MINCORE
if (md->mincore_call_threshold-- == 0) {
unsigned char mincore_vec[MINCORE_VEC_LEN(MMAP_SIZE_THRESHOLD)];

if (!mincore(to_serve->value, to_serve->len, mincore_vec)) {
const size_t pgs = MINCORE_VEC_LEN(to_serve->len);

for (size_t pg = 0; pg < pgs; pg++) {
if (mincore_vec[pg] & 0x01)
continue;

/* FIXME: madvise only the page that's not in core */
lwan_madvise_queue(to_serve->value, to_serve->len);
coro_yield(request->conn->coro, CONN_CORO_WANT_WRITE);
break;
}
}

md->mincore_call_threshold = MINCORE_CALL_THRESHOLD;
}
#endif

off_t from, to;
enum lwan_http_status status =
compute_range(request, &from, &to, (off_t)to_serve->len);
Expand Down

0 comments on commit 557a6e7

Please sign in to comment.