Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8-counters.h" | |
| 28 #include "write-buffer.h" | 29 #include "write-buffer.h" |
| 29 | 30 |
| 30 namespace v8 { | 31 namespace v8 { |
| 31 namespace internal { | 32 namespace internal { |
| 32 | 33 |
| 33 Address* WriteBuffer::top_ = NULL; | 34 Address* WriteBuffer::top_ = NULL; |
| 34 Address* WriteBuffer::start_ = NULL; | 35 Address* WriteBuffer::start_ = NULL; |
| 35 Address* WriteBuffer::limit_ = NULL; | 36 Address* WriteBuffer::limit_ = NULL; |
| 37 uintptr_t* WriteBuffer::hash_map_1_ = NULL; | |
| 38 uintptr_t* WriteBuffer::hash_map_2_ = NULL; | |
| 36 VirtualMemory* WriteBuffer::virtual_memory_ = NULL; | 39 VirtualMemory* WriteBuffer::virtual_memory_ = NULL; |
| 37 | 40 |
| 38 void WriteBuffer::Setup() { | 41 void WriteBuffer::Setup() { |
| 39 virtual_memory_ = new VirtualMemory(kWriteBufferSize * 3); | 42 virtual_memory_ = new VirtualMemory(kWriteBufferSize * 3); |
| 40 uintptr_t start_as_int = | 43 uintptr_t start_as_int = |
| 41 reinterpret_cast<uintptr_t>(virtual_memory_->address()); | 44 reinterpret_cast<uintptr_t>(virtual_memory_->address()); |
| 42 start_ = | 45 start_ = |
| 43 reinterpret_cast<Address*>(RoundUp(start_as_int, kWriteBufferSize * 2)); | 46 reinterpret_cast<Address*>(RoundUp(start_as_int, kWriteBufferSize * 2)); |
| 44 limit_ = start_ + (kWriteBufferSize / sizeof(*start_)); | 47 limit_ = start_ + (kWriteBufferSize / sizeof(*start_)); |
| 45 | 48 |
| 46 ASSERT(reinterpret_cast<Address>(start_) >= virtual_memory_->address()); | 49 ASSERT(reinterpret_cast<Address>(start_) >= virtual_memory_->address()); |
| 47 ASSERT(reinterpret_cast<Address>(limit_) >= virtual_memory_->address()); | 50 ASSERT(reinterpret_cast<Address>(limit_) >= virtual_memory_->address()); |
| 48 Address* vm_limit = reinterpret_cast<Address*>( | 51 Address* vm_limit = reinterpret_cast<Address*>( |
| 49 reinterpret_cast<char*>(virtual_memory_->address()) + | 52 reinterpret_cast<char*>(virtual_memory_->address()) + |
| 50 virtual_memory_->size()); | 53 virtual_memory_->size()); |
| 51 ASSERT(start_ <= vm_limit); | 54 ASSERT(start_ <= vm_limit); |
| 52 ASSERT(limit_ <= vm_limit); | 55 ASSERT(limit_ <= vm_limit); |
| 53 USE(vm_limit); | 56 USE(vm_limit); |
| 54 ASSERT((reinterpret_cast<uintptr_t>(limit_) & kWriteBufferOverflowBit) != 0); | 57 ASSERT((reinterpret_cast<uintptr_t>(limit_) & kWriteBufferOverflowBit) != 0); |
| 55 ASSERT((reinterpret_cast<uintptr_t>(limit_ - 1) & kWriteBufferOverflowBit) == | 58 ASSERT((reinterpret_cast<uintptr_t>(limit_ - 1) & kWriteBufferOverflowBit) == |
| 56 0); | 59 0); |
| 57 | 60 |
| 58 virtual_memory_->Commit(reinterpret_cast<Address>(start_), | 61 virtual_memory_->Commit(reinterpret_cast<Address>(start_), |
| 59 kWriteBufferSize, | 62 kWriteBufferSize, |
| 60 false); // Not executable. | 63 false); // Not executable. |
| 61 top_ = start_; | 64 top_ = start_; |
| 65 | |
| 66 hash_map_1_ = new uintptr_t[kHashMapLength]; | |
| 67 hash_map_2_ = new uintptr_t[kHashMapLength]; | |
| 62 } | 68 } |
| 63 | 69 |
| 64 | 70 |
| 65 void WriteBuffer::TearDown() { | 71 void WriteBuffer::TearDown() { |
| 66 delete virtual_memory_; | 72 delete virtual_memory_; |
| 73 delete[] hash_map_1_; | |
| 74 delete[] hash_map_2_; | |
| 67 top_ = start_ = limit_ = NULL; | 75 top_ = start_ = limit_ = NULL; |
| 68 } | 76 } |
| 69 | 77 |
| 70 | 78 |
| 71 void WriteBuffer::Compact() { | 79 void WriteBuffer::Compact() { |
| 72 top_ = start_; // TODO(gc) fix. | 80 memset(reinterpret_cast<void*>(hash_map_1_), |
| 81 0, | |
| 82 sizeof(uintptr_t) * kHashMapLength); | |
| 83 memset(reinterpret_cast<void*>(hash_map_2_), | |
| 84 0, | |
| 85 sizeof(uintptr_t) * kHashMapLength); | |
| 86 Address* stop = top_; | |
| 87 top_ = start_; | |
| 88 for (Address* current = start_; current < stop; current++) { | |
|
Vyacheslav Egorov (Chromium)
2010/12/16 10:36:40
More comments about design please.
Compaction do
| |
| 89 uintptr_t int_addr = reinterpret_cast<uintptr_t>(*current); | |
| 90 // Shift out the last bits including any tags. | |
| 91 int_addr >>= kPointerSizeLog2; | |
| 92 int hash1 = | |
| 93 ((int_addr ^ (int_addr >> kHashMapLengthLog2)) & (kHashMapLength - 1)); | |
| 94 if (hash_map_1_[hash1] == int_addr) continue; | |
| 95 int hash2 = | |
| 96 ((int_addr - (int_addr >> kHashMapLengthLog2)) & (kHashMapLength - 1)); | |
| 97 hash2 ^= hash2 >> (kHashMapLengthLog2 * 2); | |
| 98 if (hash_map_2_[hash2] == int_addr) continue; | |
| 99 if (hash_map_1_[hash1] == 0) { | |
| 100 hash_map_1_[hash1] = int_addr; | |
| 101 } else if (hash_map_2_[hash2] == 0) { | |
| 102 hash_map_2_[hash2] = int_addr; | |
| 103 } else { | |
| 104 hash_map_1_[hash1] = int_addr; | |
| 105 hash_map_2_[hash2] = 0; | |
| 106 } | |
| 107 ASSERT(top_ <= current); | |
| 108 ASSERT(top_ <= limit_); | |
| 109 *top_++ = reinterpret_cast<Address>(int_addr << kPointerSizeLog2); | |
| 110 } | |
| 111 Counters::write_buffer_compactions.Increment(); | |
| 112 if (limit_ - top_ < top_ - start_) { | |
| 113 // Compression did not free up at least half. | |
| 114 // TODO(gc): Set an interrupt to do a GC on the next back edge. | |
| 115 // TODO(gc): Allocate the rest of new space to force a GC on the next | |
| 116 // allocation. | |
| 117 if (limit_ - top_ < (top_ - start_) >> 1) { | |
| 118 // Compression did not free up at least one quarter. | |
| 119 // TODO(gc): Set a flag to scan all of memory. | |
| 120 top_ = start_; | |
| 121 Counters::write_buffer_overflows.Increment(); | |
| 122 } | |
| 123 } | |
| 73 } | 124 } |
| 74 | 125 |
| 75 } } // namespace v8::internal | 126 } } // namespace v8::internal |
| OLD | NEW |