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) do { \
84 : if ((x) > (warntime)) \
85 : HCCL_WARNING("over warning Time\n"); \
86 : } while (0)
87 :
88 : #ifdef TIME_PROFILING
89 : #define TIME_PRINT(x) \
90 : do { \
91 : auto startTime = TIME_NOW(); \
92 : auto timeGap = TIME_NOW() - startTime; \
93 : x; \
94 : HCCL_ERROR("Time Cost: cost time %llu us %s", TAKE_TIME_US((TIME_NOW() - startTime), (3 * timeGap)), #x); \
95 : } while (0)
96 : #else
97 : #define TIME_PRINT(x) \
98 : do { \
99 : x; \
100 : } while (0)
101 : #endif
102 : using HcclSystemTime = std::chrono::system_clock::time_point;
103 : #endif
104 :
105 : #ifdef __cplusplus
106 : extern "C" {
107 : #endif
108 :
109 : #define weak_alias(name, aliasname) _weak_alias(name, aliasname)
110 : #define _weak_alias(name, aliasname) extern __typeof(name) aliasname __attribute__((weak, alias(#name)))
111 :
112 : #define strong_alias(name, aliasname) _strong_alias(name, aliasname)
113 : #define _strong_alias(name, aliasname) extern __typeof(name) aliasname __attribute__((alias(#name)))
114 :
115 : constexpr s32 BUF_SIZE = 1024;
116 : constexpr size_t MEMCPY_THRESHOLD = 1024;
117 : s32 SalLog2(s32 data);
118 : #ifdef __cplusplus
119 : } // extern "C"
120 : #endif
121 :
122 : #if HCOMM_T_DESC("计算类型占用内存大小函数", true)
123 : HcclResult SalGetDataTypeSize(HcclDataType dataType, u32 &dataTypeSize);
124 : #endif
125 :
126 : HcclResult GetLocalHostIP(hccl::HcclIpAddress &ip, u32 devPhyid = 0);
127 :
128 : HcclResult FindLocalHostIP(std::vector<std::pair<std::string, hccl::HcclIpAddress>> &ifInfos, hccl::HcclIpAddress &ip);
129 : std::string GetLocalServerId(std::string &serverId);
130 : bool IsGeneralServer();
131 : HcclResult IsHostUseDevNic(bool &isHdcMode);
132 : void SetHostUseDevNicFlag(bool isHdcMode);
133 : u32 GetNicPort(u32 devicePhyId, const std::vector<u32> &ranksPort, u32 userRank, bool isUseRanksPort);
134 :
135 : HcclResult IsAllDigit(const char *strNum);
136 :
137 : void SetThreadName(const std::string &threadStr);
138 :
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 86 : inline u64 ProfGetCurCpuTimestamp()
152 : {
153 : #ifndef CCL_LLT
154 : uint64_t cntvct;
155 : AsmCntvc(cntvct);
156 : return cntvct;
157 : #endif
158 86 : return 0;
159 : }
160 : #endif // HCCL_INC_SAL_H
|