2018-06-22 12:54:33 +02:00
/*
2018-09-18 09:11:08 +02:00
* Copyright ( C ) 2018 Intel Corporation
*
* SPDX - License - Identifier : MIT
*
*/
2018-06-22 12:54:33 +02:00
2018-11-10 22:25:48 +01:00
# include "runtime/aub/aub_center.h"
2018-08-22 10:29:37 +02:00
# include "runtime/built_ins/built_ins.h"
# include "runtime/compiler_interface/compiler_interface.h"
2018-06-27 11:35:37 +02:00
# include "runtime/device/device.h"
2018-06-22 12:54:33 +02:00
# include "runtime/execution_environment/execution_environment.h"
2018-07-16 17:11:43 +02:00
# include "runtime/gmm_helper/gmm_helper.h"
2018-07-13 07:42:18 +02:00
# include "runtime/helpers/options.h"
2018-07-16 17:11:43 +02:00
# include "runtime/memory_manager/os_agnostic_memory_manager.h"
2018-08-10 11:07:17 +02:00
# include "runtime/os_interface/os_interface.h"
2018-06-22 12:54:33 +02:00
# include "runtime/platform/platform.h"
2018-08-22 10:29:37 +02:00
# include "runtime/source_level_debugger/source_level_debugger.h"
2018-07-16 17:11:43 +02:00
# include "test.h"
2018-07-16 13:01:10 +02:00
# include "unit_tests/mocks/mock_csr.h"
2018-10-12 15:20:02 +02:00
# include "unit_tests/mocks/mock_device.h"
2018-11-25 14:58:12 -08:00
# include "unit_tests/mocks/mock_execution_environment.h"
2018-10-01 16:10:54 +02:00
# include "unit_tests/mocks/mock_memory_manager.h"
2018-08-22 10:29:37 +02:00
# include "unit_tests/utilities/destructor_counted.h"
2018-12-10 17:12:32 +01:00
# include "unit_tests/helpers/debug_manager_state_restore.h"
2018-10-02 10:10:29 -07:00
# include "unit_tests/helpers/unit_test_helper.h"
2018-06-22 12:54:33 +02:00
using namespace OCLRT ;
TEST ( ExecutionEnvironment , givenDefaultConstructorWhenItIsCalledThenExecutionEnvironmentHasInitialRefCountZero ) {
ExecutionEnvironment environment ;
EXPECT_EQ ( 0 , environment . getRefInternalCount ( ) ) ;
EXPECT_EQ ( 0 , environment . getRefApiCount ( ) ) ;
}
TEST ( ExecutionEnvironment , givenPlatformWhenItIsConstructedThenItCretesExecutionEnvironmentWithOneRefCountInternal ) {
std : : unique_ptr < Platform > platform ( new Platform ) ;
ASSERT_NE ( nullptr , platform - > peekExecutionEnvironment ( ) ) ;
EXPECT_EQ ( 1 , platform - > peekExecutionEnvironment ( ) - > getRefInternalCount ( ) ) ;
}
TEST ( ExecutionEnvironment , givenPlatformAndExecutionEnvironmentWithRefCountsWhenPlatformIsDestroyedThenExecutionEnvironmentIsNotDeleted ) {
std : : unique_ptr < Platform > platform ( new Platform ) ;
auto executionEnvironment = platform - > peekExecutionEnvironment ( ) ;
executionEnvironment - > incRefInternal ( ) ;
platform . reset ( ) ;
EXPECT_EQ ( 1 , executionEnvironment - > getRefInternalCount ( ) ) ;
executionEnvironment - > decRefInternal ( ) ;
}
2018-06-27 11:35:37 +02:00
TEST ( ExecutionEnvironment , givenPlatformWhenItIsInitializedAndCreatesDevicesThenThoseDevicesAddRefcountsToExecutionEnvironment ) {
std : : unique_ptr < Platform > platform ( new Platform ) ;
auto executionEnvironment = platform - > peekExecutionEnvironment ( ) ;
platform - > initialize ( ) ;
EXPECT_LT ( 0u , platform - > getNumDevices ( ) ) ;
EXPECT_EQ ( static_cast < int32_t > ( 1u + platform - > getNumDevices ( ) ) , executionEnvironment - > getRefInternalCount ( ) ) ;
}
TEST ( ExecutionEnvironment , givenDeviceThatHaveRefferencesAfterPlatformIsDestroyedThenDeviceIsStillUsable ) {
std : : unique_ptr < Platform > platform ( new Platform ) ;
auto executionEnvironment = platform - > peekExecutionEnvironment ( ) ;
platform - > initialize ( ) ;
auto device = platform - > getDevice ( 0 ) ;
EXPECT_EQ ( 1 , device - > getRefInternalCount ( ) ) ;
device - > incRefInternal ( ) ;
platform . reset ( nullptr ) ;
EXPECT_EQ ( 1 , device - > getRefInternalCount ( ) ) ;
EXPECT_EQ ( 1 , executionEnvironment - > getRefInternalCount ( ) ) ;
device - > decRefInternal ( ) ;
2018-07-11 14:16:35 +02:00
}
TEST ( ExecutionEnvironment , givenPlatformWhenItIsCreatedThenItCreatesCommandStreamReceiverInExecutionEnvironment ) {
Platform platform ;
auto executionEnvironment = platform . peekExecutionEnvironment ( ) ;
platform . initialize ( ) ;
2018-11-20 13:58:15 +01:00
EXPECT_NE ( nullptr , executionEnvironment - > commandStreamReceivers [ 0 ] [ 0 ] . get ( ) ) ;
2018-07-11 16:47:49 +02:00
}
TEST ( ExecutionEnvironment , givenPlatformWhenItIsCreatedThenItCreatesMemoryManagerInExecutionEnvironment ) {
Platform platform ;
auto executionEnvironment = platform . peekExecutionEnvironment ( ) ;
platform . initialize ( ) ;
EXPECT_NE ( nullptr , executionEnvironment - > memoryManager ) ;
2018-07-12 08:07:23 +02:00
}
TEST ( ExecutionEnvironment , givenDeviceWhenItIsDestroyedThenMemoryManagerIsStillAvailable ) {
std : : unique_ptr < ExecutionEnvironment > executionEnvironment ( new ExecutionEnvironment ) ;
executionEnvironment - > incRefInternal ( ) ;
2018-09-11 11:28:19 +02:00
std : : unique_ptr < Device > device ( Device : : create < OCLRT : : Device > ( nullptr , executionEnvironment . get ( ) , 0u ) ) ;
2018-07-12 08:07:23 +02:00
device . reset ( nullptr ) ;
EXPECT_NE ( nullptr , executionEnvironment - > memoryManager ) ;
2018-07-13 07:42:18 +02:00
}
2018-07-16 17:11:43 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeCommandStreamReceiverIsCalledThenItIsInitalized ) {
std : : unique_ptr < ExecutionEnvironment > executionEnvironment ( new ExecutionEnvironment ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 0 ) ;
EXPECT_NE ( nullptr , executionEnvironment - > commandStreamReceivers [ 0 ] [ 0 ] ) ;
2018-07-16 17:11:43 +02:00
}
2018-09-11 15:55:04 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeIsCalledWithDifferentDeviceIndexesThenInternalStorageIsResized ) {
2018-07-16 17:11:43 +02:00
std : : unique_ptr < ExecutionEnvironment > executionEnvironment ( new ExecutionEnvironment ) ;
2018-09-11 15:55:04 +02:00
EXPECT_EQ ( 0u , executionEnvironment - > commandStreamReceivers . size ( ) ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 0 ) ;
2018-09-11 15:55:04 +02:00
EXPECT_EQ ( 1u , executionEnvironment - > commandStreamReceivers . size ( ) ) ;
2018-11-20 13:58:15 +01:00
EXPECT_NE ( nullptr , executionEnvironment - > commandStreamReceivers [ 0 ] [ 0 ] ) ;
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 1 , 0 ) ;
2018-09-11 15:55:04 +02:00
EXPECT_EQ ( 2u , executionEnvironment - > commandStreamReceivers . size ( ) ) ;
2018-11-20 13:58:15 +01:00
EXPECT_NE ( nullptr , executionEnvironment - > commandStreamReceivers [ 1 ] [ 0 ] ) ;
2018-09-11 15:55:04 +02:00
}
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeIsCalledMultipleTimesForTheSameIndexThenCommandStreamReceiverIsReused ) {
auto executionEnvironment = std : : make_unique < ExecutionEnvironment > ( ) ;
EXPECT_EQ ( 0u , executionEnvironment - > commandStreamReceivers . size ( ) ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 1 ) ;
2018-09-11 15:55:04 +02:00
2018-11-20 13:58:15 +01:00
auto currentCommandStreamReceiver = executionEnvironment - > commandStreamReceivers [ 0 ] [ 1 ] . get ( ) ;
2018-09-11 15:55:04 +02:00
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 1 ) ;
2018-09-11 15:55:04 +02:00
2018-11-20 13:58:15 +01:00
EXPECT_EQ ( currentCommandStreamReceiver , executionEnvironment - > commandStreamReceivers [ 0 ] [ 1 ] . get ( ) ) ;
2018-11-23 14:59:27 +01:00
EXPECT_EQ ( gpgpuEngineInstances . size ( ) , executionEnvironment - > commandStreamReceivers [ 0 ] . size ( ) ) ;
2018-11-20 13:58:15 +01:00
EXPECT_EQ ( nullptr , executionEnvironment - > commandStreamReceivers [ 0 ] [ 0 ] . get ( ) ) ;
2018-07-16 17:11:43 +02:00
}
2018-11-25 14:58:12 -08:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeAubCenterIsCalledThenItIsReceivesCorrectInputParams ) {
MockExecutionEnvironment executionEnvironment ;
executionEnvironment . initAubCenter ( platformDevices [ 0 ] , true , " test.aub " ) ;
EXPECT_TRUE ( executionEnvironment . initAubCenterCalled ) ;
EXPECT_TRUE ( executionEnvironment . localMemoryEnabledReceived ) ;
EXPECT_STREQ ( executionEnvironment . aubFileNameReceived . c_str ( ) , " test.aub " ) ;
}
2018-12-10 17:12:32 +01:00
TEST ( ExecutionEnvironment , givenUseAubStreamFalseWhenGetAubManagerIsCalledThenReturnNull ) {
DebugManagerStateRestore dbgRestore ;
DebugManager . flags . UseAubStream . set ( false ) ;
2018-11-26 18:17:57 -08:00
ExecutionEnvironment executionEnvironment ;
executionEnvironment . initAubCenter ( platformDevices [ 0 ] , false , " " ) ;
auto aubManager = executionEnvironment . aubCenter - > getAubManager ( ) ;
EXPECT_EQ ( nullptr , aubManager ) ;
}
2018-09-26 15:28:11 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeAubCenterIsCalledThenItIsInitalizedOnce ) {
2018-09-20 00:36:44 +02:00
ExecutionEnvironment executionEnvironment ;
2018-11-25 14:58:12 -08:00
executionEnvironment . initAubCenter ( platformDevices [ 0 ] , false , " " ) ;
2018-09-26 15:28:11 +02:00
auto currentAubCenter = executionEnvironment . aubCenter . get ( ) ;
EXPECT_NE ( nullptr , currentAubCenter ) ;
auto currentAubStreamProvider = currentAubCenter - > getStreamProvider ( ) ;
2018-09-20 00:36:44 +02:00
EXPECT_NE ( nullptr , currentAubStreamProvider ) ;
auto currentAubFileStream = currentAubStreamProvider - > getStream ( ) ;
EXPECT_NE ( nullptr , currentAubFileStream ) ;
2018-11-25 14:58:12 -08:00
executionEnvironment . initAubCenter ( platformDevices [ 0 ] , false , " " ) ;
2018-09-26 15:28:11 +02:00
EXPECT_EQ ( currentAubCenter , executionEnvironment . aubCenter . get ( ) ) ;
EXPECT_EQ ( currentAubStreamProvider , executionEnvironment . aubCenter - > getStreamProvider ( ) ) ;
EXPECT_EQ ( currentAubFileStream , executionEnvironment . aubCenter - > getStreamProvider ( ) - > getStream ( ) ) ;
2018-09-20 00:36:44 +02:00
}
2018-10-12 15:20:02 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeMemoryManagerIsCalledThenLocalMemorySupportedInMemoryManagerHasCorrectValue ) {
auto device = std : : unique_ptr < Device > ( MockDevice : : createWithNewExecutionEnvironment < MockDevice > ( platformDevices [ 0 ] ) ) ;
auto executionEnvironment = device - > getExecutionEnvironment ( ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 0 ) ;
executionEnvironment - > initializeMemoryManager ( false , device - > getEnableLocalMemory ( ) , 0 , 0 ) ;
2018-10-12 15:20:02 +02:00
EXPECT_EQ ( device - > getEnableLocalMemory ( ) , executionEnvironment - > memoryManager - > isLocalMemorySupported ( ) ) ;
}
2018-09-11 15:55:04 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWhenInitializeMemoryManagerIsCalledThenItIsInitalized ) {
auto executionEnvironment = std : : make_unique < ExecutionEnvironment > ( ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > initializeCommandStreamReceiver ( platformDevices [ 0 ] , 0 , 0 ) ;
executionEnvironment - > initializeMemoryManager ( false , false , 0 , 0 ) ;
2018-09-11 15:55:04 +02:00
EXPECT_NE ( nullptr , executionEnvironment - > memoryManager ) ;
}
2018-09-20 00:36:44 +02:00
static_assert ( sizeof ( ExecutionEnvironment ) = = sizeof ( std : : vector < std : : unique_ptr < CommandStreamReceiver > > ) + sizeof ( std : : mutex ) + ( is64bit ? 80 : 44 ) , " New members detected in ExecutionEnvironment, please ensure that destruction sequence of objects is correct " ) ;
2018-07-13 07:42:18 +02:00
TEST ( ExecutionEnvironment , givenExecutionEnvironmentWithVariousMembersWhenItIsDestroyedThenDeleteSequenceIsSpecified ) {
2018-08-22 10:29:37 +02:00
uint32_t destructorId = 0u ;
2018-08-01 09:06:46 +02:00
2018-08-22 10:29:37 +02:00
struct MockExecutionEnvironment : ExecutionEnvironment {
using ExecutionEnvironment : : gmmHelper ;
2018-07-13 07:42:18 +02:00
} ;
2018-09-20 00:36:44 +02:00
struct GmmHelperMock : public DestructorCounted < GmmHelper , 7 > {
2018-08-22 10:29:37 +02:00
GmmHelperMock ( uint32_t & destructorId , const HardwareInfo * hwInfo ) : DestructorCounted ( destructorId , hwInfo ) { }
2018-07-13 07:42:18 +02:00
} ;
2018-09-20 00:36:44 +02:00
struct OsInterfaceMock : public DestructorCounted < OSInterface , 6 > {
2018-08-22 10:29:37 +02:00
OsInterfaceMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId ) { }
2018-08-01 09:06:46 +02:00
} ;
2018-10-01 16:10:54 +02:00
struct MemoryMangerMock : public DestructorCounted < MockMemoryManager , 5 > {
2018-08-22 10:29:37 +02:00
MemoryMangerMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId ) { }
2018-08-21 15:47:21 +02:00
} ;
2018-09-26 15:28:11 +02:00
struct AubCenterMock : public DestructorCounted < AubCenter , 4 > {
2018-11-25 14:58:12 -08:00
AubCenterMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId , platformDevices [ 0 ] , false , " " ) { }
2018-09-20 00:36:44 +02:00
} ;
2018-08-22 10:29:37 +02:00
struct CommandStreamReceiverMock : public DestructorCounted < MockCommandStreamReceiver , 3 > {
2018-10-11 11:19:49 +02:00
CommandStreamReceiverMock ( uint32_t & destructorId , ExecutionEnvironment & executionEnvironment ) : DestructorCounted ( destructorId , executionEnvironment ) { }
2018-07-16 13:01:10 +02:00
} ;
2018-08-22 10:29:37 +02:00
struct BuiltinsMock : public DestructorCounted < BuiltIns , 2 > {
BuiltinsMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId ) { }
2018-07-12 15:47:48 +02:00
} ;
2018-08-22 10:29:37 +02:00
struct CompilerInterfaceMock : public DestructorCounted < CompilerInterface , 1 > {
CompilerInterfaceMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId ) { }
} ;
struct SourceLevelDebuggerMock : public DestructorCounted < SourceLevelDebugger , 0 > {
SourceLevelDebuggerMock ( uint32_t & destructorId ) : DestructorCounted ( destructorId , nullptr ) { }
2018-07-13 07:42:18 +02:00
} ;
2018-08-22 10:29:37 +02:00
auto executionEnvironment = std : : make_unique < MockExecutionEnvironment > ( ) ;
2018-11-20 13:58:15 +01:00
executionEnvironment - > commandStreamReceivers . resize ( 1 ) ;
2018-08-22 10:29:37 +02:00
executionEnvironment - > gmmHelper = std : : make_unique < GmmHelperMock > ( destructorId , platformDevices [ 0 ] ) ;
executionEnvironment - > osInterface = std : : make_unique < OsInterfaceMock > ( destructorId ) ;
executionEnvironment - > memoryManager = std : : make_unique < MemoryMangerMock > ( destructorId ) ;
2018-09-26 15:28:11 +02:00
executionEnvironment - > aubCenter = std : : make_unique < AubCenterMock > ( destructorId ) ;
2018-11-23 14:59:27 +01:00
executionEnvironment - > commandStreamReceivers [ 0 ] [ 0 ] = std : : make_unique < CommandStreamReceiverMock > ( destructorId , * executionEnvironment ) ;
2018-08-22 10:29:37 +02:00
executionEnvironment - > builtins = std : : make_unique < BuiltinsMock > ( destructorId ) ;
executionEnvironment - > compilerInterface = std : : make_unique < CompilerInterfaceMock > ( destructorId ) ;
executionEnvironment - > sourceLevelDebugger = std : : make_unique < SourceLevelDebuggerMock > ( destructorId ) ;
2018-07-13 07:42:18 +02:00
executionEnvironment . reset ( nullptr ) ;
2018-09-20 00:36:44 +02:00
EXPECT_EQ ( 8u , destructorId ) ;
2018-07-16 13:01:10 +02:00
}
2018-07-17 11:11:48 +02:00
TEST ( ExecutionEnvironment , givenMultipleDevicesWhenTheyAreCreatedTheyAllReuseTheSameMemoryManagerAndCommandStreamReceiver ) {
auto executionEnvironment = new ExecutionEnvironment ;
2018-11-22 13:57:10 +01:00
std : : unique_ptr < MockDevice > device ( Device : : create < OCLRT : : MockDevice > ( nullptr , executionEnvironment , 0u ) ) ;
2018-07-17 11:11:48 +02:00
auto & commandStreamReceiver = device - > getCommandStreamReceiver ( ) ;
auto memoryManager = device - > getMemoryManager ( ) ;
2018-11-22 13:57:10 +01:00
std : : unique_ptr < MockDevice > device2 ( Device : : create < OCLRT : : MockDevice > ( nullptr , executionEnvironment , 1u ) ) ;
2018-09-11 15:55:04 +02:00
EXPECT_NE ( & commandStreamReceiver , & device2 - > getCommandStreamReceiver ( ) ) ;
2018-07-17 11:11:48 +02:00
EXPECT_EQ ( memoryManager , device2 - > getMemoryManager ( ) ) ;
}
2018-08-17 13:38:09 +02:00
typedef : : testing : : Test ExecutionEnvironmentHw ;
2018-10-02 10:10:29 -07:00
HWTEST_F ( ExecutionEnvironmentHw , givenHwHelperInputWhenInitializingCsrThenCreatePageTableManagerIfAllowed ) {
2018-08-17 13:38:09 +02:00
HardwareInfo localHwInfo = * platformDevices [ 0 ] ;
2018-10-02 10:10:29 -07:00
localHwInfo . capabilityTable . ftrRenderCompressedBuffers = false ;
localHwInfo . capabilityTable . ftrRenderCompressedImages = false ;
2018-08-17 13:38:09 +02:00
ExecutionEnvironment executionEnvironment ;
2018-11-20 13:58:15 +01:00
executionEnvironment . initializeCommandStreamReceiver ( & localHwInfo , 0 , 0 ) ;
auto csr0 = static_cast < UltCommandStreamReceiver < FamilyType > * > ( executionEnvironment . commandStreamReceivers [ 0 ] [ 0 ] . get ( ) ) ;
2018-10-02 10:10:29 -07:00
EXPECT_FALSE ( csr0 - > createPageTableManagerCalled ) ;
localHwInfo . capabilityTable . ftrRenderCompressedBuffers = true ;
localHwInfo . capabilityTable . ftrRenderCompressedImages = false ;
2018-11-20 13:58:15 +01:00
executionEnvironment . initializeCommandStreamReceiver ( & localHwInfo , 1 , 0 ) ;
auto csr1 = static_cast < UltCommandStreamReceiver < FamilyType > * > ( executionEnvironment . commandStreamReceivers [ 1 ] [ 0 ] . get ( ) ) ;
2018-10-02 10:10:29 -07:00
EXPECT_EQ ( UnitTestHelper < FamilyType > : : isPageTableManagerSupported ( localHwInfo ) , csr1 - > createPageTableManagerCalled ) ;
localHwInfo . capabilityTable . ftrRenderCompressedBuffers = false ;
2018-08-17 13:38:09 +02:00
localHwInfo . capabilityTable . ftrRenderCompressedImages = true ;
2018-11-20 13:58:15 +01:00
executionEnvironment . initializeCommandStreamReceiver ( & localHwInfo , 2 , 0 ) ;
auto csr2 = static_cast < UltCommandStreamReceiver < FamilyType > * > ( executionEnvironment . commandStreamReceivers [ 2 ] [ 0 ] . get ( ) ) ;
2018-10-02 10:10:29 -07:00
EXPECT_EQ ( UnitTestHelper < FamilyType > : : isPageTableManagerSupported ( localHwInfo ) , csr2 - > createPageTableManagerCalled ) ;
2018-08-17 13:38:09 +02:00
}