[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 | |
| 20 | } // namespace |
| 21 | |
| 22 | // static |
| 23 | V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); } |
| 24 | |
| 25 | V8Platform::V8Platform() {} |
| 26 | |
| 27 | V8Platform::~V8Platform() {} |
| 28 | |
mlippautz | 40dfecc9 | 2016-03-07 21:06:50 | [diff] [blame^] | 29 | size_t V8Platform::NumberOfAvailableBackgroundThreads() { |
| 30 | // WorkerPool will currently always create additional threads for posted |
| 31 | // background tasks, unless there are threads sitting idle (on posix). |
| 32 | // Indicate that V8 should create no more than the number of cores available, |
| 33 | // reserving one core for the main thread. |
| 34 | const size_t available_cores = |
| 35 | static_cast<size_t>(base::SysInfo::NumberOfProcessors()); |
| 36 | if (available_cores > 1) { |
| 37 | return available_cores - 1; |
| 38 | } |
| 39 | return 1; |
| 40 | } |
| 41 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 42 | void V8Platform::CallOnBackgroundThread( |
| 43 | v8::Task* task, |
| 44 | v8::Platform::ExpectedRuntime expected_runtime) { |
| 45 | base::WorkerPool::PostTask( |
| 46 | FROM_HERE, |
| 47 | base::Bind(&v8::Task::Run, base::Owned(task)), |
| 48 | expected_runtime == v8::Platform::kLongRunningTask); |
| 49 | } |
| 50 | |
| 51 | void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) { |
skyostil | a389986 | 2015-06-12 18:21:58 | [diff] [blame] | 52 | PerIsolateData::From(isolate)->task_runner()->PostTask( |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 53 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task))); |
| 54 | } |
| 55 | |
ulan | 49cd773c | 2015-06-24 16:38:08 | [diff] [blame] | 56 | void V8Platform::CallDelayedOnForegroundThread(v8::Isolate* isolate, |
| 57 | v8::Task* task, |
| 58 | double delay_in_seconds) { |
| 59 | PerIsolateData::From(isolate)->task_runner()->PostDelayedTask( |
| 60 | FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)), |
| 61 | base::TimeDelta::FromSecondsD(delay_in_seconds)); |
| 62 | } |
| 63 | |
ulan | 3cbdcd0 | 2015-07-20 11:32:58 | [diff] [blame] | 64 | void V8Platform::CallIdleOnForegroundThread(v8::Isolate* isolate, |
| 65 | v8::IdleTask* task) { |
| 66 | DCHECK(PerIsolateData::From(isolate)->idle_task_runner()); |
| 67 | PerIsolateData::From(isolate)->idle_task_runner()->PostIdleTask(task); |
| 68 | } |
| 69 | |
| 70 | bool V8Platform::IdleTasksEnabled(v8::Isolate* isolate) { |
| 71 | return PerIsolateData::From(isolate)->idle_task_runner() != nullptr; |
| 72 | } |
| 73 | |
rmcilroy | 05d2662 | 2014-10-08 11:28:05 | [diff] [blame] | 74 | double V8Platform::MonotonicallyIncreasingTime() { |
| 75 | return base::TimeTicks::Now().ToInternalValue() / |
| 76 | static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 77 | } |
| 78 | |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 79 | const uint8_t* V8Platform::GetCategoryGroupEnabled(const char* name) { |
| 80 | return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(name); |
| 81 | } |
| 82 | |
| 83 | const char* V8Platform::GetCategoryGroupName( |
| 84 | const uint8_t* category_enabled_flag) { |
| 85 | return base::trace_event::TraceLog::GetCategoryGroupName( |
| 86 | category_enabled_flag); |
| 87 | } |
| 88 | |
| 89 | uint64_t V8Platform::AddTraceEvent(char phase, |
| 90 | const uint8_t* category_enabled_flag, |
| 91 | const char* name, |
fmeawad | ab615cf | 2016-03-01 18:03:31 | [diff] [blame] | 92 | const char* scope, |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 93 | uint64_t id, |
| 94 | uint64_t bind_id, |
| 95 | int32_t num_args, |
| 96 | const char** arg_names, |
| 97 | const uint8_t* arg_types, |
| 98 | const uint64_t* arg_values, |
| 99 | unsigned int flags) { |
| 100 | base::trace_event::TraceEventHandle handle = |
| 101 | TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_BIND_ID( |
fmeawad | ab615cf | 2016-03-01 18:03:31 | [diff] [blame] | 102 | phase, category_enabled_flag, name, scope, id, bind_id, num_args, |
| 103 | arg_names, arg_types, (const long long unsigned int*)arg_values, NULL, |
| 104 | flags); |
fmeawad | 471a220 | 2015-12-21 17:00:24 | [diff] [blame] | 105 | uint64_t result; |
| 106 | memcpy(&result, &handle, sizeof(result)); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | void V8Platform::UpdateTraceEventDuration(const uint8_t* category_enabled_flag, |
| 111 | const char* name, |
| 112 | uint64_t handle) { |
| 113 | base::trace_event::TraceEventHandle traceEventHandle; |
| 114 | memcpy(&traceEventHandle, &handle, sizeof(handle)); |
| 115 | TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_enabled_flag, name, |
| 116 | traceEventHandle); |
| 117 | } |
| 118 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 119 | } // namespace gin |