Coverage Report

Created: 2025-07-28 21:04

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