| 1 |
/* |
| 2 |
* Copyright (c) 2017, Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included |
| 12 |
* in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
//! |
| 23 |
//! \file heap_manager.h |
| 24 |
//! \brief The client facing interface for handling heaps. |
| 25 |
//! |
| 26 |
|
| 27 |
#ifndef __HEAP_MANAGER_H__ |
| 28 |
#define __HEAP_MANAGER_H__ |
| 29 |
|
| 30 |
#include "memory_block_manager.h" |
| 31 |
|
| 32 |
class FrameTrackerProducer; |
| 33 |
|
| 34 |
//! \brief Client accessible manager for heaps. |
| 35 |
class HeapManager |
| 36 |
{ |
| 37 |
|
| 38 |
public: |
| 39 |
//! \brief Expected behavior when space is not available in the heap(s) |
| 40 |
enum Behavior |
| 41 |
{ |
| 42 |
wait = 0, //<! Waits for space to become available. |
| 43 |
extend, //<! Allocates a new heap of the existing size + the default extend heap size. |
| 44 |
destructiveExtend, //<! Performs the same action as extend and marks the old heap for delete. |
| 45 |
waitAndExtend, //<! Waits for a fixed amount of time and then performs an extend if a timeout occurs. |
| 46 |
clientControlled //<! If space is not available, heap manager returns so client can decide what to do |
| 47 |
}; |
| 48 |
|
| 49 |
HeapManager() |
| 50 |
{ |
| 51 |
HEAP_FUNCTION_ENTER; |
| 52 |
} |
| 53 |
virtual ~HeapManager(); |
| 54 |
|
| 55 |
//! |
| 56 |
//! \brief Acquires space within the heap(s) |
| 57 |
//! \param [in] params |
| 58 |
//! Parameters describing the requested space |
| 59 |
//! \param [out] blocks |
| 60 |
//! A vector containing the memory blocks allocated |
| 61 |
//! \param [out] spaceNeeded |
| 62 |
//! Amount of space that the heap(s) are short of to complete space acquisition |
| 63 |
//! \return MOS_STATUS |
| 64 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 65 |
//! |
| 66 |
virtual MOS_STATUS AcquireSpace( |
| 67 |
MemoryBlockManager::AcquireParams ¶ms, |
| 68 |
std::vector<MemoryBlock> &blocks, |
| 69 |
uint32_t &spaceNeeded); |
| 70 |
|
| 71 |
//! |
| 72 |
//! \brief Indicates that the client is done editing the blocks |
| 73 |
//! \param [in] blocks |
| 74 |
//! Blocks to be submitted |
| 75 |
//! \return MOS_STATUS |
| 76 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 77 |
//! |
| 78 |
virtual MOS_STATUS SubmitBlocks(std::vector<MemoryBlock> &blocks) |
| 79 |
{ |
| 80 |
return m_blockManager.SubmitBlocks(blocks); |
| 81 |
} |
| 82 |
|
| 83 |
//! |
| 84 |
//! \brief Makes a block available in the heap. |
| 85 |
//! \details Expected to be used only when the behavior selected in HeapManager is client |
| 86 |
//! controlled--the client wants direct control over what is removed from the heap |
| 87 |
//! (static blocks would be used in this case). |
| 88 |
//! \param [in] block |
| 89 |
//! Block to be removed |
| 90 |
//! \return MOS_STATUS |
| 91 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 92 |
//! |
| 93 |
MOS_STATUS ClearSpace(MemoryBlock &block); |
| 94 |
|
| 95 |
//! |
| 96 |
//! \brief Registers the tracker data to be used for determining whether a |
| 97 |
//! memory block is available. |
| 98 |
//! \param [in] trackerData |
| 99 |
//! Must be valid; pointer to tracker data. \see MemoryBlockManager::m_trackerData. |
| 100 |
//! \return MOS_STATUS |
| 101 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 102 |
//! |
| 103 |
virtual MOS_STATUS RegisterTrackerResource(uint32_t *trackerData); |
| 104 |
|
| 105 |
//! |
| 106 |
//! \brief Registers the tracker producer to be used for determining whether a |
| 107 |
//! memory block is available. This function has a higher priority than |
| 108 |
//! RegisterTrackerResource, so if it is called, the trackerResource will |
| 109 |
//! be useless. |
| 110 |
//! \param [in] trackerProducer |
| 111 |
//! Must be valid; pointer to trackerProducer. |
| 112 |
//! \return MOS_STATUS |
| 113 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 114 |
//! |
| 115 |
MOS_STATUS RegisterTrackerProducer(FrameTrackerProducer *trackerProducer); |
| 116 |
|
| 117 |
//! |
| 118 |
//! \brief Updates the default behavior of the heap manager |
| 119 |
//! \param [in] behavior |
| 120 |
//! Expected behavior of the heap mamanger |
| 121 |
//! |
| 122 |
void SetDefaultBehavior(Behavior behavior) |
| 123 |
{ |
| 124 |
HEAP_FUNCTION_ENTER; |
| 125 |
if (behavior > clientControlled) |
| 126 |
{ |
| 127 |
behavior = wait; |
| 128 |
} |
| 129 |
m_behavior = behavior; |
| 130 |
} |
| 131 |
|
| 132 |
//! |
| 133 |
//! \brief Updates the initial heap size either for first allocation or for extension |
| 134 |
//! \param [in] size |
| 135 |
//! Updates the current heap size, must be non-zero. \see m_currHeapSize |
| 136 |
//! \return MOS_STATUS |
| 137 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 138 |
//! |
| 139 |
virtual MOS_STATUS SetInitialHeapSize(uint32_t size); |
| 140 |
|
| 141 |
//! |
| 142 |
//! \brief Updates the extend heap size when the behavior is not to wait. \see m_behavior \see Behavior::wait |
| 143 |
//! \param [in] size |
| 144 |
//! Updates the extend heap size, must be non-zero. \see m_extendHeapSize |
| 145 |
//! \return MOS_STATUS |
| 146 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 147 |
//! |
| 148 |
MOS_STATUS SetExtendHeapSize(uint32_t size); |
| 149 |
|
| 150 |
//! |
| 151 |
//! \brief Stores the provided OS interface in the heap |
| 152 |
//! \param [in] osInterface |
| 153 |
//! Must be valid |
| 154 |
//! \return MOS_STATUS |
| 155 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 156 |
//! |
| 157 |
virtual MOS_STATUS RegisterOsInterface(PMOS_INTERFACE osInterface); |
| 158 |
|
| 159 |
//! |
| 160 |
//! \brief Indicates the total size of all managed heaps |
| 161 |
//! \return The size of all heaps managed \see m_totalSizeOfHeaps |
| 162 |
//! |
| 163 |
virtual uint32_t GetTotalSize(); |
| 164 |
|
| 165 |
//! |
| 166 |
//! \brief Indicates the size of heap extensions |
| 167 |
//! \return The size by which heaps are extended by \see m_extendHeapSize |
| 168 |
//! |
| 169 |
uint32_t GetExtendSize() |
| 170 |
{ |
| 171 |
HEAP_FUNCTION_ENTER; |
| 172 |
return m_extendHeapSize; |
| 173 |
} |
| 174 |
|
| 175 |
//! |
| 176 |
//! \brief All heaps allocated are locked and kept locked for their lifetimes |
| 177 |
//! \details May only be set before any heaps are allocated. |
| 178 |
//! \return MOS_STATUS |
| 179 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 180 |
//! |
| 181 |
MOS_STATUS LockHeapsOnAllocate() |
| 182 |
{ |
| 183 |
HEAP_FUNCTION_ENTER; |
| 184 |
return m_blockManager.LockHeapsOnAllocate(); |
| 185 |
} |
| 186 |
|
| 187 |
//! |
| 188 |
//! \brief Mark the heap as hardware write only heap or not |
| 189 |
void SetHwWriteOnlyHeap(bool isHwWriteOnlyHeap) { m_hwWriteOnlyHeap = isHwWriteOnlyHeap; } |
| 190 |
|
| 191 |
private: |
| 192 |
//! |
| 193 |
//! \brief Allocates a heap of requested size |
| 194 |
//! \param [in] size |
| 195 |
//! Size of the heap to be allocated |
| 196 |
//! \return MOS_STATUS |
| 197 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 198 |
//! |
| 199 |
MOS_STATUS AllocateHeap(uint32_t size); |
| 200 |
|
| 201 |
//! |
| 202 |
//! \brief Free or attempts to free a heap |
| 203 |
//! |
| 204 |
void FreeHeap(); |
| 205 |
|
| 206 |
//! |
| 207 |
//! \brief Wait for for space to be available in the heap |
| 208 |
//! \return MOS_STATUS |
| 209 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 210 |
//! |
| 211 |
MOS_STATUS Wait(); |
| 212 |
|
| 213 |
//! |
| 214 |
//! \brief If space may not be acquired, behave according to the current specified behavior. |
| 215 |
//! \see m_behavior \see MemoryBlockManager::AcquireSpace |
| 216 |
//! \return MOS_STATUS |
| 217 |
//! MOS_STATUS_SUCCESS if success, else fail reason |
| 218 |
//! |
| 219 |
MOS_STATUS BehaveWhenNoSpace(); |
| 220 |
|
| 221 |
//! \brief Alignment used for the heap size during allocation |
| 222 |
static const uint32_t m_heapAlignment = MOS_PAGE_SIZE; |
| 223 |
//! \brief Timeout in milliseconds for wait, currently fixed |
| 224 |
static const uint32_t m_waitTimeout = 100; |
| 225 |
//! \brief Wait increment in milliseconds, currently fixed |
| 226 |
static const uint32_t m_waitIncrement = 10; |
| 227 |
|
| 228 |
//! \brief Memory block manager for the heap(s) |
| 229 |
MemoryBlockManager m_blockManager; |
| 230 |
//! \brief The default behavior when space is not found in any of the heaps. |
| 231 |
Behavior m_behavior = Behavior::wait; |
| 232 |
//! \brief Current heap ID |
| 233 |
uint32_t m_currHeapId = Heap::m_invalidId; |
| 234 |
//! \brief The current heap size |
| 235 |
uint32_t m_currHeapSize = 0; |
| 236 |
//! \brief The size by which a heap is expected to be extended by when the behavior |
| 237 |
//! is extend. |
| 238 |
uint32_t m_extendHeapSize = 0; |
| 239 |
//! \brief Stores the IDs for the heaps stored in the memory block manager |
| 240 |
std::list<uint32_t> m_heapIds; |
| 241 |
//! \brief OS interface used for managing graphics resources |
| 242 |
PMOS_INTERFACE m_osInterface = nullptr; |
| 243 |
//!< Indictaes that heap is used by hardware write only. |
| 244 |
bool m_hwWriteOnlyHeap = false; |
| 245 |
}; |
| 246 |
|
| 247 |
#endif // __HEAP_MANAGER_H__ |