xinyiZzz commented on a change in pull request #7198: URL: https://github.com/apache/incubator-doris/pull/7198#discussion_r764785774
########## File path: be/src/runtime/thread_mem_tracker.h ########## @@ -0,0 +1,80 @@ +// 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. + +#pragma once + +#include <fmt/format.h> + +#include "runtime/exec_env.h" +#include "runtime/fragment_mgr.h" +#include "runtime/mem_tracker.h" + +namespace doris { + +// TCMalloc new/delete Hook is counted in the memory_tracker of the current thread +class ThreadMemTracker { +public: + ThreadMemTracker() : _global_hook_tracker(MemTracker::GetGlobalHookTracker()) {} + ~ThreadMemTracker() { detach_query(); } + + // After attach, the current thread TCMalloc Hook starts to consume/release query mem_tracker + void attach_query(const std::string& query_id, const TUniqueId& fragment_instance_id); + + void detach_query(); + + void update_query_mem_tracker(std::weak_ptr<MemTracker> mem_tracker); + + void query_mem_limit_exceeded(int64_t mem_usage); + + void global_mem_limit_exceeded(int64_t mem_usage); + + // Note that, If call the memory allocation operation in TCMalloc new/delete Hook, + // such as calling LOG/iostream/sstream/stringstream/etc. related methods, + // must increase the control to avoid entering infinite recursion, otherwise it may cause crash or stuck, + void consume(); + + void try_consume(int64_t size); + + void stop_mem_tracker() { _stop_mem_tracker = true; } + +private: + TUniqueId _fragment_instance_id; + + std::weak_ptr<MemTracker> _query_mem_tracker; + std::shared_ptr<MemTracker> _global_hook_tracker = nullptr; + + // Consume size smaller than _tracker_consume_min_size will continue to accumulate + // to avoid frequent calls to consume/release of MemTracker. + int64_t _untracked_mem = 0; + int64_t _tracker_consume_min_size = config::mem_tracker_consume_min_size_mbytes; Review comment: Thats right, I think `_tracker_consume_cache_size` seems better~ -- 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]
