xinyiZzz commented on a change in pull request #7198: URL: https://github.com/apache/incubator-doris/pull/7198#discussion_r764793130
########## File path: be/src/runtime/tcmalloc_hook.h ########## @@ -0,0 +1,46 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gperftools/malloc_hook.h> +#include <gperftools/nallocx.h> +#include <gperftools/tcmalloc.h> + +#include "runtime/thread_context.h" + +void new_hook(const void* ptr, size_t size) { + doris::thread_local_ctx.consume_mem(tc_nallocx(size, 0)); +} + +void delete_hook(const void* ptr) { + doris::thread_local_ctx.release_mem(tc_malloc_size(const_cast<void*>(ptr))); +} + +// Notice: modify the command in New/Delete Hook should be careful enough, +// and should be as simple as possible, otherwise it may cause weird errors. E.g: +// 1. The first New Hook call of the process may be before some variables of +// the process are initialized. +// 2. Allocating memory in the Hook command causes the Hook to be entered again, +// infinite recursion. +void init_hook() { + MallocHook::AddNewHook(&new_hook); + MallocHook::AddDeleteHook(&delete_hook); +} + +void destroy_hook() { Review comment: Yes, the reserved method is for the future. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
