From 9e29b700c6a660ec9f2e7ccf1bcf8da0aa4df669 Mon Sep 17 00:00:00 2001 From: Kevin Li Date: Mon, 4 Nov 2024 14:07:37 +0800 Subject: [PATCH] add brpc@1.11.0 to registry --- registry/modules/brpc/1.11.0/MODULE.bazel | 31 ++++++++++ registry/modules/brpc/1.11.0/source.json | 5 ++ src/babylon/concurrent/thread_local.h | 75 ++++++++++++++++++++--- src/babylon/reusable/patch/arena.25.0.inc | 22 ++++--- src/babylon/reusable/patch/arena.27.0.inc | 22 ++++--- src/babylon/reusable/patch/arena.28.0.inc | 22 ++++--- 6 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 registry/modules/brpc/1.11.0/MODULE.bazel create mode 100644 registry/modules/brpc/1.11.0/source.json diff --git a/registry/modules/brpc/1.11.0/MODULE.bazel b/registry/modules/brpc/1.11.0/MODULE.bazel new file mode 100644 index 00000000..d2b679fe --- /dev/null +++ b/registry/modules/brpc/1.11.0/MODULE.bazel @@ -0,0 +1,31 @@ +module( + name = 'brpc', + version = '1.11.0', + compatibility_level = 1, +) + +# --registry=https://bcr.bazel.build +bazel_dep(name = 'abseil-cpp', version = '20210324.2', repo_name = 'com_google_absl') +bazel_dep(name = 'bazel_skylib', version = '1.0.3') +bazel_dep(name = 'boringssl', version = '0.0.0-20211025-d4f1ab9') +bazel_dep(name = 'protobuf', version = '3.19.6', repo_name = 'com_google_protobuf') +bazel_dep(name = 'gflags', version = '2.2.2', repo_name = 'com_github_gflags_gflags') +bazel_dep(name = 'glog', version = '0.5.0', repo_name = 'com_github_google_glog') +bazel_dep(name = 'platforms', version = '0.0.4') +bazel_dep(name = 'rules_cc', version = '0.0.1') +bazel_dep(name = 'rules_proto', version = '4.0.0') +bazel_dep(name = 'zlib', version = '1.2.13', repo_name = 'com_github_madler_zlib') + +# --registry=https://baidu.github.io/babylon/registry +bazel_dep(name = 'leveldb', version = '1.23', repo_name = 'com_github_google_leveldb') +bazel_dep(name = 'openssl', version = '3.3.2') +bazel_dep(name = 'thrift', version = '0.21.0', repo_name = 'org_apache_thrift') + +# test only +bazel_dep(name = 'googletest', version = '1.14.0.bcr.1', repo_name = 'com_google_googletest', dev_dependency = True) +bazel_dep(name = 'hedron_compile_commands', dev_dependency = True) +git_override( + module_name = 'hedron_compile_commands', + remote = 'https://github.com/hedronvision/bazel-compile-commands-extractor.git', + commit = '1e08f8e0507b6b6b1f4416a9a22cf5c28beaba93', # Jun 28, 2024 +) diff --git a/registry/modules/brpc/1.11.0/source.json b/registry/modules/brpc/1.11.0/source.json new file mode 100644 index 00000000..e5880e04 --- /dev/null +++ b/registry/modules/brpc/1.11.0/source.json @@ -0,0 +1,5 @@ +{ + "url": "https://github.com/apache/brpc/archive/refs/tags/1.11.0.tar.gz", + "strip_prefix": "brpc-1.11.0", + "integrity": "sha256-74flQ6ULckbOgzBlqGEIT4wypOInNptEBfIl+CqNMDA=" +} diff --git a/src/babylon/concurrent/thread_local.h b/src/babylon/concurrent/thread_local.h index b0e52412..edf82685 100644 --- a/src/babylon/concurrent/thread_local.h +++ b/src/babylon/concurrent/thread_local.h @@ -14,23 +14,18 @@ class EnumerableThreadLocal { public: // 可默认构造,可以移动,不能拷贝 EnumerableThreadLocal() noexcept; - inline EnumerableThreadLocal(EnumerableThreadLocal&&) noexcept = default; + EnumerableThreadLocal(EnumerableThreadLocal&&) noexcept; EnumerableThreadLocal(const EnumerableThreadLocal&) = delete; - inline EnumerableThreadLocal& operator=(EnumerableThreadLocal&&) noexcept = - default; + EnumerableThreadLocal& operator=(EnumerableThreadLocal&&) noexcept; EnumerableThreadLocal& operator=(const EnumerableThreadLocal&) = delete; template EnumerableThreadLocal(C&& constructor) noexcept; - void set_constructor(::std::function constructor) noexcept { - _storage.set_constructor(constructor); - } + void set_constructor(::std::function constructor) noexcept; // 获得线程局部存储 - inline T& local() { - return _storage.ensure(ThreadId::current_thread_id().value); - } + inline T& local(); // 遍历所有【当前或曾经存在】的线程局部存储 template _storage; + size_t _id {fetch_add_id()}; +}; + +template +struct EnumerableThreadLocal::Cache { + size_t id {0}; + T* item {nullptr}; }; // 典型线程局部存储为了性能一般会独占缓存行 @@ -162,12 +170,29 @@ class CompactEnumerableThreadLocal { Storage* _storage; }; +//////////////////////////////////////////////////////////////////////////////// +// EnumerableThreadLocal begin template inline EnumerableThreadLocal::EnumerableThreadLocal() noexcept { // 确保内部使用的ThreadId此时初始化,建立正确的析构顺序 ThreadId::end(); } +template +EnumerableThreadLocal::EnumerableThreadLocal( + EnumerableThreadLocal&& other) noexcept + : EnumerableThreadLocal {} { + *this = ::std::move(other); +} + +template +EnumerableThreadLocal& EnumerableThreadLocal::operator=( + EnumerableThreadLocal&& other) noexcept { + ::std::swap(_id, other._id); + ::std::swap(_storage, other._storage); + return *this; +} + template template inline EnumerableThreadLocal::EnumerableThreadLocal(C&& constructor) noexcept @@ -176,6 +201,38 @@ inline EnumerableThreadLocal::EnumerableThreadLocal(C&& constructor) noexcept ThreadId::end(); } +template +ABSL_ATTRIBUTE_NOINLINE void EnumerableThreadLocal::set_constructor( + ::std::function constructor) noexcept { + _storage.set_constructor(constructor); +} + +template +ABSL_ATTRIBUTE_ALWAYS_INLINE inline T& EnumerableThreadLocal::local() { + static thread_local Cache cache; + if (ABSL_PREDICT_TRUE(cache.id == _id)) { + return *cache.item; + } + return local_slow(cache); +} + +template +ABSL_ATTRIBUTE_NOINLINE size_t +EnumerableThreadLocal::fetch_add_id() noexcept { + static ::std::atomic next_id {1}; + return next_id.fetch_add(1, ::std::memory_order_relaxed); +} + +template +inline T& EnumerableThreadLocal::local_slow(Cache& cache) { + auto& item = _storage.ensure(ThreadId::current_thread_id().value); + cache.id = _id; + cache.item = &item; + return item; +} +// EnumerableThreadLocal end +//////////////////////////////////////////////////////////////////////////////// + template CompactEnumerableThreadLocal< T, CACHE_LINE_NUM>::CompactEnumerableThreadLocal() noexcept diff --git a/src/babylon/reusable/patch/arena.25.0.inc b/src/babylon/reusable/patch/arena.25.0.inc index 2f35e3bd..5d89f03e 100644 --- a/src/babylon/reusable/patch/arena.25.0.inc +++ b/src/babylon/reusable/patch/arena.25.0.inc @@ -47,8 +47,10 @@ constexpr size_t kAllocPolicyWithMemoryResourcePointerSize = ArenaAlignDefault::Ceil(sizeof(AllocationPolicyWithMemoryResourcePointer)); inline PROTOBUF_ALWAYS_INLINE bool use_swiss_resource(const AllocationPolicy* policy) noexcept { - return policy != nullptr - && policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + if (policy != nullptr) { + return policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + } + return false; } inline PROTOBUF_ALWAYS_INLINE AllocationPolicyWithMemoryResourcePointer::SwissMemoryResource& @@ -225,8 +227,8 @@ void* SerialArena::AllocateAlignedFallback(size_t n) { PROTOBUF_NOINLINE void* SerialArena::AllocateFromStringBlockFallback() { - if (internal::use_swiss_resource(parent_.AllocPolicy())) { - auto& resource = internal::swiss_resource(parent_.AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(parent_.AllocPolicy()))) { + auto& resource = swiss_resource(parent_.AllocPolicy()); auto ptr = reinterpret_cast<::std::string*>( resource.allocate(sizeof(std::string))); resource.register_destructor(ptr); @@ -806,7 +808,7 @@ void* ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, size_t align, } void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) { - if (use_swiss_resource(AllocPolicy())) { + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { swiss_resource(AllocPolicy()).do_register_destructor(elem, cleanup); return; } @@ -830,8 +832,8 @@ void* ThreadSafeArena::AllocateAlignedWithCleanupFallback( PROTOBUF_NOINLINE void* ThreadSafeArena::AllocateFromStringBlock() { - if (internal::use_swiss_resource(AllocPolicy())) { - auto& resource = internal::swiss_resource(AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { + auto& resource = swiss_resource(AllocPolicy()); auto ptr = resource.allocate(sizeof(::std::string)); resource.register_destructor<::std::string>(static_cast<::std::string*>(ptr)); return ptr; @@ -960,14 +962,14 @@ SerialArena* ThreadSafeArena::GetSerialArenaFallback(size_t n) { } // namespace internal void* Arena::Allocate(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); } void* Arena::AllocateForArray(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); @@ -975,7 +977,7 @@ void* Arena::AllocateForArray(size_t n) { void* Arena::AllocateAlignedWithCleanup(size_t n, size_t align, void (*destructor)(void*)) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { auto& resource = internal::swiss_resource(impl_.AllocPolicy()); auto ptr = resource.allocate<8>(n); resource.do_register_destructor(ptr, destructor); diff --git a/src/babylon/reusable/patch/arena.27.0.inc b/src/babylon/reusable/patch/arena.27.0.inc index 118565ad..dc4d9db3 100644 --- a/src/babylon/reusable/patch/arena.27.0.inc +++ b/src/babylon/reusable/patch/arena.27.0.inc @@ -48,8 +48,10 @@ constexpr size_t kAllocPolicyWithMemoryResourcePointerSize = ArenaAlignDefault::Ceil(sizeof(AllocationPolicyWithMemoryResourcePointer)); inline PROTOBUF_ALWAYS_INLINE bool use_swiss_resource(const AllocationPolicy* policy) noexcept { - return policy != nullptr - && policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + if (policy != nullptr) { + return policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + } + return false; } inline PROTOBUF_ALWAYS_INLINE AllocationPolicyWithMemoryResourcePointer::SwissMemoryResource& @@ -219,8 +221,8 @@ void* SerialArena::AllocateAlignedFallback(size_t n) { PROTOBUF_NOINLINE void* SerialArena::AllocateFromStringBlockFallback() { - if (internal::use_swiss_resource(parent_.AllocPolicy())) { - auto& resource = internal::swiss_resource(parent_.AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(parent_.AllocPolicy()))) { + auto& resource = swiss_resource(parent_.AllocPolicy()); auto ptr = reinterpret_cast<::std::string*>( resource.allocate(sizeof(std::string))); resource.register_destructor(ptr); @@ -809,7 +811,7 @@ void* ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, size_t align, } void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) { - if (use_swiss_resource(AllocPolicy())) { + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { swiss_resource(AllocPolicy()).do_register_destructor(elem, cleanup); return; } @@ -833,8 +835,8 @@ void* ThreadSafeArena::AllocateAlignedWithCleanupFallback( PROTOBUF_NOINLINE void* ThreadSafeArena::AllocateFromStringBlock() { - if (internal::use_swiss_resource(AllocPolicy())) { - auto& resource = internal::swiss_resource(AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { + auto& resource = swiss_resource(AllocPolicy()); auto ptr = resource.allocate(sizeof(::std::string)); resource.register_destructor<::std::string>(static_cast<::std::string*>(ptr)); return ptr; @@ -974,14 +976,14 @@ SerialArena* ThreadSafeArena::GetSerialArenaFallback(size_t n) { } // namespace internal void* Arena::Allocate(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); } void* Arena::AllocateForArray(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); @@ -989,7 +991,7 @@ void* Arena::AllocateForArray(size_t n) { void* Arena::AllocateAlignedWithCleanup(size_t n, size_t align, void (*destructor)(void*)) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { auto& resource = internal::swiss_resource(impl_.AllocPolicy()); auto ptr = resource.allocate<8>(n); resource.do_register_destructor(ptr, destructor); diff --git a/src/babylon/reusable/patch/arena.28.0.inc b/src/babylon/reusable/patch/arena.28.0.inc index 0dc971ba..3b6e31f4 100644 --- a/src/babylon/reusable/patch/arena.28.0.inc +++ b/src/babylon/reusable/patch/arena.28.0.inc @@ -48,8 +48,10 @@ constexpr size_t kAllocPolicyWithMemoryResourcePointerSize = ArenaAlignDefault::Ceil(sizeof(AllocationPolicyWithMemoryResourcePointer)); inline PROTOBUF_ALWAYS_INLINE bool use_swiss_resource(const AllocationPolicy* policy) noexcept { - return policy != nullptr - && policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + if (policy != nullptr) { + return policy->block_alloc == ::babylon::SwissMemoryResource::arena_block_alloc; + } + return false; } inline PROTOBUF_ALWAYS_INLINE AllocationPolicyWithMemoryResourcePointer::SwissMemoryResource& @@ -297,8 +299,8 @@ void* SerialArena::AllocateAlignedFallback(size_t n) { PROTOBUF_NOINLINE void* SerialArena::AllocateFromStringBlockFallback() { - if (internal::use_swiss_resource(parent_.AllocPolicy())) { - auto& resource = internal::swiss_resource(parent_.AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(parent_.AllocPolicy()))) { + auto& resource = swiss_resource(parent_.AllocPolicy()); auto ptr = reinterpret_cast<::std::string*>( resource.allocate(sizeof(std::string))); resource.register_destructor(ptr); @@ -856,7 +858,7 @@ void* ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, size_t align, } void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) { - if (use_swiss_resource(AllocPolicy())) { + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { swiss_resource(AllocPolicy()).do_register_destructor(elem, cleanup); return; } @@ -880,8 +882,8 @@ void* ThreadSafeArena::AllocateAlignedWithCleanupFallback( PROTOBUF_NOINLINE void* ThreadSafeArena::AllocateFromStringBlock() { - if (internal::use_swiss_resource(AllocPolicy())) { - auto& resource = internal::swiss_resource(AllocPolicy()); + if (ABSL_PREDICT_TRUE(use_swiss_resource(AllocPolicy()))) { + auto& resource = swiss_resource(AllocPolicy()); auto ptr = resource.allocate(sizeof(::std::string)); resource.register_destructor<::std::string>(static_cast<::std::string*>(ptr)); return ptr; @@ -1021,14 +1023,14 @@ SerialArena* ThreadSafeArena::GetSerialArenaFallback(size_t n) { } // namespace internal void* Arena::Allocate(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); } void* Arena::AllocateForArray(size_t n) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { return internal::swiss_resource(impl_.AllocPolicy()).allocate<8>(n); } return impl_.AllocateAligned(n); @@ -1036,7 +1038,7 @@ void* Arena::AllocateForArray(size_t n) { void* Arena::AllocateAlignedWithCleanup(size_t n, size_t align, void (*destructor)(void*)) { - if (internal::use_swiss_resource(impl_.AllocPolicy())) { + if (ABSL_PREDICT_TRUE(internal::use_swiss_resource(impl_.AllocPolicy()))) { auto& resource = internal::swiss_resource(impl_.AllocPolicy()); auto ptr = resource.allocate<8>(n); resource.do_register_destructor(ptr, destructor);