issue-489: added unit test for chromium-style decommitting

This commit is contained in:
Aliaksey Kandratsenka 2014-02-22 13:46:11 -08:00
parent eb2d69014c
commit 6a000d6dd5
1 changed files with 51 additions and 5 deletions

View File

@ -869,17 +869,17 @@ static size_t GetUnmappedBytes() {
}
#endif
class AggressiveDecommitDisabler {
class AggressiveDecommitChanger {
size_t old_value_;
public:
AggressiveDecommitDisabler() {
AggressiveDecommitChanger(size_t new_value) {
MallocExtension *inst = MallocExtension::instance();
bool rv = inst->GetNumericProperty("tcmalloc.aggressive_memory_decommit", &old_value_);
CHECK_CONDITION(rv);
rv = inst->SetNumericProperty("tcmalloc.aggressive_memory_decommit", 0);
rv = inst->SetNumericProperty("tcmalloc.aggressive_memory_decommit", new_value);
CHECK_CONDITION(rv);
}
~AggressiveDecommitDisabler() {
~AggressiveDecommitChanger() {
MallocExtension *inst = MallocExtension::instance();
bool rv = inst->SetNumericProperty("tcmalloc.aggressive_memory_decommit", old_value_);
CHECK_CONDITION(rv);
@ -897,7 +897,7 @@ static void TestReleaseToSystem() {
const double old_tcmalloc_release_rate = FLAGS_tcmalloc_release_rate;
FLAGS_tcmalloc_release_rate = 0;
AggressiveDecommitDisabler disabler;
AggressiveDecommitChanger disabler(0);
static const int MB = 1048576;
void* a = malloc(MB);
@ -949,6 +949,51 @@ static void TestReleaseToSystem() {
#endif // #ifndef DEBUGALLOCATION
}
static void TestAggressiveDecommit() {
// Debug allocation mode adds overhead to each allocation which
// messes up all the equality tests here. I just disable the
// teset in this mode.
#ifndef DEBUGALLOCATION
if(!HaveSystemRelease) return;
fprintf(LOGSTREAM, "Testing aggressive de-commit\n");
AggressiveDecommitChanger enabler(1);
static const int MB = 1048576;
void* a = malloc(MB);
void* b = malloc(MB);
size_t starting_bytes = GetUnmappedBytes();
// ReleaseToSystem shouldn't do anything either.
MallocExtension::instance()->ReleaseToSystem(MB);
EXPECT_EQ(starting_bytes, GetUnmappedBytes());
free(a);
// The span to release should be 1MB.
EXPECT_EQ(starting_bytes + MB, GetUnmappedBytes());
free(b);
EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
// Nothing else to release.
MallocExtension::instance()->ReleaseFreeMemory();
EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
a = malloc(MB);
free(a);
EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
fprintf(LOGSTREAM, "Done testing aggressive de-commit\n");
#endif // #ifndef DEBUGALLOCATION
}
// On MSVC10, in release mode, the optimizer convinces itself
// g_no_memory is never changed (I guess it doesn't realize OnNoMemory
// might be called). Work around this by setting the var volatile.
@ -1327,6 +1372,7 @@ static int RunAllTests(int argc, char** argv) {
TestHugeThreadCache();
TestRanges();
TestReleaseToSystem();
TestAggressiveDecommit();
TestSetNewMode();
return 0;