| 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 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // The queue is empty. | 96 // The queue is empty. |
| 97 CHECK_EQ(NULL, scq.Peek()); | 97 CHECK_EQ(NULL, scq.Peek()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 | 100 |
| 101 namespace { | 101 namespace { |
| 102 | 102 |
| 103 typedef v8::base::AtomicWord Record; | 103 typedef v8::base::AtomicWord Record; |
| 104 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; | 104 typedef SamplingCircularQueue<Record, 12> TestSampleQueue; |
| 105 | 105 |
| 106 class ProducerThread: public i::Thread { | 106 class ProducerThread: public v8::base::Thread { |
| 107 public: | 107 public: |
| 108 ProducerThread(TestSampleQueue* scq, | 108 ProducerThread(TestSampleQueue* scq, |
| 109 int records_per_chunk, | 109 int records_per_chunk, |
| 110 Record value, | 110 Record value, |
| 111 i::Semaphore* finished) | 111 v8::base::Semaphore* finished) |
| 112 : Thread("producer"), | 112 : Thread("producer"), |
| 113 scq_(scq), | 113 scq_(scq), |
| 114 records_per_chunk_(records_per_chunk), | 114 records_per_chunk_(records_per_chunk), |
| 115 value_(value), | 115 value_(value), |
| 116 finished_(finished) { } | 116 finished_(finished) { } |
| 117 | 117 |
| 118 virtual void Run() { | 118 virtual void Run() { |
| 119 for (Record i = value_; i < value_ + records_per_chunk_; ++i) { | 119 for (Record i = value_; i < value_ + records_per_chunk_; ++i) { |
| 120 Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue()); | 120 Record* rec = reinterpret_cast<Record*>(scq_->StartEnqueue()); |
| 121 CHECK_NE(NULL, rec); | 121 CHECK_NE(NULL, rec); |
| 122 *rec = i; | 122 *rec = i; |
| 123 scq_->FinishEnqueue(); | 123 scq_->FinishEnqueue(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 finished_->Signal(); | 126 finished_->Signal(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 TestSampleQueue* scq_; | 130 TestSampleQueue* scq_; |
| 131 const int records_per_chunk_; | 131 const int records_per_chunk_; |
| 132 Record value_; | 132 Record value_; |
| 133 i::Semaphore* finished_; | 133 v8::base::Semaphore* finished_; |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 } // namespace | 136 } // namespace |
| 137 | 137 |
| 138 TEST(SamplingCircularQueueMultithreading) { | 138 TEST(SamplingCircularQueueMultithreading) { |
| 139 // Emulate multiple VM threads working 'one thread at a time.' | 139 // Emulate multiple VM threads working 'one thread at a time.' |
| 140 // This test enqueues data from different threads. This corresponds | 140 // This test enqueues data from different threads. This corresponds |
| 141 // to the case of profiling under Linux, where signal handler that | 141 // to the case of profiling under Linux, where signal handler that |
| 142 // does sampling is called in the context of different VM threads. | 142 // does sampling is called in the context of different VM threads. |
| 143 | 143 |
| 144 const int kRecordsPerChunk = 4; | 144 const int kRecordsPerChunk = 4; |
| 145 TestSampleQueue scq; | 145 TestSampleQueue scq; |
| 146 i::Semaphore semaphore(0); | 146 v8::base::Semaphore semaphore(0); |
| 147 | 147 |
| 148 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); | 148 ProducerThread producer1(&scq, kRecordsPerChunk, 1, &semaphore); |
| 149 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); | 149 ProducerThread producer2(&scq, kRecordsPerChunk, 10, &semaphore); |
| 150 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); | 150 ProducerThread producer3(&scq, kRecordsPerChunk, 20, &semaphore); |
| 151 | 151 |
| 152 CHECK_EQ(NULL, scq.Peek()); | 152 CHECK_EQ(NULL, scq.Peek()); |
| 153 producer1.Start(); | 153 producer1.Start(); |
| 154 semaphore.Wait(); | 154 semaphore.Wait(); |
| 155 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { | 155 for (Record i = 1; i < 1 + kRecordsPerChunk; ++i) { |
| 156 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 156 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 Record* rec = reinterpret_cast<Record*>(scq.Peek()); | 180 Record* rec = reinterpret_cast<Record*>(scq.Peek()); |
| 181 CHECK_NE(NULL, rec); | 181 CHECK_NE(NULL, rec); |
| 182 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); | 182 CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(*rec)); |
| 183 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); | 183 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 184 scq.Remove(); | 184 scq.Remove(); |
| 185 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); | 185 CHECK_NE(rec, reinterpret_cast<Record*>(scq.Peek())); |
| 186 } | 186 } |
| 187 | 187 |
| 188 CHECK_EQ(NULL, scq.Peek()); | 188 CHECK_EQ(NULL, scq.Peek()); |
| 189 } | 189 } |
| OLD | NEW |