• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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_PROFILING_COMMON_INTERNING_OUTPUT_H_
18 #define SRC_PROFILING_COMMON_INTERNING_OUTPUT_H_
19 
20 #include <map>
21 #include <set>
22 
23 #include <stdint.h>
24 
25 #include "perfetto/ext/tracing/core/trace_writer.h"
26 #include "src/profiling/common/callstack_trie.h"
27 #include "src/profiling/common/interner.h"
28 
29 #include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
30 
31 namespace perfetto {
32 namespace profiling {
33 
34 class InterningOutputTracker {
35  public:
36   // Writes out a full packet containing the "empty" (zero) internings.
37   // NB: resulting packet has |incremental_state_cleared| set.
38   static void WriteFixedInterningsPacket(TraceWriter* trace_writer);
39 
40   void WriteMap(const Interned<Mapping> map, protos::pbzero::InternedData* out);
41   void WriteFrame(Interned<Frame> frame, protos::pbzero::InternedData* out);
42   void WriteBuildIDString(const Interned<std::string>& str,
43                           protos::pbzero::InternedData* out);
44   void WriteMappingPathString(const Interned<std::string>& str,
45                               protos::pbzero::InternedData* out);
46   void WriteFunctionNameString(const Interned<std::string>& str,
47                                protos::pbzero::InternedData* out);
48 
49   // Writes out the callstack represented by the given node.
50   void WriteCallstack(GlobalCallstackTrie::Node* node,
51                       GlobalCallstackTrie* trie,
52                       protos::pbzero::InternedData* out);
53 
IsCallstackNew(uint64_t callstack_id)54   bool IsCallstackNew(uint64_t callstack_id) {
55     return dumped_callstacks_.find(callstack_id) == dumped_callstacks_.end();
56   }
57 
58   void ClearHistory();
59 
60   // TODO(rsavitski): move elsewhere, used in heapprofd for orthogonal
61   // reasons. Shouldn't be cleared together with the rest of the incremental
62   // state.
HeapprofdNextIndexMutable()63   uint64_t* HeapprofdNextIndexMutable() { return &next_index_; }
64 
65  private:
66   // Map value is a bitfield distinguishing the distinct string fields
67   // the string can be emitted as, e.g. kDumpedBuildID.
68   std::map<InternID, int> dumped_strings_;
69   std::set<InternID> dumped_frames_;
70   std::set<InternID> dumped_mappings_;
71   std::set<uint64_t> dumped_callstacks_;  // uses callstack trie's node ids
72 
73   uint64_t next_index_ = 0;
74 };
75 
76 }  // namespace profiling
77 }  // namespace perfetto
78 
79 #endif  // SRC_PROFILING_COMMON_INTERNING_OUTPUT_H_
80