Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Leonid Baraz | ea0c6dc | 2021-11-02 20:38:05 | [diff] [blame] | 5 | #ifndef COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_ |
| 6 | #define COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_ |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 7 | |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 8 | #include <atomic> |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 9 | #include <cstdint> |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 10 | #include <queue> |
| 11 | #include <utility> |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 12 | |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 13 | #include "base/functional/callback_forward.h" |
Leonid Baraz | 960ea8f | 2022-05-17 01:01:06 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_refptr.h" |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 16 | #include "base/task/sequenced_task_runner.h" |
| 17 | #include "base/thread_annotations.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 18 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 19 | |
| 20 | namespace reporting { |
| 21 | |
| 22 | // Interface to resources management by Storage module. |
| 23 | // Must be implemented by the caller base on the platform limitations. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 24 | // All APIs are non-blocking. The class is thread-safe. |
| 25 | // TODO(b/258822636): The class is no longer interface, rename it and update the |
| 26 | // comments. |
Leonid Baraz | 960ea8f | 2022-05-17 01:01:06 | [diff] [blame] | 27 | class ResourceInterface : public base::RefCountedThreadSafe<ResourceInterface> { |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 28 | public: |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 29 | explicit ResourceInterface(uint64_t total_size); |
| 30 | |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 31 | // Needs to be called before attempting to allocate specified size. |
| 32 | // Returns true if requested amount can be allocated. |
| 33 | // After that the caller can actually allocate it or must call |
| 34 | // |Discard| if decided not to allocate. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 35 | bool Reserve(uint64_t size); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 36 | |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 37 | // Reverts reservation, arranges for callbacks calls as necessary. |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 38 | // Must be called after the specified amount is released. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 39 | void Discard(uint64_t size); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 40 | |
| 41 | // Returns total amount. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 42 | uint64_t GetTotal() const; |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 43 | |
| 44 | // Returns current used amount. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 45 | uint64_t GetUsed() const; |
| 46 | |
| 47 | // Registers a callback to be invoked once there is specified amount |
| 48 | // of resource available (does not reserve it, so once called back |
| 49 | // the respective code must attempt to reserve again, and if unsuccessful, |
| 50 | // may need ot re-register the callback). |
| 51 | // Callbacks will be invoked in the context of the sequenced task runner |
| 52 | // it was registered in. |
| 53 | void RegisterCallback(uint64_t size, base::OnceClosure cb); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 54 | |
| 55 | // Test only: Sets non-default usage limit. |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 56 | void Test_SetTotal(uint64_t test_total); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 57 | |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 58 | private: |
Leonid Baraz | 960ea8f | 2022-05-17 01:01:06 | [diff] [blame] | 59 | friend class base::RefCountedThreadSafe<ResourceInterface>; |
| 60 | |
Leonid Baraz | 0be7196b | 2022-12-13 00:02:52 | [diff] [blame^] | 61 | ~ResourceInterface(); |
| 62 | |
| 63 | // Flushes as many callbacks as possible given the current resource |
| 64 | // availability. Callbacks only signal that resource may be available, |
| 65 | // the resumed task must try to actually reserve it after that. |
| 66 | void FlushCallbacks(); |
| 67 | |
| 68 | uint64_t total_; // Remains constant in prod code, changes only in tests. |
| 69 | std::atomic<uint64_t> used_{0}; |
| 70 | |
| 71 | // Sequenced task runner for callbacks handling (not for calling callbacks!) |
| 72 | const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 73 | SEQUENCE_CHECKER(sequence_checker_); |
| 74 | |
| 75 | // Queue of pairs [size, callback]. |
| 76 | // When `Discard` leaves enough space available (even momentarily), |
| 77 | // calls as many of the callbacks as fit in that size, in the queue order. |
| 78 | // Note that in a meantime reservation may change - the called back code |
| 79 | // must attempt reservation before using it. |
| 80 | std::queue<std::pair<uint64_t, base::OnceClosure>> resource_callbacks_ |
| 81 | GUARDED_BY_CONTEXT(sequence_checker_); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Moveable RAII class used for scoped Reserve-Discard. |
| 85 | // |
| 86 | // Usage: |
| 87 | // { |
Leonid Baraz | 960ea8f | 2022-05-17 01:01:06 | [diff] [blame] | 88 | // ScopedReservation reservation(1024u, options.memory_resource()); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 89 | // if (!reservation.reserved()) { |
| 90 | // // Allocation failed. |
| 91 | // return; |
| 92 | // } |
| 93 | // // Allocation succeeded. |
| 94 | // ... |
| 95 | // } // Automatically discarded. |
| 96 | // |
Leonid Baraz | a206429 | 2022-07-07 00:18:41 | [diff] [blame] | 97 | // Can be handed over to another owner by move-constructor or using HandOver |
| 98 | // method: |
| 99 | // { |
| 100 | // ScopedReservation summary; |
| 101 | // for (const uint64_t size : sizes) { |
| 102 | // ScopedReservation single_reservation(size, resource); |
| 103 | // ... |
| 104 | // summary.HandOver(single_reservation); |
| 105 | // } |
| 106 | // } |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 107 | class ScopedReservation { |
| 108 | public: |
Leonid Baraz | a206429 | 2022-07-07 00:18:41 | [diff] [blame] | 109 | // Zero-size reservation with no resource interface attached. |
| 110 | // reserved() returns false. |
| 111 | ScopedReservation() noexcept; |
| 112 | // Specified reservation, must have resource interface attached. |
Leonid Baraz | af0a319f | 2022-06-15 02:17:47 | [diff] [blame] | 113 | ScopedReservation( |
| 114 | uint64_t size, |
| 115 | scoped_refptr<ResourceInterface> resource_interface) noexcept; |
Leonid Baraz | b455392 | 2022-07-12 23:32:53 | [diff] [blame] | 116 | // New reservation on the same resource interface as |other_reservation|. |
| 117 | ScopedReservation(uint64_t size, |
| 118 | const ScopedReservation& other_reservation) noexcept; |
| 119 | // Move constructor. |
Leonid Baraz | af0a319f | 2022-06-15 02:17:47 | [diff] [blame] | 120 | ScopedReservation(ScopedReservation&& other) noexcept; |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 121 | ScopedReservation(const ScopedReservation& other) = delete; |
Leonid Baraz | af0a319f | 2022-06-15 02:17:47 | [diff] [blame] | 122 | ScopedReservation& operator=(ScopedReservation&& other) = delete; |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 123 | ScopedReservation& operator=(const ScopedReservation& other) = delete; |
| 124 | ~ScopedReservation(); |
| 125 | |
| 126 | bool reserved() const; |
Leonid Baraz | af0a319f | 2022-06-15 02:17:47 | [diff] [blame] | 127 | |
| 128 | // Reduces reservation to |new_size|. |
Santiago Castano Moreno | 150ba5f | 2021-11-12 21:34:52 | [diff] [blame] | 129 | bool Reduce(uint64_t new_size); |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 130 | |
Leonid Baraz | af0a319f | 2022-06-15 02:17:47 | [diff] [blame] | 131 | // Adds |other| to |this| without assigning or releasing any reservation. |
| 132 | // Used for seamless transition from one reservation to another (more generic |
| 133 | // than std::move). Resets |other| to non-reserved state upon return from this |
| 134 | // method. |
| 135 | void HandOver(ScopedReservation& other); |
| 136 | |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 137 | private: |
Leonid Baraz | a206429 | 2022-07-07 00:18:41 | [diff] [blame] | 138 | scoped_refptr<ResourceInterface> resource_interface_; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 139 | absl::optional<uint64_t> size_; |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 140 | }; |
| 141 | |
Leonid Baraz | 3e8af4b | 2020-11-23 19:04:47 | [diff] [blame] | 142 | } // namespace reporting |
| 143 | |
Leonid Baraz | ea0c6dc | 2021-11-02 20:38:05 | [diff] [blame] | 144 | #endif // COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_ |