summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcee1 <fykcee1@gmail.com>2011-03-18 10:03:41 +0800
committerLennart Poettering <lennart@poettering.net>2011-03-28 21:42:31 +0200
commit37f85e66e8f396b6f758e063531b95531aef628e (patch)
tree4c914f49c19d6712b76fc1be5f58929a2aa9d5bc /src
parentf9276855a1d270b6c3f857cdaf2c4b49920c2228 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/macro.h11
-rw-r--r--src/readahead-collect.c3
-rw-r--r--src/readahead-replay.c2
-rw-r--r--src/util.c14
-rw-r--r--src/util.h3
5 files changed, 23 insertions, 10 deletions
diff --git a/src/macro.h b/src/macro.h
index 996b7c2ed1..e7a4d2cde1 100644
--- a/src/macro.h
+++ b/src/macro.h
@@ -27,8 +27,6 @@
#include <sys/uio.h>
#include <inttypes.h>
-#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 ca8227135e..693729598e 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 d2de7ef288..3984c36c3e 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 1febd073d2..fada69cf1a 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 192ebff1fa..04afc731e9 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)