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

Unified Diff: base/memory/shared_memory_tracker.cc

Issue 2654073002: base: Introduce SharedMemoryTracker for POSIX (but not macOS) (Closed)
Patch Set: Add comments Created 3 years, 11 months 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 side-by-side diff with in-line comments
Download patch
« base/memory/shared_memory_handle.h ('K') | « base/memory/shared_memory_tracker.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/shared_memory_tracker.cc
diff --git a/base/memory/shared_memory_tracker.cc b/base/memory/shared_memory_tracker.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e3629ffc4d5d8100f39463fb72068493627d4798
--- /dev/null
+++ b/base/memory/shared_memory_tracker.cc
@@ -0,0 +1,83 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/shared_memory_tracker.h"
+
+#include "base/memory/shared_memory.h"
+#include "base/strings/stringprintf.h"
+#include "base/trace_event/memory_dump_manager.h"
+#include "base/trace_event/process_memory_dump.h"
+
+namespace base {
+
+const char* kSharedMemoryDumpName = "shared_memory";
danakj 2017/01/25 16:58:13 static
hajimehoshi 2017/01/26 10:56:00 Done.
+
+SharedMemoryTracker* SharedMemoryTracker::GetInstance() {
+ return base::Singleton<SharedMemoryTracker, base::LeakySingletonTraits<
+ SharedMemoryTracker>>::get();
+}
+
+bool SharedMemoryTracker::OnMemoryDump(
+ const base::trace_event::MemoryDumpArgs& args,
+ base::trace_event::ProcessMemoryDump* pmd) {
+ for (auto& bucket : buckets_) {
+ base::AutoLock lock(bucket.lock);
+ for (const auto& usage : bucket.usages) {
+ SharedMemoryHandleID id = usage.first;
+ std::string id_str = GetSharedMemoryHandleIDString(id);
+ std::string dump_name =
+ base::StringPrintf("%s/%s", kSharedMemoryDumpName, id_str.c_str());
danakj 2017/01/25 16:58:13 I think you could just print the two components of
ssid 2017/01/25 23:03:06 This function would be required later for the othe
hajimehoshi 2017/01/26 10:56:00 Right, a function to convert id into string would
+ auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name);
+ base::trace_event::MemoryAllocatorDump* local_dump =
danakj 2017/01/25 16:58:13 what deletes this?
ssid 2017/01/25 23:03:06 This gets added to trace json file at MemoryDumpMa
+ pmd->CreateAllocatorDump(dump_name);
+ local_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
ssid 2017/01/25 23:03:06 I think we need a TODO here saying this size repor
hajimehoshi 2017/01/26 10:56:00 Done.
+ base::trace_event::MemoryAllocatorDump::kUnitsBytes,
+ usage.second);
+ base::trace_event::MemoryAllocatorDump* global_dump =
danakj 2017/01/25 16:58:13 what deletes this?
ssid 2017/01/25 23:03:06 Same comment as above.
+ pmd->CreateSharedGlobalAllocatorDump(guid);
+ global_dump->AddScalar(
+ base::trace_event::MemoryAllocatorDump::kNameSize,
+ base::trace_event::MemoryAllocatorDump::kUnitsBytes, usage.second);
+ pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid());
ssid 2017/01/25 23:03:06 Is there any way to identify if this is browser or
ssid 2017/01/25 23:03:06 We also need a TODO here saying this is currently
hajimehoshi 2017/01/26 10:56:00 I'm not sure about this. I'll leave TODO here inst
+ }
+ }
+ return true;
+}
+
+bool SharedMemoryTracker::IncrementMemoryUsage(
+ const SharedMemory& shared_memory) {
+ SharedMemoryHandleID id;
+ if (!GetIDFromSharedMemoryHandle(shared_memory.handle(), &id)) {
+ return false;
+ }
+ int bucket = id.file_id % kTrackerBucketNum;
+ base::AutoLock lock(buckets_[bucket].lock);
+ buckets_[bucket].usages[id] = shared_memory.mapped_size();
+ return true;
+}
+
+bool SharedMemoryTracker::DecrementMemoryUsage(
+ const SharedMemory& shared_memory) {
+ SharedMemoryHandleID id;
+ if (!GetIDFromSharedMemoryHandle(shared_memory.handle(), &id)) {
+ return false;
+ }
+ int bucket = id.file_id % kTrackerBucketNum;
+ base::AutoLock lock(buckets_[bucket].lock);
+ buckets_[bucket].usages.erase(id);
+ return true;
+}
+
+SharedMemoryTracker::SharedMemoryTracker() {
+ base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
+ this, "SharedMemoryTracker", nullptr);
+}
+
+SharedMemoryTracker::~SharedMemoryTracker() {}
danakj 2017/01/25 16:58:13 = default for these
hajimehoshi 2017/01/26 10:56:00 Done.
+
+SharedMemoryTracker::TrackerBucket::TrackerBucket() {}
+
+SharedMemoryTracker::TrackerBucket::~TrackerBucket() {}
+
+} // namespace
« base/memory/shared_memory_handle.h ('K') | « base/memory/shared_memory_tracker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698