/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 | 362 | int64_t physical_mem, bool* is_percent) { |
29 | 362 | if (mem_spec_str.empty()) { |
30 | 0 | return 0; |
31 | 0 | } |
32 | | |
33 | | // Assume last character indicates unit or percent. |
34 | 362 | int32_t number_str_len = mem_spec_str.size() - 1; |
35 | 362 | *is_percent = false; |
36 | 362 | int64_t multiplier = -1; |
37 | | |
38 | | // Look for accepted suffix character. |
39 | 362 | 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 | 4 | case 'G': |
47 | | // Gigabytes. |
48 | 4 | multiplier = 1024L * 1024L * 1024L; |
49 | 4 | break; |
50 | 1 | case 'm': |
51 | 11 | case 'M': |
52 | | // Megabytes. |
53 | 11 | multiplier = 1024L * 1024L; |
54 | 11 | 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 | 324 | case '%': |
64 | 324 | *is_percent = true; |
65 | 324 | break; |
66 | 8 | default: |
67 | | // No unit was given. Default to bytes. |
68 | 8 | number_str_len = mem_spec_str.size(); |
69 | 8 | break; |
70 | 362 | } |
71 | | |
72 | 362 | StringParser::ParseResult result; |
73 | 362 | int64_t bytes = -1; |
74 | | |
75 | 362 | if (multiplier != -1 || *is_percent) { |
76 | | // Parse float - MB or GB or percent |
77 | 343 | double limit_val = |
78 | 343 | StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result); |
79 | | |
80 | 343 | if (result != StringParser::PARSE_SUCCESS) { |
81 | 3 | return -1; |
82 | 3 | } |
83 | | |
84 | 340 | if (multiplier != -1) { |
85 | 19 | bytes = int64_t(multiplier * limit_val); |
86 | 321 | } else if (*is_percent) { |
87 | 321 | if (parent_limit == -1) { |
88 | 13 | bytes = int64_t(static_cast<double>(limit_val) / 100.0 * physical_mem); |
89 | 308 | } else { |
90 | 308 | bytes = int64_t(static_cast<double>(limit_val) / 100.0 * parent_limit); |
91 | 308 | } |
92 | 321 | } |
93 | 340 | } 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) { |
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) { |
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 | 349 | if (bytes == -1) { |
113 | 0 | return 0; |
114 | 0 | } |
115 | 349 | return bytes; |
116 | 349 | } |
117 | | |
118 | | } // namespace doris |