blob: 556f7fc5f24f0b1c924138a6b77709e7aea5bc5d [file] [log] [blame]
[email protected]b64e5212014-04-04 21:09:161// 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"
mlippautz40dfecc92016-03-07 21:06:509#include "base/sys_info.h"
[email protected]b64e5212014-04-04 21:09:1610#include "base/threading/worker_pool.h"
tmoniuszko36416932016-01-27 16:28:4511#include "base/trace_event/trace_event.h"
[email protected]b64e5212014-04-04 21:09:1612#include "gin/per_isolate_data.h"
13
14namespace gin {
15
16namespace {
17
18base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER;
19
20} // namespace
21
22// static
23V8Platform* V8Platform::Get() { return g_v8_platform.Pointer(); }
24
25V8Platform::V8Platform() {}
26
27V8Platform::~V8Platform() {}
28
mlippautz40dfecc92016-03-07 21:06:5029size_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]b64e5212014-04-04 21:09:1642void 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
51void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) {
skyostila3899862015-06-12 18:21:5852 PerIsolateData::From(isolate)->task_runner()->PostTask(
[email protected]b64e5212014-04-04 21:09:1653 FROM_HERE, base::Bind(&v8::Task::Run, base::Owned(task)));
54}
55
ulan49cd773c2015-06-24 16:38:0856void 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
ulan3cbdcd02015-07-20 11:32:5864void 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
70bool V8Platform::IdleTasksEnabled(v8::Isolate* isolate) {
71 return PerIsolateData::From(isolate)->idle_task_runner() != nullptr;
72}
73
rmcilroy05d26622014-10-08 11:28:0574double V8Platform::MonotonicallyIncreasingTime() {
75 return base::TimeTicks::Now().ToInternalValue() /
76 static_cast<double>(base::Time::kMicrosecondsPerSecond);
77}
78
fmeawad471a2202015-12-21 17:00:2479const uint8_t* V8Platform::GetCategoryGroupEnabled(const char* name) {
80 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(name);
81}
82
83const char* V8Platform::GetCategoryGroupName(
84 const uint8_t* category_enabled_flag) {
85 return base::trace_event::TraceLog::GetCategoryGroupName(
86 category_enabled_flag);
87}
88
89uint64_t V8Platform::AddTraceEvent(char phase,
90 const uint8_t* category_enabled_flag,
91 const char* name,
fmeawadab615cf2016-03-01 18:03:3192 const char* scope,
fmeawad471a2202015-12-21 17:00:2493 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(
fmeawadab615cf2016-03-01 18:03:31102 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);
fmeawad471a2202015-12-21 17:00:24105 uint64_t result;
106 memcpy(&result, &handle, sizeof(result));
107 return result;
108}
109
110void 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]b64e5212014-04-04 21:09:16119} // namespace gin