LCOV - code coverage report
Current view: top level - include/hpactor/mem - compaction.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 8 8
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 4 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright 2026 HPActor Contributors
       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                 :             : #pragma once
      16                 :             : 
      17                 :             : #include <hpactor/mem/slab_cache.hpp>
      18                 :             : 
      19                 :             : #include <cstddef>
      20                 :             : #include <cstdint>
      21                 :             : 
      22                 :             : namespace hpactor::mem {
      23                 :             : 
      24                 :             : // Per-slab fragmentation tracking info.
      25                 :             : struct SlabCompactionInfo {
      26                 :             :     uint64_t generation{0};
      27                 :             :     uint32_t total_blocks{0};
      28                 :             :     uint32_t compaction_threshold_blocks{0};
      29                 :             : 
      30                 :             :     // Returns true if this slab should be compacted.
      31                 :             :     // Threshold: compact when live/total ratio drops below 25%.
      32                 :           3 :     bool should_compact(uint32_t live_count) const noexcept {
      33                 :           3 :         if (total_blocks == 0) return false;
      34                 :           3 :         return live_count <= compaction_threshold_blocks;
      35                 :             :     }
      36                 :             : 
      37                 :           1 :     float utilization(uint32_t live_count) const noexcept {
      38                 :           1 :         if (total_blocks == 0) return 0.0f;
      39                 :           1 :         return static_cast<float>(live_count) / static_cast<float>(total_blocks);
      40                 :             :     }
      41                 :             : };
      42                 :             : 
      43                 :             : // Compaction configuration.
      44                 :             : struct CompactionConfig {
      45                 :             :     float fragmentation_budget = 0.05f;
      46                 :             :     float compaction_threshold = 0.25f;
      47                 :             :     uint64_t compaction_interval_ms = 60000;
      48                 :             : };
      49                 :             : 
      50                 :             : // Manages compaction across all slab caches.
      51                 :             : // Tracks fragmentation and triggers compaction when the waste budget
      52                 :             : // exceeds a configurable threshold (default 5%).
      53                 :             : class CompactionManager {
      54                 :             :   public:
      55                 :           3 :     explicit CompactionManager(const CompactionConfig& cfg = {}) : config_(cfg) {}
      56                 :             : 
      57                 :             :     // Check if a slab should be compacted based on utilization.
      58                 :             :     bool should_compact_slab(uint32_t live_blocks, uint32_t total_blocks) const noexcept;
      59                 :             : 
      60                 :             :     // Calculate total fragmentation waste across slabs.
      61                 :             :     // Returns (wasted_bytes, total_bytes).
      62                 :             :     struct WasteReport {
      63                 :             :         size_t wasted_bytes{0};
      64                 :             :         size_t total_bytes{0};
      65                 :             :         float waste_ratio{0.0f};
      66                 :             :     };
      67                 :             :     static WasteReport compute_waste(const SlabCache& cache) noexcept;
      68                 :             : 
      69                 :             :     // Check whether compaction should run now.
      70                 :             :     bool should_compact() const noexcept;
      71                 :             : 
      72                 :             :     // Record that a compaction cycle ran.
      73                 :             :     void record_compaction() noexcept;
      74                 :             : 
      75                 :             :     // Current config
      76                 :           1 :     const CompactionConfig& config() const noexcept { return config_; }
      77                 :             : 
      78                 :             :   private:
      79                 :             :     CompactionConfig config_;
      80                 :             :     int64_t last_compaction_ts_{0};
      81                 :             : };
      82                 :             : 
      83                 :             : } // namespace hpactor::mem
        

Generated by: LCOV version 2.0-1