Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0796af30 authored by Christopher Ferris's avatar Christopher Ferris Committed by Android (Google) Code Review
Browse files

Merge "Move to android_mallopt for malloc debug calls."

parents 3c343213 98180a34
Loading
Loading
Loading
Loading
+0 −99
Original line number Diff line number Diff line
/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define DEBUG false  // STOPSHIP if true
#include "Log.h"

#include <sstream>
#include "MemoryLeakTrackUtil.h"

/*
 * The code here originally resided in MediaPlayerService.cpp
 */

// Figure out the abi based on defined macros.
#if defined(__arm__)
#define ABI_STRING "arm"
#elif defined(__aarch64__)
#define ABI_STRING "arm64"
#elif defined(__mips__) && !defined(__LP64__)
#define ABI_STRING "mips"
#elif defined(__mips__) && defined(__LP64__)
#define ABI_STRING "mips64"
#elif defined(__i386__)
#define ABI_STRING "x86"
#elif defined(__x86_64__)
#define ABI_STRING "x86_64"
#else
#error "Unsupported ABI"
#endif

extern std::string backtrace_string(const uintptr_t* frames, size_t frame_count);

namespace android {
namespace os {
namespace statsd {

extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, size_t* infoSize,
                                     size_t* totalMemory, size_t* backtraceSize);

extern "C" void free_malloc_leak_info(uint8_t* info);

std::string dumpMemInfo(size_t limit) {
    uint8_t* info;
    size_t overallSize;
    size_t infoSize;
    size_t totalMemory;
    size_t backtraceSize;
    get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, &backtraceSize);

    size_t count;
    if (info == nullptr || overallSize == 0 || infoSize == 0 ||
        (count = overallSize / infoSize) == 0) {
        VLOG("no malloc info, libc.debug.malloc.program property should be set");
        return std::string();
    }

    std::ostringstream oss;
    oss << totalMemory << " bytes in " << count << " allocations\n";
    oss << "  ABI: '" ABI_STRING "'"
        << "\n\n";
    if (count > limit) count = limit;

    // The memory is sorted based on total size which is useful for finding
    // worst memory offenders. For diffs, sometimes it is preferable to sort
    // based on the backtrace.
    for (size_t i = 0; i < count; i++) {
        struct AllocEntry {
            size_t size;  // bit 31 is set if this is zygote allocated memory
            size_t allocations;
            uintptr_t backtrace[];
        };

        const AllocEntry* const e = (AllocEntry*)(info + i * infoSize);

        oss << (e->size * e->allocations) << " bytes ( " << e->size << " bytes * " << e->allocations
            << " allocations )\n";
        oss << backtrace_string(e->backtrace, backtraceSize) << "\n";
    }
    oss << "\n";
    free_malloc_leak_info(info);
    return oss.str();
}

}  // namespace statsd
}  // namespace os
}  // namespace android
 No newline at end of file
+0 −33
Original line number Diff line number Diff line
/*
 * Copyright 2017, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include <iostream>

namespace android {
namespace os {
namespace statsd {
/*
 * Dump the heap memory of the calling process, sorted by total size
 * (allocation size * number of allocations).
 *
 *    limit is the number of unique allocations to return.
 */
extern std::string dumpMemInfo(size_t limit);

}  // namespace statsd
}  // namespace os
}  // namespace android
 No newline at end of file
+15 −10
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@
#include <jni.h>
#include "core_jni_helpers.h"

#include <android-base/logging.h>
#include <bionic_malloc.h>

#include <utils/Log.h>
#include <utils/String8.h>

@@ -30,11 +33,6 @@
#include <sys/types.h>
#include <sys/stat.h>

extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
                                     size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);

extern "C" void free_malloc_leak_info(uint8_t* info);

#define DDMS_HEADER_SIGNATURE 0x812345dd
#define DDMS_VERSION 2

@@ -78,9 +76,16 @@ static jbyteArray DdmHandleNativeHeap_getLeakInfo(JNIEnv* env, jobject) {
    ReadFile("/proc/self/maps", maps);
    header.mapSize = maps.size();

    uint8_t* allocBytes;
    get_malloc_leak_info(&allocBytes, &header.allocSize, &header.allocInfoSize,
                         &header.totalMemory, &header.backtraceSize);
    android_mallopt_leak_info_t leak_info;
    if (!android_mallopt(M_GET_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info))) {
      PLOG(ERROR) << "*** Failed to get malloc leak info";
      return nullptr;
    }

    header.allocSize = leak_info.overall_size;
    header.allocInfoSize = leak_info.info_size;
    header.totalMemory = leak_info.total_memory;
    header.backtraceSize = leak_info.backtrace_size;

    ALOGD("*** mapSize: %zu allocSize: %zu allocInfoSize: %zu totalMemory: %zu",
          header.mapSize, header.allocSize, header.allocInfoSize, header.totalMemory);
@@ -98,10 +103,10 @@ static jbyteArray DdmHandleNativeHeap_getLeakInfo(JNIEnv* env, jobject) {
        env->SetByteArrayRegion(array, sizeof(header),
                                maps.size(), reinterpret_cast<const jbyte*>(maps.string()));
        env->SetByteArrayRegion(array, sizeof(header) + maps.size(),
                                header.allocSize, reinterpret_cast<jbyte*>(allocBytes));
                                header.allocSize, reinterpret_cast<jbyte*>(leak_info.buffer));
    }

    free_malloc_leak_info(allocBytes);
    android_mallopt(M_FREE_MALLOC_LEAK_INFO, &leak_info, sizeof(leak_info));
    return array;
}

+7 −5
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include <string>
#include <vector>

#include <android-base/logging.h>
#include <bionic_malloc.h>
#include <debuggerd/client.h>
#include <log/log.h>
#include <utils/misc.h>
@@ -663,9 +665,6 @@ static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
    return true;
}

/* pulled out of bionic */
extern "C" void write_malloc_leak_info(FILE* fp);

/*
 * Dump the native heap, writing human-readable output to the specified
 * file descriptor.
@@ -681,8 +680,11 @@ static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
    ALOGD("Native heap dump starting...\n");
    // Formatting of the native heap dump is handled by malloc debug itself.
    // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
    write_malloc_leak_info(fp.get());
    if (android_mallopt(M_WRITE_MALLOC_LEAK_INFO_TO_FILE, fp.get(), sizeof(FILE*))) {
      ALOGD("Native heap dump complete.\n");
    } else {
      PLOG(ERROR) << "Failed to write native heap dump to file";
    }
}

/*