[bmalloc] Each IsoPage gets 1MB VA because VMHeap::tryAllocateLargeChunk rounds up
[WebKit-https.git] / Source / WebCore / page / cocoa / ResourceUsageThreadCocoa.mm
1 /*
2  * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ResourceUsageThread.h"
28
29 #if ENABLE(RESOURCE_USAGE)
30
31 #include "WorkerThread.h"
32 #include <JavaScriptCore/GCActivityCallback.h>
33 #include <JavaScriptCore/Heap.h>
34 #include <JavaScriptCore/SamplingProfiler.h>
35 #include <JavaScriptCore/VM.h>
36 #include <mach/mach.h>
37 #include <mach/vm_statistics.h>
38 #include <pal/spi/cocoa/MachVMSPI.h>
39 #include <wtf/MachSendRight.h>
40 #include <wtf/text/StringConcatenateNumbers.h>
41
42 namespace WebCore {
43
44 size_t vmPageSize()
45 {
46 #if PLATFORM(IOS_FAMILY)
47     return vm_kernel_page_size;
48 #else
49     static size_t cached = sysconf(_SC_PAGESIZE);
50     return cached;
51 #endif
52 }
53
54 void logFootprintComparison(const std::array<TagInfo, 256>& before, const std::array<TagInfo, 256>& after)
55 {
56     const size_t pageSize = vmPageSize();
57
58     WTFLogAlways("Per-tag breakdown of memory reclaimed by pressure handler:");
59     WTFLogAlways("  ## %16s %10s %10s %10s", "VM Tag", "Before", "After", "Diff");
60     for (unsigned i = 0; i < 256; ++i) {
61         ssize_t dirtyBefore = before[i].dirty * pageSize;
62         ssize_t dirtyAfter = after[i].dirty * pageSize;
63         ssize_t dirtyDiff = dirtyAfter - dirtyBefore;
64         if (!dirtyBefore && !dirtyAfter)
65             continue;
66         String tagName = displayNameForVMTag(i);
67         if (!tagName)
68             tagName = makeString("Tag ", i);
69         WTFLogAlways("  %02X %16s %10ld %10ld %10ld",
70             i,
71             tagName.ascii().data(),
72             dirtyBefore,
73             dirtyAfter,
74             dirtyDiff
75         );
76     }
77 }
78
79 const char* displayNameForVMTag(unsigned tag)
80 {
81     switch (tag) {
82     case VM_MEMORY_IOKIT: return "IOKit";
83     case VM_MEMORY_LAYERKIT: return "CoreAnimation";
84     case VM_MEMORY_IMAGEIO: return "ImageIO";
85     case VM_MEMORY_CGIMAGE: return "CG image";
86     case VM_MEMORY_JAVASCRIPT_CORE: return "Gigacage";
87     case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR: return "JSC JIT";
88     case VM_MEMORY_JAVASCRIPT_JIT_REGISTER_FILE: return "IsoHeap";
89     case VM_MEMORY_MALLOC: return "malloc";
90     case VM_MEMORY_MALLOC_HUGE: return "malloc (huge)";
91     case VM_MEMORY_MALLOC_LARGE: return "malloc (large)";
92     case VM_MEMORY_MALLOC_SMALL: return "malloc (small)";
93     case VM_MEMORY_MALLOC_TINY: return "malloc (tiny)";
94     case VM_MEMORY_MALLOC_NANO: return "malloc (nano)";
95     case VM_MEMORY_TCMALLOC: return "bmalloc";
96     case VM_MEMORY_FOUNDATION: return "Foundation";
97     case VM_MEMORY_STACK: return "Stack";
98     case VM_MEMORY_SQLITE: return "SQLite";
99     case VM_MEMORY_UNSHARED_PMAP: return "pmap (unshared)";
100     case VM_MEMORY_DYLIB: return "dylib";
101     case VM_MEMORY_CORESERVICES: return "CoreServices";
102     case VM_MEMORY_OS_ALLOC_ONCE: return "os_alloc_once";
103     case VM_MEMORY_LIBDISPATCH: return "libdispatch";
104     default: return nullptr;
105     }
106 }
107
108 std::array<TagInfo, 256> pagesPerVMTag()
109 {
110     std::array<TagInfo, 256> tags;
111     task_t task = mach_task_self();
112     mach_vm_size_t size;
113     uint32_t depth = 0;
114     struct vm_region_submap_info_64 info = { };
115     mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
116     for (mach_vm_address_t addr = 0; ; addr += size) {
117         int purgeableState;
118         if (mach_vm_purgable_control(task, addr, VM_PURGABLE_GET_STATE, &purgeableState) != KERN_SUCCESS)
119             purgeableState = VM_PURGABLE_DENY;
120
121         kern_return_t kr = mach_vm_region_recurse(task, &addr, &size, &depth, (vm_region_info_t)&info, &count);
122         if (kr != KERN_SUCCESS)
123             break;
124
125         if (purgeableState == VM_PURGABLE_VOLATILE) {
126             tags[info.user_tag].reclaimable += info.pages_resident;
127             continue;
128         }
129
130         if (purgeableState == VM_PURGABLE_EMPTY) {
131             tags[info.user_tag].reclaimable += size / vmPageSize();
132             continue;
133         }
134
135         bool anonymous = !info.external_pager;
136         if (anonymous) {
137             tags[info.user_tag].dirty += info.pages_resident - info.pages_reusable;
138             tags[info.user_tag].reclaimable += info.pages_reusable;
139         } else
140             tags[info.user_tag].dirty += info.pages_dirtied;
141     }
142
143     return tags;
144 }
145
146 static unsigned categoryForVMTag(unsigned tag)
147 {
148     switch (tag) {
149     case VM_MEMORY_IOKIT:
150     case VM_MEMORY_LAYERKIT:
151         return MemoryCategory::Layers;
152     case VM_MEMORY_IMAGEIO:
153     case VM_MEMORY_CGIMAGE:
154         return MemoryCategory::Images;
155     case VM_MEMORY_JAVASCRIPT_JIT_REGISTER_FILE:
156         return MemoryCategory::IsoHeap;
157     case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR:
158         return MemoryCategory::JSJIT;
159     case VM_MEMORY_JAVASCRIPT_CORE:
160         return MemoryCategory::Gigacage;
161     case VM_MEMORY_MALLOC:
162     case VM_MEMORY_MALLOC_HUGE:
163     case VM_MEMORY_MALLOC_LARGE:
164     case VM_MEMORY_MALLOC_SMALL:
165     case VM_MEMORY_MALLOC_TINY:
166     case VM_MEMORY_MALLOC_NANO:
167         return MemoryCategory::LibcMalloc;
168     case VM_MEMORY_TCMALLOC:
169         return MemoryCategory::bmalloc;
170     default:
171         return MemoryCategory::Other;
172     }
173 }
174
175 struct ThreadInfo {
176     MachSendRight sendRight;
177     float usage { 0 };
178     String threadName;
179     String dispatchQueueName;
180 };
181
182 static Vector<ThreadInfo> threadInfos()
183 {
184     thread_array_t threadList = nullptr;
185     mach_msg_type_number_t threadCount = 0;
186     kern_return_t kr = task_threads(mach_task_self(), &threadList, &threadCount);
187     ASSERT(kr == KERN_SUCCESS);
188     if (kr != KERN_SUCCESS)
189         return { };
190
191     Vector<ThreadInfo> infos;
192     for (mach_msg_type_number_t i = 0; i < threadCount; ++i) {
193         MachSendRight sendRight = MachSendRight::adopt(threadList[i]);
194
195         thread_info_data_t threadInfo;
196         mach_msg_type_number_t threadInfoCount = THREAD_INFO_MAX;
197         kr = thread_info(sendRight.sendRight(), THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&threadInfo), &threadInfoCount);
198         ASSERT(kr == KERN_SUCCESS);
199         if (kr != KERN_SUCCESS)
200             continue;
201
202         thread_identifier_info_data_t threadIdentifierInfo;
203         mach_msg_type_number_t threadIdentifierInfoCount = THREAD_IDENTIFIER_INFO_COUNT;
204         kr = thread_info(sendRight.sendRight(), THREAD_IDENTIFIER_INFO, reinterpret_cast<thread_info_t>(&threadIdentifierInfo), &threadIdentifierInfoCount);
205         ASSERT(kr == KERN_SUCCESS);
206         if (kr != KERN_SUCCESS)
207             continue;
208
209         thread_extended_info_data_t threadExtendedInfo;
210         mach_msg_type_number_t threadExtendedInfoCount = THREAD_EXTENDED_INFO_COUNT;
211         kr = thread_info(sendRight.sendRight(), THREAD_EXTENDED_INFO, reinterpret_cast<thread_info_t>(&threadExtendedInfo), &threadExtendedInfoCount);
212         ASSERT(kr == KERN_SUCCESS);
213         if (kr != KERN_SUCCESS)
214             continue;
215
216         float usage = 0;
217         auto threadBasicInfo = reinterpret_cast<thread_basic_info_t>(threadInfo);
218         if (!(threadBasicInfo->flags & TH_FLAGS_IDLE))
219             usage = threadBasicInfo->cpu_usage / static_cast<float>(TH_USAGE_SCALE) * 100.0;
220
221         String threadName = String(threadExtendedInfo.pth_name);
222         String dispatchQueueName;
223         if (threadIdentifierInfo.dispatch_qaddr) {
224             dispatch_queue_t queue = *reinterpret_cast<dispatch_queue_t*>(threadIdentifierInfo.dispatch_qaddr);
225             dispatchQueueName = String(dispatch_queue_get_label(queue));
226         }
227
228         infos.append(ThreadInfo { WTFMove(sendRight), usage, threadName, dispatchQueueName });
229     }
230
231     kr = vm_deallocate(mach_task_self(), (vm_offset_t)threadList, threadCount * sizeof(thread_t));
232     ASSERT(kr == KERN_SUCCESS);
233
234     return infos;
235 }
236
237 void ResourceUsageThread::platformSaveStateBeforeStarting()
238 {
239 #if ENABLE(SAMPLING_PROFILER)
240     m_samplingProfilerMachThread = m_vm->samplingProfiler() ? m_vm->samplingProfiler()->machThread() : MACH_PORT_NULL;
241 #endif
242 }
243
244 void ResourceUsageThread::platformCollectCPUData(JSC::VM*, ResourceUsageData& data)
245 {
246     Vector<ThreadInfo> threads = threadInfos();
247     if (threads.isEmpty()) {
248         ASSERT_NOT_REACHED();
249         return;
250     }
251
252     // Main thread is always first.
253     ASSERT(threads[0].dispatchQueueName == "com.apple.main-thread");
254
255     mach_port_t resourceUsageMachThread = mach_thread_self();
256     mach_port_t mainThreadMachThread = threads[0].sendRight.sendRight();
257
258     HashSet<mach_port_t> knownWebKitThreads;
259     {
260         LockHolder lock(Thread::allThreadsMutex());
261         for (auto* thread : Thread::allThreads(lock)) {
262             mach_port_t machThread = thread->machThread();
263             if (machThread != MACH_PORT_NULL)
264                 knownWebKitThreads.add(machThread);
265         }
266     }
267
268     HashMap<mach_port_t, String> knownWorkerThreads;
269     {
270         LockHolder lock(WorkerThread::workerThreadsMutex());
271         for (auto* thread : WorkerThread::workerThreads(lock)) {
272             mach_port_t machThread = thread->thread()->machThread();
273             if (machThread != MACH_PORT_NULL)
274                 knownWorkerThreads.set(machThread, thread->identifier().isolatedCopy());
275         }
276     }
277
278     auto isDebuggerThread = [&](const ThreadInfo& thread) -> bool {
279         mach_port_t machThread = thread.sendRight.sendRight();
280         if (machThread == resourceUsageMachThread)
281             return true;
282 #if ENABLE(SAMPLING_PROFILER)
283         if (machThread == m_samplingProfilerMachThread)
284             return true;
285 #endif
286         return false;
287     };
288
289     auto isWebKitThread = [&](const ThreadInfo& thread) -> bool {
290         mach_port_t machThread = thread.sendRight.sendRight();
291         if (knownWebKitThreads.contains(machThread))
292             return true;
293
294         // The bmalloc scavenger thread is below WTF. Detect it by its name.
295         if (thread.threadName == "JavaScriptCore bmalloc scavenger")
296             return true;
297
298         // WebKit uses many WorkQueues with common prefixes.
299         if (thread.dispatchQueueName.startsWith("com.apple.IPC.")
300             || thread.dispatchQueueName.startsWith("com.apple.WebKit.")
301             || thread.dispatchQueueName.startsWith("org.webkit."))
302             return true;
303
304         return false;
305     };
306
307     for (auto& thread : threads) {
308         data.cpu += thread.usage;
309         if (isDebuggerThread(thread))
310             continue;
311
312         data.cpuExcludingDebuggerThreads += thread.usage;
313
314         mach_port_t machThread = thread.sendRight.sendRight();
315         if (machThread == mainThreadMachThread) {
316             data.cpuThreads.append(ThreadCPUInfo { "Main Thread"_s, String(), thread.usage, ThreadCPUInfo::Type::Main});
317             continue;
318         }
319
320         String threadIdentifier = knownWorkerThreads.get(machThread);
321         bool isWorkerThread = !threadIdentifier.isEmpty();
322         ThreadCPUInfo::Type type = (isWorkerThread || isWebKitThread(thread)) ? ThreadCPUInfo::Type::WebKit : ThreadCPUInfo::Type::Unknown;
323         data.cpuThreads.append(ThreadCPUInfo { thread.threadName, threadIdentifier, thread.usage, type });
324     }
325 }
326
327 void ResourceUsageThread::platformCollectMemoryData(JSC::VM* vm, ResourceUsageData& data)
328 {
329     auto tags = pagesPerVMTag();
330     std::array<TagInfo, MemoryCategory::NumberOfCategories> pagesPerCategory;
331     size_t totalDirtyPages = 0;
332     for (unsigned i = 0; i < 256; ++i) {
333         pagesPerCategory[categoryForVMTag(i)].dirty += tags[i].dirty;
334         pagesPerCategory[categoryForVMTag(i)].reclaimable += tags[i].reclaimable;
335         totalDirtyPages += tags[i].dirty;
336     }
337
338     for (auto& category : data.categories) {
339         if (category.isSubcategory) // Only do automatic tallying for top-level categories.
340             continue;
341         category.dirtySize = pagesPerCategory[category.type].dirty * vmPageSize();
342         category.reclaimableSize = pagesPerCategory[category.type].reclaimable * vmPageSize();
343     }
344     data.totalDirtySize = totalDirtyPages * vmPageSize();
345
346     size_t currentGCHeapCapacity = vm->heap.blockBytesAllocated();
347     size_t currentGCOwnedExtra = vm->heap.extraMemorySize();
348     size_t currentGCOwnedExternal = vm->heap.externalMemorySize();
349     ASSERT(currentGCOwnedExternal <= currentGCOwnedExtra);
350
351     data.categories[MemoryCategory::GCHeap].dirtySize = currentGCHeapCapacity;
352     data.categories[MemoryCategory::GCOwned].dirtySize = currentGCOwnedExtra - currentGCOwnedExternal;
353     data.categories[MemoryCategory::GCOwned].externalSize = currentGCOwnedExternal;
354
355     auto& mallocBucket = isFastMallocEnabled() ? data.categories[MemoryCategory::bmalloc] : data.categories[MemoryCategory::LibcMalloc];
356
357     // First subtract memory allocated by the GC heap, since we track that separately.
358     mallocBucket.dirtySize -= currentGCHeapCapacity;
359
360     // It would be nice to assert that the "GC owned" amount is smaller than the total dirty malloc size,
361     // but since the "GC owned" accounting is inexact, it's not currently feasible.
362     size_t currentGCOwnedGenerallyInMalloc = currentGCOwnedExtra - currentGCOwnedExternal;
363     if (currentGCOwnedGenerallyInMalloc < mallocBucket.dirtySize)
364         mallocBucket.dirtySize -= currentGCOwnedGenerallyInMalloc;
365
366     data.totalExternalSize = currentGCOwnedExternal;
367
368     data.timeOfNextEdenCollection = data.timestamp + vm->heap.edenActivityCallback()->timeUntilFire().valueOr(Seconds(std::numeric_limits<double>::infinity()));
369     data.timeOfNextFullCollection = data.timestamp + vm->heap.fullActivityCallback()->timeUntilFire().valueOr(Seconds(std::numeric_limits<double>::infinity()));
370 }
371
372 }
373
374 #endif