Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / contrib / heap_trace / tests / src / cl_release_mem_object_interception_test.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common_test_environment.h"
18 #include "file_content_manipulations.h"
19
20 #include "trace.h"
21
22 #include <CL/cl.h>
23
24 extern std::unique_ptr<Trace> GlobalTrace;
25
26 namespace backstage
27 {
28
29 struct ClReleaseMemObjectStub : public TestEnv
30 {
31   cl_context context;
32
33   ClReleaseMemObjectStub() : TestEnv("./cl_release_mem_object_interception_test.log") {}
34
35   void SetUp() final
36   {
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);
40
41     TestEnv::SetUp();
42   }
43
44   void TearDown() final
45   {
46     TestEnv::TearDown();
47
48     clReleaseContext(context);
49   }
50 };
51
52 TEST_F(ClReleaseMemObjectStub, should_work_as_standard_version)
53 {
54   cl_mem mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
55   clReleaseMemObject(mem);
56   ASSERT_TRUE(mem);
57 }
58
59 TEST_F(ClReleaseMemObjectStub, must_log_deallocation_events_if_trace_is_ready_for_it)
60 {
61   GlobalTrace.reset();
62   cl_mem mem1 = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
63   ASSERT_TRUE(mem1);
64   clReleaseMemObject(mem1);
65
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);
72   GlobalTrace.reset();
73
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");
77 }
78
79 TEST_F(ClReleaseMemObjectStub, must_log_deallocation_event_only_if_reference_counter_equals_to_zero)
80 {
81   cl_mem mem = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, NULL);
82   clRetainMemObject(mem);
83   clReleaseMemObject(mem);
84   GlobalTrace.reset();
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);
89
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);
95   GlobalTrace.reset();
96   ASSERT_STREQ(
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");
100 }
101
102 TEST_F(ClReleaseMemObjectStub, must_not_log_deallocation_event_if_original_function_failed)
103 {
104   cl_mem mem;
105   ASSERT_NE(clReleaseMemObject(mem), CL_SUCCESS);
106
107   GlobalTrace.reset();
108
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");
112 }
113
114 } // namespace backstage