From 2d4f2c38672b057168a4c2401b007f9c5a342aae Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 13 Mar 2020 00:04:56 +0000 Subject: [PATCH 1/7] AAL, basic arm implementation proposal. --- CMakeLists.txt | 3 ++- src/aal/aal.h | 8 ++++++- src/aal/aal_arm.h | 53 +++++++++++++++++++++++++++++++++++++++++++++ src/ds/bits.h | 1 + src/pal/pal_posix.h | 1 + 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/aal/aal_arm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e07a06d..d7dc850 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,7 +136,8 @@ if(NOT DEFINED SNMALLOC_ONLY_HEADER_LIBRARY) add_compile_options(-march=native) elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86") add_compile_options(-march=native) - # XXX elseif ARM? + elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm") + add_compile_options(-march=native) endif() endif() diff --git a/src/aal/aal.h b/src/aal/aal.h index e963250..d323bfd 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -9,6 +9,10 @@ # define PLATFORM_IS_X86 #endif +#if defined(__arm__) || defined(__aarch64__) +# define PLATFORM_IS_ARM +#endif + namespace snmalloc { /** @@ -70,8 +74,10 @@ namespace snmalloc } // namespace snmalloc -#ifdef PLATFORM_IS_X86 +#if defined(PLATFORM_IS_X86) # include "aal_x86.h" +#elif defined(PLATFORM_IS_ARM) +# include "aal_arm.h" #endif namespace snmalloc diff --git a/src/aal/aal_arm.h b/src/aal/aal_arm.h new file mode 100644 index 0000000..52bb0d0 --- /dev/null +++ b/src/aal/aal_arm.h @@ -0,0 +1,53 @@ +#pragma once + +#if defined(__arch64__) +# define SNMALLOC_VA_BITS_64 +#else +# define SNMALLOC_VA_BITS_32 +#endif + +#include +namespace snmalloc +{ + /** + * ARM-specific architecture abstraction layer. + */ + class AAL_arm + { + public: + /** + * Bitmap of AalFeature flags + */ + static constexpr uint64_t aal_features = IntegerPointers; + /** + * On pipelined processors, notify the core that we are in a spin loop and + * that speculative execution past this point may not be a performance gain. + */ + static inline void pause() + { + __asm__ volatile("yield"); + } + + /** + * Issue a prefetch hint at the specified address. + */ + static inline void prefetch(void* ptr) + { + __builtin_prefetch(ptr, 0, 1); + } + + /** + * Return a cycle counter value. + * on ARM cpu counters are accessible only in privileged mode + */ + static inline uint64_t tick() + { + struct timespec n = {0, 0ul}; + clock_gettime(CLOCK_MONOTONIC, &n); + + return static_cast((n.tv_sec) * (1000000000ul * n.tv_nsec)); + } + }; + + using AAL_Arch = AAL_arm; +} // namespace snmalloc diff --git a/src/ds/bits.h b/src/ds/bits.h index 9dc05b0..6f3621e 100644 --- a/src/ds/bits.h +++ b/src/ds/bits.h @@ -1,6 +1,7 @@ #pragma once #include +#include // #define USE_LZCNT diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 57d76ac..4344ad4 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -4,6 +4,7 @@ #include "../mem/allocconfig.h" #include +#include #include extern "C" int puts(const char* str); From e7f020cf764615e29cba57859f639aecbd6d1305 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 13 Mar 2020 15:39:49 +0000 Subject: [PATCH 2/7] implementations moved on the composer class. --- CMakeLists.txt | 4 +++- src/aal/aal.h | 12 +++++++++++- src/aal/aal_arm.h | 12 ++---------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d7dc850..414417a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,9 @@ if (WIN32) endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - target_compile_options(snmalloc_lib INTERFACE -mcx16) + if (NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm") + target_compile_options(snmalloc_lib INTERFACE -mcx16) + endif() endif () # Have to set this globally, as can't be set on an interface target. diff --git a/src/aal/aal.h b/src/aal/aal.h index d323bfd..c1cbd02 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -2,6 +2,7 @@ #include "../ds/defines.h" #include +#include #if defined(__i386__) || defined(_M_IX86) || defined(_X86_) || \ defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \ @@ -11,6 +12,10 @@ #if defined(__arm__) || defined(__aarch64__) # define PLATFORM_IS_ARM + /** + * on ARM cpu counters are accessible only in privileged mode + */ +# define SNMALLOC_NO_ALL_CPUCOUNTERS #endif namespace snmalloc @@ -63,7 +68,12 @@ namespace snmalloc */ static inline uint64_t tick() { -#if __has_builtin(__builtin_readcyclecounter) && \ +#if defined(SNMALLOC_NO_ALL_CPUCOUNTERS) + struct timespec n = {0, 0ul}; + clock_gettime(CLOCK_MONOTONIC, &n); + + return static_cast((n.tv_sec) * (1000000000 * n.tv_nsec)); +#elif __has_builtin(__builtin_readcyclecounter) && \ !defined(SNMALLOC_NO_AAL_BUILTINS) return __builtin_readcyclecounter(); #else diff --git a/src/aal/aal_arm.h b/src/aal/aal_arm.h index 52bb0d0..00964e4 100644 --- a/src/aal/aal_arm.h +++ b/src/aal/aal_arm.h @@ -31,21 +31,13 @@ namespace snmalloc /** * Issue a prefetch hint at the specified address. */ - static inline void prefetch(void* ptr) + static inline void prefetch(void*) { - __builtin_prefetch(ptr, 0, 1); } - /** - * Return a cycle counter value. - * on ARM cpu counters are accessible only in privileged mode - */ static inline uint64_t tick() { - struct timespec n = {0, 0ul}; - clock_gettime(CLOCK_MONOTONIC, &n); - - return static_cast((n.tv_sec) * (1000000000ul * n.tv_nsec)); + return 0ull; } }; From 6b8650e4ce386d79938f5a565f566b5a07c20384 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 13 Mar 2020 16:46:16 +0000 Subject: [PATCH 3/7] Using fallback when the (none)feature bit is present. --- src/aal/aal.h | 20 +++++++++++--------- src/aal/aal_arm.h | 14 +------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/aal/aal.h b/src/aal/aal.h index c1cbd02..9552c26 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -12,10 +12,6 @@ #if defined(__arm__) || defined(__aarch64__) # define PLATFORM_IS_ARM - /** - * on ARM cpu counters are accessible only in privileged mode - */ -# define SNMALLOC_NO_ALL_CPUCOUNTERS #endif namespace snmalloc @@ -31,6 +27,10 @@ namespace snmalloc * and so may use bit operations on pointer values. */ IntegerPointers = (1 << 0), + /** + * This architecture cannot access cpu cycles counters from userspace + */ + NoCpuCycleCounters = (1 << 1), }; /** @@ -68,12 +68,14 @@ namespace snmalloc */ static inline uint64_t tick() { -#if defined(SNMALLOC_NO_ALL_CPUCOUNTERS) - struct timespec n = {0, 0ul}; - clock_gettime(CLOCK_MONOTONIC, &n); + if constexpr((Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) + { + struct timespec n = {0, 0ul}; + clock_gettime(CLOCK_MONOTONIC, &n); - return static_cast((n.tv_sec) * (1000000000 * n.tv_nsec)); -#elif __has_builtin(__builtin_readcyclecounter) && \ + return static_cast((n.tv_sec) * (1000000000 * n.tv_nsec)); + } +#if __has_builtin(__builtin_readcyclecounter) && \ !defined(SNMALLOC_NO_AAL_BUILTINS) return __builtin_readcyclecounter(); #else diff --git a/src/aal/aal_arm.h b/src/aal/aal_arm.h index 00964e4..73e79f9 100644 --- a/src/aal/aal_arm.h +++ b/src/aal/aal_arm.h @@ -18,7 +18,7 @@ namespace snmalloc /** * Bitmap of AalFeature flags */ - static constexpr uint64_t aal_features = IntegerPointers; + static constexpr uint64_t aal_features = IntegerPointers | NoCpuCycleCounters; /** * On pipelined processors, notify the core that we are in a spin loop and * that speculative execution past this point may not be a performance gain. @@ -27,18 +27,6 @@ namespace snmalloc { __asm__ volatile("yield"); } - - /** - * Issue a prefetch hint at the specified address. - */ - static inline void prefetch(void*) - { - } - - static inline uint64_t tick() - { - return 0ull; - } }; using AAL_Arch = AAL_arm; From e611b325ce92b096794a8fe1bb1559ff77eefec9 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 16 Mar 2020 18:59:51 +0000 Subject: [PATCH 4/7] Last tweaks --- src/aal/aal.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/aal/aal.h b/src/aal/aal.h index 9552c26..3632236 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -68,12 +68,17 @@ namespace snmalloc */ static inline uint64_t tick() { - if constexpr((Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) + if constexpr ( + (Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) { +#ifdef _WIN32 +# error "Implement Windows Version" +#else struct timespec n = {0, 0ul}; clock_gettime(CLOCK_MONOTONIC, &n); return static_cast((n.tv_sec) * (1000000000 * n.tv_nsec)); +#endif } #if __has_builtin(__builtin_readcyclecounter) && \ !defined(SNMALLOC_NO_AAL_BUILTINS) From c6baa0baa300eb29b96c58824aca39dd9d14964e Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 17 Mar 2020 10:08:17 +0000 Subject: [PATCH 5/7] using C++11 api instead for fallback call. --- src/aal/aal.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/aal/aal.h b/src/aal/aal.h index 3632236..a62aee4 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -1,8 +1,8 @@ #pragma once #include "../ds/defines.h" +#include #include -#include #if defined(__i386__) || defined(_M_IX86) || defined(_X86_) || \ defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \ @@ -71,14 +71,11 @@ namespace snmalloc if constexpr ( (Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters) { -#ifdef _WIN32 -# error "Implement Windows Version" -#else - struct timespec n = {0, 0ul}; - clock_gettime(CLOCK_MONOTONIC, &n); - - return static_cast((n.tv_sec) * (1000000000 * n.tv_nsec)); -#endif + auto tick = std::chrono::high_resolution_clock::now(); + return static_cast( + std::chrono::duration_cast( + tick.time_since_epoch()) + .count()); } #if __has_builtin(__builtin_readcyclecounter) && \ !defined(SNMALLOC_NO_AAL_BUILTINS) From 55f1237df90ddfc987bf27efb0bc92451a1cecf4 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 17 Mar 2020 11:18:24 +0000 Subject: [PATCH 6/7] Few build tweaks. --- CMakeLists.txt | 8 +++++++- src/aal/aal.h | 2 +- src/ds/bits.h | 2 +- src/pal/pal_posix.h | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 414417a..6e7438f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,8 +94,14 @@ if (WIN32) endif() if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - if (NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm") + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") target_compile_options(snmalloc_lib INTERFACE -mcx16) + elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64") + target_compile_options(snmalloc_lib INTERFACE -mcx16) + elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64") + target_compile_options(snmalloc_lib INTERFACE -mcx16) + elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86") + target_compile_options(snmalloc_lib INTERFACE -mcx16) endif() endif () diff --git a/src/aal/aal.h b/src/aal/aal.h index a62aee4..4f5355f 100644 --- a/src/aal/aal.h +++ b/src/aal/aal.h @@ -28,7 +28,7 @@ namespace snmalloc */ IntegerPointers = (1 << 0), /** - * This architecture cannot access cpu cycles counters from userspace + * This architecture cannot access cpu cycles counters. */ NoCpuCycleCounters = (1 << 1), }; diff --git a/src/ds/bits.h b/src/ds/bits.h index 6f3621e..12c9bdc 100644 --- a/src/ds/bits.h +++ b/src/ds/bits.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include // #define USE_LZCNT diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index 4344ad4..4c3a842 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -3,8 +3,8 @@ #include "../ds/address.h" #include "../mem/allocconfig.h" -#include #include +#include #include extern "C" int puts(const char* str); From 04909305aa195253e126a34659c167b86f69076b Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Tue, 17 Mar 2020 12:28:14 +0000 Subject: [PATCH 7/7] Update src/aal/aal_arm.h --- src/aal/aal_arm.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/aal/aal_arm.h b/src/aal/aal_arm.h index 73e79f9..9dc722b 100644 --- a/src/aal/aal_arm.h +++ b/src/aal/aal_arm.h @@ -18,7 +18,8 @@ namespace snmalloc /** * Bitmap of AalFeature flags */ - static constexpr uint64_t aal_features = IntegerPointers | NoCpuCycleCounters; + static constexpr uint64_t aal_features = + IntegerPointers | NoCpuCycleCounters; /** * On pipelined processors, notify the core that we are in a spin loop and * that speculative execution past this point may not be a performance gain.