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.

e2e_dump.cc 20 kB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include "debug/data_dump/e2e_dump.h"
  17. #include <unistd.h>
  18. #include <algorithm>
  19. #include <map>
  20. #include <vector>
  21. #include "debug/data_dump/dump_json_parser.h"
  22. #include "common/trans.h"
  23. #include "debug/anf_ir_utils.h"
  24. #include "debug/common.h"
  25. #include "backend/session/anf_runtime_algorithm.h"
  26. #include "utils/ms_context.h"
  27. #include "runtime/device/kernel_runtime_manager.h"
  28. #include "utils/config_manager.h"
  29. #include "utils/file_utils.h"
  30. #ifdef ENABLE_DEBUGGER
  31. #include "debug/debug_services.h"
  32. #include "debug/tensor_load.h"
  33. #include "debug/debugger/debugger.h"
  34. #endif
  35. namespace mindspore {
  36. bool E2eDump::IsDeviceTargetGPU() {
  37. auto context = MsContext::GetInstance();
  38. MS_EXCEPTION_IF_NULL(context);
  39. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  40. }
  41. void E2eDump::DumpGPUMemToFile(const std::string &file_path, const std::string &original_kernel_name,
  42. const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  43. const TypeId &host_type, const TypeId &device_type, bool trans_flag, size_t slot,
  44. const Debugger *debugger) {
  45. #ifdef ENABLE_DEBUGGER
  46. auto format = kOpFormat_DEFAULT;
  47. MS_EXCEPTION_IF_NULL(debugger);
  48. auto ret = debugger->DumpTensorToFile(original_kernel_name, trans_flag, file_path, format, int_shapes, host_type,
  49. device_type, addr.format(), slot);
  50. if (!ret) {
  51. MS_LOG(ERROR) << "DumpTensorToFile Failed: flag:" << trans_flag << ", path:" << file_path
  52. << ", host_format:" << format;
  53. }
  54. #endif
  55. }
  56. void E2eDump::DumpOutput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  57. MS_EXCEPTION_IF_NULL(graph);
  58. auto &dump_json_parser = DumpJsonParser::GetInstance();
  59. if (!dump_json_parser.OutputNeedDump()) {
  60. return;
  61. }
  62. MS_LOG(INFO) << "Start e2e dump output";
  63. bool trans_flag = dump_json_parser.trans_flag();
  64. const auto &apply_kernels = graph->execution_order();
  65. for (const auto &node : apply_kernels) {
  66. MS_EXCEPTION_IF_NULL(node);
  67. std::string kernel_name = GetKernelNodeName(node);
  68. if (!dump_json_parser.NeedDump(kernel_name)) {
  69. continue;
  70. }
  71. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  72. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  73. }
  74. }
  75. void E2eDump::DumpOutputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  76. auto &dump_json_parser = DumpJsonParser::GetInstance();
  77. if (!dump_json_parser.OutputNeedDump()) {
  78. return;
  79. }
  80. bool trans_flag = dump_json_parser.trans_flag();
  81. MS_EXCEPTION_IF_NULL(node);
  82. std::string kernel_name = GetKernelNodeName(node);
  83. if (!dump_json_parser.NeedDump(kernel_name)) {
  84. return;
  85. }
  86. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  87. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  88. }
  89. void E2eDump::DumpOutputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  90. std::string *kernel_name, const Debugger *debugger) {
  91. MS_EXCEPTION_IF_NULL(node);
  92. GetFileKernelName(NOT_NULL(kernel_name));
  93. auto output_size = AnfAlgo::GetOutputTensorNum(node);
  94. for (size_t j = 0; j < output_size; ++j) {
  95. if (!AnfAlgo::OutputAddrExist(node, j)) {
  96. continue;
  97. }
  98. auto addr = AnfAlgo::GetOutputAddr(node, j);
  99. MS_EXCEPTION_IF_NULL(addr);
  100. ShapeVector int_shapes;
  101. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  102. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  103. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  104. std::string op_type = AnfAlgo::GetCNodeName(node);
  105. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  106. uint32_t task_id = 0;
  107. uint32_t stream_id = 0;
  108. uint64_t timestamp = GetTimeStamp();
  109. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  110. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  111. std::to_string(j);
  112. if (IsDeviceTargetGPU()) {
  113. DumpGPUMemToFile(file_path, GetKernelNodeName(node), *addr, int_shapes, type, device_type, trans_flag, j,
  114. debugger);
  115. } else {
  116. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  117. }
  118. }
  119. }
  120. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  121. MS_EXCEPTION_IF_NULL(graph);
  122. auto &dump_json_parser = DumpJsonParser::GetInstance();
  123. if (!dump_json_parser.InputNeedDump()) {
  124. return;
  125. }
  126. MS_LOG(INFO) << "Start e2e dump input";
  127. bool trans_flag = dump_json_parser.trans_flag();
  128. const auto &apply_kernels = graph->execution_order();
  129. for (const auto &node : apply_kernels) {
  130. MS_EXCEPTION_IF_NULL(node);
  131. std::string kernel_name = GetKernelNodeName(node);
  132. if (!dump_json_parser.NeedDump(kernel_name)) {
  133. continue;
  134. }
  135. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  136. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  137. }
  138. }
  139. void E2eDump::DumpInputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  140. auto &dump_json_parser = DumpJsonParser::GetInstance();
  141. if (!dump_json_parser.InputNeedDump()) {
  142. return;
  143. }
  144. bool trans_flag = dump_json_parser.trans_flag();
  145. MS_EXCEPTION_IF_NULL(node);
  146. std::string kernel_name = GetKernelNodeName(node);
  147. if (!dump_json_parser.NeedDump(kernel_name)) {
  148. return;
  149. }
  150. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  151. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  152. }
  153. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  154. std::string *kernel_name, const Debugger *debugger) {
  155. MS_EXCEPTION_IF_NULL(node);
  156. GetFileKernelName(NOT_NULL(kernel_name));
  157. auto input_size = AnfAlgo::GetInputTensorNum(node);
  158. for (size_t j = 0; j < input_size; ++j) {
  159. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  160. auto input = kernel_with_index.first;
  161. auto index = kernel_with_index.second;
  162. if (!AnfAlgo::OutputAddrExist(input, index)) {
  163. continue;
  164. }
  165. auto addr = AnfAlgo::GetOutputAddr(input, index);
  166. MS_EXCEPTION_IF_NULL(addr);
  167. std::string tensor_name;
  168. size_t slot;
  169. if (IsDeviceTargetGPU()) {
  170. auto input_kernel = node->input(j + 1);
  171. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  172. tensor_name = input_kernel_name;
  173. slot = 0;
  174. } else {
  175. tensor_name = GetKernelNodeName(node);
  176. slot = j;
  177. }
  178. ShapeVector int_shapes;
  179. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  180. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  181. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  182. std::string op_type = AnfAlgo::GetCNodeName(node);
  183. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  184. uint64_t timestamp = GetTimeStamp();
  185. uint32_t task_id = 0;
  186. uint32_t stream_id = 0;
  187. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  188. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".input." + std::to_string(j);
  189. if (IsDeviceTargetGPU()) {
  190. DumpGPUMemToFile(file_path, tensor_name, *addr, int_shapes, type, device_type, trans_flag, slot, debugger);
  191. } else {
  192. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  193. }
  194. }
  195. }
  196. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  197. bool trans_flag, std::map<std::string, size_t> *const_map, const Debugger *debugger) {
  198. MS_EXCEPTION_IF_NULL(anf_node);
  199. auto &dump_json_parser = DumpJsonParser::GetInstance();
  200. if ((!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) || IsValueNode<StringImm>(anf_node)) {
  201. return;
  202. }
  203. std::string node_name = GetKernelNodeName(anf_node);
  204. std::string dump_name = node_name;
  205. if (anf_node->isa<ValueNode>()) {
  206. auto iter = const_map->find(node_name);
  207. if (iter == const_map->end()) {
  208. return;
  209. }
  210. dump_name = std::string("cst") + std::to_string(iter->second);
  211. }
  212. if (!dump_json_parser.NeedDump(node_name)) {
  213. return;
  214. }
  215. DumpJsonParser::GetInstance().MatchKernel(node_name);
  216. GetFileKernelName(NOT_NULL(&node_name));
  217. // check if output address exists, if not, return;
  218. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  219. return;
  220. }
  221. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  222. MS_EXCEPTION_IF_NULL(addr);
  223. ShapeVector int_shapes;
  224. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  225. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  226. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  227. uint64_t timestamp = GetTimeStamp();
  228. uint32_t task_id = 0;
  229. uint32_t stream_id = 0;
  230. std::string file_path = dump_path + "/Parameter." + dump_name + '.' + std::to_string(task_id) + '.' +
  231. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output.0";
  232. if (IsDeviceTargetGPU()) {
  233. DumpGPUMemToFile(file_path, node_name, *addr, int_shapes, type, device_type, trans_flag, 0, debugger);
  234. } else {
  235. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  236. }
  237. }
  238. void E2eDump::DumpParametersAndConst(const session::KernelGraph *graph, const std::string &dump_path,
  239. const Debugger *debugger) {
  240. MS_EXCEPTION_IF_NULL(graph);
  241. auto &dump_json_parser = DumpJsonParser::GetInstance();
  242. if (!dump_json_parser.OutputNeedDump()) {
  243. return;
  244. }
  245. MS_LOG(INFO) << "Start e2e dump parameters and Const values";
  246. bool trans_flag = dump_json_parser.trans_flag();
  247. std::map<std::string, size_t> const_map;
  248. GetConstantId(graph, &const_map);
  249. // dump parameters
  250. const auto &parameters = graph->inputs();
  251. for (auto &item : parameters) {
  252. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  253. }
  254. // dump const values
  255. auto value_nodes = graph->graph_value_nodes();
  256. for (const auto &value_node : value_nodes) {
  257. DumpSingleAnfNode(value_node, VALUE_NODE_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  258. }
  259. }
  260. void E2eDump::DumpSetup(const session::KernelGraph *graph, uint32_t rank_id) {
  261. auto &dump_json_parser = DumpJsonParser::GetInstance();
  262. uint32_t cur_iter = dump_json_parser.cur_dump_iter();
  263. uint32_t graph_id = graph->graph_id();
  264. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || E2eDump::isDatasetGraph(graph));
  265. if (dump_json_parser.async_dump_enabled() || dump_json_parser.e2e_dump_enabled()) {
  266. if (IsDeviceTargetGPU()) {
  267. if (starting_graph_id == INT32_MAX) {
  268. starting_graph_id = graph_id;
  269. } else if (starting_graph_id == graph_id) {
  270. dump_json_parser.UpdateDumpIter();
  271. }
  272. } else {
  273. // Identify the first graph id and not increamenting dump iter for the first iteration (initial dump iter = 0).
  274. if (starting_graph_id == INT32_MAX) {
  275. starting_graph_id = graph_id;
  276. } else {
  277. // Update dump iter for ascend.
  278. // In multi network scripts, dump iter is equal to the number of networks that have been run so far.
  279. dump_json_parser.UpdateDumpIter();
  280. }
  281. }
  282. MS_LOG(DEBUG) << "sink_mode = " << sink_mode;
  283. }
  284. if (dump_json_parser.async_dump_enabled() && dump_json_parser.IsDumpIter(cur_iter) && !sink_mode) {
  285. auto zero_dir_dump_path =
  286. dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  287. auto root_cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/" +
  288. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id());
  289. auto cur_iter_dump_path = root_cur_iter_dump_path + "/" + std::to_string(cur_iter);
  290. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  291. MS_LOG(INFO) << "root_cur_iter_dump_path: " << root_cur_iter_dump_path;
  292. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  293. // create cur_iter_dump_path dirs
  294. auto dir_path = FileUtils::CreateNotExistDirs(root_cur_iter_dump_path);
  295. if (!dir_path.has_value()) {
  296. MS_LOG(EXCEPTION) << "Failed at CreateNotExistDirs for " << root_cur_iter_dump_path;
  297. }
  298. }
  299. }
  300. void E2eDump::DumpData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  301. MS_EXCEPTION_IF_NULL(graph);
  302. bool success = false;
  303. auto &dump_json_parser = DumpJsonParser::GetInstance();
  304. uint32_t graph_id = graph->graph_id();
  305. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || E2eDump::isDatasetGraph(graph));
  306. if (dump_json_parser.GetIterDumpFlag()) {
  307. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  308. MS_LOG(INFO) << "Current graph id is " << graph_id;
  309. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  310. DumpInput(graph, dump_path, debugger);
  311. DumpOutput(graph, dump_path, debugger);
  312. DumpParametersAndConst(graph, dump_path, debugger);
  313. success = true;
  314. } else if (dump_json_parser.async_dump_enabled() && !sink_mode) {
  315. uint32_t current_iter = dump_json_parser.cur_dump_iter();
  316. auto zero_dir_dump_path =
  317. dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  318. auto cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/" +
  319. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id()) + "/" +
  320. std::to_string(current_iter);
  321. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  322. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  323. if (dump_json_parser.IsDumpIter(current_iter)) {
  324. // create actual dir for iteration in final dump dir
  325. auto dir_path = FileUtils::CreateNotExistDirs(cur_iter_dump_path);
  326. if (!dir_path.has_value()) {
  327. MS_LOG(EXCEPTION) << "failed at CreateNotExistDirs for " << cur_iter_dump_path;
  328. }
  329. // test if zero_dir_dump_path exists (may not if there was
  330. // no data dumped, for example for an overflow dump)
  331. MS_LOG(INFO) << "Check " << zero_dir_dump_path << " exists.";
  332. bool dir_exists = DumpDirExists(zero_dir_dump_path);
  333. if (dir_exists) {
  334. // move contents from active dump dir to final dump dir
  335. MS_LOG(INFO) << "Move contents from " << zero_dir_dump_path << " to " << cur_iter_dump_path;
  336. bool move_files = MoveDumpFiles(zero_dir_dump_path, cur_iter_dump_path);
  337. if (!move_files) {
  338. MS_LOG(INFO) << "Issue with moving contents.";
  339. }
  340. } else {
  341. MS_LOG(INFO) << "active dump dir, not created yet";
  342. }
  343. } else {
  344. // test if zero_dir_dump_path exists (may not if there was
  345. // no data dumped, for example for an overflow dump)
  346. MS_LOG(INFO) << "Check " << zero_dir_dump_path << " exists.";
  347. bool dir_exists = DumpDirExists(zero_dir_dump_path);
  348. if (dir_exists) {
  349. // delete contents from active dump dir
  350. MS_LOG(INFO) << "Delete contents from active dump dir " << zero_dir_dump_path;
  351. bool delete_contents = DeleteDirContents(zero_dir_dump_path);
  352. if (!delete_contents) {
  353. MS_LOG(EXCEPTION) << "Ascend runtime has changed the dump dir structure!!!";
  354. }
  355. } else {
  356. MS_LOG(INFO) << "active dump dir, not created yet";
  357. }
  358. }
  359. success = true;
  360. }
  361. if (success) {
  362. MS_LOG(DEBUG) << "Dump Data completed!";
  363. } else {
  364. MS_LOG(DEBUG) << "Dump has not occurred!";
  365. }
  366. }
  367. bool E2eDump::DumpSingleNodeData(const CNodePtr &node, uint32_t graph_id, uint32_t rank_id, const Debugger *debugger) {
  368. bool success = false;
  369. auto &dump_json_parser = DumpJsonParser::GetInstance();
  370. if (dump_json_parser.GetIterDumpFlag()) {
  371. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  372. DumpInputSingleNode(node, dump_path, debugger);
  373. DumpOutputSingleNode(node, dump_path, debugger);
  374. success = true;
  375. }
  376. return success;
  377. }
  378. bool E2eDump::DumpParametersAndConstData(const session::KernelGraph *graph, uint32_t rank_id,
  379. const Debugger *debugger) {
  380. bool success = false;
  381. uint32_t graph_id = graph->graph_id();
  382. auto &dump_json_parser = DumpJsonParser::GetInstance();
  383. if (dump_json_parser.GetIterDumpFlag()) {
  384. MS_LOG(INFO) << "DumpParametersAndConst. Current iteration is " << dump_json_parser.cur_dump_iter();
  385. MS_LOG(INFO) << "Current graph id is " << graph_id;
  386. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  387. DumpParametersAndConst(graph, dump_path, debugger);
  388. success = true;
  389. }
  390. return success;
  391. }
  392. bool E2eDump::isDatasetGraph(const session::KernelGraph *graph) {
  393. // check if there is GetNext or InitDataSetQueue node
  394. const auto &nodes = graph->execution_order();
  395. for (const auto &node : nodes) {
  396. auto node_name = AnfAlgo::GetCNodeName(node);
  397. if (node_name == "GetNext" || node_name == "InitDataSetQueue") {
  398. return true;
  399. }
  400. }
  401. return false;
  402. }
  403. bool E2eDump::DumpDirExists(const std::string &dump_path) {
  404. DIR *dir = opendir(dump_path.c_str());
  405. if (dir != nullptr) {
  406. MS_LOG(INFO) << "Dump dir " << dump_path << " exists";
  407. if (closedir(dir) == -1) {
  408. MS_LOG(WARNING) << "Dump dir " << dump_path << " close failed!";
  409. }
  410. return true;
  411. }
  412. return false;
  413. }
  414. bool E2eDump::MoveDumpFiles(const std::string &first_dir, const std::string &second_dir) {
  415. DIR *d_handle = opendir(first_dir.c_str());
  416. struct dirent *next_file;
  417. while ((next_file = readdir(d_handle)) != NULL) {
  418. if (next_file->d_type != DT_REG) {
  419. continue;
  420. }
  421. // build the path for each file in the folder
  422. std::string file_name = next_file->d_name;
  423. std::string old_file_path = first_dir + "/" + file_name;
  424. std::string new_file_path = second_dir + "/" + file_name;
  425. if (rename(old_file_path.c_str(), new_file_path.c_str()) != 0) {
  426. if (closedir(d_handle) == -1) {
  427. MS_LOG(WARNING) << "Dump dir " << first_dir << " close failed!";
  428. }
  429. return false;
  430. }
  431. }
  432. if (closedir(d_handle) == -1) {
  433. MS_LOG(WARNING) << "Dump dir " << first_dir << " close failed!";
  434. }
  435. return true;
  436. }
  437. bool E2eDump::DeleteDirContents(const std::string &dir_path) {
  438. DIR *d_handle = opendir(dir_path.c_str());
  439. struct dirent *next_file;
  440. while ((next_file = readdir(d_handle)) != NULL) {
  441. if (next_file->d_type != DT_REG) {
  442. continue;
  443. }
  444. // build the path for each file in the folder
  445. std::string file_name = next_file->d_name;
  446. std::string file_path = dir_path + "/" + file_name;
  447. int res = remove(file_path.c_str());
  448. if (res != 0) {
  449. // Could not remove the file
  450. if (closedir(d_handle) == -1) {
  451. MS_LOG(WARNING) << "Dump dir " << dir_path << " close failed!";
  452. }
  453. return false;
  454. }
  455. }
  456. if (closedir(d_handle) == -1) {
  457. MS_LOG(WARNING) << "Dump dir " << dir_path << " close failed!";
  458. }
  459. return true;
  460. }
  461. } // namespace mindspore