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.4 kB

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