/* Copyright 2024 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef XPROF_UTILS_HOST_OFFLOAD_UTILS_H_
#define XPROF_UTILS_HOST_OFFLOAD_UTILS_H_

#include <cstdint>
#include <optional>
#include <queue>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "xla/layout.h"
#include "xla/tsl/profiler/utils/xplane_builder.h"
#include "xla/tsl/profiler/utils/xplane_visitor.h"
#include "tsl/profiler/protobuf/xplane.pb.h"

namespace tensorflow {
namespace profiler {

using ::tsl::profiler::XEventVisitor;
using ::tsl::profiler::XLineBuilder;
using ::tsl::profiler::XPlaneBuilder;

struct LineBuilderAndEventEndTimeFrontier {
  XLineBuilder line_builder;
  uint64_t event_end_time_frontier_ns;
};

class HostOffloadEventProcessor {
 public:
  HostOffloadEventProcessor(XPlaneBuilder* plane_builder,
                            uint64_t start_timestamp_ns)
      : plane_builder_(plane_builder),
        start_timestamp_ns_(start_timestamp_ns) {}
  ~HostOffloadEventProcessor() = default;

  void ProcessHostOffloadOpEvent(const XEventVisitor& event,
                                 std::optional<int64_t> group_id);

  bool IsHostOffloadOpName(const XEventVisitor& event) const;

 private:
  std::string GetOffloadInstructionID(absl::string_view op_name) const;
  std::string GetOffloadInstructionName(absl::string_view op_name) const;

  absl::flat_hash_map<std::string, std::queue<const XEventVisitor*>>
      seen_events_;
  std::string host_memory_label_ =
      absl::StrCat("S(", xla::Layout::kHostMemorySpace, ")");

  XPlaneBuilder* plane_builder_;
  uint64_t start_timestamp_ns_;

  std::vector<LineBuilderAndEventEndTimeFrontier>
      host_offload_op_line_builders_;
};

}  // namespace profiler
}  // namespace tensorflow

#endif  // XPROF_UTILS_HOST_OFFLOAD_UTILS_H_
