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