Merge "Add new mallopt M_LOG_STATS." am: 90e2827762 am: 29bedad140 am: 1e364e01b8 am: 0ceeea5a49

Original change: https://android-review.googlesource.com/c/platform/bionic/+/2585453

Change-Id: I2630cc15234a495504fca63104ba598ccdc7e246
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Christopher Ferris 2023-06-20 23:30:14 +00:00 committed by Automerger Merge Worker
commit 8d1a8a8f26
3 changed files with 56 additions and 9 deletions

View file

@ -19,10 +19,20 @@
#include <sys/param.h>
#include <unistd.h>
#include <async_safe/log.h>
#include <private/MallocXmlElem.h>
#include "jemalloc.h"
__BEGIN_DECLS
size_t je_mallinfo_narenas();
size_t je_mallinfo_nbins();
struct mallinfo je_mallinfo_arena_info(size_t);
struct mallinfo je_mallinfo_bin_info(size_t, size_t);
__END_DECLS
void* je_pvalloc(size_t bytes) {
size_t pagesize = getpagesize();
size_t size = __BIONIC_ALIGN(bytes, pagesize);
@ -121,19 +131,36 @@ int je_mallopt(int param, int value) {
return 0;
}
return 1;
} else if (param == M_LOG_STATS) {
for (size_t i = 0; i < je_mallinfo_narenas(); i++) {
struct mallinfo mi = je_mallinfo_arena_info(i);
if (mi.hblkhd != 0) {
async_safe_format_log(ANDROID_LOG_INFO, "jemalloc",
"Arena %zu: large bytes %zu huge bytes %zu bin bytes %zu", i,
mi.ordblks, mi.uordblks, mi.fsmblks);
for (size_t j = 0; j < je_mallinfo_nbins(); j++) {
struct mallinfo mi = je_mallinfo_bin_info(i, j);
if (mi.ordblks != 0) {
size_t total_allocs = 1;
if (mi.uordblks > mi.fordblks) {
total_allocs = mi.uordblks - mi.fordblks;
}
size_t bin_size = mi.ordblks / total_allocs;
async_safe_format_log(
ANDROID_LOG_INFO, "jemalloc",
" Bin %zu (%zu bytes): allocated bytes %zu nmalloc %zu ndalloc %zu", j, bin_size,
mi.ordblks, mi.uordblks, mi.fordblks);
}
}
}
}
return 1;
}
return 0;
}
__BEGIN_DECLS
size_t je_mallinfo_narenas();
size_t je_mallinfo_nbins();
struct mallinfo je_mallinfo_arena_info(size_t);
struct mallinfo je_mallinfo_bin_info(size_t, size_t);
__END_DECLS
int je_malloc_info(int options, FILE* fp) {
if (options != 0) {
errno = EINVAL;

View file

@ -319,6 +319,16 @@ enum HeapTaggingLevel {
#define M_HEAP_TAGGING_LEVEL_SYNC M_HEAP_TAGGING_LEVEL_SYNC
};
/**
* mallopt() option to print human readable statistics about the memory
* allocator to the log. There is no format for this data, each allocator
* can use a different format, and the data that is printed can
* change at any time. This is expected to be used as a debugging aid.
*
* Available since API level 35.
*/
#define M_LOG_STATS (-205)
/**
* [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies
* heap behavior. Values of `__option` are the `M_` constants from this header.

View file

@ -708,6 +708,15 @@ TEST(malloc, mallopt_purge_all) {
#endif
}
TEST(malloc, mallopt_log_stats) {
#if defined(__BIONIC__)
SKIP_WITH_HWASAN << "hwasan does not implement mallopt";
ASSERT_EQ(1, mallopt(M_LOG_STATS, 0));
#else
GTEST_SKIP() << "bionic-only test";
#endif
}
// Verify that all of the mallopt values are unique.
TEST(malloc, mallopt_unique_params) {
#if defined(__BIONIC__)
@ -722,6 +731,7 @@ TEST(malloc, mallopt_unique_params) {
std::make_pair(M_TSDS_COUNT_MAX, "M_TSDS_COUNT_MAX"),
std::make_pair(M_BIONIC_ZERO_INIT, "M_BIONIC_ZERO_INIT"),
std::make_pair(M_BIONIC_SET_HEAP_TAGGING_LEVEL, "M_BIONIC_SET_HEAP_TAGGING_LEVEL"),
std::make_pair(M_LOG_STATS, "M_LOG_STATS"),
};
std::unordered_map<int, std::string> all_params;