• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_UTIL_ANNOTATED_CALLSITES_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_ANNOTATED_CALLSITES_H_
19 
20 #include <optional>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "src/trace_processor/containers/null_term_string_view.h"
25 #include "src/trace_processor/containers/string_pool.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 #include "src/trace_processor/tables/profiler_tables_py.h"
28 
29 namespace perfetto::trace_processor {
30 
31 class TraceProcessorContext;
32 
33 enum class CallsiteAnnotation {
34   kNone,
35   kCommonFrame,
36   kCommonFrameInterp,
37   kArtInterpreted,
38   kArtJit,
39   kArtAot,
40 };
41 
42 // Helper class to augment callsite with (currently Android-specific)
43 // annotations. A given callsite will always have the same annotation. This
44 // class will internally cache already computed annotations. An annotation
45 // depends only of the current callsite and the annotations of its parent
46 // callsites (going to the root).
47 class AnnotatedCallsites {
48  public:
49   explicit AnnotatedCallsites(const TraceProcessorContext* context);
50 
GetAnnotation(const tables::StackProfileCallsiteTable::ConstRowReference & callsite)51   CallsiteAnnotation GetAnnotation(
52       const tables::StackProfileCallsiteTable::ConstRowReference& callsite) {
53     return Get(callsite).second;
54   }
55 
56  private:
57   enum class MapType {
58     kArtInterp,
59     kArtJit,
60     kArtAot,
61     kNativeLibart,
62     kNativeOther,
63     kOther
64   };
65 
66   // Annotation FSM states:
67   // * kInitial: default, native-only callstacks never leave this state.
68   // * kEraseLibart: we've seen a managed frame, and will now "erase" (i.e. tag
69   //                 as a common-frame) frames belonging to the ART runtime.
70   // * kKeepNext: we've seen a special JNI trampoline for managed->native
71   //              transition, keep the immediate child (even if it is in ART),
72   //              and then go back to kEraseLibart.
73   // Regardless of the state, managed frames get annotated with their execution
74   // mode, based on the mapping.
75   enum class State { kInitial, kEraseLibart, kKeepNext };
76 
77   State GetState(std::optional<CallsiteId> id);
78 
79   std::pair<State, CallsiteAnnotation> Get(
80       const tables::StackProfileCallsiteTable::ConstRowReference& callsite);
81 
82   MapType GetMapType(MappingId id);
83   static MapType ClassifyMap(NullTermStringView map);
84 
85   const TraceProcessorContext& context_;
86   const std::optional<StringPool::Id> art_jni_trampoline_;
87 
88   std::unordered_map<MappingId, MapType> map_types_;
89   std::unordered_map<CallsiteId, State> states_;
90 };
91 
92 }  // namespace perfetto::trace_processor
93 
94 #endif  // SRC_TRACE_PROCESSOR_UTIL_ANNOTATED_CALLSITES_H_
95