diff --git a/CMakeLists.txt b/CMakeLists.txt index aa1ec6d..0e8d469 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -805,7 +805,12 @@ if(GPERFTOOLS_BUILD_HEAP_CHECKER OR GPERFTOOLS_BUILD_HEAP_PROFILER) list(APPEND TESTS_ENVIRONMENT PPROF_PATH=${CMAKE_CURRENT_SOURCE_DIR}/src/pprof) - add_executable(sampler_test src/tests/sampler_test.cc src/sampler.cc) + add_executable(sampler_test src/tests/sampler_test.cc + src/sampler.cc + src/base/spinlock.cc + src/base/spinlock_internal.cc + src/base/sysinfo.cc + src/base/logging.cc) target_link_libraries(sampler_test gtest) add_test(sampler_test sampler_test) diff --git a/src/tests/sampling_test.cc b/src/tests/sampling_test.cc index 5596b04..cb30c6c 100644 --- a/src/tests/sampling_test.cc +++ b/src/tests/sampling_test.cc @@ -97,30 +97,32 @@ static void VerifyWithPProf(std::string_view argv0, std::string_view path) { contents.append(buf, buf + amt); } - printf("pprof output:\n%s\n\n", contents.c_str()); + fprintf(stderr, "pprof output:\n%s\n\n", contents.c_str()); regmatch_t pmatch[3]; regex_t regex; - CHECK_EQ(regcomp(®ex, "([0-9.]+) *([0-9.]+)% *_*AllocateAllocate", REG_NEWLINE | REG_EXTENDED), 0); + CHECK_EQ(regcomp(®ex, "([0-9.]+)(MB)? *([0-9.]+)% *_*AllocateAllocate", REG_NEWLINE | REG_EXTENDED), 0); CHECK_EQ(regexec(®ex, contents.c_str(), 3, pmatch, 0), 0); - printf("AllocateAllocate regex match: %.*s\n", - int(pmatch[0].rm_eo - pmatch[0].rm_so), - contents.data() + pmatch[0].rm_so); + fprintf(stderr,"AllocateAllocate regex match: %.*s\n", + int(pmatch[0].rm_eo - pmatch[0].rm_so), + contents.data() + pmatch[0].rm_so); std::string number{contents.data() + pmatch[1].rm_so, contents.data() + pmatch[1].rm_eo}; errno = 0; char* endptr; - double percent = strtod(number.c_str(), &endptr); + double megs = strtod(number.c_str(), &endptr); CHECK(endptr && *endptr == '\0'); CHECK_EQ(errno, 0); // We allocate 8*10^7 bytes of memory, which is 76M. Because we // sample, the estimate may be a bit high or a bit low: we accept // anything from 50M to 99M. - CHECK_LE(50, percent); - CHECK_LE(percent, 99); + if (!(50 <= megs && megs < 100)) { + fprintf(stderr, "expected megs to be between 50 and 100. Got: %f\n", megs); + abort(); + } } struct TempFile { @@ -166,6 +168,9 @@ struct TempFile { int main(int argc, char** argv) { tcmalloc::TestingPortal::Get()->GetSampleParameter() = 512 << 10; + // Make sure allocations we sample are done on fresh thread cache, so that + // sampling parameter update is taken into account. + MallocExtension::instance()->MarkThreadIdle(); for (int i = 0; i < 8000; i++) { AllocateAllocate(); diff --git a/vsprojects/sampler_test/sampler_test.vcxproj b/vsprojects/sampler_test/sampler_test.vcxproj index cd15774..c70a6c7 100644 --- a/vsprojects/sampler_test/sampler_test.vcxproj +++ b/vsprojects/sampler_test/sampler_test.vcxproj @@ -197,6 +197,9 @@ + + + diff --git a/vsprojects/sampler_test/sampler_test.vcxproj.filters b/vsprojects/sampler_test/sampler_test.vcxproj.filters index 820df34..7a1d930 100644 --- a/vsprojects/sampler_test/sampler_test.vcxproj.filters +++ b/vsprojects/sampler_test/sampler_test.vcxproj.filters @@ -23,6 +23,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files +