2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "common_test_environment.h"
18 #include "file_content_manipulations.h"
24 extern std::unique_ptr<Trace> GlobalTrace;
29 struct ClReleaseMemObjectStub : public TestEnv
33 ClReleaseMemObjectStub() : TestEnv("./cl_release_mem_object_interception_test.log") {}
37 cl_device_id device_id;
38 int err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
39 context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
48 clReleaseContext(context);
52 TEST_F(ClReleaseMemObjectStub, should_work_as_standard_version)
54 cl_mem mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
55 clReleaseMemObject(mem);
59 TEST_F(ClReleaseMemObjectStub, must_log_deallocation_events_if_trace_is_ready_for_it)
62 cl_mem mem1 = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
64 clReleaseMemObject(mem1);
66 GlobalTrace.reset(new Trace);
67 cl_mem mem2 = clCreateBuffer(context, CL_MEM_READ_WRITE, 128, NULL, NULL);
68 cl_mem mem3 = clCreateBuffer(context, CL_MEM_READ_WRITE, 64, NULL, NULL);
69 ASSERT_TRUE(mem2 && mem3);
70 clReleaseMemObject(mem2);
71 clReleaseMemObject(mem3);
74 ASSERT_STREQ(getContentOfFile("./cl_release_mem_object_interception_test.log").c_str(),
75 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
76 "GPU - Peak mem usage: 192 B, Total allocated: 192 B, Total deallocated: 192 B\n");
79 TEST_F(ClReleaseMemObjectStub, must_log_deallocation_event_only_if_reference_counter_equals_to_zero)
81 cl_mem mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
82 clRetainMemObject(mem);
83 clReleaseMemObject(mem);
85 ASSERT_STREQ(getContentOfFile("./cl_release_mem_object_interception_test.log").c_str(),
86 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
87 "GPU - Peak mem usage: 1024 B, Total allocated: 1024 B, Total deallocated: 0 B\n");
88 clReleaseMemObject(mem);
90 GlobalTrace.reset(new Trace);
91 mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
92 clRetainMemObject(mem);
93 clReleaseMemObject(mem);
94 clReleaseMemObject(mem);
97 getContentOfFile("./cl_release_mem_object_interception_test.log").c_str(),
98 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
99 "GPU - Peak mem usage: 1024 B, Total allocated: 1024 B, Total deallocated: 1024 B\n");
102 TEST_F(ClReleaseMemObjectStub, must_not_log_deallocation_event_if_original_function_failed)
105 ASSERT_NE(clReleaseMemObject(mem), CL_SUCCESS);
109 ASSERT_STREQ(getContentOfFile("./cl_release_mem_object_interception_test.log").c_str(),
110 "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
111 "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
114 } // namespace backstage