Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/circular-queue.cc

Issue 19642002: Fix data race in SamplingCircularQueue (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Removed some empty lines Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/circular-queue.h ('k') | src/circular-queue-inl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 15 matching lines...) Expand all
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.h" 28 #include "v8.h"
29 29
30 #include "circular-queue-inl.h" 30 #include "circular-queue-inl.h"
31 31
32 namespace v8 { 32 namespace v8 {
33 namespace internal { 33 namespace internal {
34 34
35 35
36 SamplingCircularQueue::SamplingCircularQueue(int record_size_in_bytes, 36 SamplingCircularQueue::SamplingCircularQueue(size_t record_size_in_bytes,
37 int desired_chunk_size_in_bytes, 37 size_t desired_chunk_size_in_bytes,
38 int buffer_size_in_chunks) 38 int buffer_size_in_chunks)
39 : record_size_(record_size_in_bytes / sizeof(Cell)), 39 : record_size_(record_size_in_bytes / sizeof(Cell)),
40 chunk_size_in_bytes_(desired_chunk_size_in_bytes / record_size_in_bytes * 40 chunk_size_in_bytes_(desired_chunk_size_in_bytes / record_size_in_bytes *
41 record_size_in_bytes), 41 record_size_in_bytes + sizeof(Cell)),
42 chunk_size_(chunk_size_in_bytes_ / sizeof(Cell)), 42 chunk_size_(chunk_size_in_bytes_ / sizeof(Cell)),
43 buffer_size_(chunk_size_ * buffer_size_in_chunks), 43 buffer_size_(chunk_size_ * buffer_size_in_chunks),
44 // The distance ensures that producer and consumer never step on 44 buffer_(NewArray<Cell>(buffer_size_)) {
45 // each other's chunks and helps eviction of produced data from 45 ASSERT(record_size_ * sizeof(Cell) == record_size_in_bytes);
46 // the CPU cache (having that chunk size is bigger than the cache.) 46 ASSERT(chunk_size_ * sizeof(Cell) == chunk_size_in_bytes_);
47 producer_consumer_distance_(2 * chunk_size_),
48 buffer_(NewArray<Cell>(buffer_size_ + 1)) {
49 ASSERT(buffer_size_in_chunks > 2); 47 ASSERT(buffer_size_in_chunks > 2);
50 // Clean up the whole buffer to avoid encountering a random kEnd 48 // Mark all chunks as clear.
51 // while enqueuing. 49 for (int i = 0; i < buffer_size_; i += chunk_size_) {
52 for (int i = 0; i < buffer_size_; ++i) {
53 buffer_[i] = kClear; 50 buffer_[i] = kClear;
54 } 51 }
55 buffer_[buffer_size_] = kEnd;
56 52
57 // Layout producer and consumer position pointers each on their own 53 // Layout producer and consumer position pointers each on their own
58 // cache lines to avoid cache lines thrashing due to simultaneous 54 // cache lines to avoid cache lines thrashing due to simultaneous
59 // updates of positions by different processor cores. 55 // updates of positions by different processor cores.
60 const int positions_size = 56 const int positions_size =
61 RoundUp(1, kProcessorCacheLineSize) + 57 RoundUp(1, kProcessorCacheLineSize) +
62 RoundUp(static_cast<int>(sizeof(ProducerPosition)), 58 RoundUp(static_cast<int>(sizeof(ProducerPosition)),
63 kProcessorCacheLineSize) + 59 kProcessorCacheLineSize) +
64 RoundUp(static_cast<int>(sizeof(ConsumerPosition)), 60 RoundUp(static_cast<int>(sizeof(ConsumerPosition)),
65 kProcessorCacheLineSize); 61 kProcessorCacheLineSize);
66 positions_ = NewArray<byte>(positions_size); 62 positions_ = NewArray<byte>(positions_size);
67 63
68 producer_pos_ = reinterpret_cast<ProducerPosition*>( 64 producer_pos_ = reinterpret_cast<ProducerPosition*>(
69 RoundUp(positions_, kProcessorCacheLineSize)); 65 RoundUp(positions_, kProcessorCacheLineSize));
66 producer_pos_->next_chunk_pos = buffer_;
70 producer_pos_->enqueue_pos = buffer_; 67 producer_pos_->enqueue_pos = buffer_;
71 68
72 consumer_pos_ = reinterpret_cast<ConsumerPosition*>( 69 consumer_pos_ = reinterpret_cast<ConsumerPosition*>(
73 reinterpret_cast<byte*>(producer_pos_) + kProcessorCacheLineSize); 70 reinterpret_cast<byte*>(producer_pos_) + kProcessorCacheLineSize);
74 ASSERT(reinterpret_cast<byte*>(consumer_pos_ + 1) <= 71 ASSERT(reinterpret_cast<byte*>(consumer_pos_ + 1) <=
75 positions_ + positions_size); 72 positions_ + positions_size);
76 consumer_pos_->dequeue_chunk_pos = buffer_; 73 consumer_pos_->dequeue_chunk_pos = buffer_;
77 consumer_pos_->dequeue_chunk_poll_pos = buffer_ + producer_consumer_distance_; 74 // The distance ensures that producer and consumer never step on
75 // each other's chunks and helps eviction of produced data from
76 // the CPU cache (having that chunk size is bigger than the cache.)
77 const int producer_consumer_distance = (2 * chunk_size_);
78 consumer_pos_->dequeue_chunk_poll_pos = buffer_ + producer_consumer_distance;
78 consumer_pos_->dequeue_pos = NULL; 79 consumer_pos_->dequeue_pos = NULL;
79 } 80 }
80 81
81 82
82 SamplingCircularQueue::~SamplingCircularQueue() { 83 SamplingCircularQueue::~SamplingCircularQueue() {
83 DeleteArray(positions_); 84 DeleteArray(positions_);
84 DeleteArray(buffer_); 85 DeleteArray(buffer_);
85 } 86 }
86 87
87 88
88 void* SamplingCircularQueue::StartDequeue() { 89 void* SamplingCircularQueue::StartDequeue() {
89 if (consumer_pos_->dequeue_pos != NULL) { 90 if (consumer_pos_->dequeue_pos != NULL) {
90 return consumer_pos_->dequeue_pos; 91 return consumer_pos_->dequeue_pos;
91 } else { 92 } else {
92 if (*consumer_pos_->dequeue_chunk_poll_pos != kClear) { 93 if (Acquire_Load(consumer_pos_->dequeue_chunk_poll_pos) != kClear) {
93 consumer_pos_->dequeue_pos = consumer_pos_->dequeue_chunk_pos; 94 // Skip marker.
94 consumer_pos_->dequeue_end_pos = consumer_pos_->dequeue_pos + chunk_size_; 95 consumer_pos_->dequeue_pos = consumer_pos_->dequeue_chunk_pos + 1;
96 consumer_pos_->dequeue_end_pos =
97 consumer_pos_->dequeue_chunk_pos + chunk_size_;
95 return consumer_pos_->dequeue_pos; 98 return consumer_pos_->dequeue_pos;
96 } else { 99 } else {
97 return NULL; 100 return NULL;
98 } 101 }
99 } 102 }
100 } 103 }
101 104
102 105
103 void SamplingCircularQueue::FinishDequeue() { 106 void SamplingCircularQueue::FinishDequeue() {
104 consumer_pos_->dequeue_pos += record_size_; 107 consumer_pos_->dequeue_pos += record_size_;
105 if (consumer_pos_->dequeue_pos < consumer_pos_->dequeue_end_pos) return; 108 if (consumer_pos_->dequeue_pos < consumer_pos_->dequeue_end_pos) return;
106 // Move to next chunk. 109 // Move to next chunk.
107 consumer_pos_->dequeue_pos = NULL; 110 consumer_pos_->dequeue_pos = NULL;
108 *consumer_pos_->dequeue_chunk_pos = kClear; 111 *consumer_pos_->dequeue_chunk_pos = kClear;
109 consumer_pos_->dequeue_chunk_pos += chunk_size_; 112 consumer_pos_->dequeue_chunk_pos += chunk_size_;
110 WrapPositionIfNeeded(&consumer_pos_->dequeue_chunk_pos); 113 WrapPositionIfNeeded(&consumer_pos_->dequeue_chunk_pos);
111 consumer_pos_->dequeue_chunk_poll_pos += chunk_size_; 114 consumer_pos_->dequeue_chunk_poll_pos += chunk_size_;
112 WrapPositionIfNeeded(&consumer_pos_->dequeue_chunk_poll_pos); 115 WrapPositionIfNeeded(&consumer_pos_->dequeue_chunk_poll_pos);
113 } 116 }
114 117
115 118
116 void SamplingCircularQueue::FlushResidualRecords() { 119 void SamplingCircularQueue::FlushResidualRecords() {
117 // Eliminate producer / consumer distance. 120 // Eliminate producer / consumer distance.
118 consumer_pos_->dequeue_chunk_poll_pos = consumer_pos_->dequeue_chunk_pos; 121 consumer_pos_->dequeue_chunk_poll_pos = consumer_pos_->dequeue_chunk_pos;
119 } 122 }
120 123
121 124
122 } } // namespace v8::internal 125 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/circular-queue.h ('k') | src/circular-queue-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698