summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/wtf/Partitions.cpp
blob: 60b30dbba200a2e753e7bc92d7e91b63099a8a01 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "wtf/Partitions.h"

#include "wtf/Alias.h"
#include "wtf/DefaultAllocator.h"
#include "wtf/FastMalloc.h"
#include "wtf/MainThread.h"

namespace WTF {

const char* const Partitions::kAllocatedObjectPoolName = "partition_alloc/allocated_objects";

int Partitions::s_initializationLock = 0;
bool Partitions::s_initialized = false;

PartitionAllocatorGeneric Partitions::m_fastMallocAllocator;
PartitionAllocatorGeneric Partitions::m_bufferAllocator;
SizeSpecificPartitionAllocator<3328> Partitions::m_nodeAllocator;
SizeSpecificPartitionAllocator<1024> Partitions::m_layoutAllocator;
HistogramEnumerationFunction Partitions::m_histogramEnumeration = nullptr;

void Partitions::initialize()
{
    spinLockLock(&s_initializationLock);

    if (!s_initialized) {
        partitionAllocGlobalInit(&Partitions::handleOutOfMemory);
        m_fastMallocAllocator.init();
        m_bufferAllocator.init();
        m_nodeAllocator.init();
        m_layoutAllocator.init();
        s_initialized = true;
    }

    spinLockUnlock(&s_initializationLock);
}

void Partitions::setHistogramEnumeration(HistogramEnumerationFunction histogramEnumeration)
{
    ASSERT(!m_histogramEnumeration);
    m_histogramEnumeration = histogramEnumeration;
}

void Partitions::shutdown()
{
    // We could ASSERT here for a memory leak within the partition, but it leads
    // to very hard to diagnose ASSERTs, so it's best to leave leak checking for
    // the valgrind and heapcheck bots, which run without partitions.
    (void) m_layoutAllocator.shutdown();
    (void) m_nodeAllocator.shutdown();
    (void) m_bufferAllocator.shutdown();
    (void) m_fastMallocAllocator.shutdown();
}

void Partitions::decommitFreeableMemory()
{
    ASSERT(isMainThread());

    partitionPurgeMemoryGeneric(bufferPartition(), PartitionPurgeDecommitEmptyPages);
    partitionPurgeMemoryGeneric(fastMallocPartition(), PartitionPurgeDecommitEmptyPages);
    partitionPurgeMemory(nodePartition(), PartitionPurgeDecommitEmptyPages);
    partitionPurgeMemory(layoutPartition(), PartitionPurgeDecommitEmptyPages);
}

void Partitions::reportMemoryUsageHistogram()
{
    static size_t supportedMaxSizeInMB = 4 * 1024;
    static size_t observedMaxSizeInMB = 0;

    if (!m_histogramEnumeration)
        return;
    // We only report the memory in the main thread.
    if (!isMainThread())
        return;
    // +1 is for rounding up the sizeInMB.
    size_t sizeInMB = Partitions::totalSizeOfCommittedPages() / 1024 / 1024 + 1;
    if (sizeInMB >= supportedMaxSizeInMB)
        sizeInMB = supportedMaxSizeInMB - 1;
    if (sizeInMB > observedMaxSizeInMB) {
        // Send a UseCounter only when we see the highest memory usage
        // we've ever seen.
        m_histogramEnumeration("PartitionAlloc.CommittedSize", sizeInMB, supportedMaxSizeInMB);
        observedMaxSizeInMB = sizeInMB;
    }
}

void Partitions::dumpMemoryStats(bool isLightDump, PartitionStatsDumper* partitionStatsDumper)
{
    // Object model and rendering partitions are not thread safe and can be
    // accessed only on the main thread.
    ASSERT(isMainThread());

    partitionDumpStatsGeneric(fastMallocPartition(), "fast_malloc", isLightDump, partitionStatsDumper);
    partitionDumpStatsGeneric(bufferPartition(), "buffer", isLightDump, partitionStatsDumper);
    partitionDumpStats(nodePartition(), "node", isLightDump, partitionStatsDumper);
    partitionDumpStats(layoutPartition(), "layout", isLightDump, partitionStatsDumper);
}

static NEVER_INLINE void partitionsOutOfMemoryUsing2G()
{
    size_t signature = 2UL * 1024 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing1G()
{
    size_t signature = 1UL * 1024 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing512M()
{
    size_t signature = 512 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing256M()
{
    size_t signature = 256 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing128M()
{
    size_t signature = 128 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing64M()
{
    size_t signature = 64 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing32M()
{
    size_t signature = 32 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsing16M()
{
    size_t signature = 16 * 1024 * 1024;
    alias(&signature);
    IMMEDIATE_CRASH();
}

static NEVER_INLINE void partitionsOutOfMemoryUsingLessThan16M()
{
    size_t signature = 16 * 1024 * 1024 - 1;
    alias(&signature);
    IMMEDIATE_CRASH();
}

void Partitions::handleOutOfMemory()
{
    volatile size_t totalUsage = totalSizeOfCommittedPages();

    if (totalUsage >= 2UL * 1024 * 1024 * 1024)
        partitionsOutOfMemoryUsing2G();
    if (totalUsage >= 1UL * 1024 * 1024 * 1024)
        partitionsOutOfMemoryUsing1G();
    if (totalUsage >= 512 * 1024 * 1024)
        partitionsOutOfMemoryUsing512M();
    if (totalUsage >= 256 * 1024 * 1024)
        partitionsOutOfMemoryUsing256M();
    if (totalUsage >= 128 * 1024 * 1024)
        partitionsOutOfMemoryUsing128M();
    if (totalUsage >= 64 * 1024 * 1024)
        partitionsOutOfMemoryUsing64M();
    if (totalUsage >= 32 * 1024 * 1024)
        partitionsOutOfMemoryUsing32M();
    if (totalUsage >= 16 * 1024 * 1024)
        partitionsOutOfMemoryUsing16M();
    partitionsOutOfMemoryUsingLessThan16M();
}

} // namespace WTF