Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "bootstrapper.h" |
| 31 | #include "debug.h" |
| 32 | #include "serialize.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 33 | #include "simulator.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "stub-cache.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 35 | #include "heap-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | #include "oprofile-agent.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 37 | #include "log.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | bool V8::is_running_ = false; |
| 43 | bool V8::has_been_setup_ = false; |
| 44 | bool V8::has_been_disposed_ = false; |
| 45 | bool V8::has_fatal_error_ = false; |
| 46 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 47 | bool V8::Initialize(Deserializer* des) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | bool create_heap_objects = des == NULL; |
| 49 | if (has_been_disposed_ || has_fatal_error_) return false; |
| 50 | if (IsRunning()) return true; |
| 51 | |
| 52 | is_running_ = true; |
| 53 | has_been_setup_ = true; |
| 54 | has_fatal_error_ = false; |
| 55 | has_been_disposed_ = false; |
| 56 | #ifdef DEBUG |
| 57 | // The initialization process does not handle memory exhaustion. |
| 58 | DisallowAllocationFailure disallow_allocation_failure; |
| 59 | #endif |
| 60 | |
| 61 | // Enable logging before setting up the heap |
| 62 | Logger::Setup(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 64 | CpuProfiler::Setup(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 65 | HeapProfiler::Setup(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 66 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | // Setup the platform OS support. |
| 68 | OS::Setup(); |
| 69 | |
| 70 | // Initialize other runtime facilities |
| 71 | #if !V8_HOST_ARCH_ARM && V8_TARGET_ARCH_ARM |
| 72 | ::assembler::arm::Simulator::Initialize(); |
| 73 | #endif |
| 74 | |
| 75 | { // NOLINT |
| 76 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 77 | // will ensure this too, but we don't have to use lockers if we are only |
| 78 | // using one thread. |
| 79 | ExecutionAccess lock; |
| 80 | StackGuard::InitThread(lock); |
| 81 | } |
| 82 | |
| 83 | // Setup the object heap |
| 84 | ASSERT(!Heap::HasBeenSetup()); |
| 85 | if (!Heap::Setup(create_heap_objects)) { |
| 86 | SetFatalError(); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | Bootstrapper::Initialize(create_heap_objects); |
| 91 | Builtins::Setup(create_heap_objects); |
| 92 | Top::Initialize(); |
| 93 | |
| 94 | if (FLAG_preemption) { |
| 95 | v8::Locker locker; |
| 96 | v8::Locker::StartPreemption(100); |
| 97 | } |
| 98 | |
| 99 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 100 | Debug::Setup(create_heap_objects); |
| 101 | #endif |
| 102 | StubCache::Initialize(create_heap_objects); |
| 103 | |
| 104 | // If we are deserializing, read the state into the now-empty heap. |
| 105 | if (des != NULL) { |
| 106 | des->Deserialize(); |
| 107 | StubCache::Clear(); |
| 108 | } |
| 109 | |
| 110 | // Deserializing may put strange things in the root array's copy of the |
| 111 | // stack guard. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 112 | Heap::SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | |
| 114 | // Setup the CPU support. Must be done after heap setup and after |
| 115 | // any deserialization because we have to have the initial heap |
| 116 | // objects in place for creating the code object used for probing. |
| 117 | CPU::Setup(); |
| 118 | |
| 119 | OProfileAgent::Initialize(); |
| 120 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 121 | // If we are deserializing, log non-function code objects and compiled |
| 122 | // functions found in the snapshot. |
| 123 | if (des != NULL && FLAG_log_code) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 124 | HandleScope scope; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 125 | LOG(LogCodeObjects()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 126 | LOG(LogCompiledFunctions()); |
| 127 | } |
| 128 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void V8::SetFatalError() { |
| 134 | is_running_ = false; |
| 135 | has_fatal_error_ = true; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void V8::TearDown() { |
| 140 | if (!has_been_setup_ || has_been_disposed_) return; |
| 141 | |
| 142 | OProfileAgent::TearDown(); |
| 143 | |
| 144 | if (FLAG_preemption) { |
| 145 | v8::Locker locker; |
| 146 | v8::Locker::StopPreemption(); |
| 147 | } |
| 148 | |
| 149 | Builtins::TearDown(); |
| 150 | Bootstrapper::TearDown(); |
| 151 | |
| 152 | Top::TearDown(); |
| 153 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame^] | 154 | HeapProfiler::TearDown(); |
| 155 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 156 | CpuProfiler::TearDown(); |
| 157 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 158 | Heap::TearDown(); |
| 159 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | Logger::TearDown(); |
| 161 | |
| 162 | is_running_ = false; |
| 163 | has_been_disposed_ = true; |
| 164 | } |
| 165 | |
| 166 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 167 | static uint32_t random_seed() { |
| 168 | if (FLAG_random_seed == 0) { |
| 169 | return random(); |
| 170 | } |
| 171 | return FLAG_random_seed; |
| 172 | } |
| 173 | |
| 174 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | uint32_t V8::Random() { |
| 176 | // Random number generator using George Marsaglia's MWC algorithm. |
| 177 | static uint32_t hi = 0; |
| 178 | static uint32_t lo = 0; |
| 179 | |
| 180 | // Initialize seed using the system random(). If one of the seeds |
| 181 | // should ever become zero again, or if random() returns zero, we |
| 182 | // avoid getting stuck with zero bits in hi or lo by re-initializing |
| 183 | // them on demand. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 184 | if (hi == 0) hi = random_seed(); |
| 185 | if (lo == 0) lo = random_seed(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 186 | |
| 187 | // Mix the bits. |
| 188 | hi = 36969 * (hi & 0xFFFF) + (hi >> 16); |
| 189 | lo = 18273 * (lo & 0xFFFF) + (lo >> 16); |
| 190 | return (hi << 16) + (lo & 0xFFFF); |
| 191 | } |
| 192 | |
| 193 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 194 | bool V8::IdleNotification() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | // Returning true tells the caller that there is no need to call |
| 196 | // IdleNotification again. |
| 197 | if (!FLAG_use_idle_notification) return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | |
| 199 | // Tell the heap that it may want to adjust. |
| 200 | return Heap::IdleNotification(); |
| 201 | } |
| 202 | |
| 203 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 204 | // Use a union type to avoid type-aliasing optimizations in GCC. |
| 205 | typedef union { |
| 206 | double double_value; |
| 207 | uint64_t uint64_t_value; |
| 208 | } double_int_union; |
| 209 | |
| 210 | |
| 211 | Object* V8::FillHeapNumberWithRandom(Object* heap_number) { |
| 212 | uint64_t random_bits = Random(); |
| 213 | // Make a double* from address (heap_number + sizeof(double)). |
| 214 | double_int_union* r = reinterpret_cast<double_int_union*>( |
| 215 | reinterpret_cast<char*>(heap_number) + |
| 216 | HeapNumber::kValueOffset - kHeapObjectTag); |
| 217 | // Convert 32 random bits to 0.(32 random bits) in a double |
| 218 | // by computing: |
| 219 | // ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)). |
| 220 | const double binary_million = 1048576.0; |
| 221 | r->double_value = binary_million; |
| 222 | r->uint64_t_value |= random_bits; |
| 223 | r->double_value -= binary_million; |
| 224 | |
| 225 | return heap_number; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | } } // namespace v8::internal |