Home | History | Annotate | Download | only in debug
      1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_DEBUG_GRPC_TESTLIB_H_
     17 #define TENSORFLOW_DEBUG_GRPC_TESTLIB_H_
     18 
     19 #include <atomic>
     20 #include <unordered_set>
     21 
     22 #include "grpc++/grpc++.h"
     23 #include "tensorflow/core/debug/debug_io_utils.h"
     24 #include "tensorflow/core/debug/debug_service.grpc.pb.h"
     25 #include "tensorflow/core/framework/tensor.h"
     26 #include "tensorflow/core/platform/mutex.h"
     27 
     28 namespace tensorflow {
     29 
     30 namespace test {
     31 
     32 class TestEventListenerImpl final : public EventListener::Service {
     33  public:
     34   TestEventListenerImpl() : stop_requested_(false), stopped_(false) {}
     35 
     36   void RunServer(const int server_port);
     37   void StopServer();
     38 
     39   ::grpc::Status SendEvents(
     40       ::grpc::ServerContext* context,
     41       ::grpc::ServerReaderWriter< ::tensorflow::EventReply,
     42                                   ::tensorflow::Event>* stream);
     43 
     44   // Clear debug data (e.g., Tensors) received so far.
     45   void ClearReceivedDebugData();
     46 
     47   void RequestDebugOpStateChangeAtNextStream(
     48       const EventReply::DebugOpStateChange::State new_state,
     49       const DebugNodeKey& debug_node_key);
     50 
     51   std::vector<string> debug_metadata_strings;
     52   std::vector<string> encoded_graph_defs;
     53   std::vector<string> device_names;
     54   std::vector<string> node_names;
     55   std::vector<int32> output_slots;
     56   std::vector<string> debug_ops;
     57   std::vector<Tensor> debug_tensors;
     58 
     59  private:
     60   std::atomic_bool stop_requested_;
     61   std::atomic_bool stopped_;
     62 
     63   std::vector<DebugNodeKey> debug_node_keys_ GUARDED_BY(states_mu_);
     64   std::vector<EventReply::DebugOpStateChange::State> new_states_
     65       GUARDED_BY(states_mu_);
     66 
     67   std::unordered_set<DebugNodeKey> write_enabled_debug_node_keys_;
     68 
     69   mutex states_mu_;
     70 };
     71 
     72 // Poll a gRPC debug server by sending a small tensor repeatedly till success.
     73 //
     74 // Args:
     75 //   server_url: gRPC URL of the server to poll, e.g., "grpc://foo:3333".
     76 //   max_attempts: Maximum number of attempts.
     77 //
     78 // Returns:
     79 //   Whether the polling succeeded within max_attempts.
     80 bool PollTillFirstRequestSucceeds(const string& server_url,
     81                                   const size_t max_attempts);
     82 
     83 }  // namespace test
     84 
     85 }  // namespace tensorflow
     86 
     87 #endif  // TENSORFLOW_DEBUG_GRPC_TESTLIB_H_
     88