|
| 1 | +/* Copyright 2026 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 | +#include "tsl/profiler/lib/profiler_orchestrator.h" |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <memory> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "absl/status/status.h" |
| 22 | +#include "xla/tsl/platform/errors.h" |
| 23 | +#include "xla/tsl/platform/logging.h" |
| 24 | +#include "tsl/profiler/lib/profiler_session.h" |
| 25 | +#include "tsl/profiler/protobuf/profiler_options.pb.h" |
| 26 | + |
| 27 | +namespace tsl { |
| 28 | +namespace profiler { |
| 29 | + |
| 30 | +ProfilerSessionOrchestrator::ProfilerSessionOrchestrator( |
| 31 | + const tensorflow::ProfileOptions& options) |
| 32 | + : options_(options) {} |
| 33 | + |
| 34 | +ProfilerSessionOrchestrator::~ProfilerSessionOrchestrator() { |
| 35 | + Stop().IgnoreError(); |
| 36 | +} |
| 37 | + |
| 38 | +absl::Status ProfilerSessionOrchestrator::Start() { |
| 39 | + if (session_ != nullptr) { |
| 40 | + return absl::FailedPreconditionError("Session already started."); |
| 41 | + } |
| 42 | + session_ = tsl::ProfilerSession::Create(options_); |
| 43 | + if (session_ == nullptr) { |
| 44 | + return absl::InternalError("Failed to create ProfilerSession."); |
| 45 | + } |
| 46 | + return session_->Status(); |
| 47 | +} |
| 48 | + |
| 49 | +absl::Status ProfilerSessionOrchestrator::Stop() { |
| 50 | + if (session_ == nullptr) { |
| 51 | + return absl::OkStatus(); // Already stopped or not started. |
| 52 | + } |
| 53 | + session_.reset(); |
| 54 | + return absl::OkStatus(); |
| 55 | +} |
| 56 | + |
| 57 | +absl::StatusOr<int> ProfilerSessionOrchestrator::Consume() { |
| 58 | + if (session_ == nullptr) { |
| 59 | + return absl::FailedPreconditionError("Session not started."); |
| 60 | + } |
| 61 | + |
| 62 | + consume_buffers_.emplace_back(kDefaultConsumeSize); |
| 63 | + auto& buffer = consume_buffers_.back(); |
| 64 | + TF_RETURN_IF_ERROR(session_->Consume(buffer.data())); |
| 65 | + return consume_buffers_.size() - 1; |
| 66 | +} |
| 67 | + |
| 68 | +absl::Status ProfilerSessionOrchestrator::Serialize(int buffer_index) { |
| 69 | + if (session_ == nullptr) { |
| 70 | + return absl::FailedPreconditionError("Session not started."); |
| 71 | + } |
| 72 | + |
| 73 | + if (buffer_index < 0 || buffer_index >= consume_buffers_.size()) { |
| 74 | + return absl::InvalidArgumentError("Invalid buffer index."); |
| 75 | + } |
| 76 | + serialize_space_.Clear(); |
| 77 | + auto& buffer = consume_buffers_[buffer_index]; |
| 78 | + return session_->Serialize(buffer.data(), &serialize_space_); |
| 79 | +} |
| 80 | + |
| 81 | +void ProfilerSessionOrchestrator::ClearConsumeBuffers() { |
| 82 | + std::vector<std::vector<uint8_t>>().swap(consume_buffers_); |
| 83 | +} |
| 84 | + |
| 85 | +} // namespace profiler |
| 86 | +} // namespace tsl |
0 commit comments