2017-12-21 07:45:38 +08:00
/*
2018-12-21 00:38:38 +08:00
* Copyright ( C ) 2017 - 2019 Intel Corporation
2017-12-21 07:45:38 +08:00
*
2018-09-18 15:11:08 +08:00
* SPDX - License - Identifier : MIT
2017-12-21 07:45:38 +08:00
*
*/
# include "debug_settings_manager.h"
2019-02-27 18:39:32 +08:00
2017-12-21 07:45:38 +08:00
# include "runtime/event/event.h"
# include "runtime/helpers/dispatch_info.h"
2019-02-27 18:39:32 +08:00
# include "runtime/helpers/ptr_math.h"
2017-12-21 07:45:38 +08:00
# include "runtime/helpers/string.h"
2018-10-06 03:51:57 +08:00
# include "runtime/helpers/timestamp_packet.h"
2019-02-27 18:39:32 +08:00
# include "runtime/kernel/kernel.h"
# include "runtime/mem_obj/mem_obj.h"
2019-01-19 08:40:42 +08:00
# include "runtime/os_interface/definitions/translate_debug_settings.h"
2019-02-27 18:39:32 +08:00
# include "runtime/utilities/debug_settings_reader_creator.h"
2017-12-21 07:45:38 +08:00
# include "CL/cl.h"
# include <cstdio>
# include <sstream>
2019-04-01 17:25:47 +08:00
namespace std {
static std : : string to_string ( const std : : string & arg ) {
return arg ;
}
} // namespace std
2019-03-26 18:59:46 +08:00
namespace NEO {
2017-12-21 07:45:38 +08:00
DebugSettingsManager < globalDebugFunctionalityLevel > DebugManager ;
template < DebugFunctionalityLevel DebugLevel >
DebugSettingsManager < DebugLevel > : : DebugSettingsManager ( ) {
logFileName = " igdrcl.log " ;
if ( registryReadAvailable ( ) ) {
2018-12-13 20:46:32 +08:00
readerImpl = SettingsReaderCreator : : create ( ) ;
injectSettingsFromReader ( ) ;
2019-04-01 17:25:47 +08:00
dumpFlags ( ) ;
2017-12-21 07:45:38 +08:00
}
2019-01-19 08:40:42 +08:00
translateDebugSettings ( flags ) ;
2017-12-21 07:45:38 +08:00
std : : remove ( logFileName . c_str ( ) ) ;
2018-12-21 00:38:38 +08:00
}
2017-12-21 07:45:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : writeToFile ( std : : string filename , const char * str , size_t length , std : : ios_base : : openmode mode ) {
std : : ofstream outFile ( filename , mode ) ;
if ( outFile . is_open ( ) ) {
outFile . write ( str , length ) ;
outFile . close ( ) ;
}
}
template < DebugFunctionalityLevel DebugLevel >
2018-12-13 20:46:32 +08:00
DebugSettingsManager < DebugLevel > : : ~ DebugSettingsManager ( ) = default ;
2017-12-21 07:45:38 +08:00
2018-10-04 18:44:49 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : getHardwareInfoOverride ( std : : string & hwInfoConfig ) {
std : : string str = flags . HardwareInfoOverride . get ( ) ;
if ( str [ 0 ] = = ' \" ' ) {
str . pop_back ( ) ;
hwInfoConfig = str . substr ( 1 , std : : string : : npos ) ;
} else {
hwInfoConfig = str ;
}
}
2017-12-21 07:45:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : dumpKernel ( const std : : string & name , const std : : string & src ) {
if ( false = = debugKernelDumpingAvailable ( ) ) {
return ;
}
if ( flags . DumpKernels . get ( ) ) {
DBG_LOG ( LogApiCalls , " Kernel size " , src . size ( ) , src . c_str ( ) ) ;
writeToFile ( name + " .txt " , src . c_str ( ) , src . size ( ) , std : : ios : : trunc ) ;
}
}
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : logApiCall ( const char * function , bool enter , int32_t errorCode ) {
if ( false = = debugLoggingAvailable ( ) ) {
return ;
}
if ( flags . LogApiCalls . get ( ) ) {
std : : unique_lock < std : : mutex > theLock ( mtx ) ;
std : : thread : : id thisThread = std : : this_thread : : get_id ( ) ;
std : : stringstream ss ;
ss < < " ThreadID: " < < thisThread < < " " ;
if ( enter )
ss < < " Function Enter: " ;
else
ss < < " Function Leave ( " < < errorCode < < " ): " ;
ss < < function < < std : : endl ;
auto str = ss . str ( ) ;
writeToFile ( logFileName , str . c_str ( ) , str . size ( ) , std : : ios : : app ) ;
}
}
2018-12-21 00:38:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : logAllocation ( GraphicsAllocation const * graphicsAllocation ) {
if ( false = = debugLoggingAvailable ( ) ) {
return ;
}
if ( flags . LogAllocationMemoryPool . get ( ) ) {
std : : thread : : id thisThread = std : : this_thread : : get_id ( ) ;
std : : stringstream ss ;
ss < < " ThreadID: " < < thisThread ;
ss < < " AllocationType: " < < getAllocationTypeString ( graphicsAllocation ) ;
ss < < " MemoryPool: " < < graphicsAllocation - > getMemoryPool ( ) ;
ss < < graphicsAllocation - > getAllocationInfoString ( ) ;
ss < < std : : endl ;
auto str = ss . str ( ) ;
std : : unique_lock < std : : mutex > theLock ( mtx ) ;
writeToFile ( logFileName , str . c_str ( ) , str . size ( ) , std : : ios : : app ) ;
}
}
2017-12-21 07:45:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
size_t DebugSettingsManager < DebugLevel > : : getInput ( const size_t * input , int32_t index ) {
if ( debugLoggingAvailable ( ) = = false )
return 0 ;
return input ! = nullptr ? input [ index ] : 0 ;
}
template < DebugFunctionalityLevel DebugLevel >
const std : : string DebugSettingsManager < DebugLevel > : : getEvents ( const uintptr_t * input , uint32_t numOfEvents ) {
if ( false = = debugLoggingAvailable ( ) ) {
return " " ;
}
std : : stringstream os ;
for ( uint32_t i = 0 ; i < numOfEvents ; i + + ) {
if ( input ! = nullptr ) {
cl_event event = ( ( cl_event * ) input ) [ i ] ;
os < < " cl_event " < < event < < " , Event " < < ( Event * ) event < < " , " ;
}
}
return os . str ( ) ;
}
2018-08-14 16:23:10 +08:00
template < DebugFunctionalityLevel DebugLevel >
const std : : string DebugSettingsManager < DebugLevel > : : getMemObjects ( const uintptr_t * input , uint32_t numOfObjects ) {
if ( false = = debugLoggingAvailable ( ) ) {
return " " ;
}
std : : stringstream os ;
for ( uint32_t i = 0 ; i < numOfObjects ; i + + ) {
if ( input ! = nullptr ) {
cl_mem mem = const_cast < cl_mem > ( reinterpret_cast < const cl_mem * > ( input ) [ i ] ) ;
os < < " cl_mem " < < mem < < " , MemObj " < < static_cast < MemObj * > ( mem ) < < " , " ;
}
}
return os . str ( ) ;
}
2019-04-01 17:25:47 +08:00
template < DebugFunctionalityLevel DebugLevel >
template < typename DataType >
void DebugSettingsManager < DebugLevel > : : dumpNonDefaultFlag ( const char * variableName , const DataType & variableValue , const DataType & defaultValue ) {
if ( variableValue ! = defaultValue ) {
2019-04-08 18:45:16 +08:00
const auto variableStringValue = std : : to_string ( variableValue ) ;
printDebugString ( DebugManager . flags . PrintDebugMessages . get ( ) , stdout , " Non-default value of debug variable: %s = %s \n " , variableName , variableStringValue . c_str ( ) ) ;
2019-04-01 17:25:47 +08:00
}
}
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : dumpFlags ( ) const {
if ( flags . PrintDebugSettings . get ( ) = = false ) {
return ;
}
std : : ofstream settingsDumpFile { settingsDumpFileName , std : : ios : : out } ;
DEBUG_BREAK_IF ( ! settingsDumpFile . good ( ) ) ;
# define DECLARE_DEBUG_VARIABLE(dataType, variableName, defaultValue, description) \
settingsDumpFile < < # variableName < < " = " < < flags . variableName . get ( ) < < ' \n ' ; \
dumpNonDefaultFlag ( # variableName , flags . variableName . get ( ) , defaultValue ) ;
# include "debug_variables.inl"
# undef DECLARE_DEBUG_VARIABLE
2019-04-05 14:51:01 +08:00
}
2019-04-01 17:25:47 +08:00
2017-12-21 07:45:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : dumpBinaryProgram ( int32_t numDevices , const size_t * lengths , const unsigned char * * binaries ) {
if ( false = = debugKernelDumpingAvailable ( ) ) {
return ;
}
if ( flags . DumpKernels . get ( ) ) {
if ( lengths ! = nullptr & & binaries ! = nullptr & &
lengths [ 0 ] ! = 0 & & binaries [ 0 ] ! = nullptr ) {
std : : unique_lock < std : : mutex > theLock ( mtx ) ;
writeToFile ( " programBinary.bin " , reinterpret_cast < const char * > ( binaries [ 0 ] ) , lengths [ 0 ] , std : : ios : : trunc | std : : ios : : binary ) ;
}
}
}
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : dumpKernelArgs ( const Kernel * kernel ) {
if ( false = = kernelArgDumpingAvailable ( ) ) {
return ;
}
if ( flags . DumpKernelArgs . get ( ) & & kernel ! = nullptr ) {
std : : unique_lock < std : : mutex > theLock ( mtx ) ;
std : : ofstream outFile ;
for ( unsigned int i = 0 ; i < kernel - > getKernelInfo ( ) . kernelArgInfo . size ( ) ; i + + ) {
std : : string type ;
std : : string fileName ;
const char * ptr = nullptr ;
size_t size = 0 ;
uint64_t flags = 0 ;
std : : unique_ptr < char [ ] > argVal = nullptr ;
auto & argInfo = kernel - > getKernelInfo ( ) . kernelArgInfo [ i ] ;
if ( argInfo . addressQualifier = = CL_KERNEL_ARG_ADDRESS_LOCAL ) {
type = " local " ;
} else if ( argInfo . typeStr . find ( " * " ) ! = std : : string : : npos ) {
type = " buffer " ;
auto clMem = ( const cl_mem ) kernel - > getKernelArg ( i ) ;
auto memObj = castToObject < MemObj > ( clMem ) ;
if ( memObj ! = nullptr ) {
ptr = static_cast < char * > ( memObj - > getCpuAddress ( ) ) ;
size = memObj - > getSize ( ) ;
flags = memObj - > getFlags ( ) ;
}
} else if ( argInfo . typeStr . find ( " image " ) ! = std : : string : : npos ) {
type = " image " ;
auto clMem = ( const cl_mem ) kernel - > getKernelArg ( i ) ;
auto memObj = castToObject < MemObj > ( clMem ) ;
if ( memObj ! = nullptr ) {
ptr = static_cast < char * > ( memObj - > getCpuAddress ( ) ) ;
size = memObj - > getSize ( ) ;
flags = memObj - > getFlags ( ) ;
}
} else if ( argInfo . typeStr . find ( " sampler " ) ! = std : : string : : npos ) {
type = " sampler " ;
} else {
type = " immediate " ;
auto crossThreadData = kernel - > getCrossThreadData ( ) ;
auto crossThreadDataSize = kernel - > getCrossThreadDataSize ( ) ;
argVal = std : : unique_ptr < char [ ] > ( new char [ crossThreadDataSize ] ) ;
size_t totalArgSize = 0 ;
for ( const auto & kernelArgPatchInfo : argInfo . kernelArgPatchInfoVector ) {
auto pSource = ptrOffset ( crossThreadData , kernelArgPatchInfo . crossthreadOffset ) ;
auto pDestination = ptrOffset ( argVal . get ( ) , kernelArgPatchInfo . sourceOffset ) ;
memcpy_s ( pDestination , kernelArgPatchInfo . size , pSource , kernelArgPatchInfo . size ) ;
totalArgSize + = kernelArgPatchInfo . size ;
}
size = totalArgSize ;
ptr = argVal . get ( ) ;
}
if ( ptr & & size ) {
fileName = kernel - > getKernelInfo ( ) . name + " _arg_ " + std : : to_string ( i ) + " _ " + type + " _size_ " + std : : to_string ( size ) + " _flags_ " + std : : to_string ( flags ) + " .bin " ;
writeToFile ( fileName , ptr , size , std : : ios : : trunc | std : : ios : : binary ) ;
}
}
}
}
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : dumpKernelArgs ( const MultiDispatchInfo * multiDispatchInfo ) {
if ( kernelArgDumpingAvailable ( ) = = false ) {
return ;
}
2019-02-28 18:21:01 +08:00
if ( flags . DumpKernelArgs . get ( ) = = false | | multiDispatchInfo = = nullptr ) {
2017-12-21 07:45:38 +08:00
return ;
}
for ( auto & dispatchInfo : * multiDispatchInfo ) {
dumpKernelArgs ( dispatchInfo . getKernel ( ) ) ;
}
}
2019-04-01 17:25:47 +08:00
2018-12-13 20:46:32 +08:00
template < DebugFunctionalityLevel DebugLevel >
void DebugSettingsManager < DebugLevel > : : injectSettingsFromReader ( ) {
# undef DECLARE_DEBUG_VARIABLE
# define DECLARE_DEBUG_VARIABLE(dataType, variableName, defaultValue, description) \
{ \
dataType tempData = readerImpl - > getSetting ( # variableName , flags . variableName . get ( ) ) ; \
flags . variableName . set ( tempData ) ; \
}
# include "debug_variables.inl"
2019-01-19 08:40:42 +08:00
# undef DECLARE_DEBUG_VARIABLE
2018-12-13 20:46:32 +08:00
}
2017-12-21 07:45:38 +08:00
2018-12-21 00:38:38 +08:00
template < DebugFunctionalityLevel DebugLevel >
const char * DebugSettingsManager < DebugLevel > : : getAllocationTypeString ( GraphicsAllocation const * graphicsAllocation ) {
if ( false = = debugLoggingAvailable ( ) ) {
return nullptr ;
}
auto type = graphicsAllocation - > getAllocationType ( ) ;
switch ( type ) {
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : BUFFER :
return " BUFFER " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : BUFFER_COMPRESSED :
2018-12-21 00:38:38 +08:00
return " BUFFER_COMPRESSED " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : BUFFER_HOST_MEMORY :
2018-12-21 00:38:38 +08:00
return " BUFFER_HOST_MEMORY " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : COMMAND_BUFFER :
return " COMMAND_BUFFER " ;
case GraphicsAllocation : : AllocationType : : CONSTANT_SURFACE :
return " CONSTANT_SURFACE " ;
case GraphicsAllocation : : AllocationType : : EXTERNAL_HOST_PTR :
return " EXTERNAL_HOST_PTR " ;
case GraphicsAllocation : : AllocationType : : FILL_PATTERN :
return " FILL_PATTERN " ;
case GraphicsAllocation : : AllocationType : : GLOBAL_SURFACE :
return " GLOBAL_SURFACE " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : IMAGE :
2018-12-21 00:38:38 +08:00
return " IMAGE " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : INDIRECT_OBJECT_HEAP :
return " INDIRECT_OBJECT_HEAP " ;
case GraphicsAllocation : : AllocationType : : INSTRUCTION_HEAP :
return " INSTRUCTION_HEAP " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : LINEAR_STREAM :
2018-12-21 00:38:38 +08:00
return " LINEAR_STREAM " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : PIPE :
2018-12-21 00:38:38 +08:00
return " PIPE " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : PRINTF_SURFACE :
2018-12-21 00:38:38 +08:00
return " PRINTF_SURFACE " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : PRIVATE_SURFACE :
2018-12-21 00:38:38 +08:00
return " PRIVATE_SURFACE " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : PROFILING_TAG_BUFFER :
return " PROFILING_TAG_BUFFER " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : SCRATCH_SURFACE :
2018-12-21 00:38:38 +08:00
return " SCRATCH_SURFACE " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : SHARED_RESOURCE_COPY :
2019-01-28 20:59:37 +08:00
return " SHARED_RESOURCE_COPY " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : SURFACE_STATE_HEAP :
return " SURFACE_STATE_HEAP " ;
2019-03-06 23:35:21 +08:00
case GraphicsAllocation : : AllocationType : : SVM_CPU :
return " SVM_CPU " ;
case GraphicsAllocation : : AllocationType : : SVM_GPU :
return " SVM_GPU " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : SVM_ZERO_COPY :
return " SVM_ZERO_COPY " ;
case GraphicsAllocation : : AllocationType : : TAG_BUFFER :
return " TAG_BUFFER " ;
case GraphicsAllocation : : AllocationType : : TIMESTAMP_PACKET_TAG_BUFFER :
return " TIMESTAMP_PACKET_TAG_BUFFER " ;
2019-02-28 18:21:01 +08:00
case GraphicsAllocation : : AllocationType : : UNDECIDED :
2018-12-21 00:38:38 +08:00
return " UNDECIDED " ;
2019-04-15 17:00:21 +08:00
case GraphicsAllocation : : AllocationType : : UNKNOWN :
return " UNKNOWN " ;
2018-12-21 00:38:38 +08:00
default :
return " ILLEGAL_VALUE " ;
}
}
2017-12-21 07:45:38 +08:00
template class DebugSettingsManager < DebugFunctionalityLevel : : None > ;
template class DebugSettingsManager < DebugFunctionalityLevel : : Full > ;
template class DebugSettingsManager < DebugFunctionalityLevel : : RegKeys > ;
2019-03-26 18:59:46 +08:00
} ; // namespace NEO