diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-19 21:57:59 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-05-20 12:38:48 +0000 |
commit | 64500ab17deb86b394edc81f4ad42b5a2c43fe30 (patch) | |
tree | 64334ba1513b697dacd5068981a8ee5b7ad92f3b /vespalib/src/tests/util | |
parent | cfa6ec5cdbd1cf39558d3f85101de05230d6c225 (diff) |
Fold staging_vespalib into vespalib
Diffstat (limited to 'vespalib/src/tests/util')
4 files changed, 109 insertions, 0 deletions
diff --git a/vespalib/src/tests/util/process_memory_stats/.gitignore b/vespalib/src/tests/util/process_memory_stats/.gitignore new file mode 100644 index 00000000000..81af04ee64f --- /dev/null +++ b/vespalib/src/tests/util/process_memory_stats/.gitignore @@ -0,0 +1,2 @@ +mapfile +vespalib_process_memory_stats_test_app diff --git a/vespalib/src/tests/util/process_memory_stats/CMakeLists.txt b/vespalib/src/tests/util/process_memory_stats/CMakeLists.txt new file mode 100644 index 00000000000..30a0f90d952 --- /dev/null +++ b/vespalib/src/tests/util/process_memory_stats/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_process_memory_stats_test_app TEST + SOURCES + process_memory_stats_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_process_memory_stats_test_app COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/process_memory_stats_test.sh + DEPENDS vespalib_process_memory_stats_test_app) diff --git a/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.cpp b/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.cpp new file mode 100644 index 00000000000..6d0917e6d15 --- /dev/null +++ b/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.cpp @@ -0,0 +1,92 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/testkit/test_kit.h> +#include <vespa/vespalib/util/process_memory_stats.h> +#include <vespa/vespalib/util/size_literals.h> +#include <iostream> +#include <fstream> +#include <sys/mman.h> +#include <fcntl.h> +#include <unistd.h> + + +using namespace vespalib; + +namespace { + +constexpr uint64_t SIZE_EPSILON = 4095; + +std::string toString(const ProcessMemoryStats &stats) +{ + std::ostringstream os; + os << "Mapped(" + << stats.getMappedVirt() << "," << stats.getMappedRss() << + "), Anonymous(" + << stats.getAnonymousVirt() << "," << stats.getAnonymousRss() << ")"; + return os.str(); +} + +} + +TEST("Simple stats") +{ + ProcessMemoryStats stats(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats) << std::endl; + EXPECT_LESS(0u, stats.getMappedVirt()); + EXPECT_LESS(0u, stats.getMappedRss()); + EXPECT_LESS(0u, stats.getAnonymousVirt()); + EXPECT_LESS(0u, stats.getAnonymousRss()); +} + +TEST("grow anonymous memory") +{ + ProcessMemoryStats stats1(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats1) << std::endl; + size_t mapLen = 64_Ki; + void *mapAddr = mmap(nullptr, mapLen, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + EXPECT_NOT_EQUAL(reinterpret_cast<void *>(-1), mapAddr); + ProcessMemoryStats stats2(ProcessMemoryStats::create()); + std::cout << toString(stats2) << std::endl; + EXPECT_LESS_EQUAL(stats1.getAnonymousVirt() + mapLen, + stats2.getAnonymousVirt()); + memset(mapAddr, 1, mapLen); + ProcessMemoryStats stats3(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats3) << std::endl; + // Cannot check that resident grows if swap is enabled and system loaded + munmap(mapAddr, mapLen); +} + +TEST("grow mapped memory") +{ + std::ofstream of("mapfile"); + size_t mapLen = 64_Ki; + std::vector<char> buf(mapLen, 4); + of.write(&buf[0], buf.size()); + of.close(); + int mapfileFileDescriptor = open("mapfile", O_RDONLY, 0666); + EXPECT_LESS_EQUAL(0, mapfileFileDescriptor); + ProcessMemoryStats stats1(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats1) << std::endl; + void *mapAddr = mmap(nullptr, mapLen, PROT_READ, MAP_SHARED, + mapfileFileDescriptor, 0); + EXPECT_NOT_EQUAL(reinterpret_cast<void *>(-1), mapAddr); + ProcessMemoryStats stats2(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats2) << std::endl; + EXPECT_LESS_EQUAL(stats1.getMappedVirt() + mapLen, stats2.getMappedVirt()); + EXPECT_EQUAL(0, memcmp(mapAddr, &buf[0], mapLen)); + ProcessMemoryStats stats3(ProcessMemoryStats::create(SIZE_EPSILON)); + std::cout << toString(stats3) << std::endl; + // Cannot check that resident grows if swap is enabled and system loaded + munmap(mapAddr, mapLen); +} + +TEST("order samples") +{ + ProcessMemoryStats a(0,0,0,7,0); + ProcessMemoryStats b(0,0,0,8,0); + EXPECT_TRUE(a < b); + EXPECT_FALSE(b < a); +} + +TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.sh b/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.sh new file mode 100755 index 00000000000..7fe5261ab2d --- /dev/null +++ b/vespalib/src/tests/util/process_memory_stats/process_memory_stats_test.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +set -e +rm -f mapfile +$VALGRIND ./vespalib_process_memory_stats_test_app +rm -f mapfile |