static size_t GetMaxAllocs(const memory_trace::Entry* entries, size_t num_entries) {
size_t max_allocs = 0;
size_t num_allocs = 0; for (size_t i = 0; i < num_entries; i++) { switch (entries[i].type) { case memory_trace::THREAD_DONE: case memory_trace::UNKNOWN: break; case memory_trace::MALLOC: case memory_trace::CALLOC: case memory_trace::MEMALIGN: if (entries[i].ptr != 0) {
num_allocs++;
} break; case memory_trace::REALLOC: if (entries[i].ptr == 0 && entries[i].u.old_ptr != 0) {
num_allocs--;
} elseif (entries[i].ptr != 0 && entries[i].u.old_ptr == 0) {
num_allocs++;
} break; case memory_trace::FREE: if (entries[i].ptr != 0) {
num_allocs--;
} break;
} if (num_allocs > max_allocs) {
max_allocs = num_allocs;
}
} return max_allocs;
}
staticvoid PrintLogStats(constchar* log_name) {
logger_list* list =
android_logger_list_open(android_name_to_log_id(log_name), ANDROID_LOG_NONBLOCK, 0, getpid()); if (list == nullptr) {
printf("Failed to open log for %s\n", log_name); return;
} while (true) {
log_msg entry;
ssize_t retval = android_logger_list_read(list, &entry); if (retval == 0) { break;
} if (retval < 0) { if (retval == -EINTR) { continue;
} // EAGAIN means there is nothing left to read when ANDROID_LOG_NONBLOCK is set. if (retval != -EAGAIN) {
printf("Failed to read log entry: %s\n", strerror(-retval));
} break;
} if (entry.msg() == nullptr) { continue;
} // Only print allocator tagged log entries.
std::string_view tag(entry.msg() + 1); if (tag != "scudo" && tag != "jemalloc") { continue;
}
printf("%s\n", &tag.back() + 2);
}
android_logger_list_close(list);
}
staticvoid ProcessDump(const memory_trace::Entry* entries, size_t num_entries,
size_t max_threads) { // Do a pass to get the maximum number of allocations used at one // time to allow a single mmap that can hold the maximum number of // pointers needed at once.
size_t max_allocs = GetMaxAllocs(entries, num_entries);
Pointers pointers(max_allocs);
Threads threads(&pointers, max_threads);
for (size_t i = 0; i < num_entries; i++) { if (((i + 1) % 100000) == 0) {
dprintf(STDOUT_FILENO, " At line %zu:\n", i + 1);
NativePrintInfo(" ");
} const memory_trace::Entry& entry = entries[i];
Thread* thread = threads.FindThread(entry.tid); if (thread == nullptr) {
thread = threads.CreateThread(entry.tid);
}
// Wait for the thread to complete any previous actions before handling // the next action.
thread->WaitForReady();
thread->SetEntry(&entry);
bool does_free = AllocDoesFree(entry); if (does_free) { // Make sure that any other threads doing allocations are complete // before triggering the action. Otherwise, another thread could // be creating the allocation we are going to free.
threads.WaitForAllToQuiesce();
}
// Tell the thread to execute the action.
thread->SetPending();
if (entries[i].type == memory_trace::THREAD_DONE) { // Wait for the thread to finish and clear the thread entry.
threads.Finish(thread);
}
// Wait for this action to complete. This avoids a race where // another thread could be creating the same allocation where are // trying to free. if (does_free) {
thread->WaitForReady();
}
} // Wait for all threads to stop processing actions.
threads.WaitForAllToQuiesce();
NativePrintInfo("Final ");
// Free any outstanding pointers. // This allows us to run a tool like valgrind to verify that no memory // is leaked and everything is accounted for during a run.
threads.FinishAll();
pointers.FreeAll();
// Print out the total time making all allocation calls. char buffer[256];
uint64_t total_nsecs = threads.total_time_nsecs();
NativeFormatFloat(buffer, sizeof(buffer), total_nsecs, 1000000000);
dprintf(STDOUT_FILENO, "Total Allocation/Free Time: %" PRIu64 "ns %ss\n", total_nsecs, buffer);
#ifdefined(__BIONIC__) // Send native allocator stats to the log
mallopt(M_LOG_STATS, 0); #endif
// No need to avoid allocations at this point since all stats have been sent to the log.
printf("Native Allocator Stats:\n");
PrintLogStats("system");
PrintLogStats("main");
}
int main(int argc, char** argv) { if (argc != 2 && argc != 3) { if (argc > 3) {
fprintf(stderr, "Only two arguments are expected.\n");
} else {
fprintf(stderr, "Requires at least one argument.\n");
}
fprintf(stderr, "Usage: %s MEMORY_LOG_FILE [MAX_THREADS]\n", basename(argv[0]));
fprintf(stderr, " MEMORY_LOG_FILE\n");
fprintf(stderr, " This can either be a text file or a zipped text file.\n");
fprintf(stderr, " MAX_THREADs\n");
fprintf(stderr, " The maximum number of threads in the trace. The default is %zu.\n",
kDefaultMaxThreads);
fprintf(stderr, " This pre-allocates the memory for thread data to avoid allocating\n");
fprintf(stderr, " while the trace is being replayed.\n"); return1;
}
#ifdefined(__LP64__)
dprintf(STDOUT_FILENO, "64 bit environment.\n"); #else
dprintf(STDOUT_FILENO, "32 bit environment.\n"); #endif
#ifdefined(__BIONIC__)
dprintf(STDOUT_FILENO, "Setting decay time to 1\n");
mallopt(M_DECAY_TIME, 1); #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.