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 <err.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <malloc.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <memory_trace/MemoryTrace.h>
31
32 #include "Alloc.h"
33 #include "File.h"
34 #include "NativeInfo.h"
35 #include "Pointers.h"
36 #include "Thread.h"
37 #include "Threads.h"
38
39 #include <log/log.h>
40 #include <log/log_read.h>
41
42 constexpr size_t kDefaultMaxThreads = 512;
43
GetMaxAllocs(const memory_trace::Entry * entries,size_t num_entries)44 static size_t GetMaxAllocs(const memory_trace::Entry* entries, size_t num_entries) {
45 size_t max_allocs = 0;
46 size_t num_allocs = 0;
47 for (size_t i = 0; i < num_entries; i++) {
48 switch (entries[i].type) {
49 case memory_trace::THREAD_DONE:
50 case memory_trace::UNKNOWN:
51 break;
52 case memory_trace::MALLOC:
53 case memory_trace::CALLOC:
54 case memory_trace::MEMALIGN:
55 if (entries[i].ptr != 0) {
56 num_allocs++;
57 }
58 break;
59 case memory_trace::REALLOC:
60 if (entries[i].ptr == 0 && entries[i].u.old_ptr != 0) {
61 num_allocs--;
62 } else if (entries[i].ptr != 0 && entries[i].u.old_ptr == 0) {
63 num_allocs++;
64 }
65 break;
66 case memory_trace::FREE:
67 if (entries[i].ptr != 0) {
68 num_allocs--;
69 }
70 break;
71 }
72 if (num_allocs > max_allocs) {
73 max_allocs = num_allocs;
74 }
75 }
76 return max_allocs;
77 }
78
PrintLogStats(const char * log_name)79 static void PrintLogStats(const char* log_name) {
80 logger_list* list =
81 android_logger_list_open(android_name_to_log_id(log_name), ANDROID_LOG_NONBLOCK, 0, getpid());
82 if (list == nullptr) {
83 printf("Failed to open log for %s\n", log_name);
84 return;
85 }
86 while (true) {
87 log_msg entry;
88 ssize_t retval = android_logger_list_read(list, &entry);
89 if (retval == 0) {
90 break;
91 }
92 if (retval < 0) {
93 if (retval == -EINTR) {
94 continue;
95 }
96 // EAGAIN means there is nothing left to read when ANDROID_LOG_NONBLOCK is set.
97 if (retval != -EAGAIN) {
98 printf("Failed to read log entry: %s\n", strerror(-retval));
99 }
100 break;
101 }
102 if (entry.msg() == nullptr) {
103 continue;
104 }
105 // Only print allocator tagged log entries.
106 std::string_view tag(entry.msg() + 1);
107 if (tag != "scudo" && tag != "jemalloc") {
108 continue;
109 }
110 printf("%s\n", &tag.back() + 2);
111 }
112 android_logger_list_close(list);
113 }
114
ProcessDump(const memory_trace::Entry * entries,size_t num_entries,size_t max_threads)115 static void ProcessDump(const memory_trace::Entry* entries, size_t num_entries,
116 size_t max_threads) {
117 // Do a pass to get the maximum number of allocations used at one
118 // time to allow a single mmap that can hold the maximum number of
119 // pointers needed at once.
120 size_t max_allocs = GetMaxAllocs(entries, num_entries);
121 Pointers pointers(max_allocs);
122 Threads threads(&pointers, max_threads);
123
124 dprintf(STDOUT_FILENO, "Maximum threads available: %zu\n", threads.max_threads());
125 dprintf(STDOUT_FILENO, "Maximum allocations in dump: %zu\n", max_allocs);
126 dprintf(STDOUT_FILENO, "Total pointers available: %zu\n\n", pointers.max_pointers());
127
128 NativePrintInfo("Initial ");
129
130 for (size_t i = 0; i < num_entries; i++) {
131 if (((i + 1) % 100000) == 0) {
132 dprintf(STDOUT_FILENO, " At line %zu:\n", i + 1);
133 NativePrintInfo(" ");
134 }
135 const memory_trace::Entry& entry = entries[i];
136 Thread* thread = threads.FindThread(entry.tid);
137 if (thread == nullptr) {
138 thread = threads.CreateThread(entry.tid);
139 }
140
141 // Wait for the thread to complete any previous actions before handling
142 // the next action.
143 thread->WaitForReady();
144
145 thread->SetEntry(&entry);
146
147 bool does_free = AllocDoesFree(entry);
148 if (does_free) {
149 // Make sure that any other threads doing allocations are complete
150 // before triggering the action. Otherwise, another thread could
151 // be creating the allocation we are going to free.
152 threads.WaitForAllToQuiesce();
153 }
154
155 // Tell the thread to execute the action.
156 thread->SetPending();
157
158 if (entries[i].type == memory_trace::THREAD_DONE) {
159 // Wait for the thread to finish and clear the thread entry.
160 threads.Finish(thread);
161 }
162
163 // Wait for this action to complete. This avoids a race where
164 // another thread could be creating the same allocation where are
165 // trying to free.
166 if (does_free) {
167 thread->WaitForReady();
168 }
169 }
170 // Wait for all threads to stop processing actions.
171 threads.WaitForAllToQuiesce();
172
173 NativePrintInfo("Final ");
174
175 // Free any outstanding pointers.
176 // This allows us to run a tool like valgrind to verify that no memory
177 // is leaked and everything is accounted for during a run.
178 threads.FinishAll();
179 pointers.FreeAll();
180
181 // Print out the total time making all allocation calls.
182 char buffer[256];
183 uint64_t total_nsecs = threads.total_time_nsecs();
184 NativeFormatFloat(buffer, sizeof(buffer), total_nsecs, 1000000000);
185 dprintf(STDOUT_FILENO, "Total Allocation/Free Time: %" PRIu64 "ns %ss\n", total_nsecs, buffer);
186
187 #if defined(__BIONIC__)
188 // Send native allocator stats to the log
189 mallopt(M_LOG_STATS, 0);
190 #endif
191
192 // No need to avoid allocations at this point since all stats have been sent to the log.
193 printf("Native Allocator Stats:\n");
194 PrintLogStats("system");
195 PrintLogStats("main");
196 }
197
main(int argc,char ** argv)198 int main(int argc, char** argv) {
199 if (argc != 2 && argc != 3) {
200 if (argc > 3) {
201 fprintf(stderr, "Only two arguments are expected.\n");
202 } else {
203 fprintf(stderr, "Requires at least one argument.\n");
204 }
205 fprintf(stderr, "Usage: %s MEMORY_LOG_FILE [MAX_THREADS]\n", basename(argv[0]));
206 fprintf(stderr, " MEMORY_LOG_FILE\n");
207 fprintf(stderr, " This can either be a text file or a zipped text file.\n");
208 fprintf(stderr, " MAX_THREADs\n");
209 fprintf(stderr, " The maximum number of threads in the trace. The default is %zu.\n",
210 kDefaultMaxThreads);
211 fprintf(stderr, " This pre-allocates the memory for thread data to avoid allocating\n");
212 fprintf(stderr, " while the trace is being replayed.\n");
213 return 1;
214 }
215
216 #if defined(__LP64__)
217 dprintf(STDOUT_FILENO, "64 bit environment.\n");
218 #else
219 dprintf(STDOUT_FILENO, "32 bit environment.\n");
220 #endif
221
222 #if defined(__BIONIC__)
223 dprintf(STDOUT_FILENO, "Setting decay time to 1\n");
224 mallopt(M_DECAY_TIME, 1);
225 #endif
226
227 size_t max_threads = kDefaultMaxThreads;
228 if (argc == 3) {
229 max_threads = atoi(argv[2]);
230 }
231
232 memory_trace::Entry* entries;
233 size_t num_entries;
234 GetUnwindInfo(argv[1], &entries, &num_entries);
235
236 dprintf(STDOUT_FILENO, "Processing: %s\n", argv[1]);
237
238 ProcessDump(entries, num_entries, max_threads);
239
240 FreeEntries(entries, num_entries);
241
242 return 0;
243 }
244