OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 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 "circular-queue-inl.h" |
| 31 |
| 32 namespace v8 { |
| 33 namespace internal { |
| 34 |
| 35 |
| 36 SamplingCircularQueue::SamplingCircularQueue(int record_size_in_bytes, |
| 37 int desired_chunk_size_in_bytes, |
| 38 int buffer_size_in_chunks) |
| 39 : record_size_(record_size_in_bytes / sizeof(Cell)), |
| 40 chunk_size_in_bytes_(desired_chunk_size_in_bytes / record_size_in_bytes * |
| 41 record_size_in_bytes), |
| 42 chunk_size_(chunk_size_in_bytes_ / sizeof(Cell)), |
| 43 buffer_size_(chunk_size_ * buffer_size_in_chunks), |
| 44 // The distance ensures that producer and consumer never step on |
| 45 // each other's chunks and helps eviction of produced data from |
| 46 // the CPU cache (having that chunk size is bigger than the cache.) |
| 47 producer_consumer_distance_(2 * chunk_size_), |
| 48 buffer_(NewArray<Cell>(buffer_size_ + 1)) { |
| 49 ASSERT(buffer_size_in_chunks > 2); |
| 50 // Only need to keep the first cell of a chunk clean. |
| 51 for (int i = 0; i < buffer_size_; i += chunk_size_) { |
| 52 buffer_[i] = kClear; |
| 53 } |
| 54 buffer_[buffer_size_] = kEnd; |
| 55 } |
| 56 |
| 57 |
| 58 SamplingCircularQueue::~SamplingCircularQueue() { |
| 59 DeleteArray(buffer_); |
| 60 } |
| 61 |
| 62 |
| 63 void SamplingCircularQueue::SetUpProducer() { |
| 64 producer_key_ = Thread::CreateThreadLocalKey(); |
| 65 Thread::SetThreadLocal(producer_key_, buffer_); |
| 66 } |
| 67 |
| 68 |
| 69 void SamplingCircularQueue::TearDownProducer() { |
| 70 Thread::DeleteThreadLocalKey(producer_key_); |
| 71 } |
| 72 |
| 73 |
| 74 void SamplingCircularQueue::SetUpConsumer() { |
| 75 consumer_key_ = Thread::CreateThreadLocalKey(); |
| 76 ConsumerPosition* cp = new ConsumerPosition; |
| 77 cp->dequeue_chunk_pos = buffer_; |
| 78 cp->dequeue_chunk_poll_pos = buffer_ + producer_consumer_distance_; |
| 79 cp->dequeue_pos = NULL; |
| 80 Thread::SetThreadLocal(consumer_key_, cp); |
| 81 } |
| 82 |
| 83 |
| 84 void SamplingCircularQueue::TearDownConsumer() { |
| 85 delete reinterpret_cast<ConsumerPosition*>( |
| 86 Thread::GetThreadLocal(consumer_key_)); |
| 87 Thread::DeleteThreadLocalKey(consumer_key_); |
| 88 } |
| 89 |
| 90 |
| 91 void* SamplingCircularQueue::StartDequeue() { |
| 92 ConsumerPosition* cp = reinterpret_cast<ConsumerPosition*>( |
| 93 Thread::GetThreadLocal(consumer_key_)); |
| 94 if (cp->dequeue_pos != NULL) { |
| 95 return cp->dequeue_pos; |
| 96 } else { |
| 97 if (*cp->dequeue_chunk_poll_pos != kClear) { |
| 98 cp->dequeue_pos = cp->dequeue_chunk_pos; |
| 99 cp->dequeue_end_pos = cp->dequeue_pos + chunk_size_; |
| 100 return cp->dequeue_pos; |
| 101 } else { |
| 102 return NULL; |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 |
| 108 void SamplingCircularQueue::FinishDequeue() { |
| 109 ConsumerPosition* cp = reinterpret_cast<ConsumerPosition*>( |
| 110 Thread::GetThreadLocal(consumer_key_)); |
| 111 cp->dequeue_pos += record_size_; |
| 112 if (cp->dequeue_pos < cp->dequeue_end_pos) return; |
| 113 // Move to next chunk. |
| 114 cp->dequeue_pos = NULL; |
| 115 *cp->dequeue_chunk_pos = kClear; |
| 116 cp->dequeue_chunk_pos += chunk_size_; |
| 117 WrapPositionIfNeeded(&cp->dequeue_chunk_pos); |
| 118 cp->dequeue_chunk_poll_pos += chunk_size_; |
| 119 WrapPositionIfNeeded(&cp->dequeue_chunk_poll_pos); |
| 120 } |
| 121 |
| 122 |
| 123 void SamplingCircularQueue::FlushResidualRecords() { |
| 124 ConsumerPosition* cp = reinterpret_cast<ConsumerPosition*>( |
| 125 Thread::GetThreadLocal(consumer_key_)); |
| 126 // Eliminate producer / consumer distance. |
| 127 cp->dequeue_chunk_poll_pos = cp->dequeue_chunk_pos; |
| 128 } |
| 129 |
| 130 |
| 131 } } // namespace v8::internal |
OLD | NEW |