• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/core/profiler/rpc/profiler_server.h"
17 
18 #include <memory>
19 #include <string>
20 
21 #include "grpcpp/grpcpp.h"
22 #include "absl/strings/str_cat.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/profiler/profiler_service.grpc.pb.h"
26 #include "tensorflow/core/profiler/rpc/profiler_service_impl.h"
27 
28 namespace tensorflow {
29 namespace profiler {
30 
StartProfilerServer(int32 port)31 void ProfilerServer::StartProfilerServer(int32 port) {
32   VLOG(1) << "Starting profiler server.";
33   std::string server_address = absl::StrCat("[::]:", port);
34   service_ = CreateProfilerService();
35   ::grpc::ServerBuilder builder;
36 
37   int selected_port = 0;
38   builder.AddListeningPort(server_address, ::grpc::InsecureServerCredentials(),
39                            &selected_port);
40   builder.RegisterService(service_.get());
41   server_ = builder.BuildAndStart();
42   if (!selected_port) {
43     LOG(ERROR) << "Unable to bind to " << server_address
44                << " selected port:" << selected_port;
45   } else {
46     LOG(INFO) << "Profiler server listening on " << server_address
47               << " selected port:" << selected_port;
48   }
49 }
50 
~ProfilerServer()51 ProfilerServer::~ProfilerServer() {
52   if (server_) {
53     server_->Shutdown();
54     server_->Wait();
55     LOG(INFO) << "Profiler server was shut down";
56   }
57 }
58 
59 }  // namespace profiler
60 }  // namespace tensorflow
61