1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SLICE_TRACKER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SLICE_TRACKER_H_ 19 20 #include <stdint.h> 21 22 #include "src/trace_processor/importers/common/args_tracker.h" 23 #include "src/trace_processor/storage/trace_storage.h" 24 25 namespace perfetto { 26 namespace trace_processor { 27 28 class ArgsTracker; 29 class TraceProcessorContext; 30 31 class SliceTracker { 32 public: 33 using SetArgsCallback = std::function<void(ArgsTracker::BoundInserter*)>; 34 35 explicit SliceTracker(TraceProcessorContext*); 36 virtual ~SliceTracker(); 37 38 // virtual for testing 39 virtual base::Optional<uint32_t> Begin( 40 int64_t timestamp, 41 TrackId track_id, 42 StringId category, 43 StringId name, 44 SetArgsCallback args_callback = SetArgsCallback()); 45 46 void BeginGpu(tables::GpuSliceTable::Row row, 47 SetArgsCallback args_callback = SetArgsCallback()); 48 49 void BeginFrameEvent(tables::GraphicsFrameSliceTable::Row row, 50 SetArgsCallback args_callback = SetArgsCallback()); 51 52 // virtual for testing 53 virtual base::Optional<uint32_t> Scoped( 54 int64_t timestamp, 55 TrackId track_id, 56 StringId category, 57 StringId name, 58 int64_t duration, 59 SetArgsCallback args_callback = SetArgsCallback()); 60 61 void ScopedGpu(const tables::GpuSliceTable::Row& row, 62 SetArgsCallback args_callback = SetArgsCallback()); 63 64 SliceId ScopedFrameEvent(const tables::GraphicsFrameSliceTable::Row& row, 65 SetArgsCallback args_callback = SetArgsCallback()); 66 67 // virtual for testing 68 virtual base::Optional<uint32_t> End( 69 int64_t timestamp, 70 TrackId track_id, 71 StringId opt_category = {}, 72 StringId opt_name = {}, 73 SetArgsCallback args_callback = SetArgsCallback()); 74 75 // Usually args should be added in the Begin or End args_callback but this 76 // method is for the situation where new args need to be added to an 77 // in-progress slice. 78 base::Optional<uint32_t> AddArgs(TrackId track_id, 79 StringId category, 80 StringId name, 81 SetArgsCallback args_callback); 82 83 // TODO(lalitm): eventually this method should become End and End should 84 // be renamed EndChrome. 85 base::Optional<SliceId> EndGpu( 86 int64_t ts, 87 TrackId track_id, 88 SetArgsCallback args_callback = SetArgsCallback()); 89 90 base::Optional<SliceId> EndFrameEvent( 91 int64_t ts, 92 TrackId track_id, 93 SetArgsCallback args_callback = SetArgsCallback()); 94 95 void FlushPendingSlices(); 96 97 private: 98 using SlicesStack = std::vector<std::pair<uint32_t /* row */, ArgsTracker>>; 99 using StackMap = std::unordered_map<TrackId, SlicesStack>; 100 101 base::Optional<uint32_t> StartSlice(int64_t timestamp, 102 TrackId track_id, 103 SetArgsCallback args_callback, 104 std::function<SliceId()> inserter); 105 106 base::Optional<SliceId> CompleteSlice( 107 int64_t timestamp, 108 TrackId track_id, 109 SetArgsCallback args_callback, 110 std::function<base::Optional<uint32_t>(const SlicesStack&)> finder); 111 112 void MaybeCloseStack(int64_t end_ts, SlicesStack*); 113 114 base::Optional<uint32_t> MatchingIncompleteSliceIndex( 115 const SlicesStack& stack, 116 StringId name, 117 StringId category); 118 int64_t GetStackHash(const SlicesStack&); 119 120 // Timestamp of the previous event. Used to discard events arriving out 121 // of order. 122 int64_t prev_timestamp_ = 0; 123 124 TraceProcessorContext* const context_; 125 StackMap stacks_; 126 }; 127 128 } // namespace trace_processor 129 } // namespace perfetto 130 131 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SLICE_TRACKER_H_ 132