1 /*
2 * Copyright (C) 2015 The Android Open Source Project
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 <gtest/gtest.h>
18
19 #include <memory_trace/MemoryTrace.h>
20
21 #include "Pointers.h"
22 #include "Thread.h"
23 #include "Threads.h"
24
TEST(ThreadsTest,single_thread)25 TEST(ThreadsTest, single_thread) {
26 Pointers pointers(2);
27
28 Threads threads(&pointers, 1);
29 Thread* thread = threads.CreateThread(900);
30 ASSERT_TRUE(thread != nullptr);
31 ASSERT_EQ(1U, threads.num_threads());
32
33 Thread* found_thread = threads.FindThread(900);
34 ASSERT_EQ(thread, found_thread);
35
36 memory_trace::Entry thread_done = {.type = memory_trace::THREAD_DONE};
37 thread->SetEntry(&thread_done);
38
39 thread->SetPending();
40
41 threads.Finish(thread);
42
43 ASSERT_EQ(0U, threads.num_threads());
44 }
45
TEST(ThreadsTest,multiple_threads)46 TEST(ThreadsTest, multiple_threads) {
47 Pointers pointers(4);
48
49 Threads threads(&pointers, 1);
50 Thread* thread1 = threads.CreateThread(900);
51 ASSERT_TRUE(thread1 != nullptr);
52 ASSERT_EQ(1U, threads.num_threads());
53
54 Thread* thread2 = threads.CreateThread(901);
55 ASSERT_TRUE(thread2 != nullptr);
56 ASSERT_EQ(2U, threads.num_threads());
57
58 Thread* thread3 = threads.CreateThread(902);
59 ASSERT_TRUE(thread3 != nullptr);
60 ASSERT_EQ(3U, threads.num_threads());
61
62 Thread* found_thread1 = threads.FindThread(900);
63 ASSERT_EQ(thread1, found_thread1);
64
65 Thread* found_thread2 = threads.FindThread(901);
66 ASSERT_EQ(thread2, found_thread2);
67
68 Thread* found_thread3 = threads.FindThread(902);
69 ASSERT_EQ(thread3, found_thread3);
70
71 memory_trace::Entry thread_done = {.type = memory_trace::THREAD_DONE};
72 thread1->SetEntry(&thread_done);
73 thread2->SetEntry(&thread_done);
74 thread3->SetEntry(&thread_done);
75
76 thread1->SetPending();
77 threads.Finish(thread1);
78 ASSERT_EQ(2U, threads.num_threads());
79
80 thread3->SetPending();
81 threads.Finish(thread3);
82 ASSERT_EQ(1U, threads.num_threads());
83
84 thread2->SetPending();
85 threads.Finish(thread2);
86 ASSERT_EQ(0U, threads.num_threads());
87 }
88
TEST(ThreadsTest,verify_quiesce)89 TEST(ThreadsTest, verify_quiesce) {
90 Pointers pointers(4);
91
92 Threads threads(&pointers, 1);
93 Thread* thread = threads.CreateThread(900);
94 ASSERT_TRUE(thread != nullptr);
95 ASSERT_EQ(1U, threads.num_threads());
96
97 // If WaitForAllToQuiesce is not correct, then this should provoke an error
98 // since we are overwriting the action data while it's being used.
99 constexpr size_t kAllocEntries = 512;
100 std::vector<memory_trace::Entry> mallocs(kAllocEntries);
101 std::vector<memory_trace::Entry> frees(kAllocEntries);
102 for (size_t i = 0; i < kAllocEntries; i++) {
103 mallocs[i].type = memory_trace::MALLOC;
104 mallocs[i].ptr = 0x1234 + i;
105 mallocs[i].size = 100;
106 thread->SetEntry(&mallocs[i]);
107 thread->SetPending();
108 threads.WaitForAllToQuiesce();
109
110 frees[i].type = memory_trace::FREE;
111 frees[i].ptr = 0x1234 + i;
112 thread->SetEntry(&frees[i]);
113 thread->SetPending();
114 threads.WaitForAllToQuiesce();
115 }
116
117 memory_trace::Entry thread_done = {.type = memory_trace::THREAD_DONE};
118 thread->SetEntry(&thread_done);
119 thread->SetPending();
120 threads.Finish(thread);
121 ASSERT_EQ(0U, threads.num_threads());
122 }
123
TestTooManyThreads()124 static void TestTooManyThreads() {
125 Pointers pointers(4);
126
127 Threads threads(&pointers, 1);
128 for (size_t i = 0; i <= threads.max_threads(); i++) {
129 Thread* thread = threads.CreateThread(900+i);
130 ASSERT_EQ(thread, threads.FindThread(900+i));
131 }
132 }
133
TEST(ThreadsTest,too_many_threads)134 TEST(ThreadsTest, too_many_threads) {
135 ASSERT_EXIT(TestTooManyThreads(), ::testing::ExitedWithCode(1), "");
136 }
137