forked from bitpay/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcfbf3c
commit 241766f
Showing
4 changed files
with
59 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or https://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <memusage.h> | ||
|
||
#include <test/util/setup_common.h> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
BOOST_FIXTURE_TEST_SUITE(malloc_usage_tests, BasicTestingSetup) | ||
|
||
//! Test the accuracy of MallocUsage() by noting how far the returned | ||
//! pointer advances for each allocation; most of the time it should | ||
//! be as predicted by MallocUsage(). Do this across various allocation | ||
//! sizes; it's probably sufficient to not go beyond 256, because any | ||
//! error on sizes larger than that won't be large as a fraction. | ||
BOOST_AUTO_TEST_CASE(malloc_usage) | ||
{ | ||
std::vector<void*> alloc_pointers; | ||
for (size_t size{0}; size <= 256; ++size) { | ||
void* prev_p{}; | ||
const size_t malloc_usage{memusage::MallocUsage(size)}; | ||
int accurate{0}; | ||
int inaccurate{0}; | ||
constexpr int repetitions{10000}; | ||
for (int i{0}; i < repetitions; ++i) { | ||
void* p{malloc(size)}; | ||
alloc_pointers.push_back(p); | ||
intptr_t p_int{intptr_t(p)}; | ||
intptr_t prev_p_int{intptr_t(prev_p)}; | ||
// Due to earlier allocations (fragmentation), allocation | ||
// addresses can jump around; if the latest one is too | ||
// far from the previous one, ignore it. | ||
if (p_int > prev_p_int && p_int < prev_p_int + intptr_t(malloc_usage * 4)) { | ||
if (size_t(p_int - prev_p_int) == malloc_usage) { | ||
++accurate; | ||
} else { | ||
++inaccurate; | ||
} | ||
} | ||
prev_p = p; | ||
} | ||
BOOST_CHECK_GT(accurate, inaccurate * 10); | ||
} | ||
for (void* p : alloc_pointers) { | ||
free(p); | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters