1 /*
2 * Copyright (c) 2022-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_debug_fast_dump.h
24 //!
25 
26 #pragma once
27 
28 #if ((_DEBUG || _RELEASE_INTERNAL)) && !defined(USE_MEDIA_DEBUG_TOOL)
29 #define USE_MEDIA_DEBUG_TOOL 1
30 #endif  // ((_DEBUG || _RELEASE_INTERNAL) && !EMUL) && !defined(USE_MEDIA_DEBUG_TOOL)
31 
32 #if USE_MEDIA_DEBUG_TOOL
33 
34 #include <functional>
35 #include <string>
36 #include "media_debug_serializer.h"
37 #include "media_copy_wrapper.h"
38 
39 class MediaDebugFastDump
40 {
41 public:
42     struct Config
43     {
44     private:
45         template <typename T, T MIN, T MAX>
46         class RangedValue final
47         {
48         public:
RangedValueConfig49             RangedValue(T v)
50             {
51                 value = v < MIN ? MIN : v > MAX ? MAX
52                                                 : v;
53             }
54 
TConfig55             operator T() const
56             {
57                 return value;
58             }
59 
60         private:
61             T value;
62         };
63 
64         template <uint8_t MIN = 0, uint8_t MAX = 100>
65         using RangedUint8 = RangedValue<uint8_t, MIN, MAX>;
66 
67     public:
68         bool allowDataLoss = true;  // allow dumped data loss to reduce perf impact
69 
70         // sampling mode configurations
71         const size_t *frameIdx         = nullptr;  // pointer to the frame index managed by user
72         size_t        samplingTime     = 0;        // sampling time in ms or frame index
73         size_t        samplingInterval = 0;        // sampling interval in ms or frame index
74                                                    // when sampling time and sampling interval are not both 0, sampling mode is enabled,
75                                                    // if frameIdx is null, sampling is based on ms, otherwise on frame index, e.g.,
76                                                    // if ElapsedTime%(samplingTime+samplingInterval) <= samplingTime, current dump
77                                                    // task will be queued, otherwise discarded, ElapsedTime is equal to CurrentFrameIndex
78                                                    // for frame index based sampling and CurrentTime-StartTime otherwise
79 
80         // graphic memory usage configurations
81         RangedUint8<0, 2> memUsagePolicy      = 0;   // 0: balance shared/local memory usage; 1: prioritize shared; 2: prioritize local
82         RangedUint8<10>   maxPrioritizedMem   = 75;  // max percentage of prioritized memory can be used for fast dump, 10% - 100%
83         RangedUint8<>     maxDeprioritizedMem = 75;  // max percentage of deprioritized memory can be used for fast dump, 0% - 100%
84 
85         // media copy configurations
86         RangedUint8<> weightRenderCopy = 100;  // weight for render copy, 0 - 100
87         RangedUint8<> weightVECopy     = 80;   // weight for VE copy, 0 - 100
88         RangedUint8<> weightBLTCopy    = 20;   // weight for BLT copy, 0 - 100
89                                                // when weightRenderCopy, weightVECopy and weightBLTCopy are all 0, use default copy method,
90                                                // otherwise randomly select 1 of the 3 methods based on their weights, e.g., the chance of
91                                                // selecting render copy is weightRenderCopy/(weightRenderCopy+weightVECopy+weightBLTCopy)
92 
93         // file/trace writing configurations
94         RangedUint8<0, 2> writeDst      = 0;     // 0: file; 1: trace; 2: no write, for debug purpose
95         RangedUint8<0, 2> writeMode     = 2;     // 0: binary; 1: text, valid when writeDst is 0; 2: adaptive
96         size_t            bufferSize    = 0;     // buffer size in MB for buffered writing, valid when writeDst and writeMode are both 0
97         bool              informOnError = true;  // dump 1 byte filename.error_info file instead of nothing when error occurs, valid when
98                                                  // writeDst is 0
99     };
100 
101     using DefaultSerializer = MediaDebugSerializer<void>;
102 
103 public:
104     static void CreateInstance(
105         MOS_INTERFACE    &osItf,
106         MediaCopyWrapper &mediaCopyWrapper,
107         const Config     *cfg = nullptr);
108 
109     static void DestroyInstance();
110 
111     static bool IsGood();
112 
113     // if file name contains "w[0]_h[0]_p[0]", it will be replaced to "w[RealWidth]_h[RealHeight]_p[RealPitch]" by fast dump
114     static void Dump(
115         MOS_RESOURCE &res,
116         std::string &&name,
117         size_t        dumpSize = 0,
118         size_t        offset   = 0,
119         std::function<
120             void(std::ostream &, const void *, size_t)>
121             &&serializer = DefaultSerializer());
122 
123     static void Dump(
124         const void   *res,
125         std::string &&name,
126         size_t        dumpSize = 0,
127         size_t        offset   = 0,
128         std::function<
129             void(std::ostream &, const void *, size_t)>
130             &&serializer = DefaultSerializer());
131 
132 public:
133     virtual ~MediaDebugFastDump() = default;
134 
135 protected:
136     MediaDebugFastDump() = default;
137 
138     MEDIA_CLASS_DEFINE_END(MediaDebugFastDump)
139 };
140 
141 #endif  // USE_MEDIA_DEBUG_TOOL
142