1 /* 2 * Copyright (c) 2021-2023, 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 media_perf_profiler.h 24 //! \brief Defines data structures and interfaces for media performance profiler. 25 26 #ifndef __MEDIA_PERF_PROFILER_H__ 27 #define __MEDIA_PERF_PROFILER_H__ 28 29 #include <map> 30 #include <unordered_map> 31 #include <stdint.h> 32 #include <memory> 33 #include "mos_defs.h" 34 #include "mos_os.h" 35 #include "media_class_trace.h" 36 #include "mhw_mi_itf.h" 37 #include "igfxfmid.h" 38 #include "mos_defs_specific.h" 39 #include "mos_os_specific.h" 40 namespace mhw 41 { 42 namespace mi 43 { 44 class Itf; 45 } 46 } // namespace mhw 47 48 using Map = std::map<void*, uint32_t>; 49 50 /*! \brief In order to align GPU node value for all of OS, 51 * we redifine the GPU node value here. 52 */ 53 typedef enum _PerfGPUNode 54 { 55 PERF_GPU_NODE_3D = 0, 56 PERF_GPU_NODE_VIDEO = 1, 57 PERF_GPU_NODE_BLT = 2, 58 PERF_GPU_NODE_VE = 3, 59 PERF_GPU_NODE_VIDEO2 = 4, 60 PERF_GPU_NODE_TEE = 6, 61 PERF_GPU_NODE_UNKNOW = 0xFF 62 }PerfGPUNode; 63 64 typedef enum _UMD_QUALITY_METRIC_ITEM 65 { 66 UMD_QUALITY_ITEM_SSEY = 0, 67 UMD_QUALITY_ITEM_SSEU, 68 UMD_QUALITY_ITEM_SSEV, 69 UMD_QUALITY_ITEM_MEAN_SSIM_YU, 70 UMD_QUALITY_ITEM_MEAN_SSIM_V 71 } UMD_QUALITY_METRIC_ITEM; 72 73 class MediaPerfProfiler 74 { 75 public: 76 //! 77 //! \brief Get the instance of the profiler 78 //! 79 //! \return pointer of profiler 80 //! 81 static MediaPerfProfiler *Instance(); 82 83 //! 84 //! \brief Destroy the resurces of profiler 85 //! 86 //! \param [in] profiler 87 //! Pointer of profiler 88 //! \param [in] context 89 //! Pointer of Codechal/VPHal 90 //! \param [in] osInterface 91 //! Pointer of OS interface 92 //! 93 //! \return void 94 //! 95 static void Destroy(MediaPerfProfiler* profiler, void* context, MOS_INTERFACE *osInterface); 96 97 //! 98 //! \brief Initialize profiler and allcoate resources 99 //! 100 //! \param [in] context 101 //! Pointer of Codechal/VPHal 102 //! \param [in] osInterface 103 //! Pointer of OS interface 104 //! 105 //! \return MOS_STATUS 106 //! MOS_STATUS_SUCCESS if success, else fail reason 107 //! 108 virtual MOS_STATUS Initialize(void* context, MOS_INTERFACE *osInterface); 109 110 //! 111 //! \brief Insert start command of storing performance data 112 //! 113 //! \param [in] context 114 //! Pointer of Codechal/VPHal 115 //! \param [in] osInterface 116 //! Pointer of OS interface 117 //! \param [in] miItf 118 //! Reference to Mhw MiItf. 119 //! \param [in] cmdBuffer 120 //! Pointer of OS command buffer 121 //! 122 //! \return MOS_STATUS 123 //! MOS_STATUS_SUCCESS if success, else fail reason 124 //! 125 virtual MOS_STATUS AddPerfCollectStartCmd( 126 void* context, 127 MOS_INTERFACE *osInterface, 128 std::shared_ptr<mhw::mi::Itf> miItf, 129 MOS_COMMAND_BUFFER *cmdBuffer); 130 131 //! 132 //! \brief Insert end command of storing performance data 133 //! 134 //! \param [in] context 135 //! Pointer of Codechal/VPHal 136 //! \param [in] osInterface 137 //! Pointer of OS interface 138 //! \param [in] miItf 139 //! Reference to Mhw MiItf. 140 //! \param [in] cmdBuffer 141 //! Pointer of OS command buffer 142 //! 143 //! \return MOS_STATUS 144 //! MOS_STATUS_SUCCESS if success, else fail reason 145 //! 146 virtual MOS_STATUS AddPerfCollectEndCmd( 147 void* context, 148 MOS_INTERFACE *osInterface, 149 std::shared_ptr<mhw::mi::Itf> miItf, 150 MOS_COMMAND_BUFFER *cmdBuffer); 151 152 //! 153 //! \brief Deconstructor 154 //! 155 virtual ~MediaPerfProfiler(); 156 157 private: 158 159 //! 160 //! \brief Constructor 161 //! 162 MediaPerfProfiler(); 163 164 //! 165 //! \brief Save data to the buffer which store the performance data 166 //! 167 //! \param [in] miItf 168 //! Reference to Mhw MiItf. 169 //! \param [in] cmdBuffer 170 //! Pointer of OS command buffer 171 //! \param [in] pOsContext 172 //! Pointer of DEVICE CONTEXT 173 //! \param [in] offset 174 //! Offset in the buffer 175 //! \param [in] value 176 //! Value of data 177 //! 178 //! \return MOS_STATUS 179 //! MOS_STATUS_SUCCESS if success, else fail reason 180 //! 181 MOS_STATUS StoreData( 182 std::shared_ptr<mhw::mi::Itf> miItf, 183 PMOS_COMMAND_BUFFER cmdBuffer, 184 MOS_CONTEXT_HANDLE pOsContext, 185 uint32_t offset, 186 uint32_t value); 187 188 //! 189 //! \brief Save register value to the buffer which store the performance data 190 //! \param [in] osInterface 191 //! Pointer of MOS_INTERFACE 192 //! 193 //! \param [in] miItf 194 //! Reference to Mhw MiItf. 195 //! \param [in] cmdBuffer 196 //! Pointer of OS command buffer 197 //! \param [in] offset 198 //! Offset in the buffer 199 //! \param [in] reg 200 //! Address of register 201 //! 202 //! \return MOS_STATUS 203 //! MOS_STATUS_SUCCESS if success, else fail reason 204 //! 205 MOS_STATUS StoreRegister( 206 MOS_INTERFACE *osInterface, 207 std::shared_ptr<mhw::mi::Itf> miItf, 208 PMOS_COMMAND_BUFFER cmdBuffer, 209 uint32_t offset, 210 uint32_t reg); 211 212 //! 213 //! \brief Save timestamp to the buffer by Pipe control command 214 //! 215 //! \param [in] miItf 216 //! Reference to Mhw MiItf. 217 //! \param [in] cmdBuffer 218 //! Pointer of OS command buffer 219 //! \param [in] pOsContext 220 //! Pointer of DEVICE CONTEXT 221 //! \param [in] offset 222 //! Offset in the buffer 223 //! 224 //! \return MOS_STATUS 225 //! MOS_STATUS_SUCCESS if success, else fail reason 226 //! 227 MOS_STATUS StoreTSByPipeCtrl( 228 std::shared_ptr<mhw::mi::Itf> miItf, 229 PMOS_COMMAND_BUFFER cmdBuffer, 230 MOS_CONTEXT_HANDLE pOsContext, 231 uint32_t offset); 232 233 //! 234 //! \brief Save timestamp to the buffer by MI command 235 //! 236 //! \param [in] miItf 237 //! Reference to Mhw MiItf. 238 //! \param [in] pOsContext 239 //! Pointer of DEVICE CONTEXT 240 //! \param [in] cmdBuffer 241 //! Pointer of OS command buffer 242 //! \param [in] offset 243 //! Offset in the buffer 244 //! 245 //! \return MOS_STATUS 246 //! MOS_STATUS_SUCCESS if success, else fail reason 247 //! 248 MOS_STATUS StoreTSByMiFlush( 249 std::shared_ptr<mhw::mi::Itf> miItf, 250 PMOS_COMMAND_BUFFER cmdBuffer, 251 MOS_CONTEXT_HANDLE pOsContext, 252 uint32_t offset); 253 254 //! 255 //! \brief Save performance data in to a file 256 //! 257 //! \param [in] osInterface 258 //! Pointer of OS interface 259 //! 260 //! \return MOS_STATUS 261 //! MOS_STATUS_SUCCESS if success, else fail reason 262 //! 263 MOS_STATUS SavePerfData(MOS_INTERFACE *osInterface); 264 265 //! 266 //! \brief Convert GPU context to GPU node 267 //! 268 //! \param [in] context 269 //! GPU context 270 //! 271 //! \return PerfGPUNode 272 //! 273 PerfGPUNode GpuContextToGpuNode(MOS_GPU_CONTEXT context); 274 275 //! 276 //! \brief Check the performance mode 277 //! 278 //! \param [in] regs 279 //! The registers' array 280 //! 281 //! \return bool 282 //! true if include the memory information 283 //! 284 bool IsPerfModeWidthMemInfo(uint32_t *regs); 285 286 //! 287 //! \brief Map the platform ID 288 //! 289 //! \param [in] platform 290 //! 291 //! \return uint32_t 292 //! mapped platform id used to fill node header 293 //! 294 uint32_t PlatFormIdMap(PLATFORM platform); 295 296 //! 297 //! \brief Save data to the buffer which store the performance data 298 //! 299 //! \param [in] miInterface 300 //! Pointer of MI interface 301 //! \param [in] cmdBuffer 302 //! Pointer of OS command buffer 303 //! \param [in] pOsContext 304 //! Pointer of DEVICE CONTEXT 305 //! \param [in] offset 306 //! Offset in the buffer 307 //! \param [in] value 308 //! Value of data 309 //! 310 //! \return MOS_STATUS 311 //! MOS_STATUS_SUCCESS if success, else fail reason 312 //! 313 MOS_STATUS StoreData( 314 MhwMiInterface *miInterface, 315 PMOS_COMMAND_BUFFER cmdBuffer, 316 PMOS_CONTEXT pOsContext, 317 uint32_t offset, 318 uint32_t value); 319 320 //! 321 //! \brief Save data to the buffer which store the performance data 322 //! 323 //! \param [in] miInterface 324 //! Pointer of MI interface 325 //! \param [in] cmdBuffer 326 //! Pointer of OS command buffer 327 //! \param [in] pOsContext 328 //! Pointer of DEVICE CONTEXT 329 //! \param [in] offset 330 //! Offset in the buffer 331 //! \param [in] value 332 //! Value of data 333 //! 334 //! \return MOS_STATUS 335 //! MOS_STATUS_SUCCESS if success, else fail reason 336 //! 337 MOS_STATUS StoreDataNext( 338 MhwMiInterface *miInterface, 339 PMOS_COMMAND_BUFFER cmdBuffer, 340 PMOS_CONTEXT pOsContext, 341 uint32_t offset, 342 uint32_t value); 343 344 //! 345 //! \brief Save register value to the buffer which store the performance data 346 //! \param [in] osInterface 347 //! Pointer of MOS_INTERFACE 348 //! 349 //! \param [in] miInterface 350 //! Pointer of MI interface 351 //! \param [in] cmdBuffer 352 //! Pointer of OS command buffer 353 //! \param [in] offset 354 //! Offset in the buffer 355 //! \param [in] reg 356 //! Address of register 357 //! 358 //! \return MOS_STATUS 359 //! MOS_STATUS_SUCCESS if success, else fail reason 360 //! 361 MOS_STATUS StoreRegister( 362 MOS_INTERFACE *osInterface, 363 MhwMiInterface *miInterface, 364 PMOS_COMMAND_BUFFER cmdBuffer, 365 uint32_t offset, 366 uint32_t reg); 367 368 //! 369 //! \brief Save register value to the buffer which store the performance data 370 //! \param [in] osInterface 371 //! Pointer of MOS_INTERFACE 372 //! 373 //! \param [in] miInterface 374 //! Pointer of MI interface 375 //! \param [in] cmdBuffer 376 //! Pointer of OS command buffer 377 //! \param [in] offset 378 //! Offset in the buffer 379 //! \param [in] reg 380 //! Address of register 381 //! 382 //! \return MOS_STATUS 383 //! MOS_STATUS_SUCCESS if success, else fail reason 384 //! 385 MOS_STATUS StoreRegisterNext( 386 MOS_INTERFACE *osInterface, 387 MhwMiInterface *miInterface, 388 PMOS_COMMAND_BUFFER cmdBuffer, 389 uint32_t offset, 390 uint32_t reg); 391 392 //! 393 //! \brief Save timestamp to the buffer by Pipe control command 394 //! 395 //! \param [in] miInterface 396 //! Pointer of MI interface 397 //! \param [in] cmdBuffer 398 //! Pointer of OS command buffer 399 //! \param [in] pOsContext 400 //! Pointer of DEVICE CONTEXT 401 //! \param [in] offset 402 //! Offset in the buffer 403 //! 404 //! \return MOS_STATUS 405 //! MOS_STATUS_SUCCESS if success, else fail reason 406 //! 407 MOS_STATUS StoreTSByPipeCtrl( 408 MhwMiInterface *miInterface, 409 PMOS_COMMAND_BUFFER cmdBuffer, 410 PMOS_CONTEXT pOsContext, 411 uint32_t offset); 412 413 //! 414 //! \brief Save timestamp to the buffer by Pipe control command 415 //! 416 //! \param [in] miInterface 417 //! Pointer of MI interface 418 //! \param [in] cmdBuffer 419 //! Pointer of OS command buffer 420 //! \param [in] pOsContext 421 //! Pointer of DEVICE CONTEXT 422 //! \param [in] offset 423 //! Offset in the buffer 424 //! 425 //! \return MOS_STATUS 426 //! MOS_STATUS_SUCCESS if success, else fail reason 427 //! 428 MOS_STATUS StoreTSByPipeCtrlNext( 429 MhwMiInterface *miInterface, 430 PMOS_COMMAND_BUFFER cmdBuffer, 431 PMOS_CONTEXT pOsContext, 432 uint32_t offset); 433 434 //! 435 //! \brief Save timestamp to the buffer by MI command 436 //! 437 //! \param [in] miInterface 438 //! Pointer of MI interface 439 //! \param [in] cmdBuffer 440 //! Pointer of OS command buffer 441 //! \param [in] pOsContext 442 //! Pointer of DEVICE CONTEXT 443 //! \param [in] offset 444 //! Offset in the buffer 445 //! 446 //! \return MOS_STATUS 447 //! MOS_STATUS_SUCCESS if success, else fail reason 448 //! 449 MOS_STATUS StoreTSByMiFlush( 450 MhwMiInterface *miInterface, 451 PMOS_COMMAND_BUFFER cmdBuffer, 452 PMOS_CONTEXT pOsContext, 453 uint32_t offset); 454 455 //! 456 //! \brief Save timestamp to the buffer by MI command 457 //! 458 //! \param [in] miInterface 459 //! Pointer of MI interface 460 //! \param [in] cmdBuffer 461 //! Pointer of OS command buffer 462 //! \param [in] pOsContext 463 //! Pointer of DEVICE CONTEXT 464 //! \param [in] offset 465 //! Offset in the buffer 466 //! 467 //! \return MOS_STATUS 468 //! MOS_STATUS_SUCCESS if success, else fail reason 469 //! 470 MOS_STATUS StoreTSByMiFlushNext( 471 MhwMiInterface* miInterface, 472 PMOS_COMMAND_BUFFER cmdBuffer, 473 PMOS_CONTEXT pOsContext, 474 uint32_t offset); 475 476 //! 477 //! \brief Copy DW data from src to dst 478 //! 479 //! \param [in] miInterface 480 //! Pointer of MI interface 481 //! \param [in] cmdBuffer 482 //! Pointer of OS command buffer 483 //! \param [in] pOsContext 484 //! Pointer of DEVICE CONTEXT 485 //! \param [in] presSrc 486 //! SRC resource 487 //! \param [in] dwSrcOffset 488 //! Offset in the SRC 489 //! \param [in] dwDstOffset 490 //! Offset in the DS 491 //! 492 //! \return MOS_STATUS 493 //! MOS_STATUS_SUCCESS if success, else fail reason 494 //! 495 MOS_STATUS CopyMemData( 496 std::shared_ptr<mhw::mi::Itf>& miItf, 497 PMOS_COMMAND_BUFFER cmdBuffer, 498 MOS_CONTEXT_HANDLE pOsContext, 499 PMOS_RESOURCE presSrc, 500 uint32_t dwSrcOffset, 501 uint32_t dwDstOffset); 502 503 public: 504 //! 505 //! \brief Insert start command of storing performance data 506 //! 507 //! \param [in] context 508 //! Pointer of Codechal/VPHal 509 //! \param [in] osInterface 510 //! Pointer of OS interface 511 //! \param [in] miInterface 512 //! pointer of MI interface 513 //! \param [in] cmdBuffer 514 //! Pointer of OS command buffer 515 //! 516 //! \return MOS_STATUS 517 //! MOS_STATUS_SUCCESS if success, else fail reason 518 //! 519 virtual MOS_STATUS AddPerfCollectStartCmd( 520 void* context, 521 MOS_INTERFACE *osInterface, 522 MhwMiInterface *miInterface, 523 MOS_COMMAND_BUFFER *cmdBuffer); 524 525 //! 526 //! \brief Insert end command of storing performance data 527 //! 528 //! \param [in] context 529 //! Pointer of Codechal/VPHal 530 //! \param [in] osInterface 531 //! Pointer of OS interface 532 //! \param [in] miInterface 533 //! pointer of MI interface 534 //! \param [in] cmdBuffer 535 //! Pointer of OS command buffer 536 //! 537 //! \return MOS_STATUS 538 //! MOS_STATUS_SUCCESS if success, else fail reason 539 //! 540 virtual MOS_STATUS AddPerfCollectEndCmd( 541 void* context, 542 MOS_INTERFACE *osInterface, 543 MhwMiInterface *miInterface, 544 MOS_COMMAND_BUFFER *cmdBuffer); 545 546 //! 547 //! \brief Insert MEM Copy command of storing quality data 548 //! 549 //! \param [in] context 550 //! Pointer of Codechal/VPHal 551 //! \param [in] osInterface 552 //! Pointer of OS interface 553 //! \param [in] miInterface 554 //! pointer of MI interface 555 //! \param [in] cmdBuffer 556 //! Pointer of OS command buffer 557 //! \param [in] item 558 //! Quality metric item 559 //! \param [in] presSrc 560 //! SRC resource 561 //! \param [in] dwSrcOffset 562 //! Offset in the SRC 563 //! 564 //! \return MOS_STATUS 565 //! MOS_STATUS_SUCCESS if success, else fail reason 566 //! 567 MOS_STATUS AddCopyQualityMetricCmd( 568 void *context, 569 MOS_INTERFACE *osInterface, 570 std::shared_ptr<mhw::mi::Itf>& miItf, 571 MOS_COMMAND_BUFFER *cmdBuffer, 572 UMD_QUALITY_METRIC_ITEM item, 573 PMOS_RESOURCE presSrc, 574 uint32_t dwSrcOffset); 575 576 //! 577 //! \brief Insert MEM Copy command of storing quality data 578 //! 579 //! \param [in] context 580 //! Pointer of Codechal/VPHal 581 //! \param [in] osInterface 582 //! Pointer of OS interface 583 //! \param [in] miInterface 584 //! pointer of MI interface 585 //! \param [in] cmdBuffer 586 //! Pointer of OS command buffer 587 //! \param [in] reg 588 //! Address of register 589 //! 590 //! \return MOS_STATUS 591 //! MOS_STATUS_SUCCESS if success, else fail reason 592 //! 593 MOS_STATUS AddStoreBitstreamSizeCmd( 594 void *context, 595 MOS_INTERFACE *osInterface, 596 std::shared_ptr<mhw::mi::Itf>& miItf, 597 MOS_COMMAND_BUFFER *cmdBuffer, 598 uint32_t reg); 599 600 private: 601 std::unordered_map<PMOS_CONTEXT, PMOS_RESOURCE> m_perfStoreBufferMap; //!< Buffer for perf data collection 602 std::unordered_map<PMOS_CONTEXT,uint32_t> m_refMap; //!< The number of refereces 603 std::unordered_map<PMOS_CONTEXT,uint32_t> m_perfDataIndexMap; //!< The index of performance data node in buffer 604 std::unordered_map<PMOS_CONTEXT,bool> m_initializedMap; //!< Indicate whether profiler was initialized 605 606 Map m_contextIndexMap; //!< Map between CodecHal/VPHal and PerfDataContext 607 PMOS_MUTEX m_mutex = nullptr; //!< Mutex for protecting data of profiler when refereced multi times 608 uint32_t m_bufferSize = 10000000; //!< The size of perf data buffer 609 uint32_t m_timerBase = 0; //!< time frequency 610 int32_t m_multiprocess = 0; //!< multi process support 611 uint32_t m_registers[8] = { 0 }; //!< registers of Memory information 612 const std::string m_registersKey[8] = {__MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_1, 613 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_2, 614 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_3, 615 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_4, 616 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_5, 617 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_6, 618 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_7, 619 __MEDIA_USER_FEATURE_VALUE_PERF_PROFILER_REGISTER_KEY_8}; //!< registers key 620 int32_t m_profilerEnabled; //!< UMD Perf Profiler enable or not 621 std::string m_outputFileName = ""; //!< Name of output file 622 bool m_enableProfilerDump = true; //!< Indicate whether enable UMD Profiler dump 623 std::shared_ptr<mhw::mi::Itf> m_miItf = nullptr; 624 int32_t m_multiprocesssinglebin = 0; //!< multi process single binary flag 625 int32_t m_mergeheader = 0; //!< multi header support 626 uint32_t* m_perfDataCombined = nullptr; //!< Combined perf data pointer 627 uint32_t m_perfDataCombinedSize = 0; //!< Combined perf data size 628 uint32_t m_perfDataCombinedIndex = 0; //!< Combined perf data index 629 uint32_t m_perfDataCombinedOffset = 0; //!< Combined perf data offset 630 MEDIA_CLASS_DEFINE_END(MediaPerfProfiler) 631 }; 632 633 #endif // __MEDIA_PERF_PROFILER_H__