Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: components/sync/protocol/proto_memory_estimations.cc

Issue 2452713003: [Sync] Implement MemoryDumpProvider. (Closed)
Patch Set: Fix presumit; fix Windows; git cl format Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/memory_usage_estimator.h"
10 #include "components/sync/protocol/proto_visitors.h"
11
12 namespace {
13
14 // This class is a VisitProtoFields()-compatible visitor that estimates
15 // proto's memory usage:
16 //
17 // MemoryUsageVisitor visitor;
18 // VisitProtoFields(visitor, proto);
19 // size_t memory_usage = visitor.memory_usage();
20 //
21 class MemoryUsageVisitor {
22 public:
23 MemoryUsageVisitor() : memory_usage_(0) {}
24
25 size_t memory_usage() const { return memory_usage_; }
26
27 template <class P>
28 void VisitBytes(const P& parent_proto,
29 const char* field_name,
30 const std::string& field) {
31 // Delegate to Visit(..., const std::string&) below.
32 Visit(parent_proto, field_name, field);
33 }
34
35 template <class P, class E>
36 void VisitEnum(const P&, const char* field_name, E field) {}
37
38 // Types derived from MessageLite (i.e. protos)
39 template <class P, class F>
40 typename std::enable_if<
41 std::is_base_of<google::protobuf::MessageLite, F>::value>::type
42 Visit(const P&, const char* field_name, const F& field) {
43 using base::trace_event::EstimateMemoryUsage;
44 // All object fields are dynamically allocated.
45 memory_usage_ += sizeof(F) + EstimateMemoryUsage(field);
46 }
47
48 // Integral types
49 template <class P, class F>
50 typename std::enable_if<std::is_integral<F>::value>::type
51 Visit(const P&, const char* field_name, const F& field) {
52 // Integral fields (integers, floats & bool) don't allocate.
53 }
54
55 // std::string
56 template <class P>
57 void Visit(const P&, const char* field_name, const std::string& field) {
58 using base::trace_event::EstimateMemoryUsage;
59 // All strings are of type ArenaStringPtr, which essentially
60 // is std::string*.
61 memory_usage_ += sizeof(std::string) + EstimateMemoryUsage(field);
62 }
63
64 // RepeatedPtrField
65 template <class P, class F>
66 void Visit(const P&,
67 const char* field_name,
68 const google::protobuf::RepeatedPtrField<F>& fields) {
69 using base::trace_event::EstimateMemoryUsage;
70 // Can't use RepeatedPtrField::SpaceUsedExcludingSelf() because it will
71 // end up calling undefined TypeHandler::SpaceUsed() method.
72 memory_usage_ += fields.Capacity() ? sizeof(void*) : 0; // header
73 memory_usage_ += fields.Capacity() * sizeof(void*);
74 for (const auto& field : fields) {
75 memory_usage_ += sizeof(F) + EstimateMemoryUsage(field);
76 }
77 }
78
79 // RepeatedField<integral type>
80 template <class P, class F>
81 typename std::enable_if<std::is_integral<F>::value>::type Visit(
82 const P&,
83 const char* field_name,
84 const google::protobuf::RepeatedField<F>& fields) {
85 memory_usage_ += fields.SpaceUsedExcludingSelf();
86 // Integral fields (integers, floats & bool) don't allocate, so no
87 // point in iterating over |fields|.
88 }
89
90 // RepeatedField<std::string>
91 template <class P>
92 void Visit(const P&,
93 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 } // namespace sync_pb
OLDNEW
« no previous file with comments | « components/sync/protocol/proto_memory_estimations.h ('k') | components/sync/protocol/proto_value_conversions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698