From d82e47e9d29a5505006ad66063f5ed6182d05602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Fri, 15 Nov 2019 00:00:00 +0000 Subject: [PATCH] cpp atomic: define to 1 which is saner --- README.adoc | 2 ++ userland/cpp/atomic/aarch64_add.cpp | 2 +- userland/cpp/atomic/aarch64_ldadd.cpp | 2 +- userland/cpp/atomic/fail.cpp | 2 +- userland/cpp/atomic/main.hpp | 22 +++++++++++----------- userland/cpp/atomic/mutex.cpp | 2 +- userland/cpp/atomic/std_atomic.cpp | 2 +- userland/cpp/atomic/x86_64_inc.cpp | 2 +- userland/cpp/atomic/x86_64_lock_inc.cpp | 2 +- 9 files changed, 20 insertions(+), 18 deletions(-) diff --git a/README.adoc b/README.adoc index 7543e8f..a68ccc8 100644 --- a/README.adoc +++ b/README.adoc @@ -11097,6 +11097,8 @@ dhrystone ===== BST vs heap vs hashmap +TODO: move benchmark graph from link:userland/cpp/bst_vs_heap_vs_hashmap.cpp[] to link:userland/algorithm/set[]. + The following benchmark setup works both: * on host through timers + https://stackoverflow.com/questions/51952471/why-do-i-get-a-constant-instead-of-logarithmic-curve-for-an-insert-time-benchmar/51953081#51953081[granule] diff --git a/userland/cpp/atomic/aarch64_add.cpp b/userland/cpp/atomic/aarch64_add.cpp index ecdd2f8..a822555 100644 --- a/userland/cpp/atomic/aarch64_add.cpp +++ b/userland/cpp/atomic/aarch64_add.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_AARCH64_ADD +#define LKMC_USERLAND_ATOMIC_AARCH64_ADD 1 #include "main.hpp" diff --git a/userland/cpp/atomic/aarch64_ldadd.cpp b/userland/cpp/atomic/aarch64_ldadd.cpp index 830200d..cb7532e 100644 --- a/userland/cpp/atomic/aarch64_ldadd.cpp +++ b/userland/cpp/atomic/aarch64_ldadd.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_AARCH64_LDADD +#define LKMC_USERLAND_ATOMIC_AARCH64_LDADD 1 #include "main.hpp" diff --git a/userland/cpp/atomic/fail.cpp b/userland/cpp/atomic/fail.cpp index 1d9d023..0a773b1 100644 --- a/userland/cpp/atomic/fail.cpp +++ b/userland/cpp/atomic/fail.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_FAIL +#define LKMC_USERLAND_ATOMIC_FAIL 1 #include "main.hpp" diff --git a/userland/cpp/atomic/main.hpp b/userland/cpp/atomic/main.hpp index 1600ac5..7cd272b 100644 --- a/userland/cpp/atomic/main.hpp +++ b/userland/cpp/atomic/main.hpp @@ -10,29 +10,29 @@ size_t niters; -#if defined(LKMC_USERLAND_ATOMIC_STD_ATOMIC) +#if LKMC_USERLAND_ATOMIC_STD_ATOMIC std::atomic_ulong global(0); #else uint64_t global = 0; #endif -#if defined(LKMC_USERLAND_ATOMIC_MUTEX) +#if LKMC_USERLAND_ATOMIC_MUTEX std::mutex mutex; #endif void threadMain() { for (size_t i = 0; i < niters; ++i) { -#if defined(LKMC_USERLAND_ATOMIC_MUTEX) +#if LKMC_USERLAND_ATOMIC_MUTEX mutex.lock(); #endif -#if defined(LKMC_USERLAND_ATOMIC_X86_64_INC) +#if LKMC_USERLAND_ATOMIC_X86_64_INC __asm__ __volatile__ ( "incq %0;" : "+g" (global) : : ); -#elif defined(LKMC_USERLAND_ATOMIC_X86_64_LOCK_INC) +#elif LKMC_USERLAND_ATOMIC_X86_64_LOCK_INC // https://cirosantilli.com/linux-kernel-module-cheat#x86-lock-prefix __asm__ __volatile__ ( "lock;" @@ -41,14 +41,14 @@ void threadMain() { : : ); -#elif defined(LKMC_USERLAND_ATOMIC_AARCH64_ADD) +#elif LKMC_USERLAND_ATOMIC_AARCH64_ADD __asm__ __volatile__ ( "add %0, %0, 1;" : "+r" (global) : : ); -#elif defined(LKMC_USERLAND_ATOMIC_AARCH64_LDADD) +#elif LKMC_USERLAND_ATOMIC_AARCH64_LDADD // https://cirosantilli.com/linux-kernel-module-cheat#arm-lse __asm__ __volatile__ ( "ldadd %[inc], xzr, [%[addr]];" @@ -60,7 +60,7 @@ void threadMain() { #else global++; #endif -#if defined(LKMC_USERLAND_ATOMIC_MUTEX) +#if LKMC_USERLAND_ATOMIC_MUTEX mutex.unlock(); #endif #if 0 @@ -88,9 +88,9 @@ int main(int argc, char **argv) { for (size_t i = 0; i < nthreads; ++i) threads[i].join(); uint64_t expect = nthreads * niters; -#if defined(LKMC_USERLAND_ATOMIC_FAIL) || \ - defined(LKMC_USERLAND_ATOMIC_X86_64_INC) || \ - defined(LKMC_USERLAND_ATOMIC_AARCH64_INC) +#if LKMC_USERLAND_ATOMIC_FAIL || \ + LKMC_USERLAND_ATOMIC_X86_64_INC || \ + LKMC_USERLAND_ATOMIC_AARCH64_INC // These fail, so we just print the outcomes. std::cout << "expect " << expect << std::endl; std::cout << "global " << global << std::endl; diff --git a/userland/cpp/atomic/mutex.cpp b/userland/cpp/atomic/mutex.cpp index 27c2946..996a9c5 100644 --- a/userland/cpp/atomic/mutex.cpp +++ b/userland/cpp/atomic/mutex.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_MUTEX +#define LKMC_USERLAND_ATOMIC_MUTEX 1 #include "main.hpp" diff --git a/userland/cpp/atomic/std_atomic.cpp b/userland/cpp/atomic/std_atomic.cpp index 0569abc..dc48e05 100644 --- a/userland/cpp/atomic/std_atomic.cpp +++ b/userland/cpp/atomic/std_atomic.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_STD_ATOMIC +#define LKMC_USERLAND_ATOMIC_STD_ATOMIC 1 #include "main.hpp" diff --git a/userland/cpp/atomic/x86_64_inc.cpp b/userland/cpp/atomic/x86_64_inc.cpp index 2eeb793..843b23b 100644 --- a/userland/cpp/atomic/x86_64_inc.cpp +++ b/userland/cpp/atomic/x86_64_inc.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_X86_64_INC +#define LKMC_USERLAND_ATOMIC_X86_64_INC 1 #include "main.hpp" diff --git a/userland/cpp/atomic/x86_64_lock_inc.cpp b/userland/cpp/atomic/x86_64_lock_inc.cpp index 4431411..0afb5af 100644 --- a/userland/cpp/atomic/x86_64_lock_inc.cpp +++ b/userland/cpp/atomic/x86_64_lock_inc.cpp @@ -1,2 +1,2 @@ -#define LKMC_USERLAND_ATOMIC_X86_64_LOCK_INC +#define LKMC_USERLAND_ATOMIC_X86_64_LOCK_INC 1 #include "main.hpp"