Coverage Report

Created: 2024-11-21 13:02

/root/doris/be/src/util/pretty_printer.h
Line
Count
Source (jump to first uncovered line)
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
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/pretty-printer.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/RuntimeProfile_types.h>
24
25
#include <boost/algorithm/string.hpp>
26
#include <cmath>
27
#include <iomanip>
28
#include <sstream>
29
30
#include "util/binary_cast.hpp"
31
#include "util/cpu_info.h"
32
33
/// Truncate a double to offset decimal places.
34
0
#define DOUBLE_TRUNCATE(val, offset) floor(val* pow(10, offset)) / pow(10, offset)
35
36
namespace doris {
37
38
/// Methods for printing numeric values with optional units, or other types with an
39
/// applicable operator<<.
40
class PrettyPrinter {
41
public:
42
0
    static std::string print(bool value, TUnit::type ignored, bool verbose = false) {
43
0
        std::stringstream ss;
44
0
        ss << std::boolalpha << value;
45
0
        return ss.str();
46
0
    }
47
48
    /// Prints the 'value' in a human friendly format depending on the data type.
49
    /// i.e. for bytes: 3145728 -> 3MB
50
    /// If verbose is true, this also prints the raw value (before unit conversion) for
51
    /// types where this is applicable.
52
    template <typename T>
53
    static typename std::enable_if<std::is_arithmetic<T>::value, std::string>::type print(
54
3
            T value, TUnit::type unit, bool verbose = false) {
55
3
        std::stringstream ss;
56
3
        ss.flags(std::ios::fixed);
57
3
        switch (unit) {
58
0
        case TUnit::NONE: {
59
            // TUnit::NONE is used as a special counter, it is just a label
60
            //         - PeakMemoryUsage:
61
            //              - BuildKeyArena: 0
62
            //              - ProbeKeyArena: 0
63
            // So do not need output its value
64
0
            return ss.str();
65
0
        }
66
67
0
        case TUnit::UNIT: {
68
0
            std::string unit;
69
0
            double output = get_unit(value, &unit);
70
0
            if (unit.empty()) {
71
0
                ss << value;
72
0
            } else {
73
0
                ss << std::setprecision(PRECISION) << output << unit;
74
0
            }
75
0
            if (verbose) ss << " (" << value << ")";
76
0
            break;
77
0
        }
78
79
0
        case TUnit::UNIT_PER_SECOND: {
80
0
            std::string unit;
81
0
            double output = get_unit(value, &unit);
82
0
            if (output == 0) {
83
0
                ss << "0";
84
0
            } else {
85
0
                ss << std::setprecision(PRECISION) << output << " " << unit << "/sec";
86
0
            }
87
0
            break;
88
0
        }
89
90
0
        case TUnit::CPU_TICKS: {
91
0
            if (value < CpuInfo::cycles_per_ms()) {
92
0
                ss << std::setprecision(PRECISION) << (value / 1000.) << "K clock cycles";
93
0
            } else {
94
0
                value /= CpuInfo::cycles_per_ms();
95
0
                print_timems(value, &ss);
96
0
            }
97
0
            break;
98
0
        }
99
100
0
        case TUnit::TIME_NS: {
101
0
            ss << std::setprecision(TIME_NS_PRECISION);
102
0
            if (value >= BILLION) {
103
                /// If the time is over a second, print it up to ms.
104
0
                value /= MILLION;
105
0
                print_timems(value, &ss);
106
0
            } else if (value >= MILLION) {
107
                /// if the time is over a ms, print it up to microsecond in the unit of ms.
108
0
                ss << DOUBLE_TRUNCATE(static_cast<double>(value) / MILLION, TIME_NS_PRECISION)
109
0
                   << "ms";
110
0
            } else if (value > 1000) {
111
                /// if the time is over a microsecond, print it using unit microsecond
112
0
                ss << DOUBLE_TRUNCATE(static_cast<double>(value) / 1000, TIME_NS_PRECISION) << "us";
113
0
            } else {
114
0
                ss << DOUBLE_TRUNCATE(value, TIME_NS_PRECISION) << "ns";
115
0
            }
116
0
            break;
117
0
        }
118
119
0
        case TUnit::TIME_MS: {
120
0
            print_timems(value, &ss);
121
0
            break;
122
0
        }
123
124
0
        case TUnit::TIME_S: {
125
0
            print_timems(value * 1000, &ss);
126
0
            break;
127
0
        }
128
129
3
        case TUnit::BYTES: {
130
3
            std::string unit;
131
3
            double output = get_byte_unit(value, &unit);
132
3
            if (output == 0) {
133
0
                ss << "0";
134
3
            } else {
135
3
                ss << std::setprecision(PRECISION) << output << " " << unit;
136
3
                if (verbose) ss << " (" << value << ")";
137
3
            }
138
3
            break;
139
0
        }
140
141
0
        case TUnit::BYTES_PER_SECOND: {
142
0
            std::string unit;
143
0
            double output = get_byte_unit(value, &unit);
144
0
            ss << std::setprecision(PRECISION) << output << " " << unit << "/sec";
145
0
            break;
146
0
        }
147
148
        /// TODO: Remove DOUBLE_VALUE. IMPALA-1649
149
0
        case TUnit::DOUBLE_VALUE: {
150
0
            double output = binary_cast<T, double>(value);
151
0
            ss << std::setprecision(PRECISION) << output << " ";
152
0
            break;
153
0
        }
154
155
0
        default:
156
0
            DCHECK(false) << "Unsupported TUnit: " << value;
157
0
            break;
158
3
        }
159
3
        return ss.str();
160
3
    }
_ZN5doris13PrettyPrinter5printIlEENSt9enable_ifIXsr3std13is_arithmeticIT_EE5valueENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE4typeES3_NS_5TUnit4typeEb
Line
Count
Source
54
3
            T value, TUnit::type unit, bool verbose = false) {
55
3
        std::stringstream ss;
56
3
        ss.flags(std::ios::fixed);
57
3
        switch (unit) {
58
0
        case TUnit::NONE: {
59
            // TUnit::NONE is used as a special counter, it is just a label
60
            //         - PeakMemoryUsage:
61
            //              - BuildKeyArena: 0
62
            //              - ProbeKeyArena: 0
63
            // So do not need output its value
64
0
            return ss.str();
65
0
        }
66
67
0
        case TUnit::UNIT: {
68
0
            std::string unit;
69
0
            double output = get_unit(value, &unit);
70
0
            if (unit.empty()) {
71
0
                ss << value;
72
0
            } else {
73
0
                ss << std::setprecision(PRECISION) << output << unit;
74
0
            }
75
0
            if (verbose) ss << " (" << value << ")";
76
0
            break;
77
0
        }
78
79
0
        case TUnit::UNIT_PER_SECOND: {
80
0
            std::string unit;
81
0
            double output = get_unit(value, &unit);
82
0
            if (output == 0) {
83
0
                ss << "0";
84
0
            } else {
85
0
                ss << std::setprecision(PRECISION) << output << " " << unit << "/sec";
86
0
            }
87
0
            break;
88
0
        }
89
90
0
        case TUnit::CPU_TICKS: {
91
0
            if (value < CpuInfo::cycles_per_ms()) {
92
0
                ss << std::setprecision(PRECISION) << (value / 1000.) << "K clock cycles";
93
0
            } else {
94
0
                value /= CpuInfo::cycles_per_ms();
95
0
                print_timems(value, &ss);
96
0
            }
97
0
            break;
98
0
        }
99
100
0
        case TUnit::TIME_NS: {
101
0
            ss << std::setprecision(TIME_NS_PRECISION);
102
0
            if (value >= BILLION) {
103
                /// If the time is over a second, print it up to ms.
104
0
                value /= MILLION;
105
0
                print_timems(value, &ss);
106
0
            } else if (value >= MILLION) {
107
                /// if the time is over a ms, print it up to microsecond in the unit of ms.
108
0
                ss << DOUBLE_TRUNCATE(static_cast<double>(value) / MILLION, TIME_NS_PRECISION)
109
0
                   << "ms";
110
0
            } else if (value > 1000) {
111
                /// if the time is over a microsecond, print it using unit microsecond
112
0
                ss << DOUBLE_TRUNCATE(static_cast<double>(value) / 1000, TIME_NS_PRECISION) << "us";
113
0
            } else {
114
0
                ss << DOUBLE_TRUNCATE(value, TIME_NS_PRECISION) << "ns";
115
0
            }
116
0
            break;
117
0
        }
118
119
0
        case TUnit::TIME_MS: {
120
0
            print_timems(value, &ss);
121
0
            break;
122
0
        }
123
124
0
        case TUnit::TIME_S: {
125
0
            print_timems(value * 1000, &ss);
126
0
            break;
127
0
        }
128
129
3
        case TUnit::BYTES: {
130
3
            std::string unit;
131
3
            double output = get_byte_unit(value, &unit);
132
3
            if (output == 0) {
133
0
                ss << "0";
134
3
            } else {
135
3
                ss << std::setprecision(PRECISION) << output << " " << unit;
136
3
                if (verbose) ss << " (" << value << ")";
137
3
            }
138
3
            break;
139
0
        }
140
141
0
        case TUnit::BYTES_PER_SECOND: {
142
0
            std::string unit;
143
0
            double output = get_byte_unit(value, &unit);
144
0
            ss << std::setprecision(PRECISION) << output << " " << unit << "/sec";
145
0
            break;
146
0
        }
147
148
        /// TODO: Remove DOUBLE_VALUE. IMPALA-1649
149
0
        case TUnit::DOUBLE_VALUE: {
150
0
            double output = binary_cast<T, double>(value);
151
0
            ss << std::setprecision(PRECISION) << output << " ";
152
0
            break;
153
0
        }
154
155
0
        default:
156
0
            DCHECK(false) << "Unsupported TUnit: " << value;
157
0
            break;
158
3
        }
159
3
        return ss.str();
160
3
    }
Unexecuted instantiation: _ZN5doris13PrettyPrinter5printImEENSt9enable_ifIXsr3std13is_arithmeticIT_EE5valueENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE4typeES3_NS_5TUnit4typeEb
161
162
    /// For non-arithmetics, just write the value as a string and return it.
163
    //
164
    /// TODO: There's no good is_string equivalent, so there's a needless copy for strings
165
    /// here.
166
    template <typename T>
167
    static typename std::enable_if<!std::is_arithmetic<T>::value, std::string>::type print(
168
1
            const T& value, TUnit::type unit) {
169
1
        std::stringstream ss;
170
1
        ss << std::boolalpha << value;
171
1
        return ss.str();
172
1
    }
173
174
    /// Utility method to print an iterable type to a stringstream like [v1, v2, v3]
175
    template <typename I>
176
    static void print_stringList(const I& iterable, TUnit::type unit, std::stringstream* out) {
177
        std::vector<std::string> strings;
178
        for (typename I::const_iterator it = iterable.begin(); it != iterable.end(); ++it) {
179
            std::stringstream ss;
180
            ss << PrettyPrinter::print(*it, unit);
181
            strings.push_back(ss.str());
182
        }
183
184
        (*out) << "[" << boost::algorithm::join(strings, ", ") << "]";
185
    }
186
187
    /// Convenience method
188
0
    static std::string print_bytes(int64_t value) {
189
0
        return PrettyPrinter::print(value, TUnit::BYTES);
190
0
    }
191
192
private:
193
    static const int PRECISION = 2;
194
    static const int TIME_NS_PRECISION = 3;
195
    static const int64_t KILOBYTE = 1024;
196
    static const int64_t MEGABYTE = KILOBYTE * 1024;
197
    static const int64_t GIGABYTE = MEGABYTE * 1024;
198
199
    static const int64_t SECOND = 1000;
200
    static const int64_t MINUTE = SECOND * 60;
201
    static const int64_t HOUR = MINUTE * 60;
202
203
    static const int64_t THOUSAND = 1000;
204
    static const int64_t MILLION = THOUSAND * 1000;
205
    static const int64_t BILLION = MILLION * 1000;
206
207
    template <typename T>
208
3
    static double get_byte_unit(T value, std::string* unit) {
209
3
        if (value == 0) {
210
0
            *unit = "";
211
0
            return value;
212
3
        } else if (value >= GIGABYTE) {
213
1
            *unit = "GB";
214
1
            return value / (double)GIGABYTE;
215
2
        } else if (value >= MEGABYTE) {
216
2
            *unit = "MB";
217
2
            return value / (double)MEGABYTE;
218
2
        } else if (value >= KILOBYTE) {
219
0
            *unit = "KB";
220
0
            return value / (double)KILOBYTE;
221
0
        } else {
222
0
            *unit = "B";
223
0
            return value;
224
0
        }
225
3
    }
_ZN5doris13PrettyPrinter13get_byte_unitIlEEdT_PNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
208
3
    static double get_byte_unit(T value, std::string* unit) {
209
3
        if (value == 0) {
210
0
            *unit = "";
211
0
            return value;
212
3
        } else if (value >= GIGABYTE) {
213
1
            *unit = "GB";
214
1
            return value / (double)GIGABYTE;
215
2
        } else if (value >= MEGABYTE) {
216
2
            *unit = "MB";
217
2
            return value / (double)MEGABYTE;
218
2
        } else if (value >= KILOBYTE) {
219
0
            *unit = "KB";
220
0
            return value / (double)KILOBYTE;
221
0
        } else {
222
0
            *unit = "B";
223
0
            return value;
224
0
        }
225
3
    }
Unexecuted instantiation: _ZN5doris13PrettyPrinter13get_byte_unitImEEdT_PNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
226
227
    template <typename T>
228
0
    static double get_unit(T value, std::string* unit) {
229
0
        if (value >= BILLION) {
230
0
            *unit = "B";
231
0
            return value / (1. * BILLION);
232
0
        } else if (value >= MILLION) {
233
0
            *unit = "M";
234
0
            return value / (1. * MILLION);
235
0
        } else if (value >= THOUSAND) {
236
0
            *unit = "K";
237
0
            return value / (1. * THOUSAND);
238
0
        } else {
239
0
            *unit = "";
240
0
            return value;
241
0
        }
242
0
    }
Unexecuted instantiation: _ZN5doris13PrettyPrinter8get_unitIlEEdT_PNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris13PrettyPrinter8get_unitImEEdT_PNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
243
244
    /// Utility to perform integer modulo if T is integral, otherwise to use fmod().
245
    template <typename T>
246
    static typename boost::enable_if_c<boost::is_integral<T>::value, int64_t>::type mod(
247
0
            const T& value, const int modulus) {
248
0
        return value % modulus;
249
0
    }
Unexecuted instantiation: _ZN5doris13PrettyPrinter3modIlEEN5boost11enable_if_cIXsr5boost11is_integralIT_EE5valueElE4typeERKS4_i
Unexecuted instantiation: _ZN5doris13PrettyPrinter3modImEEN5boost11enable_if_cIXsr5boost11is_integralIT_EE5valueElE4typeERKS4_i
250
251
    template <typename T>
252
    static typename boost::enable_if_c<!boost::is_integral<T>::value, double>::type mod(
253
            const T& value, int modulus) {
254
        return fmod(value, 1. * modulus);
255
    }
256
257
    /// Print the value (time in ms) to ss
258
    template <typename T>
259
0
    static void print_timems(T value, std::stringstream* ss) {
260
0
        DCHECK_GE(value, static_cast<T>(0));
261
0
        if (value == 0) {
262
0
            *ss << "0";
263
0
        } else {
264
0
            bool hour = false;
265
0
            bool minute = false;
266
0
            bool second = false;
267
0
            if (value >= HOUR) {
268
0
                *ss << static_cast<int64_t>(value / HOUR) << "h";
269
0
                value = mod(value, HOUR);
270
0
                hour = true;
271
0
            }
272
0
            if (value >= MINUTE) {
273
0
                *ss << static_cast<int64_t>(value / MINUTE) << "m";
274
0
                value = mod(value, MINUTE);
275
0
                minute = true;
276
0
            }
277
0
            if (!hour && value >= SECOND) {
278
0
                *ss << static_cast<int64_t>(value / SECOND) << "s";
279
0
                value = mod(value, SECOND);
280
0
                second = true;
281
0
            }
282
0
            if (!hour && !minute) {
283
0
                if (second) *ss << std::setw(3) << std::setfill('0');
284
0
                *ss << static_cast<int64_t>(value) << "ms";
285
0
            }
286
0
        }
287
0
    }
Unexecuted instantiation: _ZN5doris13PrettyPrinter12print_timemsIlEEvT_PNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris13PrettyPrinter12print_timemsImEEvT_PNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEE
288
};
289
290
} // namespace doris