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 | #include "config.h" |
27 | #include "ResourceUsageThread.h" |
28 | |
29 | #if ENABLE(RESOURCE_USAGE) |
30 | |
31 | #include "CommonVM.h" |
32 | #include "JSDOMWindow.h" |
33 | #include <thread> |
34 | #include <wtf/MainThread.h> |
35 | #include <wtf/Vector.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | ResourceUsageThread::ResourceUsageThread() |
40 | { |
41 | } |
42 | |
43 | ResourceUsageThread& ResourceUsageThread::singleton() |
44 | { |
45 | static NeverDestroyed<ResourceUsageThread> resourceUsageThread; |
46 | return resourceUsageThread; |
47 | } |
48 | |
49 | void ResourceUsageThread::addObserver(void* key, ResourceUsageCollectionMode mode, std::function<void (const ResourceUsageData&)> function) |
50 | { |
51 | auto& resourceUsageThread = ResourceUsageThread::singleton(); |
52 | resourceUsageThread.createThreadIfNeeded(); |
53 | |
54 | { |
55 | LockHolder locker(resourceUsageThread.m_lock); |
56 | bool wasEmpty = resourceUsageThread.m_observers.isEmpty(); |
57 | resourceUsageThread.m_observers.set(key, std::make_pair(mode, function)); |
58 | |
59 | resourceUsageThread.recomputeCollectionMode(); |
60 | |
61 | if (wasEmpty) { |
62 | resourceUsageThread.platformSaveStateBeforeStarting(); |
63 | resourceUsageThread.m_condition.notifyAll(); |
64 | } |
65 | } |
66 | } |
67 | |
68 | void ResourceUsageThread::removeObserver(void* key) |
69 | { |
70 | auto& resourceUsageThread = ResourceUsageThread::singleton(); |
71 | |
72 | { |
73 | LockHolder locker(resourceUsageThread.m_lock); |
74 | resourceUsageThread.m_observers.remove(key); |
75 | |
76 | resourceUsageThread.recomputeCollectionMode(); |
77 | } |
78 | } |
79 | |
80 | void ResourceUsageThread::waitUntilObservers() |
81 | { |
82 | LockHolder locker(m_lock); |
83 | while (m_observers.isEmpty()) { |
84 | m_condition.wait(m_lock); |
85 | |
86 | // Wait a bit after waking up for the first time. |
87 | WTF::sleep(10_ms); |
88 | } |
89 | } |
90 | |
91 | void ResourceUsageThread::notifyObservers(ResourceUsageData&& data) |
92 | { |
93 | callOnMainThread([data = WTFMove(data)]() mutable { |
94 | Vector<std::pair<ResourceUsageCollectionMode, std::function<void (const ResourceUsageData&)>>> pairs; |
95 | |
96 | { |
97 | auto& resourceUsageThread = ResourceUsageThread::singleton(); |
98 | LockHolder locker(resourceUsageThread.m_lock); |
99 | pairs = copyToVector(resourceUsageThread.m_observers.values()); |
100 | } |
101 | |
102 | for (auto& pair : pairs) |
103 | pair.second(data); |
104 | }); |
105 | } |
106 | |
107 | void ResourceUsageThread::recomputeCollectionMode() |
108 | { |
109 | m_collectionMode = None; |
110 | |
111 | for (auto& pair : m_observers.values()) |
112 | m_collectionMode = static_cast<ResourceUsageCollectionMode>(m_collectionMode | pair.first); |
113 | } |
114 | |
115 | void ResourceUsageThread::createThreadIfNeeded() |
116 | { |
117 | if (m_thread) |
118 | return; |
119 | |
120 | m_vm = &commonVM(); |
121 | m_thread = Thread::create("WebCore: ResourceUsage" , [this] { |
122 | threadBody(); |
123 | }); |
124 | } |
125 | |
126 | NO_RETURN void ResourceUsageThread::threadBody() |
127 | { |
128 | // Wait a bit after waking up for the first time. |
129 | WTF::sleep(10_ms); |
130 | |
131 | while (true) { |
132 | // Only do work if we have observers. |
133 | waitUntilObservers(); |
134 | |
135 | auto start = WallTime::now(); |
136 | |
137 | ResourceUsageData data; |
138 | ResourceUsageCollectionMode mode = m_collectionMode; |
139 | if (mode & CPU) |
140 | platformCollectCPUData(m_vm, data); |
141 | if (mode & Memory) |
142 | platformCollectMemoryData(m_vm, data); |
143 | |
144 | notifyObservers(WTFMove(data)); |
145 | |
146 | // NOTE: Web Inspector expects this interval to be 500ms (CPU / Memory timelines), |
147 | // so if this interval changes Web Inspector may need to change. |
148 | auto duration = WallTime::now() - start; |
149 | auto difference = 500_ms - duration; |
150 | WTF::sleep(difference); |
151 | } |
152 | } |
153 | |
154 | } // namespace WebCore |
155 | |
156 | #endif // ENABLE(RESOURCE_USAGE) |
157 | |