1 /* Copyright 2020 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/client/remote_profiler_session_manager.h"
17
18 #include <cstddef>
19 #include <memory>
20
21 #include "absl/memory/memory.h"
22 #include "absl/strings/string_view.h"
23 #include "absl/time/clock.h"
24 #include "absl/time/time.h"
25 #include "tensorflow/core/platform/env_time.h"
26 #include "tensorflow/core/platform/errors.h"
27 #include "tensorflow/core/platform/logging.h"
28 #include "tensorflow/core/platform/types.h"
29 #include "tensorflow/core/profiler/rpc/client/profiler_client.h"
30 #include "tensorflow/core/profiler/utils/time_utils.h"
31
32 namespace tensorflow {
33 namespace profiler {
34
35 /*static*/ std::unique_ptr<RemoteProfilerSessionManager>
Create(const RemoteProfilerSessionManagerOptions & options,const ProfileRequest & request,tensorflow::Status & out_status,AddressResolver resolver)36 RemoteProfilerSessionManager::Create(
37 const RemoteProfilerSessionManagerOptions& options,
38 const ProfileRequest& request, tensorflow::Status& out_status,
39 AddressResolver resolver) {
40 VLOG(1) << "Creating a RemoteProfilerSessionManager.";
41 auto session_manager = absl::WrapUnique(
42 new RemoteProfilerSessionManager(options, request, resolver));
43 out_status = session_manager->Init();
44 if (!out_status.ok()) {
45 return nullptr;
46 }
47 return session_manager;
48 }
49
RemoteProfilerSessionManager(RemoteProfilerSessionManagerOptions options,ProfileRequest request,AddressResolver resolver)50 RemoteProfilerSessionManager::RemoteProfilerSessionManager(
51 RemoteProfilerSessionManagerOptions options, ProfileRequest request,
52 AddressResolver resolver)
53 : options_(options), request_(request) {
54 if (resolver) {
55 resolver_ = resolver;
56 } else {
57 resolver_ = [](absl::string_view addr) { return std::string(addr); };
58 }
59 }
60
~RemoteProfilerSessionManager()61 RemoteProfilerSessionManager::~RemoteProfilerSessionManager() {
62 VLOG(2) << "Destroying RemoteProfilerSessionManager.";
63 }
64
Init()65 Status RemoteProfilerSessionManager::Init() {
66 mutex_lock lock(mutex_);
67 VLOG(1) << "SessionManager initializing.";
68
69 const absl::Time session_created_ts =
70 absl::FromUnixNanos(options_.session_creation_timestamp_ns());
71 const absl::Time deadline =
72 session_created_ts +
73 absl::Milliseconds(options_.max_session_duration_ms());
74
75 LOG(INFO) << "Deadline set to " << deadline
76 << " because max_session_duration_ms was "
77 << options_.max_session_duration_ms()
78 << " and session_creation_timestamp_ns was "
79 << options_.session_creation_timestamp_ns() << " ["
80 << session_created_ts << "]";
81
82 // Prepare a list of clients.
83 clients_.reserve(options_.service_addresses_size());
84
85 ProfileRequest request = request_;
86 for (auto& service_address : options_.service_addresses()) {
87 std::string resolved_service_address = resolver_(service_address);
88 request.set_host_name(resolved_service_address);
89
90 // Creation also issues Profile RPC asynchronously.
91 auto client = RemoteProfilerSession::Create(resolved_service_address,
92 deadline, request);
93 clients_.push_back(std::move(client));
94 }
95
96 LOG(INFO) << "Issued Profile gRPC to " << clients_.size() << " clients";
97 return Status::OK();
98 }
99
100 std::vector<RemoteProfilerSessionManager::Response>
WaitForCompletion()101 RemoteProfilerSessionManager::WaitForCompletion() {
102 mutex_lock lock(mutex_);
103 std::vector<RemoteProfilerSessionManager::Response> remote_responses(
104 clients_.size());
105
106 for (int32 idx = 0; idx < clients_.size(); ++idx) {
107 auto& remote_response = remote_responses[idx];
108 auto* client = clients_[idx].get();
109 remote_response.profile_response =
110 client->WaitForCompletion(remote_response.status);
111 remote_response.service_address = std::string(client->GetServiceAddress());
112 }
113 return remote_responses;
114 }
115
116 } // namespace profiler
117 } // namespace tensorflow
118