Skip to content

Commit 34e45a7

Browse files
authored
[Refactor] Remove tcmalloc (StarRocks#27130)
Signed-off-by: trueeyu <[email protected]>
1 parent fc928ad commit 34e45a7

18 files changed

+19
-766
lines changed

be/CMakeLists.txt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -808,16 +808,10 @@ set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS}
808808
-static-libgcc
809809
)
810810

811-
# Add sanitize static link flags or tcmalloc
811+
# Add sanitize static link flags or jemalloc
812812
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RELEASE")
813-
if (USE_JEMALLOC)
814-
message("use jemalloc")
815-
add_definitions(-DUSE_JEMALLOC)
816-
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} jemalloc)
817-
else()
818-
message("use tcmalloc")
819-
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} tcmalloc)
820-
endif()
813+
message("use jemalloc")
814+
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} jemalloc)
821815
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "ASAN")
822816
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
823817
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-libsan)
@@ -827,14 +821,8 @@ elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "ASAN")
827821
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "LSAN")
828822
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-liblsan)
829823
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "UBSAN")
830-
if (USE_JEMALLOC)
831-
message("use jemalloc")
832-
add_definitions(-DUSE_JEMALLOC)
833-
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-libubsan jemalloc)
834-
else()
835-
message("use tcmalloc")
836-
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-libubsan tcmalloc)
837-
endif()
824+
message("use jemalloc")
825+
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-libubsan jemalloc)
838826
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "TSAN")
839827
set(STARROCKS_LINK_LIBS ${STARROCKS_LINK_LIBS} -static-libtsan)
840828
else()

be/src/common/config.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ CONF_Int32(brpc_num_threads, "-1");
5555
// If no ip match this rule, will choose one randomly.
5656
CONF_String(priority_networks, "");
5757

58-
////
59-
//// tcmalloc gc parameter
60-
////
61-
// Min memory for TCmalloc, when used memory is smaller than this, do not returned to OS.
62-
CONF_mInt64(tc_use_memory_min, "0");
63-
// free memory rate.[0-100]
64-
CONF_mInt64(tc_free_memory_rate, "0");
65-
// tcmalloc gc period, default 60, it should be between [1, 180]
66-
CONF_mInt64(tc_gc_period, "60");
67-
6858
CONF_mBool(enable_auto_adjust_pagecache, "true");
6959
// Memory urget water level, if the memory usage exceeds this level, reduce the size of
7060
// the Pagecache immediately, it should be between (memory_high_level, 100].
@@ -77,17 +67,6 @@ CONF_mInt64(pagecache_adjust_period, "20");
7767
// Sleep time in seconds between pagecache adjust iterations.
7868
CONF_mInt64(auto_adjust_pagecache_interval_seconds, "10");
7969

80-
// Bound on the total amount of bytes allocated to thread caches.
81-
// This bound is not strict, so it is possible for the cache to go over this bound
82-
// in certain circumstances. The maximum value of this flag is capped to 1GB.
83-
// This value defaults to 1GB.
84-
// If you suspect your application is not scaling to many threads due to lock contention in TCMalloc,
85-
// you can try increasing this value. This may improve performance, at a cost of extra memory
86-
// use by TCMalloc.
87-
// reference: https://gperftools.github.io/gperftools/tcmalloc.html: TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES
88-
// https://github.com/gperftools/gperftools/issues/1111
89-
CONF_Int64(tc_max_total_thread_cache_bytes, "1073741824");
90-
9170
// process memory limit specified as number of bytes
9271
// ('<int>[bB]?'), megabytes ('<float>[mM]'), gigabytes ('<float>[gG]'),
9372
// or percentage of the physical memory ('<int>%').

be/src/common/daemon.cpp

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,14 @@
3535
#include "common/daemon.h"
3636

3737
#include <gflags/gflags.h>
38-
#ifdef USE_JEMALLOC
39-
#include "jemalloc/jemalloc.h"
40-
#else
41-
#include <gperftools/malloc_extension.h>
42-
#endif
4338

4439
#include "column/column_helper.h"
4540
#include "column/column_pool.h"
4641
#include "common/config.h"
4742
#include "common/minidump.h"
4843
#include "exec/workgroup/work_group.h"
4944
#include "gutil/cpu.h"
45+
#include "jemalloc/jemalloc.h"
5046
#include "runtime/memory/mem_chunk_allocator.h"
5147
#include "runtime/time_types.h"
5248
#include "runtime/user_function_cache.h"
@@ -101,44 +97,14 @@ class ReleaseColumnPool {
10197
void gc_memory(void* arg_this) {
10298
using namespace starrocks;
10399
const static float kFreeRatio = 0.5;
104-
GCHelper gch(config::tc_gc_period, config::memory_maintenance_sleep_time_s, MonoTime::Now());
105100

106101
auto* daemon = static_cast<Daemon*>(arg_this);
107102
while (!daemon->stopped()) {
108103
sleep(static_cast<unsigned int>(config::memory_maintenance_sleep_time_s));
109-
#if !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
110-
MallocExtension::instance()->MarkThreadBusy();
111-
#endif
104+
112105
ReleaseColumnPool releaser(kFreeRatio);
113106
ForEach<ColumnPoolList>(releaser);
114107
LOG_IF(INFO, releaser.freed_bytes() > 0) << "Released " << releaser.freed_bytes() << " bytes from column pool";
115-
116-
#if !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
117-
size_t used_size = 0;
118-
size_t free_size = 0;
119-
MallocExtension::instance()->GetNumericProperty("generic.current_allocated_bytes", &used_size);
120-
MallocExtension::instance()->GetNumericProperty("tcmalloc.pageheap_free_bytes", &free_size);
121-
size_t phy_size = used_size + free_size; // physical memory usage
122-
size_t total_bytes_to_gc = 0;
123-
if (phy_size > config::tc_use_memory_min) {
124-
size_t max_free_size = phy_size * config::tc_free_memory_rate / 100;
125-
if (free_size > max_free_size) {
126-
total_bytes_to_gc = free_size - max_free_size;
127-
}
128-
}
129-
size_t bytes_to_gc = gch.bytes_should_gc(MonoTime::Now(), total_bytes_to_gc);
130-
if (bytes_to_gc > 0) {
131-
size_t bytes = bytes_to_gc;
132-
while (bytes >= GCBYTES_ONE_STEP) {
133-
MallocExtension::instance()->ReleaseToSystem(GCBYTES_ONE_STEP);
134-
bytes -= GCBYTES_ONE_STEP;
135-
}
136-
if (bytes > 0) {
137-
MallocExtension::instance()->ReleaseToSystem(bytes);
138-
}
139-
}
140-
MallocExtension::instance()->MarkThreadIdle();
141-
#endif
142108
}
143109
}
144110

be/src/http/action/pprof_actions.cpp

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
// under the License.
3434

3535
#include "http/action/pprof_actions.h"
36-
#ifdef USE_JEMALLOC
37-
#include "jemalloc/jemalloc.h"
38-
#endif
39-
#include <gperftools/heap-profiler.h>
40-
#include <gperftools/malloc_extension.h>
36+
4137
#include <gperftools/profiler.h>
4238

4339
#include <fstream>
@@ -51,6 +47,7 @@
5147
#include "http/http_channel.h"
5248
#include "http/http_headers.h"
5349
#include "http/http_request.h"
50+
#include "jemalloc/jemalloc.h"
5451
#include "util/bfd_parser.h"
5552

5653
namespace starrocks {
@@ -69,7 +66,7 @@ void HeapAction::handle(HttpRequest* req) {
6966
std::string str = "Heap profiling is not available with address sanitizer builds.";
7067

7168
HttpChannel::send_reply(req, str);
72-
#elif defined(USE_JEMALLOC)
69+
#else
7370
(void)kPprofDefaultSampleSecs; // Avoid unused variable warning.
7471

7572
std::lock_guard<std::mutex> lock(kPprofActionMutex);
@@ -85,30 +82,8 @@ void HeapAction::handle(HttpRequest* req) {
8582
std::ifstream f(fname_cstr);
8683
str = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
8784
} else {
88-
std::string str = "dump jemalloc prof file failed";
89-
}
90-
HttpChannel::send_reply(req, str);
91-
#else
92-
std::lock_guard<std::mutex> lock(kPprofActionMutex);
93-
94-
int seconds = kPprofDefaultSampleSecs;
95-
const std::string& seconds_str = req->param(SECOND_KEY);
96-
if (!seconds_str.empty()) {
97-
seconds = std::atoi(seconds_str.c_str());
85+
str = "dump jemalloc prof file failed";
9886
}
99-
100-
std::stringstream tmp_prof_file_name;
101-
// Build a temporary file name that is hopefully unique.
102-
tmp_prof_file_name << config::pprof_profile_dir << "/heap_profile." << getpid() << "." << rand();
103-
104-
HeapProfilerStart(tmp_prof_file_name.str().c_str());
105-
// Sleep to allow for some samples to be collected.
106-
sleep(seconds);
107-
const char* profile = GetHeapProfile();
108-
HeapProfilerStop();
109-
std::string str = profile;
110-
delete profile;
111-
11287
HttpChannel::send_reply(req, str);
11388
#endif
11489
}
@@ -117,18 +92,11 @@ void GrowthAction::handle(HttpRequest* req) {
11792
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER)
11893
std::string str = "Growth profiling is not available with address sanitizer builds.";
11994
HttpChannel::send_reply(req, str);
120-
#elif defined(USE_JEMALLOC)
95+
#else
12196
std::string str =
12297
"Growth profiling is not available with jemalloc builds.You can set the `--base` flag to jeprof to compare "
12398
"the results of two Heap Profiling";
12499
HttpChannel::send_reply(req, str);
125-
#else
126-
std::lock_guard<std::mutex> lock(kPprofActionMutex);
127-
128-
std::string heap_growth_stack;
129-
MallocExtension::instance()->GetHeapGrowthStacks(&heap_growth_stack);
130-
131-
HttpChannel::send_reply(req, heap_growth_stack);
132100
#endif
133101
}
134102

be/src/http/default_path_handlers.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@
3434

3535
#include "http/default_path_handlers.h"
3636

37-
#ifdef USE_JEMALLOC
38-
#include "jemalloc/jemalloc.h"
39-
#else
40-
#include <gperftools/malloc_extension.h>
41-
#endif
4237
#include <gutil/strings/numbers.h>
4338
#include <gutil/strings/substitute.h>
4439

@@ -47,6 +42,7 @@
4742

4843
#include "common/configbase.h"
4944
#include "http/web_page_handler.h"
45+
#include "jemalloc/jemalloc.h"
5046
#include "runtime/exec_env.h"
5147
#include "runtime/mem_tracker.h"
5248
#include "storage/storage_engine.h"
@@ -211,12 +207,10 @@ void mem_tracker_handler(MemTracker* mem_tracker, const WebPageHandler::Argument
211207
(*output) << "</tbody></table>\n";
212208
}
213209

214-
#ifdef USE_JEMALLOC
215210
void malloc_stats_write_cb(void* opaque, const char* data) {
216211
auto* buf = static_cast<std::string*>(opaque);
217212
buf->append(data);
218213
}
219-
#endif
220214

221215
// Registered to handle "/memz", and prints out memory allocation statistics.
222216
void mem_usage_handler(MemTracker* mem_tracker, const WebPageHandler::ArgumentMap& args, std::stringstream* output) {
@@ -234,18 +228,11 @@ void mem_usage_handler(MemTracker* mem_tracker, const WebPageHandler::ArgumentMa
234228
(*output) << "<pre>";
235229
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || defined(THREAD_SANITIZER)
236230
(*output) << "Memory tracking is not available with address sanitizer builds.";
237-
#elif defined(USE_JEMALLOC)
231+
#else
238232
std::string buf;
239233
je_malloc_stats_print(malloc_stats_write_cb, &buf, "a");
240234
boost::replace_all(buf, "\n", "<br>");
241235
(*output) << buf << "</pre>";
242-
#else
243-
char buf[2048];
244-
MallocExtension::instance()->GetStats(buf, 2048);
245-
// Replace new lines with <br> for html
246-
std::string tmp(buf);
247-
boost::replace_all(tmp, "\n", "<br>");
248-
(*output) << tmp << "</pre>";
249236
#endif
250237
(*output) << "<pre>";
251238
string stats = StorageEngine::instance()->update_manager()->detail_memory_stats();

be/src/service/mem_hook.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifdef USE_JEMALLOC
16-
#include "jemalloc/jemalloc.h"
17-
#else
18-
#include <gperftools/nallocx.h>
19-
#include <gperftools/tcmalloc.h>
20-
#endif
21-
2215
#include <atomic>
2316
#include <iostream>
2417

2518
#include "common/compiler_util.h"
2619
#include "common/config.h"
2720
#include "glog/logging.h"
21+
#include "jemalloc/jemalloc.h"
2822
#include "util/stack_util.h"
2923

3024
#ifndef BE_TEST
@@ -191,7 +185,6 @@ void operator delete[](void* p, size_t size, std::align_val_t al) noexcept {
191185
}
192186
*/
193187

194-
#ifdef USE_JEMALLOC
195188
#define STARROCKS_MALLOC_SIZE(ptr) je_malloc_usable_size(ptr)
196189
#define STARROCKS_NALLOX(size, flags) je_nallocx(size, flags)
197190
#define STARROCKS_MALLOC(size) je_malloc(size)
@@ -202,18 +195,6 @@ void operator delete[](void* p, size_t size, std::align_val_t al) noexcept {
202195
#define STARROCKS_POSIX_MEMALIGN(ptr, align, size) je_posix_memalign(ptr, align, size)
203196
#define STARROCKS_CFREE(ptr) je_free(ptr)
204197
#define STARROCKS_VALLOC(size) je_valloc(size)
205-
#else
206-
#define STARROCKS_MALLOC_SIZE(ptr) tc_malloc_size(ptr)
207-
#define STARROCKS_NALLOX(size, flags) tc_nallocx(size, flags)
208-
#define STARROCKS_MALLOC(size) tc_malloc(size)
209-
#define STARROCKS_FREE(ptr) tc_free(ptr)
210-
#define STARROCKS_REALLOC(ptr, size) tc_realloc(ptr, size)
211-
#define STARROCKS_CALLOC(number, size) tc_calloc(number, size)
212-
#define STARROCKS_ALIGNED_ALLOC(align, size) tc_memalign(align, size)
213-
#define STARROCKS_POSIX_MEMALIGN(ptr, align, size) tc_posix_memalign(ptr, align, size)
214-
#define STARROCKS_CFREE(ptr) tc_cfree(ptr)
215-
#define STARROCKS_VALLOC(size) tc_valloc(size)
216-
#endif
217198

218199
#ifndef BE_TEST
219200
#define MEMORY_CONSUME_SIZE(size) \
@@ -256,7 +237,6 @@ std::atomic<int64_t> g_mem_usage(0);
256237
#define IS_BAD_ALLOC_CATCHED() false
257238
#endif
258239

259-
#ifdef USE_JEMALLOC
260240
const size_t large_memory_alloc_report_threshold = 1073741824;
261241
inline thread_local bool skip_report = false;
262242
inline void report_large_memory_alloc(size_t size) {
@@ -271,9 +251,6 @@ inline void report_large_memory_alloc(size_t size) {
271251
}
272252
}
273253
#define STARROCKS_REPORT_LARGE_MEM_ALLOC(size) report_large_memory_alloc(size)
274-
#else
275-
#define STARROCKS_REPORT_LARGE_MEM_ALLOC(size) (void)0
276-
#endif
277254

278255
extern "C" {
279256
// malloc
@@ -491,7 +468,5 @@ void* aligned_alloc(size_t align, size_t size) __THROW ALIAS(my_aligned_alloc);
491468
void* valloc(size_t size) __THROW ALIAS(my_valloc);
492469
void* pvalloc(size_t size) __THROW ALIAS(my_pvalloc);
493470
int posix_memalign(void** r, size_t a, size_t s) __THROW ALIAS(my_posix_memalign);
494-
#ifdef USE_JEMALLOC
495471
size_t malloc_usable_size(void* ptr) __THROW ALIAS(my_malloc_usebale_size);
496-
#endif
497472
}

be/src/service/starrocks_main.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ int main(int argc, char** argv) {
134134
exit(-1);
135135
}
136136

137-
if (getenv("TCMALLOC_HEAP_LIMIT_MB") == nullptr) {
138-
fprintf(stderr,
139-
"Environment variable TCMALLOC_HEAP_LIMIT_MB is not set,"
140-
" maybe you forgot to replace bin directory\n");
141-
exit(-1);
142-
}
143-
144137
// S2 will crashes when deserialization fails and FLAGS_s2debug was true.
145138
FLAGS_s2debug = false;
146139

@@ -190,22 +183,6 @@ int main(int argc, char** argv) {
190183
starrocks::Status::access_directory_of_inject();
191184
#endif
192185

193-
#if !defined(ADDRESS_SANITIZER) && !defined(LEAK_SANITIZER) && !defined(THREAD_SANITIZER) && !defined(USE_JEMALLOC)
194-
// Aggressive decommit is required so that unused pages in the TCMalloc page heap are
195-
// not backed by physical pages and do not contribute towards memory consumption.
196-
//
197-
// 2020-08-31: Disable aggressive decommit, which will decrease the performance of
198-
// memory allocation and deallocation.
199-
// MallocExtension::instance()->SetNumericProperty("tcmalloc.aggressive_memory_decommit", 1);
200-
201-
// Change the total TCMalloc thread cache size if necessary.
202-
if (!MallocExtension::instance()->SetNumericProperty("tcmalloc.max_total_thread_cache_bytes",
203-
starrocks::config::tc_max_total_thread_cache_bytes)) {
204-
fprintf(stderr, "Failed to change TCMalloc total thread cache size.\n");
205-
return -1;
206-
}
207-
#endif
208-
209186
Aws::SDKOptions aws_sdk_options;
210187
if (starrocks::config::aws_sdk_logging_trace_enabled) {
211188
auto level = parse_aws_sdk_log_level(starrocks::config::aws_sdk_logging_trace_level);

0 commit comments

Comments
 (0)