From f8a9b715c2307aaba6e56bea976af177a860c2a3 Mon Sep 17 00:00:00 2001
From: cee1 <fykcee1@gmail.com>
Date: Fri, 18 Mar 2011 10:03:41 +0800
Subject: [PATCH] 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              |   16 ++++++++++++++++
 src/util.h              |    3 +++
 5 files changed, 25 insertions(+), 10 deletions(-)

diff --git a/src/macro.h b/src/macro.h
index 996b7c2..e7a4d2c 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 ca82271..6937295 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 d2de7ef..3984c36 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 c9c8892..56e0e1f 100644
--- a/src/util.c
+++ b/src/util.c
@@ -61,6 +61,22 @@
 #include "exit-status.h"
 #include "hashmap.h"
 
+size_t page_size(void) {
+        static __thread size_t pgsz = 0;
+        long r;
+
+        if (pgsz) return pgsz;
+
+        r = sysconf(_SC_PAGESIZE);
+        if (r <= 0) {
+                log_warning("sysconf(_SC_PAGESIZE) failed, assume page size is 4096: %m");
+                pgsz = 4096;
+        } else
+                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 192ebff..04afc73 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)
 
-- 
1.7.4.1

