1 /**
2 * Copyright 2023 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*!
18 * \file op_log.h
19 * \brief
20 */
21 #ifndef CUSTOMIZE_OP_PROTO_UTILS_OP_LOG_H
22 #define CUSTOMIZE_OP_PROTO_UTILS_OP_LOG_H
23
24 #include <string>
25 #include <type_traits>
26 #include <cinttypes>
27 #include "graph/operator.h"
28
29 #define LOG_CPP
30 #if !defined(__ANDROID__) && !defined(ANDROID)
31 #include "toolchain/slog.h"
32 #else
33 #include <utils/Log.h>
34 #endif
35
36 #ifdef __GNUC__
37 #include <unistd.h>
38 #include <sys/syscall.h>
39 #else
40 #include "mmpa/mmpa_api.h"
41 #endif
42
43 #define OPPROTO_SUBMOD_NAME "OP_PROTO"
44
45 class OpLog {
46 public:
GetTid()47 static uint64_t GetTid() {
48 #ifdef __GNUC__
49 const uint64_t tid = static_cast<uint64_t>(syscall(__NR_gettid));
50 #else
51 const uint64_t tid = static_cast<uint64_t>(GetCurrentThreadId());
52 #endif
53 return tid;
54 }
55 };
56
get_cstr(const std::string & str)57 inline const char *get_cstr(const std::string &str) { return str.c_str(); }
58
get_cstr(const char * str)59 inline const char *get_cstr(const char *str) { return str; }
60
get_op_info(const std::string & str)61 inline const std::string &get_op_info(const std::string &str) { return str; }
62
get_op_info(const char * str)63 inline const char *get_op_info(const char *str) { return str; }
64
65 template <class T>
is_ge_operator_type()66 constexpr bool is_ge_operator_type() {
67 return std::is_base_of<ge::Operator, typename std::decay<T>::type>::value;
68 }
69
70 template <class T>
get_op_info(const T & op)71 typename std::enable_if<is_ge_operator_type<T>(), std::string>::type get_op_info(const T &op) {
72 ge::AscendString name;
73 ge::AscendString type;
74 auto get_name_ret = op.GetName(name);
75 auto get_type_ret = op.GetOpType(type);
76 std::string op_info = get_type_ret == ge::GRAPH_SUCCESS ? type.GetString() : "nil";
77 op_info += ":";
78 op_info += get_name_ret == ge::GRAPH_SUCCESS ? name.GetString() : "nil";
79 return op_info;
80 }
81
82 template <class T>
is_context_type()83 constexpr bool is_context_type() {
84 return !std::is_base_of<ge::Operator, typename std::decay<T>::type>::value &&
85 !std::is_same<const char *, typename std::decay<T>::type>::value &&
86 !std::is_same<char *, typename std::decay<T>::type>::value &&
87 !std::is_same<std::string, typename std::decay<T>::type>::value;
88 }
89
90 template <class T>
get_op_info(T context)91 typename std::enable_if<is_context_type<T>(), std::string>::type get_op_info(T context) {
92 if (context == nullptr) {
93 return "nil:nil";
94 }
95 std::string op_info = context->GetNodeType() != nullptr ? context->GetNodeType() : "nil";
96 op_info += ":";
97 op_info += context->GetNodeName() != nullptr ? context->GetNodeName() : "nil";
98 return op_info;
99 }
100
101 template <typename T>
TbeGetName(const T & op)102 std::string TbeGetName(const T &op) {
103 ge::AscendString op_ascend_name;
104 ge::graphStatus ret = op.GetName(op_ascend_name);
105 if (ret != ge::GRAPH_SUCCESS) {
106 std::string op_name = "None";
107 return op_name;
108 }
109 return op_ascend_name.GetString();
110 }
111
112 template <typename T>
TbeGetOpType(const T & op)113 std::string TbeGetOpType(const T &op) {
114 ge::AscendString op_ascend_name;
115 ge::graphStatus ret = op.GetOpType(op_ascend_name);
116 if (ret != ge::GRAPH_SUCCESS) {
117 std::string op_name = "None";
118 return op_name;
119 }
120 return op_ascend_name.GetString();
121 }
122
123 #define CHECK_DIVISOR_ZERO(divisor) \
124 if ((divisor) == 0) { \
125 return; \
126 }
127
128 #define CHECK_DIVISOR_ZERO_RET(divisor, ret) \
129 if ((divisor) == 0) { \
130 return ret; \
131 }
132
133 #define OP_CHECK(cond, log_func, return_expr) \
134 if (cond) { \
135 log_func; \
136 return_expr; \
137 }
138
139 #if !defined(__ANDROID__) && !defined(ANDROID)
140 #define AICPU_OP_LOGI(opname, ...) AICPU_D_OP_LOGI(get_op_info(opname), __VA_ARGS__)
141 #define AICPU_OP_LOGW(opname, ...) AICPU_D_OP_LOGW(get_op_info(opname), __VA_ARGS__)
142 #define AICPU_OP_LOGD(opname, ...) AICPU_D_OP_LOGD(get_op_info(opname), __VA_ARGS__)
143 #define AICPU_OP_LOGE_WITHOUT_REPORT(opname, ...) AICPU_D_OP_LOGE(get_op_info(opname), __VA_ARGS__)
144 #define AICPU_OP_LOGE(op_name, ...) \
145 do { \
146 AICPU_OP_LOGE_WITHOUT_REPORT(op_name, ##__VA_ARGS__); \
147 } while (0)
148
149 #define OP_LOGI(opname, ...) D_OP_LOGI(get_op_info(opname), __VA_ARGS__)
150 #define OP_LOGW(opname, ...) D_OP_LOGW(get_op_info(opname), __VA_ARGS__)
151
152 #define OP_LOGE_WITHOUT_REPORT(opname, ...) D_OP_LOGE(get_op_info(opname), __VA_ARGS__)
153 #define OP_LOGE(op_name, ...) \
154 do { \
155 OP_LOGE_WITHOUT_REPORT(op_name, ##__VA_ARGS__); \
156 } while (0)
157
158 #define OP_LOGD(opname, ...) D_OP_LOGD(get_op_info(opname), __VA_ARGS__)
159 #define OP_EVENT(opname, ...) D_OP_EVENT(get_op_info(opname), __VA_ARGS__)
160 #define GE_OP_LOGI(opname, ...) GE_D_OP_LOGI(get_op_info(opname), __VA_ARGS__)
161 #define GE_OP_LOGW(opname, ...) GE_D_OP_LOGW(get_op_info(opname), __VA_ARGS__)
162 #define GE_OP_LOGE(opname, ...) GE_D_OP_LOGE(get_op_info(opname), __VA_ARGS__)
163 #define GE_OP_LOGD(opname, ...) GE_D_OP_LOGD(get_op_info(opname), __VA_ARGS__)
164 #define FUSION_PASS_LOGI(...) D_FUSION_PASS_LOGI(__VA_ARGS__)
165 #define FUSION_PASS_LOGW(...) D_FUSION_PASS_LOGW(__VA_ARGS__)
166 #define FUSION_PASS_LOGE(...) D_FUSION_PASS_LOGE(__VA_ARGS__)
167 #define FUSION_PASS_LOGD(...) D_FUSION_PASS_LOGD(__VA_ARGS__)
168 #else
169 #define AICPU_OP_LOGI(opname, ...)
170 #define AICPU_OP_LOGW(opname, ...)
171 #define AICPU_OP_LOGE(opname, ...)
172 #define AICPU_OP_LOGD(opname, ...)
173 #define AICPU_OP_LOGE_WITHOUT_REPORT(opname, ...)
174 #define OP_LOGI(opname, ...)
175 #define OP_LOGW(opname, ...)
176 #define OP_LOGE_WITHOUT_REPORT(opname, ...)
177 #define OP_LOGE(opname, ...)
178 #define OP_LOGD(opname, ...)
179 #define OP_EVENT(opname, ...)
180 #define FUSION_PASS_LOGI(...)
181 #define FUSION_PASS_LOGW(...)
182 #define FUSION_PASS_LOGE(...)
183 #define FUSION_PASS_LOGD(...)
184 #endif
185
186 #define OpLogSub(moduleId, level, op_info, fmt, ...) \
187 DlogSub(static_cast<int>(moduleId), OPPROTO_SUBMOD_NAME, level, "[%s][%" PRIu64 "] OpName:[%s] " #fmt, __FUNCTION__, \
188 OpLog::GetTid(), get_cstr(op_info), ##__VA_ARGS__)
189
190 #if !defined(__ANDROID__) && !defined(ANDROID)
191 #define AICPU_D_OP_LOGI(opname, fmt, ...) OpLogSub(AICPU, DLOG_INFO, opname, fmt, ##__VA_ARGS__)
192 #define AICPU_D_OP_LOGW(opname, fmt, ...) OpLogSub(AICPU, DLOG_WARN, opname, fmt, ##__VA_ARGS__)
193 #define AICPU_D_OP_LOGE(opname, fmt, ...) OpLogSub(AICPU, DLOG_ERROR, opname, fmt, ##__VA_ARGS__)
194 #define AICPU_D_OP_LOGD(opname, fmt, ...) OpLogSub(AICPU, DLOG_DEBUG, opname, fmt, ##__VA_ARGS__)
195 #define D_OP_LOGI(opname, fmt, ...) OpLogSub(OP, DLOG_INFO, opname, fmt, ##__VA_ARGS__)
196 #define D_OP_LOGW(opname, fmt, ...) OpLogSub(OP, DLOG_WARN, opname, fmt, ##__VA_ARGS__)
197 #define D_OP_LOGE(opname, fmt, ...) OpLogSub(OP, DLOG_ERROR, opname, fmt, ##__VA_ARGS__)
198 #define D_OP_LOGD(opname, fmt, ...) OpLogSub(OP, DLOG_DEBUG, opname, fmt, ##__VA_ARGS__)
199 #define D_OP_EVENT(opname, fmt, ...) OpLogSub(OP, DLOG_EVENT, opname, fmt, ##__VA_ARGS__)
200 #define GE_D_OP_LOGI(opname, fmt, ...) OpLogSub(GE, DLOG_INFO, opname, fmt, ##__VA_ARGS__)
201 #define GE_D_OP_LOGW(opname, fmt, ...) OpLogSub(GE, DLOG_WARN, opname, fmt, ##__VA_ARGS__)
202 #define GE_D_OP_LOGE(opname, fmt, ...) OpLogSub(GE, DLOG_ERROR, opname, fmt, ##__VA_ARGS__)
203 #define GE_D_OP_LOGD(opname, fmt, ...) OpLogSub(GE, DLOG_DEBUG, opname, fmt, ##__VA_ARGS__)
204 #define D_FUSION_PASS_LOGI(fmt, ...) \
205 DlogSub(FE, OPPROTO_SUBMOD_NAME, DLOG_INFO, " %s:%d " #fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
206 #define D_FUSION_PASS_LOGW(fmt, ...) \
207 DlogSub(FE, OPPROTO_SUBMOD_NAME, DLOG_WARN, " %s:%d " #fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
208 #define D_FUSION_PASS_LOGE(fmt, ...) \
209 DlogSub(FE, OPPROTO_SUBMOD_NAME, DLOG_ERROR, " %s:%d " #fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
210 #define D_FUSION_PASS_LOGD(fmt, ...) \
211 DlogSub(FE, OPPROTO_SUBMOD_NAME, DLOG_DEBUG, " %s:%d " #fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
212 #else
213 #define AICPU_D_OP_LOGI(opname, fmt, ...)
214 #define AICPU_D_OP_LOGW(opname, fmt, ...)
215 #define AICPU_D_OP_LOGE(opname, fmt, ...)
216 #define AICPU_D_OP_LOGD(opname, fmt, ...)
217 #define D_OP_LOGI(opname, fmt, ...)
218 #define D_OP_LOGW(opname, fmt, ...)
219 #define D_OP_LOGE(opname, fmt, ...)
220 #define D_OP_LOGD(opname, fmt, ...)
221 #define D_OP_EVENT(opname, fmt, ...)
222 #define D_FUSION_PASS_LOGI(fmt, ...)
223 #define D_FUSION_PASS_LOGW(fmt, ...)
224 #define D_FUSION_PASS_LOGE(fmt, ...)
225 #define D_FUSION_PASS_LOGD(fmt, ...)
226 #endif
227
228 #define unlikely(x) __builtin_expect((x), 0)
229 #define likely(x) __builtin_expect((x), 1)
230
231 #define OP_LOGE_IF(condition, return_value, op_name, fmt, ...) \
232 static_assert(std::is_same<bool, std::decay<decltype(condition)>::type>::value, "condition should be bool"); \
233 do { \
234 if (unlikely(condition)) { \
235 OP_LOGE(op_name, fmt, ##__VA_ARGS__); \
236 return return_value; \
237 } \
238 } while (0)
239
240 #define OP_LOGW_IF(condition, op_name, fmt, ...) \
241 static_assert(std::is_same<bool, std::decay<decltype(condition)>::type>::value, "condition should be bool"); \
242 do { \
243 if (unlikely(condition)) { \
244 OP_LOGW(op_name, fmt, ##__VA_ARGS__); \
245 } \
246 } while (0)
247
248 #define OP_LOGI_IF_RETURN(condition, return_value, op_name, fmt, ...) \
249 static_assert(std::is_same<bool, std::decay<decltype(condition)>::type>::value, "condition should be bool"); \
250 do { \
251 if (unlikely(condition)) { \
252 OP_LOGI(op_name, fmt, ##__VA_ARGS__); \
253 return return_value; \
254 } \
255 } while (0)
256 #endif // OPS_COMMON_INC_OP_LOG_H_
257