xinyiZzz commented on a change in pull request #7198: URL: https://github.com/apache/incubator-doris/pull/7198#discussion_r757446999
########## File path: be/src/runtime/thread_status.h ########## @@ -0,0 +1,78 @@ +// 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 <string> + +#include "runtime/exec_env.h" +#include "runtime/mem_tracker.h" + +namespace doris { + +class ThreadStatus { +public: + ThreadStatus() : _thread_id(std::this_thread::get_id()) {} + ~ThreadStatus() { update_mem_tracker(nullptr); } + + void attach_query(const doris::TUniqueId& query_id) { + _query_id = doris::print_id(query_id); + update_mem_tracker(ExecEnv::GetInstance()->query_mem_trackers()->RegisterQueryMemTracker( + doris::print_id(query_id))); + } + + void update_mem_tracker(std::shared_ptr<MemTracker> mem_tracker) { + if (_untracked_mem != 0 && _mem_tracker != nullptr) { + if (!_mem_tracker->TryConsume(_untracked_mem)) { + return; // add call back + } + _untracked_mem = 0; + } + _mem_tracker = mem_tracker; + } + + void consume(int64_t size) { + if (_mem_tracker == nullptr && ExecEnv::GetInstance()->is_init()) { + _mem_tracker = ExecEnv::GetInstance()->hook_process_mem_tracker(); + } + _untracked_mem += size; Review comment: `_untracked_mem` only represents untracked memory for a short period of time. When there are more releases than consume during this period of time, `_untracked_mem` may be less than 0. The statistical value of MemTacker is not expected to be less than 0, which is DCHECK in MemTacke consume/release -- 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]
