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 #include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation_mojom_traits.h" 6 7 #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h" 8 9 namespace mojo { 10 11 // static 12 memory_instrumentation::mojom::DumpType 13 EnumTraits<memory_instrumentation::mojom::DumpType, 14 base::trace_event::MemoryDumpType>:: ToMojom(base::trace_event::MemoryDumpType type)15 ToMojom(base::trace_event::MemoryDumpType type) { 16 switch (type) { 17 case base::trace_event::MemoryDumpType::PERIODIC_INTERVAL: 18 return memory_instrumentation::mojom::DumpType::PERIODIC_INTERVAL; 19 case base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED: 20 return memory_instrumentation::mojom::DumpType::EXPLICITLY_TRIGGERED; 21 case base::trace_event::MemoryDumpType::SUMMARY_ONLY: 22 return memory_instrumentation::mojom::DumpType::SUMMARY_ONLY; 23 default: 24 CHECK(false) << "Invalid type: " << static_cast<uint8_t>(type); 25 // This should not be reached. Just return a random value. 26 return memory_instrumentation::mojom::DumpType::PERIODIC_INTERVAL; 27 } 28 } 29 30 // static 31 bool EnumTraits<memory_instrumentation::mojom::DumpType, 32 base::trace_event::MemoryDumpType>:: FromMojom(memory_instrumentation::mojom::DumpType input,base::trace_event::MemoryDumpType * out)33 FromMojom(memory_instrumentation::mojom::DumpType input, 34 base::trace_event::MemoryDumpType* out) { 35 switch (input) { 36 case memory_instrumentation::mojom::DumpType::PERIODIC_INTERVAL: 37 *out = base::trace_event::MemoryDumpType::PERIODIC_INTERVAL; 38 break; 39 case memory_instrumentation::mojom::DumpType::EXPLICITLY_TRIGGERED: 40 *out = base::trace_event::MemoryDumpType::EXPLICITLY_TRIGGERED; 41 break; 42 case memory_instrumentation::mojom::DumpType::SUMMARY_ONLY: 43 *out = base::trace_event::MemoryDumpType::SUMMARY_ONLY; 44 break; 45 default: 46 NOTREACHED() << "Invalid type: " << static_cast<uint8_t>(input); 47 return false; 48 } 49 return true; 50 } 51 52 // static 53 memory_instrumentation::mojom::LevelOfDetail 54 EnumTraits<memory_instrumentation::mojom::LevelOfDetail, 55 base::trace_event::MemoryDumpLevelOfDetail>:: ToMojom(base::trace_event::MemoryDumpLevelOfDetail level_of_detail)56 ToMojom(base::trace_event::MemoryDumpLevelOfDetail level_of_detail) { 57 switch (level_of_detail) { 58 case base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND: 59 return memory_instrumentation::mojom::LevelOfDetail::BACKGROUND; 60 case base::trace_event::MemoryDumpLevelOfDetail::LIGHT: 61 return memory_instrumentation::mojom::LevelOfDetail::LIGHT; 62 case base::trace_event::MemoryDumpLevelOfDetail::DETAILED: 63 return memory_instrumentation::mojom::LevelOfDetail::DETAILED; 64 default: 65 CHECK(false) << "Invalid type: " << static_cast<uint8_t>(level_of_detail); 66 // This should not be reached. Just return a random value. 67 return memory_instrumentation::mojom::LevelOfDetail::BACKGROUND; 68 } 69 } 70 71 // static 72 bool EnumTraits<memory_instrumentation::mojom::LevelOfDetail, 73 base::trace_event::MemoryDumpLevelOfDetail>:: FromMojom(memory_instrumentation::mojom::LevelOfDetail input,base::trace_event::MemoryDumpLevelOfDetail * out)74 FromMojom(memory_instrumentation::mojom::LevelOfDetail input, 75 base::trace_event::MemoryDumpLevelOfDetail* out) { 76 switch (input) { 77 case memory_instrumentation::mojom::LevelOfDetail::BACKGROUND: 78 *out = base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND; 79 break; 80 case memory_instrumentation::mojom::LevelOfDetail::LIGHT: 81 *out = base::trace_event::MemoryDumpLevelOfDetail::LIGHT; 82 break; 83 case memory_instrumentation::mojom::LevelOfDetail::DETAILED: 84 *out = base::trace_event::MemoryDumpLevelOfDetail::DETAILED; 85 break; 86 default: 87 NOTREACHED() << "Invalid type: " << static_cast<uint8_t>(input); 88 return false; 89 } 90 return true; 91 } 92 93 // static 94 memory_instrumentation::mojom::Determinism 95 EnumTraits<memory_instrumentation::mojom::Determinism, 96 base::trace_event::MemoryDumpDeterminism>:: ToMojom(base::trace_event::MemoryDumpDeterminism determinism)97 ToMojom(base::trace_event::MemoryDumpDeterminism determinism) { 98 switch (determinism) { 99 case base::trace_event::MemoryDumpDeterminism::NONE: 100 return memory_instrumentation::mojom::Determinism::NONE; 101 case base::trace_event::MemoryDumpDeterminism::FORCE_GC: 102 return memory_instrumentation::mojom::Determinism::FORCE_GC; 103 default: 104 CHECK(false) << "Invalid type: " << static_cast<uint8_t>(determinism); 105 // This should not be reached. Just return a random value. 106 return memory_instrumentation::mojom::Determinism::NONE; 107 } 108 } 109 110 // static 111 bool EnumTraits<memory_instrumentation::mojom::Determinism, 112 base::trace_event::MemoryDumpDeterminism>:: FromMojom(memory_instrumentation::mojom::Determinism input,base::trace_event::MemoryDumpDeterminism * out)113 FromMojom(memory_instrumentation::mojom::Determinism input, 114 base::trace_event::MemoryDumpDeterminism* out) { 115 switch (input) { 116 case memory_instrumentation::mojom::Determinism::NONE: 117 *out = base::trace_event::MemoryDumpDeterminism::NONE; 118 break; 119 case memory_instrumentation::mojom::Determinism::FORCE_GC: 120 *out = base::trace_event::MemoryDumpDeterminism::FORCE_GC; 121 break; 122 default: 123 NOTREACHED() << "Invalid type: " << static_cast<uint8_t>(input); 124 return false; 125 } 126 return true; 127 } 128 129 // static 130 bool StructTraits<memory_instrumentation::mojom::RequestArgsDataView, 131 base::trace_event::MemoryDumpRequestArgs>:: Read(memory_instrumentation::mojom::RequestArgsDataView input,base::trace_event::MemoryDumpRequestArgs * out)132 Read(memory_instrumentation::mojom::RequestArgsDataView input, 133 base::trace_event::MemoryDumpRequestArgs* out) { 134 out->dump_guid = input.dump_guid(); 135 if (!input.ReadDumpType(&out->dump_type)) 136 return false; 137 if (!input.ReadLevelOfDetail(&out->level_of_detail)) 138 return false; 139 if (!input.ReadDeterminism(&out->determinism)) 140 return false; 141 return true; 142 } 143 144 // static 145 bool StructTraits< 146 memory_instrumentation::mojom::RawAllocatorDumpEdgeDataView, 147 base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge>:: Read(memory_instrumentation::mojom::RawAllocatorDumpEdgeDataView input,base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge * out)148 Read(memory_instrumentation::mojom::RawAllocatorDumpEdgeDataView input, 149 base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge* out) { 150 out->source = base::trace_event::MemoryAllocatorDumpGuid(input.source_id()); 151 out->target = base::trace_event::MemoryAllocatorDumpGuid(input.target_id()); 152 out->importance = input.importance(); 153 out->overridable = input.overridable(); 154 return true; 155 } 156 157 // static 158 bool UnionTraits< 159 memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView, 160 base::trace_event::MemoryAllocatorDump::Entry>:: Read(memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView input,base::trace_event::MemoryAllocatorDump::Entry * out)161 Read( 162 memory_instrumentation::mojom::RawAllocatorDumpEntryValueDataView input, 163 base::trace_event::MemoryAllocatorDump::Entry* out) { 164 using memory_instrumentation::mojom::RawAllocatorDumpEntryValue; 165 switch (input.tag()) { 166 case RawAllocatorDumpEntryValue::Tag::VALUE_STRING: { 167 std::string value_string; 168 if (!input.ReadValueString(&value_string)) 169 return false; 170 out->value_string = std::move(value_string); 171 out->entry_type = base::trace_event::MemoryAllocatorDump::Entry::kString; 172 break; 173 } 174 case RawAllocatorDumpEntryValue::Tag::VALUE_UINT64: { 175 out->value_uint64 = input.value_uint64(); 176 out->entry_type = base::trace_event::MemoryAllocatorDump::Entry::kUint64; 177 break; 178 } 179 default: 180 return false; 181 } 182 return true; 183 } 184 185 // static 186 bool StructTraits<memory_instrumentation::mojom::RawAllocatorDumpEntryDataView, 187 base::trace_event::MemoryAllocatorDump::Entry>:: Read(memory_instrumentation::mojom::RawAllocatorDumpEntryDataView input,base::trace_event::MemoryAllocatorDump::Entry * out)188 Read(memory_instrumentation::mojom::RawAllocatorDumpEntryDataView input, 189 base::trace_event::MemoryAllocatorDump::Entry* out) { 190 if (!input.ReadName(&out->name) || !input.ReadUnits(&out->units)) 191 return false; 192 if (!input.ReadValue(out)) 193 return false; 194 return true; 195 } 196 197 // static 198 bool StructTraits<memory_instrumentation::mojom::RawAllocatorDumpDataView, 199 std::unique_ptr<base::trace_event::MemoryAllocatorDump>>:: Read(memory_instrumentation::mojom::RawAllocatorDumpDataView input,std::unique_ptr<base::trace_event::MemoryAllocatorDump> * out)200 Read(memory_instrumentation::mojom::RawAllocatorDumpDataView input, 201 std::unique_ptr<base::trace_event::MemoryAllocatorDump>* out) { 202 std::string absolute_name; 203 if (!input.ReadAbsoluteName(&absolute_name)) 204 return false; 205 base::trace_event::MemoryDumpLevelOfDetail level_of_detail; 206 if (!input.ReadLevelOfDetail(&level_of_detail)) 207 return false; 208 auto mad = std::make_unique<base::trace_event::MemoryAllocatorDump>( 209 absolute_name, level_of_detail, 210 base::trace_event::MemoryAllocatorDumpGuid(input.id())); 211 if (input.weak()) 212 mad->set_flags(base::trace_event::MemoryAllocatorDump::WEAK); 213 if (!input.ReadEntries(mad->mutable_entries_for_serialization())) 214 return false; 215 *out = std::move(mad); 216 return true; 217 } 218 219 // static 220 bool StructTraits<memory_instrumentation::mojom::RawProcessMemoryDumpDataView, 221 std::unique_ptr<base::trace_event::ProcessMemoryDump>>:: Read(memory_instrumentation::mojom::RawProcessMemoryDumpDataView input,std::unique_ptr<base::trace_event::ProcessMemoryDump> * out)222 Read(memory_instrumentation::mojom::RawProcessMemoryDumpDataView input, 223 std::unique_ptr<base::trace_event::ProcessMemoryDump>* out) { 224 base::trace_event::MemoryDumpArgs dump_args; 225 if (!input.ReadLevelOfDetail(&dump_args.level_of_detail)) 226 return false; 227 std::vector<base::trace_event::ProcessMemoryDump::MemoryAllocatorDumpEdge> 228 edges; 229 if (!input.ReadAllocatorDumpEdges(&edges)) 230 return false; 231 std::vector<std::unique_ptr<base::trace_event::MemoryAllocatorDump>> dumps; 232 if (!input.ReadAllocatorDumps(&dumps)) 233 return false; 234 auto pmd = std::make_unique<base::trace_event::ProcessMemoryDump>(dump_args); 235 pmd->SetAllocatorDumpsForSerialization(std::move(dumps)); 236 pmd->SetAllEdgesForSerialization(edges); 237 *out = std::move(pmd); 238 return true; 239 } 240 241 } // namespace mojo 242