aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/debug/debug_grpc_testlib.h
blob: 93376613b608cfc75c7edf473a4edc12e81a377a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_CORE_DEBUG_DEBUG_GRPC_TESTLIB_H_
#define TENSORFLOW_CORE_DEBUG_DEBUG_GRPC_TESTLIB_H_

#include <atomic>
#include <unordered_set>

#include "grpcpp/grpcpp.h"
#include "tensorflow/core/debug/debug_io_utils.h"
#include "tensorflow/core/debug/debug_service.grpc.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/mutex.h"

namespace tensorflow {

namespace test {

class TestEventListenerImpl final : public EventListener::Service {
 public:
  TestEventListenerImpl() : stop_requested_(false), stopped_(false) {}

  void RunServer(const int server_port);
  void StopServer();

  ::grpc::Status SendEvents(
      ::grpc::ServerContext* context,
      ::grpc::ServerReaderWriter< ::tensorflow::EventReply,
                                  ::tensorflow::Event>* stream);

  // Clear debug data (e.g., Tensors) received so far.
  void ClearReceivedDebugData();

  void RequestDebugOpStateChangeAtNextStream(
      const EventReply::DebugOpStateChange::State new_state,
      const DebugNodeKey& debug_node_key);

  std::vector<string> debug_metadata_strings;
  std::vector<string> encoded_graph_defs;
  std::vector<string> device_names;
  std::vector<string> node_names;
  std::vector<int32> output_slots;
  std::vector<string> debug_ops;
  std::vector<Tensor> debug_tensors;

 private:
  std::atomic_bool stop_requested_;
  std::atomic_bool stopped_;

  std::vector<DebugNodeKey> debug_node_keys_ GUARDED_BY(states_mu_);
  std::vector<EventReply::DebugOpStateChange::State> new_states_
      GUARDED_BY(states_mu_);

  std::unordered_set<DebugNodeKey> write_enabled_debug_node_keys_;

  mutex states_mu_;
};

// Poll a gRPC debug server by sending a small tensor repeatedly till success.
//
// Args:
//   server_url: gRPC URL of the server to poll, e.g., "grpc://foo:3333".
//   max_attempts: Maximum number of attempts.
//
// Returns:
//   Whether the polling succeeded within max_attempts.
bool PollTillFirstRequestSucceeds(const string& server_url,
                                  const size_t max_attempts);

}  // namespace test

}  // namespace tensorflow

#endif  // TENSORFLOW_CORE_DEBUG_DEBUG_GRPC_TESTLIB_H_