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 : #include "sal.h"
11 : #include <unistd.h>
12 : #include "log.h"
13 : #include <unistd.h>
14 : #include <sys/time.h> /* 获取时间 */
15 :
16 : namespace Hccl {
17 :
18 1909 : void SaluSleep(u32 usec)
19 : {
20 : /* usleep()可能会因为进程收到信号(比如alarm)而提前返回EINTR, 后续优化 */
21 1909 : s32 iRet = usleep(usec);
22 1909 : if (iRet != 0) {
23 0 : HCCL_WARNING("Sleep: usleep failed[%d]: %s [%d]", iRet, strerror(errno), errno);
24 : }
25 1909 : }
26 :
27 0 : void SalSleep(u32 sec)
28 : {
29 : /* sleep()可能会因为进程收到信号(比如alarm)而提前返回EINTR, 后续优化 */
30 0 : s32 iRet = sleep(sec);
31 0 : if (iRet != 0) {
32 0 : HCCL_WARNING("Sleep: sleep failed[%d]: %s [%d]", iRet, strerror(errno), errno);
33 : }
34 0 : }
35 :
36 310 : std::string SalGetEnv(const char *name)
37 : {
38 310 : if (name == nullptr || getenv(name) == nullptr) {
39 392 : return "EmptyString";
40 : }
41 :
42 228 : return getenv(name);
43 : }
44 :
45 : constexpr u32 INVALID_UINT = 0xFFFFFFFF;
46 : // 字串符转换成无符号整型
47 38 : HcclResult SalStrToULong(const std::string str, int base, u32 &val)
48 : {
49 : try {
50 38 : u64 tmp = std::stoull(str, nullptr, base);
51 36 : if (tmp > INVALID_UINT) {
52 0 : HCCL_ERROR("[Transform][StrToULong]stoul out of range, str[%s] base[%d] val[%llu]", str.c_str(), base, tmp);
53 0 : return HCCL_E_PARA;
54 : } else {
55 36 : val = static_cast<u32>(tmp);
56 : }
57 : }
58 2 : catch (std::invalid_argument&) {
59 6 : HCCL_ERROR("[Transform][StrToULong]stoull invalid argument, str[%s] base[%d] val[%u]", str.c_str(), base, val);
60 2 : return HCCL_E_PARA;
61 2 : }
62 0 : catch (std::out_of_range&) {
63 0 : HCCL_ERROR("[Transform][StrToULong]stoull out of range, str[%s] base[%d] val[%u]", str.c_str(), base, val);
64 0 : return HCCL_E_PARA;
65 0 : }
66 0 : catch (...) {
67 0 : HCCL_ERROR("[Transform][StrToULong]stoull catch errror, str[%s] base[%d] val[%u]", str.c_str(), base, val);
68 0 : return HCCL_E_PARA;
69 0 : }
70 36 : return HCCL_SUCCESS;
71 : }
72 :
73 1 : u64 SalGetCurrentTimestamp()
74 : {
75 : u64 timestamp;
76 1 : struct timeval tv{};
77 1 : int ret = gettimeofday(&tv, nullptr);
78 1 : if (ret != 0) {
79 0 : HCCL_ERROR("[Get][tCurrentTimestamp]get timestamp fail, return[%d].", ret);
80 : }
81 1 : timestamp = tv.tv_sec * 1000000 + tv.tv_usec; // 1000000: 单位转换 秒 -> 微秒
82 1 : return timestamp;
83 : }
84 :
85 2 : u64 GetCurAicpuTimestamp()
86 : {
87 : struct timespec timestamp;
88 2 : (void)clock_gettime(1, ×tamp);
89 2 : return static_cast<u64>((timestamp.tv_sec * 1000000000U) + (timestamp.tv_nsec));
90 : }
91 :
92 : // 返回当前线程ID
93 78 : s32 SalGetTid()
94 : {
95 78 : return syscall(SYS_gettid);
96 : }
97 :
98 4 : void SetThreadName(const std::string &threadStr)
99 : {
100 : // 线程名应限制在15个字符内,防止被截断
101 4 : s32 sRet = pthread_setname_np(pthread_self(), threadStr.c_str());
102 8 : CHK_PRT_CONT(sRet != 0, HCCL_WARNING("err[%d] link[%s] nameSet failed.", sRet, threadStr.c_str()));
103 4 : }
104 :
105 : } // namespace Hccl
|