• 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 #include <memory>
18 #include <utility>
19 #include "grpcpp/grpcpp.h"
20 #include "tensorflow/core/platform/grpc_services.h"
21 #include "tensorflow/core/profiler/rpc/profiler_service_impl.h"
22 #include "tensorflow/core/util/ptr_util.h"
23 
24 namespace tensorflow {
25 
StartProfilerServer(ProfilerContext * const profiler_context,int32 port)26 std::unique_ptr<Thread> StartProfilerServer(
27     ProfilerContext* const profiler_context, int32 port) {
28   Env* env = profiler_context->eager_context != nullptr
29                  ? profiler_context->eager_context->TFEnv()
30                  : Env::Default();
31   // Starting the server in the child thread may be delay and user may already
32   // delete the profiler context at that point. So we need to make a copy.
33   ProfilerContext ctx = *profiler_context;
34   return WrapUnique(env->StartThread({}, "profiler server", [ctx, port]() {
35     string server_address = strings::StrCat("0.0.0.0:", port);
36     std::unique_ptr<grpc::ProfilerService::Service> service =
37         CreateProfilerService(ctx);
38     ::grpc::ServerBuilder builder;
39     builder.AddListeningPort(server_address,
40                              ::grpc::InsecureServerCredentials());
41     builder.RegisterService(service.get());
42     std::unique_ptr<::grpc::Server> server(builder.BuildAndStart());
43     LOG(INFO) << "Profiling Server listening on " << server_address;
44     server->Wait();
45   }));
46 }
47 
48 }  // namespace tensorflow
49