• 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 INCLUDE_PERFETTO_EXT_TRACE_PROCESSOR_IMPORTERS_MEMORY_TRACKER_RAW_MEMORY_GRAPH_NODE_H_
18 #define INCLUDE_PERFETTO_EXT_TRACE_PROCESSOR_IMPORTERS_MEMORY_TRACKER_RAW_MEMORY_GRAPH_NODE_H_
19 
20 #include <stdint.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include "perfetto/base/export.h"
28 #include "perfetto/ext/trace_processor/importers/memory_tracker/memory_allocator_node_id.h"
29 #include "perfetto/ext/trace_processor/importers/memory_tracker/memory_graph_edge.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 
34 // Describes the level of detail of the memory graph.
35 enum class LevelOfDetail : uint32_t {
36   kFirst,
37 
38   // For background tracing mode. The node time is quick, and typically just the
39   // totals are expected. Suballocations need not be specified. Node name must
40   // contain only pre-defined strings and string arguments cannot be added.
41   kBackground = kFirst,
42 
43   // For the levels below, MemoryNodeProvider instances must guarantee that the
44   // total size reported in the root node is consistent. Only the granularity of
45   // the child MemoryAllocatorNode(s) differs with the levels.
46 
47   // Few entries, typically a fixed number, per node.
48   kLight,
49 
50   // Unrestricted amount of entries per node.
51   kDetailed,
52 
53   kLast = kDetailed
54 };
55 
56 // Data model for user-land memory nodes.
57 class PERFETTO_EXPORT RawMemoryGraphNode {
58  public:
59   enum Flags {
60     kDefault = 0,
61 
62     // A node marked weak will be discarded if there is no ownership edge exists
63     // from a non-weak node.
64     kWeak = 1 << 0,
65   };
66 
67   // In the UI table each MemoryAllocatorNode becomes
68   // a row and each Entry generates a column (if it doesn't already
69   // exist).
70   struct PERFETTO_EXPORT MemoryNodeEntry {
71     enum EntryType {
72       kUint64,
73       kString,
74     };
75 
76     MemoryNodeEntry(const std::string& name,
77                     const std::string& units,
78                     uint64_t value);
79     MemoryNodeEntry(const std::string& name,
80                     const std::string& units,
81                     const std::string& value);
82 
83     bool operator==(const MemoryNodeEntry& rhs) const;
84 
85     std::string name;
86     std::string units;
87 
88     EntryType entry_type;
89 
90     uint64_t value_uint64;
91     std::string value_string;
92   };
93 
94   RawMemoryGraphNode(const std::string& absolute_name,
95                      LevelOfDetail level,
96                      MemoryAllocatorNodeId id);
97 
98   RawMemoryGraphNode(
99       const std::string& absolute_name,
100       LevelOfDetail level,
101       MemoryAllocatorNodeId id,
102       std::vector<RawMemoryGraphNode::MemoryNodeEntry>&& entries);
103 
104   // Standard attribute |name|s for the AddScalar and AddString() methods.
105   static const char kNameSize[];         // To represent allocated space.
106   static const char kNameObjectCount[];  // To represent number of objects.
107 
108   // Standard attribute |unit|s for the AddScalar and AddString() methods.
109   static const char kUnitsBytes[];    // Unit name to represent bytes.
110   static const char kUnitsObjects[];  // Unit name to represent #objects.
111 
112   // Constants used only internally and by tests.
113   static const char kTypeScalar[];  // Type name for scalar attributes.
114   static const char kTypeString[];  // Type name for string attributes.
115 
116   // |id| is an optional global node identifier, unique across all processes
117   // within the scope of a global node.
118   // Subsequent MemoryAllocatorNode(s) with the same |absolute_name| are
119   // expected to have the same id.
id()120   MemoryAllocatorNodeId id() const { return id_; }
121 
122   // Absolute name, unique within the scope of an entire ProcessMemoryNode.
absolute_name()123   const std::string& absolute_name() const { return absolute_name_; }
124 
entries()125   const std::vector<MemoryNodeEntry>& entries() const { return entries_; }
126 
level_of_detail()127   LevelOfDetail level_of_detail() const { return level_of_detail_; }
128 
129   // Use enum Flags to set values.
set_flags(int flags)130   void set_flags(int flags) { flags_ |= flags; }
clear_flags(int flags)131   void clear_flags(int flags) { flags_ &= ~flags; }
flags()132   int flags() const { return flags_; }
133 
134  private:
135   std::string absolute_name_;
136   LevelOfDetail level_of_detail_;
137   std::vector<MemoryNodeEntry> entries_;
138   MemoryAllocatorNodeId id_;
139 
140   // A node marked weak will be discarded by TraceViewer.
141   int flags_;  // See enum Flags.
142 };
143 
144 }  // namespace trace_processor
145 }  // namespace perfetto
146 
147 #endif  // INCLUDE_PERFETTO_EXT_TRACE_PROCESSOR_IMPORTERS_MEMORY_TRACKER_RAW_MEMORY_GRAPH_NODE_H_
148