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_SLICE_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_SLICE_TRACKER_H_
19 
20 #include <stdint.h>
21 
22 #include "src/trace_processor/trace_storage.h"
23 
24 namespace perfetto {
25 namespace trace_processor {
26 
27 class TraceProcessorContext;
28 
29 class SliceTracker {
30  public:
31   explicit SliceTracker(TraceProcessorContext*);
32   virtual ~SliceTracker();
33 
34   void BeginAndroid(int64_t timestamp,
35                     uint32_t ftrace_tid,
36                     uint32_t atrace_tgid,
37                     StringId cat,
38                     StringId name);
39 
40   // virtual for testing
41   virtual void Begin(int64_t timestamp,
42                      UniqueTid utid,
43                      StringId cat,
44                      StringId name);
45 
46   // virtual for testing
47   virtual void Scoped(int64_t timestamp,
48                       UniqueTid utid,
49                       StringId cat,
50                       StringId name,
51                       int64_t duration);
52 
53   void EndAndroid(int64_t timestamp, uint32_t ftrace_tid, uint32_t atrace_tgid);
54 
55   // virtual for testing
56   virtual void End(int64_t timestamp,
57                    UniqueTid utid,
58                    StringId opt_cat = {},
59                    StringId opt_name = {});
60 
61  private:
62   using SlicesStack = std::vector<size_t>;
63 
64   void StartSlice(int64_t timestamp,
65                   int64_t duration,
66                   UniqueTid utid,
67                   StringId cat,
68                   StringId name);
69   void CompleteSlice(UniqueTid tid);
70 
71   void MaybeCloseStack(int64_t end_ts, SlicesStack*);
72   int64_t GetStackHash(const SlicesStack&);
73 
74   TraceProcessorContext* const context_;
75   std::unordered_map<UniqueTid, SlicesStack> threads_;
76   std::unordered_map<uint32_t, uint32_t> ftrace_to_atrace_tgid_;
77 };
78 
79 }  // namespace trace_processor
80 }  // namespace perfetto
81 
82 #endif  // SRC_TRACE_PROCESSOR_SLICE_TRACKER_H_
83