From 4b29b30db18a0d055e18770934493bf57582e849 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Thu, 23 Aug 2018 17:53:58 +0200 Subject: [PATCH] Add OsContext class in shared code Change-Id: If5aea2126abe1b892068af9ca53e7f448e5b85a6 --- runtime/os_interface/CMakeLists.txt | 1 + runtime/os_interface/linux/CMakeLists.txt | 5 ++- .../os_interface/linux/os_context_linux.cpp | 32 ++++++++++++++ runtime/os_interface/os_context.h | 39 ++++++++++++++++ .../os_interface/windows/os_context_win.cpp | 25 ++++++++--- runtime/os_interface/windows/os_context_win.h | 12 ++--- runtime/os_interface/windows/wddm/wddm.h | 4 +- .../windows/wddm/wddm_interface.h | 5 ++- unit_tests/os_interface/linux/CMakeLists.txt | 1 + .../linux/os_context_linux_tests.cpp | 34 ++++++++++++++ .../linux/os_interface_linux_tests.cpp | 1 - .../os_interface/windows/CMakeLists.txt | 1 + .../windows/os_context_win_tests.cpp | 44 +++++++++++++++++++ 13 files changed, 189 insertions(+), 15 deletions(-) create mode 100644 runtime/os_interface/linux/os_context_linux.cpp create mode 100644 runtime/os_interface/os_context.h create mode 100644 unit_tests/os_interface/linux/os_context_linux_tests.cpp create mode 100644 unit_tests/os_interface/windows/os_context_win_tests.cpp diff --git a/runtime/os_interface/CMakeLists.txt b/runtime/os_interface/CMakeLists.txt index 55e1e434ac..868c19743e 100644 --- a/runtime/os_interface/CMakeLists.txt +++ b/runtime/os_interface/CMakeLists.txt @@ -26,6 +26,7 @@ set(RUNTIME_SRCS_OS_INTERFACE_BASE ${CMAKE_CURRENT_SOURCE_DIR}/debug_settings_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/debug_settings_manager.h ${CMAKE_CURRENT_SOURCE_DIR}/device_factory.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_context.h ${CMAKE_CURRENT_SOURCE_DIR}/os_inc_base.h ${CMAKE_CURRENT_SOURCE_DIR}/os_interface.h ${CMAKE_CURRENT_SOURCE_DIR}/os_library.h diff --git a/runtime/os_interface/linux/CMakeLists.txt b/runtime/os_interface/linux/CMakeLists.txt index 343a2c96eb..b09653e7f3 100644 --- a/runtime/os_interface/linux/CMakeLists.txt +++ b/runtime/os_interface/linux/CMakeLists.txt @@ -45,13 +45,14 @@ set(RUNTIME_SRCS_OS_INTERFACE_LINUX ${CMAKE_CURRENT_SOURCE_DIR}/drm_null_device.h ${CMAKE_CURRENT_SOURCE_DIR}/hw_info_config.cpp ${CMAKE_CURRENT_SOURCE_DIR}/linux_inc.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/os_thread_linux.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/os_thread_linux.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_context_linux.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_inc.h ${CMAKE_CURRENT_SOURCE_DIR}/os_interface.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_interface.h ${CMAKE_CURRENT_SOURCE_DIR}/os_library.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_library.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_thread_linux.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/os_thread_linux.h ${CMAKE_CURRENT_SOURCE_DIR}/os_time_linux.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_time_linux.h ${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_linux.cpp diff --git a/runtime/os_interface/linux/os_context_linux.cpp b/runtime/os_interface/linux/os_context_linux.cpp new file mode 100644 index 0000000000..1b10aa2606 --- /dev/null +++ b/runtime/os_interface/linux/os_context_linux.cpp @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2018, 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. +*/ + +#include "runtime/os_interface/os_context.h" + +namespace OCLRT { +class OsContext::OsContextImpl {}; +OsContext::OsContext(OSInterface &osInterface) { + osContextImpl = std::make_unique(); +} + +OsContext::~OsContext() = default; +} // namespace OCLRT diff --git a/runtime/os_interface/os_context.h b/runtime/os_interface/os_context.h new file mode 100644 index 0000000000..73297f1f42 --- /dev/null +++ b/runtime/os_interface/os_context.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018, 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. + */ + +#pragma once +#include +namespace OCLRT { +class OSInterface; +class OsContext { + public: + class OsContextImpl; + OsContext(OSInterface &osInterface); + ~OsContext(); + OsContextImpl *get() const { + return osContextImpl.get(); + }; + + protected: + std::unique_ptr osContextImpl; +}; +} // namespace OCLRT diff --git a/runtime/os_interface/windows/os_context_win.cpp b/runtime/os_interface/windows/os_context_win.cpp index bd05388eca..1df0005467 100644 --- a/runtime/os_interface/windows/os_context_win.cpp +++ b/runtime/os_interface/windows/os_context_win.cpp @@ -23,20 +23,29 @@ #include "runtime/os_interface/windows/os_context_win.h" #include "runtime/os_interface/windows/wddm/wddm.h" #include "runtime/os_interface/windows/wddm/wddm_interface.h" +#include "runtime/os_interface/windows/os_interface.h" namespace OCLRT { -OsContextWin::OsContextWin(Wddm &wddm) : wddm(wddm) { + +OsContextWin::OsContextImpl(Wddm &wddm) : wddm(wddm) { auto wddmInterface = wddm.getWddmInterface(); - if (!wddm.createContext(context)) + if (!wddmInterface) { return; + } + if (!wddm.createContext(context)) { + return; + } if (wddmInterface->hwQueuesSupported()) { - if (!wddmInterface->createHwQueue(wddm.getPreemptionMode(), *this)) + if (!wddmInterface->createHwQueue(wddm.getPreemptionMode(), *this)) { return; + } } initialized = wddmInterface->createMonitoredFence(*this); }; -OsContextWin::~OsContextWin() { - wddm.getWddmInterface()->destroyHwQueue(hwQueueHandle); +OsContextWin::~OsContextImpl() { + if (wddm.getWddmInterface()) { + wddm.getWddmInterface()->destroyHwQueue(hwQueueHandle); + } wddm.destroyContext(context); } @@ -47,4 +56,10 @@ void OsContextWin::resetMonitoredFenceParams(D3DKMT_HANDLE &handle, uint64_t *cp monitoredFence.cpuAddress = cpuAddress; monitoredFence.gpuAddress = gpuAddress; } + +OsContext::OsContext(OSInterface &osInterface) { + osContextImpl = std::make_unique(*osInterface.get()->getWddm()); +} +OsContext::~OsContext() = default; + } // namespace OCLRT diff --git a/runtime/os_interface/windows/os_context_win.h b/runtime/os_interface/windows/os_context_win.h index 8e852ebc0f..2c0d9c7fcd 100644 --- a/runtime/os_interface/windows/os_context_win.h +++ b/runtime/os_interface/windows/os_context_win.h @@ -21,18 +21,20 @@ */ #pragma once +#include "runtime/os_interface/os_context.h" #include "runtime/os_interface/windows/windows_wrapper.h" #include "runtime/os_interface/windows/windows_defs.h" #include namespace OCLRT { -class Wddm; -class OsContextWin { +class Wddm; +using OsContextWin = OsContext::OsContextImpl; +class OsContext::OsContextImpl { public: - OsContextWin() = delete; - OsContextWin(Wddm &wddm); - ~OsContextWin(); + OsContextImpl() = delete; + OsContextImpl(Wddm &wddm); + ~OsContextImpl(); D3DKMT_HANDLE getContext() const { return context; } diff --git a/runtime/os_interface/windows/wddm/wddm.h b/runtime/os_interface/windows/wddm/wddm.h index e38727a536..5625277680 100644 --- a/runtime/os_interface/windows/wddm/wddm.h +++ b/runtime/os_interface/windows/wddm/wddm.h @@ -21,6 +21,7 @@ */ #pragma once +#include "runtime/os_interface/os_context.h" #include "runtime/os_interface/windows/windows_wrapper.h" #include "runtime/os_interface/windows/windows_defs.h" #include "runtime/os_interface/windows/wddm/wddm_interface.h" @@ -45,11 +46,12 @@ class Gdi; class Gmm; class LinearStream; class GmmPageTableMngr; -class OsContextWin; struct FeatureTable; struct WorkaroundTable; struct KmDafListener; +using OsContextWin = OsContext::OsContextImpl; + enum class WddmInterfaceVersion { Wddm20 = 20, Wddm23 = 23, diff --git a/runtime/os_interface/windows/wddm/wddm_interface.h b/runtime/os_interface/windows/wddm/wddm_interface.h index bffe9a6faf..2aae5928ea 100644 --- a/runtime/os_interface/windows/wddm/wddm_interface.h +++ b/runtime/os_interface/windows/wddm/wddm_interface.h @@ -20,6 +20,7 @@ * OTHER DEALINGS IN THE SOFTWARE. */ #pragma once +#include "runtime/os_interface/os_context.h" #include "runtime/os_interface/windows/windows_wrapper.h" #include #include @@ -27,8 +28,10 @@ namespace OCLRT { class Gdi; -class OsContextWin; class Wddm; + +using OsContextWin = OsContext::OsContextImpl; + class WddmInterface { public: WddmInterface(Wddm &wddm) : wddm(wddm){}; diff --git a/unit_tests/os_interface/linux/CMakeLists.txt b/unit_tests/os_interface/linux/CMakeLists.txt index 57f62e76e3..e01bdbfc48 100644 --- a/unit_tests/os_interface/linux/CMakeLists.txt +++ b/unit_tests/os_interface/linux/CMakeLists.txt @@ -40,6 +40,7 @@ set(IGDRCL_SRCS_tests_os_interface_linux ${CMAKE_CURRENT_SOURCE_DIR}/mock_os_time_linux.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_performance_counters_linux.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_performance_counters_linux.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_context_linux_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_interface_linux_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_time_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/performance_counters_linux_tests.cpp diff --git a/unit_tests/os_interface/linux/os_context_linux_tests.cpp b/unit_tests/os_interface/linux/os_context_linux_tests.cpp new file mode 100644 index 0000000000..a78f23b9d4 --- /dev/null +++ b/unit_tests/os_interface/linux/os_context_linux_tests.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018, 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. + */ + +#include "runtime/os_interface/os_context.h" +#include "runtime/os_interface/os_interface.h" +#include "gtest/gtest.h" + +namespace OCLRT { + +TEST(OsContextTest, WhenOsContextIsCreatedThenImplIsAvailable) { + OSInterface osInterface; + auto osContext2 = std::make_unique(osInterface); + EXPECT_NE(nullptr, osContext2->get()); +} +} // namespace OCLRT diff --git a/unit_tests/os_interface/linux/os_interface_linux_tests.cpp b/unit_tests/os_interface/linux/os_interface_linux_tests.cpp index a4c1004b7f..1ce2d3cd7c 100644 --- a/unit_tests/os_interface/linux/os_interface_linux_tests.cpp +++ b/unit_tests/os_interface/linux/os_interface_linux_tests.cpp @@ -1,4 +1,3 @@ - /* * Copyright (c) 2017 - 2018, Intel Corporation * diff --git a/unit_tests/os_interface/windows/CMakeLists.txt b/unit_tests/os_interface/windows/CMakeLists.txt index b74f4c028a..476f9709c5 100644 --- a/unit_tests/os_interface/windows/CMakeLists.txt +++ b/unit_tests/os_interface/windows/CMakeLists.txt @@ -35,6 +35,7 @@ set(IGDRCL_SRCS_tests_os_interface_windows ${CMAKE_CURRENT_SOURCE_DIR}/mock_performance_counters_win.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_performance_counters_win.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_wddm_memory_manager.h + ${CMAKE_CURRENT_SOURCE_DIR}/os_context_win_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_interface_win_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/os_interface_win_tests.h ${CMAKE_CURRENT_SOURCE_DIR}/os_library_win_tests.cpp diff --git a/unit_tests/os_interface/windows/os_context_win_tests.cpp b/unit_tests/os_interface/windows/os_context_win_tests.cpp new file mode 100644 index 0000000000..907efcd9c0 --- /dev/null +++ b/unit_tests/os_interface/windows/os_context_win_tests.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018, 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. + */ + +#include "unit_tests/os_interface/windows/wddm_fixture.h" +#include "runtime/os_interface/windows/os_context_win.h" +#include "runtime/os_interface/windows/os_interface.h" + +TEST(OsContextTest, givenWddmWhenCreateOsContextBeforeInitWddmThenOsContextIsNotInitialized) { + auto wddm = new WddmMock; + OSInterface osInterface; + osInterface.get()->setWddm(wddm); + auto osContext = std::make_unique(osInterface); + EXPECT_NE(nullptr, osContext->get()); + EXPECT_FALSE(osContext->get()->isInitialized()); +} + +TEST(OsContextTest, givenWddmWhenCreateOsContextAfterInitWddmThenOsContextIsInitialized) { + auto wddm = new WddmMock; + OSInterface osInterface; + osInterface.get()->setWddm(wddm); + wddm->init(); + auto osContext = std::make_unique(osInterface); + EXPECT_NE(nullptr, osContext->get()); + EXPECT_TRUE(osContext->get()->isInitialized()); +}