blob: d2d37dcb34f27e8b184c6b64dd0729b517fbb02a [file] [log] [blame]
sergeyv7dc370b2016-06-17 11:21:11 -07001/*
2 * Copyright (C) 2016 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 "LeakChecker.h"
18
sergeyv7dc370b2016-06-17 11:21:11 -070019#include "TestUtils.h"
20
sergeyv7dc370b2016-06-17 11:21:11 -070021#include <memunreachable/memunreachable.h>
22#include <unistd.h>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <cstdio>
24#include <iostream>
sergeyv7dc370b2016-06-17 11:21:11 -070025#include <unordered_set>
26
27using namespace std;
28
29namespace android {
30namespace uirenderer {
31namespace test {
32
33static void logUnreachable(initializer_list<UnreachableMemoryInfo> infolist) {
34 // merge them all
35 UnreachableMemoryInfo merged;
36 unordered_set<uintptr_t> addrs;
37 merged.allocation_bytes = 0;
38 merged.leak_bytes = 0;
39 merged.num_allocations = 0;
40 merged.num_leaks = 0;
41 for (auto& info : infolist) {
42 // We'll be a little hazzy about these ones and just hope the biggest
43 // is the most accurate
44 merged.allocation_bytes = max(merged.allocation_bytes, info.allocation_bytes);
45 merged.num_allocations = max(merged.num_allocations, info.num_allocations);
46 for (auto& leak : info.leaks) {
John Reck1bcacfd2017-11-03 10:12:19 -070047 if (addrs.find(leak.begin) == addrs.end()) {
48 merged.leaks.push_back(leak);
49 merged.num_leaks++;
50 merged.leak_bytes += leak.size;
51 addrs.insert(leak.begin);
52 }
sergeyv7dc370b2016-06-17 11:21:11 -070053 }
54 }
55
56 // Now log the result
57 if (merged.num_leaks) {
58 cout << endl << "Leaked memory!" << endl;
59 if (!merged.leaks[0].backtrace.num_frames) {
John Reckf211cc42017-07-06 12:52:31 -070060 cout << "Re-run with 'export LIBC_DEBUG_MALLOC_OPTIONS=backtrace' to get backtraces"
John Reck1bcacfd2017-11-03 10:12:19 -070061 << endl;
sergeyv7dc370b2016-06-17 11:21:11 -070062 }
63 cout << merged.ToString(false);
64 }
65}
66
67void LeakChecker::checkForLeaks() {
68 // TODO: Until we can shutdown the RT thread we need to do this in
69 // two passes as GetUnreachableMemory has limited insight into
70 // thread-local caches so some leaks will not be properly tagged as leaks
sergeyv7dc370b2016-06-17 11:21:11 -070071 UnreachableMemoryInfo rtMemInfo;
72 TestUtils::runOnRenderThread([&rtMemInfo](renderthread::RenderThread& thread) {
sergeyv7dc370b2016-06-17 11:21:11 -070073 // Check for leaks
74 if (!GetUnreachableMemory(rtMemInfo)) {
75 cerr << "Failed to get unreachable memory!" << endl;
76 return;
77 }
78 });
79 UnreachableMemoryInfo uiMemInfo;
80 if (!GetUnreachableMemory(uiMemInfo)) {
81 cerr << "Failed to get unreachable memory!" << endl;
82 return;
83 }
84 logUnreachable({rtMemInfo, uiMemInfo});
sergeyv7dc370b2016-06-17 11:21:11 -070085}
86
87} /* namespace test */
88} /* namespace uirenderer */
89} /* namespace android */