Debian Codegraph @

media_softlet/agnostic/common/os/mos_gpucontextmgr_next.cpp

raw
1 /*
2 * Copyright (c) 2019-2024, 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 mos_gpucontextmgr_next.cpp
24 //! \brief Container class for the basic gpu context manager
25 //!
26
27 #include "mos_gpucontextmgr_next.h"
28 #include "mos_gpucontext_specific_next.h"
29 #include "mos_graphicsresource_specific_next.h"
30 #include "mos_context_next.h"
31 #include "mos_oca_rtlog_mgr.h"
32
33 GpuContextMgrNext::GpuContextMgrNext(OsContextNext *osContext)
34 {
35 MOS_OS_FUNCTION_ENTER;
36 m_initialized = false;
37
38 if (osContext)
39 {
40 m_osContext = osContext;
41 }
42 else
43 {
44 MOS_OS_ASSERTMESSAGE("Input osContext cannot be nullptr");
45 return;
46 }
47 }
48
49 GpuContextMgrNext::~GpuContextMgrNext()
50 {
51 MOS_OS_FUNCTION_ENTER;
52
53 if (m_gpuContextArrayMutex)
54 {
55 MosUtilities::MosDestroyMutex(m_gpuContextArrayMutex);
56 m_gpuContextArrayMutex = nullptr;
57 }
58
59 if (m_gpuContextDeleteArrayMutex)
60 {
61 MosUtilities::MosDestroyMutex(m_gpuContextDeleteArrayMutex);
62 m_gpuContextDeleteArrayMutex = nullptr;
63 }
64 }
65
66 MOS_STATUS GpuContextMgrNext::Initialize()
67 {
68 MOS_STATUS status = MOS_STATUS_SUCCESS;
69 m_gpuContextArrayMutex = MosUtilities::MosCreateMutex();
70 MOS_OS_CHK_NULL_RETURN(m_gpuContextArrayMutex);
71
72 m_gpuContextDeleteArrayMutex = MosUtilities::MosCreateMutex();
73 MOS_OS_CHK_NULL_RETURN(m_gpuContextDeleteArrayMutex);
74
75 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
76 m_gpuContextMap.clear();
77 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
78
79 m_initialized = true;
80 return status;
81 }
82
83
84 GpuContextMgrNext *GpuContextMgrNext::GetObject(
85 OsContextNext *osContext)
86 {
87 MOS_OS_FUNCTION_ENTER;
88 if (osContext == nullptr)
89 {
90 MOS_OS_ASSERTMESSAGE("Invalid input parameters!");
91 return nullptr;
92 }
93
94 MOS_STATUS status = MOS_STATUS_SUCCESS;
95 GpuContextMgrNext* pGpuContext = MOS_New(GpuContextMgrNext, osContext);
96 if (!pGpuContext)
97 {
98 return nullptr;
99 }
100 status = pGpuContext->Initialize();
101 if (MOS_FAILED(status))
102 {
103 MOS_Delete(pGpuContext);
104 return nullptr;
105 }
106 return pGpuContext;
107 }
108
109 void GpuContextMgrNext::CleanUp()
110 {
111 MOS_OS_FUNCTION_ENTER;
112
113 if (m_initialized)
114 {
115 DestroyAllGpuContexts();
116
117 // Destroy all deferred gpu contexts
118 MosUtilities::MosLockMutex(m_gpuContextDeleteArrayMutex);
119 for (auto *gpuContext : m_deferredDestroyQueue)
120 {
121 MOS_Delete(gpuContext);
122 }
123 m_deferredDestroyQueue.clear();
124 MosUtilities::MosUnlockMutex(m_gpuContextDeleteArrayMutex);
125
126 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
127 m_gpuContextMap.clear();
128 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
129
130 m_initialized = false;
131 }
132
133 return;
134 }
135
136 bool GpuContextMgrNext::ContextReuseNeeded()
137 {
138 MOS_OS_FUNCTION_ENTER;
139
140 // to be added after scalable design is nailed down
141 return false;
142 }
143
144 GpuContextNext *GpuContextMgrNext::SelectContextToReuse()
145 {
146 MOS_OS_FUNCTION_ENTER;
147
148 // to be added after scalable design is nailed down
149 return nullptr;
150 }
151
152 GpuContextNext *GpuContextMgrNext::CreateGpuContext(
153 const MOS_GPU_NODE gpuNode,
154 CmdBufMgrNext * cmdBufMgr)
155 {
156 MOS_OS_FUNCTION_ENTER;
157
158 if (cmdBufMgr == nullptr && !m_osContext->IsAynchronous())
159 {
160 MOS_OS_ASSERTMESSAGE("nullptr of cmdbufmgr in normal mode. nullptr can only be applied in Async mode");
161 return nullptr;
162 }
163
164 GpuContextNext *reusedContext = nullptr;
165 if (ContextReuseNeeded())
166 {
167 reusedContext = SelectContextToReuse();
168 }
169
170 GpuContextNext *gpuContext = GpuContextSpecificNext::Create(gpuNode, cmdBufMgr, reusedContext);
171 if (gpuContext == nullptr)
172 {
173 MOS_OS_ASSERTMESSAGE("nullptr returned by GpuContext::Create.");
174 return nullptr;
175 }
176
177 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
178
179 GPU_CONTEXT_HANDLE gpuContextHandle = 0;
180 bool found = false;
181
182 if (m_noCycledGpuCxtMgmt)
183 {
184 // new created context at the end of m_gpuContextArray.
185 gpuContextHandle = m_gpuContextHanleForNonCycledCase;
186 m_gpuContextHanleForNonCycledCase++;
187 }
188 else
189 {
190 // Directly replace nullptr with new created context in m_gpuContextArray.
191 GpuContextNext *curGpuContext = nullptr;
192 for (auto &igpu : m_gpuContextMap)
193 {
194 if (igpu.second == nullptr)
195 {
196 gpuContextHandle = igpu.first;
197 igpu.second = gpuContext;
198 found = true;
199 break;
200 }
201 }
202 if(!found)
203 {
204 gpuContextHandle = m_gpuContextMap.size();
205 }
206 }
207 gpuContext->SetGpuContextHandle(gpuContextHandle);
208
209
210 m_gpuContextMap[gpuContextHandle] = gpuContext;
211 m_gpuContextCount++;
212
213 MT_LOG5(MT_MOS_GPUCXT_CREATE, MT_NORMAL, MT_MOS_GPUCXT_MGR_PTR, (int64_t)this, MT_MOS_GPUCXT_PTR, (int64_t)gpuContext,
214 MT_MOS_GPUCXT_COUNT, m_gpuContextCount, MT_MOS_GPU_NODE, gpuNode, MT_MOS_GPUCXT_HANDLE, gpuContextHandle);
215
216 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
217
218 return gpuContext;
219 }
220
221 GpuContextNext *GpuContextMgrNext::GetGpuContext(GPU_CONTEXT_HANDLE gpuContextHandle)
222 {
223 MOS_OS_FUNCTION_ENTER;
224
225 if (gpuContextHandle == MOS_GPU_CONTEXT_INVALID_HANDLE)
226 {
227 MOS_OS_ASSERTMESSAGE("Input gpucontext handle cannot be MOS_GPU_CONTEXT_INVALID_HANDLE!");
228 return nullptr;
229 }
230
231 GpuContextNext *gpuContext = nullptr;
232 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
233 auto it = std::find_if(m_gpuContextMap.begin(), m_gpuContextMap.end(), [&](auto &curGpuCtx)
234 { return curGpuCtx.first == gpuContextHandle; });
235 if (m_gpuContextMap.end() != it)
236 {
237 gpuContext = m_gpuContextMap.at(gpuContextHandle);
238 }
239 else
240 {
241 MOS_OS_NORMALMESSAGE("Gpu context may have been deleted already!");
242 MT_LOG2(MT_MOS_GPUCXT_GET, MT_NORMAL, MT_MOS_GPUCXT_MGR_PTR, (int64_t)this, MT_MOS_GPUCXT_HANDLE, gpuContextHandle);
243 gpuContext = nullptr;
244 }
245 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
246 return gpuContext;
247 }
248
249 void GpuContextMgrNext::DestroyGpuContext(GpuContextNext *gpuContext)
250 {
251 MOS_OS_FUNCTION_ENTER;
252 MOS_OS_CHK_NULL_NO_STATUS_RETURN(gpuContext);
253
254 GpuContextNext *curGpuContext = nullptr;
255 bool found = false;
256
257 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
258 auto it = std::find_if(m_gpuContextMap.begin(), m_gpuContextMap.end(), [&](auto &curGpuCtx)
259 { return curGpuCtx.second == gpuContext; });
260 if (m_gpuContextMap.end() != it)
261 {
262 found = true;
263 if (m_noCycledGpuCxtMgmt)
264 {
265 m_gpuContextMap.erase(it);
266 }
267 else
268 {
269 it->second = nullptr;
270 }
271 m_gpuContextCount--;
272 }
273
274 if (m_gpuContextCount == 0 && !m_noCycledGpuCxtMgmt)
275 {
276 m_gpuContextMap.clear(); // clear whole array
277 }
278
279 MT_LOG3(MT_MOS_GPUCXT_DESTROY, MT_NORMAL, MT_MOS_GPUCXT_MGR_PTR, (int64_t)this, MT_MOS_GPUCXT_PTR, (int64_t)gpuContext, MT_MOS_GPUCXT_COUNT, m_gpuContextCount);
280
281 MosStreamState streamState = {};
282 streamState.osDeviceContext = m_osContext;
283 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
284
285 if (found)
286 {
287 MosUtilities::MosLockMutex(m_gpuContextDeleteArrayMutex);
288
289 if (m_osContext && m_osContext->IsDeferredGpuContextDestroySupported())
290 {
291 gpuContext->ClearForDeferredDestroy();
292 m_deferredDestroyQueue.push_back(gpuContext);
293
294 while (m_deferredDestroyQueue.size() > GPU_CONTEXT_DEFERRED_DESTROY_MAX_COUNT)
295 {
296 GpuContextNext *oldest = m_deferredDestroyQueue.front();
297 m_deferredDestroyQueue.erase(m_deferredDestroyQueue.begin());
298 MOS_OS_NORMALMESSAGE("Deferred destroy queue full, destroying oldest gpu context %p (HW queue/context)", oldest);
299 MOS_Delete(oldest);
300 }
301 }
302 else
303 {
304 MOS_Delete(gpuContext); // delete gpu context.
305 }
306
307 MosUtilities::MosUnlockMutex(m_gpuContextDeleteArrayMutex);
308 }
309 else
310 {
311 MOS_OS_ASSERTMESSAGE("cannot find specified gpuContext in the gpucontext pool, something must be wrong");
312 MT_ERR3(MT_MOS_GPUCXT_DESTROY, MT_MOS_GPUCXT_MGR_PTR, (int64_t)this, MT_MOS_GPUCXT_PTR, (int64_t)gpuContext, MT_CODE_LINE, __LINE__);
313 }
314 }
315
316 void GpuContextMgrNext::DestroyAllGpuContexts()
317 {
318 MOS_OS_FUNCTION_ENTER;
319
320 GpuContextNext *curGpuContext = nullptr;
321
322 MosUtilities::MosLockMutex(m_gpuContextArrayMutex);
323
324 // delete each instance in m_gpuContextArray
325 for (auto &curGpuContext : m_gpuContextMap)
326 {
327 MT_LOG2(MT_MOS_GPUCXT_DESTROY, MT_NORMAL, MT_MOS_GPUCXT_MGR_PTR, (int64_t)this, MT_MOS_GPUCXT_PTR, (int64_t)curGpuContext.second);
328 MOS_Delete(curGpuContext.second);
329 }
330
331 m_gpuContextMap.clear(); // clear whole array
332
333 MosUtilities::MosUnlockMutex(m_gpuContextArrayMutex);
334 }