[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gin/public/v8_platform.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/location.h" |
mlippautz | 40dfecc9 | 2016-03-07 21:06:50 | [diff] [blame] | 9 | #include "base/sys_info.h" |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 10 | #include "base/threading/worker_pool.h" |
tmoniuszko | 3641693 | 2016-01-27 16:28:45 | [diff] [blame] | 11 | #include "base/trace_event/trace_event.h" |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 12 | #include "gin/per_isolate_data.h" |
| 13 | |
| 14 | namespace gin { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER; |
| 19 | |
jochen | 76acff10 | 2016-11-08 08:20:37 | [diff] [blame^] | 20 | void RunWithLocker(v8::Isolate* isolate, v8::Task* task) { |
| 21 | v8::Locker lock(isolate); |
| 22 | task->Run(); |
| 23 | } |
| 24 | |
| 25 | class IdleTaskWithLocker : public v8::IdleTask { |
| 26 | public: |
| 27 | IdleTaskWithLocker(v8::Isolate* isolate, v8::IdleTask* task) |
| 28 | : isolate_(isolate), task_(task) {} |
| 29 | |
| 30 | ~IdleTaskWithLocker() override = default; |
| 31 | |
| 32 | // v8::IdleTask implementation. |
| 33 | void Run(double deadline_in_seconds) override { |
| 34 | v8::Locker lock(isolate_); |
| 35 | task_->Run(deadline_in_seconds); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | v8::Isolate* isolate_; |
| 40 | std::unique_ptr<v8::IdleTask> task_; |
| 41 | |
| 42 | DISALLOW_COPY_AND_ASSIGN(IdleTaskWithLocker); |
| 43 | }; |
| 44 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | // static |
| 48 | V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } |
| 49 | |
| 50 | V8Platform::V8Platform() {} |
| 51 | |
| 52 | V8Platform::~V8Platform() {} |
| 53 | |
mlippautz | 40dfecc9 | 2016-03-07 21:06:50 | [diff] [blame] | 54 | size_t V8Platform::NumberOfAvailableBackgroundThreads() { |
| 55 | // WorkerPool will currently always create additional threads for posted |
| 56 | // background tasks, unless there are threads sitting idle (on posix). |
| 57 | // Indicate that V8 should create no more than the number of cores available, |
| 58 | // reserving one core for the main thread. |
| 59 | const size_t available_cores = |
| 60 | static_cast<size_t>(base::SysInfo::NumberOfProcessors()); |
| 61 | if (available_cores > 1) { |
| 62 | return available_cores - 1; |
| 63 | } |
| 64 | return 1; |
| 65 | } |
| 66 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 67 | void V8Platform::CallOnBackgroundThread( |
| 68 | v8::Task* task, |
| 69 | v8::Platform::ExpectedRuntime expected_runtime) { |
| 70 | base::WorkerPool::PostTask( |
| 71 | FROM_HERE, |
| 72 | base::Bind(&v8::Task::Run, base::Owned(task)), |
| 73 | expected_runtime == v8::Platform::kLongRunningTask); |
| 74 | } |
| 75 | |
| 76 | void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { |
jochen | 76acff10 | 2016-11-08 08:20:37 | [diff] [blame^] | 77 | PerIsolateData* data = PerIsolateData::From(isolate); |
| 78 | if (data->access_mode() == IsolateHolder::kUseLocker) { |
| 79 | data->task_runner()->PostTask( |
| 80 | FROM_HERE, base::Bind(RunWithLocker, base::Unretained(isolate), |
| 81 | base::Owned(task))); |
| 82 | } else { |
| 83 | data->task_runner()->PostTask( |
| 84 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task))); |
| 85 | } |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 86 | } |
| 87 | |
ulan | 49cd773c | 2015-06-24 16:38:08 | [diff] [blame] | 88 | void V8Platform::CallDelayedOnForegroundThread(v8::Isolate* isolate, |
| 89 | v8::Task* task, |
| 90 | double delay_in_seconds) { |
jochen | 76acff10 | 2016-11-08 08:20:37 | [diff] [blame^] | 91 | PerIsolateData* data = PerIsolateData::From(isolate); |
| 92 | if (data->access_mode() == IsolateHolder::kUseLocker) { |
| 93 | data->task_runner()->PostDelayedTask( |
| 94 | FROM_HERE, |
| 95 | base::Bind(RunWithLocker, base::Unretained(isolate), base::Owned(task)), |
| 96 | base::TimeDelta::FromSecondsD(delay_in_seconds)); |
| 97 | } else { |
| 98 | data->task_runner()->PostDelayedTask( |
| 99 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)), |
| 100 | base::TimeDelta::FromSecondsD(delay_in_seconds)); |
| 101 | } |
ulan | 49cd773c | 2015-06-24 16:38:08 | [diff] [blame] | 102 | } |
| 103 | |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 104 | void V8Platform::CallIdleOnForegroundThread(v8::Isolate* isolate, |
| 105 | v8::IdleTask* task) { |
jochen | 76acff10 | 2016-11-08 08:20:37 | [diff] [blame^] | 106 | PerIsolateData* data = PerIsolateData::From(isolate); |
| 107 | DCHECK(data->idle_task_runner()); |
| 108 | if (data->access_mode() == IsolateHolder::kUseLocker) { |
| 109 | data->idle_task_runner()->PostIdleTask( |
| 110 | new IdleTaskWithLocker(isolate, task)); |
| 111 | } else { |
| 112 | data->idle_task_runner()->PostIdleTask(task); |
| 113 | } |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool V8Platform::IdleTasksEnabled(v8::Isolate* isolate) { |
| 117 | return PerIsolateData::From(isolate)->idle_task_runner() != nullptr; |
| 118 | } |
| 119 | |
rmcilroy | 05d2662 | 2014-10-08 11:28:05 | [diff] [blame] | 120 | double V8Platform::MonotonicallyIncreasingTime() { |
| 121 | return base::TimeTicks::Now().ToInternalValue() / |
| 122 | static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 123 | } |
| 124 | |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 125 | const uint8_t* V8Platform::GetCategoryGroupEnabled(const char* name) { |
| 126 | return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(name); |
| 127 | } |
| 128 | |
| 129 | const char* V8Platform::GetCategoryGroupName( |
| 130 | const uint8_t* category_enabled_flag) { |
| 131 | return base::trace_event::TraceLog::GetCategoryGroupName( |
| 132 | category_enabled_flag); |
| 133 | } |
| 134 | |
alph | 92d855a4 | 2016-09-28 09:34:52 | [diff] [blame] | 135 | namespace { |
| 136 | |
| 137 | class ConvertableToTraceFormatWrapper |
| 138 | : public base::trace_event::ConvertableToTraceFormat { |
| 139 | public: |
| 140 | explicit ConvertableToTraceFormatWrapper( |
| 141 | std::unique_ptr<v8::ConvertableToTraceFormat>& inner) |
| 142 | : inner_(std::move(inner)) {} |
| 143 | ~ConvertableToTraceFormatWrapper() override = default; |
| 144 | void AppendAsTraceFormat(std::string* out) const final { |
| 145 | inner_->AppendAsTraceFormat(out); |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | std::unique_ptr<v8::ConvertableToTraceFormat> inner_; |
| 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(ConvertableToTraceFormatWrapper); |
| 152 | }; |
| 153 | |
| 154 | } // namespace |
| 155 | |
| 156 | uint64_t V8Platform::AddTraceEvent( |
| 157 | char phase, |
| 158 | const uint8_t* category_enabled_flag, |
| 159 | const char* name, |
| 160 | const char* scope, |
| 161 | uint64_t id, |
| 162 | uint64_t bind_id, |
| 163 | int32_t num_args, |
| 164 | const char** arg_names, |
| 165 | const uint8_t* arg_types, |
| 166 | const uint64_t* arg_values, |
| 167 | std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables, |
| 168 | unsigned int flags) { |
alph | 7722c1c | 2016-09-23 01:47:21 | [diff] [blame] | 169 | std::unique_ptr<base::trace_event::ConvertableToTraceFormat> convertables[2]; |
| 170 | if (num_args > 0 && arg_types[0] == TRACE_VALUE_TYPE_CONVERTABLE) { |
| 171 | convertables[0].reset( |
alph | 92d855a4 | 2016-09-28 09:34:52 | [diff] [blame] | 172 | new ConvertableToTraceFormatWrapper(arg_convertables[0])); |
alph | 7722c1c | 2016-09-23 01:47:21 | [diff] [blame] | 173 | } |
| 174 | if (num_args > 1 && arg_types[1] == TRACE_VALUE_TYPE_CONVERTABLE) { |
| 175 | convertables[1].reset( |
alph | 92d855a4 | 2016-09-28 09:34:52 | [diff] [blame] | 176 | new ConvertableToTraceFormatWrapper(arg_convertables[1])); |
alph | 7722c1c | 2016-09-23 01:47:21 | [diff] [blame] | 177 | } |
alph | 92d855a4 | 2016-09-28 09:34:52 | [diff] [blame] | 178 | DCHECK_LE(num_args, 2); |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 179 | base::trace_event::TraceEventHandle handle = |
| 180 | TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_BIND_ID( |
fmeawad | ab615cf | 2016-03-01 18:03:31 | [diff] [blame] | 181 | phase, category_enabled_flag, name, scope, id, bind_id, num_args, |
alph | 7722c1c | 2016-09-23 01:47:21 | [diff] [blame] | 182 | arg_names, arg_types, (const long long unsigned int*)arg_values, |
| 183 | convertables, flags); |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 184 | uint64_t result; |
| 185 | memcpy(&result, &handle, sizeof(result)); |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | void V8Platform::UpdateTraceEventDuration(const uint8_t* category_enabled_flag, |
| 190 | const char* name, |
| 191 | uint64_t handle) { |
| 192 | base::trace_event::TraceEventHandle traceEventHandle; |
| 193 | memcpy(&traceEventHandle, &handle, sizeof(handle)); |
| 194 | TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_enabled_flag, name, |
| 195 | traceEventHandle); |
| 196 | } |
| 197 | |
alph | 290f630 | 2016-09-21 00:00:39 | [diff] [blame] | 198 | namespace { |
| 199 | |
| 200 | class EnabledStateObserverImpl final |
| 201 | : public base::trace_event::TraceLog::EnabledStateObserver { |
| 202 | public: |
lpy | 241ab2f | 2016-11-02 00:27:42 | [diff] [blame] | 203 | EnabledStateObserverImpl() = default; |
alph | 290f630 | 2016-09-21 00:00:39 | [diff] [blame] | 204 | |
| 205 | void OnTraceLogEnabled() final { |
| 206 | base::AutoLock lock(mutex_); |
| 207 | for (auto o : observers_) { |
| 208 | o->OnTraceEnabled(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void OnTraceLogDisabled() final { |
| 213 | base::AutoLock lock(mutex_); |
| 214 | for (auto o : observers_) { |
| 215 | o->OnTraceDisabled(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void AddObserver(v8::Platform::TraceStateObserver* observer) { |
| 220 | base::AutoLock lock(mutex_); |
| 221 | DCHECK(!observers_.count(observer)); |
| 222 | observers_.insert(observer); |
| 223 | if (observers_.size() == 1) { |
| 224 | base::trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void RemoveObserver(v8::Platform::TraceStateObserver* observer) { |
| 229 | base::AutoLock lock(mutex_); |
| 230 | DCHECK(observers_.count(observer) == 1); |
| 231 | observers_.erase(observer); |
| 232 | if (observers_.empty()) { |
| 233 | base::trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver( |
| 234 | this); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | base::Lock mutex_; |
| 240 | std::unordered_set<v8::Platform::TraceStateObserver*> observers_; |
| 241 | |
| 242 | DISALLOW_COPY_AND_ASSIGN(EnabledStateObserverImpl); |
| 243 | }; |
| 244 | |
| 245 | base::LazyInstance<EnabledStateObserverImpl>::Leaky g_trace_state_dispatcher = |
| 246 | LAZY_INSTANCE_INITIALIZER; |
| 247 | |
| 248 | } // namespace |
| 249 | |
| 250 | void V8Platform::AddTraceStateObserver( |
| 251 | v8::Platform::TraceStateObserver* observer) { |
| 252 | g_trace_state_dispatcher.Get().AddObserver(observer); |
| 253 | } |
| 254 | |
| 255 | void V8Platform::RemoveTraceStateObserver( |
| 256 | v8::Platform::TraceStateObserver* observer) { |
| 257 | g_trace_state_dispatcher.Get().RemoveObserver(observer); |
| 258 | } |
| 259 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 260 | } // namespace gin |