2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
30 #include "LineBuffer.h"
31 #include "NativeInfo.h"
36 static char g_buffer[65535];
38 size_t GetMaxAllocs(int fd) {
39 lseek(fd, 0, SEEK_SET);
40 LineBuffer line_buf(fd, g_buffer, sizeof(g_buffer));
43 size_t num_allocs = 0;
44 while (line_buf.GetLine(&line, &line_len)) {
45 char* word = reinterpret_cast<char*>(memchr(line, ':', line_len));
46 if (word == nullptr) {
51 while (*word++ == ' ');
52 // This will treat a realloc as an allocation, even if it frees
53 // another allocation. Since reallocs are relatively rare, this
54 // shouldn't inflate the numbers that much.
56 // Check if this is a free of zero.
58 if (sscanf(word, "free %" SCNxPTR, &pointer) == 1 && pointer != 0) {
61 } else if (*word != 't') {
62 // Skip the thread_done message.
69 void ProcessDump(int fd, size_t max_allocs, size_t max_threads) {
70 lseek(fd, 0, SEEK_SET);
71 Pointers pointers(max_allocs);
72 Threads threads(&pointers, max_threads);
74 printf("Maximum threads available: %zu\n", threads.max_threads());
75 printf("Maximum allocations in dump: %zu\n", max_allocs);
76 printf("Total pointers available: %zu\n", pointers.max_pointers());
79 PrintNativeInfo("Initial ");
81 LineBuffer line_buf(fd, g_buffer, sizeof(g_buffer));
84 size_t line_number = 0;
85 while (line_buf.GetLine(&line, &line_len)) {
89 uintptr_t key_pointer;
91 // Every line is of this format:
92 // <tid>: <action_type> <pointer>
93 // Some actions have extra arguments which will be used and verified
94 // when creating the Action object.
95 if (sscanf(line, "%d: %s %" SCNxPTR " %n", &tid, type, &key_pointer, &line_pos) != 3) {
96 err(1, "Unparseable line found: %s\n", line);
99 if ((line_number % 100000) == 0) {
100 printf(" At line %zu:\n", line_number);
101 PrintNativeInfo(" ");
103 Thread* thread = threads.FindThread(tid);
104 if (thread == nullptr) {
105 thread = threads.CreateThread(tid);
108 // Wait for the thread to complete any previous actions before handling
110 thread->WaitForReady();
112 Action* action = thread->CreateAction(key_pointer, type, line + line_pos);
113 if (action == nullptr) {
114 err(1, "Cannot create action from line: %s\n", line);
117 bool does_free = action->DoesFree();
119 // Make sure that any other threads doing allocations are complete
120 // before triggering the action. Otherwise, another thread could
121 // be creating the allocation we are going to free.
122 threads.WaitForAllToQuiesce();
125 // Tell the thread to execute the action.
126 thread->SetPending();
128 if (action->EndThread()) {
129 // Wait for the thread to finish and clear the thread entry.
130 threads.Finish(thread);
133 // Wait for this action to complete. This avoids a race where
134 // another thread could be creating the same allocation where are
137 thread->WaitForReady();
140 // Wait for all threads to stop processing actions.
141 threads.WaitForAllToQuiesce();
143 PrintNativeInfo("Final ");
145 // Free any outstanding pointers.
146 // This allows us to run a tool like valgrind to verify that no memory
147 // is leaked and everything is accounted for during a run.
151 // Print out the total time making all allocation calls.
152 printf("Total Allocation/Free Time: %" PRIu64 "ns %0.2fs\n",
153 threads.total_time_nsecs(), threads.total_time_nsecs()/1000000000.0);
156 constexpr size_t DEFAULT_MAX_THREADS = 512;
158 int main(int argc, char** argv) {
159 if (argc != 2 && argc != 3) {
161 fprintf(stderr, "Only two arguments are expected.\n");
163 fprintf(stderr, "Requires at least one argument.\n");
165 fprintf(stderr, "Usage: %s MEMORY_LOG_FILE [MAX_THREADS]\n", basename(argv[0]));
169 size_t max_threads = DEFAULT_MAX_THREADS;
171 max_threads = atoi(argv[2]);
174 int dump_fd = open(argv[1], O_RDONLY);
176 fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
180 printf("Processing: %s\n", argv[1]);
182 // Do a first pass to get the total number of allocations used at one
183 // time to allow a single mmap that can hold the maximum number of
184 // pointers needed at once.
185 size_t max_allocs = GetMaxAllocs(dump_fd);
186 ProcessDump(dump_fd, max_allocs, max_threads);