Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/syncable/directory.h" | 5 #include "sync/syncable/directory.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | |
| 7 #include <stddef.h> | 8 #include <stddef.h> |
| 8 #include <stdint.h> | 9 #include <stdint.h> |
| 9 | 10 |
| 10 #include <algorithm> | 11 #include <algorithm> |
| 11 #include <iterator> | 12 #include <iterator> |
| 12 #include <utility> | 13 #include <utility> |
| 13 | 14 |
| 14 #include "base/base64.h" | 15 #include "base/base64.h" |
| 15 #include "base/guid.h" | 16 #include "base/guid.h" |
| 16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 17 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/stringprintf.h" | |
| 21 #include "base/trace_event/memory_dump_manager.h" | |
| 22 #include "base/trace_event/process_memory_dump.h" | |
| 19 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
| 20 #include "sync/internal_api/public/base/attachment_id_proto.h" | 24 #include "sync/internal_api/public/base/attachment_id_proto.h" |
| 21 #include "sync/internal_api/public/base/unique_position.h" | 25 #include "sync/internal_api/public/base/unique_position.h" |
| 22 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" | 26 #include "sync/internal_api/public/util/unrecoverable_error_handler.h" |
| 27 #include "sync/protocol/proto_value_conversions.h" | |
| 23 #include "sync/syncable/entry.h" | 28 #include "sync/syncable/entry.h" |
| 24 #include "sync/syncable/entry_kernel.h" | 29 #include "sync/syncable/entry_kernel.h" |
| 25 #include "sync/syncable/in_memory_directory_backing_store.h" | 30 #include "sync/syncable/in_memory_directory_backing_store.h" |
| 26 #include "sync/syncable/model_neutral_mutable_entry.h" | 31 #include "sync/syncable/model_neutral_mutable_entry.h" |
| 27 #include "sync/syncable/on_disk_directory_backing_store.h" | 32 #include "sync/syncable/on_disk_directory_backing_store.h" |
| 28 #include "sync/syncable/scoped_kernel_lock.h" | 33 #include "sync/syncable/scoped_kernel_lock.h" |
| 29 #include "sync/syncable/scoped_parent_child_index_updater.h" | 34 #include "sync/syncable/scoped_parent_child_index_updater.h" |
| 30 #include "sync/syncable/syncable-inl.h" | 35 #include "sync/syncable/syncable-inl.h" |
| 31 #include "sync/syncable/syncable_base_transaction.h" | 36 #include "sync/syncable/syncable_base_transaction.h" |
| 32 #include "sync/syncable/syncable_changes_version.h" | 37 #include "sync/syncable/syncable_changes_version.h" |
| 33 #include "sync/syncable/syncable_read_transaction.h" | 38 #include "sync/syncable/syncable_read_transaction.h" |
| 34 #include "sync/syncable/syncable_util.h" | 39 #include "sync/syncable/syncable_util.h" |
| 35 #include "sync/syncable/syncable_write_transaction.h" | 40 #include "sync/syncable/syncable_write_transaction.h" |
| 36 | 41 |
| 37 using std::string; | 42 using std::string; |
| 38 | 43 |
| 39 namespace syncer { | 44 namespace syncer { |
| 40 namespace syncable { | 45 namespace syncable { |
| 41 | 46 |
| 47 namespace { | |
| 48 | |
| 49 template <typename Key, typename Value> | |
| 50 size_t GetMapMemoryUsage(const std::unordered_map<Key, Value> map) { | |
| 51 return map.size() * (sizeof(Key) + sizeof(Value)); | |
|
stanisc
2016/07/06 21:50:32
This is too simplistic. See http://info.prelert.co
ssid
2016/07/19 22:10:35
Made it more accurate. Apart from these there are
| |
| 52 } | |
| 53 } | |
| 54 | |
| 42 // static | 55 // static |
| 43 const base::FilePath::CharType Directory::kSyncDatabaseFilename[] = | 56 const base::FilePath::CharType Directory::kSyncDatabaseFilename[] = |
| 44 FILE_PATH_LITERAL("SyncData.sqlite3"); | 57 FILE_PATH_LITERAL("SyncData.sqlite3"); |
| 45 | 58 |
| 46 Directory::PersistedKernelInfo::PersistedKernelInfo() { | 59 Directory::PersistedKernelInfo::PersistedKernelInfo() { |
| 47 ModelTypeSet protocol_types = ProtocolTypes(); | 60 ModelTypeSet protocol_types = ProtocolTypes(); |
| 48 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); | 61 for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good(); |
| 49 iter.Inc()) { | 62 iter.Inc()) { |
| 50 ResetDownloadProgress(iter.Get()); | 63 ResetDownloadProgress(iter.Get()); |
| 51 transaction_version[iter.Get()] = 0; | 64 transaction_version[iter.Get()] = 0; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 // Save changes back in case there are any metahandles to purge. | 221 // Save changes back in case there are any metahandles to purge. |
| 209 if (!SaveChanges()) | 222 if (!SaveChanges()) |
| 210 return FAILED_INITIAL_WRITE; | 223 return FAILED_INITIAL_WRITE; |
| 211 | 224 |
| 212 // Now that we've successfully opened the store, install an error handler to | 225 // Now that we've successfully opened the store, install an error handler to |
| 213 // deal with catastrophic errors that may occur later on. Use a weak pointer | 226 // deal with catastrophic errors that may occur later on. Use a weak pointer |
| 214 // because we cannot guarantee that this Directory will outlive the Closure. | 227 // because we cannot guarantee that this Directory will outlive the Closure. |
| 215 store_->SetCatastrophicErrorHandler(base::Bind( | 228 store_->SetCatastrophicErrorHandler(base::Bind( |
| 216 &Directory::OnCatastrophicError, weak_ptr_factory_.GetWeakPtr())); | 229 &Directory::OnCatastrophicError, weak_ptr_factory_.GetWeakPtr())); |
| 217 | 230 |
| 231 UMA_HISTOGRAM_COUNTS("Sync.DirectoryMemoryUsageKB", | |
|
stanisc
2016/07/06 21:50:32
Someone from the current sync team should comment
ssid
2016/07/19 22:10:35
Maybe that can be added later on top of this imple
| |
| 232 GetApproximateMemoryUsage()); | |
| 233 | |
| 218 return OPENED; | 234 return OPENED; |
| 219 } | 235 } |
| 220 | 236 |
| 221 DeleteJournal* Directory::delete_journal() { | 237 DeleteJournal* Directory::delete_journal() { |
| 222 DCHECK(delete_journal_.get()); | 238 DCHECK(delete_journal_.get()); |
| 223 return delete_journal_.get(); | 239 return delete_journal_.get(); |
| 224 } | 240 } |
| 225 | 241 |
| 226 void Directory::Close() { | 242 void Directory::Close() { |
| 227 store_.reset(); | 243 store_.reset(); |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 913 ScopedKernelLock lock(this); | 929 ScopedKernelLock lock(this); |
| 914 kernel_->persisted_info.download_progress[model_type].SerializeToString( | 930 kernel_->persisted_info.download_progress[model_type].SerializeToString( |
| 915 value_out); | 931 value_out); |
| 916 } | 932 } |
| 917 | 933 |
| 918 size_t Directory::GetEntriesCount() const { | 934 size_t Directory::GetEntriesCount() const { |
| 919 ScopedKernelLock lock(this); | 935 ScopedKernelLock lock(this); |
| 920 return kernel_->metahandles_map.size(); | 936 return kernel_->metahandles_map.size(); |
| 921 } | 937 } |
| 922 | 938 |
| 939 size_t Directory::GetApproximateMemoryUsage() { | |
| 940 size_t total = 0; | |
| 941 ScopedKernelLock lock(this); | |
| 942 total += sizeof(this) + GetMapMemoryUsage(kernel_->metahandles_map) + | |
| 943 GetMapMemoryUsage(kernel_->ids_map) + | |
| 944 GetMapMemoryUsage(kernel_->server_tags_map) + | |
| 945 GetMapMemoryUsage(kernel_->client_tags_map) + | |
| 946 GetMapMemoryUsage(kernel_->index_by_attachment_id); | |
| 947 total += kernel_->parent_child_index.bytes_used(); | |
| 948 | |
| 949 for (size_t i = 0; i < MODEL_TYPE_COUNT; ++i) | |
| 950 total += kernel_->persisted_info.datatype_context[i].context().capacity(); | |
| 951 | |
| 952 for (auto entry : kernel_->index_by_attachment_id) | |
| 953 total += entry.first.capacity() + entry.second.size() * sizeof(int64_t); | |
| 954 | |
| 955 for (auto handle : kernel_->metahandles_map) { | |
| 956 if (handle.second->cached_size()) { | |
|
stanisc
2016/07/06 21:50:32
A comment here would be useful
ssid
2016/07/19 22:10:35
Done.
| |
| 957 total += handle.second->cached_size(); | |
| 958 continue; | |
| 959 } | |
| 960 | |
| 961 size_t entry_size = 0; | |
| 962 for (unsigned i = ID_FIELDS_BEGIN; i < ID_FIELDS_END; ++i) { | |
| 963 // Twice because the |ids_map| stores a copy of these strings. | |
| 964 entry_size += | |
| 965 2 * handle.second->ref(static_cast<IdField>(i)).value().capacity(); | |
|
stanisc
2016/07/06 21:50:31
I am not sure this is right. client ID might be th
ssid
2016/07/19 22:10:35
counting the ids_map separately. The string capaci
| |
| 966 } | |
| 967 for (unsigned i = STRING_FIELDS_BEGIN; i < STRING_FIELDS_END; ++i) { | |
| 968 if (i == UNIQUE_SERVER_TAG || i == UNIQUE_CLIENT_TAG) { | |
| 969 // These are stored again in tags_map. | |
|
stanisc
2016/07/06 21:50:32
The same is here - if the server tag is the same a
ssid
2016/07/19 22:10:35
This one will appear twice if the id is same. Ther
| |
| 970 entry_size += | |
| 971 2 * handle.second->ref(static_cast<StringField>(i)).capacity(); | |
| 972 } else { | |
| 973 entry_size += | |
| 974 handle.second->ref(static_cast<StringField>(i)).capacity(); | |
| 975 } | |
| 976 } | |
| 977 for (unsigned i = PROTO_FIELDS_BEGIN; i < PROTO_FIELDS_END; ++i) { | |
| 978 const auto& field = handle.second->ref(static_cast<ProtoField>(i)); | |
| 979 if (&sync_pb::EntitySpecifics::default_instance() != &field) { | |
| 980 entry_size += GetEntitySpecificsSize(field); | |
|
stanisc
2016/07/06 21:50:32
Proto fields are often shared which means any cons
ssid
2016/07/19 22:10:35
Done.
| |
| 981 } | |
| 982 } | |
| 983 for (unsigned i = ATTACHMENT_METADATA_FIELDS_BEGIN; | |
| 984 i < ATTACHMENT_METADATA_FIELDS_END; ++i) { | |
| 985 const auto& field = | |
| 986 handle.second->ref(static_cast<AttachmentMetadataField>(i)); | |
| 987 if (&sync_pb::AttachmentMetadata::default_instance() != &field) { | |
| 988 entry_size += sizeof(sync_pb::AttachmentMetadata); | |
|
stanisc
2016/07/06 21:50:32
The same is here.
ssid
2016/07/19 22:10:35
Done.
| |
| 989 entry_size += | |
| 990 field.record_size() * (sizeof(sync_pb::AttachmentMetadataRecord) + | |
| 991 sizeof(sync_pb::AttachmentIdProto)); | |
| 992 for (int i = 0; i < field.record_size(); ++i) | |
| 993 entry_size += field.record(i).id().unique_id().capacity(); | |
| 994 } | |
| 995 } | |
| 996 for (unsigned i = UNIQUE_POSITION_FIELDS_BEGIN; | |
| 997 i < UNIQUE_POSITION_FIELDS_END; ++i) { | |
| 998 entry_size += handle.second->ref(static_cast<UniquePositionField>(i)) | |
| 999 .compressed_size(); | |
| 1000 } | |
| 1001 | |
| 1002 handle.second->set_cached_size(entry_size); | |
| 1003 total += entry_size; | |
| 1004 } | |
| 1005 return total; | |
| 1006 } | |
| 1007 | |
| 1008 void Directory::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) { | |
| 1009 auto dump = pmd->CreateAllocatorDump(base::StringPrintf( | |
| 1010 "sync/0x%" PRIXPTR, reinterpret_cast<uintptr_t>(this))); | |
| 1011 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 1012 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 1013 GetApproximateMemoryUsage()); | |
| 1014 const char* system_allocator_name = | |
| 1015 base::trace_event::MemoryDumpManager::GetInstance() | |
| 1016 ->system_allocator_pool_name(); | |
| 1017 if (system_allocator_name) | |
| 1018 pmd->AddSuballocation(dump->guid(), system_allocator_name); | |
| 1019 } | |
| 1020 | |
| 923 void Directory::SetDownloadProgress( | 1021 void Directory::SetDownloadProgress( |
| 924 ModelType model_type, | 1022 ModelType model_type, |
| 925 const sync_pb::DataTypeProgressMarker& new_progress) { | 1023 const sync_pb::DataTypeProgressMarker& new_progress) { |
| 926 ScopedKernelLock lock(this); | 1024 ScopedKernelLock lock(this); |
| 927 kernel_->persisted_info.download_progress[model_type].CopyFrom(new_progress); | 1025 kernel_->persisted_info.download_progress[model_type].CopyFrom(new_progress); |
| 928 kernel_->info_status = KERNEL_SHARE_INFO_DIRTY; | 1026 kernel_->info_status = KERNEL_SHARE_INFO_DIRTY; |
| 929 } | 1027 } |
| 930 | 1028 |
| 931 bool Directory::HasEmptyDownloadProgress(ModelType type) const { | 1029 bool Directory::HasEmptyDownloadProgress(ModelType type) const { |
| 932 ScopedKernelLock lock(this); | 1030 ScopedKernelLock lock(this); |
| (...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1579 Directory::Kernel* Directory::kernel() { | 1677 Directory::Kernel* Directory::kernel() { |
| 1580 return kernel_; | 1678 return kernel_; |
| 1581 } | 1679 } |
| 1582 | 1680 |
| 1583 const Directory::Kernel* Directory::kernel() const { | 1681 const Directory::Kernel* Directory::kernel() const { |
| 1584 return kernel_; | 1682 return kernel_; |
| 1585 } | 1683 } |
| 1586 | 1684 |
| 1587 } // namespace syncable | 1685 } // namespace syncable |
| 1588 } // namespace syncer | 1686 } // namespace syncer |
| OLD | NEW |