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 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "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. #define CHUNK_SIZE 1024 * 1024 * 3
  29. namespace mindspore {
  30. GrpcClient::GrpcClient(const std::string &host, const std::string &port) : stub_(nullptr) { Init(host, port); }
  31. void GrpcClient::Init(const std::string &host, const std::string &port) {
  32. std::string target_str = host + ":" + port;
  33. MS_LOG(INFO) << "GrpcClient connecting to: " << target_str;
  34. std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
  35. stub_ = EventListener::NewStub(channel);
  36. }
  37. void GrpcClient::Reset() { stub_ = nullptr; }
  38. EventReply GrpcClient::WaitForCommand(const Metadata &metadata) {
  39. EventReply reply;
  40. grpc::ClientContext context;
  41. grpc::Status status = stub_->WaitCMD(&context, metadata, &reply);
  42. if (!status.ok()) {
  43. MS_LOG(ERROR) << "RPC failed: WaitForCommand";
  44. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  45. reply.set_status(EventReply_Status_FAILED);
  46. }
  47. return reply;
  48. }
  49. EventReply GrpcClient::SendMetadata(const Metadata &metadata) {
  50. EventReply reply;
  51. grpc::ClientContext context;
  52. grpc::Status status = stub_->SendMetadata(&context, metadata, &reply);
  53. if (!status.ok()) {
  54. MS_LOG(ERROR) << "RPC failed: SendMetadata";
  55. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  56. reply.set_status(EventReply_Status_FAILED);
  57. }
  58. return reply;
  59. }
  60. std::vector<std::string> ChunkString(std::string str, int graph_size) {
  61. std::vector<std::string> buf;
  62. int size_iter = 0;
  63. while (size_iter < graph_size) {
  64. int chunk_size = CHUNK_SIZE;
  65. if (graph_size - size_iter < 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. size_iter += CHUNK_SIZE;
  76. }
  77. return buf;
  78. }
  79. EventReply GrpcClient::SendGraph(const GraphProto &graph) {
  80. EventReply reply;
  81. grpc::ClientContext context;
  82. Chunk chunk;
  83. std::unique_ptr<grpc::ClientWriter<Chunk> > writer(stub_->SendGraph(&context, &reply));
  84. std::string str = graph.SerializeAsString();
  85. int graph_size = graph.ByteSize();
  86. auto buf = ChunkString(str, graph_size);
  87. for (unsigned int i = 0; i < buf.size(); i++) {
  88. MS_LOG(INFO) << "RPC:sending the " << i << "chunk in graph";
  89. chunk.set_buffer(buf[i]);
  90. if (!writer->Write(chunk)) {
  91. break;
  92. }
  93. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  94. }
  95. writer->WritesDone();
  96. grpc::Status status = writer->Finish();
  97. if (!status.ok()) {
  98. MS_LOG(ERROR) << "RPC failed: SendGraph";
  99. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  100. reply.set_status(EventReply_Status_FAILED);
  101. }
  102. return reply;
  103. }
  104. EventReply GrpcClient::SendTensors(const std::list<TensorProto> &tensors) {
  105. EventReply reply;
  106. grpc::ClientContext context;
  107. std::unique_ptr<grpc::ClientWriter<TensorProto> > writer(stub_->SendTensors(&context, &reply));
  108. for (const auto &tensor : tensors) {
  109. if (!writer->Write(tensor)) {
  110. break;
  111. }
  112. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  113. }
  114. writer->WritesDone();
  115. grpc::Status status = writer->Finish();
  116. if (!status.ok()) {
  117. MS_LOG(ERROR) << "RPC failed: SendTensors";
  118. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  119. reply.set_status(EventReply_Status_FAILED);
  120. }
  121. return reply;
  122. }
  123. EventReply GrpcClient::SendWatchpointHits(const std::list<WatchpointHit> &watchpoints) {
  124. EventReply reply;
  125. grpc::ClientContext context;
  126. std::unique_ptr<grpc::ClientWriter<WatchpointHit> > writer(stub_->SendWatchpointHits(&context, &reply));
  127. for (const auto &watchpoint : watchpoints) {
  128. if (!writer->Write(watchpoint)) {
  129. break;
  130. }
  131. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  132. }
  133. writer->WritesDone();
  134. grpc::Status status = writer->Finish();
  135. if (!status.ok()) {
  136. MS_LOG(ERROR) << "RPC failed: SendWatchpointHits";
  137. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  138. reply.set_status(EventReply_Status_FAILED);
  139. }
  140. return reply;
  141. }
  142. } // namespace mindspore