8646a0ce49ab9ed18dc1cdf59ffab65e593817b9
[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_JIT_EXECUTABLE_ALLOCATOR: return "JSC JIT";
87     case VM_MEMORY_JAVASCRIPT_CORE: return "WebAssembly";
88     case VM_MEMORY_MALLOC: return "malloc";
89     case VM_MEMORY_MALLOC_HUGE: return "malloc (huge)";
90     case VM_MEMORY_MALLOC_LARGE: return "malloc (large)";
91     case VM_MEMORY_MALLOC_SMALL: return "malloc (small)";
92     case VM_MEMORY_MALLOC_TINY: return "malloc (tiny)";
93     case VM_MEMORY_MALLOC_NANO: return "malloc (nano)";
94     case VM_MEMORY_TCMALLOC: return "bmalloc";
95     case VM_MEMORY_FOUNDATION: return "Foundation";
96     case VM_MEMORY_STACK: return "Stack";
97     case VM_MEMORY_SQLITE: return "SQLite";
98     case VM_MEMORY_UNSHARED_PMAP: return "pmap (unshared)";
99     case VM_MEMORY_DYLIB: return "dylib";
100     case VM_MEMORY_CORESERVICES: return "CoreServices";
101     case VM_MEMORY_OS_ALLOC_ONCE: return "os_alloc_once";
102     case VM_MEMORY_LIBDISPATCH: return "libdispatch";
103     default: return nullptr;
104     }
105 }
106
107 std::array<TagInfo, 256> pagesPerVMTag()
108 {
109     std::array<TagInfo, 256> tags;
110     task_t task = mach_task_self();
111     mach_vm_size_t size;
112     uint32_t depth = 0;
113     struct vm_region_submap_info_64 info = { };
114     mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64;
115     for (mach_vm_address_t addr = 0; ; addr += size) {
116         int purgeableState;
117         if (mach_vm_purgable_control(task, addr, VM_PURGABLE_GET_STATE, &purgeableState) != KERN_SUCCESS)
118             purgeableState = VM_PURGABLE_DENY;
119
120         kern_return_t kr = mach_vm_region_recurse(task, &addr, &size, &depth, (vm_region_info_t)&info, &count);
121         if (kr != KERN_SUCCESS)
122             break;
123
124         if (purgeableState == VM_PURGABLE_VOLATILE) {
125             tags[info.user_tag].reclaimable += info.pages_resident;
126             continue;
127         }
128
129         if (purgeableState == VM_PURGABLE_EMPTY) {
130             tags[info.user_tag].reclaimable += size / vmPageSize();
131             continue;
132         }
133
134         bool anonymous = !info.external_pager;
135         if (anonymous) {
136             tags[info.user_tag].dirty += info.pages_resident - info.pages_reusable;
137             tags[info.user_tag].reclaimable += info.pages_reusable;
138         } else
139             tags[info.user_tag].dirty += info.pages_dirtied;
140     }
141
142     return tags;
143 }
144
145 static unsigned categoryForVMTag(unsigned tag)
146 {
147     switch (tag) {
148     case VM_MEMORY_IOKIT:
149     case VM_MEMORY_LAYERKIT:
150         return MemoryCategory::Layers;
151     case VM_MEMORY_IMAGEIO:
152     case VM_MEMORY_CGIMAGE:
153         return MemoryCategory::Images;
154     case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR:
155         return MemoryCategory::JSJIT;
156     case VM_MEMORY_JAVASCRIPT_CORE:
157         return MemoryCategory::WebAssembly;
158     case VM_MEMORY_MALLOC:
159     case VM_MEMORY_MALLOC_HUGE:
160     case VM_MEMORY_MALLOC_LARGE:
161     case VM_MEMORY_MALLOC_SMALL:
162     case VM_MEMORY_MALLOC_TINY:
163     case VM_MEMORY_MALLOC_NANO:
164         return MemoryCategory::LibcMalloc;
165     case VM_MEMORY_TCMALLOC:
166         return MemoryCategory::bmalloc;
167     default:
168         return MemoryCategory::Other;
169     }
170 }
171
172 struct ThreadInfo {
173     MachSendRight sendRight;
174     float usage { 0 };
175     String threadName;
176     String dispatchQueueName;
177 };
178
179 static Vector<ThreadInfo> threadInfos()
180 {
181     thread_array_t threadList = nullptr;
182     mach_msg_type_number_t threadCount = 0;
183     kern_return_t kr = task_threads(mach_task_self(), &threadList, &threadCount);
184     ASSERT(kr == KERN_SUCCESS);
185     if (kr != KERN_SUCCESS)
186         return { };
187
188     Vector<ThreadInfo> infos;
189     for (mach_msg_type_number_t i = 0; i < threadCount; ++i) {
190         MachSendRight sendRight = MachSendRight::adopt(threadList[i]);
191
192         thread_info_data_t threadInfo;
193         mach_msg_type_number_t threadInfoCount = THREAD_INFO_MAX;
194         kr = thread_info(sendRight.sendRight(), THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&threadInfo), &threadInfoCount);
195         ASSERT(kr == KERN_SUCCESS);
196         if (kr != KERN_SUCCESS)
197             continue;
198
199         thread_identifier_info_data_t threadIdentifierInfo;
200         mach_msg_type_number_t threadIdentifierInfoCount = THREAD_IDENTIFIER_INFO_COUNT;
201         kr = thread_info(sendRight.sendRight(), THREAD_IDENTIFIER_INFO, reinterpret_cast<thread_info_t>(&threadIdentifierInfo), &threadIdentifierInfoCount);
202         ASSERT(kr == KERN_SUCCESS);
203         if (kr != KERN_SUCCESS)
204             continue;
205
206         thread_extended_info_data_t threadExtendedInfo;
207         mach_msg_type_number_t threadExtendedInfoCount = THREAD_EXTENDED_INFO_COUNT;
208         kr = thread_info(sendRight.sendRight(), THREAD_EXTENDED_INFO, reinterpret_cast<thread_info_t>(&threadExtendedInfo), &threadExtendedInfoCount);
209         ASSERT(kr == KERN_SUCCESS);
210         if (kr != KERN_SUCCESS)
211             continue;
212
213         float usage = 0;
214         auto threadBasicInfo = reinterpret_cast<thread_basic_info_t>(threadInfo);
215         if (!(threadBasicInfo->flags & TH_FLAGS_IDLE))
216             usage = threadBasicInfo->cpu_usage / static_cast<float>(TH_USAGE_SCALE) * 100.0;
217
218         String threadName = String(threadExtendedInfo.pth_name);
219         String dispatchQueueName;
220         if (threadIdentifierInfo.dispatch_qaddr) {
221             dispatch_queue_t queue = *reinterpret_cast<dispatch_queue_t*>(threadIdentifierInfo.dispatch_qaddr);
222             dispatchQueueName = String(dispatch_queue_get_label(queue));
223         }
224
225         infos.append(ThreadInfo { WTFMove(sendRight), usage, threadName, dispatchQueueName });
226     }
227
228     kr = vm_deallocate(mach_task_self(), (vm_offset_t)threadList, threadCount * sizeof(thread_t));
229     ASSERT(kr == KERN_SUCCESS);
230
231     return infos;
232 }
233
234 void ResourceUsageThread::platformSaveStateBeforeStarting()
235 {
236 #if ENABLE(SAMPLING_PROFILER)
237     m_samplingProfilerMachThread = m_vm->samplingProfiler() ? m_vm->samplingProfiler()->machThread() : MACH_PORT_NULL;
238 #endif
239 }
240
241 void ResourceUsageThread::platformCollectCPUData(JSC::VM*, ResourceUsageData& data)
242 {
243     Vector<ThreadInfo> threads = threadInfos();
244     if (threads.isEmpty()) {
245         ASSERT_NOT_REACHED();
246         return;
247     }
248
249     // Main thread is always first.
250     ASSERT(threads[0].dispatchQueueName == "com.apple.main-thread");
251
252     mach_port_t resourceUsageMachThread = mach_thread_self();
253     mach_port_t mainThreadMachThread = threads[0].sendRight.sendRight();
254
255     HashSet<mach_port_t> knownWebKitThreads;
256     {
257         LockHolder lock(Thread::allThreadsMutex());
258         for (auto* thread : Thread::allThreads(lock)) {
259             mach_port_t machThread = thread->machThread();
260             if (machThread != MACH_PORT_NULL)
261                 knownWebKitThreads.add(machThread);
262         }
263     }
264
265     HashMap<mach_port_t, String> knownWorkerThreads;
266     {
267         LockHolder lock(WorkerThread::workerThreadsMutex());
268         for (auto* thread : WorkerThread::workerThreads(lock)) {
269             mach_port_t machThread = thread->thread()->machThread();
270             if (machThread != MACH_PORT_NULL)
271                 knownWorkerThreads.set(machThread, thread->identifier().isolatedCopy());
272         }
273     }
274
275     auto isDebuggerThread = [&](const ThreadInfo& thread) -> bool {
276         mach_port_t machThread = thread.sendRight.sendRight();
277         if (machThread == resourceUsageMachThread)
278             return true;
279 #if ENABLE(SAMPLING_PROFILER)
280         if (machThread == m_samplingProfilerMachThread)
281             return true;
282 #endif
283         return false;
284     };
285
286     auto isWebKitThread = [&](const ThreadInfo& thread) -> bool {
287         mach_port_t machThread = thread.sendRight.sendRight();
288         if (knownWebKitThreads.contains(machThread))
289             return true;
290
291         // The bmalloc scavenger thread is below WTF. Detect it by its name.
292         if (thread.threadName == "JavaScriptCore bmalloc scavenger")
293             return true;
294
295         // WebKit uses many WorkQueues with common prefixes.
296         if (thread.dispatchQueueName.startsWith("com.apple.IPC.")
297             || thread.dispatchQueueName.startsWith("com.apple.WebKit.")
298             || thread.dispatchQueueName.startsWith("org.webkit."))
299             return true;
300
301         return false;
302     };
303
304     for (auto& thread : threads) {
305         data.cpu += thread.usage;
306         if (isDebuggerThread(thread))
307             continue;
308
309         data.cpuExcludingDebuggerThreads += thread.usage;
310
311         mach_port_t machThread = thread.sendRight.sendRight();
312         if (machThread == mainThreadMachThread) {
313             data.cpuThreads.append(ThreadCPUInfo { "Main Thread"_s, String(), thread.usage, ThreadCPUInfo::Type::Main});
314             continue;
315         }
316
317         String threadIdentifier = knownWorkerThreads.get(machThread);
318         bool isWorkerThread = !threadIdentifier.isEmpty();
319         ThreadCPUInfo::Type type = (isWorkerThread || isWebKitThread(thread)) ? ThreadCPUInfo::Type::WebKit : ThreadCPUInfo::Type::Unknown;
320         data.cpuThreads.append(ThreadCPUInfo { thread.threadName, threadIdentifier, thread.usage, type });
321     }
322 }
323
324 void ResourceUsageThread::platformCollectMemoryData(JSC::VM* vm, ResourceUsageData& data)
325 {
326     auto tags = pagesPerVMTag();
327     std::array<TagInfo, MemoryCategory::NumberOfCategories> pagesPerCategory;
328     size_t totalDirtyPages = 0;
329     for (unsigned i = 0; i < 256; ++i) {
330         pagesPerCategory[categoryForVMTag(i)].dirty += tags[i].dirty;
331         pagesPerCategory[categoryForVMTag(i)].reclaimable += tags[i].reclaimable;
332         totalDirtyPages += tags[i].dirty;
333     }
334
335     for (auto& category : data.categories) {
336         if (category.isSubcategory) // Only do automatic tallying for top-level categories.
337             continue;
338         category.dirtySize = pagesPerCategory[category.type].dirty * vmPageSize();
339         category.reclaimableSize = pagesPerCategory[category.type].reclaimable * vmPageSize();
340     }
341     data.totalDirtySize = totalDirtyPages * vmPageSize();
342
343     size_t currentGCHeapCapacity = vm->heap.blockBytesAllocated();
344     size_t currentGCOwnedExtra = vm->heap.extraMemorySize();
345     size_t currentGCOwnedExternal = vm->heap.externalMemorySize();
346     ASSERT(currentGCOwnedExternal <= currentGCOwnedExtra);
347
348     data.categories[MemoryCategory::GCHeap].dirtySize = currentGCHeapCapacity;
349     data.categories[MemoryCategory::GCOwned].dirtySize = currentGCOwnedExtra - currentGCOwnedExternal;
350     data.categories[MemoryCategory::GCOwned].externalSize = currentGCOwnedExternal;
351
352     auto& mallocBucket = isFastMallocEnabled() ? data.categories[MemoryCategory::bmalloc] : data.categories[MemoryCategory::LibcMalloc];
353
354     // First subtract memory allocated by the GC heap, since we track that separately.
355     mallocBucket.dirtySize -= currentGCHeapCapacity;
356
357     // It would be nice to assert that the "GC owned" amount is smaller than the total dirty malloc size,
358     // but since the "GC owned" accounting is inexact, it's not currently feasible.
359     size_t currentGCOwnedGenerallyInMalloc = currentGCOwnedExtra - currentGCOwnedExternal;
360     if (currentGCOwnedGenerallyInMalloc < mallocBucket.dirtySize)
361         mallocBucket.dirtySize -= currentGCOwnedGenerallyInMalloc;
362
363     data.totalExternalSize = currentGCOwnedExternal;
364
365     data.timeOfNextEdenCollection = data.timestamp + vm->heap.edenActivityCallback()->timeUntilFire().valueOr(Seconds(std::numeric_limits<double>::infinity()));
366     data.timeOfNextFullCollection = data.timestamp + vm->heap.fullActivityCallback()->timeUntilFire().valueOr(Seconds(std::numeric_limits<double>::infinity()));
367 }
368
369 }
370
371 #endif