1 /*
2 * Copyright (c) 2021 The Khronos Group Inc.
3 * Copyright (c) 2021 Valve Corporation
4 * Copyright (c) 2021 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Charles Giessen <charles@lunarg.com>
26 */
27
28 #include "test_environment.h"
29
30 #include <mutex>
31 #include <thread>
32 #include <atomic>
33
create_destroy_instance_loop_with_function_queries(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_instance, uint32_t num_loops_try_get_instance_proc_addr, uint32_t num_loops_try_get_device_proc_addr)34 void create_destroy_instance_loop_with_function_queries(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_instance,
35 uint32_t num_loops_try_get_instance_proc_addr,
36 uint32_t num_loops_try_get_device_proc_addr) {
37 for (uint32_t i = 0; i < num_loops_create_destroy_instance; i++) {
38 InstWrapper inst{env->vulkan_functions};
39 inst.CheckCreate();
40 PFN_vkEnumeratePhysicalDevices enum_pd = nullptr;
41 for (uint32_t j = 0; j < num_loops_try_get_instance_proc_addr; j++) {
42 enum_pd = inst.load("vkEnumeratePhysicalDevices");
43 ASSERT_NE(enum_pd, nullptr);
44 }
45 VkPhysicalDevice phys_dev = inst.GetPhysDev();
46
47 DeviceWrapper dev{inst};
48 dev.CheckCreate(phys_dev);
49 for (uint32_t j = 0; j < num_loops_try_get_device_proc_addr; j++) {
50 PFN_vkCmdBindPipeline p = dev.load("vkCmdBindPipeline");
51 p(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE);
52 }
53 }
54 }
55
create_destroy_device_loop(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_device, uint32_t num_loops_try_get_proc_addr)56 void create_destroy_device_loop(FrameworkEnvironment* env, uint32_t num_loops_create_destroy_device,
57 uint32_t num_loops_try_get_proc_addr) {
58 InstWrapper inst{env->vulkan_functions};
59 inst.CheckCreate();
60 for (uint32_t i = 0; i < num_loops_create_destroy_device; i++) {
61 DeviceWrapper dev{inst};
62 dev.CheckCreate(inst.GetPhysDev());
63
64 for (uint32_t j = 0; j < num_loops_try_get_proc_addr; j++) {
65 PFN_vkCmdBindPipeline p = dev.load("vkCmdBindPipeline");
66 PFN_vkCmdBindDescriptorSets d = dev.load("vkCmdBindDescriptorSets");
67 PFN_vkCmdBindVertexBuffers vb = dev.load("vkCmdBindVertexBuffers");
68 PFN_vkCmdBindIndexBuffer ib = dev.load("vkCmdBindIndexBuffer");
69 PFN_vkCmdDraw c = dev.load("vkCmdDraw");
70 p(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE);
71 d(VK_NULL_HANDLE, VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS, VK_NULL_HANDLE, 0, 0, nullptr, 0, nullptr);
72 vb(VK_NULL_HANDLE, 0, 0, nullptr, nullptr);
73 ib(VK_NULL_HANDLE, 0, 0, VkIndexType::VK_INDEX_TYPE_UINT16);
74 c(VK_NULL_HANDLE, 0, 0, 0, 0);
75 }
76 }
77 }
test_vkCmdBindPipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline)78 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindPipeline(VkCommandBuffer, VkPipelineBindPoint, VkPipeline) {}
test_vkCmdBindDescriptorSets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t, const VkDescriptorSet*, uint32_t, const uint32_t*)79 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindDescriptorSets(VkCommandBuffer, VkPipelineBindPoint, VkPipelineLayout, uint32_t, uint32_t,
80 const VkDescriptorSet*, uint32_t, const uint32_t*) {}
test_vkCmdBindVertexBuffers(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*)81 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindVertexBuffers(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*) {}
test_vkCmdBindIndexBuffer(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*)82 VKAPI_ATTR void VKAPI_CALL test_vkCmdBindIndexBuffer(VkCommandBuffer, uint32_t, uint32_t, const VkBuffer*, const VkDeviceSize*) {}
test_vkCmdDraw(VkCommandBuffer, uint32_t, uint32_t, uint32_t, uint32_t)83 VKAPI_ATTR void VKAPI_CALL test_vkCmdDraw(VkCommandBuffer, uint32_t, uint32_t, uint32_t, uint32_t) {}
TEST(Threading, InstanceCreateDestroyLoop)84 TEST(Threading, InstanceCreateDestroyLoop) {
85 const auto processor_count = std::thread::hardware_concurrency();
86
87 FrameworkEnvironment env{FrameworkSettings{}.set_log_filter("")};
88 auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
89 uint32_t num_loops_create_destroy_instance = 500;
90 uint32_t num_loops_try_get_instance_proc_addr = 5;
91 uint32_t num_loops_try_get_device_proc_addr = 100;
92
93 driver.physical_devices.emplace_back("physical_device_0")
94 .known_device_functions.push_back({"vkCmdBindPipeline", to_vkVoidFunction(test_vkCmdBindPipeline)});
95
96 std::vector<std::thread> instance_creation_threads;
97 std::vector<std::thread> function_query_threads;
98 for (uint32_t i = 0; i < processor_count; i++) {
99 instance_creation_threads.emplace_back(create_destroy_instance_loop_with_function_queries, &env,
100 num_loops_create_destroy_instance, num_loops_try_get_instance_proc_addr,
101 num_loops_try_get_device_proc_addr);
102 }
103 for (uint32_t i = 0; i < processor_count; i++) {
104 instance_creation_threads[i].join();
105 }
106 }
107
TEST(Threading, DeviceCreateDestroyLoop)108 TEST(Threading, DeviceCreateDestroyLoop) {
109 const auto processor_count = std::thread::hardware_concurrency();
110
111 FrameworkEnvironment env{FrameworkSettings{}.set_log_filter("")};
112 auto& driver = env.add_icd(TestICDDetails(TEST_ICD_PATH_VERSION_2_EXPORT_ICD_GPDPA));
113
114 uint32_t num_loops_create_destroy_device = 1000;
115 uint32_t num_loops_try_get_device_proc_addr = 5;
116
117 driver.physical_devices.emplace_back("physical_device_0").known_device_functions = {
118 {"vkCmdBindPipeline", to_vkVoidFunction(test_vkCmdBindPipeline)},
119 {"vkCmdBindDescriptorSets", to_vkVoidFunction(test_vkCmdBindDescriptorSets)},
120 {"vkCmdBindVertexBuffers", to_vkVoidFunction(test_vkCmdBindVertexBuffers)},
121 {"vkCmdBindIndexBuffer", to_vkVoidFunction(test_vkCmdBindIndexBuffer)},
122 {"vkCmdDraw", to_vkVoidFunction(test_vkCmdDraw)}};
123
124 std::vector<std::thread> device_creation_threads;
125
126 for (uint32_t i = 0; i < processor_count; i++) {
127 device_creation_threads.emplace_back(create_destroy_device_loop, &env, num_loops_create_destroy_device,
128 num_loops_try_get_device_proc_addr);
129 }
130 for (uint32_t i = 0; i < processor_count; i++) {
131 device_creation_threads[i].join();
132 }
133 }
134