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 | | |
27 | | /* |
28 | | * can be consumed manually by consume()/release(), or put into SCOPED_CONSUME_MEM_TRACKER, |
29 | | * which will automatically track all memory usage of the code segment where it is located. |
30 | | * |
31 | | * This class is thread-safe. |
32 | | */ |
33 | | class MemTracker final { |
34 | | public: |
35 | 57.5k | MemTracker() = default; |
36 | 49.9M | MemTracker(std::string label) : _label(std::move(label)) {}; |
37 | 50.0M | ~MemTracker() = default; |
38 | | |
39 | 37.5M | void consume(int64_t bytes) { _mem_counter.add(bytes); } |
40 | 0 | void consume_no_update_peak(int64_t bytes) { _mem_counter.add_no_update_peak(bytes); } |
41 | 2.27M | void release(int64_t bytes) { _mem_counter.sub(bytes); } |
42 | 217k | void set_consumption(int64_t bytes) { _mem_counter.set(bytes); } |
43 | 4.84M | int64_t consumption() const { return _mem_counter.current_value(); } |
44 | 46.7M | int64_t peak_consumption() const { return _mem_counter.peak_value(); } |
45 | | |
46 | 6 | const std::string& label() const { return _label; } |
47 | 55 | std::string log_usage() const { |
48 | 55 | return fmt::format("MemTracker name={}, Used={}({} B), Peak={}({} B)", _label, |
49 | 55 | PrettyPrinter::print_bytes(consumption()), consumption(), |
50 | 55 | PrettyPrinter::print_bytes(peak_consumption()), peak_consumption()); |
51 | 55 | } |
52 | | |
53 | | private: |
54 | | MemCounter _mem_counter; |
55 | | std::string _label {"None"}; |
56 | | }; |
57 | | |
58 | | } // namespace doris |