1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_MEMORY_INSTRUMENTATION_MOJOM_TRAITS_H_
6 #define SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_MEMORY_INSTRUMENTATION_MOJOM_TRAITS_H_
7
8 #include "base/component_export.h"
9 #include "base/process/process_handle.h"
10 #include "base/trace_event/memory_allocator_dump.h"
11 #include "base/trace_event/memory_dump_manager.h"
12 #include "base/trace_event/memory_dump_request_args.h"
13 #include "base/trace_event/process_memory_dump.h"
14 #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom-shared.h"
15
16 namespace mojo {
17
18 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)19 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
20 EnumTraits<memory_instrumentation::mojom::DumpType,
21 base::trace_event::MemoryDumpType> {
22 static memory_instrumentation::mojom::DumpType ToMojom(
23 base::trace_event::MemoryDumpType type);
24 static bool FromMojom(memory_instrumentation::mojom::DumpType input,
25 base::trace_event::MemoryDumpType* out);
26 };
27
28 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)29 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
30 EnumTraits<memory_instrumentation::mojom::LevelOfDetail,
31 base::trace_event::MemoryDumpLevelOfDetail> {
32 static memory_instrumentation::mojom::LevelOfDetail ToMojom(
33 base::trace_event::MemoryDumpLevelOfDetail level_of_detail);
34 static bool FromMojom(memory_instrumentation::mojom::LevelOfDetail input,
35 base::trace_event::MemoryDumpLevelOfDetail* out);
36 };
37
38 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)39 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
40 EnumTraits<memory_instrumentation::mojom::Determinism,
41 base::trace_event::MemoryDumpDeterminism> {
42 static memory_instrumentation::mojom::Determinism ToMojom(
43 base::trace_event::MemoryDumpDeterminism determinism);
44 static bool FromMojom(memory_instrumentation::mojom::Determinism input,
45 base::trace_event::MemoryDumpDeterminism* out);
46 };
47
48 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)49 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
50 StructTraits<memory_instrumentation::mojom::RequestArgsDataView,
51 base::trace_event::MemoryDumpRequestArgs> {
52 static uint64_t dump_guid(
53 const base::trace_event::MemoryDumpRequestArgs& args) {
54 return args.dump_guid;
55 }
56 static base::trace_event::MemoryDumpType dump_type(
57 const base::trace_event::MemoryDumpRequestArgs& args) {
58 return args.dump_type;
59 }
60 static base::trace_event::MemoryDumpLevelOfDetail level_of_detail(
61 const base::trace_event::MemoryDumpRequestArgs& args) {
62 return args.level_of_detail;
63 }
64 static base::trace_event::MemoryDumpDeterminism determinism(
65 const base::trace_event::MemoryDumpRequestArgs& args) {
66 return args.determinism;
67 }
68 static bool Read(memory_instrumentation::mojom::RequestArgsDataView input,
69 base::trace_event::MemoryDumpRequestArgs* out);
70 };
71
72 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)73 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM) StructTraits<
74 memory_instrumentation::mojom::RawAllocatorDumpEdgeDataView,
75 base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge> {
76 static uint64_t source_id(
77 const base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge&
78 edge) {
79 return edge.source.ToUint64();
80 }
81 static uint64_t target_id(
82 const base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge&
83 edge) {
84 return edge.target.ToUint64();
85 }
86 static int importance(
87 const base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge&
88 edge) {
89 return edge.importance;
90 }
91 static bool overridable(
92 const base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge&
93 edge) {
94 return edge.overridable;
95 }
96 static bool Read(
97 memory_instrumentation::mojom::RawAllocatorDumpEdgeDataView input,
98 base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge* out);
99 };
100
101 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)102 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM) UnionTraits<
103 memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView,
104 base::trace_event::MemoryAllocatorDump::Entry> {
105 static memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView::Tag
106 GetTag(const base::trace_event::MemoryAllocatorDump::Entry& args) {
107 switch (args.entry_type) {
108 case base::trace_event::MemoryAllocatorDump::Entry::EntryType::kUint64:
109 return memory_instrumentation::mojom::
110 RawAllocatorDumpEntryValueDataView::Tag::VALUE_UINT64;
111 case base::trace_event::MemoryAllocatorDump::Entry::EntryType::kString:
112 return memory_instrumentation::mojom::
113 RawAllocatorDumpEntryValueDataView::Tag::VALUE_STRING;
114 }
115 NOTREACHED();
116 return memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView::
117 Tag::VALUE_UINT64;
118 }
119
120 static uint64_t value_uint64(
121 const base::trace_event::MemoryAllocatorDump::Entry& args) {
122 return args.value_uint64;
123 }
124
125 static const std::string& value_string(
126 const base::trace_event::MemoryAllocatorDump::Entry& args) {
127 return args.value_string;
128 }
129
130 static bool Read(
131 memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView data,
132 base::trace_event::MemoryAllocatorDump::Entry* out);
133 };
134
135 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)136 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
137 StructTraits<memory_instrumentation::mojom::RawAllocatorDumpEntryDataView,
138 base::trace_event::MemoryAllocatorDump::Entry> {
139 static const std::string& name(
140 const base::trace_event::MemoryAllocatorDump::Entry& entry) {
141 return entry.name;
142 }
143 static const std::string& units(
144 const base::trace_event::MemoryAllocatorDump::Entry& entry) {
145 return entry.units;
146 }
147 static const base::trace_event::MemoryAllocatorDump::Entry& value(
148 const base::trace_event::MemoryAllocatorDump::Entry& entry) {
149 return entry;
150 }
151 static bool Read(
152 memory_instrumentation::mojom::RawAllocatorDumpEntryDataView input,
153 base::trace_event::MemoryAllocatorDump::Entry* out);
154 };
155
156 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)157 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
158 StructTraits<memory_instrumentation::mojom::RawAllocatorDumpDataView,
159 std::unique_ptr<base::trace_event::MemoryAllocatorDump>> {
160 static uint64_t id(
161 const std::unique_ptr<base::trace_event::MemoryAllocatorDump>& mad) {
162 return mad->guid().ToUint64();
163 }
164 static const std::string& absolute_name(
165 const std::unique_ptr<base::trace_event::MemoryAllocatorDump>& mad) {
166 return mad->absolute_name();
167 }
168 static bool weak(
169 const std::unique_ptr<base::trace_event::MemoryAllocatorDump>& mad) {
170 return mad->flags() & base::trace_event::MemoryAllocatorDump::WEAK;
171 }
172 static base::trace_event::MemoryDumpLevelOfDetail level_of_detail(
173 const std::unique_ptr<base::trace_event::MemoryAllocatorDump>& mad) {
174 return mad->level_of_detail();
175 }
176 static const std::vector<base::trace_event::MemoryAllocatorDump::Entry>&
177 entries(const std::unique_ptr<base::trace_event::MemoryAllocatorDump>& mad) {
178 return *mad->mutable_entries_for_serialization();
179 }
180 static bool Read(
181 memory_instrumentation::mojom::RawAllocatorDumpDataView input,
182 std::unique_ptr<base::trace_event::MemoryAllocatorDump>* out);
183 };
184
185 template <>
COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)186 struct COMPONENT_EXPORT(RESOURCE_COORDINATOR_PUBLIC_MOJOM)
187 StructTraits<memory_instrumentation::mojom::RawProcessMemoryDumpDataView,
188 std::unique_ptr<base::trace_event::ProcessMemoryDump>> {
189 // TODO(primiano): Remove this wrapping vector to adapt the underlying map<>
190 // and use ArrayTraits instead (crbug.com/763441).
191 static std::vector<
192 base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge>
193 allocator_dump_edges(
194 const std::unique_ptr<base::trace_event::ProcessMemoryDump>& pmd) {
195 return pmd->GetAllEdgesForSerialization();
196 }
197 static std::vector<std::unique_ptr<base::trace_event::MemoryAllocatorDump>>
198 allocator_dumps(
199 const std::unique_ptr<base::trace_event::ProcessMemoryDump>& pmd) {
200 std::vector<std::unique_ptr<base::trace_event::MemoryAllocatorDump>> dumps;
201 dumps.reserve(pmd->mutable_allocator_dumps_for_serialization()->size());
202 for (auto& it : *pmd->mutable_allocator_dumps_for_serialization())
203 dumps.push_back(std::move(it.second));
204 return dumps;
205 }
206 static base::trace_event::MemoryDumpLevelOfDetail level_of_detail(
207 const std::unique_ptr<base::trace_event::ProcessMemoryDump>& pmd) {
208 return pmd->dump_args().level_of_detail;
209 }
210 static base::trace_event::MemoryDumpDeterminism determinism(
211 const std::unique_ptr<base::trace_event::ProcessMemoryDump>& pmd) {
212 return pmd->dump_args().determinism;
213 }
214
215 static void SetToNull(
216 std::unique_ptr<base::trace_event::ProcessMemoryDump>* out) {
217 out->reset();
218 }
219
220 static bool IsNull(
221 const std::unique_ptr<base::trace_event::ProcessMemoryDump>& pmd) {
222 return !pmd;
223 }
224
225 static bool Read(
226 memory_instrumentation::mojom::RawProcessMemoryDumpDataView input,
227 std::unique_ptr<base::trace_event::ProcessMemoryDump>* out);
228 };
229
230 } // namespace mojo
231
232 #endif // SERVICES_RESOURCE_COORDINATOR_PUBLIC_CPP_MEMORY_INSTRUMENTATION_MEMORY_INSTRUMENTATION_MOJOM_TRAITS_H_
233