From 49f11e84829d4045fdaea5b5986994f87c1d27c0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 8 Aug 2008 00:43:20 +0000 Subject: [PATCH] add TLS access benchmark git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55015 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/benchmarks/Makefile.in | 6 +- tests/benchmarks/bench.bkl | 1 + tests/benchmarks/bench.dsp | 4 ++ tests/benchmarks/bench.h | 1 + tests/benchmarks/bench.vcproj | 3 + tests/benchmarks/makefile.bcc | 6 +- tests/benchmarks/makefile.gcc | 6 +- tests/benchmarks/makefile.vc | 6 +- tests/benchmarks/makefile.wat | 6 +- tests/benchmarks/tls.cpp | 127 ++++++++++++++++++++++++++++++++++ 10 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tests/benchmarks/tls.cpp diff --git a/tests/benchmarks/Makefile.in b/tests/benchmarks/Makefile.in index 9160871cb2..92058a3b53 100644 --- a/tests/benchmarks/Makefile.in +++ b/tests/benchmarks/Makefile.in @@ -44,7 +44,8 @@ BENCH_CXXFLAGS = -D__WX$(TOOLKIT)__ $(__WXUNIV_DEFINE_p) \ $(CXXFLAGS) BENCH_OBJECTS = \ bench_bench.o \ - bench_strings.o + bench_strings.o \ + bench_tls.o ### Conditionally set variables: ### @@ -111,6 +112,9 @@ bench_bench.o: $(srcdir)/bench.cpp bench_strings.o: $(srcdir)/strings.cpp $(CXXC) -c -o $@ $(BENCH_CXXFLAGS) $(srcdir)/strings.cpp +bench_tls.o: $(srcdir)/tls.cpp + $(CXXC) -c -o $@ $(BENCH_CXXFLAGS) $(srcdir)/tls.cpp + $(srcdir)/include/wx/stc/stc.h: \ $(srcdir)/src/stc/scintilla/include/Scintilla.iface \ diff --git a/tests/benchmarks/bench.bkl b/tests/benchmarks/bench.bkl index 9107e459e7..d8016412aa 100644 --- a/tests/benchmarks/bench.bkl +++ b/tests/benchmarks/bench.bkl @@ -12,6 +12,7 @@ bench.cpp strings.cpp + tls.cpp base diff --git a/tests/benchmarks/bench.dsp b/tests/benchmarks/bench.dsp index 795a9ce8e4..65dec996de 100644 --- a/tests/benchmarks/bench.dsp +++ b/tests/benchmarks/bench.dsp @@ -241,6 +241,10 @@ SOURCE=.\bench.cpp SOURCE=.\strings.cpp # End Source File +# Begin Source File + +SOURCE=.\tls.cpp +# End Source File # End Group # End Target # End Project diff --git a/tests/benchmarks/bench.h b/tests/benchmarks/bench.h index af7fba2723..96a9a1fc8c 100644 --- a/tests/benchmarks/bench.h +++ b/tests/benchmarks/bench.h @@ -12,6 +12,7 @@ #define _WX_TESTS_BENCHMARKS_BENCH_H_ #include "wx/cpp.h" +#include "wx/defs.h" namespace Bench { diff --git a/tests/benchmarks/bench.vcproj b/tests/benchmarks/bench.vcproj index cb31b290c8..dc6e68e116 100644 --- a/tests/benchmarks/bench.vcproj +++ b/tests/benchmarks/bench.vcproj @@ -789,6 +789,9 @@ + diff --git a/tests/benchmarks/makefile.bcc b/tests/benchmarks/makefile.bcc index 38987c9ce5..a11072feef 100644 --- a/tests/benchmarks/makefile.bcc +++ b/tests/benchmarks/makefile.bcc @@ -36,7 +36,8 @@ BENCH_CXXFLAGS = $(__RUNTIME_LIBS_7) -I$(BCCDIR)\include $(__DEBUGINFO) \ -DwxUSE_GUI=0 $(CPPFLAGS) $(CXXFLAGS) BENCH_OBJECTS = \ $(OBJS)\bench_bench.obj \ - $(OBJS)\bench_strings.obj + $(OBJS)\bench_strings.obj \ + $(OBJS)\bench_tls.obj ### Conditionally set variables: ### @@ -194,3 +195,6 @@ $(OBJS)\bench_bench.obj: .\bench.cpp $(OBJS)\bench_strings.obj: .\strings.cpp $(CXX) -q -c -P -o$@ $(BENCH_CXXFLAGS) .\strings.cpp +$(OBJS)\bench_tls.obj: .\tls.cpp + $(CXX) -q -c -P -o$@ $(BENCH_CXXFLAGS) .\tls.cpp + diff --git a/tests/benchmarks/makefile.gcc b/tests/benchmarks/makefile.gcc index 20cc9a78fc..4da2fb6fb1 100644 --- a/tests/benchmarks/makefile.gcc +++ b/tests/benchmarks/makefile.gcc @@ -29,7 +29,8 @@ BENCH_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG_2) $(__THREADSFLAG) \ -Wno-ctor-dtor-privacy $(CPPFLAGS) $(CXXFLAGS) BENCH_OBJECTS = \ $(OBJS)\bench_bench.o \ - $(OBJS)\bench_strings.o + $(OBJS)\bench_strings.o \ + $(OBJS)\bench_tls.o ### Conditionally set variables: ### @@ -184,6 +185,9 @@ $(OBJS)\bench_bench.o: ./bench.cpp $(OBJS)\bench_strings.o: ./strings.cpp $(CXX) -c -o $@ $(BENCH_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\bench_tls.o: ./tls.cpp + $(CXX) -c -o $@ $(BENCH_CXXFLAGS) $(CPPDEPS) $< + .PHONY: all clean diff --git a/tests/benchmarks/makefile.vc b/tests/benchmarks/makefile.vc index cf37916d83..d6a3a3cf7b 100644 --- a/tests/benchmarks/makefile.vc +++ b/tests/benchmarks/makefile.vc @@ -30,7 +30,8 @@ BENCH_CXXFLAGS = /M$(__RUNTIME_LIBS_8)$(__DEBUGRUNTIME_3) /DWIN32 \ $(CXXFLAGS) BENCH_OBJECTS = \ $(OBJS)\bench_bench.obj \ - $(OBJS)\bench_strings.obj + $(OBJS)\bench_strings.obj \ + $(OBJS)\bench_tls.obj ### Conditionally set variables: ### @@ -251,3 +252,6 @@ $(OBJS)\bench_bench.obj: .\bench.cpp $(OBJS)\bench_strings.obj: .\strings.cpp $(CXX) /c /nologo /TP /Fo$@ $(BENCH_CXXFLAGS) .\strings.cpp +$(OBJS)\bench_tls.obj: .\tls.cpp + $(CXX) /c /nologo /TP /Fo$@ $(BENCH_CXXFLAGS) .\tls.cpp + diff --git a/tests/benchmarks/makefile.wat b/tests/benchmarks/makefile.wat index 29e865b960..feb639f5c2 100644 --- a/tests/benchmarks/makefile.wat +++ b/tests/benchmarks/makefile.wat @@ -210,7 +210,8 @@ BENCH_CXXFLAGS = $(__DEBUGINFO_0) $(__OPTIMIZEFLAG_2) $(__THREADSFLAG_5) & -dwxUSE_GUI=0 $(__RTTIFLAG_7) $(__EXCEPTIONSFLAG_8) $(CPPFLAGS) $(CXXFLAGS) BENCH_OBJECTS = & $(OBJS)\bench_bench.obj & - $(OBJS)\bench_strings.obj + $(OBJS)\bench_strings.obj & + $(OBJS)\bench_tls.obj all : $(OBJS) @@ -247,3 +248,6 @@ $(OBJS)\bench_bench.obj : .AUTODEPEND .\bench.cpp $(OBJS)\bench_strings.obj : .AUTODEPEND .\strings.cpp $(CXX) -bt=nt -zq -fo=$^@ $(BENCH_CXXFLAGS) $< +$(OBJS)\bench_tls.obj : .AUTODEPEND .\tls.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(BENCH_CXXFLAGS) $< + diff --git a/tests/benchmarks/tls.cpp b/tests/benchmarks/tls.cpp new file mode 100644 index 0000000000..afccef5377 --- /dev/null +++ b/tests/benchmarks/tls.cpp @@ -0,0 +1,127 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: tests/benchmarks/strings.cpp +// Purpose: String-related benchmarks +// Author: Vadim Zeitlin +// Created: 2008-07-19 +// RCS-ID: $Id$ +// Copyright: (c) 2008 Vadim Zeitlin +// Licence: wxWindows license +///////////////////////////////////////////////////////////////////////////// + +#include "bench.h" + +#ifdef __UNIX__ + #define HAVE_PTHREAD + #include +#endif + +#if wxCHECK_GCC_VERSION(3, 3) + #define HAVE_COMPILER_THREAD + #define wxTHREAD_SPECIFIC __thread +#endif + +// uncomment this to also test Boost version (you will also need to link with +// libboost_threads) +//#define HAVE_BOOST_THREAD +#ifdef HAVE_BOOST_THREAD + #include +#endif + + +static const int NUM_ITER = 1000; + +// this is just a baseline +BENCHMARK_FUNC(DummyTLS) +{ + static int s_global = 0; + + for ( int n = 0; n < NUM_ITER; n++ ) + { + if ( n % 2 ) + s_global = 0; + else + s_global = n; + } + + return !s_global; +} + +#ifdef HAVE_COMPILER_THREAD + +BENCHMARK_FUNC(CompilerTLS) +{ + static wxTHREAD_SPECIFIC int s_global = 0; + + for ( int n = 0; n < NUM_ITER; n++ ) + { + if ( n % 2 ) + s_global = 0; + else + s_global = n; + } + + return !s_global; +} + +#endif // HAVE_COMPILER_THREAD + +#ifdef HAVE_PTHREAD + +class PthreadKey +{ +public: + PthreadKey() + { + pthread_key_create(&m_key, NULL); + } + + ~PthreadKey() + { + pthread_key_delete(m_key); + } + + operator pthread_key_t() const { return m_key; } + +private: + pthread_key_t m_key; + + DECLARE_NO_COPY_CLASS(PthreadKey) +}; + +BENCHMARK_FUNC(PosixTLS) +{ + static PthreadKey s_key; + + for ( int n = 0; n < NUM_ITER; n++ ) + { + if ( n % 2 ) + pthread_setspecific(s_key, 0); + else + pthread_setspecific(s_key, &n); + } + + return !pthread_getspecific(s_key); +} + +#endif // HAVE_PTHREAD + +#ifdef HAVE_BOOST_THREAD + +BENCHMARK_FUNC(BoostTLS) +{ + static boost::thread_specific_ptr s_ptr; + if ( !s_ptr.get() ) + s_ptr.reset(new int(0)); + + for ( int n = 0; n < NUM_ITER; n++ ) + { + if ( n % 2 ) + *s_ptr = 0; + else + *s_ptr = n; + } + + return !*s_ptr; +} + +#endif // HAVE_BOOST_THREAD -- 2.45.2