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.

dump_data_builder.h 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright 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_DUMP_DATA_BUILDER_H_
  17. #define MINDSPORE_CCSRC_DEBUG_DUMP_DATA_BUILDER_H_
  18. #include <vector>
  19. #include <string>
  20. #include <iostream>
  21. #include <algorithm>
  22. #include "utils/log_adapter.h"
  23. #ifdef ENABLE_D
  24. #include "proto/dump_data.pb.h"
  25. #include "toolchain/adx_datadump_callback.h"
  26. using Adx::DumpChunk;
  27. #endif
  28. // This class is for building dump data receiving from adx server. Tensor Data for each kernel will be divided in pieces
  29. // and each piece would be wrapped into DumpChunk struct. This class provides function to merge dump chunks and
  30. // construct dump data object.
  31. class DumpDataBuilder {
  32. public:
  33. DumpDataBuilder() {}
  34. ~DumpDataBuilder() = default;
  35. #ifdef ENABLE_D
  36. bool CopyDumpChunk(const DumpChunk *dump_chunk) {
  37. try {
  38. uint32_t buf_sz = dump_chunk->bufLen;
  39. std::string buffer_str(reinterpret_cast<const char *>(dump_chunk->dataBuf), buf_sz);
  40. chunk_list_.push_back(buffer_str);
  41. total_sz_ += buf_sz;
  42. } catch (std::bad_alloc &err) {
  43. MS_LOG(ERROR) << "Failed to allocate memory for " << dump_chunk->fileName << ", reason: " << err.what();
  44. return false;
  45. }
  46. return true;
  47. }
  48. bool ConstructDumpData(debugger::dump::DumpData *dump_data_proto, std::vector<char> *data_ptr) {
  49. if (chunk_list_.empty()) {
  50. return false;
  51. }
  52. // merge several chunks into one piece.
  53. std::string dump_proto_str;
  54. dump_proto_str.reserve(total_sz_);
  55. for (auto item : chunk_list_) {
  56. dump_proto_str += item;
  57. }
  58. chunk_list_.clear();
  59. const int8_t header_len_offset = 8;
  60. uint64_t header_len = *reinterpret_cast<const uint64_t *>(dump_proto_str.c_str());
  61. std::string header = dump_proto_str.substr(header_len_offset, header_len);
  62. if (!(*dump_data_proto).ParseFromString(header)) {
  63. MS_LOG(ERROR) << "Failed to parse dump proto file.";
  64. return false;
  65. }
  66. auto data_sz = total_sz_ - header_len_offset - header_len;
  67. data_ptr->resize(data_sz);
  68. // The security memory copy function 'memcpy_s' has a size limit (SECUREC_MEM_MAX_LEN). If the data size is greater
  69. // than that, it should be cut into segments to copy. Otherwise, memcpy_s will fail.
  70. int ret;
  71. if (data_sz < SECUREC_MEM_MAX_LEN) {
  72. ret = memcpy_s(data_ptr->data(), data_sz, dump_proto_str.c_str() + header_len_offset + header_len, data_sz);
  73. } else {
  74. size_t mem_cpy_len;
  75. for (size_t pos = 0; pos < data_sz; pos += SECUREC_MEM_MAX_LEN) {
  76. mem_cpy_len = std::min(data_sz - pos, SECUREC_MEM_MAX_LEN);
  77. ret = memcpy_s(data_ptr->data() + pos, mem_cpy_len,
  78. dump_proto_str.c_str() + header_len_offset + header_len + pos, mem_cpy_len);
  79. if (ret != 0) {
  80. break;
  81. }
  82. }
  83. }
  84. if (ret != 0) {
  85. MS_LOG(ERROR) << "Failed to memcpy: error code (" << ret << ").";
  86. return false;
  87. }
  88. return true;
  89. }
  90. #endif
  91. private:
  92. std::vector<std::string> chunk_list_;
  93. uint64_t total_sz_{0};
  94. };
  95. #endif // MINDSPORE_CCSRC_DEBUG_DUMP_DATA_BUILDER_H_