1 /*
2 * Copyright (c) 2020, 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 //!
24 //! \file     media_debug_config_manager.cpp
25 //! \brief    Defines the dump configuration manager.
26 //! \details  The debug interface dumps configuration manager file which parse attributes.
27 //!
28 
29 #include "media_debug_interface.h"
30 #if USE_MEDIA_DEBUG_TOOL
31 #include "media_debug_config_manager.h"
32 #include <fstream>
33 #include <sstream>
34 
MediaDebugConfigMgr(std::string outputFolderPath)35 MediaDebugConfigMgr::MediaDebugConfigMgr(
36     std::string outputFolderPath)
37     : m_outputFolderPath(outputFolderPath)
38 {
39 }
40 
~MediaDebugConfigMgr()41 MediaDebugConfigMgr::~MediaDebugConfigMgr()
42 {
43     if (m_debugAllConfigs != nullptr)
44     {
45         MOS_Delete(m_debugAllConfigs);
46     }
47 }
48 
ParseConfig(MOS_CONTEXT_HANDLE mosCtx)49 MOS_STATUS MediaDebugConfigMgr::ParseConfig(MOS_CONTEXT_HANDLE mosCtx)
50 {
51     std::string               configFileName;
52     bool                      isGenCfgEnabled   = false;
53 
54     configFileName    = InitFileName(m_mediaFunction);
55     configFileName    = m_outputFolderPath + configFileName;
56     std::ifstream configStream(configFileName);
57 
58     if (!configStream.good())
59     {
60         ReadUserSettingForDebug(
61             GetUserSettingInstance(),
62             isGenCfgEnabled,
63             __MEDIA_USER_FEATURE_VALUE_MEDIA_DEBUG_CFG_GENERATION,
64             MediaUserSetting::Group::Device);
65         if (isGenCfgEnabled)
66         {
67             GenerateDefaultConfig(configFileName);
68         }
69         return MOS_STATUS_SUCCESS;
70     }
71 
72     std::string line;
73 
74     bool    parseEnable = false;
75     int32_t minFrameNum = -1;
76     int32_t maxFrameNum = -1;
77 
78     while (std::getline(configStream, line))
79     {
80         if (line.empty())
81         {
82             continue;
83         }
84         auto        pos     = line.find_first_not_of("\t ");
85         std::string newline = line.substr(pos);
86         if (newline[0] == '\n' || newline[0] == '#')
87         {
88             continue;
89         }
90         else if (newline[0] == '@')  // parse keyword
91         {
92             if (!strncmp(newline.substr(1, 4).c_str(), "mode", 4))
93             {
94                 auto pos1 = newline.find_first_not_of("\t ", 5);
95                 if (pos1 == std::string::npos)
96                 {
97                     continue;
98                 }
99                 if ((!strncmp(newline.substr(pos1, 3).c_str(), "ALL", 3)) ||
100                     (!strncmp(newline.substr(pos1, 6).c_str(), "encode", 6) &&
101                         m_mediaFunction == MEDIA_FUNCTION_ENCODE) ||
102                     (!strncmp(newline.substr(pos1, 6).c_str(), "decode", 6) &&
103                         (m_mediaFunction == MEDIA_FUNCTION_DECODE ||
104                          m_mediaFunction == MEDIA_FUNCTION_CENC_DECODE)) ||
105                     (!strncmp(newline.substr(pos1, 2).c_str(), "vp", 2) &&
106                         (m_mediaFunction == MEDIA_FUNCTION_VP)))
107                 {
108                     parseEnable = true;
109                 }
110                 else
111                 {
112                     parseEnable = false;
113                 }
114                 continue;
115             }
116 
117             if (!parseEnable)
118             {
119                 continue;
120             }
121 
122             if (!strncmp(newline.substr(1, 5).c_str(), "Frame", 5))
123             {
124                 // start with new frame config
125                 auto pos1 = newline.find_first_not_of("\t ", 6);
126                 if (pos1 == std::string::npos)
127                     continue;
128 
129                 minFrameNum = maxFrameNum = -1;
130                 if (!strncmp(newline.substr(pos1, 3).c_str(), "ALL", 3))
131                 {
132                     // coonfig applid to all frames
133                     if (nullptr == m_debugAllConfigs)
134                     {
135                         m_debugAllConfigs = MOS_New(MediaDbgCfg);
136                     }
137                 }
138                 else
139                 {
140                     auto pos2 = newline.find_first_of("-", 6);
141                     if (pos2 != std::string::npos)  // handle frame range
142                     {
143                         minFrameNum = std::stoi(newline.substr(pos1, pos2 - pos1));
144                         maxFrameNum = std::stoi(newline.substr(pos2 + 1));
145                     }
146                     else
147                     {
148                         minFrameNum = maxFrameNum = std::stoi(newline.substr(pos1));
149                     }
150                 }
151             }
152             else if (!strncmp(newline.substr(1, 5).c_str(), "Force", 5))
153             {
154             }
155             else if (!strncmp(newline.substr(1, 14).c_str(), "OverwriteCurbe", 14))
156             {
157             }
158             else
159             {
160                 if (minFrameNum < 0)
161                 {
162                     ParseKernelAttribs(newline, nullptr);
163                 }
164                 else
165                 {
166                     for (int32_t i = minFrameNum; i <= maxFrameNum; i++)
167                     {
168                         ParseKernelAttribs(newline, &m_debugFrameConfigs.at(GetFrameConfig(i)));
169                     }
170                 }
171             }
172         }
173         else if (parseEnable)
174         {
175             if (minFrameNum < 0)
176             {
177                 StoreDebugAttribs(newline, nullptr);
178             }
179             else
180             {
181                 for (int32_t i = minFrameNum; i <= maxFrameNum; i++)
182                 {
183                     StoreDebugAttribs(newline, &m_debugFrameConfigs.at(GetFrameConfig(i)));
184                 }
185             }
186         }
187     }
188 
189     return MOS_STATUS_SUCCESS;
190 }
191 
GetFrameConfig(uint32_t frameIdx)192 uint32_t MediaDebugConfigMgr::GetFrameConfig(uint32_t frameIdx)
193 {
194     uint32_t pos;
195     for (pos = 0; pos < m_debugFrameConfigs.size(); pos++)
196     {
197         if (m_debugFrameConfigs[pos].frameIndex == frameIdx)
198         {
199             return pos;
200         }
201     }
202     MediaDbgCfg frameConfig;
203     frameConfig.frameIndex = frameIdx;
204     m_debugFrameConfigs.push_back(frameConfig);
205     return pos;
206 }
207 
StoreDebugAttribs(std::string line,MediaDbgCfg * dbgCfg)208 void MediaDebugConfigMgr::StoreDebugAttribs(std::string line, MediaDbgCfg *dbgCfg)
209 {
210     MediaDbgCfg *config = dbgCfg != nullptr ? dbgCfg : m_debugAllConfigs;
211     if (config == nullptr)
212     {
213         return;
214     }
215 
216     auto delimeterPos = line.find(':');
217     if (delimeterPos == std::string::npos)
218     {
219         return;
220     }
221 
222     auto        attrEndPos = line.substr(0, delimeterPos).find_last_not_of("\t ");
223     std::string attrName   = line.substr(0, attrEndPos + 1);
224     int32_t     attrValue  = std::stoi(line.substr(delimeterPos + 1));
225 
226     auto it = config->cmdAttribs.find(attrName);
227     if (it != config->cmdAttribs.end())
228     {
229         config->cmdAttribs.erase(it);
230     }
231 
232     config->cmdAttribs[attrName] = attrValue;
233 }
234 
ParseKernelAttribs(std::string line,MediaDbgCfg * dbgCfg)235 void MediaDebugConfigMgr::ParseKernelAttribs(std::string line, MediaDbgCfg *dbgCfg)
236 {
237     MediaDbgCfg *config = dbgCfg != nullptr ? dbgCfg : m_debugAllConfigs;
238     if (config == nullptr)
239     {
240         return;
241     }
242 
243     auto        kernelNameEndPos = line.find_first_of("\t ", 1);
244     std::string kernelName       = line.substr(1, kernelNameEndPos - 1);
245 
246     std::istringstream iss(line.substr(kernelNameEndPos));
247     do
248     {
249         std::string attrName;
250         iss >> attrName;
251         if (attrName.empty())
252         {
253             continue;
254         }
255 
256         if (!strncmp(attrName.c_str(), "ALL", 3))
257         {
258             config->kernelAttribs[kernelName].dumpDsh         = true;
259             config->kernelAttribs[kernelName].dumpSsh         = true;
260             config->kernelAttribs[kernelName].dumpIsh         = true;
261             config->kernelAttribs[kernelName].dumpCurbe       = true;
262             config->kernelAttribs[kernelName].dumpCmdBuffer   = true;
263             config->kernelAttribs[kernelName].dump2ndLvlBatch = true;
264             config->kernelAttribs[kernelName].dumpInput       = true;
265             config->kernelAttribs[kernelName].dumpOutput      = true;
266             break;
267         }
268         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrDsh, sizeof(MediaDbgAttr::attrDsh) - 1))
269         {
270             config->kernelAttribs[kernelName].dumpDsh = true;
271         }
272         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrSsh, sizeof(MediaDbgAttr::attrSsh) - 1))
273         {
274             config->kernelAttribs[kernelName].dumpSsh = true;
275         }
276         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrIsh, sizeof(MediaDbgAttr::attrIsh) - 1))
277         {
278             config->kernelAttribs[kernelName].dumpIsh = true;
279         }
280         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrCurbe, sizeof(MediaDbgAttr::attrCurbe) - 1))
281         {
282             config->kernelAttribs[kernelName].dumpCurbe = true;
283         }
284         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrCmdBuffer, sizeof(MediaDbgAttr::attrCmdBuffer) - 1))
285         {
286             config->kernelAttribs[kernelName].dumpCmdBuffer = true;
287         }
288         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attr2ndLvlBatch, sizeof(MediaDbgAttr::attr2ndLvlBatch) - 1))
289         {
290             config->kernelAttribs[kernelName].dump2ndLvlBatch = true;
291         }
292         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrInput, sizeof(MediaDbgAttr::attrInput) - 1))
293         {
294             config->kernelAttribs[kernelName].dumpInput = true;
295         }
296         else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrOutput, sizeof(MediaDbgAttr::attrOutput) - 1))
297         {
298             config->kernelAttribs[kernelName].dumpOutput = true;
299         }
300 
301     } while (iss);
302 }
303 
DeleteCfgNode(uint32_t frameIdx)304 MOS_STATUS MediaDebugConfigMgr::DeleteCfgNode(uint32_t frameIdx)
305 {
306     for (auto it = m_debugFrameConfigs.begin(); it != m_debugFrameConfigs.end(); it++)
307     {
308         if (it->frameIndex == frameIdx)
309         {
310             m_debugFrameConfigs.erase(it);
311             break;
312         }
313     }
314     return MOS_STATUS_SUCCESS;
315 }
316 
AttrIsEnabled(std::string attrName)317 bool MediaDebugConfigMgr::AttrIsEnabled(std::string attrName)
318 {
319     if (nullptr != m_debugAllConfigs)
320     {
321         int attrValue = m_debugAllConfigs->cmdAttribs[attrName];
322         if (attrValue > 0)
323         {
324             return true;
325         }
326     }
327 
328     for (auto it : m_debugFrameConfigs)
329     {
330         if (it.frameIndex == GetDumpFrameNum())
331         {
332             int attrValue = it.cmdAttribs[attrName];
333             return attrValue > 0;
334         }
335     }
336 
337     return false;
338 }
339 
KernelAttrEnabled(MediaKernelDumpConfig kernelConfig,std::string attrName)340 bool MediaDebugConfigMgr::KernelAttrEnabled(MediaKernelDumpConfig kernelConfig, std::string attrName)
341 {
342     if (!strncmp(attrName.c_str(), MediaDbgAttr::attrDsh, sizeof(MediaDbgAttr::attrDsh) - 1))
343     {
344         return kernelConfig.dumpDsh;
345     }
346     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrSsh, sizeof(MediaDbgAttr::attrSsh) - 1))
347     {
348         return kernelConfig.dumpSsh;
349     }
350     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrIsh, sizeof(MediaDbgAttr::attrIsh) - 1))
351     {
352         return kernelConfig.dumpIsh;
353     }
354     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrCurbe, sizeof(MediaDbgAttr::attrCurbe) - 1))
355     {
356         return kernelConfig.dumpCurbe;
357     }
358     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrCmdBuffer, sizeof(MediaDbgAttr::attrCmdBuffer) - 1))
359     {
360         return kernelConfig.dumpCmdBuffer;
361     }
362     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attr2ndLvlBatch, sizeof(MediaDbgAttr::attr2ndLvlBatch) - 1))
363     {
364         return kernelConfig.dump2ndLvlBatch;
365     }
366     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrInput, sizeof(MediaDbgAttr::attrInput) - 1))
367     {
368         return kernelConfig.dumpInput;
369     }
370     else if (!strncmp(attrName.c_str(), MediaDbgAttr::attrOutput, sizeof(MediaDbgAttr::attrOutput) - 1))
371     {
372         return kernelConfig.dumpOutput;
373     }
374     else
375         return false;
376 }
377 
GenerateDefaultConfig(std::string configFileName)378 void MediaDebugConfigMgr::GenerateDefaultConfig(std::string configFileName)
379 {
380     std::ofstream ofs(configFileName);
381 
382     ofs << "###################################################################" << std::endl;
383     ofs << "## - White space should be agnostic." << std::endl;
384     ofs << "## - '#' as first non-white space char denotes line as comment" << std::endl;
385     ofs << "## - '@frame ALL' MUST be present as the first directive, even if " << std::endl;
386     ofs << "##   nothing is dumped for every frame." << std::endl;
387     ofs << "###################################################################" << std::endl;
388     ofs << std::endl;
389 
390     ofs << "###################################################################" << std::endl;
391     ofs << "## key words defined under @mode ALL works for encode decode vp" << std::endl;
392     ofs << "###################################################################" << std::endl;
393     ofs << std::endl;
394     ofs << "@mode ALL" << std::endl;
395     ofs << "@Frame ALL" << std::endl;
396     ofs << std::endl;
397 
398     ofs << "#" << MediaDbgAttr::attrPicParams << ":0" << std::endl;
399     ofs << "#" << MediaDbgAttr::attrSlcParams << ":0" << std::endl;
400     ofs << "#" << MediaDbgAttr::attrSubsetsParams << ":0" << std::endl;
401     ofs << "#" << MediaDbgAttr::attrIqParams << ":0" << std::endl;
402     ofs << "#" << MediaDbgAttr::attrDecodeBitstream << ":0" << std::endl;
403     ofs << "#" << MediaDbgAttr::attrBitstream << ":0" << std::endl;
404     ofs << "#" << MediaDbgAttr::attrHucRegions << ":0" << std::endl;
405     ofs << "#" << MediaDbgAttr::attrHuCDmem << ":0" << std::endl;
406     ofs << "#" << MediaDbgAttr::attrCmdBufferMfx << ":0" << std::endl;
407     ofs << "#" << MediaDbgAttr::attr2ndLvlBatchMfx << ":0" << std::endl;
408     ofs << "#" << MediaDbgAttr::attrSurfaceInfo << ":0" << std::endl;
409     ofs << "#" << MediaDbgAttr::attrHuffmanTbl << ":0" << std::endl;
410     ofs << "#" << MediaDbgAttr::attrScanParams << ":0" << std::endl;
411     ofs << "#" << MediaDbgAttr::attrDriverUltDump << ":0" << std::endl;
412     ofs << "#" << MediaDbgAttr::attrDumpBufferInBinary << ":0" << std::endl;
413     ofs << "#" << MediaDbgAttr::attrDumpToThreadFolder << ":0" << std::endl;
414     ofs << "#" << MediaDbgAttr::attrDumpCmdBufInBinary << ":0" << std::endl;
415     ofs << "#" << MediaDbgAttr::attrEnableFastDump << ":1" << std::endl;
416     ofs << "#" << MediaDbgAttr::attrStatusReport << ":0" << std::endl;
417     ofs << std::endl;
418 
419     ofs << "##" << MediaDbgAttr::attrStreamOut << ":0" << std::endl;
420     ofs << "##" << MediaDbgAttr::attrStreamIn << ":0" << std::endl;
421     ofs << "##" << MediaDbgAttr::attrResidualDifference << ":0" << std::endl;
422     ofs << "##" << MediaDbgAttr::attrDeblocking << ":0" << std::endl;
423     ofs << "##" << MediaDbgAttr::attrMvData << ":0" << std::endl;
424     ofs << "##" << MediaDbgAttr::attrForceYUVDumpWithMemcpy << ":0" << std::endl;
425     ofs << "##" << MediaDbgAttr::attrDisableSwizzleForDumps << ":0" << std::endl;
426     ofs << "##" << MediaDbgAttr::attrSfcOutputSurface << ":0" << std::endl;
427     ofs << "##" << MediaDbgAttr::attrSfcBuffers << ":0" << std::endl;
428     ofs << "##" << MediaDbgAttr::attrDecodeReferenceSurfaces << ":0" << std::endl;
429     ofs << "##" << MediaDbgAttr::attrReferenceSurfaces << ":0" << std::endl;
430     ofs << "##" << MediaDbgAttr::attrEncodeRawInputSurface << ":0" << std::endl;
431     ofs << "##" << MediaDbgAttr::attrReconstructedSurface << ":0" << std::endl;
432     ofs << "##" << MediaDbgAttr::attrPakInput << ":0" << std::endl;
433     ofs << "##" << MediaDbgAttr::attrPakOutput << ":0" << std::endl;
434     ofs << "##" << MediaDbgAttr::attrUpsamlingInput << ":0" << std::endl;
435     ofs << "##" << MediaDbgAttr::attrResidualSurface << ":0" << std::endl;
436     ofs << "##" << MediaDbgAttr::attrStCoeff << ":0" << std::endl;
437     ofs << "##" << MediaDbgAttr::attrCoeffPredCs << ":0" << std::endl;
438     ofs << "##" << MediaDbgAttr::attrMbRecord << ":0" << std::endl;
439     ofs << "##" << MediaDbgAttr::attrPakObjStreamout << ":0" << std::endl;
440     ofs << "##" << MediaDbgAttr::attrTileBasedStats << ":0" << std::endl;
441     ofs << "##" << MediaDbgAttr::attrOverwriteCommands << ":0" << std::endl;
442     ofs << "##" << MediaDbgAttr::attrForceCmdDumpLvl << ":0" << std::endl;
443     ofs << "##" << MediaDbgAttr::attrForceCurbeDumpLvl << ":0" << std::endl;
444     ofs << "##" << MediaDbgAttr::attrFrameState << ":0" << std::endl;
445     ofs << "##" << MediaDbgAttr::attrBrcPakStats << ":0" << std::endl;
446     ofs << "##" << MediaDbgAttr::attrCUStreamout << ":0" << std::endl;
447     ofs << "##" << MediaDbgAttr::attrImageState << ":0" << std::endl;
448     ofs << "##" << MediaDbgAttr::attrSliceSizeStreamout << ":0" << std::endl;
449     ofs << "##" << MediaDbgAttr::attrCoeffProb << ":0" << std::endl;
450     ofs << "##" << MediaDbgAttr::attrROISurface << ":0" << std::endl;
451     ofs << "##" << MediaDbgAttr::attrHuCStitchDataBuf << ":0" << std::endl;
452 
453     // MD5 attributes
454     ofs << "##" << MediaDbgAttr::attrMD5HashEnable << ":0" << std::endl;
455     ofs << "##" << MediaDbgAttr::attrMD5FlushInterval << ":0" << std::endl;
456     ofs << "##" << MediaDbgAttr::attrMD5PicWidth << ":0" << std::endl;
457     ofs << "##" << MediaDbgAttr::attrMD5PicHeight << ":0" << std::endl;
458     ofs << std::endl;
459 
460     ofs << "###################################################################" << std::endl;
461     ofs << "## key words defined under @mode decode works for decode only" << std::endl;
462     ofs << "###################################################################" << std::endl;
463     ofs << std::endl;
464     ofs << "#@mode decode" << std::endl;
465     ofs << "#@Frame ALL" << std::endl;
466     ofs << std::endl;
467 
468     ofs << "#" << MediaDbgAttr::attrSegmentParams << ":0" << std::endl;
469     ofs << "#" << MediaDbgAttr::attrMbParams << ":0" << std::endl;
470     ofs << "#" << MediaDbgAttr::attrVc1Bitplane << ":0" << std::endl;
471     ofs << "#" << MediaDbgAttr::attrCoefProb << ":0" << std::endl;
472     ofs << "#" << MediaDbgAttr::attrSegId << ":0" << std::endl;
473     ofs << "#" << MediaDbgAttr::attrDecodeOutputSurface << ":0" << std::endl;
474     ofs << "#" << MediaDbgAttr::attrDecodeAuxSurface << ":0" << std::endl;
475     ofs << "#" << MediaDbgAttr::attrDecodeBltOutput << ":0" << std::endl;
476     ofs << "#" << MediaDbgAttr::attrDecodeProcParams << ":0" << std::endl;
477     ofs << std::endl;
478 
479     ofs << "###############################################################" << std::endl;
480     ofs << "## key words defined under @mode encode works for encode only" << std::endl;
481     ofs << "###############################################################" << std::endl;
482     ofs << std::endl;
483     ofs << "#@mode encode" << std::endl;
484     ofs << "#@Frame ALL" << std::endl;
485     ofs << std::endl;
486 
487     ofs << "#" << MediaDbgAttr::attrFeiPicParams << ":0" << std::endl;
488     ofs << "#" << MediaDbgAttr::attrSeqParams << ":0" << std::endl;
489     ofs << "#" << MediaDbgAttr::attrVuiParams << ":0" << std::endl;
490     ofs << "#" << MediaDbgAttr::attrDumpEncodePar << ":0" << std::endl;
491     ofs << "#" << MediaDbgAttr::attrVdencOutput << ":0" << std::endl;
492     ofs << std::endl;
493 
494     ofs << "### Encode plug-in ###" << std::endl;
495     ofs << "#@force" << std::endl;
496     ofs << "#ForceCmpDumpLvl:3" << std::endl;
497     ofs << "#@OverwriteCurbe" << std::endl;
498     ofs << "#ForceCurbeDumpLvl:3" << std::endl;
499 
500     ofs.close();
501 }
502 
503 #endif  // USE_MEDIA_DEBUG_TOOL
504