Skip to content

Commit

Permalink
Add leak checking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kswiecicki committed Mar 14, 2023
1 parent 41813b7 commit 2f3fad8
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 1 deletion.
13 changes: 12 additions & 1 deletion test/layers/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ function(add_validation_test name)
set_property(TEST ${name} PROPERTY ENVIRONMENT
"UR_ENABLE_VALIDATION_LAYER=1"
"UR_ENABLE_PARAMETER_VALIDATION=1"
"UR_ADAPTERS_FORCE_LOAD=$<TARGET_FILE:ur_adapter_null>")
"UR_ENABLE_LEAK_CHECKING=1"
"UR_ADAPTERS_FORCE_LOAD=$<TARGET_FILE:ur_adapter_null>"
"UR_LOG_VALIDATION=level:debug\;flush:debug\;output:stdout")
endfunction()

function(add_validation_match_test name match_file)
add_validation_test(${name} ${ARGN})
file(READ ${match_file} MATCH_STRING)
set_tests_properties(${name} PROPERTIES
PASS_REGULAR_EXPRESSION "${MATCH_STRING}")
endfunction()

add_validation_test(parameters parameters.cpp)
add_validation_match_test(leaks leaks.out.match leaks.cpp)
add_validation_match_test(leaks_mt leaks_mt.out.match leaks_mt.cpp)
50 changes: 50 additions & 0 deletions test/layers/validation/fixtures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,54 @@ struct valPlatformsTest : urTest {
std::vector<ur_platform_handle_t> platforms;
};

struct valPlatformTest : valPlatformsTest {

void SetUp() override {
valPlatformsTest::SetUp();
ASSERT_GE(platforms.size(), 1);
platform = platforms[0]; // TODO - which to choose?
}

ur_platform_handle_t platform;
};

struct valAllDevicesTest : valPlatformTest {

void SetUp() override {
valPlatformTest::SetUp();

uint32_t count = 0;
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, 0, nullptr, &count) ||
count == 0) {
FAIL() << "Failed to get devices";
}

devices.resize(count);
if (urDeviceGet(platform, UR_DEVICE_TYPE_ALL, count, devices.data(),
nullptr)) {
FAIL() << "Failed to get devices";
}
}
std::vector<ur_device_handle_t> devices;
};

struct valDeviceTest : valAllDevicesTest {

void SetUp() override {
valAllDevicesTest::SetUp();
ASSERT_GE(devices.size(), 1);
device = devices[0];
}
ur_device_handle_t device;
};

struct valDeviceTestMultithreaded : valDeviceTest, public ::testing::WithParamInterface<int> {

void SetUp() override {
valDeviceTest::SetUp();
threadCount = GetParam();
}
int threadCount;
};

#endif // UR_VALIDATION_TEST_HELPERS_H
41 changes: 41 additions & 0 deletions test/layers/validation/leaks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: MIT

#include "fixtures.hpp"

TEST_F(valDeviceTest, testUrContextCreateLeak) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
}

TEST_F(valDeviceTest, testUrContextRetainLeak) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
ASSERT_EQ(urContextRetain(context), UR_RESULT_SUCCESS);
}

TEST_F(valDeviceTest, testUrContextCreateSuccess) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
}

TEST_F(valDeviceTest, testUrContextRetainSuccess) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
ASSERT_EQ(urContextRetain(context), UR_RESULT_SUCCESS);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
}

TEST_F(valDeviceTest, testUrContextReleaseLeak) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
}
24 changes: 24 additions & 0 deletions test/layers/validation/leaks.out.match
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2
<VALIDATION>\[DEBUG\]: Retained 2 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1
<VALIDATION>\[DEBUG\]: Retained -1 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
62 changes: 62 additions & 0 deletions test/layers/validation/leaks_mt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: MIT

#include "fixtures.hpp"

#include <iostream>
#include <thread>
#include <vector>

INSTANTIATE_TEST_SUITE_P(threadCountForValDeviceTest, valDeviceTestMultithreaded,
::testing::Values(2, 8, std::thread::hardware_concurrency()));

TEST_P(valDeviceTestMultithreaded, testUrContextRetainLeakMt) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; i++) {
threads.emplace_back([&context]() {
ASSERT_EQ(urContextRetain(context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
});
}

for (auto &thread : threads) {
thread.join();
}
}

TEST_P(valDeviceTestMultithreaded, testUrContextReleaseLeakMt) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; i++) {
threads.emplace_back([&context]() {
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
});
}

for (auto &thread : threads) {
thread.join();
}
}

TEST_P(valDeviceTestMultithreaded, testUrContextRetainReleaseLeakMt) {
ur_context_handle_t context = nullptr;
ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), UR_RESULT_SUCCESS);

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; i++) {
threads.emplace_back([&context]() {
ASSERT_EQ(urContextRetain(context), UR_RESULT_SUCCESS);
ASSERT_NE(nullptr, context);
ASSERT_EQ(urContextRelease(context), UR_RESULT_SUCCESS);
});
}

for (auto &thread : threads) {
thread.join();
}
}
72 changes: 72 additions & 0 deletions test/layers/validation/leaks_mt.out.match
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 3
<VALIDATION>\[DEBUG\]: Retained 3 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 3
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 4
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 5
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 6
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 7
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 8
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 9
<VALIDATION>\[DEBUG\]: Retained 9 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1
<VALIDATION>\[DEBUG\]: Retained -1 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -2
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -3
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -4
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -5
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -6
<VALIDATION>\[DEBUG\]: Attempting to release nonexistent handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -7
<VALIDATION>\[DEBUG\]: Retained -7 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+
<VALIDATION>\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1
<VALIDATION>\[DEBUG\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+
<VALIDATION>\[DEBUG\]: Handle [0-9xa-fA-F]+ was recorded for first time here:
(.*)

0 comments on commit 2f3fad8

Please sign in to comment.