| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2011, Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/config.h" | |
| 32 #include "sky/engine/bindings/core/v8/ScriptProfiler.h" | |
| 33 | |
| 34 #include "bindings/core/v8/V8Node.h" | |
| 35 #include "bindings/core/v8/V8Window.h" | |
| 36 #include "sky/engine/bindings/core/v8/RetainedDOMInfo.h" | |
| 37 #include "sky/engine/bindings/core/v8/ScriptValue.h" | |
| 38 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 39 #include "sky/engine/bindings/core/v8/WrapperTypeInfo.h" | |
| 40 #include "sky/engine/core/dom/Document.h" | |
| 41 #include "sky/engine/wtf/ThreadSpecific.h" | |
| 42 #include "v8/include/v8-profiler.h" | |
| 43 #include "v8/include/v8.h" | |
| 44 | |
| 45 namespace blink { | |
| 46 | |
| 47 typedef HashMap<String, double> ProfileNameIdleTimeMap; | |
| 48 | |
| 49 void ScriptProfiler::setSamplingInterval(int intervalUs) | |
| 50 { | |
| 51 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 52 v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); | |
| 53 if (profiler) | |
| 54 profiler->SetSamplingInterval(intervalUs); | |
| 55 } | |
| 56 | |
| 57 void ScriptProfiler::start(const String& title) | |
| 58 { | |
| 59 ProfileNameIdleTimeMap* profileNameIdleTimeMap = ScriptProfiler::currentProf
ileNameIdleTimeMap(); | |
| 60 if (profileNameIdleTimeMap->contains(title)) | |
| 61 return; | |
| 62 profileNameIdleTimeMap->add(title, 0); | |
| 63 | |
| 64 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 65 v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); | |
| 66 if (!profiler) | |
| 67 return; | |
| 68 v8::HandleScope handleScope(isolate); | |
| 69 profiler->StartProfiling(v8String(isolate, title), true); | |
| 70 } | |
| 71 | |
| 72 PassRefPtr<ScriptProfile> ScriptProfiler::stop(const String& title) | |
| 73 { | |
| 74 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 75 v8::CpuProfiler* profiler = isolate->GetCpuProfiler(); | |
| 76 if (!profiler) | |
| 77 return nullptr; | |
| 78 v8::HandleScope handleScope(isolate); | |
| 79 v8::CpuProfile* profile = profiler->StopProfiling(v8String(isolate, title)); | |
| 80 if (!profile) | |
| 81 return nullptr; | |
| 82 | |
| 83 String profileTitle = toCoreString(profile->GetTitle()); | |
| 84 double idleTime = 0.0; | |
| 85 ProfileNameIdleTimeMap* profileNameIdleTimeMap = ScriptProfiler::currentProf
ileNameIdleTimeMap(); | |
| 86 ProfileNameIdleTimeMap::iterator profileIdleTime = profileNameIdleTimeMap->f
ind(profileTitle); | |
| 87 if (profileIdleTime != profileNameIdleTimeMap->end()) { | |
| 88 idleTime = profileIdleTime->value * 1000.0; | |
| 89 profileNameIdleTimeMap->remove(profileIdleTime); | |
| 90 } | |
| 91 | |
| 92 return ScriptProfile::create(profile, idleTime); | |
| 93 } | |
| 94 | |
| 95 void ScriptProfiler::collectGarbage() | |
| 96 { | |
| 97 v8::Isolate::GetCurrent()->LowMemoryNotification(); | |
| 98 } | |
| 99 | |
| 100 ScriptValue ScriptProfiler::objectByHeapObjectId(unsigned id) | |
| 101 { | |
| 102 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 103 v8::HeapProfiler* profiler = isolate->GetHeapProfiler(); | |
| 104 v8::HandleScope handleScope(isolate); | |
| 105 v8::Handle<v8::Value> value = profiler->FindObjectById(id); | |
| 106 if (value.IsEmpty() || !value->IsObject()) | |
| 107 return ScriptValue(); | |
| 108 | |
| 109 v8::Handle<v8::Object> object = value.As<v8::Object>(); | |
| 110 | |
| 111 if (object->InternalFieldCount() >= v8DefaultWrapperInternalFieldCount) { | |
| 112 v8::Handle<v8::Value> wrapper = object->GetInternalField(v8DOMWrapperObj
ectIndex); | |
| 113 // Skip wrapper boilerplates which are like regular wrappers but don't h
ave | |
| 114 // native object. | |
| 115 if (!wrapper.IsEmpty() && wrapper->IsUndefined()) | |
| 116 return ScriptValue(); | |
| 117 } | |
| 118 | |
| 119 ScriptState* scriptState = ScriptState::from(object->CreationContext()); | |
| 120 return ScriptValue(scriptState, object); | |
| 121 } | |
| 122 | |
| 123 unsigned ScriptProfiler::getHeapObjectId(const ScriptValue& value) | |
| 124 { | |
| 125 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 126 v8::HeapProfiler* profiler = isolate->GetHeapProfiler(); | |
| 127 v8::SnapshotObjectId id = profiler->GetObjectId(value.v8Value()); | |
| 128 return id; | |
| 129 } | |
| 130 | |
| 131 void ScriptProfiler::clearHeapObjectIds() | |
| 132 { | |
| 133 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 134 v8::HeapProfiler* profiler = isolate->GetHeapProfiler(); | |
| 135 profiler->ClearObjectIds(); | |
| 136 } | |
| 137 | |
| 138 namespace { | |
| 139 | |
| 140 class ActivityControlAdapter final : public v8::ActivityControl { | |
| 141 public: | |
| 142 ActivityControlAdapter(ScriptProfiler::HeapSnapshotProgress* progress) | |
| 143 : m_progress(progress), m_firstReport(true) { } | |
| 144 virtual ControlOption ReportProgressValue(int done, int total) override | |
| 145 { | |
| 146 ControlOption result = m_progress->isCanceled() ? kAbort : kContinue; | |
| 147 if (m_firstReport) { | |
| 148 m_firstReport = false; | |
| 149 m_progress->Start(total); | |
| 150 } else { | |
| 151 m_progress->Worked(done); | |
| 152 } | |
| 153 if (done >= total) | |
| 154 m_progress->Done(); | |
| 155 return result; | |
| 156 } | |
| 157 private: | |
| 158 ScriptProfiler::HeapSnapshotProgress* m_progress; | |
| 159 bool m_firstReport; | |
| 160 }; | |
| 161 | |
| 162 class GlobalObjectNameResolver final : public v8::HeapProfiler::ObjectNameResolv
er { | |
| 163 public: | |
| 164 virtual const char* GetName(v8::Handle<v8::Object> object) override | |
| 165 { | |
| 166 LocalDOMWindow* window = toDOMWindow(object, v8::Isolate::GetCurrent()); | |
| 167 if (!window) | |
| 168 return 0; | |
| 169 CString url = window->document()->url().string().utf8(); | |
| 170 m_strings.append(url); | |
| 171 return url.data(); | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 Vector<CString> m_strings; | |
| 176 }; | |
| 177 | |
| 178 } // namespace | |
| 179 | |
| 180 void ScriptProfiler::startTrackingHeapObjects(bool trackAllocations) | |
| 181 { | |
| 182 v8::Isolate::GetCurrent()->GetHeapProfiler()->StartTrackingHeapObjects(track
Allocations); | |
| 183 } | |
| 184 | |
| 185 namespace { | |
| 186 | |
| 187 class HeapStatsStream : public v8::OutputStream { | |
| 188 public: | |
| 189 HeapStatsStream(ScriptProfiler::OutputStream* stream) : m_stream(stream) { } | |
| 190 virtual void EndOfStream() override { } | |
| 191 | |
| 192 virtual WriteResult WriteAsciiChunk(char* data, int size) override | |
| 193 { | |
| 194 ASSERT(false); | |
| 195 return kAbort; | |
| 196 } | |
| 197 | |
| 198 virtual WriteResult WriteHeapStatsChunk(v8::HeapStatsUpdate* updateData, int
count) override | |
| 199 { | |
| 200 Vector<uint32_t> rawData(count * 3); | |
| 201 for (int i = 0; i < count; ++i) { | |
| 202 int offset = i * 3; | |
| 203 rawData[offset] = updateData[i].index; | |
| 204 rawData[offset + 1] = updateData[i].count; | |
| 205 rawData[offset + 2] = updateData[i].size; | |
| 206 } | |
| 207 m_stream->write(rawData.data(), rawData.size()); | |
| 208 return kContinue; | |
| 209 } | |
| 210 | |
| 211 private: | |
| 212 ScriptProfiler::OutputStream* m_stream; | |
| 213 }; | |
| 214 | |
| 215 } | |
| 216 | |
| 217 unsigned ScriptProfiler::requestHeapStatsUpdate(ScriptProfiler::OutputStream* st
ream) | |
| 218 { | |
| 219 HeapStatsStream heapStatsStream(stream); | |
| 220 return v8::Isolate::GetCurrent()->GetHeapProfiler()->GetHeapStats(&heapStats
Stream); | |
| 221 } | |
| 222 | |
| 223 void ScriptProfiler::stopTrackingHeapObjects() | |
| 224 { | |
| 225 v8::Isolate::GetCurrent()->GetHeapProfiler()->StopTrackingHeapObjects(); | |
| 226 } | |
| 227 | |
| 228 // FIXME: This method should receive a ScriptState, from which we should retriev
e an Isolate. | |
| 229 PassRefPtr<ScriptHeapSnapshot> ScriptProfiler::takeHeapSnapshot(const String& ti
tle, HeapSnapshotProgress* control) | |
| 230 { | |
| 231 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 232 v8::HeapProfiler* profiler = isolate->GetHeapProfiler(); | |
| 233 if (!profiler) | |
| 234 return nullptr; | |
| 235 v8::HandleScope handleScope(isolate); | |
| 236 ASSERT(control); | |
| 237 ActivityControlAdapter adapter(control); | |
| 238 GlobalObjectNameResolver resolver; | |
| 239 const v8::HeapSnapshot* snapshot = profiler->TakeHeapSnapshot(v8String(isola
te, title), &adapter, &resolver); | |
| 240 return snapshot ? ScriptHeapSnapshot::create(snapshot) : nullptr; | |
| 241 } | |
| 242 | |
| 243 static v8::RetainedObjectInfo* retainedDOMInfo(uint16_t classId, v8::Handle<v8::
Value> wrapper) | |
| 244 { | |
| 245 ASSERT(classId == WrapperTypeInfo::NodeClassId); | |
| 246 if (!wrapper->IsObject()) | |
| 247 return 0; | |
| 248 Node* node = V8Node::toNative(wrapper.As<v8::Object>()); | |
| 249 return node ? new RetainedDOMInfo(node) : 0; | |
| 250 } | |
| 251 | |
| 252 void ScriptProfiler::initialize() | |
| 253 { | |
| 254 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 255 v8::HeapProfiler* profiler = isolate->GetHeapProfiler(); | |
| 256 if (profiler) | |
| 257 profiler->SetWrapperClassInfoProvider(WrapperTypeInfo::NodeClassId, &ret
ainedDOMInfo); | |
| 258 } | |
| 259 | |
| 260 ProfileNameIdleTimeMap* ScriptProfiler::currentProfileNameIdleTimeMap() | |
| 261 { | |
| 262 AtomicallyInitializedStatic(WTF::ThreadSpecific<ProfileNameIdleTimeMap>*, ma
p = new WTF::ThreadSpecific<ProfileNameIdleTimeMap>); | |
| 263 return *map; | |
| 264 } | |
| 265 | |
| 266 void ScriptProfiler::setIdle(bool isIdle) | |
| 267 { | |
| 268 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 269 if (v8::CpuProfiler* profiler = isolate->GetCpuProfiler()) | |
| 270 profiler->SetIdle(isIdle); | |
| 271 } | |
| 272 | |
| 273 } // namespace blink | |
| OLD | NEW |