1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
18 #include <sys/auxv.h>
19 #include <asm/hwcap.h>
20 #endif
21 #include "src/common/utils.h"
22 #if defined(_MSC_VER) || defined(_WIN32)
23 #include <windows.h>
24 #undef ERROR
25 #else
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #endif
30
31 namespace mindspore {
32 namespace lite {
GetTimeUs()33 uint64_t GetTimeUs() {
34 #ifdef SUPPORT_MSVC
35 const int sec_to_us = 1000000;
36 LARGE_INTEGER cur_time, frequency;
37 QueryPerformanceCounter(&cur_time);
38 QueryPerformanceFrequency(&frequency);
39 uint64_t sec = cur_time.QuadPart / frequency.QuadPart;
40 uint64_t usec = (cur_time.QuadPart % frequency.QuadPart) * sec_to_us / frequency.QuadPart;
41 return sec * sec_to_us + usec;
42 #else
43 struct timespec ts = {0, 0};
44 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
45 return 0;
46 }
47 // USECS_IN_SEC *NSECS_IN_USEC;
48 auto ret_val = static_cast<uint64_t>((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC));
49 return ret_val;
50 #endif
51 }
52
RemoveSubStr(const std::string & from,const std::string & sub_str,RemoveSubStrMode mode)53 std::string RemoveSubStr(const std::string &from, const std::string &sub_str, RemoveSubStrMode mode) {
54 std::string result = from;
55 if (from.empty()) {
56 MS_LOG(ERROR) << "string is empty";
57 return "";
58 }
59 if (sub_str.length() > from.length()) {
60 MS_LOG(ERROR) << "sub_str is longer than from";
61 return "";
62 }
63 if (mode == PREFIX) {
64 if (from.substr(0, sub_str.length()) == sub_str) {
65 result = from.substr(sub_str.length());
66 }
67 } else if (mode == SUFFIX) {
68 if (from.rfind(sub_str) == from.length() - sub_str.length()) {
69 result = from.substr(0, from.length() - sub_str.length());
70 }
71 } else {
72 size_t index;
73 while ((index = result.find(sub_str)) != std::string::npos) {
74 result = result.erase(index, sub_str.length());
75 }
76 }
77
78 return result;
79 }
80
StrSplit(const std::string & str,const std::string & pattern)81 std::vector<std::string> StrSplit(const std::string &str, const std::string &pattern) {
82 if (str.empty()) {
83 MS_LOG(ERROR) << "string is empty";
84 return {};
85 }
86 std::string::size_type pos;
87 std::vector<std::string> result;
88 std::string tmpStr(str + pattern);
89 std::string::size_type size = tmpStr.size();
90
91 for (std::string::size_type i = 0; i < size; i++) {
92 pos = tmpStr.find(pattern, i);
93 if (pos < size) {
94 std::string s = tmpStr.substr(i, pos - i);
95 result.push_back(s);
96 i = pos + pattern.size() - 1;
97 }
98 }
99 return result;
100 }
101
Tokenize(const std::string & src,const std::string & delimiters,const Option<size_t> & max_token_num)102 std::vector<std::string> Tokenize(const std::string &src, const std::string &delimiters,
103 const Option<size_t> &max_token_num) {
104 if (max_token_num.IsSome() && max_token_num.Get() == 0) {
105 return {};
106 }
107
108 if (src.empty()) {
109 MS_LOG(ERROR) << "string is empty";
110 return {};
111 }
112
113 std::vector<std::string> tokens;
114 size_t offset = 0;
115
116 while (true) {
117 size_t nonDelimiter = src.find_first_not_of(delimiters, offset);
118 if (nonDelimiter == std::string::npos) {
119 break;
120 }
121 size_t delimiter = src.find_first_of(delimiters, nonDelimiter);
122 if (delimiter == std::string::npos || (max_token_num.IsSome() && tokens.size() == max_token_num.Get() - 1)) {
123 tokens.push_back(src.substr(nonDelimiter));
124 break;
125 }
126
127 tokens.push_back(src.substr(nonDelimiter, delimiter - nonDelimiter));
128 offset = delimiter;
129 }
130 return tokens;
131 }
132
133 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
getHwCap(int hwcap_type)134 uint32_t getHwCap(int hwcap_type) {
135 uint32_t ret = getauxval(hwcap_type);
136 return ret;
137 }
138 #endif
139
IsSupportSDot()140 bool IsSupportSDot() {
141 bool status = false;
142 #ifdef ENABLE_ARM64
143 #if defined(__ANDROID__) || defined(MS_COMPILE_OHOS)
144 int hwcap_type = 16;
145 uint32_t hwcap = getHwCap(hwcap_type);
146 if (hwcap & HWCAP_ASIMDDP) {
147 MS_LOG(DEBUG) << "Hw cap support SMID Dot Product, hwcap: 0x" << hwcap;
148 status = true;
149 } else {
150 MS_LOG(DEBUG) << "Hw cap NOT support SIMD Dot Product, hwcap: 0x" << hwcap;
151 status = false;
152 }
153 #endif
154 #endif
155 return status;
156 }
157
GetMaxMallocSize()158 size_t GetMaxMallocSize() {
159 size_t max_malloc_size = 0;
160 #if defined(_MSC_VER) || defined(_WIN32)
161 MEMORYSTATUSEX status;
162 status.dwLength = sizeof(status);
163 GlobalMemoryStatusEx(&status);
164 max_malloc_size = static_cast<size_t>(status.ullTotalPhys);
165 #else
166 max_malloc_size = static_cast<size_t>(sysconf(_SC_PHYS_PAGES)) * static_cast<size_t>(sysconf(_SC_PAGESIZE));
167 #endif
168 return max_malloc_size;
169 }
170
171 } // namespace lite
172 } // namespace mindspore
173