diff options
author | Yijie Ma <yijiem@google.com> | 2023-06-02 16:55:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-02 16:55:01 -0700 |
commit | 4ad81d3da5a86e1f4c84c3a10a72f088a90017a6 (patch) | |
tree | 83d05ddfa2ab532ff0cc7d7335b562f04308047a | |
parent | 91ff8b2bcad426285e50db54275aa670f4166e68 (diff) | |
download | grpc-grpc-4ad81d3da5a86e1f4c84c3a10a72f088a90017a6.tar.gz |
[channelz] Backport "[channelz] Make the cacheline stuff work properly for C++14 (#33327)" to v1.56.x (#33331)
Backport https://github.com/grpc/grpc/pull/33327 to v1.56.x
<!--
If you know who should review your pull request, please assign it to
that
person, otherwise the pull request would get assigned randomly.
If your pull request is for a specific language, please add the
appropriate
lang label.
-->
Co-authored-by: Craig Tiller <ctiller@google.com>
-rw-r--r-- | src/core/lib/channel/channelz.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/core/lib/channel/channelz.h b/src/core/lib/channel/channelz.h index 772aa0fcde..1713171dd6 100644 --- a/src/core/lib/channel/channelz.h +++ b/src/core/lib/channel/channelz.h @@ -152,12 +152,33 @@ class PerCpuCallCountingHelper { // testing peer friend. friend class testing::CallCountingHelperPeer; + // We want to ensure that this per-cpu data structure lands on different + // cachelines per cpu. + // With C++17 we can do so explicitly with an `alignas` specifier. + // Prior versions we can at best approximate it by padding the structure. + // It'll probably work out ok, but it's not guaranteed across allocators. + // (in the bad case where this gets split across cachelines we'll just have + // two cpus fighting over the same cacheline with a slight performance + // degregation). + // TODO(ctiller): When we move to C++17 delete the duplicate definition. +#if __cplusplus >= 201703L struct alignas(GPR_CACHELINE_SIZE) PerCpuData { std::atomic<int64_t> calls_started{0}; std::atomic<int64_t> calls_succeeded{0}; std::atomic<int64_t> calls_failed{0}; std::atomic<gpr_cycle_counter> last_call_started_cycle{0}; }; +#else + struct PerCpuDataHeader { + std::atomic<int64_t> calls_started{0}; + std::atomic<int64_t> calls_succeeded{0}; + std::atomic<int64_t> calls_failed{0}; + std::atomic<gpr_cycle_counter> last_call_started_cycle{0}; + }; + struct PerCpuData : public PerCpuDataHeader { + uint8_t padding[GPR_CACHELINE_SIZE - sizeof(PerCpuDataHeader)]; + }; +#endif PerCpu<PerCpuData> per_cpu_data_{PerCpuOptions().SetCpusPerShard(4)}; }; |