Remove IsNested and add XEventBuilder::GetTimespan

PiperOrigin-RevId: 345108962
Change-Id: Iba9fa6dbaf7a0fc281b80beff86ef3acc54e0b2b
diff --git a/tensorflow/core/profiler/convert/xplane_to_op_metrics_db.cc b/tensorflow/core/profiler/convert/xplane_to_op_metrics_db.cc
index b33cd91..cb90942 100644
--- a/tensorflow/core/profiler/convert/xplane_to_op_metrics_db.cc
+++ b/tensorflow/core/profiler/convert/xplane_to_op_metrics_db.cc
@@ -152,7 +152,7 @@
               event.GetStat(StatType::kIsEager)) {
         is_eager = stat->IntValue();
       }
-      Timespan span(event.TimestampPs(), event.DurationPs());
+      Timespan span = event.GetTimespan();
       tf_activities->push_back(
           {span.begin_ps(), tf_op_id, kTfOpBegin, *tf_op, is_eager});
       tf_activities->push_back(
diff --git a/tensorflow/core/profiler/convert/xplane_to_step_events.cc b/tensorflow/core/profiler/convert/xplane_to_step_events.cc
index 0af9eca..ec446e3 100644
--- a/tensorflow/core/profiler/convert/xplane_to_step_events.cc
+++ b/tensorflow/core/profiler/convert/xplane_to_step_events.cc
@@ -98,20 +98,20 @@
     if (use_device_step_events &&
         device_step_events.find(group_id) == device_step_events.end())
       return;
-    Timespan timespan = Timespan(event.TimestampPs(), event.DurationPs());
     if (IsExplicitHostStepMarker(event.Name())) {
-      result[group_id].AddMarker(StepMarker(
-          StepMarkerType::kExplicitHostStepMarker, event.Name(), timespan));
+      result[group_id].AddMarker(
+          StepMarker(StepMarkerType::kExplicitHostStepMarker, event.Name(),
+                     event.GetTimespan()));
     } else if (!step_name.empty()) {
       // Grouping adds a step_name stat to implicit host step markers.
-      result[group_id].AddMarker(StepMarker(
-          StepMarkerType::kImplicitHostStepMarker, event.Name(), timespan));
+      result[group_id].AddMarker(
+          StepMarker(StepMarkerType::kImplicitHostStepMarker, event.Name(),
+                     event.GetTimespan()));
     } else if (IsRealCpuCompute(event.Name())) {
-      EventTypeSpan event_type_span(
-          ClassifyCpuEvent(event.Name(), correlation_id,
-                           use_device_step_events),
-          timespan);
-      result[group_id].AddEvent(event_type_span);
+      result[group_id].AddEvent(
+          EventTypeSpan(ClassifyCpuEvent(event.Name(), correlation_id,
+                                         use_device_step_events),
+                        event.GetTimespan()));
     }
   });
   return result;
@@ -136,7 +136,7 @@
     if (absl::optional<XStatVisitor> stat = event.GetStat(StatType::kGroupId)) {
       result[stat->IntValue()].AddMarker(
           StepMarker(StepMarkerType::kDeviceStepMarker, event.Name(),
-                     Timespan(event.TimestampPs(), event.DurationPs())));
+                     event.GetTimespan()));
     }
   });
   return result;
diff --git a/tensorflow/core/profiler/convert/xplane_to_tf_data_stats.cc b/tensorflow/core/profiler/convert/xplane_to_tf_data_stats.cc
index 0b278fc..91f48bf 100644
--- a/tensorflow/core/profiler/convert/xplane_to_tf_data_stats.cc
+++ b/tensorflow/core/profiler/convert/xplane_to_tf_data_stats.cc
@@ -168,7 +168,7 @@
   iterator_stat.set_duration_ps(iterator_stat.duration_ps() +
                                 visitor.DurationPs());
   int64 self_time_ps = visitor.DurationPs();
-  tensorflow::profiler::Timespan self_time_span = visitor.GetTimespan();
+  Timespan self_time_span = visitor.GetTimespan();
   for (EventNode* child : iterator_event.GetChildren()) {
     const XEventVisitor& child_visitor = child->GetEventVisitor();
     if (ParseTfOpFullname(child_visitor.Name()).category == Category::kTfData) {
diff --git a/tensorflow/core/profiler/utils/BUILD b/tensorflow/core/profiler/utils/BUILD
index 38f209a..0bd148c 100644
--- a/tensorflow/core/profiler/utils/BUILD
+++ b/tensorflow/core/profiler/utils/BUILD
@@ -209,6 +209,7 @@
     visibility = [":friends"],
     deps = [
         ":time_utils",
+        ":timespan",
         "//tensorflow/core:lib",
         "//tensorflow/core/profiler/protobuf:xplane_proto_cc",
         "@com_google_absl//absl/container:flat_hash_map",
diff --git a/tensorflow/core/profiler/utils/xplane_builder.h b/tensorflow/core/profiler/utils/xplane_builder.h
index 505340e..4760100 100644
--- a/tensorflow/core/profiler/utils/xplane_builder.h
+++ b/tensorflow/core/profiler/utils/xplane_builder.h
@@ -28,6 +28,7 @@
 #include "tensorflow/core/platform/types.h"
 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
 #include "tensorflow/core/profiler/utils/time_utils.h"
+#include "tensorflow/core/profiler/utils/timespan.h"
 
 namespace tensorflow {
 namespace profiler {
@@ -199,6 +200,11 @@
                   event_->offset_ps());
   }
 
+  Timespan GetTimespan() const {
+    return Timespan(NanosToPicos(line_->timestamp_ns()) + event_->offset_ps(),
+                    event_->duration_ps());
+  }
+
  private:
   const XLine* line_;
   XEvent* event_;
diff --git a/tensorflow/core/profiler/utils/xplane_utils.cc b/tensorflow/core/profiler/utils/xplane_utils.cc
index b992a9c..db67e76 100644
--- a/tensorflow/core/profiler/utils/xplane_utils.cc
+++ b/tensorflow/core/profiler/utils/xplane_utils.cc
@@ -113,10 +113,6 @@
   return result;
 }
 
-bool IsNested(const XEvent& event, const XEvent& parent) {
-  return XEventTimespan(parent).Includes(XEventTimespan(event));
-}
-
 XStat* FindOrAddMutableStat(const XStatMetadata& stat_metadata, XEvent* event) {
   for (auto& stat : *event->mutable_stats()) {
     if (stat.metadata_id() == stat_metadata.id()) {
diff --git a/tensorflow/core/profiler/utils/xplane_utils.h b/tensorflow/core/profiler/utils/xplane_utils.h
index 286469c..396625b 100644
--- a/tensorflow/core/profiler/utils/xplane_utils.h
+++ b/tensorflow/core/profiler/utils/xplane_utils.h
@@ -40,10 +40,6 @@
 std::vector<XPlane*> FindMutablePlanesWithPrefix(XSpace* space,
                                                  absl::string_view prefix);
 
-// Returns true if event is nested by parent.
-bool IsNested(const tensorflow::profiler::XEvent& event,
-              const tensorflow::profiler::XEvent& parent);
-
 XStat* FindOrAddMutableStat(const XStatMetadata& stat_metadata, XEvent* event);
 
 void RemovePlane(XSpace* space, const XPlane* plane);
diff --git a/tensorflow/core/profiler/utils/xplane_utils_test.cc b/tensorflow/core/profiler/utils/xplane_utils_test.cc
index 359c0de..3f72a89 100644
--- a/tensorflow/core/profiler/utils/xplane_utils_test.cc
+++ b/tensorflow/core/profiler/utils/xplane_utils_test.cc
@@ -38,19 +38,6 @@
   return event;
 }
 
-// Tests IsNested.
-TEST(XPlaneUtilsTest, IsNestedTest) {
-  XEvent event = CreateEvent(100, 100);
-  XEvent parent = CreateEvent(50, 200);
-  EXPECT_TRUE(IsNested(event, parent));
-  // Returns false if there is no overlap.
-  XEvent not_parent = CreateEvent(30, 50);
-  EXPECT_FALSE(IsNested(event, not_parent));
-  // Returns false if they overlap only partially.
-  not_parent = CreateEvent(50, 100);
-  EXPECT_FALSE(IsNested(event, not_parent));
-}
-
 TEST(XPlaneUtilsTest, AddAndRemovePlanes) {
   XSpace space;