From 6e80963f1fd3ecbd9f1d8cf0149afe74ebd6e190 Mon Sep 17 00:00:00 2001 From: nakai Date: Mon, 8 Jun 2009 09:36:21 +0000 Subject: [PATCH] =?utf8?q?atomic=E3=83=86=E3=82=B9=E3=83=88=E7=94=A8?= =?utf8?q?=E3=82=B3=E3=83=BC=E3=83=89=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://10.144.169.20/repos/um/branches/l7vsd-3.x-shamshel-refine@7890 1ed66053-1c2d-0410-8867-f7571e6e31d3 --- unit_tests/bentch/Makefile | 28 +++++++++++++ unit_tests/bentch/atomic_test.cpp | 82 +++++++++++++++++++++++++++++++++++++++ unit_tests/bentch/rdtsc64.h | 18 +++++++++ 3 files changed, 128 insertions(+) create mode 100644 unit_tests/bentch/Makefile create mode 100644 unit_tests/bentch/atomic_test.cpp create mode 100644 unit_tests/bentch/rdtsc64.h diff --git a/unit_tests/bentch/Makefile b/unit_tests/bentch/Makefile new file mode 100644 index 00000000..5a2a0ef6 --- /dev/null +++ b/unit_tests/bentch/Makefile @@ -0,0 +1,28 @@ +TARGET = atomic_bench +CPP = g++ +CPPFLAGS = -Wall -O2 -g -Werror -pthread +INCLUDES = -I../../include +LIBS = + +LDFLAGS = -lrt -ldl + + +SRCS = atomic_test.cpp + + +.SUFFIXES: .o .cpp +.cpp.o: + $(CPP) $(CPPFLAGS) $(INCLUDES) -c $< -o $@ + +OBJS = $(SRCS:.cpp=.o) + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CPP) $(INCLUDES) -o $@ $(LDFLAGS) $(OBJS) $(LIBS) + + +clean: + rm -f $(TARGET) $(OBJS) + + diff --git a/unit_tests/bentch/atomic_test.cpp b/unit_tests/bentch/atomic_test.cpp new file mode 100644 index 00000000..5b14129e --- /dev/null +++ b/unit_tests/bentch/atomic_test.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include "../../include/atomic.h" +#include "rdtsc64.h" + + +typedef l7vs::atomic< unsigned long long > atomic_ulong_t; +atomic_ulong_t ulong_value; + +pthread_mutex_t mutex; +unsigned long long lock_ulong_value; + +void* thread_func_atomic( void* param ){ + unsigned long long starttime, endtime; + RDTSC64( starttime ); + for( int i = 0 ; i < 10000; ++i ){ + ulong_value -= 1000; + ulong_value -= 1000; + } + RDTSC64( endtime ); + std::cout << endtime - starttime << std::endl; + return 0; +} + +void* thread_func_mutex( void* param ){ + unsigned long long starttime, endtime; + RDTSC64( starttime ); + for( int i = 0 ; i < 10000; ++i ){ + pthread_mutex_lock( &mutex ); + lock_ulong_value += 1000; + lock_ulong_value -= 1000; + pthread_mutex_unlock( &mutex ); + } + RDTSC64( endtime ); + std::cout << endtime - starttime << std::endl; + return 0; +} + +int main( int argc, char* argv[] ){ + ulong_value = 0; + lock_ulong_value = 0; + pthread_mutex_init( &mutex, NULL ); + if( argc < 2 ){ + std::cout << "usage : a.out [thread_num] " << std::endl; + return 0; + } + + int count = boost::lexical_cast( argv[1] ); + std::vector thread_vec; + pthread_t thd; + + //atomic version + std::cout << "atomic func time start" << std::endl; + for( int i = 0; i < count; ++i ){ + pthread_create( &thd, NULL, thread_func_atomic, NULL ); + thread_vec.push_back( thd ); + } + for( std::vector::iterator itr = thread_vec.begin(); + itr != thread_vec.end(); + ++itr ){ + pthread_join( *itr, NULL ); + } + thread_vec.clear(); + + //mutex version + std::cout << "mutex func time start" << std::endl; + for( int i = 0; i < count; ++i ){ + pthread_create( &thd, NULL, thread_func_mutex, NULL ); + thread_vec.push_back( thd ); + } + for( std::vector::iterator itr = thread_vec.begin(); + itr != thread_vec.end(); + ++itr ){ + pthread_join( *itr, NULL ); + } + + pthread_mutex_destroy( &mutex ); + + return 0; +} diff --git a/unit_tests/bentch/rdtsc64.h b/unit_tests/bentch/rdtsc64.h new file mode 100644 index 00000000..3367f5ae --- /dev/null +++ b/unit_tests/bentch/rdtsc64.h @@ -0,0 +1,18 @@ +#ifndef RDTSC64_H +#define RDTSC64_H +// +// this code is AMD64 only. don't use other platform. +// n.nakai@sdy.co.jp +// +// + +#define RDTSC64(X) __asm__ __volatile__ ( \ + "rdtsc;" \ + "movq %%rdx,%0;" \ + "salq $32,%0;" \ + "orq %%rax, %0" \ + : "=r"(X) \ + : \ + : "%rax", "%rdx" ); + +#endif //RDTSC64_H -- 2.11.0