LCOV - code coverage report
Current view: top level - atrace/utrace - trace_attr.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.0 % 99 97
Test Date: 2026-08-12 11:04:46 Functions: 100.0 % 19 19

            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 "trace_attr.h"
      12              : #include "adiag_print.h"
      13              : #include "adiag_utils.h"
      14              : #include "trace_driver_api.h"
      15              : #include "trace_system_api.h"
      16              : 
      17              : #define DEFAULT_ENV_TIMEOUT    0 // timeout default is 0
      18              : #define MAX_ENV_TIMEOUT        (3 * 60 * 1000) // max timeout 180s
      19              : 
      20              : STATIC TraceInnerAttr g_traceAttr = { 0 };
      21              : 
      22         1930 : STATIC void TraceAttrPlatformInit(void)
      23              : {
      24         1930 :     g_traceAttr.systemAttr.platform = PLATFORM_INVALID_VALUE;
      25         1930 :     TraStatus ret = TraceDriverInit();
      26         1930 :     if (ret != TRACE_SUCCESS) {
      27           60 :         return;
      28              :     }
      29              : 
      30         1890 :     uint32_t platform = PLATFORM_INVALID_VALUE;
      31         1890 :     ret = TraceDrvGetPlatformInfo(&platform);
      32         1890 :     if (ret != TRACE_SUCCESS) {
      33            0 :         return;
      34              :     }
      35              : 
      36         1890 :     uint32_t devNum = 0;
      37         1890 :     ret = TraceDrvGetDevNum(&devNum);
      38         1890 :     if (ret != TRACE_SUCCESS) {
      39           20 :         return;
      40              :     }
      41              : 
      42         1870 :     if ((platform == PLATFORM_HOST_SIDE) && (devNum != 0)) {
      43         1810 :         g_traceAttr.systemAttr.platform = PLATFORM_HOST_SIDE; // EP host
      44              :     } else {
      45           60 :         g_traceAttr.systemAttr.platform = platform;
      46              :     }
      47              : }
      48              : 
      49              : #ifdef ATRACE_API
      50      1318011 : STATIC uint32_t TraceAttrGetPlatform(void)
      51              : {
      52      1318011 :     return g_traceAttr.systemAttr.platform;
      53              : }
      54              : #endif
      55              : 
      56              : /**
      57              :  * @brief       check cpu-only trace feature is supported or not.
      58              :  *              On the general server (pure cpu) scene, only the cann package is installed
      59              :  *              without driver/firmware, dlopen driver fails and platform stays invalid.
      60              :  *              In this case the cpu-only features (write trace log, stacktrace) are still
      61              :  *              supported; only the device side is not supported.
      62              :  * @return      true for support, false for not support
      63              :  */
      64      1317866 : bool AtraceCheckSupported(void)
      65              : {
      66              : #ifdef ATRACE_API
      67      1317811 :     if (TraceAttrGetPlatform() == PLATFORM_DEVICE_SIDE) {
      68          140 :         return false;
      69              :     }
      70              : #endif
      71      1317726 :     return true;
      72              : }
      73              : 
      74              : /**
      75              :  * @brief       check the device-interacting trace feature is supported or not.
      76              :  *              Only the host side with a real npu device can interact with device.
      77              :  *              On the general server (pure cpu) scene the platform is invalid, so the
      78              :  *              device-interacting features (AtraceReportStart/AtraceReportStop) are not supported.
      79              :  * @return      true for support, false for not support
      80              :  */
      81           60 : bool AtraceCheckDeviceSupported(void)
      82              : {
      83              : #ifdef ATRACE_API
      84           60 :     if (TraceAttrGetPlatform() != PLATFORM_HOST_SIDE) {
      85           40 :         return false;
      86              :     }
      87              : #endif
      88           20 :     return true;
      89              : }
      90              : 
      91         1870 : STATIC TraStatus TraceAttrIdInit(void)
      92              : {
      93         1870 :     g_traceAttr.systemAttr.pid = getpid();
      94         1870 :     uint32_t uid = getuid();
      95         1870 :     const struct passwd *userInfo = getpwuid(uid);
      96         1870 :     if (userInfo != NULL) {
      97         1850 :         g_traceAttr.systemAttr.uid = userInfo->pw_uid;
      98         1850 :         g_traceAttr.systemAttr.gid = userInfo->pw_gid;
      99         1850 :         g_traceAttr.systemAttr.pgid = getpgrp();
     100         1850 :         return TRACE_SUCCESS;
     101              :     }
     102           20 :     ADIAG_ERR("get uid and gid failed, uid=%u, strerr=%s.", uid, strerror(AdiagGetErrorCode()));
     103           20 :     return TRACE_FAILURE;
     104              : }
     105              : 
     106         1850 : STATIC TraStatus TraceAttrTimeInit(void)
     107              : {
     108         1850 :     AdiagStatus ret = TraceTimeDstInit();
     109         1850 :     ADIAG_CHK_EXPR_ACTION(ret != ADIAG_SUCCESS, return TRACE_FAILURE, "init time DST failed, ret=%d.", ret);
     110         1830 :     ret = TimestampToFileStr(GetRealTime(), g_traceAttr.systemAttr.timeStamp, TIMESTAMP_MAX_LENGTH);
     111         1830 :     ADIAG_CHK_EXPR_ACTION(ret != ADIAG_SUCCESS, return TRACE_FAILURE, "get timestamp string failed, ret=%d.", ret);
     112         1830 :     return TRACE_SUCCESS;
     113              : }
     114              : 
     115         1830 : STATIC void TraceAttrEnvInit(void)
     116              : {
     117         1830 :     g_traceAttr.systemAttr.envTimeout = DEFAULT_ENV_TIMEOUT;
     118         1830 :     const char *env = NULL;
     119         1830 :     MM_SYS_GET_ENV(MM_ENV_ASCEND_LOG_DEVICE_FLUSH_TIMEOUT, (env));
     120         1830 :     if (env == NULL) {
     121         1730 :         ADIAG_INF("doesn't set env ASCEND_LOG_DEVICE_FLUSH_TIMEOUT, use default timeout=%dms.", g_traceAttr.systemAttr.envTimeout);
     122         1730 :         return;
     123              :     }
     124              : 
     125          100 :     int32_t value = -1;
     126          100 :     if ((AdiagStrToInt(env, &value) == TRACE_SUCCESS) && (value <= MAX_ENV_TIMEOUT) && (value >= 0)) {
     127           40 :         ADIAG_INF("set timeout %dms by env ASCEND_LOG_DEVICE_FLUSH_TIMEOUT.", g_traceAttr.systemAttr.envTimeout);
     128           40 :         g_traceAttr.systemAttr.envTimeout = value;
     129              :     } else {
     130           60 :         ADIAG_WAR("env ASCEND_LOG_DEVICE_FLUSH_TIMEOUT is invalid, use default timeout=%dms.", g_traceAttr.systemAttr.envTimeout);
     131              :     }
     132              : }
     133              : 
     134          280 : int32_t TraceGetTimeout(void)
     135              : {
     136          280 :     return g_traceAttr.systemAttr.envTimeout;
     137              : }
     138              : 
     139         8737 : int32_t TraceAttrGetPid(void)
     140              : {
     141         8737 :     return g_traceAttr.systemAttr.pid;
     142              : }
     143              : 
     144         8722 : int32_t TraceAttrGetPgid(void)
     145              : {
     146         8722 :     return g_traceAttr.systemAttr.pgid;
     147              : }
     148              : 
     149         8722 : const char *TraceAttrGetTime(void)
     150              : {
     151         8722 :     return (const char *)g_traceAttr.systemAttr.timeStamp;
     152              : }
     153              : 
     154        19844 : uint32_t TraceAttrGetUid(void)
     155              : {
     156        19844 :     return g_traceAttr.systemAttr.uid;
     157              : }
     158              : 
     159        19844 : uint32_t TraceAttrGetGid(void)
     160              : {
     161        19844 :     return g_traceAttr.systemAttr.gid;
     162              : }
     163              : 
     164           27 : TraStatus TraceSetGlobalAttr(const TraceGlobalAttr *attr)
     165              : {
     166           27 :     if (attr == NULL) {
     167            0 :         return TRACE_FAILURE;
     168              :     }
     169           27 :     g_traceAttr.userAttr.saveMode = attr->saveMode;
     170           27 :     g_traceAttr.userAttr.deviceId = attr->deviceId;
     171           27 :     g_traceAttr.userAttr.pid = attr->pid;
     172           27 :     ADIAG_INF("set global attr successfully, saveMode=%u, device id=%u, pid=%u.", attr->saveMode, attr->deviceId, attr->pid);
     173           27 :     return TRACE_SUCCESS;
     174              : }
     175              :  
     176           35 : uint8_t TraceAttrGetSaveMode(void)
     177              : {
     178           35 :     return g_traceAttr.userAttr.saveMode;
     179              : }
     180              :  
     181           44 : uint8_t TraceAttrGetGlobalDevId(void)
     182              : {
     183           44 :     return g_traceAttr.userAttr.deviceId;
     184              : }
     185              :  
     186           35 : uint32_t TraceAttrGetGlobalPid(void)
     187              : {
     188           35 :     return g_traceAttr.userAttr.pid;
     189              : }
     190              : 
     191         1930 : TraStatus TraceAttrInit(void)
     192              : {
     193         1930 :     TraceAttrPlatformInit();
     194              : 
     195         1930 :     if (!AtraceCheckSupported()) {
     196           60 :         return TRACE_SUCCESS;
     197              :     }
     198         1870 :     TraStatus ret = TraceAttrIdInit();
     199         1870 :     if (ret != TRACE_SUCCESS) {
     200           20 :         ADIAG_ERR("init attr id failed.");
     201           20 :         return TRACE_FAILURE;
     202              :     }
     203              : 
     204         1850 :     ret = TraceAttrTimeInit();
     205         1850 :     if (ret != TRACE_SUCCESS) {
     206           20 :         ADIAG_ERR("init attr time failed.");
     207           20 :         return TRACE_FAILURE;
     208              :     }
     209              : 
     210         1830 :     TraceAttrEnvInit();
     211         1830 :     ADIAG_INF("attr init successfully, timeout=%dms.", g_traceAttr.systemAttr.envTimeout);
     212         1830 :     return TRACE_SUCCESS;
     213              : }
     214              : 
     215         1880 : void TraceAttrExit(void)
     216              : {
     217         1880 :     TraceDriverExit();
     218         1880 : }
        

Generated by: LCOV version 2.0-1