chiark / gitweb /
util: detect page size runtime.
authorcee1 <fykcee1@gmail.com>
Fri, 18 Mar 2011 02:03:41 +0000 (10:03 +0800)
committerLennart Poettering <lennart@poettering.net>
Mon, 28 Mar 2011 19:42:31 +0000 (21:42 +0200)
Some architectures support multiple machine types with diffenent
page sizes, and some machine types even support multiple
page sizes themselves.

src/macro.h
src/readahead-collect.c
src/readahead-replay.c
src/util.c
src/util.h

index 996b7c2ed141b42c4f8ce8ea3ffa4881cf1108c1..e7a4d2cde1940e531b113a0231a52c517bbf006d 100644 (file)
@@ -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))
 #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]))
index ca8227135e06336e8be082a2c197c6afa8b67702..693729598e4102dd1c7a8ef3d1341b8f7d6b1b39 100644 (file)
@@ -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;
index d2de7ef2887b67620bc9cafc42838929e51bfd13..3984c36c3e4dfdb2deab50d0efbcc06049de99d3 100644 (file)
@@ -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;
                         }
index 1febd073d2eeef37f9e7ecf99179ae711a7acbdd..fada69cf1a981893f85998ac2da465865a2d0074 100644 (file)
 #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 */
index 192ebff1fae4eb87dd19e90c17ca30579ff98841..04afc731e9d29f1d48fb518ee78cefb17f0fc856 100644 (file)
@@ -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)