• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEBUG_COMMON_H_
18 #define MINDSPORE_CCSRC_DEBUG_COMMON_H_
19 
20 #include <string>
21 #include <optional>
22 #include "utils/contract.h"
23 #include "utils/ms_context.h"
24 #include "utils/comm_manager.h"
25 #include "utils/system/base.h"
26 
27 namespace mindspore {
28 static const int MAX_DIRECTORY_LENGTH = 1024;
29 static const int MAX_FILENAME_LENGTH = 128;
30 static const int MAX_OS_FILENAME_LENGTH = 255;
31 class Common {
32  public:
33   Common() = default;
34   ~Common() = default;
35   static std::optional<std::string> CreatePrefixPath(const std::string &input_path,
36                                                      const bool support_relative_path = false);
37   static std::optional<std::string> GetConfigFile(const std::string &env);
38   static bool IsStrLengthValid(const std::string &str, size_t length_limit, const std::string &error_message = "");
39   static bool IsPathValid(const std::string &path, size_t length_limit, const std::string &error_message = "");
40   static bool IsFilenameValid(const std::string &filename, size_t length_limit, const std::string &error_message = "");
41 
42   static std::string AddId(const std::string &filename, const std::string &suffix);
43   static bool SaveStringToFile(const std::string filename, const std::string string_info);
44   static bool FileExists(const std::string &filepath);
45   static bool CommonFuncForConfigPath(const std::string &default_path, const std::string &env_path, std::string *value);
46 
47  private:
48   static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
49 };
50 
51 inline std::string GetSaveGraphsPathName(const std::string &file_name, const std::string &save_path = "") {
52   std::string save_graphs_path;
53   if (save_path.empty()) {
54     auto ms_context = MsContext::GetInstance();
55     MS_EXCEPTION_IF_NULL(ms_context);
56     save_graphs_path = ms_context->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
57     if (save_graphs_path.empty()) {
58       save_graphs_path = ".";
59     }
60   } else {
61     save_graphs_path = save_path;
62   }
63   if (IsStandAlone()) {
64     return save_graphs_path + "/" + file_name;
65   }
66   return save_graphs_path + "/rank_" + std::to_string(GetRank()) + "/" + file_name;
67 }
68 
ErrnoToString(const int error_number)69 inline std::string ErrnoToString(const int error_number) {
70   std::ostringstream ret_info;
71   ret_info << " Errno: " << error_number;
72 #if defined(SYSTEM_ENV_POSIX)
73   char err_info[MAX_FILENAME_LENGTH];
74   char *ret = strerror_r(error_number, err_info, sizeof(err_info));
75   if (ret != nullptr) {
76     ret_info << ", ErrInfo: " << ret;
77   }
78 #elif defined(SYSTEM_ENV_WINDOWS)
79   char err_info[MAX_FILENAME_LENGTH];
80   (void)strerror_s(err_info, sizeof(err_info), error_number);
81   ret_info << ", ErrInfo: " << err_info;
82 #endif
83   return ret_info.str();
84 }
85 }  // namespace mindspore
86 #endif  // MINDSPORE_CCSRC_DEBUG_COMMON_H_
87