From 4caa1f09770ea3e5ca22afbe8aa0900810a0dbfe Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 20 Aug 2014 09:16:57 -0700 Subject: Implement malloc_info(3). Expose jemalloc stats through the malloc_info(3) interface. Bug: 16874689 Change-Id: I4358ac283002e60ff161107028d1a3fb1e9afb0a --- tests/malloc_test.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'tests/malloc_test.cpp') diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp index 6b7a28b1c..b76625a7a 100644 --- a/tests/malloc_test.cpp +++ b/tests/malloc_test.cpp @@ -22,6 +22,8 @@ #include #include +#include + #include "private/bionic_config.h" TEST(malloc, malloc_std) { @@ -322,3 +324,51 @@ TEST(malloc, valloc_overflow) { ASSERT_EQ(NULL, valloc(SIZE_MAX)); } #endif + +TEST(malloc, malloc_info) { +#ifdef __BIONIC__ + char* buf; + size_t bufsize; + FILE* memstream = open_memstream(&buf, &bufsize); + ASSERT_NE(nullptr, memstream); + ASSERT_EQ(0, malloc_info(0, memstream)); + ASSERT_EQ(0, fclose(memstream)); + + tinyxml2::XMLDocument doc; + ASSERT_EQ(tinyxml2::XML_SUCCESS, doc.Parse(buf)); + + auto root = doc.FirstChildElement(); + ASSERT_NE(nullptr, root); + ASSERT_STREQ("malloc", root->Name()); + ASSERT_STREQ("jemalloc-1", root->Attribute("version")); + + auto arena = root->FirstChildElement(); + for (; arena != nullptr; arena = arena->NextSiblingElement()) { + int val; + + ASSERT_STREQ("heap", arena->Name()); + ASSERT_EQ(tinyxml2::XML_SUCCESS, arena->QueryIntAttribute("nr", &val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + arena->FirstChildElement("allocated-large")->QueryIntText(&val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + arena->FirstChildElement("allocated-huge")->QueryIntText(&val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + arena->FirstChildElement("allocated-bins")->QueryIntText(&val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + arena->FirstChildElement("bins-total")->QueryIntText(&val)); + + auto bin = arena->FirstChildElement("bin"); + for (; bin != nullptr; bin = bin ->NextSiblingElement()) { + if (strcmp(bin->Name(), "bin") == 0) { + ASSERT_EQ(tinyxml2::XML_SUCCESS, bin->QueryIntAttribute("nr", &val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + bin->FirstChildElement("allocated")->QueryIntText(&val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + bin->FirstChildElement("nmalloc")->QueryIntText(&val)); + ASSERT_EQ(tinyxml2::XML_SUCCESS, + bin->FirstChildElement("ndalloc")->QueryIntText(&val)); + } + } + } +#endif +} -- cgit v1.2.3