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 #include "tensorflow/core/profiler/utils/xplane_test_utils.h"
16
17 #include <string>
18 #include <utility>
19
20 #include "absl/container/flat_hash_map.h"
21 #include "absl/strings/string_view.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/core/profiler/protobuf/xplane.pb.h"
24 #include "tensorflow/core/profiler/utils/xplane_builder.h"
25 #include "tensorflow/core/profiler/utils/xplane_schema.h"
26 #include "tensorflow/core/profiler/utils/xplane_utils.h"
27
28 namespace tensorflow {
29 namespace profiler {
30 namespace {
31
32 class XStatValueVisitor {
33 public:
XStatValueVisitor(XEventBuilder * event,const XStatMetadata * stat_metadata)34 XStatValueVisitor(XEventBuilder* event, const XStatMetadata* stat_metadata)
35 : event_(event), stat_metadata_(stat_metadata) {}
36
37 template <typename T>
operator ()(const T & value)38 void operator()(const T& value) {
39 event_->AddStatValue(*stat_metadata_, value);
40 }
41
42 private:
43 XEventBuilder* event_;
44 const XStatMetadata* stat_metadata_;
45 };
46
47 } // namespace
48
GetOrCreateHostXPlane(XSpace * space)49 XPlane* GetOrCreateHostXPlane(XSpace* space) {
50 return FindOrAddMutablePlaneWithName(space, kHostThreadsPlaneName);
51 }
52
GetOrCreateTpuXPlane(XSpace * space,int32_t device_ordinal,absl::string_view device_type)53 XPlane* GetOrCreateTpuXPlane(XSpace* space, int32_t device_ordinal,
54 absl::string_view device_type) {
55 std::string name = TpuPlaneName(device_ordinal);
56 XPlane* xplane = FindOrAddMutablePlaneWithName(space, name);
57 XPlaneBuilder builder(xplane);
58 builder.AddStatValue(
59 *builder.GetOrCreateStatMetadata(GetStatTypeStr(kDeviceTypeString)),
60 device_type);
61 return xplane;
62 }
63
GetOrCreateGpuXPlane(XSpace * space,int32_t device_ordinal)64 XPlane* GetOrCreateGpuXPlane(XSpace* space, int32_t device_ordinal) {
65 std::string name = GpuPlaneName(device_ordinal);
66 return FindOrAddMutablePlaneWithName(space, name);
67 }
68
CreateXEvent(XPlaneBuilder * plane_builder,XLineBuilder * line_builder,absl::string_view event_name,int64_t offset_ps,int64_t duration_ps,std::initializer_list<std::pair<StatType,XStatValue>> stats)69 void CreateXEvent(
70 XPlaneBuilder* plane_builder, XLineBuilder* line_builder,
71 absl::string_view event_name, int64_t offset_ps, int64_t duration_ps,
72 std::initializer_list<std::pair<StatType, XStatValue>> stats) {
73 auto event_builder = line_builder->AddEvent(
74 *plane_builder->GetOrCreateEventMetadata(event_name));
75 event_builder.SetOffsetPs(offset_ps);
76 event_builder.SetDurationPs(duration_ps);
77 for (const auto& stat_type_and_value : stats) {
78 StatType stat_type = stat_type_and_value.first;
79 const XStatValue& stat_value = stat_type_and_value.second;
80 XStatValueVisitor stat_value_visitor(
81 &event_builder,
82 plane_builder->GetOrCreateStatMetadata(GetStatTypeStr(stat_type)));
83 absl::visit(stat_value_visitor, stat_value);
84 }
85 }
86
CreateXEvent(XPlaneBuilder * plane_builder,XLineBuilder * line_builder,HostEventType event_type,int64_t offset_ps,int64_t duration_ps,std::initializer_list<std::pair<StatType,XStatValue>> stats)87 void CreateXEvent(
88 XPlaneBuilder* plane_builder, XLineBuilder* line_builder,
89 HostEventType event_type, int64_t offset_ps, int64_t duration_ps,
90 std::initializer_list<std::pair<StatType, XStatValue>> stats) {
91 CreateXEvent(plane_builder, line_builder, GetHostEventTypeStr(event_type),
92 offset_ps, duration_ps, stats);
93 }
94
CreateTfFunctionCallEvent(XPlaneBuilder * plane_builder,XLineBuilder * line_builder,absl::string_view function_name,int64_t offset_ps,int64_t duration_ps,absl::string_view execution_mode,int64_t tracing_count)95 void CreateTfFunctionCallEvent(XPlaneBuilder* plane_builder,
96 XLineBuilder* line_builder,
97 absl::string_view function_name,
98 int64_t offset_ps, int64_t duration_ps,
99 absl::string_view execution_mode,
100 int64_t tracing_count) {
101 if (tracing_count >= 0) {
102 // Adds the tracing_count stats only if tracing_count is valid.
103 CreateXEvent(plane_builder, line_builder, function_name, offset_ps,
104 duration_ps,
105 {{StatType::kTfFunctionCall, execution_mode},
106 {StatType::kTfFunctionTracingCount, tracing_count}});
107 } else {
108 CreateXEvent(plane_builder, line_builder, function_name, offset_ps,
109 duration_ps, {{StatType::kTfFunctionCall, execution_mode}});
110 }
111 }
112
113 } // namespace profiler
114 } // namespace tensorflow
115