| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 10963 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10974 reinterpret_cast<i::Address>(function)); | 10974 reinterpret_cast<i::Address>(function)); |
| 10975 CHECK(code != NULL); | 10975 CHECK(code != NULL); |
| 10976 | 10976 |
| 10977 if (bar_ptr != NULL && code == (*bar_ptr)->code()) | 10977 if (bar_ptr != NULL && code == (*bar_ptr)->code()) |
| 10978 ++bar_count; | 10978 ++bar_count; |
| 10979 | 10979 |
| 10980 if (foo_ptr != NULL && code == (*foo_ptr)->code()) | 10980 if (foo_ptr != NULL && code == (*foo_ptr)->code()) |
| 10981 ++foo_count; | 10981 ++foo_count; |
| 10982 | 10982 |
| 10983 // TODO(siggi): Verify return_addr_location. | 10983 // TODO(siggi): Verify return_addr_location. |
| 10984 // This can be done by capturing JitCodeEvents, but requires an ordered | |
| 10985 // collection. | |
| 10986 } | 10984 } |
| 10987 | 10985 |
| 10988 | 10986 |
| 10989 static void RunLoopInNewEnv() { | 10987 static void RunLoopInNewEnv() { |
| 10990 bar_ptr = NULL; | 10988 bar_ptr = NULL; |
| 10991 foo_ptr = NULL; | 10989 foo_ptr = NULL; |
| 10992 | 10990 |
| 10993 v8::HandleScope outer; | 10991 v8::HandleScope outer; |
| 10994 v8::Persistent<Context> env = Context::New(); | 10992 v8::Persistent<Context> env = Context::New(); |
| 10995 env->Enter(); | 10993 env->Enter(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11061 // functions from the previous invocation. | 11059 // functions from the previous invocation. |
| 11062 v8::internal::Isolate::Current()->compilation_cache()->Clear(); | 11060 v8::internal::Isolate::Current()->compilation_cache()->Clear(); |
| 11063 | 11061 |
| 11064 // Verify that entry hooking is now disabled. | 11062 // Verify that entry hooking is now disabled. |
| 11065 RunLoopInNewEnv(); | 11063 RunLoopInNewEnv(); |
| 11066 CHECK_EQ(0u, bar_count); | 11064 CHECK_EQ(0u, bar_count); |
| 11067 CHECK_EQ(0u, foo_count); | 11065 CHECK_EQ(0u, foo_count); |
| 11068 } | 11066 } |
| 11069 | 11067 |
| 11070 | 11068 |
| 11071 static i::HashMap* code_map = NULL; | |
| 11072 static int saw_bar = 0; | |
| 11073 static int move_events = 0; | |
| 11074 | |
| 11075 | |
| 11076 static bool FunctionNameIs(const char* expected, | |
| 11077 const v8::JitCodeEvent* event) { | |
| 11078 // Log lines for functions are of the general form: | |
| 11079 // "LazyCompile:<type><function_name>", where the type is one of | |
| 11080 // "*", "~" or "". | |
| 11081 static const char kPreamble[] = "LazyCompile:"; | |
| 11082 static size_t kPreambleLen = sizeof(kPreamble) - 1; | |
| 11083 | |
| 11084 if (event->name.len < sizeof(kPreamble) - 1 || | |
| 11085 strncmp(kPreamble, event->name.str, kPreambleLen) != 0) { | |
| 11086 return false; | |
| 11087 } | |
| 11088 | |
| 11089 const char* tail = event->name.str + kPreambleLen; | |
| 11090 size_t tail_len = event->name.len - kPreambleLen; | |
| 11091 size_t expected_len = strlen(expected); | |
| 11092 if (tail_len == expected_len + 1) { | |
| 11093 if (*tail == '*' || *tail == '~') { | |
| 11094 --tail_len; | |
| 11095 ++tail; | |
| 11096 } else { | |
| 11097 return false; | |
| 11098 } | |
| 11099 } | |
| 11100 | |
| 11101 if (tail_len != expected_len) | |
| 11102 return false; | |
| 11103 | |
| 11104 return strncmp(tail, expected, expected_len) == 0; | |
| 11105 } | |
| 11106 | |
| 11107 | |
| 11108 static void event_handler(const v8::JitCodeEvent* event) { | |
| 11109 CHECK(event != NULL); | |
| 11110 CHECK(code_map != NULL); | |
| 11111 | |
| 11112 uint32_t hash = static_cast<uint32_t>( | |
| 11113 reinterpret_cast<uintptr_t>(event->code_start)); | |
| 11114 switch (event->type) { | |
| 11115 case v8::JitCodeEvent::CODE_ADDED: { | |
| 11116 CHECK(event->code_start != NULL); | |
| 11117 CHECK_NE(0, event->code_len); | |
| 11118 CHECK(event->name.str != NULL); | |
| 11119 i::HashMap::Entry* entry = | |
| 11120 code_map->Lookup(event->code_start, hash, true); | |
| 11121 entry->value = reinterpret_cast<void*>(event->code_len); | |
| 11122 | |
| 11123 if (FunctionNameIs("bar", event)) { | |
| 11124 ++saw_bar; | |
| 11125 } | |
| 11126 } | |
| 11127 break; | |
| 11128 | |
| 11129 case v8::JitCodeEvent::CODE_MOVED: { | |
| 11130 ++move_events; | |
| 11131 | |
| 11132 // We should never see code move that we haven't seen before. | |
| 11133 i::HashMap::Entry* entry = | |
| 11134 code_map->Lookup(event->code_start, hash, false); | |
| 11135 CHECK(entry != NULL); | |
| 11136 CHECK_EQ(reinterpret_cast<void*>(event->code_len), | |
| 11137 code_map->Remove(event->code_start, hash)); | |
| 11138 entry = code_map->Lookup(event->code_start, hash, true); | |
| 11139 entry->value = reinterpret_cast<void*>(event->code_len); | |
| 11140 } | |
| 11141 break; | |
| 11142 | |
| 11143 case v8::JitCodeEvent::CODE_REMOVED: | |
| 11144 // Object/code removal events are currently not dispatched from the GC. | |
| 11145 CHECK(false); | |
| 11146 break; | |
| 11147 default: | |
| 11148 // Impossible event. | |
| 11149 CHECK(false); | |
| 11150 break; | |
| 11151 } | |
| 11152 } | |
| 11153 | |
| 11154 | |
| 11155 // Implemented in the test-alloc.cc test suite. | |
| 11156 void SimulateFullSpace(i::PagedSpace* space); | |
| 11157 | |
| 11158 | |
| 11159 static bool MatchPointers(void* key1, void* key2) { | |
| 11160 return key1 == key2; | |
| 11161 } | |
| 11162 | |
| 11163 | |
| 11164 TEST(SetJitCodeEventHandler) { | |
| 11165 const char* script = | |
| 11166 "function bar() {" | |
| 11167 " var sum = 0;" | |
| 11168 " for (i = 0; i < 100; ++i)" | |
| 11169 " sum = foo(i);" | |
| 11170 " return sum;" | |
| 11171 "}" | |
| 11172 "function foo(i) { return i * i; };" | |
| 11173 "bar();"; | |
| 11174 | |
| 11175 // Run this test in a new isolate to make sure we don't | |
| 11176 // have remnants of state from other code. | |
| 11177 v8::Isolate* isolate = v8::Isolate::New(); | |
| 11178 isolate->Enter(); | |
| 11179 | |
| 11180 { | |
| 11181 i::HashMap code(MatchPointers); | |
| 11182 code_map = &code; | |
| 11183 saw_bar = 0; | |
| 11184 move_events = 0; | |
| 11185 | |
| 11186 i::FLAG_stress_compaction = true; | |
| 11187 V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, event_handler); | |
| 11188 | |
| 11189 v8::HandleScope scope; | |
| 11190 // Generate new code objects sparsely distributed across several | |
| 11191 // different fragmented code-space pages. | |
| 11192 const int kIterations = 10; | |
| 11193 for (int i = 0; i < kIterations; ++i) { | |
| 11194 LocalContext env; | |
| 11195 | |
| 11196 v8::Handle<v8::Script> compiled_script; | |
| 11197 { | |
| 11198 i::AlwaysAllocateScope always_allocate; | |
| 11199 SimulateFullSpace(HEAP->code_space()); | |
| 11200 compiled_script = v8_compile(script); | |
| 11201 } | |
| 11202 compiled_script->Run(); | |
| 11203 | |
| 11204 // Clear the compilation cache to get more wastage. | |
| 11205 ISOLATE->compilation_cache()->Clear(); | |
| 11206 } | |
| 11207 | |
| 11208 // Force code movement. | |
| 11209 HEAP->CollectAllAvailableGarbage("TestSetJitCodeEventHandler"); | |
| 11210 | |
| 11211 CHECK_LE(kIterations, saw_bar); | |
| 11212 CHECK_NE(0, move_events); | |
| 11213 | |
| 11214 code_map = NULL; | |
| 11215 V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, NULL); | |
| 11216 } | |
| 11217 | |
| 11218 isolate->Exit(); | |
| 11219 isolate->Dispose(); | |
| 11220 | |
| 11221 // Do this in a new isolate. | |
| 11222 isolate = v8::Isolate::New(); | |
| 11223 isolate->Enter(); | |
| 11224 | |
| 11225 // Verify that we get callbacks for existing code objects when we | |
| 11226 // request enumeration of existing code. | |
| 11227 { | |
| 11228 v8::HandleScope scope; | |
| 11229 LocalContext env; | |
| 11230 CompileRun(script); | |
| 11231 | |
| 11232 // Now get code through initial iteration. | |
| 11233 i::HashMap code(MatchPointers); | |
| 11234 code_map = &code; | |
| 11235 | |
| 11236 V8::SetJitCodeEventHandler(v8::kJitCodeEventEnumExisting, event_handler); | |
| 11237 V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, NULL); | |
| 11238 | |
| 11239 code_map = NULL; | |
| 11240 | |
| 11241 // We expect that we got some events. Note that if we could get code removal | |
| 11242 // notifications, we could compare two collections, one created by listening | |
| 11243 // from the time of creation of an isolate, and the other by subscribing | |
| 11244 // with EnumExisting. | |
| 11245 CHECK_NE(0, code.occupancy()); | |
| 11246 } | |
| 11247 | |
| 11248 isolate->Exit(); | |
| 11249 isolate->Dispose(); | |
| 11250 } | |
| 11251 | |
| 11252 | |
| 11253 static int64_t cast(intptr_t x) { return static_cast<int64_t>(x); } | 11069 static int64_t cast(intptr_t x) { return static_cast<int64_t>(x); } |
| 11254 | 11070 |
| 11255 | 11071 |
| 11256 THREADED_TEST(ExternalAllocatedMemory) { | 11072 THREADED_TEST(ExternalAllocatedMemory) { |
| 11257 v8::HandleScope outer; | 11073 v8::HandleScope outer; |
| 11258 v8::Persistent<Context> env(Context::New()); | 11074 v8::Persistent<Context> env(Context::New()); |
| 11259 CHECK(!env.IsEmpty()); | 11075 CHECK(!env.IsEmpty()); |
| 11260 const intptr_t kSize = 1024*1024; | 11076 const intptr_t kSize = 1024*1024; |
| 11261 CHECK_EQ(cast(v8::V8::AdjustAmountOfExternalAllocatedMemory(kSize)), | 11077 CHECK_EQ(cast(v8::V8::AdjustAmountOfExternalAllocatedMemory(kSize)), |
| 11262 cast(kSize)); | 11078 cast(kSize)); |
| (...skipping 6168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 17431 | 17247 |
| 17432 i::Semaphore* sem_; | 17248 i::Semaphore* sem_; |
| 17433 volatile int sem_value_; | 17249 volatile int sem_value_; |
| 17434 }; | 17250 }; |
| 17435 | 17251 |
| 17436 | 17252 |
| 17437 THREADED_TEST(SemaphoreInterruption) { | 17253 THREADED_TEST(SemaphoreInterruption) { |
| 17438 ThreadInterruptTest().RunTest(); | 17254 ThreadInterruptTest().RunTest(); |
| 17439 } | 17255 } |
| 17440 #endif // WIN32 | 17256 #endif // WIN32 |
| OLD | NEW |