Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / performance-tests / Synch-Benchmarks / Base_Test / Baseline_Test.cpp
bloba03bfa0f1bcb5038274799c437045cf07c595af6
1 #define ACE_BUILD_SVC_DLL
3 #include "Baseline_Test.h"
5 # if defined (ACE_HAS_THREADS)
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/Service_Repository.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/Thread_Manager.h"
12 #if !defined (__ACE_INLINE__)
13 #include "Baseline_Test.inl"
14 #endif /* __ACE_INLINE__ */
16 Baseline_Test_Options baseline_options;
17 // Static Baseline Options holds the test configuration information
18 // and the test statistics.
20 Baseline_Test_Base::Baseline_Test_Base ()
21 : Benchmark_Base (Benchmark_Base::BASELINE),
22 yield_method_ (Baseline_Test_Options::USE_SLEEP_ZERO),
23 iteration_ (DEFAULT_ITERATIONS),
24 what_(TEST_LOCK)
28 int
29 Baseline_Test_Base::init (int argc, ACE_TCHAR *argv[])
31 return this->parse_args (argc, argv);
34 int
35 Baseline_Test_Base::parse_args (int argc, ACE_TCHAR *argv[])
37 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("i:ylrw"), 0);
38 int c;
40 while ((c = get_opt ()) != -1)
41 switch (c)
43 case 'i': // Total iterations
45 int tmp = ACE_OS::atoi (get_opt.opt_arg ());
46 if (tmp <= 0)
47 ACE_ERROR_RETURN ((LM_ERROR,
48 "%d is not a valid value for iteration\n",
49 tmp), -1);
50 else
51 this->iteration_ = static_cast<size_t> (tmp);
53 break;
55 case 'y': // Use thr_yield.
56 this->yield_method_ = Baseline_Test_Options::USE_THR_YIELD;
57 break;
59 case 'l':
60 this->what_ = TEST_LOCK;
61 break;
63 case 'r':
64 this->what_ = TEST_READLOCK;
65 break;
67 case 'w':
68 this->what_ = TEST_WRITELOCK;
69 break;
71 default:
72 ACE_ERROR ((LM_ERROR, "Invalid argument %c used\n", c));
73 break;
75 return 0;
78 void
79 Baseline_Test_Base::yield ()
81 if (this->yield_method_ == Baseline_Test_Options::USE_SLEEP_ZERO)
82 ACE_OS::sleep (0);
83 else
84 ACE_OS::thr_yield ();
87 Baseline_Test_Options::Baseline_Test_Options ()
88 : test_try_lock_ (0),
89 verbose_ (0),
90 current_yield_method_ (0),
91 current_iteration_ (0),
92 total_iteration_ (DEFAULT_ITERATIONS)
96 int
97 Baseline_Test_Options::parse_args (int argc, ACE_TCHAR *argv[])
99 //FUZZ: disable check_for_lack_ACE_OS
100 ACE_Get_Opt getopt (argc, argv, ACE_TEXT("tv"), 0);
101 int c;
103 while ((c = getopt ()) != -1)
104 //FUZZ: enable check_for_lack_ACE_OS
105 switch (c)
107 case 't':
108 this->test_try_lock_ = 1;
109 break;
111 case 'v':
112 this->verbose_ = 1;
113 break;
115 default:
116 ACE_ERROR ((LM_ERROR, "Invalid arguemnt %c used.\n", c));
117 break;
119 return 0;
123 Baseline_Test_Options::reset_params (size_t iteration,
124 int yield)
126 this->current_iteration_ = 0;
127 this->timer.reset ();
129 this->current_yield_method_ = yield;
130 this->total_iteration_ = iteration;
131 return 0;
134 void
135 Baseline_Test_Options::print_result ()
137 ACE_Time_Value tv;
138 ACE_hrtime_t nsec;
140 this->timer.elapsed_time_incr (tv);
141 this->timer.elapsed_time_incr (nsec);
142 ACE_DEBUG ((LM_DEBUG,
143 "Total Time: %d sec %d usec for a "
144 "total of %d iterations\n"
145 "Average time: %d nanoseconds.\n",
146 tv.sec (), tv.usec (),
147 this->current_iteration_,
148 (int) (nsec / this->current_iteration_)));
151 Baseline_Test::Baseline_Test ()
152 : current_test_ (0),
153 get_lock_ (2),
154 let_go_lock_ (2)
158 // Initialize and run the benchmarks tests.
161 Baseline_Test::init (int argc, ACE_TCHAR **argv)
163 return baseline_options.parse_args (argc, argv);
167 Baseline_Test::pre_run_test (Benchmark_Base *bb)
169 this->current_test_ = (Baseline_Test_Base *) bb;
170 baseline_options.reset_params (this->current_test_->iteration (),
171 this->current_test_->yield_method ());
172 if (baseline_options.test_try_lock ())
174 ACE_Thread_Manager::instance ()->spawn
175 (ACE_THR_FUNC (Baseline_Test::hold_lock),
176 (void *) this);
178 this->get_lock_.wait ();
179 // Wait until the lock is held by the spawning thread.
182 return 0;
186 Baseline_Test::run_test ()
188 if (baseline_options.test_try_lock ())
189 return this->current_test_->test_try_lock ();
190 else
191 return this->current_test_->test_acquire_release ();
195 Baseline_Test::post_run_test ()
197 if (baseline_options.test_try_lock ())
199 // Release the lock we hold.
200 this->let_go_lock_.wait ();
202 ACE_Thread_Manager::instance ()->wait ();
205 baseline_options.print_result ();
207 return 0;
211 Baseline_Test::valid_test_object (Benchmark_Base *bb)
213 return (bb->benchmark_type () == Benchmark_Base::BASELINE);
216 void *
217 Baseline_Test::hold_lock (void *arg)
219 Baseline_Test *this_test = (Baseline_Test *) arg;
220 this_test->current_test_->acquire ();
221 this_test->get_lock_.wait ();
223 this_test->let_go_lock_.wait ();
224 return 0;
227 ACE_SVC_FACTORY_DEFINE (Baseline_Test)
229 #endif /* ACE_HAS_THREADS */