be/src/runtime/memory/mem_tracker.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | #pragma once |
19 | | |
20 | | #include <cstdint> |
21 | | #include <string> |
22 | | |
23 | | #include "runtime/memory/mem_counter.h" |
24 | | |
25 | | namespace doris { |
26 | | #include "common/compile_check_begin.h" |
27 | | |
28 | | /* |
29 | | * can be consumed manually by consume()/release(), or put into SCOPED_CONSUME_MEM_TRACKER, |
30 | | * which will automatically track all memory usage of the code segment where it is located. |
31 | | * |
32 | | * This class is thread-safe. |
33 | | */ |
34 | | class MemTracker final { |
35 | | public: |
36 | 180k | MemTracker() = default; |
37 | 70.5M | MemTracker(std::string label) : _label(std::move(label)) {}; |
38 | 70.7M | ~MemTracker() = default; |
39 | | |
40 | 46.9M | void consume(int64_t bytes) { _mem_counter.add(bytes); } |
41 | 0 | void consume_no_update_peak(int64_t bytes) { _mem_counter.add_no_update_peak(bytes); } |
42 | 2.89M | void release(int64_t bytes) { _mem_counter.sub(bytes); } |
43 | 100k | void set_consumption(int64_t bytes) { _mem_counter.set(bytes); } |
44 | 4.71M | int64_t consumption() const { return _mem_counter.current_value(); } |
45 | 67.4M | int64_t peak_consumption() const { return _mem_counter.peak_value(); } |
46 | | |
47 | 6 | const std::string& label() const { return _label; } |
48 | 83 | std::string log_usage() const { |
49 | 83 | return fmt::format("MemTracker name={}, Used={}({} B), Peak={}({} B)", _label, |
50 | 83 | PrettyPrinter::print_bytes(consumption()), consumption(), |
51 | 83 | PrettyPrinter::print_bytes(peak_consumption()), peak_consumption()); |
52 | 83 | } |
53 | | |
54 | | private: |
55 | | MemCounter _mem_counter; |
56 | | std::string _label {"None"}; |
57 | | }; |
58 | | |
59 | | #include "common/compile_check_end.h" |
60 | | } // namespace doris |