blob: 45d88e33556c32eb1fb07bf9f6a7a3e5979e3e25 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2017 The Chromium Authors
hajimehoshi2acea432017-03-08 08:55:372// 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
hajimehoshie370c172017-05-25 06:12:168#include <map>
Siddharthae241d0a2017-08-18 23:11:029#include <string>
brettw1ce49f62017-04-27 19:42:3210
David Sanders6e709942022-04-05 06:49:2611#include "base/base_export.h"
Alexandr Ilin7b882502018-03-27 07:44:5312#include "base/memory/shared_memory_mapping.h"
hajimehoshi2acea432017-03-08 08:55:3713#include "base/synchronization/lock.h"
Etienne Pierre-dorayfc7952f02025-06-06 00:04:3314#include "base/trace_event/memory_dump_provider.h"
hajimehoshi2acea432017-03-08 08:55:3715
16namespace base {
17
18namespace trace_event {
Siddharthae241d0a2017-08-18 23:11:0219class MemoryAllocatorDump;
Hajime Hoshi99042d02017-06-06 02:58:1120class MemoryAllocatorDumpGuid;
hajimehoshi2acea432017-03-08 08:55:3721class ProcessMemoryDump;
Peter Kasting134ef9af2024-12-28 02:30:0922} // namespace trace_event
hajimehoshi2acea432017-03-08 08:55:3723
24// SharedMemoryTracker tracks shared memory usage.
hajimehoshie370c172017-05-25 06:12:1625class BASE_EXPORT SharedMemoryTracker : public trace_event::MemoryDumpProvider {
hajimehoshi2acea432017-03-08 08:55:3726 public:
27 // Returns a singleton instance.
28 static SharedMemoryTracker* GetInstance();
29
Peter Boström75cd3c02021-09-28 15:23:1830 SharedMemoryTracker(const SharedMemoryTracker&) = delete;
31 SharedMemoryTracker& operator=(const SharedMemoryTracker&) = delete;
32
Siddharthae241d0a2017-08-18 23:11:0233 static std::string GetDumpNameForTracing(const UnguessableToken& id);
Hajime Hoshi99042d02017-06-06 02:58:1134
Hajime Hoshide0c27b2017-06-27 05:57:0435 static trace_event::MemoryAllocatorDumpGuid GetGlobalDumpIdForTracing(
Hajime Hoshi99042d02017-06-06 02:58:1136 const UnguessableToken& id);
37
Siddharthae241d0a2017-08-18 23:11:0238 // Gets or creates if non-existant, a memory dump for the |shared_memory|
39 // inside the given |pmd|. Also adds the necessary edges for the dump when
40 // creating the dump.
41 static const trace_event::MemoryAllocatorDump* GetOrCreateSharedMemoryDump(
Alexandr Ilinae5e45c02018-04-25 06:52:4142 const SharedMemoryMapping& shared_memory,
43 trace_event::ProcessMemoryDump* pmd);
Siddharthae241d0a2017-08-18 23:11:0244
Alexandr Ilin7b882502018-03-27 07:44:5345 // Records shared memory usage on valid mapping.
Alexandr Ilin7b882502018-03-27 07:44:5346 void IncrementMemoryUsage(const SharedMemoryMapping& mapping);
hajimehoshi2acea432017-03-08 08:55:3747
48 // Records shared memory usage on unmapping.
Alexandr Ilin7b882502018-03-27 07:44:5349 void DecrementMemoryUsage(const SharedMemoryMapping& mapping);
hajimehoshi2acea432017-03-08 08:55:3750
Lalit Maganti6b20dfea2017-11-07 15:49:2151 // Root dump name for all shared memory dumps.
52 static const char kDumpRootName[];
53
hajimehoshi2acea432017-03-08 08:55:3754 private:
hajimehoshi2acea432017-03-08 08:55:3755 SharedMemoryTracker();
56 ~SharedMemoryTracker() override;
57
hajimehoshie370c172017-05-25 06:12:1658 // trace_event::MemoryDumpProvider implementation.
59 bool OnMemoryDump(const trace_event::MemoryDumpArgs& args,
60 trace_event::ProcessMemoryDump* pmd) override;
hajimehoshi2acea432017-03-08 08:55:3761
Alexandr Ilin7b882502018-03-27 07:44:5362 static const trace_event::MemoryAllocatorDump*
63 GetOrCreateSharedMemoryDumpInternal(void* mapped_memory,
64 size_t mapped_size,
65 const UnguessableToken& mapped_id,
66 trace_event::ProcessMemoryDump* pmd);
67
68 // Information associated with each mapped address.
69 struct UsageInfo {
70 UsageInfo(size_t size, const UnguessableToken& id)
71 : mapped_size(size), mapped_id(id) {}
72
73 size_t mapped_size;
74 UnguessableToken mapped_id;
75 };
76
hajimehoshi2acea432017-03-08 08:55:3777 Lock usages_lock_;
Benoit Lize25859152020-07-09 11:52:0978 std::map<void*, UsageInfo> usages_ GUARDED_BY(usages_lock_);
hajimehoshi2acea432017-03-08 08:55:3779};
80
81} // namespace base
82
83#endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_