Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / contrib / heap_trace / tests / src / malloc_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 #include "symbol_searcher.h"
22 #include "memory_pool_for_symbol_searcher_internals.h"
23
24 #include <limits>
25
26 extern std::unique_ptr<Trace> GlobalTrace;
27
28 namespace backstage
29 {
30
31 struct MallocStub : public TestEnv
32 {
33   MallocStub() : TestEnv("./malloc_interception_test.log") {}
34 };
35
36 TEST_F(MallocStub, must_allocate_space_as_standard_malloc)
37 {
38   void *p = malloc(128);
39
40   ASSERT_TRUE(p);
41   free(p);
42 }
43
44 TEST_F(MallocStub, must_log_allocation_events_if_trace_is_ready_for_it)
45 {
46   GlobalTrace.reset();
47   void *p1 = malloc(1024);
48
49   GlobalTrace.reset(new Trace);
50   void *p2 = malloc(128);
51   void *p3 = malloc(64);
52   GlobalTrace.reset();
53
54   ASSERT_TRUE(p1 && p2 && p3);
55   ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
56                "On CPU - Peak heap usage: 192 B, Total allocated: 192 B, Total deallocated: 0 "
57                "B\nOn GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
58   free(p1);
59   free(p2);
60   free(p3);
61 }
62
63 TEST_F(MallocStub, must_not_do_the_record_about_allocation_event_if_original_function_failed)
64 {
65   void *p = malloc(std::numeric_limits<size_t>::max());
66   GlobalTrace.reset();
67
68   ASSERT_FALSE(p);
69   ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
70                "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
71                "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
72 }
73
74 TEST_F(MallocStub, should_allocate_memory_from_pool_for_symbol_searcher_internal_usage_if_need)
75 {
76   signalizeThatNextAllocationsWillBeForSymbolSearcherInternalUsage();
77   void *p = malloc(1024);
78   signalizeThatSymbolSearcherEndedOfWork();
79   GlobalTrace.reset();
80
81   MemoryPoolForSymbolSearcherInternals pool;
82   ASSERT_TRUE(p);
83   ASSERT_TRUE(pool.containsMemorySpaceStartedFromPointer(p));
84   ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
85                "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
86                "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
87 }
88
89 TEST_F(
90   MallocStub,
91   should_not_influence_on_trace_results_even_if_orignal_function_return_any_not_null_ptr_when_incoming_size_is_zero)
92 {
93   void *p = malloc(0);
94   free(p);
95   GlobalTrace.reset();
96
97   ASSERT_TRUE(p);
98   ASSERT_STREQ(getContentOfFile("./malloc_interception_test.log").c_str(),
99                "On CPU - Peak heap usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\nOn "
100                "GPU - Peak mem usage: 0 B, Total allocated: 0 B, Total deallocated: 0 B\n");
101 }
102
103 } // namespace backstage