You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.h 4.1 kB

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