diff options
author | udovichenko-r <udovichenko-r@yandex-team.ru> | 2022-07-04 14:16:38 +0300 |
---|---|---|
committer | udovichenko-r <udovichenko-r@yandex-team.ru> | 2022-07-04 14:16:38 +0300 |
commit | 5fe1c2b2d90b4ddbd7d1683191a48851363cf53d (patch) | |
tree | c413b43fb69611ff1185ead7813a8e0973dca3dc /library/cpp/balloc/setup | |
parent | f9651ab5ad67347bf06d6d0789b5d6eb31a7b2cc (diff) | |
download | ydb-5fe1c2b2d90b4ddbd7d1683191a48851363cf53d.tar.gz |
[KIKIMR-15108] Add perf programs to build
ref:8f081efde09627da76e52231d32a83e34b78c1e4
Diffstat (limited to 'library/cpp/balloc/setup')
-rw-r--r-- | library/cpp/balloc/setup/CMakeLists.txt | 17 | ||||
-rw-r--r-- | library/cpp/balloc/setup/alloc.cpp | 102 | ||||
-rw-r--r-- | library/cpp/balloc/setup/alloc.h | 35 | ||||
-rw-r--r-- | library/cpp/balloc/setup/disable_by_default/disable.cpp | 9 | ||||
-rw-r--r-- | library/cpp/balloc/setup/enable.cpp | 9 | ||||
-rw-r--r-- | library/cpp/balloc/setup/enable.h | 11 |
6 files changed, 183 insertions, 0 deletions
diff --git a/library/cpp/balloc/setup/CMakeLists.txt b/library/cpp/balloc/setup/CMakeLists.txt new file mode 100644 index 00000000000..82c9d69c0c0 --- /dev/null +++ b/library/cpp/balloc/setup/CMakeLists.txt @@ -0,0 +1,17 @@ + +# This file was gererated by the build system used internally in the Yandex monorepo. +# Only simple modifications are allowed (adding source-files to targets, adding simple properties +# like target_include_directories). These modifications will be ported to original +# ya.make files by maintainers. Any complex modifications which can't be ported back to the +# original buildsystem will not be accepted. + + + +add_library(cpp-balloc-setup) +target_link_libraries(cpp-balloc-setup PUBLIC + contrib-libs-cxxsupp +) +target_sources(cpp-balloc-setup PRIVATE + ${CMAKE_SOURCE_DIR}/library/cpp/balloc/setup/alloc.cpp + ${CMAKE_SOURCE_DIR}/library/cpp/balloc/setup/enable.cpp +) diff --git a/library/cpp/balloc/setup/alloc.cpp b/library/cpp/balloc/setup/alloc.cpp new file mode 100644 index 00000000000..f32b15df39b --- /dev/null +++ b/library/cpp/balloc/setup/alloc.cpp @@ -0,0 +1,102 @@ +#include <new> +#include <stdio.h> +#include <stdlib.h> + +#include "alloc.h" +#include "enable.h" +#include <util/system/platform.h> + +namespace NAllocSetup { + size_t softLimit = size_t(-1); + size_t hardLimit = size_t(-1); + size_t allocationThreshold = size_t(-1); + size_t softReclaimDivisor = 100; + size_t angryReclaimDivisor = 100; + + struct TThrowInfo { + size_t CurrSize; + size_t MaxSize; + }; +#if defined(_unix_) && !defined(_darwin_) + __thread TThrowInfo info; + void ThrowOnError(size_t allocSize) { + info.CurrSize += allocSize; + if (info.MaxSize && info.MaxSize < info.CurrSize) { +#ifndef NDEBUG + __builtin_trap(); +#endif + info.CurrSize = 0; + throw std::bad_alloc(); + } + } + void SetThrowConditions(size_t currSize, size_t maxSize) { + info.CurrSize = currSize; + info.MaxSize = maxSize; + } +#else // _unix_ && ! _darwin_ + void ThrowOnError(size_t /*allocSize*/) { + } + void SetThrowConditions(size_t /*currSize*/, size_t /*maxSize*/) { + } +#endif // _unix_ && ! _darwin_ + + void SetSoftLimit(size_t softLimit_) { + softLimit = softLimit_; + } + void SetHardLimit(size_t hardLimit_) { + hardLimit = hardLimit_; + } + void SetAllocationThreshold(size_t allocationThreshold_) { + allocationThreshold = allocationThreshold_; + } + void SetSoftReclaimDivisor(size_t softReclaimDivisor_) { + softReclaimDivisor = softReclaimDivisor_; + } + void SetAngryReclaimDivisor(size_t angryReclaimDivisor_) { + angryReclaimDivisor = angryReclaimDivisor_; + } + size_t GetSoftLimit() { + return softLimit; + } + size_t GetHardLimit() { + return hardLimit; + } + size_t GetAllocationThreshold() { + return allocationThreshold; + } + size_t GetSoftReclaimDivisor() { + return softReclaimDivisor; + } + size_t GetAngryReclaimDivisor() { + return angryReclaimDivisor; + } + + size_t allocSize; + size_t totalAllocSize; + size_t gcSize; + + size_t GetTotalAllocSize() { + return totalAllocSize; + } + size_t GetCurSize() { + return allocSize; + } + size_t GetGCSize() { + return gcSize; + } + + bool CanAlloc(size_t allocSize_, size_t totalAllocSize_) { + allocSize = allocSize_; + totalAllocSize = totalAllocSize_; + return allocSize_ < hardLimit || totalAllocSize_ < allocationThreshold; + } + bool NeedReclaim(size_t gcSize_, size_t counter) { + gcSize = gcSize_; + size_t limit = gcSize_ < softLimit ? softReclaimDivisor : angryReclaimDivisor; + return counter > limit; + } + + bool IsEnabledByDefault() { + return EnableByDefault; + } +} diff --git a/library/cpp/balloc/setup/alloc.h b/library/cpp/balloc/setup/alloc.h new file mode 100644 index 00000000000..89fee3e3e78 --- /dev/null +++ b/library/cpp/balloc/setup/alloc.h @@ -0,0 +1,35 @@ +#pragma once + +#include <stddef.h> + +namespace NAllocSetup { + void ThrowOnError(size_t allocSize); + void SetThrowConditions(size_t currSize, size_t maxSize); + void SetSoftLimit(size_t softLimit); + void SetHardLimit(size_t hardLimit); + void SetAllocationThreshold(size_t allocationThreshold); + void SetSoftReclaimDivisor(size_t softReclaimDivisor); + void SetAngryReclaimDivisor(size_t angryReclaimDivisor); + bool CanAlloc(size_t allocSize, size_t totalAllocSize); + bool NeedReclaim(size_t gcSize_, size_t counter); + size_t GetTotalAllocSize(); + size_t GetCurSize(); + size_t GetGCSize(); + + size_t GetSoftLimit(); + size_t GetHardLimit(); + size_t GetAllocationThreshold(); + size_t GetSoftReclaimDivisor(); + size_t GetAngryReclaimDivisor(); + + bool IsEnabledByDefault(); + + struct TAllocGuard { + TAllocGuard(size_t maxSize) { + SetThrowConditions(0, maxSize); + } + ~TAllocGuard() { + SetThrowConditions(0, 0); + } + }; +} diff --git a/library/cpp/balloc/setup/disable_by_default/disable.cpp b/library/cpp/balloc/setup/disable_by_default/disable.cpp new file mode 100644 index 00000000000..fe39d5c559a --- /dev/null +++ b/library/cpp/balloc/setup/disable_by_default/disable.cpp @@ -0,0 +1,9 @@ +#include <library/cpp/balloc/setup/enable.h> + +#include <util/system/compiler.h> + +namespace NAllocSetup { + // Overriding a weak symbol defined in library/cpp/balloc/setup/enable.cpp. + // Don't link with this object if your platform doesn't support weak linkage. + extern const bool EnableByDefault = false; +} diff --git a/library/cpp/balloc/setup/enable.cpp b/library/cpp/balloc/setup/enable.cpp new file mode 100644 index 00000000000..9eba81e7a71 --- /dev/null +++ b/library/cpp/balloc/setup/enable.cpp @@ -0,0 +1,9 @@ +#include "enable.h" + +#include <util/system/compiler.h> + +namespace NAllocSetup { + // This constant can be overridden on platforms that support weak linkage. + // See library/cpp/balloc/setup/disable_by_default/disabled.cpp + extern const bool EnableByDefault Y_WEAK = true; +} diff --git a/library/cpp/balloc/setup/enable.h b/library/cpp/balloc/setup/enable.h new file mode 100644 index 00000000000..78449c10004 --- /dev/null +++ b/library/cpp/balloc/setup/enable.h @@ -0,0 +1,11 @@ +#pragma once + +namespace NAllocSetup { + // The IsEnabledByDefault variable should always have static initialization. It is safe to use it in initialization + // of global and thread-local objects because standard guarantees that static initalization always takes place + // before dynamic initialization: + // * C++11 3.6.2.2: "Static initialization shall be performed before any dynamic initialization takes place." + // * C++17 6.6.2.2: "All static initialization strongly happens before any dynamic initialization." + // On practice a constant value is just baked into the executable during the linking. + extern const bool EnableByDefault; +} |