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