summaryrefslogtreecommitdiff
path: root/base/memory/shared_memory_tracker.cc
blob: 8613f595336ada19357c900cf0ab9bf4d243b7f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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 {

SharedMemoryTracker::Usage::Usage() = default;

SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default;

SharedMemoryTracker::Usage::~Usage() = default;

// static
SharedMemoryTracker* SharedMemoryTracker::GetInstance() {
  static SharedMemoryTracker* instance = new SharedMemoryTracker;
  return instance;
}

void SharedMemoryTracker::IncrementMemoryUsage(
    const SharedMemory& shared_memory) {
  Usage usage;
  // |shared_memory|'s unique ID must be generated here and it'd be too late at
  // OnMemoryDump. An ID is generated with a SharedMemoryHandle, but the handle
  // might already be closed at that time. Now IncrementMemoryUsage is called
  // just after mmap and the handle must live then. See the discussion at
  // crbug.com/604726#c30.
  SharedMemory::UniqueId id;
  if (!shared_memory.GetUniqueId(&id))
    return;
  usage.unique_id = id;
  usage.size = shared_memory.mapped_size();
  AutoLock hold(usages_lock_);
  usages_[&shared_memory] = usage;
}

void SharedMemoryTracker::DecrementMemoryUsage(
    const SharedMemory& shared_memory) {
  AutoLock hold(usages_lock_);
  usages_.erase(&shared_memory);
}

bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args,
                                       trace_event::ProcessMemoryDump* pmd) {
  std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash>
      sizes;
  {
    AutoLock hold(usages_lock_);
    for (const auto& usage : usages_)
      sizes[usage.second.unique_id] += usage.second.size;
  }
  for (auto& size : sizes) {
    const SharedMemory::UniqueId& id = size.first;
    std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory",
                                         static_cast<long long>(id.first),
                                         static_cast<long long>(id.second));
    auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name);
    trace_event::MemoryAllocatorDump* local_dump =
        pmd->CreateAllocatorDump(dump_name);
    // TODO(hajimehoshi): The size is not resident size but virtual size so far.
    // Fix this to record resident size.
    local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize,
                          trace_event::MemoryAllocatorDump::kUnitsBytes,
                          size.second);
    trace_event::MemoryAllocatorDump* global_dump =
        pmd->CreateSharedGlobalAllocatorDump(guid);
    global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize,
                           trace_event::MemoryAllocatorDump::kUnitsBytes,
                           size.second);
    // TOOD(hajimehoshi): Detect which the shared memory comes from browser,
    // renderer or GPU process.
    // TODO(hajimehoshi): Shared memory reported by GPU and discardable is
    // currently double-counted. Add ownership edges to avoid this.
    pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid());
  }
  return true;
}

SharedMemoryTracker::SharedMemoryTracker() {
  trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
      this, "SharedMemoryTracker", nullptr);
}

SharedMemoryTracker::~SharedMemoryTracker() = default;

}  // namespace