1 /**
2 * Copyright 2020-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_CCSRC_INCLUDE_COMMON_DEBUG_COMMON_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_COMMON_H_
19
20 #include <string>
21 #include <optional>
22 #include <set>
23 #if defined(SYSTEM_ENV_POSIX)
24 #include <mutex>
25 #endif
26 #include "include/common/visible.h"
27 #include "include/common/utils/contract.h"
28 #include "utils/ms_context.h"
29 #include "include/common/utils/comm_manager.h"
30 #include "utils/system/base.h"
31
32 namespace mindspore {
33 static const int MAX_DIRECTORY_LENGTH = 1024;
34 static const int MAX_FILENAME_LENGTH = 128;
35 static const int MAX_OS_FILENAME_LENGTH = 255;
36
37 class COMMON_EXPORT Common {
38 public:
39 Common() = default;
40 ~Common() = default;
41 static bool NeedMapping(const std::string &origin_name);
42 static std::string GetRandomStr();
43 static std::string GetRandomStr(size_t str_len);
44 static bool MappingName(const std::string &input_path, std::optional<std::string> *prefix_path,
45 std::optional<std::string> *origin_name, std::optional<std::string> *mapped_name);
46 static std::optional<std::string> CreatePrefixPath(const std::string &input_path,
47 const bool support_relative_path = false);
48 static std::optional<std::string> GetConfigFile(const std::string &env);
49 static bool IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message = "");
50 static bool IsPathValid(const std::string &path, size_t length_limit, const std::string &error_message = "");
51 static bool IsFilenameValid(const std::string &filename, size_t length_limit, const std::string &error_message = "");
52
53 static std::string AddId(const std::string &filename, const std::string &suffix);
54 static bool SaveStringToFile(const std::string filename, const std::string string_info);
55 static bool FileExists(const std::string &filepath);
56 static bool CommonFuncForConfigPath(const std::string &default_path, const std::string &env_path,
57 std::string *const value);
58 static std::string GetCompilerCachePath();
59 static std::string GetKernelMetaTempDir();
60 static std::string GetUserDefineCachePath();
61 static bool GetDebugTerminate();
62 static bool GetDebugExitSuccess();
63 static void DebugTerminate(bool val, bool exit_success);
64 static bool CheckInterval();
65 static bool CheckIfPrintIrPass(const std::string &pass_name);
66
67 // Get time stamp since epoch in microseconds
68 static uint64_t GetTimeStamp();
69
70 private:
71 static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
72
73 inline static bool debugger_terminate_ = false;
74 inline static bool exit_success_ = false;
75 inline static size_t g_id_ = 0;
76 #if defined(SYSTEM_ENV_POSIX)
77 inline static std::mutex random_data_lock_;
78 #endif
79 };
80
81 inline std::string GetSaveGraphsPathName(const std::string &file_name, const std::string &save_path = "") {
82 std::string save_graphs_path;
83 if (save_path.empty()) {
84 auto ms_context = MsContext::GetInstance();
85 MS_EXCEPTION_IF_NULL(ms_context);
86 save_graphs_path = ms_context->GetSaveGraphsPath();
87 if (save_graphs_path.empty()) {
88 save_graphs_path = ".";
89 }
90 } else {
91 save_graphs_path = save_path;
92 }
93 if (IsStandAlone()) {
94 return save_graphs_path + "/" + file_name;
95 }
96 return save_graphs_path + "/rank_" + std::to_string(GetRank()) + "/" + file_name;
97 }
98
ErrnoToString(const int error_number)99 inline std::string ErrnoToString(const int error_number) {
100 std::ostringstream ret_info;
101 ret_info << " Errno: " << error_number;
102 #if defined(__APPLE__)
103 char err_info[MAX_FILENAME_LENGTH];
104 (void)strerror_r(error_number, err_info, sizeof(err_info));
105 ret_info << ", ErrInfo: " << err_info;
106 #elif defined(SYSTEM_ENV_POSIX)
107 char err_info[MAX_FILENAME_LENGTH];
108 char *ret = strerror_r(error_number, err_info, sizeof(err_info));
109 if (ret != nullptr) {
110 ret_info << ", ErrInfo: " << ret;
111 }
112 #elif defined(SYSTEM_ENV_WINDOWS)
113 char err_info[MAX_FILENAME_LENGTH];
114 (void)strerror_s(err_info, sizeof(err_info), error_number);
115 ret_info << ", ErrInfo: " << err_info;
116 #endif
117 return ret_info.str();
118 }
119 } // namespace mindspore
120 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_COMMON_H_
121