From 108e5a1e5e7d7037061bc84acf148e8558074b3e Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 12 Sep 2016 18:36:58 +0000 Subject: [PATCH] Port: Add bt_common_get_page_size MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- common/common.c | 16 +++++ include/Makefile.am | 1 + include/babeltrace/align-internal.h | 5 -- include/babeltrace/common-internal.h | 6 ++ include/babeltrace/compat/unistd-internal.h | 61 +++++++++++++++++++ .../ctf-writer/serialize-internal.h | 3 + include/babeltrace/mmap-align-internal.h | 8 ++- lib/ctf-ir/fields.c | 2 - lib/ctf-writer/serialize.c | 3 +- plugins/ctf/fs-src/fs.c | 3 +- 10 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 include/babeltrace/compat/unistd-internal.h diff --git a/common/common.c b/common/common.c index 032a1e06..b92fc89d 100644 --- a/common/common.c +++ b/common/common.c @@ -30,8 +30,10 @@ #include #include #include +#include #include #include +#include #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins" #define HOME_ENV_VAR "HOME" @@ -1125,3 +1127,17 @@ end: return norm_path; } + +BT_HIDDEN +size_t bt_common_get_page_size(void) +{ + int page_size; + + page_size = bt_sysconf(_SC_PAGESIZE); + if (page_size < 0) { + printf_error("Cannot get system page size."); + abort(); + } + + return page_size; +} diff --git a/include/Makefile.am b/include/Makefile.am index 86eade69..bf0ca69c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -86,6 +86,7 @@ noinst_HEADERS = \ babeltrace/compat/stdio-internal.h \ babeltrace/compat/stdlib-internal.h \ babeltrace/compat/string-internal.h \ + babeltrace/compat/unistd-internal.h \ babeltrace/compat/utc-internal.h \ babeltrace/compat/uuid-internal.h \ babeltrace/compiler-internal.h \ diff --git a/include/babeltrace/align-internal.h b/include/babeltrace/align-internal.h index a2566c99..5d86ad57 100644 --- a/include/babeltrace/align-internal.h +++ b/include/babeltrace/align-internal.h @@ -26,13 +26,8 @@ */ #include -#include #include -#ifndef PAGE_SIZE /* Cygwin limits.h defines its own PAGE_SIZE */ -#define PAGE_SIZE sysconf(_SC_PAGE_SIZE) -#endif - #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) #define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a)) diff --git a/include/babeltrace/common-internal.h b/include/babeltrace/common-internal.h index eb055f5e..880b2e47 100644 --- a/include/babeltrace/common-internal.h +++ b/include/babeltrace/common-internal.h @@ -199,4 +199,10 @@ bool bt_common_star_glob_match(const char *pattern, size_t pattern_len, BT_HIDDEN GString *bt_common_normalize_path(const char *path, const char *wd); +/* + * Return the system page size. + */ +BT_HIDDEN +size_t bt_common_get_page_size(void); + #endif /* BABELTRACE_COMMON_INTERNAL_H */ diff --git a/include/babeltrace/compat/unistd-internal.h b/include/babeltrace/compat/unistd-internal.h new file mode 100644 index 00000000..b06cdc86 --- /dev/null +++ b/include/babeltrace/compat/unistd-internal.h @@ -0,0 +1,61 @@ +#ifndef _BABELTRACE_COMPAT_UNISTD_H +#define _BABELTRACE_COMPAT_UNISTD_H + +/* + * babeltrace/compat/unistd.h + * + * (C) Copyright 2016 - Michael Jeanson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + + +#include + +#ifdef __MINGW32__ +#include +#include + +#define _SC_PAGESIZE 30 + +static inline +long bt_sysconf(int name) +{ + SYSTEM_INFO si; + + switch(name) { + case _SC_PAGESIZE: + GetNativeSystemInfo(&si); + return si.dwPageSize; + default: + errno = EINVAL; + return -1; + } +} + +#else + +static inline +long bt_sysconf(int name) +{ + return sysconf(name); +} + +#endif +#endif /* _BABELTRACE_COMPAT_UNISTD_H */ diff --git a/include/babeltrace/ctf-writer/serialize-internal.h b/include/babeltrace/ctf-writer/serialize-internal.h index 5cf8c5bd..e029764e 100644 --- a/include/babeltrace/ctf-writer/serialize-internal.h +++ b/include/babeltrace/ctf-writer/serialize-internal.h @@ -37,9 +37,12 @@ #include #include #include +#include #include #include +#define PACKET_LEN_INCREMENT (bt_common_get_page_size() * 8 * CHAR_BIT) + struct bt_ctf_stream_pos { int fd; int prot; /* mmap protection */ diff --git a/include/babeltrace/mmap-align-internal.h b/include/babeltrace/mmap-align-internal.h index d89affb5..0667ab0c 100644 --- a/include/babeltrace/mmap-align-internal.h +++ b/include/babeltrace/mmap-align-internal.h @@ -29,6 +29,7 @@ #include #include #include +#include /* * This header implements a wrapper over mmap (mmap_align) that memory @@ -53,19 +54,22 @@ struct mmap_align *mmap_align(size_t length, int prot, { struct mmap_align *mma; off_t page_aligned_offset; /* mmap offset, aligned to floor */ + size_t page_size; + + page_size = bt_common_get_page_size(); mma = malloc(sizeof(*mma)); if (!mma) return MAP_FAILED; mma->length = length; - page_aligned_offset = ALIGN_FLOOR(offset, PAGE_SIZE); + page_aligned_offset = ALIGN_FLOOR(offset, page_size); /* * Page aligned length needs to contain the requested range. * E.g., for a small range that fits within a single page, we might * require a 2 pages page_aligned_length if the range crosses a page * boundary. */ - mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, PAGE_SIZE); + mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size); mma->page_aligned_addr = mmap(NULL, mma->page_aligned_length, prot, flags, fd, page_aligned_offset); if (mma->page_aligned_addr == (void *) -1UL) { diff --git a/lib/ctf-ir/fields.c b/lib/ctf-ir/fields.c index d95fe335..e9c6d8be 100644 --- a/lib/ctf-ir/fields.c +++ b/lib/ctf-ir/fields.c @@ -39,8 +39,6 @@ #include #include -#define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) - static struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); static diff --git a/lib/ctf-writer/serialize.c b/lib/ctf-writer/serialize.c index f20ad405..11ce8b44 100644 --- a/lib/ctf-writer/serialize.c +++ b/lib/ctf-writer/serialize.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #if (FLT_RADIX != 2) @@ -284,7 +285,7 @@ void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index, /* The writer will add padding */ pos->mmap_offset += pos->packet_size / CHAR_BIT; - pos->packet_size = getpagesize() * 8 * CHAR_BIT; + pos->packet_size = PACKET_LEN_INCREMENT; do { ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, pos->packet_size / CHAR_BIT); diff --git a/plugins/ctf/fs-src/fs.c b/plugins/ctf/fs-src/fs.c index 0e199e92..bcb33026 100644 --- a/plugins/ctf/fs-src/fs.c +++ b/plugins/ctf/fs-src/fs.c @@ -42,7 +42,6 @@ #include #include #include -#include #include "fs.h" #include "metadata.h" #include "data-stream-file.h" @@ -1277,7 +1276,7 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp, } ctf_fs->error_fp = stderr; - ctf_fs->page_size = (size_t) getpagesize(); + ctf_fs->page_size = bt_common_get_page_size(); ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); if (!ctf_fs->port_data) { goto error; -- 2.34.1