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