LCOV - code coverage report
Current view: top level - legacy/ascend950/common - sal.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 44.9 % 49 22
Test Date: 2026-07-28 12:11:00 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          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           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              :     }
      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 :     }
      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           28 :     return HCCL_SUCCESS;
      71              : }
      72              : 
      73            0 : u64 SalGetCurrentTimestamp()
      74              : {
      75              :     u64 timestamp;
      76            0 :     struct timeval tv{};
      77            0 :     int ret = gettimeofday(&tv, nullptr);
      78            0 :     if (ret != 0) {
      79            0 :         HCCL_ERROR("[Get][tCurrentTimestamp]get timestamp fail, return[%d].", ret);
      80              :     }
      81            0 :     timestamp = tv.tv_sec * 1000000 + tv.tv_usec; // 1000000: 单位转换 秒 -> 微秒
      82            0 :     return timestamp;
      83              : }
      84              : 
      85            2 : u64 GetCurAicpuTimestamp()
      86              : {
      87              :     struct timespec timestamp;
      88            2 :     (void)clock_gettime(1, &timestamp);
      89            2 :     return static_cast<u64>((timestamp.tv_sec * 1000000000U) + (timestamp.tv_nsec));
      90              : }
      91              :  
      92              : // 返回当前线程ID
      93           68 : s32 SalGetTid()
      94              : {
      95           68 :     return syscall(SYS_gettid);
      96              : }
      97              : 
      98            3 : void SetThreadName(const std::string &threadStr)
      99              : {
     100              :     // 线程名应限制在15个字符内,防止被截断
     101            3 :     s32 sRet = pthread_setname_np(pthread_self(), threadStr.c_str());
     102            7 :     CHK_PRT_CONT(sRet != 0, HCCL_WARNING("err[%d] link[%s] nameSet failed.", sRet, threadStr.c_str()));
     103            3 : }
     104              : 
     105              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1