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 : #ifndef _AE_DEFINE_H_
12 : #define _AE_DEFINE_H_
13 :
14 : #include <unistd.h>
15 : #include <sys/syscall.h>
16 : #include "aicpu_engine.h"
17 : #include "aicpu_engine_struct.h"
18 : #ifdef RUN_ON_AICPU
19 : #include "aicpuprocess_weak_log.h"
20 : #else
21 : #define CCECPU 0
22 : #endif
23 :
24 : #include <stdlib.h>
25 : #include <stdio.h>
26 : #include <stdint.h>
27 : #if defined(RUN_ON_AICPU) && defined(AE_PERF_LOG)
28 : #include <sys/time.h>
29 : #endif
30 : #ifdef _AOSCORE_
31 : #include <pthread.h>
32 : #endif
33 : namespace cce {
34 : using char_t = char;
35 :
36 : enum class AicpuOpErrorCode : uint32_t {
37 : AICPU_TASK_WATI_FLAG = 101U, // aicpu task wait flag
38 : AICPU_END_OF_SEQUENCE_FLAG = 201U, // aicpu end of sequence flag
39 : AICPU_TASK_ABORT = 301U, // ccl kernel task abort flag
40 : AICPU_SILENT_FAULT = 501U, // aicpu silent fault flag
41 : AICPU_DETECT_FAULT = 502U, // aicpu detect fault flag
42 : AICPU_DETECT_FAULT_NORAS = 503U, // aicpu detect fault flag no RAS
43 : AICPU_DETECT_LOW_BIT_FAULT = 504U, // aicpu detect low-bit fault flag
44 : AICPU_DETECT_LOW_BIT_FAULT_NORAS = 505U, // aicpu detect low-bit fault flag no RAS
45 : AICPU_KFC_PASSTHROUGH_START = 1000U, // aicpu will passthrough errcode to rts
46 : AICPU_KFC_PASSTHROUGH_END = 1099U
47 : };
48 :
49 39 : inline long GetTid()
50 : {
51 39 : thread_local static long tid = syscall(__NR_gettid);
52 39 : return tid;
53 : }
54 :
55 : #ifdef __GNUC__
56 : #define likely(X) __builtin_expect((static_cast<int64_t>(X)), 1L)
57 : #define unlikely(X) __builtin_expect((static_cast<int64_t>(X)), 0L)
58 : #else
59 : #define likely(X) (X)
60 : #define unlikely(X) (X)
61 : #endif
62 :
63 : #define AE_MODULE_ID CCECPU
64 :
65 : #ifdef RUN_ON_AICPU
66 :
67 : #define AE_ERR_LOG(id, fmt, args...) \
68 : do { \
69 : if (&DlogRecord != nullptr) { \
70 : dlog_error(static_cast<int32_t>(AE_MODULE_ID), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
71 : &__FUNCTION__[0], cce::GetTid(), ##args); \
72 : } \
73 : } while (0)
74 :
75 : #define AE_WARN_LOG(id, fmt, args...) \
76 : do { \
77 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
78 : dlog_warn(static_cast<int32_t>(AE_MODULE_ID), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
79 : &__FUNCTION__[0], cce::GetTid(), ##args); \
80 : } \
81 : } while (0)
82 :
83 : #define AE_INFO_LOG(id, fmt, args...) \
84 : do { \
85 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
86 : dlog_info(static_cast<int32_t>(AE_MODULE_ID), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
87 : &__FUNCTION__[0], cce::GetTid(), ##args); \
88 : } \
89 : } while (0)
90 :
91 : #define AE_DEBUG_LOG(id, fmt, args...) \
92 : do { \
93 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
94 : dlog_debug(static_cast<int32_t>(AE_MODULE_ID), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
95 : &__FUNCTION__[0], cce::GetTid(), ##args); \
96 : } \
97 : } while (0)
98 :
99 : #define AE_RUN_INFO_LOG(id, fmt, args...) \
100 : do { \
101 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
102 : dlog_info(static_cast<int32_t>(static_cast<uint32_t>(AE_MODULE_ID) | \
103 : static_cast<uint32_t>(RUN_LOG_MASK)), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
104 : &__FUNCTION__[0], cce::GetTid(), ##args); \
105 : } \
106 : } while (0)
107 :
108 : #define AE_RUN_WARN_LOG(id, fmt, args...) \
109 : do { \
110 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
111 : dlog_warn(static_cast<int32_t>(static_cast<uint32_t>(AE_MODULE_ID) | \
112 : static_cast<uint32_t>(RUN_LOG_MASK)), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
113 : &__FUNCTION__[0], cce::GetTid(), ##args); \
114 : } \
115 : } while (0)
116 :
117 : #define AE_MEMORY_LOG(id, fmt, args...) \
118 : do { \
119 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
120 : dlog_info(static_cast<int32_t>(AE_MODULE_ID), "[%s][tid:%ld][AICPU_PROCESSER] " fmt, \
121 : &__FUNCTION__[0], cce::GetTid(), ##args); \
122 : } \
123 : } while (0)
124 :
125 : #else
126 : #define AE_ERR_LOG(id, fmt, args...) printf("[ERROR] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
127 : #define AE_WARN_LOG(id, fmt, args...) printf("[ERROR] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
128 : #define AE_INFO_LOG(id, fmt, args...) printf("[INFO] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
129 : #define AE_DEBUG_LOG(id, fmt, args...) printf("[DEBUG] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
130 : #define AE_MEMORY_LOG(id, fmt, args...) printf("[INFO] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
131 : #define AE_RUN_INFO_LOG(id, fmt, args...) \
132 : printf("[INFO] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
133 : #define AE_RUN_WARN_LOG(id, fmt, args...) \
134 : printf("[WARN] [%s:%d][AICPU_PROCESSER] " fmt "\n", __FILE__, __LINE__, ##args)
135 : #endif
136 :
137 : // perf log feature
138 : #if defined(RUN_ON_AICPU)&&defined(AE_PERF_LOG)
139 : #define FUNC_PROFILE_START \
140 : struct timeval ts; \
141 : (void)gettimeofday(&ts, nullptr); \
142 : const uint64_t startUsec = (static_cast<uint64_t>(ts.tv_sec) * 1000000LU) + static_cast<uint64_t>(ts.tv_usec);
143 :
144 : #define FUNC_PROFILE_END \
145 : (void)gettimeofday(&ts, nullptr); \
146 : const uint64_t endUsec = (static_cast<uint64_t>(ts.tv_sec) * 1000000LU) + static_cast<uint64_t>(ts.tv_usec); \
147 : const uint64_t costUsec = endUsec - startUsec; \
148 : AE_INFO_LOG(AE_MODULE_ID, "[PERF] start=%lu, end=%lu, use=%lu us\n", \
149 : startUsec, endUsec, costUsec);
150 : #else
151 : #define FUNC_PROFILE_START
152 : #define FUNC_PROFILE_END
153 : #endif
154 :
155 : using tAERwLock = pthread_rwlock_t;
156 : #define AE_RW_LOCK_RD_LOCK(lock) (void)pthread_rwlock_rdlock(lock)
157 : #define AE_RW_LOCK_WR_LOCK(lock) (void)pthread_rwlock_wrlock(lock)
158 : #define AE_RW_LOCK_UN_LOCK(lock) (void)pthread_rwlock_unlock(lock)
159 : #define AE_RW_LOCK_DESTROY(lock) (void)pthread_rwlock_destroy(lock)
160 :
161 : #define AE_SINGLETON_CREAT(CLASS) do {\
162 : mtx_.lock();\
163 : if (instance_ != nullptr)\
164 : {\
165 : mtx_.unlock();\
166 : return instance_;\
167 : }\
168 : else\
169 : {\
170 : if (instance_ == nullptr)\
171 : {\
172 : instance_ = new(std::nothrow) CLASS();\
173 : if(instance_ == nullptr)\
174 : {\
175 : mtx_.unlock();\
176 : return nullptr;\
177 : }\
178 : (void)instance_->Init(); \
179 : }\
180 : mtx_.unlock();\
181 : \
182 : return instance_;\
183 : }\
184 : }while(0)
185 :
186 : #define AE_SINGLETON_DESTROY() do {\
187 : mtx_.lock();\
188 : if (instance_ == nullptr)\
189 : {\
190 : mtx_.unlock();\
191 : return;\
192 : }\
193 : delete instance_;\
194 : instance_ = nullptr;\
195 : mtx_.unlock();\
196 : \
197 : }while(0)
198 :
199 : }
200 : #endif
|