EmmyMiao87 commented on a change in pull request #7198:
URL: https://github.com/apache/incubator-doris/pull/7198#discussion_r764634245



##########
File path: be/src/runtime/thread_mem_tracker.cpp
##########
@@ -0,0 +1,129 @@
+// 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 "runtime/thread_mem_tracker.h"
+
+#include "service/backend_options.h"
+
+namespace doris {
+
+void ThreadMemTracker::attach_query(const std::string& query_id,
+                                    const TUniqueId& fragment_instance_id) {
+#ifdef BE_TEST
+    if (ExecEnv::GetInstance()->query_mem_tracker_registry() == nullptr) {
+        return;
+    }
+#endif
+    update_query_mem_tracker(
+            
ExecEnv::GetInstance()->query_mem_tracker_registry()->get_query_mem_tracker(query_id));
+    _fragment_instance_id = fragment_instance_id;
+}
+
+void ThreadMemTracker::detach_query() {
+    update_query_mem_tracker(std::weak_ptr<MemTracker>());
+    _fragment_instance_id = TUniqueId();
+}
+
+void ThreadMemTracker::update_query_mem_tracker(std::weak_ptr<MemTracker> 
mem_tracker) {
+    if (_untracked_mem != 0) {
+        consume();
+        _untracked_mem = 0;
+    }
+    _query_mem_tracker = mem_tracker;
+}
+
+void ThreadMemTracker::query_mem_limit_exceeded(int64_t mem_usage) {
+    if (_fragment_instance_id != TUniqueId() && 
ExecEnv::GetInstance()->initialized() &&
+        
ExecEnv::GetInstance()->fragment_mgr()->is_canceling(_fragment_instance_id).ok())
 {
+        std::string detail = "Query Memory exceed limit in TCMalloc Hook New.";
+        auto st = _query_mem_tracker.lock()->MemLimitExceeded(nullptr, detail, 
mem_usage);
+
+        detail +=
+                " Query Memory exceed limit in TCMalloc Hook New, Backend: {}, 
Fragment: {}, Used: "
+                "{}, Limit: {}. You can change the limit by session variable 
exec_mem_limit.";
+        fmt::format(detail, BackendOptions::get_localhost(), 
print_id(_fragment_instance_id),

Review comment:
       Is it better to print query id?

##########
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:
       Didn't use?

##########
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();

Review comment:
       ```suggestion
       private void consume();
   ```




-- 
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]

Reply via email to