1 /**
2 * Copyright 2021 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 #ifndef MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_LOG_H_
18 #define MINDSPORE_CORE_MINDRT_INCLUDE_ACTOR_LOG_H_
19
20 #include <signal.h>
21 #include <iostream>
22
23 #include <sstream>
24 #include <string>
25
26 #include "actor/errcode.h"
27 #ifdef USE_GLOG
28 #include "utils/log_adapter.h"
29 #else
30 #include "common/log_adapter.h"
31 #endif
32 namespace mindspore {
33 #define FlushHLogCache()
34 // Kill the process for safe exiting.
KillProcess(const std::string & ret)35 inline void KillProcess(const std::string &ret) {
36 MS_LOG(DEBUG) << "MINDRT Exit Tip:" << ret.c_str();
37 // flush the log in cache to disk before exiting.
38 FlushHLogCache();
39 }
40
41 } // namespace mindspore
42
43 #define MINDRT_ASSERT(expression) \
44 do { \
45 if (!(expression)) { \
46 std::stringstream ss; \
47 ss << "Assertion failed: " << #expression << ", file: " << __FILE__ << ", line: " << __LINE__; \
48 mindspore::KillProcess(ss.str()); \
49 } \
50 } while (0)
51
52 #define MINDRT_EXIT(ret) \
53 do { \
54 std::stringstream ss; \
55 ss << (ret) << " ( file: " << __FILE__ << ", line: " << __LINE__ << " )."; \
56 mindspore::KillProcess(ss.str()); \
57 } while (0)
58
59 #define MINDRT_OOM_EXIT(ptr) \
60 { \
61 if (ptr == nullptr) { \
62 MINDRT_EXIT("Exit for OOM."); \
63 } \
64 }
65
66 constexpr int LOG_CHECK_EVERY_FIRSTNUM = 10;
67 constexpr int LOG_CHECK_EVERY_NUM1 = 10;
68 constexpr int LOG_CHECK_EVERY_NUM2 = 100;
69 constexpr int LOG_CHECK_EVERY_NUM3 = 1000;
70 constexpr int LOG_CHECK_EVERY_NUM4 = 10000;
71
72 #define LOG_CHECK_ID_CONCAT(word1, word2) word1##word2
73
74 #define LOG_CHECK_ID LOG_CHECK_ID_CONCAT(__FUNCTION__, __LINE__)
75
76 #define LOG_CHECK_FIRST_N \
77 [](uint32_t firstNum) { \
78 static uint32_t LOG_CHECK_ID = 0; \
79 ++LOG_CHECK_ID; \
80 return (LOG_CHECK_ID <= firstNum); \
81 }
82
83 #define LOG_CHECK_EVERY_N1 \
84 [](uint32_t firstNum, uint32_t num) { \
85 static uint32_t LOG_CHECK_ID = 0; \
86 ++LOG_CHECK_ID; \
87 return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID % num == 0)); \
88 }
89
90 #define LOG_CHECK_EVERY_N2 \
91 [](uint32_t firstNum, uint32_t num1, uint32_t num2) { \
92 static uint32_t LOG_CHECK_ID = 0; \
93 ++LOG_CHECK_ID; \
94 return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
95 (LOG_CHECK_ID % num2 == 0)); \
96 }
97
98 #define LOG_CHECK_EVERY_N3 \
99 [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3) { \
100 static uint32_t LOG_CHECK_ID = 0; \
101 ++LOG_CHECK_ID; \
102 return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
103 (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID % num3 == 0)); \
104 }
105
106 #define LOG_CHECK_EVERY_N4 \
107 [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4) { \
108 static uint32_t LOG_CHECK_ID = 0; \
109 ++LOG_CHECK_ID; \
110 return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \
111 (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID < num4 && LOG_CHECK_ID % num3 == 0) || \
112 (LOG_CHECK_ID % num4 == 0)); \
113 }
114
115 #define LOG_CHECK_EVERY_N \
116 []() { \
117 static uint32_t LOG_CHECK_ID = 0; \
118 ++LOG_CHECK_ID; \
119 return ((LOG_CHECK_ID <= LOG_CHECK_EVERY_FIRSTNUM) || \
120 (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM2 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM1 == 0) || \
121 (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM3 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM2 == 0) || \
122 (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM4 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM3 == 0) || \
123 (LOG_CHECK_ID % LOG_CHECK_EVERY_NUM4 == 0)); \
124 }
125
126 #endif
127