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/types/types.hpp>
18 : :
19 : : #include <cstddef>
20 : : #include <cstdint>
21 : : #include <mutex>
22 : : #include <unordered_map>
23 : :
24 : : namespace hpactor::mem {
25 : :
26 : : // Hibernation buffer: serialized actor state stored in cold memory.
27 : : struct HibernationBuffer {
28 : : void* ptr{nullptr};
29 : : size_t size{0};
30 : : uint64_t hibernated_at_ts{0};
31 : : uint32_t actor_id{0};
32 : : };
33 : :
34 : : // Thread-safe registry mapping ActorId → HibernationBuffer.
35 : : // Stores serialized actor state for hibernated actors.
36 : : class HibernationRegistry {
37 : : public:
38 : : static HibernationRegistry& instance();
39 : :
40 : : // Store a hibernated actor's buffer. Takes ownership of ptr.
41 : : void store(ActorId id, HibernationBuffer buf);
42 : :
43 : : // Retrieve and remove (on reactivation). Returns buffer with nullptr if not found.
44 : : HibernationBuffer load(ActorId id);
45 : :
46 : : // Remove without retrieving (on actor termination while hibernated).
47 : : void remove(ActorId id);
48 : :
49 : : // Check if an actor is hibernated.
50 : : bool contains(ActorId id) const;
51 : :
52 : : // Total number of hibernated actors.
53 : : size_t count() const;
54 : :
55 : : // Total hibernated bytes across all actors.
56 : : size_t total_bytes() const;
57 : :
58 : : private:
59 : 1 : HibernationRegistry() = default;
60 : :
61 : : mutable std::mutex mutex_;
62 : : std::unordered_map<uint64_t, HibernationBuffer> entries_;
63 : : };
64 : :
65 : : } // namespace hpactor::mem
|