|
| 1 | +// Copyright 2026 Google LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef THIRD_PARTY_TPU_RAIDEN_TPU_SYNC_TELEMETRY_SHM_SHM_LAYOUT_H_ |
| 16 | +#define THIRD_PARTY_TPU_RAIDEN_TPU_SYNC_TELEMETRY_SHM_SHM_LAYOUT_H_ |
| 17 | + |
| 18 | +#include <atomic> |
| 19 | +#include <cmath> |
| 20 | +#include <cstddef> |
| 21 | +#include <cstdint> |
| 22 | +#include <iterator> |
| 23 | +#include <type_traits> |
| 24 | + |
| 25 | +#include "tpu_sync/telemetry/metrics_backend.h" |
| 26 | + |
| 27 | +namespace tpu_raiden::telemetry { |
| 28 | + |
| 29 | +inline constexpr uint32_t kRaidenShmMagic = 0xABCD1234; |
| 30 | +inline constexpr uint32_t kSupportedVersion = 2; |
| 31 | +inline constexpr size_t kMaxTocEntries = 1024; |
| 32 | +inline constexpr size_t kMaxDataPoolBytes = 64 * 1024; // 64 KB |
| 33 | +inline constexpr size_t kNumHistogramBuckets = |
| 34 | + std::size(kDefaultHistogramBuckets); |
| 35 | + |
| 36 | +enum class TocEntryState : uint32_t { |
| 37 | + kUninitialized = 0, |
| 38 | + kWriting = 1, |
| 39 | + kCommitted = 2, |
| 40 | +}; |
| 41 | + |
| 42 | +// Table of Contents entry descriptor for a single metric stream. |
| 43 | +struct alignas(64) ShmTocEntry { |
| 44 | + char metric_name[64]; |
| 45 | + char encoded_labels[128]; |
| 46 | + MetricType type; |
| 47 | + uint32_t offset; |
| 48 | + uint32_t size; |
| 49 | + std::atomic<uint32_t> entry_state{ |
| 50 | + static_cast<uint32_t>(TocEntryState::kUninitialized)}; |
| 51 | + uint8_t padding[48]{}; |
| 52 | +}; |
| 53 | + |
| 54 | +// 64-byte aligned header for the shared-memory telemetry segment. |
| 55 | +struct alignas(64) ShmTocHeader { |
| 56 | + std::atomic<uint32_t> magic{0}; |
| 57 | + uint32_t version{kSupportedVersion}; |
| 58 | + int64_t pid{0}; |
| 59 | + std::atomic<uint32_t> toc_entry_count{0}; |
| 60 | + uint32_t max_toc_entries{kMaxTocEntries}; |
| 61 | + uint32_t data_pool_offset{0}; |
| 62 | + std::atomic<uint32_t> data_pool_bytes{0}; |
| 63 | + uint32_t chunk_index{0}; |
| 64 | + uint8_t padding[28]{}; |
| 65 | +}; |
| 66 | + |
| 67 | +// Lock-free shared-memory slot for a single histogram metric stream. |
| 68 | +struct alignas(64) ShmHistogramSlot { |
| 69 | + std::atomic<uint64_t> sample_count{0}; |
| 70 | + std::atomic<double> sample_sum{0.0}; |
| 71 | + std::atomic<uint64_t> bucket_counts[kNumHistogramBuckets + 1]{}; |
| 72 | + uint8_t padding[8]{}; |
| 73 | + |
| 74 | + void Observe(double value) { |
| 75 | + if (!std::isfinite(value)) return; |
| 76 | + sample_count.fetch_add(1, std::memory_order_relaxed); |
| 77 | + double current_sum = sample_sum.load(std::memory_order_relaxed); |
| 78 | + while (!sample_sum.compare_exchange_weak(current_sum, current_sum + value, |
| 79 | + std::memory_order_relaxed, |
| 80 | + std::memory_order_relaxed)) { |
| 81 | + } |
| 82 | + for (size_t i = 0; i < kNumHistogramBuckets; ++i) { |
| 83 | + if (value <= kDefaultHistogramBuckets[i]) { |
| 84 | + bucket_counts[i].fetch_add(1, std::memory_order_relaxed); |
| 85 | + } |
| 86 | + } |
| 87 | + bucket_counts[kNumHistogramBuckets].fetch_add(1, std::memory_order_relaxed); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +// Complete 64-byte aligned segment layout containing header and TOC entries. |
| 92 | +struct alignas(64) ShmSegmentLayout { |
| 93 | + ShmTocHeader header; |
| 94 | + ShmTocEntry toc[kMaxTocEntries]; |
| 95 | +}; |
| 96 | + |
| 97 | +constexpr size_t CalculateChunkFileSize(size_t max_toc_entries, |
| 98 | + size_t data_pool_bytes) { |
| 99 | + return sizeof(ShmTocHeader) + (sizeof(ShmTocEntry) * max_toc_entries) + |
| 100 | + data_pool_bytes; |
| 101 | +} |
| 102 | + |
| 103 | +inline constexpr size_t kSegmentTotalFileSize = |
| 104 | + CalculateChunkFileSize(kMaxTocEntries, kMaxDataPoolBytes); |
| 105 | + |
| 106 | +static_assert(std::is_standard_layout_v<ShmTocEntry> && |
| 107 | + std::is_standard_layout_v<ShmTocHeader> && |
| 108 | + std::is_standard_layout_v<ShmHistogramSlot> && |
| 109 | + std::is_standard_layout_v<ShmSegmentLayout>); |
| 110 | + |
| 111 | +static_assert(std::is_trivially_copyable_v<ShmTocEntry> && |
| 112 | + std::is_trivially_copyable_v<ShmTocHeader> && |
| 113 | + std::is_trivially_copyable_v<ShmHistogramSlot> && |
| 114 | + std::is_trivially_copyable_v<ShmSegmentLayout>); |
| 115 | + |
| 116 | +static_assert(alignof(ShmTocHeader) == 64 && sizeof(ShmTocHeader) == 64); |
| 117 | +static_assert(alignof(ShmTocEntry) == 64 && sizeof(ShmTocEntry) == 256); |
| 118 | +static_assert(alignof(ShmHistogramSlot) == 64 && |
| 119 | + sizeof(ShmHistogramSlot) == 192); |
| 120 | +static_assert(alignof(ShmSegmentLayout) == 64 && |
| 121 | + sizeof(ShmSegmentLayout) == 262208); |
| 122 | + |
| 123 | +static_assert(std::atomic<uint64_t>::is_always_lock_free && |
| 124 | + std::atomic<int64_t>::is_always_lock_free && |
| 125 | + std::atomic<double>::is_always_lock_free && |
| 126 | + std::atomic<uint32_t>::is_always_lock_free); |
| 127 | + |
| 128 | +} // namespace tpu_raiden::telemetry |
| 129 | + |
| 130 | +#endif // THIRD_PARTY_TPU_RAIDEN_TPU_SYNC_TELEMETRY_SHM_SHM_LAYOUT_H_ |
0 commit comments