Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 HCOMM_HCCL_INC_SAL_PUB_H
12 : #define HCOMM_HCCL_INC_SAL_PUB_H
13 :
14 : #include <climits>
15 : #include <chrono>
16 : #include <exception>
17 : #include <securec.h>
18 : #include <string>
19 : #include <map>
20 : #include <vector>
21 : #include <set>
22 : #include <hccl/hccl_types.h>
23 :
24 : #include "hccl/base.h"
25 : #include "hccl_ip_address.h"
26 :
27 : #ifndef HCOMM_T_DESC
28 : #define HCOMM_T_DESC(_msg, _y) ((_y) ? true : false)
29 : #endif
30 :
31 : std::string SalGetEnv(const char* name); // deprecated, 环境变量读取统一使用 MM_SYS_GET_ENV 接口
32 :
33 : #if HCOMM_T_DESC("库函数封装", true)
34 : constexpr int HCCL_BASE_DECIMAL = 10; // 10进制字符串转换
35 : constexpr int HCCL_BASE_HEX = 16; // 16进制字符串转换
36 :
37 : HcclResult SalStrToInt(const std::string str, int base, s32& val);
38 : HcclResult SalStrToULong(const std::string str, int base, u32& val);
39 : HcclResult SalStrToULonglong(const std::string str, int base, u64& val);
40 : HcclResult SalStrToLonglong(const std::string str, int base, s64& val);
41 : #endif
42 :
43 : #if HCOMM_T_DESC("跨进程处理函数", true)
44 : s32 SalGetPid();
45 : HcclResult SalGetBareTgid(s32* pid);
46 : u32 SalGetUid();
47 : s32 SalGetTid();
48 : extern HcclResult SalGetUniqueId(char* salUniqueId, int maxLen = INT_MAX);
49 :
50 : #endif
51 :
52 : #if HCOMM_T_DESC("路径信息函数", true)
53 : HcclResult SalIsDirExist(const std::string& dir, s32& status);
54 : #endif
55 :
56 : #if HCOMM_T_DESC("C字符串处理函数适配", true)
57 : std::string SalTrim(const std::string& s);
58 : #endif
59 :
60 : #if HCOMM_T_DESC("设置指定位值函数", true)
61 : void SalSetBitOne(u64& value, u64 index);
62 : #endif
63 :
64 : #if HCOMM_T_DESC("时间处理接口适配", true)
65 : constexpr u32 SOCKET_SLEEP_MILLISECONDS = 1;
66 : constexpr u32 ONE_HUNDRED_MICROSECOND_OF_USLEEP = 100;
67 : constexpr u32 TWO_HUNDRED_MICROSECOND_OF_USLEEP = 200;
68 : constexpr u32 ONE_MILLISECOND_OF_USLEEP = 1000;
69 : constexpr u32 TEN_MILLISECOND_OF_USLEEP = 10000;
70 : constexpr u32 TCP_SEND_THREAD_SLEEP_TWO_HUNDRED_MICROSECOND = 200;
71 : constexpr u32 TIME_S_TO_MS = 1000;
72 : s64 SalGetSysTime();
73 : void SaluSleep(u32 usec);
74 : void SalSleep(u32 sec);
75 : HcclResult SalGetCurrentTimestamp(u64& timestamp);
76 : u64 GetCurAicpuTimestamp();
77 :
78 : using HcclUs = std::chrono::steady_clock::time_point;
79 :
80 : #define DURATION_US(x) (std::chrono::duration_cast<std::chrono::microseconds>(x))
81 : #define TAKE_TIME_US(x, y) (DURATION_US(x) - DURATION_US(y))
82 : #define TIME_NOW() ({ std::chrono::steady_clock::now(); })
83 : #define CHECK_WARNTIME(x, warntime) \
84 : do { \
85 : if ((x) > (warntime)) \
86 : HCCL_WARNING("over warning Time\n"); \
87 : } while (0)
88 :
89 : #ifdef TIME_PROFILING
90 : #define TIME_PRINT(x) \
91 : do { \
92 : auto startTime = TIME_NOW(); \
93 : auto timeGap = TIME_NOW() - startTime; \
94 : x; \
95 : HCCL_ERROR("Time Cost: cost time %llu us %s", TAKE_TIME_US((TIME_NOW() - startTime), (3 * timeGap)), #x); \
96 : } while (0)
97 : #else
98 : #define TIME_PRINT(x) \
99 : do { \
100 : x; \
101 : } while (0)
102 : #endif
103 : using HcclSystemTime = std::chrono::system_clock::time_point;
104 : #endif
105 :
106 : #ifdef __cplusplus
107 : extern "C" {
108 : #endif
109 :
110 : #define weak_alias(name, aliasname) _weak_alias(name, aliasname)
111 : #define _weak_alias(name, aliasname) extern __typeof(name) aliasname __attribute__((weak, alias(#name)))
112 :
113 : #define strong_alias(name, aliasname) _strong_alias(name, aliasname)
114 : #define _strong_alias(name, aliasname) extern __typeof(name) aliasname __attribute__((alias(#name)))
115 :
116 : constexpr s32 BUF_SIZE = 1024;
117 : constexpr size_t MEMCPY_THRESHOLD = 1024;
118 : s32 SalLog2(s32 data);
119 : #ifdef __cplusplus
120 : } // extern "C"
121 : #endif
122 :
123 : #if HCOMM_T_DESC("计算类型占用内存大小函数", true)
124 : HcclResult SalGetDataTypeSize(HcclDataType dataType, u32& dataTypeSize);
125 : #endif
126 :
127 : HcclResult GetLocalHostIP(hccl::HcclIpAddress& ip, u32 devPhyid = 0);
128 :
129 : HcclResult FindLocalHostIP(std::vector<std::pair<std::string, hccl::HcclIpAddress>>& ifInfos, hccl::HcclIpAddress& ip);
130 : std::string GetLocalServerId(std::string& serverId);
131 : bool IsGeneralServer();
132 : HcclResult IsHostUseDevNic(bool& isHdcMode);
133 : void SetHostUseDevNicFlag(bool isHdcMode);
134 : u32 GetNicPort(u32 devicePhyId, const std::vector<u32>& ranksPort, u32 userRank, bool isUseRanksPort);
135 :
136 : HcclResult IsAllDigit(const char* strNum);
137 :
138 : void SetThreadName(const std::string& threadStr);
139 :
140 : #ifndef CCL_LLT
141 : inline void AsmCntvc(uint64_t& cntvct)
142 : {
143 : #if defined __aarch64__
144 : asm volatile("mrs %0, cntvct_el0" : "=r"(cntvct));
145 : #else
146 : cntvct = 0;
147 : #endif
148 : }
149 : #endif
150 :
151 72 : inline u64 ProfGetCurCpuTimestamp()
152 : {
153 : #ifndef CCL_LLT
154 : uint64_t cntvct;
155 : AsmCntvc(cntvct);
156 : return cntvct;
157 : #endif
158 72 : return 0;
159 : }
160 : #endif // HCCL_INC_SAL_H
|