20 #include <boost/thread.hpp>
21 #include <boost/thread/mutex.hpp>
28 using namespace impala;
75 typedef function<void (int64_t, int64_t*)>
Fn;
79 volatile int64_t* v = value;
80 for (int64_t i = 0; i < n; ++i) {
86 volatile int64_t* v = value;
87 for (int64_t i = 0; i < n; ++i) {
93 for (int64_t i = 0; i < n; ++i) {
94 __sync_fetch_and_add(value, -1);
98 for (int64_t i = 0; i < n; ++i) {
99 __sync_fetch_and_add(value, 1);
104 for (int64_t i = 0; i < n; ++i) {
110 for (int64_t i = 0; i < n; ++i) {
117 for (int64_t i = 0; i < n; ++i) {
118 lock_guard<mutex> l(
lock_);
123 for (int64_t i = 0; i < n; ++i) {
124 lock_guard<mutex> l(
lock_);
134 num_per_producer *= scale;
135 num_per_consumer *= scale;
136 thread_group consumers, producers;
138 consumers.add_thread(
139 new thread(consume_fn, num_per_consumer, &data->
value));
142 consumers.add_thread(
143 new thread(produce_fn, num_per_producer, &data->
value));
145 consumers.join_all();
146 producers.join_all();
156 CHECK_EQ(data->
value, 0);
162 CHECK_EQ(data->
value, 0);
168 CHECK_EQ(data->
value, 0);
171 int main(
int argc,
char **argv) {
176 const int max_producers = 12;
180 for (
int i = 0; i < max_producers; i += 2) {
188 suffix <<
" " << (i+1) * 2 <<
"-Total Threads";
191 name <<
"Unlocked" << suffix.str();
195 name <<
"Atomic" << suffix.str();
199 name <<
"SpinLock" << suffix.str();
203 name <<
"Boost" << suffix.str();
206 cout << suite.
Measure() << endl;
void SpinLockConsumeThread(int64_t n, int64_t *value)
int AddBenchmark(const std::string &name, BenchmarkFunction fn, void *args, int baseline_idx=0)
static std::string GetMachineInfo()
Output machine/build configuration as a string.
void TestBoost(int batch_size, void *d)
boost::mutex lock_
protects all fields below
void TestUnlocked(int batch_size, void *d)
std::string Measure()
Runs all the benchmarks and returns the result in a formatted string.
void AtomicConsumeThread(int64_t n, int64_t *value)
void TestAtomic(int batch_size, void *d)
void AtomicProduceThread(int64_t n, int64_t *value)
void TestSpinLock(int batch_size, void *d)
function< void(int64_t, int64_t, AtomicInt< int > *)> Fn
void SpinLockProduceThread(int64_t n, int64_t *value)
void BoostProduceThread(int64_t n, int64_t *value)
int main(int argc, char **argv)
void UnlockedProduceThread(int64_t n, int64_t *value)
void LaunchThreads(void *d, Fn consume_fn, Fn produce_fn, int64_t scale)
void BoostConsumeThread(int64_t n, int64_t *value)
static void Init()
Initialize CpuInfo.
void UnlockedConsumeThread(int64_t n, int64_t *value)
function< void(int64_t, int64_t *)> Fn