Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 // Defines StructTraits specializations for translating between mojo types and | |
| 6 // base::StackSamplingProfiler types, with data validity checks. | |
| 7 | |
| 8 #ifndef COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
| 9 #define COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
| 10 | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/optional.h" | |
| 15 #include "base/profiler/stack_sampling_profiler.h" | |
| 16 #include "components/metrics/call_stack_profile_collector.mojom.h" | |
| 17 | |
| 18 namespace mojo { | |
| 19 | |
| 20 template <> | |
| 21 struct StructTraits<metrics::mojom::CallStackModule, | |
| 22 base::StackSamplingProfiler::Module> { | |
| 23 static uint64_t base_address( | |
| 24 const base::StackSamplingProfiler::Module& module) { | |
| 25 return module.base_address; | |
| 26 } | |
| 27 static const std::string& id( | |
| 28 const base::StackSamplingProfiler::Module& module) { | |
| 29 return module.id; | |
| 30 } | |
| 31 static const base::FilePath& filename( | |
| 32 const base::StackSamplingProfiler::Module& module) { | |
| 33 return module.filename; | |
| 34 } | |
| 35 | |
| 36 static bool Read(metrics::mojom::CallStackModuleDataView data, | |
| 37 base::StackSamplingProfiler::Module* out) { | |
| 38 std::string id; | |
| 39 base::FilePath filename; | |
| 40 if (!data.ReadId(&id) || !data.ReadFilename(&filename)) | |
| 41 return false; | |
| 42 | |
| 43 *out = | |
| 44 base::StackSamplingProfiler::Module(data.base_address(), id, filename); | |
| 45 return true; | |
| 46 } | |
| 47 }; | |
| 48 | |
| 49 template <> | |
| 50 struct StructTraits<metrics::mojom::CallStackFrame, | |
| 51 base::StackSamplingProfiler::Frame> { | |
| 52 static const uint64_t instruction_pointer( | |
| 53 const base::StackSamplingProfiler::Frame& frame) { | |
| 54 return frame.instruction_pointer; | |
| 55 } | |
| 56 static const uint64_t module_index( | |
| 57 const base::StackSamplingProfiler::Frame& frame) { | |
| 58 return frame.module_index == | |
| 59 base::StackSamplingProfiler::Frame::kUnknownModuleIndex ? | |
| 60 static_cast<uint64_t>(-1) : | |
| 61 frame.module_index; | |
| 62 } | |
| 63 | |
| 64 static bool Read(metrics::mojom::CallStackFrameDataView data, | |
| 65 base::StackSamplingProfiler::Frame* out) { | |
| 66 size_t module_index = data.module_index() == static_cast<uint64_t>(-1) ? | |
| 67 base::StackSamplingProfiler::Frame::kUnknownModuleIndex : | |
| 68 data.module_index(); | |
| 69 | |
| 70 // The module_index field must be checked to be in the valid range when | |
| 71 // reading CallStackProfile. | |
| 72 *out = base::StackSamplingProfiler::Frame(data.instruction_pointer(), | |
| 73 module_index); | |
| 74 return true; | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 template <> | |
| 79 struct StructTraits<metrics::mojom::CallStackProfile, | |
| 80 base::StackSamplingProfiler::CallStackProfile> { | |
| 81 static const std::vector<base::StackSamplingProfiler::Module>& modules( | |
| 82 const base::StackSamplingProfiler::CallStackProfile& profile, | |
| 83 void* context) { | |
| 84 Context* typed_context = static_cast<Context*>(context); | |
| 85 if (!typed_context->modules) | |
| 86 typed_context->modules = profile.modules; | |
| 87 return *typed_context->modules; | |
| 88 } | |
| 89 static const std::vector<base::StackSamplingProfiler::Sample>& samples( | |
| 90 const base::StackSamplingProfiler::CallStackProfile& profile, | |
| 91 void* context) { | |
| 92 Context* typed_context = static_cast<Context*>(context); | |
| 93 if (!typed_context->samples) | |
| 94 typed_context->samples = profile.samples; | |
| 95 return *typed_context->samples; | |
| 96 } | |
| 97 static const base::TimeDelta profile_duration( | |
| 98 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
| 99 return profile.profile_duration; | |
| 100 } | |
| 101 static const base::TimeDelta sampling_period( | |
| 102 const base::StackSamplingProfiler::CallStackProfile& profile) { | |
| 103 return profile.sampling_period; | |
| 104 } | |
| 105 | |
| 106 static void* SetUpContext( | |
| 107 const base::StackSamplingProfiler::CallStackProfile& input) { | |
| 108 return new Context; | |
| 109 } | |
| 110 static void TearDownContext( | |
| 111 const base::StackSamplingProfiler::CallStackProfile& input, | |
| 112 void* context) { | |
| 113 delete static_cast<Context*>(context); | |
| 114 } | |
| 115 | |
| 116 static bool ValidateSamples( | |
| 117 std::vector<base::StackSamplingProfiler::Sample> samples, | |
| 118 size_t module_count) { | |
| 119 for (const base::StackSamplingProfiler::Sample& sample : samples) { | |
| 120 for (const base::StackSamplingProfiler::Frame& frame : sample) { | |
| 121 if (frame.module_index >= module_count && frame.module_index != | |
| 122 base::StackSamplingProfiler::Frame::kUnknownModuleIndex) | |
| 123 return false; | |
| 124 } | |
| 125 } | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 static bool Read(metrics::mojom::CallStackProfileDataView data, | |
| 130 base::StackSamplingProfiler::CallStackProfile* out) { | |
| 131 std::vector<base::StackSamplingProfiler::Module> modules; | |
| 132 std::vector<base::StackSamplingProfiler::Sample> samples; | |
| 133 base::TimeDelta profile_duration, sampling_period; | |
| 134 if (!data.ReadModules(&modules) || !data.ReadSamples(&samples) || | |
| 135 !data.ReadProfileDuration(&profile_duration) || | |
| 136 !data.ReadSamplingPeriod(&sampling_period) || | |
| 137 !ValidateSamples(samples, modules.size())) | |
| 138 return false; | |
| 139 | |
| 140 *out = base::StackSamplingProfiler::CallStackProfile(); | |
| 141 out->modules = std::move(modules); | |
| 142 out->samples = std::move(samples); | |
| 143 out->profile_duration = profile_duration; | |
| 144 out->sampling_period = sampling_period; | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 private: | |
| 149 struct Context { | |
| 150 base::Optional<std::vector<base::StackSamplingProfiler::Module>> modules; | |
|
sky
2016/08/17 21:01:33
How come this is optional? The mojom doesn't have
Mike Wittman
2016/08/17 22:40:15
I'm using Optional to determine if these fields ha
Mike Wittman
2016/08/17 23:20:57
Actually, I don't believe I need Context at all...
| |
| 151 base::Optional<std::vector<base::StackSamplingProfiler::Sample>> samples; | |
| 152 }; | |
| 153 }; | |
| 154 | |
| 155 } // mojo | |
| 156 | |
| 157 #endif // COMPONENTS_METRICS_CALL_STACK_PROFILE_STRUCT_TRAITS_H_ | |
| OLD | NEW |