scudo: Add a dump of primary allocation sizes to malloc_info output.

This will be useful for optimizing the size class map.

Differential Revision: https://reviews.llvm.org/D74098
This commit is contained in:
Peter Collingbourne 2020-02-05 14:49:19 -08:00
parent 041547eb4e
commit 9068766b9a
2 changed files with 27 additions and 3 deletions

View File

@ -303,7 +303,11 @@ TEST(ScudoWrappersCTest, MallocDisableDeadlock) {
#if !SCUDO_FUCHSIA
TEST(ScudoWrappersCTest, MallocInfo) {
char Buffer[64];
// Use volatile so that the allocations don't get optimized away.
void *volatile P1 = malloc(1234);
void *volatile P2 = malloc(4321);
char Buffer[16384];
FILE *F = fmemopen(Buffer, sizeof(Buffer), "w+");
EXPECT_NE(F, nullptr);
errno = 0;
@ -311,6 +315,11 @@ TEST(ScudoWrappersCTest, MallocInfo) {
EXPECT_EQ(errno, 0);
fclose(F);
EXPECT_EQ(strncmp(Buffer, "<malloc version=\"scudo-", 23), 0);
EXPECT_NE(nullptr, strstr(Buffer, "<alloc size=\"1234\" count=\""));
EXPECT_NE(nullptr, strstr(Buffer, "<alloc size=\"4321\" count=\""));
free(P1);
free(P2);
}
TEST(ScudoWrappersCTest, Fork) {

View File

@ -180,8 +180,23 @@ INTERFACE WEAK void *SCUDO_PREFIX(aligned_alloc)(size_t alignment,
}
INTERFACE WEAK int SCUDO_PREFIX(malloc_info)(UNUSED int options, FILE *stream) {
fputs("<malloc version=\"scudo-1\">", stream);
fputs("</malloc>", stream);
const scudo::uptr max_size =
decltype(SCUDO_ALLOCATOR)::PrimaryT::SizeClassMap::MaxSize;
auto *sizes =
static_cast<scudo::uptr *>(calloc(max_size, sizeof(scudo::uptr)));
auto callback = [](uintptr_t, size_t size, void* arg) {
auto *sizes = reinterpret_cast<scudo::uptr *>(arg);
if (size < max_size)
sizes[size]++;
};
SCUDO_ALLOCATOR.iterateOverChunks(0, -1ul, callback, sizes);
fputs("<malloc version=\"scudo-1\">\n", stream);
for (scudo::uptr i = 0; i != max_size; ++i)
if (sizes[i])
fprintf(stream, "<alloc size=\"%lu\" count=\"%lu\"/>\n", i, sizes[i]);
fputs("</malloc>\n", stream);
free(sizes);
return 0;
}