Coverage Report

Created: 2025-04-30 15:20

/root/doris/be/src/util/parse_util.cpp
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/parse-util.cc
19
// and modified by Doris
20
21
#include "util/parse_util.h"
22
23
#include "util/string_parser.hpp"
24
25
namespace doris {
26
27
int64_t ParseUtil::parse_mem_spec(const std::string& mem_spec_str, int64_t parent_limit,
28
2.32k
                                  int64_t physical_mem, bool* is_percent) {
29
2.32k
    if (mem_spec_str.empty()) {
30
0
        return 0;
31
0
    }
32
33
    // Assume last character indicates unit or percent.
34
2.32k
    int32_t number_str_len = mem_spec_str.size() - 1;
35
2.32k
    *is_percent = false;
36
2.32k
    int64_t multiplier = -1;
37
38
    // Look for accepted suffix character.
39
2.32k
    switch (*mem_spec_str.rbegin()) {
40
2
    case 't':
41
4
    case 'T':
42
        // Terabytes.
43
4
        multiplier = 1024L * 1024L * 1024L * 1024L;
44
4
        break;
45
2
    case 'g':
46
8
    case 'G':
47
        // Gigabytes.
48
8
        multiplier = 1024L * 1024L * 1024L;
49
8
        break;
50
2
    case 'm':
51
14
    case 'M':
52
        // Megabytes.
53
14
        multiplier = 1024L * 1024L;
54
14
        break;
55
2
    case 'k':
56
4
    case 'K':
57
        // Kilobytes
58
4
        multiplier = 1024L;
59
4
        break;
60
20
    case 'b':
61
22
    case 'B':
62
22
        break;
63
2.25k
    case '%':
64
2.25k
        *is_percent = true;
65
2.25k
        break;
66
16
    default:
67
        // No unit was given. Default to bytes.
68
16
        number_str_len = mem_spec_str.size();
69
16
        break;
70
2.32k
    }
71
72
2.32k
    StringParser::ParseResult result;
73
2.32k
    int64_t bytes = -1;
74
75
2.32k
    if (multiplier != -1 || *is_percent) {
76
        // Parse float - MB or GB or percent
77
2.28k
        double limit_val =
78
2.28k
                StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result);
79
80
2.28k
        if (result != StringParser::PARSE_SUCCESS) {
81
6
            return -1;
82
6
        }
83
84
2.27k
        if (multiplier != -1) {
85
30
            bytes = int64_t(multiplier * limit_val);
86
2.24k
        } else if (*is_percent) {
87
2.24k
            if (parent_limit == -1) {
88
209
                bytes = int64_t(static_cast<double>(limit_val) / 100.0 * physical_mem);
89
2.03k
            } else {
90
2.03k
                bytes = int64_t(static_cast<double>(limit_val) / 100.0 * parent_limit);
91
2.03k
            }
92
2.24k
        }
93
2.27k
    } else {
94
        // Parse int - bytes
95
38
        int64_t limit_val =
96
38
                StringParser::string_to_int<int64_t>(mem_spec_str.data(), number_str_len, &result);
97
98
38
        if (result != StringParser::PARSE_SUCCESS) {
99
18
            return -1;
100
18
        }
101
102
20
        auto limit_val_double =
103
20
                StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result);
104
20
        if (result == StringParser::PARSE_SUCCESS && limit_val_double != limit_val) {
105
2
            return -1; // mem_spec_str is double.
106
2
        }
107
108
18
        bytes = limit_val;
109
18
    }
110
111
    // Accept -1 as indicator for infinite memory that we report by a 0 return value.
112
2.29k
    if (bytes == -1) {
113
0
        return 0;
114
0
    }
115
2.29k
    return bytes;
116
2.29k
}
117
118
} // namespace doris