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