liberate sampler_test from linking to libtcmalloc

This test is exercising purely sampling interval logic.
This commit is contained in:
Aliaksey Kandratsenka 2024-02-07 12:27:30 -05:00
parent a02ac30bbf
commit 59f0247033
6 changed files with 28 additions and 40 deletions

View File

@ -661,9 +661,9 @@ raw_printer_test_LDADD = libtcmalloc.la $(PTHREAD_LIBS)
TESTS_ENVIRONMENT += TCMALLOC_SAMPLE_PARAMETER=524288
TESTS += sampler_test
sampler_test_SOURCES = src/tests/sampler_test.cc
sampler_test_LDFLAGS = $(TCMALLOC_FLAGS) $(AM_LDFLAGS)
sampler_test_LDADD = libtcmalloc.la
sampler_test_SOURCES = src/tests/sampler_test.cc \
src/sampler.cc
sampler_test_LDADD = libcommon.la
# These unittests often need to run binaries. They're in the current dir
TESTS_ENVIRONMENT += BINDIR=.
@ -751,12 +751,6 @@ tcmalloc_debug_unittest_CXXFLAGS = $(tcmalloc_unittest_CXXFLAGS) -DDEBUGALLOCATI
tcmalloc_debug_unittest_LDFLAGS = $(tcmalloc_unittest_LDFLAGS)
tcmalloc_debug_unittest_LDADD = libtcmalloc_debug.la
TESTS += sampler_debug_test
sampler_debug_test_SOURCES = $(sampler_test_SOURCES)
sampler_debug_test_CXXFLAGS = $(samples_test_CXXFLAGS)
sampler_debug_test_LDFLAGS = $(sampler_test_LDFLAGS)
sampler_debug_test_LDADD = libtcmalloc_debug.la
TESTS += sampling_debug_test.sh$(EXEEXT)
sampling_debug_test_sh_SOURCES = src/tests/sampling_test.sh
sampling_debug_test.sh$(EXEEXT): $(top_srcdir)/$(sampling_test_sh_SOURCES) \

View File

@ -37,9 +37,8 @@
#include <algorithm> // For min()
#include <math.h>
#include "base/commandlineflags.h"
using std::min;
#include "base/commandlineflags.h"
// The approximate gap in bytes between sampling actions.
// I.e., we take one sample approximately once every
@ -63,7 +62,7 @@ int Sampler::GetSamplePeriod() {
// Run this before using your sampler
void Sampler::Init(uint64_t seed) {
ASSERT(seed != 0);
DCHECK_NE(seed, 0);
// Initialize PRNG
rnd_ = seed;
@ -75,7 +74,7 @@ void Sampler::Init(uint64_t seed) {
bytes_until_sample_ = PickNextSamplingPoint();
}
#define MAX_SSIZE (static_cast<ssize_t>(static_cast<size_t>(static_cast<ssize_t>(-1)) >> 1))
static constexpr auto kMaxSSize = (static_cast<ssize_t>(static_cast<size_t>(static_cast<ssize_t>(-1)) >> 1));
// Generates a geometric variable with the specified mean (512K by default).
// This is done by generating a random number between 0 and 1 and applying
@ -115,7 +114,7 @@ ssize_t Sampler::PickNextSamplingPoint() {
// hit such improbable condition, we simply cheat and clamp interval
// to largest supported value.
return static_cast<ssize_t>(
std::min<double>(interval, static_cast<double>(MAX_SSIZE)));
std::min<double>(interval, static_cast<double>(kMaxSSize)));
}
bool Sampler::RecordAllocationSlow(size_t k) {

View File

@ -40,9 +40,9 @@
#include <stddef.h> // for size_t
#include <stdint.h> // for uint64_t, uint32_t, int32_t
#include <string.h> // for memcpy
#include "base/basictypes.h" // for ASSERT
#include "internal_logging.h" // for ASSERT
#include "static_vars.h"
#include "base/basictypes.h" // ssize_t
#include "base/logging.h"
namespace tcmalloc {
@ -101,7 +101,7 @@ namespace tcmalloc {
class SamplerTest;
class PERFTOOLS_DLL_DECL Sampler {
class Sampler {
public:
constexpr Sampler() {}
@ -131,11 +131,6 @@ class PERFTOOLS_DLL_DECL Sampler {
// The following are public for the purposes of testing
static uint64_t NextRandom(uint64_t rnd_); // Returns the next prng value
// C++03 requires that types stored in TLS be POD. As a result, you must
// initialize these members to {0, 0, false} before using this class!
//
// TODO(ahh): C++11 support will let us make these private.
// Bytes until we sample next.
//
// More specifically when bytes_until_sample_ is X, we can allocate
@ -143,7 +138,7 @@ class PERFTOOLS_DLL_DECL Sampler {
// byte, the containing allocation will be sampled.
//
// Always non-negative with only very brief exceptions (see
// DecrementFast{,Finish}, so casting to size_t is ok.
// TryRecordAllocationFast, so casting to size_t is ok.
private:
friend class SamplerTest;
bool RecordAllocationSlow(size_t k);
@ -154,22 +149,15 @@ class PERFTOOLS_DLL_DECL Sampler {
};
inline bool Sampler::RecordAllocation(size_t k) {
// The first time we enter this function we expect bytes_until_sample_
// to be zero, and we must call SampleAllocationSlow() to ensure
// proper initialization of static vars.
ASSERT(Static::IsInited() || bytes_until_sample_ == 0);
// Note that we have to deal with arbitrarily large values of k
// here. Thus we're upcasting bytes_until_sample_ to unsigned rather
// than the other way around. And this is why this code cannot be
// merged with DecrementFast code below.
if (static_cast<size_t>(bytes_until_sample_) < k) {
bool result = RecordAllocationSlow(k);
ASSERT(Static::IsInited());
return result;
} else {
bytes_until_sample_ -= k;
ASSERT(Static::IsInited());
return true;
}
}
@ -188,7 +176,7 @@ inline bool Sampler::TryRecordAllocationFast(size_t k) {
//
// Our API contract explicitly states that only small values of k
// are permitted. And thus it makes sense to assert on that.
ASSERT(static_cast<ssize_t>(k) >= 0);
DCHECK_GE(static_cast<ssize_t>(k), 0);
bytes_until_sample_ -= static_cast<ssize_t>(k);
if (PREDICT_FALSE(bytes_until_sample_ < 0)) {

View File

@ -358,10 +358,12 @@ inline void ThreadCache::SetMaxSize(int32_t new_max_size) {
#ifndef NO_TCMALLOC_SAMPLES
inline bool ThreadCache::SampleAllocation(size_t k) {
ASSERT(Static::IsInited());
return !sampler_.RecordAllocation(k);
}
inline bool ThreadCache::TryRecordAllocationFast(size_t k) {
ASSERT(Static::IsInited());
return sampler_.TryRecordAllocationFast(k);
}

View File

@ -125,7 +125,6 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<ForceSymbolReferences>__tcmalloc;%(ForceSymbolReferences)</ForceSymbolReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -137,7 +136,6 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<ForceSymbolReferences>__tcmalloc;%(ForceSymbolReferences)</ForceSymbolReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release-Patch|Win32'">
@ -152,7 +150,6 @@
<SubSystem>Console</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<ForceSymbolReferences>__tcmalloc;%(ForceSymbolReferences)</ForceSymbolReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release-Override|Win32'">
@ -202,6 +199,9 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\tests\sampler_test.cc" />
<ClCompile Include="..\..\src\sampler.cc" />
<ClCompile Include="..\..\src\base\logging.cc" />
<ClCompile Include="..\..\src\windows\port.cc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\base\commandlineflags.h" />
@ -211,13 +211,9 @@
<ClInclude Include="..\..\src\windows\port.h" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\libtcmalloc_minimal\libtcmalloc_minimal.vcxproj">
<Project>{55e2b3ae-3ca1-4db6-97f7-0a044d6f446f}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Import Project="$(MSBuildProjectDirectory)\..\common.vcxproj.props" />
</Project>
</Project>

View File

@ -14,6 +14,15 @@
<ClCompile Include="..\..\src\tests\sampler_test.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\sampler.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\base\logging.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\windows\port.cc">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\base\commandlineflags.h">