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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <thread>
  17. #include "debug/debugger/grpc_client.h"
  18. #include "utils/log_adapter.h"
  19. using debugger::EventListener;
  20. using debugger::EventReply;
  21. using debugger::EventReply_Status_FAILED;
  22. using debugger::GraphProto;
  23. using debugger::Metadata;
  24. using debugger::TensorProto;
  25. using debugger::WatchpointHit;
  26. namespace mindspore {
  27. GrpcClient::GrpcClient(const std::string &host, const std::string &port) : stub_(nullptr) { Init(host, port); }
  28. void GrpcClient::Init(const std::string &host, const std::string &port) {
  29. std::string target_str = host + ":" + port;
  30. MS_LOG(INFO) << "GrpcClient connecting to: " << target_str;
  31. std::shared_ptr<grpc::Channel> channel = grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
  32. stub_ = EventListener::NewStub(channel);
  33. }
  34. void GrpcClient::Reset() { stub_ = nullptr; }
  35. EventReply GrpcClient::WaitForCommand(const Metadata &metadata) {
  36. EventReply reply;
  37. grpc::ClientContext context;
  38. grpc::Status status = stub_->WaitCMD(&context, metadata, &reply);
  39. if (!status.ok()) {
  40. MS_LOG(ERROR) << "RPC failed: WaitForCommand";
  41. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  42. reply.set_status(EventReply_Status_FAILED);
  43. }
  44. return reply;
  45. }
  46. EventReply GrpcClient::SendMetadata(const Metadata &metadata) {
  47. EventReply reply;
  48. grpc::ClientContext context;
  49. grpc::Status status = stub_->SendMetadata(&context, metadata, &reply);
  50. if (!status.ok()) {
  51. MS_LOG(ERROR) << "RPC failed: SendMetadata";
  52. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  53. reply.set_status(EventReply_Status_FAILED);
  54. }
  55. return reply;
  56. }
  57. EventReply GrpcClient::SendGraph(const GraphProto &graph) {
  58. EventReply reply;
  59. grpc::ClientContext context;
  60. grpc::Status status = stub_->SendGraph(&context, graph, &reply);
  61. if (!status.ok()) {
  62. MS_LOG(ERROR) << "RPC failed: SendGraph";
  63. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  64. reply.set_status(EventReply_Status_FAILED);
  65. }
  66. return reply;
  67. }
  68. EventReply GrpcClient::SendTensors(const std::list<TensorProto> &tensors) {
  69. EventReply reply;
  70. grpc::ClientContext context;
  71. std::unique_ptr<grpc::ClientWriter<TensorProto> > writer(stub_->SendTensors(&context, &reply));
  72. for (const auto &tensor : tensors) {
  73. if (!writer->Write(tensor)) {
  74. break;
  75. }
  76. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  77. }
  78. writer->WritesDone();
  79. grpc::Status status = writer->Finish();
  80. if (!status.ok()) {
  81. MS_LOG(ERROR) << "RPC failed: SendTensors";
  82. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  83. reply.set_status(EventReply_Status_FAILED);
  84. }
  85. return reply;
  86. }
  87. EventReply GrpcClient::SendWatchpointHits(const std::list<WatchpointHit> &watchpoints) {
  88. EventReply reply;
  89. grpc::ClientContext context;
  90. std::unique_ptr<grpc::ClientWriter<WatchpointHit> > writer(stub_->SendWatchpointHits(&context, &reply));
  91. for (const auto &watchpoint : watchpoints) {
  92. if (!writer->Write(watchpoint)) {
  93. break;
  94. }
  95. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  96. }
  97. writer->WritesDone();
  98. grpc::Status status = writer->Finish();
  99. if (!status.ok()) {
  100. MS_LOG(ERROR) << "RPC failed: SendWatchpointHits";
  101. MS_LOG(ERROR) << status.error_code() << ": " << status.error_message();
  102. reply.set_status(EventReply_Status_FAILED);
  103. }
  104. return reply;
  105. }
  106. } // namespace mindspore