File: cm_hal_dump.cpp

package info (click to toggle)
intel-media-driver 18.4.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 77,144 kB
  • sloc: cpp: 784,288; ansic: 95,944; asm: 42,125; python: 353; sh: 156; makefile: 15
file content (745 lines) | stat: -rw-r--r-- 26,521 bytes parent folder | download | duplicates (2)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
* Copyright (c) 2017, 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      cm_hal_dump.cpp 
//! \brief     Functions related to dump generated hw commands/curbe data 
//!

#include "cm_hal.h"
#include "renderhal_platform_interface.h"

#if (MDF_COMMAND_BUFFER_DUMP || MDF_CURBE_DATA_DUMP || MDF_SURFACE_STATE_DUMP)
#if defined(ANDROID) || defined(LINUX)
#define PlatformSNPrintf snprintf
#define PLATFORM_DIR_SEPERATOR   "/"
#else
#define PlatformSNPrintf sprintf_s
#define PLATFORM_DIR_SEPERATOR   "\\"
#endif

#define SIZE_OF_DWORD_PLUS_ONE                  (2*sizeof(uint32_t) +1)
//!
//! \brief    Dump Hex Dword to dest buffer
//! \param    [in] destBuf
//!           dest buffer
//! \param    [in] buflen
//!           length of buffer
//! \param    [in] srcBuf
//!           pointer to surface of buffer
//! \param    [in] uNum
//!           number of dword to dump
//! \return   number of bytes written
//!
static uint32_t HalCm_CopyHexDwordLine(char  *destBuf, size_t buflen, uint32_t *srcBuf, uint32_t uNum)
{
    uint32_t bytesWritten = 0;
    for (uint32_t i = 0; i < uNum; ++i) {
        bytesWritten += PlatformSNPrintf(destBuf + bytesWritten, buflen - bytesWritten, "%08x ", srcBuf[i]);
    }
    bytesWritten += PlatformSNPrintf(destBuf + bytesWritten, buflen - bytesWritten, "\n");

    return bytesWritten;
}

//!
//! \brief    Get dump file name and counter
//! \param    [in] fileNamePrefix[]
//!           dump file name
//! \param    [in] timeStampFlag
//!           flag to control if need time stamp
//! \param    [in] counter
//!           dump file counter
//! \param    [in] pValueName
//!           pointer to value name, used for user feature read
//! \param    [in] outputDir
//!           pointer to output dir
//! \param    [in] outputFile
//!           pointer output file prefix
//! \return   number of bytes written
//!
int32_t GetFileNameAndCounter(char fileNamePrefix[], bool timeStampFlag, int32_t counter,
                              const char *pValueName, const char *outputDir,const char *outputFile)
{
    GetLogFileLocation(outputDir, fileNamePrefix);
    PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix), 
                     MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), PLATFORM_DIR_SEPERATOR);

    if (timeStampFlag)
    {
        SYSTEMTIME systime;
        GetLocalTime(&systime);
        PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix), 
                        MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), "%s_%d_%d_%d_%d_%d_%d_%d.txt",
                        outputFile, systime.wMonth, systime.wDay, systime.wHour, 
                        systime.wMinute, systime.wSecond, systime.wMilliseconds, counter);
    }
    else
    {
        //check if command buffer or surface state counter, and get it
        if (!strcmp(outputFile,"Command_Buffer"))
        {
            counter = GetCommandBufferDumpCounter(__MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_COUNTER);
        }
        else if (!strcmp(outputFile,"Surface_State_Dump"))
        {
            counter = GetSurfaceStateDumpCounter(__MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER);
        }
        else if (!strcmp(outputFile, "Interface_Descriptor_Data_Dump"))
        {
            counter = GetInterfaceDescriptorDataDumpCounter(__MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_COUNTER);
        }
       
        PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix), 
                         MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), "%s_%d.txt", outputFile, counter);
    }
    return counter;

}

#endif

#if MDF_COMMAND_BUFFER_DUMP

#define HALCM_COMMAND_BUFFER_OUTPUT_DIR         "HALCM_Command_Buffer_Dumps"
#define HALCM_COMMAND_BUFFER_OUTPUT_FILE        "Command_Buffer"

//!
//! \brief    Read Register key to check if dump flag enabled
//! \param    [in] state
//!           Pointer to cm hal state
//! \return   CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_InitDumpCommandBuffer(PCM_HAL_STATE state)
{
    MOS_USER_FEATURE        userFeature;
    char                    fileName[MOS_MAX_HLT_FILENAME_LEN];
    MOS_STATUS              eStatus;
    MOS_USER_FEATURE_VALUE  userFeatureValue;
    int32_t                 hr = CM_FAILURE;

    MOS_OS_ASSERT(state);

    MOS_ZeroMemory(&userFeatureValue, sizeof(MOS_USER_FEATURE_VALUE));
    MOS_ZeroMemory(&userFeature, sizeof(userFeature));

    // Check if command buffer dump was enabled in user feature settings.
    userFeature.Type = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues = &userFeatureValue;
    userFeature.uiNumValues = 1;
    userFeatureValue.bData = false;

    GetLogFileLocation(HALCM_COMMAND_BUFFER_OUTPUT_DIR, fileName);

    eStatus = MOS_UserFeature_ReadValue(
        nullptr,
        &userFeature,
        "Dump Command Buffer Enable",
        MOS_USER_FEATURE_VALUE_TYPE_INT32);
    if (eStatus != MOS_STATUS_SUCCESS)
    {
        MOS_OS_NORMALMESSAGE("Unable to read command buffer user feature key. Status = %d", eStatus);
        goto finish;
    }
    if (userFeatureValue.bData)
    {
       
        eStatus = MOS_CreateDirectory(fileName);
        if (eStatus != MOS_STATUS_SUCCESS)
        {
            MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
            goto finish;
        }
        // Setup member function and variable.
        state->dumpCommandBuffer = userFeatureValue.bData?true: false;
        if (userFeatureValue.bData == 17)
        {
            state->enableCMDDumpTimeStamp = true;
        }
        else
        {
            state->enableCMDDumpTimeStamp = false;
        }
    }
    hr = CM_SUCCESS;
finish:
    return hr;
}

//!
//! \brief    Dump command buffer to file
//! \param    [in] state
//!           pointer to cm hal state
//! \param    [in] cmdBuffer
//!           pointer to command buffer
//! \param    [in] offsetSurfaceState
//!           offset to surface state
//! \param    [in] sizeOfSurfaceState
//!           size of surface state
//! \return   int32_t
//!           CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_DumpCommadBuffer(PCM_HAL_STATE state, PMOS_COMMAND_BUFFER cmdBuffer, int offsetSurfaceState, size_t sizeOfSurfaceState)
{

    int32_t        commandBufferNumber = 0;
    int32_t         hr = CM_FAILURE;
    MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
    char            *outputBuffer = nullptr;
    // Each hex value should have 9 chars.
    uint32_t        bytesWritten = 0;
    uint32_t        numberOfDwords = 0;
    uint32_t        sizeToAllocate = 0;
    char            fileName[MOS_MAX_HLT_FILENAME_LEN];
    uint32_t        offset = 0;

    PMOS_INTERFACE osInterface = state->osInterface;
    PRENDERHAL_STATE_HEAP stateHeap   = state->renderHal->pStateHeap;

    MOS_OS_ASSERT(state);
    MOS_OS_ASSERT(cmdBuffer);

   //Check if use timestamp in cmd buffer dump file
    commandBufferNumber = GetFileNameAndCounter(fileName, state->enableCMDDumpTimeStamp,
                          commandBufferNumber,
                          __MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_COUNTER, 
                          HALCM_COMMAND_BUFFER_OUTPUT_DIR,
                          HALCM_COMMAND_BUFFER_OUTPUT_FILE);

    //get the command buffer header size
    offset = GetCommandBufferHeaderDWords(osInterface);

    numberOfDwords = cmdBuffer->iOffset / sizeof(uint32_t) - offset;
    sizeToAllocate = numberOfDwords * (SIZE_OF_DWORD_PLUS_ONE)+2 +   //length of command buffer line
        stateHeap->iCurrentSurfaceState *
        (SIZE_OF_DWORD_PLUS_ONE * 
        state->renderHal->pRenderHalPltInterface->GetSurfaceStateCmdSize() / sizeof(uint32_t) + 2); //length of surface state lines
                                                                                                                          // Alloc output buffer.
    outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
    if (!outputBuffer) {
        MOS_OS_NORMALMESSAGE("Failed to allocate memory for command buffer dump");
        return MOS_STATUS_NO_SPACE;
    }    

    // write command buffer dwords.
    bytesWritten += HalCm_CopyHexDwordLine(outputBuffer, sizeToAllocate - bytesWritten,
                                          (uint32_t *)cmdBuffer->pCmdBase + offset, numberOfDwords);
    MOS_OS_CHK_STATUS(MOS_WriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));
    commandBufferNumber++;

    //Record command buffer dump counter
    if (!state->enableCMDDumpTimeStamp)
    {        
        RecordCommandBufferDumpCounter(commandBufferNumber,
                                       __MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_COUNTER_ID);
    }  
    hr = CM_SUCCESS;
finish:
    // Free the memory.
    if (outputBuffer)
    {
        MOS_FreeMemAndSetNull(outputBuffer);
    }
    return hr;
}

#endif

#if MDF_CURBE_DATA_DUMP

#define HALCM_CURBE_DATA_OUTPUT_DIR         "HALCM_Curbe_Data_Dumps"
#define HALCM_CURBE_DATA_OUTPUT_FILE        "Curbe_Data"

//!
//! \brief    Read Register key to check if Curbe Data dump flag enabled
//! \param    [in] state
//!           Pointer to cm hal state
//! \return   CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_InitDumpCurbeData(PCM_HAL_STATE state)
{
    MOS_USER_FEATURE        userFeature;
    char                    fileName[MOS_MAX_HLT_FILENAME_LEN];
    MOS_STATUS              eStatus;
    MOS_USER_FEATURE_VALUE  userFeatureValue;
    int32_t                 hr = CM_FAILURE;

    MOS_OS_ASSERT(state);

    MOS_ZeroMemory(&userFeatureValue, sizeof(MOS_USER_FEATURE_VALUE));
    MOS_ZeroMemory(&userFeature, sizeof(userFeature));

    // Check if curbe data dump was enabled in user feature settings.
    userFeature.Type = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues = &userFeatureValue;
    userFeature.uiNumValues = 1;
    userFeatureValue.bData = false;

    eStatus = MOS_UserFeature_ReadValue(
        nullptr,
        &userFeature,
        __MEDIA_USER_FEATURE_VALUE_MDF_CURBE_DUMP_ENABLE,
        MOS_USER_FEATURE_VALUE_TYPE_INT32);
    if (eStatus != MOS_STATUS_SUCCESS)
    {
        MOS_OS_NORMALMESSAGE("Unable to read curbe data dump user feature key. Status = %d", eStatus);
        goto finish;
    }
    if (userFeatureValue.bData)
    {
        GetLogFileLocation(HALCM_CURBE_DATA_OUTPUT_DIR, fileName);
        eStatus = MOS_CreateDirectory(fileName);
        if (eStatus != MOS_STATUS_SUCCESS)
        {
            MOS_OS_NORMALMESSAGE("Failed to create curbe data output directory. Status = %d", eStatus);
            goto finish;
        }
        // Setup member function and variable.
        state->dumpCurbeData = userFeatureValue.bData ? true: false;
    }
    hr = CM_SUCCESS;
finish:
    return hr;
}

//!
//! \brief    Dump Curbe Data to file
//! \param    [in] state
//!           pointer to cm hal state
//! \return   int32_t
//!           CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_DumpCurbeData(PCM_HAL_STATE state)
{
    static uint32_t curbeDataNumber = 0;
    int32_t         hr = CM_FAILURE;
    MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
    char            *outputBuffer = nullptr;
    uint32_t        bytesWritten = 0;
    char            fileName[MOS_MAX_HLT_FILENAME_LEN];
    uint32_t        numberOfDwords = 0;
    uint32_t        sizeToAllocate = 0;
    PMOS_INTERFACE osInterface = state->osInterface;
    uint32_t        *curbeData = nullptr;
    PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;

    MOS_OS_ASSERT(state);

    // Set the file name.
    GetLogFileLocation(HALCM_CURBE_DATA_OUTPUT_DIR, fileName);

    PlatformSNPrintf(fileName + strlen(fileName), MOS_MAX_HLT_FILENAME_LEN - strlen(fileName),
                     PLATFORM_DIR_SEPERATOR);
    PlatformSNPrintf(fileName + strlen(fileName), MOS_MAX_HLT_FILENAME_LEN - strlen(fileName),
                     "%s_%d.txt", HALCM_CURBE_DATA_OUTPUT_FILE, (int)curbeDataNumber);

    // write curbe data dwords.
    if (state->dshEnabled)
    {
        numberOfDwords = stateHeap->pCurMediaState->pDynamicState->Curbe.dwSize / sizeof(uint32_t);
        sizeToAllocate = numberOfDwords*SIZE_OF_DWORD_PLUS_ONE+2;
        outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
        curbeData = (uint32_t *)MOS_AllocAndZeroMemory(stateHeap->pCurMediaState->pDynamicState->Curbe.dwSize);
        stateHeap->pCurMediaState->pDynamicState->memoryBlock.ReadData(curbeData,
            stateHeap->pCurMediaState->pDynamicState->Curbe.dwOffset,
            stateHeap->pCurMediaState->pDynamicState->Curbe.dwSize);

        bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
                          sizeToAllocate - bytesWritten,
                          curbeData,
                          numberOfDwords);
    }
    else
    {
        numberOfDwords = stateHeap->pCurMediaState->iCurbeOffset / sizeof(uint32_t);
        sizeToAllocate = numberOfDwords*SIZE_OF_DWORD_PLUS_ONE+2;
        outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
        bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
                          sizeToAllocate - bytesWritten,
                          (uint32_t*)(stateHeap->pGshBuffer + stateHeap->pCurMediaState->dwOffset + stateHeap->dwOffsetCurbe),
                          numberOfDwords);
    }

    MOS_OS_CHK_STATUS(MOS_WriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));

    curbeDataNumber++;

    hr = CM_SUCCESS;

finish:
    // Free the memory.
    if (outputBuffer)
    {
        MOS_FreeMemAndSetNull(outputBuffer);
    }
    
    if (curbeData)
    {
        MOS_FreeMemAndSetNull(curbeData);
    }

    return hr;

}

#endif

#if MDF_SURFACE_CONTENT_DUMP

//!
//! \brief    Read Register key to check if Surface content flag enabled
//! \param    [in] state
//!           Pointer to cm hal state
//! \return   CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_InitSurfaceDump(PCM_HAL_STATE state)
{
    MOS_USER_FEATURE        userFeature;
    MOS_STATUS              eStatus;
    MOS_USER_FEATURE_VALUE  userFeatureValue;
    int32_t                 hr = CM_FAILURE;

    MOS_OS_ASSERT(state);

    MOS_ZeroMemory(&userFeatureValue, sizeof(MOS_USER_FEATURE_VALUE));
    MOS_ZeroMemory(&userFeature, sizeof(userFeature));

    // Check if surface content dump was enabled in user feature settings.
    userFeature.Type = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues = &userFeatureValue;
    userFeature.uiNumValues = 1;
    userFeatureValue.bData = false;

    eStatus = MOS_UserFeature_ReadValue(
        nullptr,
        &userFeature,
        __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_DUMP_ENABLE,
        MOS_USER_FEATURE_VALUE_TYPE_INT32);
    if (eStatus != MOS_STATUS_SUCCESS)
    {
        MOS_OS_NORMALMESSAGE("Unable to read surface content dump user feature key. Status = %d", eStatus);
        goto finish;
    }
    if (userFeatureValue.bData)
    {
        // Setup member function and variable.
        state->dumpSurfaceContent = userFeatureValue.bData ? true: false;
    }
    hr = CM_SUCCESS;
finish:
    return hr;
}

#endif


#if MDF_SURFACE_STATE_DUMP
#define HALCM_SURFACE_STATE_OUTPUT_DIR         "HALCM_Surface_State_Dumps"
#define HALCM_SURFACE_STATE_OUTPUT_FILE        "Surface_State_Dump"

int32_t HalCm_InitDumpSurfaceState(PCM_HAL_STATE state)
{
    MOS_USER_FEATURE        userFeature;
    char                    fileName[MOS_MAX_HLT_FILENAME_LEN];
    MOS_STATUS              eStatus;
    MOS_USER_FEATURE_VALUE  userFeatureValue;
    int32_t                 hr = CM_FAILURE;

    MOS_OS_ASSERT(state);

    MOS_ZeroMemory(&userFeatureValue, sizeof(MOS_USER_FEATURE_VALUE));
    MOS_ZeroMemory(&userFeature, sizeof(userFeature));

    // Check if command buffer dump was enabled in user feature settings.
    userFeature.Type = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues = &userFeatureValue;
    userFeature.uiNumValues = 1;
    userFeatureValue.bData = false;

    GetLogFileLocation(HALCM_SURFACE_STATE_OUTPUT_DIR, fileName);

    eStatus = MOS_UserFeature_ReadValue(
        nullptr,
        &userFeature,
        __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_ENABLE,
        MOS_USER_FEATURE_VALUE_TYPE_INT32);
    if (eStatus != MOS_STATUS_SUCCESS)
    {
        MOS_OS_NORMALMESSAGE("Unable to read surface state user feature key. Status = %d", eStatus);
        goto finish;
    }
    if (userFeatureValue.bData)
    {
        eStatus = MOS_CreateDirectory(fileName);
        if (eStatus != MOS_STATUS_SUCCESS)
        {
            MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
            goto finish;
        }
        // Setup member function and variable.
        state->dumpSurfaceState = userFeatureValue.bData ? true : false;

        if (userFeatureValue.bData == 17)
        {
            state->enableSurfaceStateDumpTimeStamp = true;
        }
        else
        {
            state->enableSurfaceStateDumpTimeStamp = false;
        }
    }
    hr = CM_SUCCESS;
finish:
    return hr;
}

//!
//! \brief    Dump surface state to file
//! \param    [in] state
//!           pointer to cm hal state
//! \param    [in] offsetSurfaceState
//!           offset to surface state
//! \param    [in] sizeOfSurfaceState
//!           size of surface state
//! \return   int32_t
//!           CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_DumpSurfaceState(PCM_HAL_STATE state,  int offsetSurfaceState, size_t sizeOfSurfaceState)
{
    int32_t         hr = CM_FAILURE;
    MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;

    PMOS_INTERFACE osInterface = state->osInterface;
    PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;

    char            filename[MOS_MAX_HLT_FILENAME_LEN];
    int32_t         surfacestatedumpNumber = 0;
    uint32_t        surfacebytesWritten = 0;
    char            *surfaceoutputBuffer = nullptr;
    uint32_t        surfacesizeToAllocate = 0;
   
   //Check if use timestamp in cmd buffer dump file
    surfacestatedumpNumber = GetFileNameAndCounter(filename, state->enableSurfaceStateDumpTimeStamp,
        surfacestatedumpNumber,
        __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER,
        HALCM_SURFACE_STATE_OUTPUT_DIR,
        HALCM_SURFACE_STATE_OUTPUT_FILE);
    
    //calculate surface state dump allocation size
    surfacesizeToAllocate = stateHeap->iCurrentSurfaceState *
        (SIZE_OF_DWORD_PLUS_ONE * 
        state->renderHal->pRenderHalPltInterface->GetSurfaceStateCmdSize() / sizeof(uint32_t) + 2);
    
    //allocate surface output buffer
    surfaceoutputBuffer = (char *)MOS_AllocAndZeroMemory(surfacesizeToAllocate);
    if (!surfaceoutputBuffer) {
        MOS_OS_NORMALMESSAGE("Failed to allocate memory for surface state dump");
        return MOS_STATUS_NO_SPACE;
    }

    //write all surface states
    for (int32_t index = 0; index < stateHeap->iCurrentSurfaceState; ++index) {
        PRENDERHAL_SURFACE_STATE_ENTRY entry = stateHeap->pSurfaceEntry + index;
        void *surfaceState = (char*)entry->pSurfaceState;
        //the address of surface states are 32bit or uint32_t aligned.
        surfacebytesWritten += HalCm_CopyHexDwordLine(surfaceoutputBuffer + surfacebytesWritten, 
                               surfacesizeToAllocate - surfacebytesWritten, (uint32_t*)surfaceState,
                               sizeOfSurfaceState / sizeof(uint32_t));
    }

    //Write to file
    MOS_OS_CHK_STATUS(MOS_WriteFileFromPtr((const char *)filename, surfaceoutputBuffer, 
                      surfacebytesWritten));

    surfacestatedumpNumber++;

    if (!state->enableSurfaceStateDumpTimeStamp)
    {
        RecordSurfaceStateDumpCounter(surfacestatedumpNumber, 
                                      __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER_ID);
    }

    hr = CM_SUCCESS;

finish:
    // Free the memory.
    if (surfaceoutputBuffer)
    {
        MOS_FreeMemAndSetNull(surfaceoutputBuffer);
    }

    return hr;

}
#endif

#if MDF_INTERFACE_DESCRIPTOR_DATA_DUMP
#define HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR         "HALCM_Interface_Descriptor_Data_Dumps"
#define HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_FILE        "Interface_Descriptor_Data_Dump"

int32_t HalCm_InitDumpInterfaceDescriporData(PCM_HAL_STATE state)
{
    MOS_USER_FEATURE        userFeature;
    char                    fileName[MOS_MAX_HLT_FILENAME_LEN];
    MOS_STATUS              eStatus;
    MOS_USER_FEATURE_VALUE  userFeatureValue;
    int32_t                 hr = CM_FAILURE;

    MOS_OS_ASSERT(state);

    MOS_ZeroMemory(&userFeatureValue, sizeof(MOS_USER_FEATURE_VALUE));
    MOS_ZeroMemory(&userFeature, sizeof(userFeature));

    // Check if command buffer dump was enabled in user feature settings.
    userFeature.Type = MOS_USER_FEATURE_TYPE_USER;
    userFeature.pPath = __MEDIA_USER_FEATURE_SUBKEY_INTERNAL;
    userFeature.pValues = &userFeatureValue;
    userFeature.uiNumValues = 1;
    userFeatureValue.bData = false;

    GetLogFileLocation(HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR, fileName);

    eStatus = MOS_UserFeature_ReadValue(
        nullptr,
        &userFeature,
        __MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_DUMP,
        MOS_USER_FEATURE_VALUE_TYPE_INT32);
    if (eStatus != MOS_STATUS_SUCCESS)
    {
        MOS_OS_NORMALMESSAGE("Unable to read interface descriptor data dump user feature key. Status = %d", eStatus);
        goto finish;
    }
    if (userFeatureValue.bData)
    {
        eStatus = MOS_CreateDirectory(fileName);
        if (eStatus != MOS_STATUS_SUCCESS)
        {
            MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
            goto finish;
        }
        // Setup member function and variable.
        state->dumpIDData = userFeatureValue.bData ? true : false;
        if (userFeatureValue.bData == 17)
        {
            state->enableIDDumpTimeStamp = true;
        }
        else
        {
            state->enableIDDumpTimeStamp = false;
        }
        
    }
    hr = CM_SUCCESS;
finish:
    return hr;
}

//!
//! \brief    Dump interface descriptor data to file
//! \param    [in] state
//!           pointer to cm hal state
//! \return   int32_t
//!           CM_SUCCESS if success, else fail reason
//!
int32_t HalCm_DumpInterfaceDescriptorData(PCM_HAL_STATE state)
{
    uint32_t        IDDNumber = 0; 
    int32_t         hr = CM_FAILURE;
    MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
    char            *outputBuffer = nullptr;
    uint32_t        bytesWritten = 0;
    char            fileName[MOS_MAX_HLT_FILENAME_LEN];
    uint32_t        numberOfDwords = 0;
    uint32_t        sizeToAllocate = 0;
    PMOS_INTERFACE osInterface = state->osInterface;
    uint32_t        *InterfaceDescriptorData = nullptr;
    PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;

    MOS_OS_ASSERT(state);

    // Set the file name.
    GetLogFileLocation(HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR, fileName);

    //Check if use timestamp in cmd buffer dump file
    IDDNumber = GetFileNameAndCounter(fileName, state->enableIDDumpTimeStamp,
        IDDNumber,
        __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER,
        HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR,
        HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_FILE);

    // write interface descriptor data dwords.
    if (state->dshEnabled)
    {
        numberOfDwords = stateHeap->pCurMediaState->pDynamicState->MediaID.dwSize / sizeof(uint32_t);
        sizeToAllocate = numberOfDwords * SIZE_OF_DWORD_PLUS_ONE + 2;
        outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
        InterfaceDescriptorData = (uint32_t *)MOS_AllocAndZeroMemory(stateHeap->pCurMediaState->pDynamicState->MediaID.dwSize);
        stateHeap->pCurMediaState->pDynamicState->memoryBlock.ReadData(InterfaceDescriptorData,
            stateHeap->pCurMediaState->pDynamicState->MediaID.dwOffset,
            stateHeap->pCurMediaState->pDynamicState->MediaID.dwSize);

        bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
            sizeToAllocate - bytesWritten,
            InterfaceDescriptorData,
            numberOfDwords);
    }
    else
    {
        numberOfDwords = stateHeap->dwSizeMediaID / sizeof(uint32_t);
        sizeToAllocate = numberOfDwords * SIZE_OF_DWORD_PLUS_ONE + 2;
        outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
        bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
            sizeToAllocate - bytesWritten,
            (uint32_t*)(stateHeap->pGshBuffer + stateHeap->pCurMediaState->dwOffset + stateHeap->dwOffsetMediaID),
            numberOfDwords);
    }

    MOS_OS_CHK_STATUS(MOS_WriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));

    IDDNumber++;
    if (!state->enableIDDumpTimeStamp)
    {
        RecordInterfaceDescriptorDataDumpCounter(IDDNumber,
            __MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_COUNTER_ID);
    }

    hr = CM_SUCCESS;

finish:
    // Free the memory.
    if (outputBuffer)
    {
        MOS_FreeMemAndSetNull(outputBuffer);
    }

    if (InterfaceDescriptorData)
    {
        MOS_FreeMemAndSetNull(InterfaceDescriptorData);
    }

    return hr;

}
#endif