OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 // Keep this file in sync with the .proto files in this directory. |
| 6 |
| 7 #include "components/sync/protocol/proto_memory_estimations.h" |
| 8 |
| 9 #include "base/trace_event/estimate_memory_usage.h" |
| 10 // This include is here so that I don't forget to remove test-only |
| 11 // instantiations at the end. |
| 12 #include "components/sync/protocol/proto_unittest_serialized_data.h" |
| 13 #include "components/sync/protocol/proto_visitors.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // This class is a VisitProtoFields()-compatible visitor that estimates |
| 18 // proto's memory usage: |
| 19 // |
| 20 // MemoryUsageVisitor visitor; |
| 21 // VisitProtoFields(visitor, proto); |
| 22 // size_t memory_usage = visitor.memory_usage(); |
| 23 // |
| 24 class MemoryUsageVisitor { |
| 25 public: |
| 26 MemoryUsageVisitor(): memory_usage_(0) {} |
| 27 |
| 28 size_t memory_usage() const { return memory_usage_; } |
| 29 |
| 30 template <class P> |
| 31 void VisitBytes(const P& parent_proto, const char* field_name, |
| 32 const std::string& field) { |
| 33 // Delegate to Visit(..., const std::string&) below. |
| 34 Visit(parent_proto, field_name, field); |
| 35 } |
| 36 |
| 37 template <class P, class E> |
| 38 void VisitEnum(const P&, const char* field_name, E field) {} |
| 39 |
| 40 // Types derived from MessageLite (i.e. protos) |
| 41 template <class P, class F> |
| 42 typename std::enable_if< |
| 43 std::is_base_of<google::protobuf::MessageLite, F>::value |
| 44 >::type |
| 45 Visit(const P&, const char* field_name, const F& field) { |
| 46 using base::trace_event::EstimateMemoryUsage; |
| 47 // All object fields are dynamically allocated. |
| 48 memory_usage_ += sizeof(F) + EstimateMemoryUsage(field); |
| 49 } |
| 50 |
| 51 // Integral types |
| 52 template <class P, class F> |
| 53 typename std::enable_if<std::is_integral<F>::value>::type |
| 54 Visit(const P&, const char* field_name, const F& field) { |
| 55 // Integral fields (integers, floats & bool) don't allocate. |
| 56 } |
| 57 |
| 58 // std::string |
| 59 template <class P> |
| 60 void Visit(const P&, const char* field_name, const std::string& field) { |
| 61 using base::trace_event::EstimateMemoryUsage; |
| 62 // All strings are of type ArenaStringPtr, which essentially |
| 63 // is std::string*. |
| 64 memory_usage_ += sizeof(std::string) + EstimateMemoryUsage(field); |
| 65 } |
| 66 |
| 67 // RepeatedPtrField |
| 68 template <class P, class F> |
| 69 void Visit(const P&, const char* field_name, |
| 70 const google::protobuf::RepeatedPtrField<F>& fields) { |
| 71 using base::trace_event::EstimateMemoryUsage; |
| 72 // Can't use RepeatedPtrField::SpaceUsedExcludingSelf() because it will |
| 73 // end up calling undefined TypeHandler::SpaceUsed() method. |
| 74 memory_usage_ += fields.Capacity() ? sizeof(void*) : 0; // header |
| 75 memory_usage_ += fields.Capacity() * sizeof(void*); |
| 76 for (const auto& field: fields) { |
| 77 memory_usage_ += sizeof(F) + EstimateMemoryUsage(field); |
| 78 } |
| 79 } |
| 80 |
| 81 // RepeatedField<integral type> |
| 82 template <class P, class F> |
| 83 typename std::enable_if<std::is_integral<F>::value>::type |
| 84 Visit(const P&, const char* field_name, |
| 85 const google::protobuf::RepeatedField<F>& fields) { |
| 86 memory_usage_ += fields.SpaceUsedExcludingSelf(); |
| 87 // Integral fields (integers, floats & bool) don't allocate, so no |
| 88 // point in iterating over |fields|. |
| 89 } |
| 90 |
| 91 // RepeatedField<std::string> |
| 92 template <class P> |
| 93 void Visit(const P&, const char* field_name, |
| 94 const google::protobuf::RepeatedField<std::string>& fields) { |
| 95 using base::trace_event::EstimateMemoryUsage; |
| 96 memory_usage_ += fields.SpaceUsedExcludingSelf(); |
| 97 for (const auto& field: fields) { |
| 98 memory_usage_ += EstimateMemoryUsage(field); |
| 99 } |
| 100 } |
| 101 |
| 102 private: |
| 103 size_t memory_usage_; |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 namespace sync_pb { |
| 109 |
| 110 template <class P> |
| 111 size_t EstimateMemoryUsage(const P& proto) { |
| 112 MemoryUsageVisitor visitor; |
| 113 syncer::VisitProtoFields(visitor, proto); |
| 114 return visitor.memory_usage(); |
| 115 } |
| 116 |
| 117 // Explicit instantiations |
| 118 |
| 119 #define INSTANTIATE(Proto) \ |
| 120 template size_t EstimateMemoryUsage<Proto>(const Proto&); |
| 121 |
| 122 INSTANTIATE(EntitySpecifics) |
| 123 INSTANTIATE(AttachmentMetadata) |
| 124 INSTANTIATE(DataTypeContext) |
| 125 |
| 126 // TODO(dskiba): needed only for testing, remove |
| 127 |
| 128 INSTANTIATE(EncryptedData) |
| 129 INSTANTIATE(AppListSpecifics) |
| 130 INSTANTIATE(AppNotificationSettings) |
| 131 INSTANTIATE(LinkedAppIconInfo) |
| 132 INSTANTIATE(ArcPackageSpecifics) |
| 133 INSTANTIATE(SessionHeader) |
| 134 INSTANTIATE(SessionTab) |
| 135 INSTANTIATE(SessionWindow) |
| 136 INSTANTIATE(TabNavigation) |
| 137 INSTANTIATE(NavigationRedirect) |
| 138 INSTANTIATE(PasswordSpecificsData) |
| 139 INSTANTIATE(GlobalIdDirective) |
| 140 INSTANTIATE(TimeRangeDirective) |
| 141 INSTANTIATE(SessionSpecifics) |
| 142 INSTANTIATE(PrinterPPDReference) |
| 143 INSTANTIATE(AppNotification) |
| 144 INSTANTIATE(AppSettingSpecifics) |
| 145 INSTANTIATE(AppSpecifics) |
| 146 INSTANTIATE(ArticleSpecifics) |
| 147 INSTANTIATE(AutofillSpecifics) |
| 148 INSTANTIATE(AutofillProfileSpecifics) |
| 149 INSTANTIATE(WalletMetadataSpecifics) |
| 150 INSTANTIATE(AutofillWalletSpecifics) |
| 151 INSTANTIATE(BookmarkSpecifics) |
| 152 INSTANTIATE(DeviceInfoSpecifics) |
| 153 INSTANTIATE(DictionarySpecifics) |
| 154 INSTANTIATE(ExperimentsSpecifics) |
| 155 INSTANTIATE(PriorityPreferenceSpecifics) |
| 156 INSTANTIATE(ExtensionSettingSpecifics) |
| 157 INSTANTIATE(ExtensionSpecifics) |
| 158 INSTANTIATE(FaviconImageSpecifics) |
| 159 INSTANTIATE(FaviconTrackingSpecifics) |
| 160 INSTANTIATE(HistoryDeleteDirectiveSpecifics) |
| 161 INSTANTIATE(ManagedUserSettingSpecifics) |
| 162 INSTANTIATE(ManagedUserSpecifics) |
| 163 INSTANTIATE(ManagedUserSharedSettingSpecifics) |
| 164 INSTANTIATE(ManagedUserWhitelistSpecifics) |
| 165 INSTANTIATE(NigoriSpecifics) |
| 166 INSTANTIATE(PasswordSpecifics) |
| 167 INSTANTIATE(PreferenceSpecifics) |
| 168 INSTANTIATE(PrinterSpecifics) |
| 169 INSTANTIATE(ReadingListSpecifics) |
| 170 INSTANTIATE(SyncedNotificationAppInfoSpecifics) |
| 171 INSTANTIATE(SyncedNotificationSpecifics) |
| 172 INSTANTIATE(SearchEngineSpecifics) |
| 173 INSTANTIATE(ThemeSpecifics) |
| 174 INSTANTIATE(TypedUrlSpecifics) |
| 175 INSTANTIATE(WalletMaskedCreditCard) |
| 176 INSTANTIATE(WalletPostalAddress) |
| 177 INSTANTIATE(WifiCredentialSpecifics) |
| 178 INSTANTIATE(SyncEntity) |
| 179 INSTANTIATE(ClientToServerMessage) |
| 180 INSTANTIATE(ClientToServerResponse) |
| 181 INSTANTIATE(DatatypeAssociationStats) |
| 182 INSTANTIATE(DebugEventInfo) |
| 183 INSTANTIATE(DebugInfo) |
| 184 INSTANTIATE(SyncCycleCompletedEventInfo) |
| 185 INSTANTIATE(ClientConfigParams) |
| 186 INSTANTIATE(AttachmentIdProto) |
| 187 INSTANTIATE(EntityMetadata) |
| 188 |
| 189 #undef INSTANTIATE |
| 190 |
| 191 } // namespace sync_pb |
OLD | NEW |