diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/malloc | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/malloc')
-rw-r--r-- | library/cpp/malloc/api/helpers/io.cpp | 10 | ||||
-rw-r--r-- | library/cpp/malloc/api/helpers/ya.make | 13 | ||||
-rw-r--r-- | library/cpp/malloc/api/malloc.cpp | 37 | ||||
-rw-r--r-- | library/cpp/malloc/api/malloc.h | 32 | ||||
-rw-r--r-- | library/cpp/malloc/api/ut/ut.cpp | 10 | ||||
-rw-r--r-- | library/cpp/malloc/api/ut/ya.make | 13 | ||||
-rw-r--r-- | library/cpp/malloc/api/ya.make | 11 | ||||
-rw-r--r-- | library/cpp/malloc/jemalloc/malloc-info.cpp | 65 | ||||
-rw-r--r-- | library/cpp/malloc/jemalloc/ya.make | 21 | ||||
-rw-r--r-- | library/cpp/malloc/tcmalloc/malloc-info.cpp | 9 | ||||
-rw-r--r-- | library/cpp/malloc/tcmalloc/ya.make | 15 | ||||
-rw-r--r-- | library/cpp/malloc/ya.make | 26 |
12 files changed, 262 insertions, 0 deletions
diff --git a/library/cpp/malloc/api/helpers/io.cpp b/library/cpp/malloc/api/helpers/io.cpp new file mode 100644 index 0000000000..5177969f4d --- /dev/null +++ b/library/cpp/malloc/api/helpers/io.cpp @@ -0,0 +1,10 @@ +#include <library/cpp/malloc/api/malloc.h> + +#include <util/stream/output.h> + +using namespace NMalloc; + +template <> +void Out<TMallocInfo>(IOutputStream& out, const TMallocInfo& info) { + out << "malloc (name = " << info.Name << ")"; +} diff --git a/library/cpp/malloc/api/helpers/ya.make b/library/cpp/malloc/api/helpers/ya.make new file mode 100644 index 0000000000..62875bca0e --- /dev/null +++ b/library/cpp/malloc/api/helpers/ya.make @@ -0,0 +1,13 @@ +LIBRARY() + +OWNER(pg) + +PEERDIR( + library/cpp/malloc/api +) + +SRCS( + io.cpp +) + +END() diff --git a/library/cpp/malloc/api/malloc.cpp b/library/cpp/malloc/api/malloc.cpp new file mode 100644 index 0000000000..eed1c58a38 --- /dev/null +++ b/library/cpp/malloc/api/malloc.cpp @@ -0,0 +1,37 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "malloc.h" + +namespace { + bool SetEmptyParam(const char*, const char*) { + return false; + } + + const char* GetEmptyParam(const char*) { + return nullptr; + } + + bool CheckEmptyParam(const char*, bool defaultValue) { + return defaultValue; + } +} + +namespace NMalloc { + volatile bool IsAllocatorCorrupted = false; + + TMallocInfo::TMallocInfo() + : Name() + , SetParam(SetEmptyParam) + , GetParam(GetEmptyParam) + , CheckParam(CheckEmptyParam) + { + } + + void AbortFromCorruptedAllocator(const char* errorMessage) { + errorMessage = errorMessage ? errorMessage : "<unspecified>"; + fprintf(stderr, "Allocator error: %s\n", errorMessage); + IsAllocatorCorrupted = true; + abort(); + } +} diff --git a/library/cpp/malloc/api/malloc.h b/library/cpp/malloc/api/malloc.h new file mode 100644 index 0000000000..ebd545d6dd --- /dev/null +++ b/library/cpp/malloc/api/malloc.h @@ -0,0 +1,32 @@ +#pragma once + +#include <string.h> +#include <util/system/compiler.h> + +namespace NMalloc { + struct TMallocInfo { + TMallocInfo(); + + const char* Name; + + bool (*SetParam)(const char* param, const char* value); + const char* (*GetParam)(const char* param); + + bool (*CheckParam)(const char* param, bool defaultValue); + }; + + extern volatile bool IsAllocatorCorrupted; + void AbortFromCorruptedAllocator(const char* errorMessage = nullptr); + + // this function should be implemented by malloc implementations + TMallocInfo MallocInfo(); + + struct TAllocHeader { + void* Block; + size_t AllocSize; + void Y_FORCE_INLINE Encode(void* block, size_t size, size_t signature) { + Block = block; + AllocSize = size | signature; + } + }; +} diff --git a/library/cpp/malloc/api/ut/ut.cpp b/library/cpp/malloc/api/ut/ut.cpp new file mode 100644 index 0000000000..7eccd0bf8d --- /dev/null +++ b/library/cpp/malloc/api/ut/ut.cpp @@ -0,0 +1,10 @@ +#include <library/cpp/testing/unittest/registar.h> + +#include <library/cpp/malloc/api/malloc.h> + +Y_UNIT_TEST_SUITE(MallocApi) { + Y_UNIT_TEST(ToStream) { + TStringStream ss; + ss << NMalloc::MallocInfo(); + } +} diff --git a/library/cpp/malloc/api/ut/ya.make b/library/cpp/malloc/api/ut/ya.make new file mode 100644 index 0000000000..e57225b45d --- /dev/null +++ b/library/cpp/malloc/api/ut/ya.make @@ -0,0 +1,13 @@ +UNITTEST() + +OWNER(nga) + +PEERDIR( + library/cpp/malloc/api/helpers +) + +SRCS( + ut.cpp +) + +END() diff --git a/library/cpp/malloc/api/ya.make b/library/cpp/malloc/api/ya.make new file mode 100644 index 0000000000..0ebaa0c589 --- /dev/null +++ b/library/cpp/malloc/api/ya.make @@ -0,0 +1,11 @@ +LIBRARY() + +NO_UTIL() + +OWNER(nga) + +SRCS( + malloc.cpp +) + +END() diff --git a/library/cpp/malloc/jemalloc/malloc-info.cpp b/library/cpp/malloc/jemalloc/malloc-info.cpp new file mode 100644 index 0000000000..2643ca4766 --- /dev/null +++ b/library/cpp/malloc/jemalloc/malloc-info.cpp @@ -0,0 +1,65 @@ +#include <library/cpp/malloc/api/malloc.h> + +using namespace NMalloc; + +#if defined(_MSC_VER) +TMallocInfo NMalloc::MallocInfo() { + TMallocInfo r; + r.Name = "jemalloc"; + return r; +} +#else +#include <strings.h> +#include <stdlib.h> +#include <inttypes.h> + +#include <contrib/libs/jemalloc/include/jemalloc/jemalloc.h> + +namespace { + bool JESetParam(const char* param, const char*) { + if (param) { + if (strcmp(param, "j:reset_epoch") == 0) { + uint64_t epoch = 1; + size_t sz = sizeof(epoch); + + mallctl("epoch", &epoch, &sz, &epoch, sz); + + return true; + } + + return false; + } + + return false; + } + + const char* JEGetParam(const char* param) { + if (param) { + if (strcmp(param, "allocated") == 0) { + JESetParam("j:reset_epoch", nullptr); + + size_t allocated = 0; + size_t sz = sizeof(allocated); + + mallctl("stats.allocated", &allocated, &sz, nullptr, 0); + + static_assert(sizeof(size_t) == sizeof(void*), "fix me"); + + return (const char*)(void*)allocated; + } + + return nullptr; + } + + return nullptr; + } +} + +TMallocInfo NMalloc::MallocInfo() { + TMallocInfo r; + r.Name = "jemalloc"; + r.SetParam = JESetParam; + r.GetParam = JEGetParam; + return r; +} +#endif diff --git a/library/cpp/malloc/jemalloc/ya.make b/library/cpp/malloc/jemalloc/ya.make new file mode 100644 index 0000000000..99db474eab --- /dev/null +++ b/library/cpp/malloc/jemalloc/ya.make @@ -0,0 +1,21 @@ +LIBRARY() + +NO_UTIL() + +OWNER(nga) + +IF (OS_ANDROID) + PEERDIR( + library/cpp/malloc/system + ) +ELSE() + PEERDIR( + library/cpp/malloc/api + contrib/libs/jemalloc + ) + SRCS( + malloc-info.cpp + ) +ENDIF() + +END() diff --git a/library/cpp/malloc/tcmalloc/malloc-info.cpp b/library/cpp/malloc/tcmalloc/malloc-info.cpp new file mode 100644 index 0000000000..fbcfa7ee06 --- /dev/null +++ b/library/cpp/malloc/tcmalloc/malloc-info.cpp @@ -0,0 +1,9 @@ +#include <library/cpp/malloc/api/malloc.h> + +using namespace NMalloc; + +TMallocInfo NMalloc::MallocInfo() { + TMallocInfo r; + r.Name = "tcmalloc"; + return r; +} diff --git a/library/cpp/malloc/tcmalloc/ya.make b/library/cpp/malloc/tcmalloc/ya.make new file mode 100644 index 0000000000..21372ff9e2 --- /dev/null +++ b/library/cpp/malloc/tcmalloc/ya.make @@ -0,0 +1,15 @@ +LIBRARY() + +NO_UTIL() + +OWNER(ayles) + +PEERDIR( + library/cpp/malloc/api + contrib/libs/tcmalloc/malloc_extension +) +SRCS( + malloc-info.cpp +) + +END() diff --git a/library/cpp/malloc/ya.make b/library/cpp/malloc/ya.make new file mode 100644 index 0000000000..0ec9db71d2 --- /dev/null +++ b/library/cpp/malloc/ya.make @@ -0,0 +1,26 @@ +RECURSE( + api + api/helpers + api/ut + tcmalloc + galloc + jemalloc + lockless + nalf + sample-client + system + mimalloc + mimalloc/link_test + hu + hu/link_test +) + +IF (NOT OS_WINDOWS) + RECURSE( + calloc + calloc/tests + calloc/calloc_profile_diff + calloc/calloc_profile_scan + calloc/calloc_profile_scan/ut + ) +ENDIF() |