From 37f85e66e8f396b6f758e063531b95531aef628e Mon Sep 17 00:00:00 2001 From: cee1 Date: Fri, 18 Mar 2011 10:03:41 +0800 Subject: [PATCH] util: detect page size runtime. Some architectures support multiple machine types with diffenent page sizes, and some machine types even support multiple page sizes themselves. --- src/macro.h | 11 +++-------- src/readahead-collect.c | 3 ++- src/readahead-replay.c | 2 +- src/util.c | 14 ++++++++++++++ src/util.h | 3 +++ 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/macro.h b/src/macro.h index 996b7c2e..e7a4d2cd 100644 --- a/src/macro.h +++ b/src/macro.h @@ -27,8 +27,6 @@ #include #include -#define PAGE_SIZE 4096 - #define _printf_attr_(a,b) __attribute__ ((format (printf, a, b))) #define _sentinel_ __attribute__ ((sentinel)) #define _noreturn_ __attribute__((noreturn)) @@ -51,12 +49,9 @@ #define STRINGIFY(x) XSTRINGIFY(x) /* Rounds up */ -static inline size_t ALIGN(size_t l) { - return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1)); -} - -static inline size_t PAGE_ALIGN(size_t l) { - return ((l + PAGE_SIZE - 1) & ~(PAGE_SIZE -1)); +#define ALIGN(l) ALIGN_TO((l), sizeof(void*)) +static inline size_t ALIGN_TO(size_t l, size_t ali) { + return ((l + ali - 1) & ~(ali - 1)); } #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) diff --git a/src/readahead-collect.c b/src/readahead-collect.c index ca822713..69372959 100644 --- a/src/readahead-collect.c +++ b/src/readahead-collect.c @@ -119,9 +119,10 @@ static int pack_file(FILE *pack, const char *fn, bool on_btrfs) { goto finish; } - pages = l / PAGE_SIZE; + pages = l / page_size(); vec = alloca(pages); + memset(vec, 0, pages); if (mincore(start, l, vec) < 0) { log_warning("mincore(%s) failed: %m", fn); r = -errno; diff --git a/src/readahead-replay.c b/src/readahead-replay.c index d2de7ef2..3984c36c 100644 --- a/src/readahead-replay.c +++ b/src/readahead-replay.c @@ -94,7 +94,7 @@ static int unpack_file(FILE *pack) { any = true; if (fd >= 0) - if (posix_fadvise(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE, POSIX_FADV_WILLNEED) < 0) { + if (posix_fadvise(fd, b * page_size(), (c - b) * page_size(), POSIX_FADV_WILLNEED) < 0) { log_warning("posix_fadvise() failed: %m"); goto finish; } diff --git a/src/util.c b/src/util.c index 1febd073..fada69cf 100644 --- a/src/util.c +++ b/src/util.c @@ -61,6 +61,20 @@ #include "exit-status.h" #include "hashmap.h" +size_t page_size(void) { + static __thread size_t pgsz = 0; + long r; + + if (pgsz) + return pgsz; + + assert_se((r = sysconf(_SC_PAGESIZE)) > 0); + + pgsz = (size_t) r; + + return pgsz; +} + bool streq_ptr(const char *a, const char *b) { /* Like streq(), but tries to make sense of NULL pointers */ diff --git a/src/util.h b/src/util.h index 192ebff1..04afc731 100644 --- a/src/util.h +++ b/src/util.h @@ -83,6 +83,9 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u); usec_t timeval_load(const struct timeval *tv); struct timeval *timeval_store(struct timeval *tv, usec_t u); +size_t page_size(void); +#define PAGE_ALIGN(l) ALIGN_TO((l), page_size()) + #define streq(a,b) (strcmp((a),(b)) == 0) #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) -- 2.39.5