| 1 | /* |
| 2 | * Copyright (C) 2016 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(RESOURCE_USAGE) |
| 29 | |
| 30 | #include "ResourceUsageData.h" |
| 31 | #include <array> |
| 32 | #include <functional> |
| 33 | #include <wtf/Condition.h> |
| 34 | #include <wtf/HashMap.h> |
| 35 | #include <wtf/Lock.h> |
| 36 | #include <wtf/NeverDestroyed.h> |
| 37 | #include <wtf/Noncopyable.h> |
| 38 | #include <wtf/Threading.h> |
| 39 | |
| 40 | #if OS(DARWIN) |
| 41 | #include <mach/mach.h> |
| 42 | #endif |
| 43 | |
| 44 | namespace JSC { |
| 45 | class VM; |
| 46 | } |
| 47 | |
| 48 | namespace WebCore { |
| 49 | |
| 50 | enum ResourceUsageCollectionMode { |
| 51 | None = 0, |
| 52 | CPU = 1 << 0, |
| 53 | Memory = 1 << 1, |
| 54 | All = CPU | Memory, |
| 55 | }; |
| 56 | |
| 57 | class ResourceUsageThread { |
| 58 | WTF_MAKE_NONCOPYABLE(ResourceUsageThread); |
| 59 | |
| 60 | public: |
| 61 | static void addObserver(void* key, ResourceUsageCollectionMode, std::function<void (const ResourceUsageData&)>); |
| 62 | static void removeObserver(void* key); |
| 63 | |
| 64 | private: |
| 65 | friend NeverDestroyed<ResourceUsageThread>; |
| 66 | ResourceUsageThread(); |
| 67 | static ResourceUsageThread& singleton(); |
| 68 | |
| 69 | void waitUntilObservers(); |
| 70 | void notifyObservers(ResourceUsageData&&); |
| 71 | |
| 72 | void recomputeCollectionMode(); |
| 73 | |
| 74 | void createThreadIfNeeded(); |
| 75 | void threadBody(); |
| 76 | |
| 77 | void platformSaveStateBeforeStarting(); |
| 78 | void platformCollectCPUData(JSC::VM*, ResourceUsageData&); |
| 79 | void platformCollectMemoryData(JSC::VM*, ResourceUsageData&); |
| 80 | |
| 81 | RefPtr<Thread> m_thread; |
| 82 | Lock m_lock; |
| 83 | Condition m_condition; |
| 84 | HashMap<void*, std::pair<ResourceUsageCollectionMode, std::function<void (const ResourceUsageData&)>>> m_observers; |
| 85 | ResourceUsageCollectionMode m_collectionMode { None }; |
| 86 | |
| 87 | // Platforms may need to access some data from the common VM. |
| 88 | // They should ensure their use of the VM is thread safe. |
| 89 | JSC::VM* m_vm { nullptr }; |
| 90 | |
| 91 | #if ENABLE(SAMPLING_PROFILER) && OS(DARWIN) |
| 92 | mach_port_t m_samplingProfilerMachThread { MACH_PORT_NULL }; |
| 93 | #endif |
| 94 | |
| 95 | }; |
| 96 | |
| 97 | #if PLATFORM(COCOA) |
| 98 | struct TagInfo { |
| 99 | TagInfo() { } |
| 100 | size_t dirty { 0 }; |
| 101 | size_t reclaimable { 0 }; |
| 102 | }; |
| 103 | |
| 104 | const char* displayNameForVMTag(unsigned); |
| 105 | size_t vmPageSize(); |
| 106 | std::array<TagInfo, 256> pagesPerVMTag(); |
| 107 | void logFootprintComparison(const std::array<TagInfo, 256>&, const std::array<TagInfo, 256>&); |
| 108 | #endif |
| 109 | |
| 110 | } // namespace WebCore |
| 111 | |
| 112 | #endif // ENABLE(RESOURCE_USAGE) |
| 113 | |