Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef TSD_PUB_FACILITY_UTIL_FUNC_TSD_UTIL_FUNC_H
12 : #define TSD_PUB_FACILITY_UTIL_FUNC_TSD_UTIL_FUNC_H
13 :
14 : #include <memory>
15 : #include <chrono>
16 : #include <string>
17 : #include <vector>
18 : #include "tsd/status.h"
19 : #include "tsd_log.h"
20 : #include "tsd_scope_guard.h"
21 :
22 : #define TSD_CHECK_NO_RETURN(condition, log, ...) \
23 : do { \
24 : const bool cond = (condition); \
25 : if (!cond) { \
26 : TSD_ERROR(log, ##__VA_ARGS__); \
27 : } \
28 : } while (false)
29 :
30 : #define TSD_CHECK_NO_RETURN_RUNINFO_LOG(condition, log, ...) \
31 : do { \
32 : const bool cond = (condition); \
33 : if (!cond) { \
34 : TSD_RUN_INFO(log, ##__VA_ARGS__); \
35 : } \
36 : } while (false)
37 :
38 : #define TSD_CHECK_EQ_RETURN_RUNWARN_LOG(condition, errCode, log, ...) \
39 : do { \
40 : const bool cond = (condition); \
41 : if (cond) { \
42 : TSD_RUN_WARN(log, ##__VA_ARGS__); \
43 : return (errCode); \
44 : } \
45 : } while (false)
46 :
47 : #define TSD_CHECK(condition, retValue, log, ...) \
48 : do { \
49 : const bool cond = (condition); \
50 : if (!cond) { \
51 : TSD_ERROR(log, ##__VA_ARGS__); \
52 : return (retValue); \
53 : } \
54 : } while (false)
55 :
56 : #define TSD_CHECK_NULLPTR_VOID(value) \
57 : do { \
58 : if ((value) == nullptr) { \
59 : return; \
60 : } \
61 : } while (false)
62 :
63 : #define TSD_CHECK_NULLPTR(value, errorCode, log, ...) \
64 : do { \
65 : if ((value) == nullptr) { \
66 : TSD_ERROR(log, ##__VA_ARGS__); \
67 : return (errorCode); \
68 : } \
69 : } while (false)
70 :
71 : #define TSD_CHECK_RET_VOID(condition, log, ...) \
72 : do { \
73 : const bool cond = (condition); \
74 : if (!cond) { \
75 : TSD_ERROR(log, ##__VA_ARGS__); \
76 : return; \
77 : } \
78 : } while (false)
79 :
80 : #define TSD_BITMAP_GET(val, pos) (((val) >> (pos)) & 0x01U)
81 :
82 : #define TSD_BITMAP_SET(val, pos) ((val) |= (1ULL << (pos)))
83 :
84 : namespace tsd {
85 :
86 : constexpr uint32_t MAX_DEVNUM_PER_OS = 64U; // 当前单OS上芯片最大数是64个
87 :
88 : constexpr uint32_t TSDCLIENT_SUPPORT_NEW_ERRORCODE = 1U;
89 :
90 : constexpr uint32_t MAX_DEVNUM_PER_HOST = 128U; // 当前host侧看到的是两个OS拼接后的芯片数8个,对应不同进程
91 :
92 : constexpr int32_t TDT_RETURN_ERROR = -1;
93 :
94 : constexpr uint64_t S_TO_US = 1000000UL;
95 :
96 : constexpr uint64_t MS_TO_US = 1000UL;
97 :
98 : constexpr uint64_t S_TO_MS = 1000UL;
99 :
100 : constexpr uint32_t PER_OS_CHIP_NUM = 4U;
101 :
102 : void Trim(std::string& str);
103 :
104 : // 修副本中任意空白字符(含空格/制表符/换行/回车等)的首尾
105 : void TrimWhitespace(std::string& str);
106 :
107 : // 按字符 sep 切分,不跳过空串
108 : std::vector<std::string> SplitByChar(const std::string& s, char sep);
109 :
110 : // 两串完全为数字时按数值比较,否则字典序。返回 -1/0/1
111 : int32_t CompareSegmentNumeric(const std::string& a, const std::string& b);
112 :
113 : uint64_t CalFileSize(const std::string& filePath);
114 :
115 : bool ValidateStr(const std::string& str, const std::string& mode);
116 :
117 : bool IsFpgaEnv();
118 :
119 : inline uint64_t GetCurrentTime()
120 : {
121 : uint64_t ret = 0U;
122 : struct timeval tv;
123 : if (gettimeofday(&tv, nullptr) == 0) {
124 : ret = (static_cast<uint64_t>(tv.tv_sec) * S_TO_US) + static_cast<uint64_t>(tv.tv_usec);
125 : }
126 :
127 : return ret;
128 : }
129 :
130 2 : inline bool IsHeterogeneousProduct()
131 : {
132 : #ifdef TSD_HELPER
133 : return true;
134 : #else
135 2 : return false;
136 : #endif
137 : }
138 :
139 : void GetScheduleEnv(const char_t* const envName, std::string& envValue);
140 :
141 : bool CheckRealPath(const std::string& inputPath);
142 :
143 : bool CheckValidatePath(const std::string& path);
144 :
145 : int32_t PackSystem(const char_t* const cmdLine);
146 :
147 : std::string SafeStrerror();
148 :
149 : uint32_t CalcUniqueVfId(const uint32_t deviceId, const uint32_t vfId);
150 :
151 : bool TransStrToInt(const std::string& para, int32_t& value);
152 :
153 : void RemoveOneFile(const std::string& filePath);
154 :
155 : std::string CalFileSha256HashValue(const std::string& filePath);
156 :
157 : bool IsCurrentVfMode(const uint32_t deviceId, const uint32_t vfId);
158 :
159 : bool IsVfModeCheckedByDeviceId(const uint32_t deviceId);
160 :
161 : bool IsDirEmpty(const std::string& dirPath);
162 :
163 : bool GetFlagFromEnv(const char_t* const envStr, const char_t* const envValue);
164 :
165 : // Locate the directory of the host-side HAL shared library (the .so that
166 : // exports drvHdcSendFile) by inspecting the address of that symbol with
167 : // dladdr(3). Returns an empty string on failure.
168 : std::string GetHostSoPath();
169 :
170 : std::string ExtractSubString(const std::string& input, const std::string& begin, const std::string& end);
171 : } // namespace tsd
172 : #endif // TSD_PUB_FACILITY_UTIL_FUNC_TSD_UTIL_FUNC_H
|