• 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   static void WriteFixedInterningsPacket(TraceWriter* trace_writer,
38                                          uint32_t sequence_flags);
39   void WriteMap(const Interned<Mapping> map, protos::pbzero::InternedData* out);
40   void WriteFrame(Interned<Frame> frame, protos::pbzero::InternedData* out);
41   void WriteBuildIDString(const Interned<std::string>& str,
42                           protos::pbzero::InternedData* out);
43   void WriteMappingPathString(const Interned<std::string>& str,
44                               protos::pbzero::InternedData* out);
45   void WriteFunctionNameString(const Interned<std::string>& str,
46                                protos::pbzero::InternedData* out);
47 
48   // Writes out the callstack represented by the given node.
49   void WriteCallstack(GlobalCallstackTrie::Node* node,
50                       GlobalCallstackTrie* trie,
51                       protos::pbzero::InternedData* out);
52 
IsCallstackNew(uint64_t callstack_id)53   bool IsCallstackNew(uint64_t callstack_id) {
54     return dumped_callstacks_.find(callstack_id) == dumped_callstacks_.end();
55   }
56 
57   void ClearHistory();
58 
59   // TODO(rsavitski): move elsewhere, used in heapprofd for orthogonal
60   // reasons. Shouldn't be cleared together with the rest of the incremental
61   // state.
HeapprofdNextIndexMutable()62   uint64_t* HeapprofdNextIndexMutable() { return &next_index_; }
63 
64  private:
65   // Map value is a bitfield distinguishing the distinct string fields
66   // the string can be emitted as, e.g. kDumpedBuildID.
67   std::map<InternID, int> dumped_strings_;
68   std::set<InternID> dumped_frames_;
69   std::set<InternID> dumped_mappings_;
70   std::set<uint64_t> dumped_callstacks_;  // uses callstack trie's node ids
71 
72   uint64_t next_index_ = 0;
73 };
74 
75 }  // namespace profiling
76 }  // namespace perfetto
77 
78 #endif  // SRC_PROFILING_COMMON_INTERNING_OUTPUT_H_
79