1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* Copyright (c) 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//!
//! \file mos_cmdbufmgr.cpp
//! \brief Container class for the basic command buffer manager
//!
#include "mos_cmdbufmgr.h"
#include <algorithm>
CmdBufMgr::CmdBufMgr()
{
MOS_OS_FUNCTION_ENTER;
m_availableCmdBufPool.clear();
m_inUseCmdBufPool.clear();
m_initialized = false;
}
CmdBufMgr::~CmdBufMgr()
{
MOS_OS_FUNCTION_ENTER;
}
CmdBufMgr* CmdBufMgr::GetObject()
{
MOS_OS_FUNCTION_ENTER;
return MOS_New(CmdBufMgr);
}
MOS_STATUS CmdBufMgr::Initialize(OsContext *osContext, uint32_t cmdBufSize)
{
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
MOS_OS_FUNCTION_ENTER;
MOS_OS_CHK_NULL_RETURN(osContext);
if (!m_initialized)
{
m_osContext = osContext;
m_inUsePoolMutex = MOS_CreateMutex();
MOS_OS_CHK_NULL_RETURN(m_inUsePoolMutex);
m_availablePoolMutex = MOS_CreateMutex();
MOS_OS_CHK_NULL_RETURN(m_availablePoolMutex);
for (uint32_t i = 0; i < m_initBufNum; i++)
{
auto cmdBuf = CommandBuffer::CreateCmdBuf();
if (cmdBuf == nullptr)
{
MOS_OS_ASSERTMESSAGE("input nullptr returned by CommandBuffer::CreateCmdBuf.");
return MOS_STATUS_INVALID_HANDLE;
}
eStatus = cmdBuf->Allocate(m_osContext, cmdBufSize);
if (eStatus != MOS_STATUS_SUCCESS)
{
MOS_OS_ASSERTMESSAGE("Allocate CmdBuf#%d failed", i);
return MOS_STATUS_INVALID_HANDLE;
}
MOS_LockMutex(m_availablePoolMutex);
m_availableCmdBufPool.push_back(cmdBuf);
MOS_UnlockMutex(m_availablePoolMutex);
m_cmdBufTotalNum++;
}
m_initialized = true;
}
return MOS_STATUS_SUCCESS;
}
void CmdBufMgr::CleanUp()
{
MOS_OS_FUNCTION_ENTER;
if (m_initialized)
{
CommandBuffer *cmdBuf = nullptr;
MOS_LockMutex(m_availablePoolMutex);
for (auto& cmdBuf : m_availableCmdBufPool)
{
if (cmdBuf != nullptr)
{
cmdBuf->Free();
MOS_Delete(cmdBuf);
}
else
{
MOS_OS_ASSERTMESSAGE("Unexpected, found null command buffer!");
}
}
// clear available command buffer pool
m_availableCmdBufPool.clear();
MOS_UnlockMutex(m_availablePoolMutex);
MOS_LockMutex(m_inUsePoolMutex);
if (!m_inUseCmdBufPool.empty())
{
MOS_OS_ASSERTMESSAGE("Unexpected, inUseCmdBufPool is not empty!");
for (auto& cmdBuf : m_inUseCmdBufPool)
{
if (cmdBuf != nullptr)
{
cmdBuf->Free();
MOS_Delete(cmdBuf);
}
}
}
// clear in-use command buffer pool
m_inUseCmdBufPool.clear();
MOS_UnlockMutex(m_inUsePoolMutex);
m_cmdBufTotalNum = 0;
m_initialized = false;
MOS_DestroyMutex(m_inUsePoolMutex);
m_inUsePoolMutex = nullptr;
MOS_DestroyMutex(m_availablePoolMutex);
m_availablePoolMutex = nullptr;
}
}
CommandBuffer *CmdBufMgr::PickupOneCmdBuf(uint32_t size)
{
MOS_OS_FUNCTION_ENTER;
if (!m_initialized)
{
MOS_OS_ASSERTMESSAGE("cmd buf pool need be initialized before buffer picking up!");
return nullptr;
}
// lock for both in-use and available command buffer pool before pick up
MOS_LockMutex(m_inUsePoolMutex);
MOS_LockMutex(m_availablePoolMutex);
CommandBuffer* cmdBuf = nullptr;
CommandBuffer* retbuf = nullptr;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
if (!m_availableCmdBufPool.empty())
{
cmdBuf = *(m_availableCmdBufPool.begin());
if (cmdBuf == nullptr)
{
MOS_OS_ASSERTMESSAGE("available command buf pool is null.");
MOS_UnlockMutex(m_inUsePoolMutex);
MOS_UnlockMutex(m_availablePoolMutex);
return nullptr;
}
// find available buf
if (size <= cmdBuf->GetCmdBufSize())
{
m_inUseCmdBufPool.push_back(cmdBuf);
m_availableCmdBufPool.erase(m_availableCmdBufPool.begin());
MOS_OS_VERBOSEMESSAGE("successfully get available buf from pool");
}
// available buf is not large enough, need reallocate
else
{
MOS_OS_VERBOSEMESSAGE("find available buf, but is not large enough");
cmdBuf = CommandBuffer::CreateCmdBuf();
if (cmdBuf == nullptr)
{
MOS_OS_ASSERTMESSAGE("input nullptr returned by CommandBuffer::CreateCmdBuf.");
}
else
{
eStatus = cmdBuf->Allocate(m_osContext, size);
if (eStatus != MOS_STATUS_SUCCESS)
{
MOS_OS_ASSERTMESSAGE("Allocate CmdBuf failed");
}
// directly push into inuse pool
m_inUseCmdBufPool.push_back(cmdBuf);
m_cmdBufTotalNum++;
}
}
retbuf = cmdBuf;
}
// no available buf in the pool, will allocate in batch
else
{
MOS_OS_VERBOSEMESSAGE("No more cmd buf in the pool");
if (m_cmdBufTotalNum < m_maxPoolSize)
{
MOS_OS_VERBOSEMESSAGE("Increase the cmd buf pool size by %d", m_bufIncStepSize);
for (uint32_t i = 0; i < m_bufIncStepSize; i++)
{
cmdBuf = CommandBuffer::CreateCmdBuf();
if (cmdBuf == nullptr)
{
MOS_OS_ASSERTMESSAGE("input nullptr returned by CommandBuffer::CreateCmdBuf.");
continue;
}
eStatus = cmdBuf->Allocate(m_osContext, size);
if (eStatus != MOS_STATUS_SUCCESS)
{
MOS_OS_ASSERTMESSAGE("Allocate CmdBuf#%d failed", i);
continue;
}
if (i == 0)
{
// directly push into inuse pool
m_inUseCmdBufPool.push_back(cmdBuf);
retbuf = cmdBuf;
}
else
{
m_availableCmdBufPool.insert(m_availableCmdBufPool.begin(), cmdBuf);
}
m_cmdBufTotalNum++;
}
// sort by decent order
std::sort(m_availableCmdBufPool.begin(), m_availableCmdBufPool.end(), &CmdBufMgr::GreaterSizeSort);
}
else
{
MOS_OS_ASSERTMESSAGE("No availabe cmd buf in pool and the total buf num hit the ceiling, may need wait for a while.");
retbuf = nullptr;
}
}
// unlock after got return buffer
MOS_UnlockMutex(m_inUsePoolMutex);
MOS_UnlockMutex(m_availablePoolMutex);
return retbuf;
}
void CmdBufMgr::UpperInsert(CommandBuffer *cmdBuf)
{
auto it = std::find_if(m_availableCmdBufPool.begin(), m_availableCmdBufPool.end(), [=](CommandBuffer * p1){return p1->GetCmdBufSize() < cmdBuf->GetCmdBufSize();});
m_availableCmdBufPool.emplace(it, cmdBuf);
}
MOS_STATUS CmdBufMgr::ReleaseCmdBuf(CommandBuffer *cmdBuf)
{
MOS_OS_FUNCTION_ENTER;
MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
if (!m_initialized)
{
MOS_OS_ASSERTMESSAGE("cmd buf pool need be initialized before buffer release!");
return MOS_STATUS_NULL_POINTER;
}
MOS_OS_CHK_NULL_RETURN(cmdBuf);
// lock for both in-use and available command buffer pool before release
MOS_LockMutex(m_inUsePoolMutex);
MOS_LockMutex(m_availablePoolMutex);
bool found = false;
for (auto iter = m_inUseCmdBufPool.begin(); iter != m_inUseCmdBufPool.end(); iter++)
{
if (cmdBuf == *iter)
{
found = true;
m_inUseCmdBufPool.erase(iter);
break;
}
}
if (!found)
{
MOS_OS_ASSERTMESSAGE("Cannot find the specified cmdbuf in inusepool, sth must be wrong!");
eStatus = MOS_STATUS_UNKNOWN;
}
else
{
UpperInsert(cmdBuf);
}
// unlock after release buffer
MOS_UnlockMutex(m_inUsePoolMutex);
MOS_UnlockMutex(m_availablePoolMutex);
return eStatus;
}
MOS_STATUS CmdBufMgr::ResizeOneCmdBuf(CommandBuffer *cmdBufToResize, uint32_t newSize)
{
MOS_OS_FUNCTION_ENTER;
MOS_OS_CHK_NULL_RETURN(cmdBufToResize);
if (!m_initialized)
{
MOS_OS_ASSERTMESSAGE("cmd buf pool need be initialized before buffer resize!");
return MOS_STATUS_UNKNOWN;
}
return cmdBufToResize->ReSize(newSize);
}
|