blob: 600520959559f2046a8dbccf7b10734095581b8b [file] [log] [blame]
Josh Gao7d15dc32017-03-13 17:10:46 -07001/*
2 * Copyright (C) 2017 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 <err.h>
18#include <inttypes.h>
19#include <pthread.h>
20#include <stdio.h>
21#include <string.h>
22#include <sys/mman.h>
23#include <sys/user.h>
24#include <unistd.h>
25
26#include <gtest/gtest.h>
27
28#include <chrono>
29#include <thread>
30#include <vector>
31
32#include <android-base/macros.h>
33
34#include "utils.h"
35
36using namespace std::chrono_literals;
37
Zhenhua WANGcf17b482017-05-12 13:53:51 +080038static void WaitUntilAllExited(pid_t* pids, size_t pid_count) {
39 // Wait until all children have exited.
40 bool alive = true;
41 while (alive) {
42 alive = false;
43 for (size_t i = 0; i < pid_count; ++i) {
44 if (pids[i] != 0) {
45 if (kill(pids[i], 0) == 0) {
46 alive = true;
47 } else {
48 EXPECT_EQ(errno, ESRCH);
49 pids[i] = 0; // Skip in next loop.
50 }
51 }
52 }
53 }
Josh Gao7d15dc32017-03-13 17:10:46 -070054}
55
Zhenhua WANGcf17b482017-05-12 13:53:51 +080056class LeakChecker {
57 public:
58 LeakChecker() {
Elliott Hughesa8384902017-10-03 10:18:58 -070059 // Avoid resizing and using memory later.
60 // 64Ki is the default limit on VMAs per process.
61 maps_.reserve(64*1024);
Zhenhua WANGcf17b482017-05-12 13:53:51 +080062 Reset();
63 }
64
65 ~LeakChecker() {
66 Check();
67 }
68
69 void Reset() {
70 previous_size_ = GetMappingSize();
71 }
72
73 void DumpTo(std::ostream& os) const {
74 os << previous_size_;
75 }
76
77 private:
78 size_t previous_size_;
Elliott Hughesa8384902017-10-03 10:18:58 -070079 std::vector<map_record> maps_;
Zhenhua WANGcf17b482017-05-12 13:53:51 +080080
81 void Check() {
82 auto current_size = GetMappingSize();
83 if (current_size > previous_size_) {
84 FAIL() << "increase in process map size: " << previous_size_ << " -> " << current_size;
85 }
86 }
Elliott Hughesa8384902017-10-03 10:18:58 -070087
88 size_t GetMappingSize() {
89 if (!Maps::parse_maps(&maps_)) {
90 err(1, "failed to parse maps");
91 }
92
93 size_t result = 0;
94 for (const map_record& map : maps_) {
95 result += map.addr_end - map.addr_start;
96 }
97
98 return result;
99 }
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800100};
101
102std::ostream& operator<<(std::ostream& os, const LeakChecker& lc) {
103 lc.DumpTo(os);
104 return os;
Josh Gao7d15dc32017-03-13 17:10:46 -0700105}
106
107// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800108TEST(pthread_leak, join) {
109 LeakChecker lc;
Evgeny Eltsin775fb092018-05-04 20:08:37 +0200110
111 for (size_t pass = 0; pass < 2; ++pass) {
112 for (int i = 0; i < 100; ++i) {
113 pthread_t thread;
114 ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* { return nullptr; }, nullptr));
115 ASSERT_EQ(0, pthread_join(thread, nullptr));
116 }
117
118 // A native bridge implementation might need a warm up pass to reach a steady state.
119 // http://b/37920774.
120 if (pass == 0) lc.Reset();
Josh Gao7d15dc32017-03-13 17:10:46 -0700121 }
122}
123
124// http://b/36045112
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800125TEST(pthread_leak, detach) {
126 LeakChecker lc;
Dmytro Chystiakova3392332019-05-10 09:30:04 -0700127 constexpr int kThreadCount = 100;
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800128
Dmytro Chystiakova3392332019-05-10 09:30:04 -0700129 // Devices with low power cores/low number of cores can not finish test in time hence decreasing
130 // threads count to 90.
131 // http://b/129924384.
132 int threads_count = (sysconf(_SC_NPROCESSORS_CONF) > 2) ? kThreadCount : (kThreadCount - 10);
133
134 for (size_t pass = 0; pass < 1; ++pass) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700135 struct thread_data { pthread_barrier_t* barrier; pid_t* tid; } threads[kThreadCount] = {};
136
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800137 pthread_barrier_t barrier;
Dmytro Chystiakova3392332019-05-10 09:30:04 -0700138 ASSERT_EQ(pthread_barrier_init(&barrier, nullptr, threads_count + 1), 0);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800139
140 // Start child threads.
Elliott Hughesa8384902017-10-03 10:18:58 -0700141 pid_t tids[kThreadCount];
Dmytro Chystiakova3392332019-05-10 09:30:04 -0700142 for (int i = 0; i < threads_count; ++i) {
Elliott Hughesa8384902017-10-03 10:18:58 -0700143 threads[i] = {&barrier, &tids[i]};
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800144 const auto thread_function = +[](void* ptr) -> void* {
145 thread_data* data = static_cast<thread_data*>(ptr);
146 *data->tid = gettid();
147 pthread_barrier_wait(data->barrier);
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800148 return nullptr;
149 };
150 pthread_t thread;
Elliott Hughesa8384902017-10-03 10:18:58 -0700151 ASSERT_EQ(0, pthread_create(&thread, nullptr, thread_function, &threads[i]));
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800152 ASSERT_EQ(0, pthread_detach(thread));
153 }
154
155 pthread_barrier_wait(&barrier);
156 ASSERT_EQ(pthread_barrier_destroy(&barrier), 0);
157
158 WaitUntilAllExited(tids, arraysize(tids));
159
Evgeny Eltsin775fb092018-05-04 20:08:37 +0200160 // A native bridge implementation might need a warm up pass to reach a steady state.
161 // http://b/37920774.
Zhenhua WANGcf17b482017-05-12 13:53:51 +0800162 if (pass == 0) lc.Reset();
Josh Gao7d15dc32017-03-13 17:10:46 -0700163 }
Josh Gao7d15dc32017-03-13 17:10:46 -0700164}