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_BITMAP_GET(val, pos) (((val) >> (pos)) & 0x01U)
72 :
73 : #define TSD_BITMAP_SET(val, pos) ((val) |= (1ULL << (pos)))
74 :
75 : namespace tsd {
76 :
77 : constexpr uint32_t MAX_DEVNUM_PER_OS = 64U; // 当前单OS上芯片最大数是64个
78 :
79 : constexpr uint32_t TSDCLIENT_SUPPORT_NEW_ERRORCODE = 1U;
80 :
81 : constexpr uint32_t MAX_DEVNUM_PER_HOST = 128U; // 当前host侧看到的是两个OS拼接后的芯片数8个,对应不同进程
82 :
83 : constexpr int32_t TDT_RETURN_ERROR = -1;
84 :
85 : constexpr uint64_t S_TO_US = 1000000UL;
86 :
87 : constexpr uint64_t MS_TO_US = 1000UL;
88 :
89 : constexpr uint64_t S_TO_MS = 1000UL;
90 :
91 : constexpr uint32_t PER_OS_CHIP_NUM = 4U;
92 :
93 : void Trim(std::string& str);
94 :
95 : // 修副本中任意空白字符(含空格/制表符/换行/回车等)的首尾
96 : void TrimWhitespace(std::string& str);
97 :
98 : // 按字符 sep 切分,不跳过空串
99 : std::vector<std::string> SplitByChar(const std::string& s, char sep);
100 :
101 : // 两串完全为数字时按数值比较,否则字典序。返回 -1/0/1
102 : int32_t CompareSegmentNumeric(const std::string& a, const std::string& b);
103 :
104 : uint64_t CalFileSize(const std::string& filePath);
105 :
106 : bool ValidateStr(const std::string& str, const std::string& mode);
107 :
108 : bool IsFpgaEnv();
109 :
110 : inline uint64_t GetCurrentTime()
111 : {
112 : uint64_t ret = 0U;
113 : struct timeval tv;
114 : if (gettimeofday(&tv, nullptr) == 0) {
115 : ret = (static_cast<uint64_t>(tv.tv_sec) * S_TO_US) + static_cast<uint64_t>(tv.tv_usec);
116 : }
117 :
118 : return ret;
119 : }
120 :
121 5 : inline bool IsHeterogeneousProduct()
122 : {
123 : #ifdef TSD_HELPER
124 : return true;
125 : #else
126 5 : return false;
127 : #endif
128 : }
129 :
130 : void GetScheduleEnv(const char_t* const envName, std::string& envValue);
131 :
132 : bool CheckRealPath(const std::string& inputPath);
133 :
134 : bool CheckValidatePath(const std::string& path);
135 :
136 : int32_t PackSystem(const char_t* const cmdLine);
137 :
138 : std::string SafeStrerror();
139 :
140 : uint32_t CalcUniqueVfId(const uint32_t deviceId, const uint32_t vfId);
141 :
142 : bool TransStrToInt(const std::string& para, int32_t& value);
143 :
144 : void RemoveOneFile(const std::string& filePath);
145 :
146 : std::string CalFileSha256HashValue(const std::string& filePath);
147 :
148 : bool IsCurrentVfMode(const uint32_t deviceId, const uint32_t vfId);
149 :
150 : bool IsVfModeCheckedByDeviceId(const uint32_t deviceId);
151 :
152 : bool IsDirEmpty(const std::string& dirPath);
153 :
154 : bool GetFlagFromEnv(const char_t* const envStr, const char_t* const envValue);
155 :
156 : // Locate the directory of the host-side HAL shared library (the .so that
157 : // exports drvHdcSendFile) by inspecting the address of that symbol with
158 : // dladdr(3). Returns an empty string on failure.
159 : std::string GetHostSoPath();
160 :
161 : std::string ExtractSubString(const std::string& input, const std::string& begin, const std::string& end);
162 : } // namespace tsd
163 : #endif // TSD_PUB_FACILITY_UTIL_FUNC_TSD_UTIL_FUNC_H
|