Line data Source code
1 : // Copyright 2012 the V8 project 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 "src/v8.h"
6 :
7 : #include <fstream>
8 :
9 : #include "src/api.h"
10 : #include "src/base/atomicops.h"
11 : #include "src/base/once.h"
12 : #include "src/base/platform/platform.h"
13 : #include "src/bootstrapper.h"
14 : #include "src/cpu-features.h"
15 : #include "src/debug/debug.h"
16 : #include "src/deoptimizer.h"
17 : #include "src/elements.h"
18 : #include "src/frames.h"
19 : #include "src/interface-descriptors.h"
20 : #include "src/isolate.h"
21 : #include "src/libsampler/sampler.h"
22 : #include "src/objects-inl.h"
23 : #include "src/profiler/heap-profiler.h"
24 : #include "src/reloc-info.h"
25 : #include "src/runtime-profiler.h"
26 : #include "src/simulator.h"
27 : #include "src/snapshot/natives.h"
28 : #include "src/snapshot/snapshot.h"
29 : #include "src/tracing/tracing-category-observer.h"
30 : #include "src/wasm/wasm-engine.h"
31 :
32 : namespace v8 {
33 : namespace internal {
34 :
35 : V8_DECLARE_ONCE(init_once);
36 :
37 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
38 : V8_DECLARE_ONCE(init_natives_once);
39 : V8_DECLARE_ONCE(init_snapshot_once);
40 : #endif
41 :
42 : v8::Platform* V8::platform_ = nullptr;
43 :
44 61617 : bool V8::Initialize() {
45 : InitializeOncePerProcess();
46 61617 : return true;
47 : }
48 :
49 :
50 31878 : void V8::TearDown() {
51 : #if defined(USE_SIMULATOR)
52 : Simulator::GlobalTearDown();
53 : #endif
54 31878 : wasm::WasmEngine::GlobalTearDown();
55 31878 : CallDescriptors::TearDown();
56 31878 : Bootstrapper::TearDownExtensions();
57 31878 : ElementsAccessor::TearDown();
58 31878 : RegisteredExtension::UnregisterAll();
59 31878 : sampler::Sampler::TearDown();
60 31878 : FlagList::ResetAllFlags(); // Frees memory held by string arguments.
61 31878 : }
62 :
63 :
64 61281 : void V8::InitializeOncePerProcessImpl() {
65 61281 : FlagList::EnforceFlagImplications();
66 :
67 61281 : if (FLAG_predictable && FLAG_random_seed == 0) {
68 : // Avoid random seeds in predictable mode.
69 0 : FLAG_random_seed = 12347;
70 : }
71 :
72 61281 : if (FLAG_stress_compaction) {
73 95 : FLAG_force_marking_deque_overflows = true;
74 95 : FLAG_gc_global = true;
75 95 : FLAG_max_semi_space_size = 1;
76 : }
77 :
78 61281 : if (FLAG_trace_turbo) {
79 : // Create an empty file shared by the process (e.g. the wasm engine).
80 : std::ofstream(Isolate::GetTurboCfgFileName(nullptr).c_str(),
81 3 : std::ios_base::trunc);
82 : }
83 :
84 61281 : base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);
85 :
86 61281 : if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
87 :
88 61281 : Isolate::InitializeOncePerProcess();
89 :
90 : #if defined(USE_SIMULATOR)
91 : Simulator::InitializeOncePerProcess();
92 : #endif
93 61281 : sampler::Sampler::SetUp();
94 : CpuFeatures::Probe(false);
95 61281 : ElementsAccessor::InitializeOncePerProcess();
96 61281 : Bootstrapper::InitializeOncePerProcess();
97 61281 : CallDescriptors::InitializeOncePerProcess();
98 61281 : wasm::WasmEngine::InitializeOncePerProcess();
99 61281 : }
100 :
101 :
102 0 : void V8::InitializeOncePerProcess() {
103 61617 : base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
104 0 : }
105 :
106 :
107 61281 : void V8::InitializePlatform(v8::Platform* platform) {
108 61281 : CHECK(!platform_);
109 61281 : CHECK(platform);
110 61281 : platform_ = platform;
111 61281 : v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
112 61281 : v8::tracing::TracingCategoryObserver::SetUp();
113 61281 : }
114 :
115 :
116 60151 : void V8::ShutdownPlatform() {
117 60151 : CHECK(platform_);
118 60151 : v8::tracing::TracingCategoryObserver::TearDown();
119 60151 : v8::base::SetPrintStackTrace(nullptr);
120 60151 : platform_ = nullptr;
121 60151 : }
122 :
123 :
124 35762209 : v8::Platform* V8::GetCurrentPlatform() {
125 : v8::Platform* platform = reinterpret_cast<v8::Platform*>(
126 35762209 : base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
127 : DCHECK(platform);
128 35762209 : return platform;
129 : }
130 :
131 600 : void V8::SetPlatformForTesting(v8::Platform* platform) {
132 : base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
133 : reinterpret_cast<base::AtomicWord>(platform));
134 600 : }
135 :
136 61288 : void V8::SetNativesBlob(StartupData* natives_blob) {
137 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
138 61288 : base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
139 : #else
140 : UNREACHABLE();
141 : #endif
142 61288 : }
143 :
144 :
145 61288 : void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
146 : #ifdef V8_USE_EXTERNAL_STARTUP_DATA
147 61288 : base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
148 : #else
149 : UNREACHABLE();
150 : #endif
151 61288 : }
152 : } // namespace internal
153 :
154 : // static
155 0 : double Platform::SystemClockTimeMillis() {
156 0 : return base::OS::TimeCurrentMillis();
157 : }
158 183867 : } // namespace v8
|