LCOV - code coverage report
Current view: top level - adump/common - sys_utils.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 80.9 % 47 38
Test Date: 2026-08-31 10:09:28 Functions: 66.7 % 6 4

            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 "sys_utils.h"
      11              : #include <map>
      12              : #include <ctime>
      13              : #include <chrono>
      14              : #include <sstream>
      15              : #include <iomanip>
      16              : #include "adx_log.h"
      17              : 
      18              : namespace Adx {
      19              : namespace {
      20              : constexpr uint64_t SEC_TO_USEC = 1000000UL;
      21              : constexpr uint64_t SEC_TO_MSEC = 1000UL;
      22              : constexpr uint32_t MILLI_S_FORMAT_LEN = 3;
      23              : } // namespace
      24              : 
      25            0 : std::string SysUtils::GetCurrentWorkDir()
      26              : {
      27            0 :     char currentPath[MMPA_MAX_PATH] = {0};
      28            0 :     return mmGetCwd(currentPath, MMPA_MAX_PATH) == EN_OK ? currentPath : "";
      29              : }
      30              : 
      31          280 : std::string SysUtils::HandleEnv(const char* env)
      32              : {
      33          280 :     if (env == nullptr) {
      34          284 :         return "";
      35              :     }
      36          138 :     size_t lenOfRet = strlen(env);
      37          138 :     size_t envLen = lenOfRet;
      38          138 :     if (lenOfRet < MMPA_MEM_MAX_LEN - 1) {
      39          138 :         envLen += 1;
      40              :     }
      41              : 
      42          138 :     if ((envLen != MMPA_ZERO) && (MMPA_MAX_PATH - 1 < envLen)) {
      43            6 :         return "";
      44              :     }
      45          135 :     char envValue[MMPA_MAX_PATH] = {0};
      46          185 :     IDE_CTRL_VALUE_FAILED(
      47              :         memcpy_s(envValue, MMPA_MAX_PATH - 1, env, envLen) == EN_OK, return "", "Failed to copy environment data %s.",
      48              :         env);
      49              : 
      50          220 :     return envValue;
      51              : }
      52              : 
      53           65 : uint64_t SysUtils::GetTimestamp()
      54              : {
      55           65 :     uint64_t time = 0U;
      56              :     mmTimeval tv;
      57           65 :     if (mmGetTimeOfDay(&tv, nullptr) == 0) {
      58           65 :         time = (static_cast<uint64_t>(tv.tv_sec) * SEC_TO_USEC) + static_cast<uint64_t>(tv.tv_usec);
      59              :     }
      60           65 :     return time;
      61              : }
      62              : 
      63          368 : std::string SysUtils::GetCurrentTime()
      64              : {
      65          368 :     const std::time_t now = std::time(nullptr);
      66          368 :     const std::tm* const ptm = std::localtime(&now);
      67          368 :     if (ptm == nullptr) {
      68            0 :         return "";
      69              :     }
      70              : 
      71          368 :     constexpr int32_t timeBufferLen = 32;
      72          368 :     char buffer[timeBufferLen + 1] = {0};
      73          368 :     (void)std::strftime(&buffer[0], static_cast<size_t>(timeBufferLen), "%Y%m%d%H%M%S", ptm);
      74          736 :     return std::string(buffer);
      75              : }
      76              : 
      77           79 : std::string SysUtils::GetCurrentTimeWithMillisecond()
      78              : {
      79           79 :     auto now = std::chrono::system_clock::now();
      80           79 :     std::time_t nowClock = std::chrono::system_clock::to_time_t(now);
      81           79 :     auto duration = now.time_since_epoch();
      82           79 :     auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % SEC_TO_MSEC;
      83              : 
      84           79 :     std::tm localTime{};
      85           79 :     if (localtime_r(&nowClock, &localTime) == nullptr) {
      86            3 :         return DEFAULT_MILLISECOND_TIME;
      87              :     }
      88              : 
      89           76 :     std::ostringstream oss;
      90           76 :     oss << std::put_time(&localTime, "%Y%m%d%H%M%S");
      91           76 :     oss << std::setfill('0') << std::setw(MILLI_S_FORMAT_LEN) << millis;
      92              : 
      93           76 :     return oss.str();
      94           76 : }
      95              : 
      96            0 : std::string SysUtils::GetPid()
      97              : {
      98            0 :     const auto tid = mmGetPid();
      99            0 :     if (tid != EN_ERROR) {
     100            0 :         return std::to_string(tid);
     101              :     }
     102            0 :     return "";
     103              : }
     104              : } // namespace Adx
        

Generated by: LCOV version 2.0-1