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