From 51f25bb87de4721826b9065bbef7c1609ef72007 Mon Sep 17 00:00:00 2001 From: Ivan Morozko Date: Thu, 12 Dec 2024 20:24:26 +0400 Subject: [PATCH] Move ShiftBuffer function from common::sdp to utils This function is too useful and universal to be in that namespace --- autotest/autotest.cpp | 2 +- cli/telegraf.h | 8 ++++---- common/sdpclient.h | 22 +++++++++++----------- common/sdpcommon.h | 15 ++++++--------- common/utils.h | 11 ++++++++--- controlplane/controlplane.cpp | 2 +- controlplane/telegraf.cpp | 2 +- dataplane/controlplane.cpp | 2 +- dataplane/dataplane.cpp | 9 ++++++--- dataplane/unittest/sdp.cpp | 32 ++++++++++++++++---------------- dataplane/worker.cpp | 11 ++++++----- dataplane/worker_gc.cpp | 4 ++-- 12 files changed, 63 insertions(+), 57 deletions(-) diff --git a/autotest/autotest.cpp b/autotest/autotest.cpp index a53655dc..b5acddbb 100644 --- a/autotest/autotest.cpp +++ b/autotest/autotest.cpp @@ -1891,7 +1891,7 @@ common::PacketBufferRing::item_t* read_shm_packet(common::PacketBufferRing* buff return nullptr; } - return common::sdp::ShiftBuffer(ring->memory, position * buffer->unit_size); + return utils::ShiftBuffer(ring->memory, position * buffer->unit_size); } bool tAutotest::step_dumpPackets(const YAML::Node& yamlStep, diff --git a/cli/telegraf.h b/cli/telegraf.h index 577d5f0a..6169fe2a 100644 --- a/cli/telegraf.h +++ b/cli/telegraf.h @@ -710,8 +710,8 @@ void main_counters() for (const auto& [coreId, worker_info] : sdp_data.workers) { std::vector values; - auto* buffer = common::sdp::ShiftBuffer(worker_info.buffer, - sdp_data.metadata_worker.start_counters); + auto* buffer = utils::ShiftBuffer(worker_info.buffer, + sdp_data.metadata_worker.start_counters); for (const auto& [name, index] : sdp_data.metadata_worker.counter_positions) { values.emplace_back(name.data(), buffer[index]); @@ -722,8 +722,8 @@ void main_counters() for (const auto& [coreId, worker_info] : sdp_data.workers_gc) { std::vector values; - auto* buffer = common::sdp::ShiftBuffer(worker_info.buffer, - sdp_data.metadata_worker.start_counters); + auto* buffer = utils::ShiftBuffer(worker_info.buffer, + sdp_data.metadata_worker.start_counters); for (const auto& [name, index] : sdp_data.metadata_worker_gc.counter_positions) { values.emplace_back(name.data(), buffer[index]); diff --git a/common/sdpclient.h b/common/sdpclient.h index 990cb31a..0b2010c6 100644 --- a/common/sdpclient.h +++ b/common/sdpclient.h @@ -131,7 +131,7 @@ class SdpClient shift); return eResult::errorInitSharedMemory; } - iter.second.buffer = ShiftBuffer(buffer, shift); + iter.second.buffer = ShiftBuffer(buffer, shift); } for (auto& iter : sdp_data.workers_gc) { @@ -147,7 +147,7 @@ class SdpClient shift); return eResult::errorInitSharedMemory; } - iter.second.buffer = ShiftBuffer(buffer, shift); + iter.second.buffer = ShiftBuffer(buffer, shift); } return eResult::success; @@ -177,8 +177,8 @@ class SdpClient { if (!core_id.has_value() || worker_core_id == core_id) { - auto* counters = common::sdp::ShiftBuffer(worker_info.buffer, - sdp_data.metadata_worker.start_counters); + auto* counters = ShiftBuffer(worker_info.buffer, + sdp_data.metadata_worker.start_counters); result[worker_core_id] = counters[index]; } } @@ -193,8 +193,8 @@ class SdpClient { if (!core_id.has_value() || worker_core_id == core_id) { - auto* counters = common::sdp::ShiftBuffer(worker_info.buffer, - sdp_data.metadata_worker.start_counters); + auto* counters = ShiftBuffer(worker_info.buffer, + sdp_data.metadata_worker.start_counters); result[worker_core_id] = counters[index]; } } @@ -232,8 +232,8 @@ class SdpClient std::vector buffers; for (const auto& iter : sdp_data.workers) { - buffers.push_back(common::sdp::ShiftBuffer(iter.second.buffer, - sdp_data.metadata_worker.start_counters)); + buffers.push_back(ShiftBuffer(iter.second.buffer, + sdp_data.metadata_worker.start_counters)); } for (size_t i = 0; i < counter_ids.size(); i++) @@ -429,7 +429,7 @@ class SdpClient static uint64_t ReadValue(void* buffer, uint64_t index) { - auto* data = common::sdp::ShiftBuffer(buffer, index * sizeof(uint64_t)); + auto* data = ShiftBuffer(buffer, index * sizeof(uint64_t)); uint64_t result = 0; for (int i = 0; i < 8; i++) { @@ -443,9 +443,9 @@ class SdpClient values.clear(); for (uint64_t index = 0; index < count; index++) { - void* current = common::sdp::ShiftBuffer(buffer, shift + 128 * index); + void* current = ShiftBuffer(buffer, shift + 128 * index); uint64_t value = ReadValue(current, 0); - char* str = common::sdp::ShiftBuffer(current, 8); + char* str = ShiftBuffer(current, 8); if (str[119] != 0) { // 119 - index of last symbol diff --git a/common/sdpcommon.h b/common/sdpcommon.h index 0fda626d..721dd140 100644 --- a/common/sdpcommon.h +++ b/common/sdpcommon.h @@ -6,6 +6,7 @@ #include "define.h" #include "idp.h" +#include "utils.h" // #define YANET_USE_POSIX_SHARED_MEMORY @@ -87,6 +88,8 @@ Block for worker_gc namespace common::sdp { +using utils::ShiftBuffer; + #ifdef YANET_USE_POSIX_SHARED_MEMORY inline std::string FileNameWorkerOnNumaNode(tSocketId socket_id) { @@ -94,12 +97,6 @@ inline std::string FileNameWorkerOnNumaNode(tSocketId socket_id) } #endif -template -inline TResult ShiftBuffer(TBuffer buffer, uint64_t size) -{ - return reinterpret_cast((reinterpret_cast(buffer) + size)); -} - template bool MapsEqual(const std::map& left, const std::map& right) { @@ -224,9 +221,9 @@ struct DataPlaneInSharedMemory { auto count_errors = static_cast(common::idp::errorType::size); auto count_requests = static_cast(common::idp::requestType::size); - auto* requests = common::sdp::ShiftBuffer(dataplane_data, start_bus_section); - auto* errors = common::sdp::ShiftBuffer(dataplane_data, start_bus_section + count_requests * sizeof(uint64_t)); - auto* durations = common::sdp::ShiftBuffer(dataplane_data, start_bus_section + (count_requests + count_errors) * sizeof(uint64_t)); + auto* requests = ShiftBuffer(dataplane_data, start_bus_section); + auto* errors = ShiftBuffer(dataplane_data, start_bus_section + count_requests * sizeof(uint64_t)); + auto* durations = ShiftBuffer(dataplane_data, start_bus_section + (count_requests + count_errors) * sizeof(uint64_t)); return {requests, errors, durations}; } }; diff --git a/common/utils.h b/common/utils.h index dd8d818b..24213e99 100644 --- a/common/utils.h +++ b/common/utils.h @@ -1,15 +1,20 @@ #pragma once -#include -#include -#include #include #include #include +#include namespace utils { +template +TResult ShiftBuffer(void* buffer, size_t size) +{ + static_assert(std::is_pointer_v, "TResult must be a pointer type."); + return reinterpret_cast(static_cast(buffer) + size); +} + // Utility to calculate percentage // TODO C++20: use std::type_identity_t to establish non-deduced context // Will allow to do `to_percent(4.2, 1)` diff --git a/controlplane/controlplane.cpp b/controlplane/controlplane.cpp index ae31b56d..b74dc7f0 100644 --- a/controlplane/controlplane.cpp +++ b/controlplane/controlplane.cpp @@ -1004,7 +1004,7 @@ std::vector cControlPlane::getAclCounters() uint64_t start_acl_counters = sdp_data.metadata_worker.start_acl_counters; for (const auto& iter : sdp_data.workers) { - auto* aclCounters = common::sdp::ShiftBuffer(iter.second.buffer, start_acl_counters); + auto* aclCounters = utils::ShiftBuffer(iter.second.buffer, start_acl_counters); for (size_t i = 0; i < YANET_CONFIG_ACL_COUNTERS_SIZE; i++) { response[i] += aclCounters[i]; diff --git a/controlplane/telegraf.cpp b/controlplane/telegraf.cpp index 05dc955f..3817148b 100644 --- a/controlplane/telegraf.cpp +++ b/controlplane/telegraf.cpp @@ -304,7 +304,7 @@ common::icp::telegraf_other::response telegraf_t::telegraf_other() { std::array bursts; auto* worker_bursts = - common::sdp::ShiftBuffer(worker_info.buffer, sdp_data->metadata_worker.start_bursts); + utils::ShiftBuffer(worker_info.buffer, sdp_data->metadata_worker.start_bursts); memcpy(&bursts[0], worker_bursts, sizeof(uint64_t) * (CONFIG_YADECAP_MBUFS_BURST_SIZE + 1)); currWorkers[coreId] = bursts; } diff --git a/dataplane/controlplane.cpp b/dataplane/controlplane.cpp index 79677e39..e17146e4 100644 --- a/dataplane/controlplane.cpp +++ b/dataplane/controlplane.cpp @@ -1096,7 +1096,7 @@ common::idp::hexdump_ring::response cControlPlane::hexdump_ring(const common::id return {}; } - auto addr = common::sdp::ShiftBuffer(shmaddr, offset); + auto addr = utils::ShiftBuffer(shmaddr, offset); } response.hexdumped_ring = //hexdump addr, size (what size) here; diff --git a/dataplane/dataplane.cpp b/dataplane/dataplane.cpp index 64aec56d..bc83abb7 100644 --- a/dataplane/dataplane.cpp +++ b/dataplane/dataplane.cpp @@ -36,8 +36,8 @@ #include "common/tsc_deltas.h" #include "dataplane.h" #include "dataplane/sdpserver.h" -#include "sharedmemory.h" #include "globalbase.h" +#include "sharedmemory.h" #include "sock_dev.h" #include "work_runner.h" #include "worker.h" @@ -1681,6 +1681,9 @@ eResult cDataPlane::allocateSharedMemory() /// split memory per worker eResult cDataPlane::splitSharedMemoryPerWorkers() { + using sharedmemory::SharedMemoryDumpRing; + using utils::ShiftBuffer; + for (cWorker* worker : workers_vector) { tSocketId socket_id = worker->socketId; @@ -1700,7 +1703,7 @@ eResult cDataPlane::splitSharedMemoryPerWorkers() { const auto& [dump_size, dump_count, format] = ring_cfg; - auto memaddr = common::sdp::ShiftBuffer(shm, offset); + auto memaddr = utils::ShiftBuffer(shm, offset); sharedmemory::SharedMemoryDumpRing ring(format, memaddr, dump_size, dump_count); worker->dumpRings[ring_id] = ring; @@ -1715,7 +1718,7 @@ eResult cDataPlane::splitSharedMemoryPerWorkers() ring_id++; } - auto memaddr = common::sdp::ShiftBuffer(shm, offset); + auto memaddr = utils::ShiftBuffer(shm, offset); worker->tsc_deltas = new (memaddr) dataplane::perf::tsc_deltas{}; offset += sizeof(dataplane::perf::tsc_deltas); diff --git a/dataplane/unittest/sdp.cpp b/dataplane/unittest/sdp.cpp index 992cd7ff..407077c8 100644 --- a/dataplane/unittest/sdp.cpp +++ b/dataplane/unittest/sdp.cpp @@ -38,12 +38,12 @@ class TestBus void CompareWithClient(const common::sdp::DataPlaneInSharedMemory& sdp_data_client) { - void* buffer = common::sdp::ShiftBuffer(sdp_data_client.dataplane_data, sdp_data_client.start_bus_section); + void* buffer = utils::ShiftBuffer(sdp_data_client.dataplane_data, sdp_data_client.start_bus_section); auto count_errors = static_cast(common::idp::errorType::size); auto count_requests = static_cast(common::idp::requestType::size); - auto* requests = common::sdp::ShiftBuffer(buffer, 0); - auto* errors = common::sdp::ShiftBuffer(buffer, count_requests * sizeof(uint64_t)); - auto* durations = common::sdp::ShiftBuffer(buffer, (count_requests + count_errors) * sizeof(uint64_t)); + auto* requests = utils::ShiftBuffer(buffer, 0); + auto* errors = utils::ShiftBuffer(buffer, count_requests * sizeof(uint64_t)); + auto* durations = utils::ShiftBuffer(buffer, (count_requests + count_errors) * sizeof(uint64_t)); for (uint32_t index = 0; index < static_cast(common::idp::requestType::size); index++) { @@ -159,11 +159,11 @@ class TestWorker void SetBufferForCounters(void* buffer, const common::sdp::MetadataWorker& metadata) { - counters = common::sdp::ShiftBuffer(buffer, metadata.start_counters); - aclCounters = common::sdp::ShiftBuffer(buffer, metadata.start_acl_counters); - bursts = common::sdp::ShiftBuffer(buffer, metadata.start_bursts); - stats = common::sdp::ShiftBuffer(buffer, metadata.start_stats); - statsPorts = common::sdp::ShiftBuffer(buffer, metadata.start_stats_ports); + counters = utils::ShiftBuffer(buffer, metadata.start_counters); + aclCounters = utils::ShiftBuffer(buffer, metadata.start_acl_counters); + bursts = utils::ShiftBuffer(buffer, metadata.start_bursts); + stats = utils::ShiftBuffer(buffer, metadata.start_stats); + statsPorts = utils::ShiftBuffer(buffer, metadata.start_stats_ports); } void SetTestValues(tCoreId coreId) @@ -207,7 +207,7 @@ class TestWorker ASSERT_EQ(common::sdp::SdpClient::GetCounterByName(sdp_data_client, "dropPackets", coreId)[coreId], stats->dropPackets); // statsPorts - auto* bufStatsPorts = common::sdp::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_stats_ports); + auto* bufStatsPorts = utils::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_stats_ports); for (uint32_t index = 0; index < CONFIG_YADECAP_PORTS_SIZE + 1; index++) { ASSERT_EQ(statsPorts[index].controlPlane_drops, bufStatsPorts[index].controlPlane_drops); @@ -215,21 +215,21 @@ class TestWorker } // bursts - auto* bufBursts = common::sdp::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_bursts); + auto* bufBursts = utils::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_bursts); for (uint32_t index = 0; index < CONFIG_YADECAP_MBUFS_BURST_SIZE + 1; index++) { ASSERT_EQ(bursts[index], bufBursts[index]); } // counters - auto* bufCounters = common::sdp::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_counters); + auto* bufCounters = utils::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_counters); for (uint32_t index = 0; index < YANET_CONFIG_COUNTERS_SIZE; index++) { ASSERT_EQ(counters[index], bufCounters[index]); } // aclCounters - auto* bufAclCounters = common::sdp::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_acl_counters); + auto* bufAclCounters = utils::ShiftBuffer(buffer, sdp_data_client.metadata_worker.start_acl_counters); for (uint32_t index = 0; index < YANET_CONFIG_ACL_COUNTERS_SIZE; index++) { ASSERT_EQ(aclCounters[index], bufAclCounters[index]); @@ -276,8 +276,8 @@ class TestWorkerGc void SetBufferForCounters(void* buffer, const common::sdp::MetadataWorkerGc& metadata) { - counters = common::sdp::ShiftBuffer(buffer, metadata.start_counters); - stats = common::sdp::ShiftBuffer(buffer, metadata.start_stats); + counters = utils::ShiftBuffer(buffer, metadata.start_counters); + stats = utils::ShiftBuffer(buffer, metadata.start_stats); } void SetTestValues(tCoreId coreId) @@ -302,7 +302,7 @@ class TestWorkerGc ASSERT_EQ(common::sdp::SdpClient::GetCounterByName(sdp_data_client, "drop_samples", coreId)[coreId], stats->drop_samples); // counters - auto* bufCounters = common::sdp::ShiftBuffer(buffer, sdp_data_client.metadata_worker_gc.start_counters); + auto* bufCounters = utils::ShiftBuffer(buffer, sdp_data_client.metadata_worker_gc.start_counters); for (uint32_t index = 0; index < YANET_CONFIG_COUNTERS_SIZE; index++) { ASSERT_EQ(counters[index], bufCounters[index]); diff --git a/dataplane/worker.cpp b/dataplane/worker.cpp index 7a1ad8e5..22800ee7 100644 --- a/dataplane/worker.cpp +++ b/dataplane/worker.cpp @@ -21,6 +21,7 @@ #include "common/fallback.h" #include "common/nat46clat.h" +#include "common/utils.h" #include "dataplane/sdpserver.h" #include "action_dispatcher.h" @@ -344,11 +345,11 @@ void cWorker::FillMetadataWorkerCounters(common::sdp::MetadataWorker& metadata) void cWorker::SetBufferForCounters(void* buffer, const common::sdp::MetadataWorker& metadata) { - counters = common::sdp::ShiftBuffer(buffer, metadata.start_counters); - aclCounters = common::sdp::ShiftBuffer(buffer, metadata.start_acl_counters); - bursts = common::sdp::ShiftBuffer(buffer, metadata.start_bursts); - stats = common::sdp::ShiftBuffer(buffer, metadata.start_stats); - statsPorts = common::sdp::ShiftBuffer(buffer, metadata.start_stats_ports); + counters = utils::ShiftBuffer(buffer, metadata.start_counters); + aclCounters = utils::ShiftBuffer(buffer, metadata.start_acl_counters); + bursts = utils::ShiftBuffer(buffer, metadata.start_bursts); + stats = utils::ShiftBuffer(buffer, metadata.start_stats); + statsPorts = utils::ShiftBuffer(buffer, metadata.start_stats_ports); } eResult cWorker::sanityCheck() diff --git a/dataplane/worker_gc.cpp b/dataplane/worker_gc.cpp index 2a645aa4..66c20434 100644 --- a/dataplane/worker_gc.cpp +++ b/dataplane/worker_gc.cpp @@ -179,8 +179,8 @@ void worker_gc_t::FillMetadataWorkerCounters(common::sdp::MetadataWorkerGc& meta void worker_gc_t::SetBufferForCounters(void* buffer, const common::sdp::MetadataWorkerGc& metadata) { - counters = common::sdp::ShiftBuffer(buffer, metadata.start_counters); - stats = common::sdp::ShiftBuffer(buffer, metadata.start_stats); + counters = utils::ShiftBuffer(buffer, metadata.start_counters); + stats = utils::ShiftBuffer(buffer, metadata.start_stats); } YANET_INLINE_NEVER void worker_gc_t::thread()