Skip to content

Commit af4ddc7

Browse files
aarongreigkbenzie
authored andcommitted
Re-do docs and testing
1 parent 0a56569 commit af4ddc7

File tree

8 files changed

+29
-69
lines changed

8 files changed

+29
-69
lines changed

scripts/core/INTRO.rst

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,30 +258,26 @@ For more information about the usage of mentioned environment variables see `Env
258258

259259
Mocking
260260
---------------------
261-
A mock UR adapter can be accessed for test purposes by enabling the ``MOCK``
262-
layer as described below. When the mock layer is enabled, calls to the API will
263-
still be intercepted by other layers (e.g. validation, tracing), but they will
264-
stop short of the loader - the call chain will end in either a generic fallback
265-
behavior defined by the mock layer itself, or a user defined replacement
266-
callback.
267-
268-
The default fallback behavior for entry points in the mock layer is to simply
261+
A mock UR adapter can be accessed for test purposes by enabling it via
262+
${x}LoaderConfigSetMockingEnabled.
263+
264+
The default fallback behavior for entry points in the mock adapter is to simply
269265
return ``UR_RESULT_SUCCESS``. For entry points concerning handles, i.e. those
270266
that create a new handle or modify the reference count of an existing one, a
271-
dummy handle mechanism is used. This means the layer will return generic
267+
dummy handle mechanism is used. This means the adapter will return generic
272268
handles that track a reference count, and ``Retain``/``Release`` entry points will
273269
function as expected when used with these handles.
274270

275-
During global setup the behavior of the mock layer can be customized by setting
276-
chain of structs, with each registering a callback with a given entry point in
277-
the API. Callbacks can be registered to be called ``BEFORE`` or ``AFTER`` the
278-
generic implementation, or they can be registered to entirely ``REPLACE`` it. A
279-
given entry point can only have one of each kind of callback associated with
280-
it, multiple structs with the same function/mode combination will override
281-
eachother.
271+
The behavior of the mock adapter can be customized by linking the
272+
``unified-runtime::mock`` library and making use of the ``mock::callbacks``
273+
object. Callbacks can be passed into this object to run either before or after a
274+
given entry point, or they can be set to entirely replace the default behavior.
275+
Only one callback of each type (before, replace, after) can be set per entry
276+
point, with subsequent callbacks set in the same "slot" overwriting any set
277+
previously.
282278

283279
The callback signature defined by ``ur_mock_callback_t`` takes a single
284-
``void *`` parameter. When calling a user callback the layer will pack the
280+
``void *`` parameter. When calling a user callback the adapter will pack the
285281
entry point's parameters into the appropriate ``_params_t`` struct (e.g.
286282
``ur_adapter_get_params_t``) and pass a pointer to that struct into the
287283
callback. This allows parameters to be accessed and modified. The definitions
@@ -309,8 +305,6 @@ Layers currently included with the runtime are as follows:
309305
- Enables the XPTI tracing layer, see Tracing_ for more detail.
310306
* - UR_LAYER_ASAN \| UR_LAYER_MSAN \| UR_LAYER_TSAN
311307
- Enables the device-side sanitizer layer, see Sanitizers_ for more detail.
312-
* - UR_LAYER_MOCK
313-
- Enables adapter mocking for test purposes. Similar behavior to the null adapter except entry points can be overridden or instrumented with callbacks. See Mocking_ for more detail.
314308

315309
Environment Variables
316310
---------------------

scripts/templates/mockddi.cpp.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from templates import helper as th
99
X=x.upper()
1010
%>/*
1111
*
12-
* Copyright (C) 2019-2024 Intel Corporation
12+
* Copyright (C) 2024 Intel Corporation
1313
*
1414
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
1515
* See LICENSE.TXT

source/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ add_definitions(-DUR_VALIDATION_LAYER_SUPPORTED_VERSION="${PROJECT_VERSION_MAJOR
88

99
add_subdirectory(common)
1010
add_subdirectory(loader)
11-
#add_subdirectory(layers)
1211
add_subdirectory(mock)
1312
add_subdirectory(adapters)

source/loader/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ if(UR_ENABLE_SANITIZER)
159159
)
160160
endif()
161161

162+
162163
# link validation backtrace dependencies
163164
if(UNIX)
164165
find_package(Libbacktrace)

source/loader/ur_lib.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ urLoaderConfigSetCodeLocationCallback(ur_loader_config_handle_t hLoaderConfig,
225225
ur_result_t
226226
urLoaderConfigSetMockingEnabled(ur_loader_config_handle_t hLoaderConfig,
227227
ur_bool_t enable) {
228+
if (!hLoaderConfig) {
229+
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
230+
}
228231
hLoaderConfig->enableMock = enable;
229232
return UR_RESULT_SUCCESS;
230233
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_subdirectory(adapters)
2626
add_subdirectory(usm)
2727
add_subdirectory(layers)
2828
add_subdirectory(unit)
29+
add_subdirectory(mock)
2930
if(UR_BUILD_TOOLS)
3031
add_subdirectory(tools)
3132
endif()

test/layers/mock/CMakeLists.txt renamed to test/mock/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ target_link_libraries(${MOCK_TEST_NAME}
1111
${PROJECT_NAME}::loader
1212
${PROJECT_NAME}::headers
1313
${PROJECT_NAME}::testing
14+
${PROJECT_NAME}::mock
1415
GTest::gtest_main)
1516

1617
add_test(NAME ${MOCK_TEST_NAME}
1718
COMMAND ${MOCK_TEST_NAME}
1819
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
1920

2021
set_tests_properties(${MOCK_TEST_NAME} PROPERTIES LABELS "mock")
21-
22-
set_property(TEST ${MOCK_TEST_NAME} PROPERTY ENVIRONMENT
23-
"UR_ADAPTERS_FORCE_LOAD=\"$<TARGET_FILE:ur_adapter_mock>\"")

test/layers/mock/mock.cpp renamed to test/mock/mock.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,17 @@
1414
#include <gtest/gtest.h>
1515
#include <ur_api.h>
1616

17-
TEST(Mock, NullProperties) {
18-
uur::raii::LoaderConfig loader_config;
19-
ASSERT_EQ(urLoaderConfigCreate(loader_config.ptr()), UR_RESULT_SUCCESS);
20-
ASSERT_EQ(urLoaderConfigSetMockCallbacks(loader_config, nullptr),
21-
UR_RESULT_ERROR_INVALID_NULL_POINTER);
22-
}
23-
24-
TEST(Mock, NullCallback) {
25-
uur::raii::LoaderConfig loader_config;
26-
ASSERT_EQ(urLoaderConfigCreate(loader_config.ptr()), UR_RESULT_SUCCESS);
27-
28-
ur_mock_callback_properties_t callback_properties = {
29-
UR_STRUCTURE_TYPE_MOCK_CALLBACK_PROPERTIES, nullptr, "urAdapterGet",
30-
UR_CALLBACK_OVERRIDE_MODE_REPLACE, nullptr};
31-
32-
ASSERT_EQ(
33-
urLoaderConfigSetMockCallbacks(loader_config, &callback_properties),
34-
UR_RESULT_ERROR_INVALID_NULL_POINTER);
35-
}
36-
37-
ur_result_t generic_callback(void *) { return UR_RESULT_SUCCESS; }
17+
#include <ur_mock_helpers.hpp>
3818

3919
TEST(Mock, NullHandle) {
40-
ur_mock_callback_properties_t callback_properties = {
41-
UR_STRUCTURE_TYPE_MOCK_CALLBACK_PROPERTIES, nullptr, "urAdapterGet",
42-
UR_CALLBACK_OVERRIDE_MODE_REPLACE, &generic_callback};
43-
44-
ASSERT_EQ(urLoaderConfigSetMockCallbacks(nullptr, &callback_properties),
20+
ASSERT_EQ(urLoaderConfigSetMockingEnabled(nullptr, true),
4521
UR_RESULT_ERROR_INVALID_NULL_HANDLE);
4622
}
4723

4824
TEST(Mock, DefaultBehavior) {
4925
uur::raii::LoaderConfig loader_config;
5026
ASSERT_EQ(urLoaderConfigCreate(loader_config.ptr()), UR_RESULT_SUCCESS);
51-
ASSERT_EQ(urLoaderConfigEnableLayer(loader_config, "UR_LAYER_MOCK"),
27+
ASSERT_EQ(urLoaderConfigSetMockingEnabled(loader_config, true),
5228
UR_RESULT_SUCCESS);
5329
ASSERT_EQ(urLoaderInit(0, loader_config), UR_RESULT_SUCCESS);
5430

@@ -108,32 +84,20 @@ ur_result_t afterUrAdapterGet(void *pParams) {
10884
TEST(Mock, Callbacks) {
10985
uur::raii::LoaderConfig loader_config;
11086
ASSERT_EQ(urLoaderConfigCreate(loader_config.ptr()), UR_RESULT_SUCCESS);
87+
ASSERT_EQ(urLoaderConfigSetMockingEnabled(loader_config, true),
88+
UR_RESULT_SUCCESS);
89+
ASSERT_EQ(urLoaderInit(0, loader_config), UR_RESULT_SUCCESS);
11190

11291
// This callback is set up to check *phAdapters is still the pre-call
11392
// init value we set below
114-
ur_mock_callback_properties_t adapterGetBeforeProperties = {
115-
UR_STRUCTURE_TYPE_MOCK_CALLBACK_PROPERTIES, nullptr, "urAdapterGet",
116-
UR_CALLBACK_OVERRIDE_MODE_BEFORE, &beforeUrAdapterGet};
93+
mock::callbacks.set_before_callback("urAdapterGet", &beforeUrAdapterGet);
11794

11895
// This callback is set up to return a distinct test value in phAdapters
11996
// rather than the default generic handle
120-
ur_mock_callback_properties_t adapterGetReplaceProperties = {
121-
UR_STRUCTURE_TYPE_MOCK_CALLBACK_PROPERTIES, &adapterGetBeforeProperties,
122-
"urAdapterGet", UR_CALLBACK_OVERRIDE_MODE_REPLACE,
123-
&replaceUrAdapterGet};
97+
mock::callbacks.set_replace_callback("urAdapterGet", &replaceUrAdapterGet);
12498

12599
// This callback is set up to check our replace callback did its job
126-
ur_mock_callback_properties_t adapterGetAfterProperties = {
127-
UR_STRUCTURE_TYPE_MOCK_CALLBACK_PROPERTIES,
128-
&adapterGetReplaceProperties, "urAdapterGet",
129-
UR_CALLBACK_OVERRIDE_MODE_AFTER, &afterUrAdapterGet};
130-
131-
ASSERT_EQ(urLoaderConfigSetMockCallbacks(loader_config,
132-
&adapterGetAfterProperties),
133-
UR_RESULT_SUCCESS);
134-
ASSERT_EQ(urLoaderConfigEnableLayer(loader_config, "UR_LAYER_MOCK"),
135-
UR_RESULT_SUCCESS);
136-
ASSERT_EQ(urLoaderInit(0, loader_config), UR_RESULT_SUCCESS);
100+
mock::callbacks.set_after_callback("urAdapterGet", &afterUrAdapterGet);
137101

138102
ur_adapter_handle_t adapter =
139103
reinterpret_cast<ur_adapter_handle_t>(0xF00DCAFE);

0 commit comments

Comments
 (0)