LCOV - code coverage report
Current view: top level - legacy/ascend950/common - sal.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 45.7 % 46 21
Test Date: 2026-08-17 10:19:35 Functions: 75.0 % 8 6

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

Generated by: LCOV version 2.0-1