OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #ifndef BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ |
| 6 #define BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ |
| 7 |
| 8 #include "base/atomicops.h" |
| 9 #include "base/memory/shared_memory_handle.h" |
| 10 #include "base/memory/singleton.h" |
| 11 #include "base/trace_event/memory_dump_provider.h" |
| 12 |
| 13 namespace base { |
| 14 |
| 15 class SharedMemory; |
| 16 |
| 17 namespace trace_event { |
| 18 class ProcessMemoryDump; |
| 19 } |
| 20 |
| 21 // SharedMemoryTracker tracks shared memory usage. |
| 22 class BASE_EXPORT SharedMemoryTracker |
| 23 : public base::trace_event::MemoryDumpProvider { |
| 24 public: |
| 25 // Returns a singleton instance. |
| 26 static SharedMemoryTracker* GetInstance(); |
| 27 |
| 28 // base::trace_event::MemoryDumpProvider implementation. |
| 29 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 30 base::trace_event::ProcessMemoryDump* pmd) override; |
| 31 |
| 32 // Records shared memory usage on mapping. |
| 33 void IncrementMemoryUsage(const SharedMemory& shared_memory); |
| 34 |
| 35 // Records shared memory usage on unmapping. |
| 36 void DecrementMemoryUsage(const SharedMemory& shared_memory); |
| 37 |
| 38 private: |
| 39 // TrackerBucket represents a group of shared memory handle ids and their |
| 40 // usages. Shared memorys are split into these buckets instead of using one |
| 41 // big unordered_map so that locking for exclusive control would occur much |
| 42 // less often. |
| 43 struct TrackerBucket { |
| 44 TrackerBucket(); |
| 45 ~TrackerBucket(); |
| 46 |
| 47 base::Lock lock; |
| 48 std::unordered_map<const SharedMemory*, size_t> usages; |
| 49 }; |
| 50 |
| 51 friend struct base::DefaultSingletonTraits<SharedMemoryTracker>; |
| 52 |
| 53 // TODO(hajimehoshi): Update this number based on actual performance |
| 54 // measurements. |
| 55 static const int kTrackerBucketNum = 16; |
| 56 |
| 57 TrackerBucket buckets_[kTrackerBucketNum]; |
| 58 |
| 59 SharedMemoryTracker(); |
| 60 ~SharedMemoryTracker() override; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SharedMemoryTracker); |
| 63 }; |
| 64 |
| 65 } // namespace base |
| 66 |
| 67 #endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ |
OLD | NEW |