Coverage Report

Created: 2024-11-20 12:56

/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
250
                                  int64_t physical_mem, bool* is_percent) {
29
250
    if (mem_spec_str.empty()) {
30
0
        return 0;
31
0
    }
32
33
    // Assume last character indicates unit or percent.
34
250
    int32_t number_str_len = mem_spec_str.size() - 1;
35
250
    *is_percent = false;
36
250
    int64_t multiplier = -1;
37
38
    // Look for accepted suffix character.
39
250
    switch (*mem_spec_str.rbegin()) {
40
1
    case 't':
41
2
    case 'T':
42
        // Terabytes.
43
2
        multiplier = 1024L * 1024L * 1024L * 1024L;
44
2
        break;
45
1
    case 'g':
46
2
    case 'G':
47
        // Gigabytes.
48
2
        multiplier = 1024L * 1024L * 1024L;
49
2
        break;
50
1
    case 'm':
51
2
    case 'M':
52
        // Megabytes.
53
2
        multiplier = 1024L * 1024L;
54
2
        break;
55
1
    case 'k':
56
2
    case 'K':
57
        // Kilobytes
58
2
        multiplier = 1024L;
59
2
        break;
60
10
    case 'b':
61
11
    case 'B':
62
11
        break;
63
228
    case '%':
64
228
        *is_percent = true;
65
228
        break;
66
3
    default:
67
        // No unit was given. Default to bytes.
68
3
        number_str_len = mem_spec_str.size();
69
3
        break;
70
250
    }
71
72
250
    StringParser::ParseResult result;
73
250
    int64_t bytes = -1;
74
75
250
    if (multiplier != -1 || *is_percent) {
76
        // Parse float - MB or GB or percent
77
236
        double limit_val =
78
236
                StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result);
79
80
236
        if (result != StringParser::PARSE_SUCCESS) {
81
3
            return -1;
82
3
        }
83
84
233
        if (multiplier != -1) {
85
8
            bytes = multiplier * limit_val;
86
225
        } else if (*is_percent) {
87
225
            if (parent_limit == -1) {
88
5
                bytes = (static_cast<double>(limit_val) / 100.0) * physical_mem;
89
220
            } else {
90
220
                bytes = (static_cast<double>(limit_val) / 100.0) * parent_limit;
91
220
            }
92
225
        }
93
233
    } else {
94
        // Parse int - bytes
95
14
        int64_t limit_val =
96
14
                StringParser::string_to_int<int64_t>(mem_spec_str.data(), number_str_len, &result);
97
98
14
        if (result != StringParser::PARSE_SUCCESS) {
99
10
            return -1;
100
10
        }
101
4
        bytes = limit_val;
102
4
    }
103
104
    // Accept -1 as indicator for infinite memory that we report by a 0 return value.
105
237
    if (bytes == -1) {
106
0
        return 0;
107
0
    }
108
237
    return bytes;
109
237
}
110
111
} // namespace doris