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.

memory_dumper.cc 6.5 kB

5 years ago
5 years ago
5 years ago
5 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
5 years ago
5 years ago
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
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright 2020 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. #include "common/debug/memory_dumper.h"
  17. #include <string>
  18. #include "framework/common/debug/log.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/common/util.h"
  21. #include "framework/common/ge_inner_error_codes.h"
  22. using std::string;
  23. namespace {
  24. const int kInvalidFd = (-1);
  25. } // namespace
  26. namespace ge {
  27. MemoryDumper::MemoryDumper() : fd_(kInvalidFd) {}
  28. MemoryDumper::~MemoryDumper() { Close(); }
  29. // Dump the data to the file
  30. Status MemoryDumper::DumpToFile(const char *filename, void *data, int64_t len) {
  31. #ifdef FMK_SUPPORT_DUMP
  32. GE_CHECK_NOTNULL(filename);
  33. GE_CHECK_NOTNULL(data);
  34. if (len == 0) {
  35. GELOGE(FAILED, "[Check][Param]Failed, data length is 0.");
  36. REPORT_INNER_ERROR("E19999", "Check param failed, data length is 0.");
  37. return PARAM_INVALID;
  38. }
  39. // Open the file
  40. int fd = OpenFile(filename);
  41. if (fd == kInvalidFd) {
  42. GELOGE(FAILED, "[Open][File]Failed, filename:%s.", filename);
  43. REPORT_INNER_ERROR("E19999", "Opne file failed, filename:%s.", filename);
  44. return FAILED;
  45. }
  46. // Write the data to the file
  47. Status ret = SUCCESS;
  48. int32_t mmpa_ret = mmWrite(fd, data, len);
  49. // mmWrite return -1:Failed to write data to file;return -2:Invalid parameter
  50. if (mmpa_ret == EN_ERROR || mmpa_ret == EN_INVALID_PARAM) {
  51. GELOGE(FAILED, "[Write][Data]Failed, errno:%d, errmsg:%s", mmpa_ret, strerror(errno));
  52. REPORT_INNER_ERROR("E19999", "Write data failed, errno:%d, errmsg:%s.",
  53. mmpa_ret, strerror(errno));
  54. ret = FAILED;
  55. }
  56. // Close the file
  57. if (mmClose(fd) != EN_OK) { // mmClose return 0: success
  58. GELOGE(FAILED, "[Close][File]Failed, error_code:%u, filename:%s errmsg:%s.", ret, filename, strerror(errno));
  59. REPORT_INNER_ERROR("E19999", "Close file failed, error_code:%u, filename:%s errmsg:%s.",
  60. ret, filename, strerror(errno));
  61. ret = FAILED;
  62. }
  63. return ret;
  64. #else
  65. GELOGW("need to define FMK_SUPPORT_DUMP for dump op input and output.");
  66. return SUCCESS;
  67. #endif
  68. }
  69. // Open file
  70. Status MemoryDumper::Open(const char *filename) {
  71. GE_CHK_BOOL_RET_STATUS(filename != nullptr, FAILED, "Incorrect parameter. filename is nullptr");
  72. // Try to remove file first for reduce the close time by overwriting way
  73. // (The process of file closing will be about 100~200ms slower per file when written by overwriting way)
  74. // If remove file failed, then try to open it with overwriting way
  75. int ret = remove(filename);
  76. // If remove file failed, print the warning log
  77. if (ret != 0) {
  78. GELOGW("Remove file failed.");
  79. }
  80. fd_ = OpenFile(filename);
  81. if (fd_ == kInvalidFd) {
  82. GELOGE(FAILED, "[Open][File]Failed, filename:%s.", filename);
  83. REPORT_INNER_ERROR("E19999", "Open file:%s failed.", filename);
  84. return FAILED;
  85. }
  86. return SUCCESS;
  87. }
  88. // Dump the data to file
  89. Status MemoryDumper::Dump(void *data, uint32_t len) const {
  90. GE_CHK_BOOL_RET_STATUS(data != nullptr, FAILED, "Incorrect parameter. data is nullptr");
  91. #ifdef FMK_SUPPORT_DUMP
  92. int32_t mmpa_ret = mmWrite(fd_, data, len);
  93. // mmWrite return -1:failed to write data to file;return -2:invalid parameter
  94. if (mmpa_ret == EN_ERROR || mmpa_ret == EN_INVALID_PARAM) {
  95. GELOGE(FAILED, "[Write][Data]Failed, errno:%d, errmsg:%s", mmpa_ret, strerror(errno));
  96. REPORT_INNER_ERROR("E19999", "Write data to file failed, errno:%d, errmsg:%s.",
  97. mmpa_ret, strerror(errno));
  98. return FAILED;
  99. }
  100. return SUCCESS;
  101. #else
  102. GELOGW("need to define FMK_SUPPORT_DUMP for dump op input and output.");
  103. return SUCCESS;
  104. #endif
  105. }
  106. // Close file
  107. void MemoryDumper::Close() noexcept {
  108. // Close file
  109. if (fd_ != kInvalidFd && mmClose(fd_) != EN_OK) {
  110. GELOGW("Close file failed, errmsg:%s.", strerror(errno));
  111. }
  112. fd_ = kInvalidFd;
  113. }
  114. // Open file
  115. int MemoryDumper::OpenFile(const char *filename) {
  116. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(filename == nullptr, return kInvalidFd, "Incorrect parameter. filename is nullptr");
  117. // Find the last separator
  118. int path_split_pos = static_cast<int>(strlen(filename) - 1);
  119. for (; path_split_pos >= 0; path_split_pos--) {
  120. GE_IF_BOOL_EXEC(filename[path_split_pos] == '\\' || filename[path_split_pos] == '/', break;)
  121. }
  122. // Get the absolute path
  123. string real_path;
  124. char tmp_path[MMPA_MAX_PATH] = {0};
  125. GE_IF_BOOL_EXEC(
  126. -1 != path_split_pos, string prefix_path = std::string(filename).substr(0, path_split_pos);
  127. string last_path = std::string(filename).substr(path_split_pos, strlen(filename) - 1);
  128. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(prefix_path.length() >= MMPA_MAX_PATH,
  129. return kInvalidFd, "Prefix path is too long!");
  130. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(mmRealPath(prefix_path.c_str(), tmp_path, MMPA_MAX_PATH) != EN_OK, return kInvalidFd,
  131. "Dir %s does not exit, errmsg:%s.", prefix_path.c_str(), strerror(errno));
  132. real_path = std::string(tmp_path) + last_path;)
  133. GE_IF_BOOL_EXEC(
  134. path_split_pos == -1 || path_split_pos == 0,
  135. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(filename) >= MMPA_MAX_PATH, return kInvalidFd, "Prefix path is too long!");
  136. GE_IF_BOOL_EXEC(mmRealPath(filename, tmp_path, MMPA_MAX_PATH) != EN_OK,
  137. GELOGI("File %s does not exit, it will be created.", filename));
  138. real_path = std::string(tmp_path);)
  139. // Open file, only the current user can read and write, to avoid malicious application access
  140. // Using the O_EXCL, if the file already exists,return failed to avoid privilege escalation vulnerability.
  141. mmMode_t mode = M_IRUSR | M_IWUSR;
  142. int32_t fd = mmOpen2(real_path.c_str(), M_RDWR | M_CREAT | M_APPEND, mode);
  143. if (fd == EN_ERROR || fd == EN_INVALID_PARAM) {
  144. GELOGE(kInvalidFd, "[Open][File]Failed. errno:%d, errmsg:%s, filename:%s.",
  145. fd, strerror(errno), filename);
  146. return kInvalidFd;
  147. }
  148. return fd;
  149. }
  150. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示