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/convert/post_process_single_host_xplane.h"
16 
17 #include "tensorflow/core/profiler/utils/derived_timeline.h"
18 #include "tensorflow/core/profiler/utils/group_events.h"
19 #include "tensorflow/core/profiler/utils/xplane_schema.h"
20 #include "tensorflow/core/profiler/utils/xplane_utils.h"
21 
22 namespace tensorflow {
23 namespace profiler {
24 namespace {
25 
26 // Merges XPlanes generated by TraceMe, CUPTI API trace and Python tracer.
MergeHostPlanesAndSortLines(XSpace * space)27 void MergeHostPlanesAndSortLines(XSpace* space) {
28   XPlane* host_plane =
29       FindOrAddMutablePlaneWithName(space, kHostThreadsPlaneName);
30   std::vector<const XPlane*> additional_host_planes = FindPlanesWithNames(
31       *space, {kCuptiDriverApiPlaneName, kPythonTracerPlaneName});
32   if (!additional_host_planes.empty()) {
33     MergePlanes(additional_host_planes, host_plane);
34     RemovePlanes(space, additional_host_planes);
35   }
36   SortXLinesBy(host_plane, XLinesComparatorByName());
37 }
38 
39 }  // namespace
40 
PostProcessSingleHostXSpace(XSpace * space,uint64 start_time_ns)41 void PostProcessSingleHostXSpace(XSpace* space, uint64 start_time_ns) {
42   VLOG(3) << "Post processing local profiler XSpace.";
43   // Post processing the collected XSpace without hold profiler lock.
44   // 1. Merge all host planes and sorts lines by name.
45   MergeHostPlanesAndSortLines(space);
46   // 2. Normalize all timestamps by shifting timeline to profiling start time.
47   // NOTE: this have to be done before sorting XSpace due to timestamp overflow.
48   NormalizeTimestamps(space, start_time_ns);
49   // 3. Sort each plane of the XSpace
50   SortXSpace(space);
51   // 4. Grouping (i.e. marking step number) events in the XSpace.
52   EventForest event_forest;
53   GroupTfEvents(space, &event_forest);
54   // 5. Generated miscellaneous derived time lines for device planes.
55   GenerateDerivedTimeLines(event_forest.GetGroupMetadataMap(), space);
56 }
57 
58 }  // namespace profiler
59 }  // namespace tensorflow
60