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.

grpc_client.cc 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/debugger/grpc_client.h"
  17. #include <thread>
  18. #include <vector>
  19. #include "utils/log_adapter.h"
  20. using debugger::Chunk;
  21. using debugger::EventListener;
  22. using debugger::EventReply;
  23. using debugger::EventReply_Status_FAILED;
  24. using debugger::GraphProto;
  25. using debugger::Metadata;
  26. using debugger::TensorProto;
  27. using debugger::WatchpointHit;
  28. namespace mindspore {
  29. GrpcClient::GrpcClient(const std::string &host, const std::string &port) : stub_(nullptr) { Init(host, port); }
  30. void GrpcClient::Init(const std::string &host, const std::string &port) {
  31. std::string target_str = host + ":" + port;
  32. MS_LOG(INFO) << "GrpcClient connecting to: " << target_str;
  33. std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
  34. stub_ = EventListener::NewStub(channel);
  35. }
  36. void GrpcClient::Reset() { stub_ = nullptr; }
  37. EventReply GrpcClient::WaitForCommand(const Metadata &metadata) {
  38. EventReply reply;
  39. grpc::ClientContext context;
  40. grpc::Status status = stub_->WaitCMD(&context, metadata, &reply);
  41. if (!status.ok()) {
  42. MS_LOG(ERROR) << "RPC failed: WaitForCommand";
  43. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  44. reply.set_status(EventReply_Status_FAILED);
  45. }
  46. return reply;
  47. }
  48. EventReply GrpcClient::SendMetadata(const Metadata &metadata) {
  49. EventReply reply;
  50. grpc::ClientContext context;
  51. grpc::Status status = stub_->SendMetadata(&context, metadata, &reply);
  52. if (!status.ok()) {
  53. MS_LOG(ERROR) << "RPC failed: SendMetadata";
  54. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  55. reply.set_status(EventReply_Status_FAILED);
  56. }
  57. return reply;
  58. }
  59. std::vector<std::string> GrpcClient::ChunkString(std::string str, int graph_size) {
  60. std::vector<std::string> buf;
  61. constexpr auto l_chunk_size = 1024 * 1024 * 3;
  62. int size_iter = 0;
  63. while (size_iter < graph_size) {
  64. int chunk_size = l_chunk_size;
  65. if (graph_size - size_iter < l_chunk_size) {
  66. chunk_size = graph_size - size_iter;
  67. }
  68. std::string buffer;
  69. buffer.resize(chunk_size);
  70. auto err = memcpy_s(reinterpret_cast<char *>(buffer.data()), chunk_size, str.data() + size_iter, chunk_size);
  71. if (err != 0) {
  72. MS_LOG(EXCEPTION) << "memcpy_s failed. errorno is: " << err;
  73. }
  74. buf.push_back(buffer);
  75. if (size_iter > INT_MAX - l_chunk_size) {
  76. MS_EXCEPTION(ValueError) << size_iter << " + " << l_chunk_size << "would lead to integer overflow!";
  77. }
  78. size_iter += l_chunk_size;
  79. }
  80. return buf;
  81. }
  82. EventReply GrpcClient::SendGraph(const GraphProto &graph) {
  83. EventReply reply;
  84. grpc::ClientContext context;
  85. Chunk chunk;
  86. std::unique_ptr<grpc::ClientWriter<Chunk> > writer(stub_->SendGraph(&context, &reply));
  87. std::string str = graph.SerializeAsString();
  88. int graph_size = graph.ByteSize();
  89. auto buf = ChunkString(str, graph_size);
  90. for (unsigned int i = 0; i < buf.size(); i++) {
  91. MS_LOG(INFO) << "RPC:sending the " << i << "chunk in graph";
  92. chunk.set_buffer(buf[i]);
  93. if (!writer->Write(chunk)) {
  94. break;
  95. }
  96. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  97. }
  98. writer->WritesDone();
  99. grpc::Status status = writer->Finish();
  100. if (!status.ok()) {
  101. MS_LOG(ERROR) << "RPC failed: SendGraph";
  102. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  103. reply.set_status(EventReply_Status_FAILED);
  104. }
  105. return reply;
  106. }
  107. EventReply GrpcClient::SendMultiGraphs(const std::list<Chunk> &chunks) {
  108. EventReply reply;
  109. grpc::ClientContext context;
  110. std::unique_ptr<grpc::ClientWriter<Chunk> > writer(stub_->SendMultiGraphs(&context, &reply));
  111. for (const auto &chunk : chunks) {
  112. if (!writer->Write(chunk)) {
  113. break;
  114. }
  115. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  116. }
  117. writer->WritesDone();
  118. grpc::Status status = writer->Finish();
  119. if (!status.ok()) {
  120. MS_LOG(ERROR) << "RPC failed: SendMultigraphs";
  121. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  122. reply.set_status(EventReply_Status_FAILED);
  123. }
  124. return reply;
  125. }
  126. EventReply GrpcClient::SendTensors(const std::list<TensorProto> &tensors) {
  127. EventReply reply;
  128. grpc::ClientContext context;
  129. std::unique_ptr<grpc::ClientWriter<TensorProto> > writer(stub_->SendTensors(&context, &reply));
  130. for (const auto &tensor : tensors) {
  131. if (!writer->Write(tensor)) {
  132. break;
  133. }
  134. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  135. }
  136. writer->WritesDone();
  137. grpc::Status status = writer->Finish();
  138. if (!status.ok()) {
  139. MS_LOG(ERROR) << "RPC failed: SendTensors";
  140. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  141. reply.set_status(EventReply_Status_FAILED);
  142. }
  143. return reply;
  144. }
  145. EventReply GrpcClient::SendWatchpointHits(const std::list<WatchpointHit> &watchpoints) {
  146. EventReply reply;
  147. grpc::ClientContext context;
  148. std::unique_ptr<grpc::ClientWriter<WatchpointHit> > writer(stub_->SendWatchpointHits(&context, &reply));
  149. for (const auto &watchpoint : watchpoints) {
  150. if (!writer->Write(watchpoint)) {
  151. break;
  152. }
  153. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  154. }
  155. writer->WritesDone();
  156. grpc::Status status = writer->Finish();
  157. if (!status.ok()) {
  158. MS_LOG(ERROR) << "RPC failed: SendWatchpointHits";
  159. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  160. reply.set_status(EventReply_Status_FAILED);
  161. }
  162. return reply;
  163. }
  164. } // namespace mindspore