μHAL (v2.6.5)
Part of the IPbus software repository
test_multithreaded.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23  Marc Magrans de Abril, CERN
24  email: marc.magrans.de.abril <AT> cern.ch
25 
26  Andrew Rose, Imperial College, London
27  email: awr01 <AT> imperial.ac.uk
28 
29  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
30  email: tom.williams <AT> cern.ch
31 
32 ---------------------------------------------------------------------------
33 */
34 
35 #include "uhal/uhal.hpp"
37 #include "uhal/tests/fixtures.hpp"
38 #include "uhal/tests/tools.hpp"
39 #include "uhal/log/log.hpp"
40 
41 #include <boost/thread.hpp>
42 #include <boost/date_time/posix_time/posix_time.hpp>
43 #include <boost/thread/mutex.hpp>
44 #include <boost/thread/condition_variable.hpp>
45 #include <boost/test/unit_test.hpp>
46 
47 #include <iostream>
48 #include <cstdlib>
49 #include <typeinfo>
50 
51 // Linux C++ headers
52 #include <sys/time.h>
53 
54 
55 #define N_THREADS 5
56 #define N_ITERATIONS 5
57 #define N_SIZE uint32_t(10*1024/4)
58 #define TIMEOUT_S 50
59 
60 
61 namespace uhal {
62 namespace tests {
63 
64 
65 void job_multiple ( const std::string& connection, const std::string& id )
66 {
68 
69  for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
70  {
71  log ( Info() , "Iteration " , Integer ( iter ) );
72  ConnectionManager manager ( connection );
73  HwInterface hw = manager.getDevice ( id );
74  hw.setTimeoutPeriod ( TIMEOUT_S*1000 );
75 
76  uint32_t x = static_cast<uint32_t> ( rand() );
77  hw.getNode ( "REG" ).write ( x );
78  ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
79  std::vector<uint32_t> xx;
80 
81  for ( size_t i=0; i!= N_SIZE; ++i )
82  {
83  xx.push_back ( static_cast<uint32_t> ( rand() ) );
84  }
85 
86  hw.getNode ( "MEM" ).writeBlock ( xx );
88  log ( Warning() , ThisLocation() );
89  hw.dispatch();
90  log ( Warning() , ThisLocation() );
91  BOOST_CHECK ( reg.valid() );
92  BOOST_CHECK ( mem.valid() );
93  BOOST_CHECK_EQUAL ( mem.size(), N_SIZE );
94  //can not check content in the mutlithreaded case
95  }
96  );
97 }
98 
99 
100 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, multiple_hwinterfaces, DummyHardwareFixture,
101 {
102  if (deviceType != IPBUS_2_0_PCIE)
103  {
104  std::vector<boost::thread*> jobs;
105 
106  for ( size_t i=0; i!=N_THREADS; ++i )
107  {
108  log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
109  jobs.push_back ( new boost::thread ( job_multiple, connectionFileURI, deviceId ) );
110  }
111 
112  for ( size_t i=0; i!=N_THREADS; ++i )
113  {
114  log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
115  //boost::posix_time::time_duration timeout = boost::posix_time::seconds ( TIMEOUT_S );
116  //CACTUS_CHECK ( jobs[i]->timed_join ( timeout ) );
117  jobs[i]->join();
118  delete jobs[i];
119  }
120 
121  log ( Warning() , ThisLocation() );
122  }
123  else
124  std::cout << " ** Skipping multiple HwInterface test for PCIe **" << std::endl;
125 }
126 )
127 
128 
129 void job_single ( HwInterface& hw )
130 {
132  uint32_t x = static_cast<uint32_t> ( rand() );
133  hw.getNode ( "REG" ).write ( x );
134  ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
135  std::vector<uint32_t> xx;
136 
137  for ( size_t i=0; i!= N_SIZE; ++i )
138  {
139  xx.push_back ( static_cast<uint32_t> ( rand() ) );
140  }
141 
142  hw.getNode ( "MEM" ).writeBlock ( xx );
143  ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
144  hw.dispatch();
145  BOOST_CHECK ( reg.valid() );
146  BOOST_CHECK ( mem.valid() );
147  BOOST_CHECK_EQUAL ( mem.size(), N_SIZE );
148  );
149  //can not check content in the mutlithreaded case
150 }
151 
152 
153 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_hwinterface, DummyHardwareFixture,
154 {
155  for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
156  {
157  HwInterface hw = getHwInterface();
158  std::vector<boost::thread*> jobs;
159 
160  for ( size_t i=0; i!=N_THREADS; ++i )
161  {
162  jobs.push_back ( new boost::thread ( job_single,hw ) );
163  }
164 
165  for ( size_t i=0; i!=N_THREADS; ++i )
166  {
167  jobs[i]->join();
168  delete jobs[i];
169  }
170  }
171 }
172 )
173 
174 
175 void job_single_copied ( HwInterface hw )
176 {
178  uint32_t x = static_cast<uint32_t> ( rand() );
179  hw.getNode ( "REG" ).write ( x );
180  ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
181  std::vector<uint32_t> xx;
182 
183  for ( size_t i=0; i!= N_SIZE; ++i )
184  {
185  xx.push_back ( static_cast<uint32_t> ( 0xDEADBEEF /*rand()*/ ) );
186  }
187 
188  hw.getNode ( "MEM" ).writeBlock ( xx );
189  ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
190  hw.dispatch();
191  BOOST_CHECK ( reg.valid() );
192  BOOST_CHECK ( mem.valid() );
193  BOOST_CHECK_EQUAL ( mem.size(), N_SIZE );
194  );
195 }
196 
197 
198 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_copied_hwinterface, DummyHardwareFixture,
199 {
200  for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
201  {
202  HwInterface hw = getHwInterface();
203  std::vector<boost::thread*> jobs;
204 
205  for ( size_t i=0; i!=N_THREADS; ++i )
206  {
207  jobs.push_back ( new boost::thread ( job_single_copied,hw ) );
208  }
209 
210  for ( size_t i=0; i!=N_THREADS; ++i )
211  {
212  jobs[i]->join();
213  delete jobs[i];
214  }
215  }
216 }
217 )
218 
219 
220 } // end ns tests
221 } // end ns uhal
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:59
#define N_THREADS
UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(BlockReadWriteTestSuite, block_write_read, DummyHardwareFixture, { std::vector< size_t > lDepths=getBlockUnitTestDepths();for(size_t i=0;i< lDepths.size();i++) { const size_t N=lDepths.at(i);BOOST_TEST_MESSAGE(" N = "<< N);HwInterface hw=getHwInterface();std::vector< uint32_t > xx;xx.reserve(N);for(size_t i=0;i!=N;++i) { xx.push_back(static_cast< uint32_t >(rand()));} hw.getNode("LARGE_MEM").writeBlock(xx);ValVector< uint32_t > mem=hw.getNode("LARGE_MEM").readBlock(N);BOOST_CHECK(!mem.valid());BOOST_CHECK_EQUAL(mem.size(), N);if(N > 0) { BOOST_CHECK_THROW(mem.at(0), uhal::exception::NonValidatedMemory);} BOOST_CHECK_THROW(mem.value(), uhal::exception::NonValidatedMemory);BOOST_CHECK_NO_THROW(hw.dispatch());BOOST_CHECK(mem.valid());BOOST_CHECK_EQUAL(mem.size(), N);if(N< N_10MB) { bool correct_block_write_read=true;std::vector< uint32_t >::const_iterator j=xx.begin();for(ValVector< uint32_t >::const_iterator i(mem.begin());i!=mem.end();++i,++j) { correct_block_write_read=correct_block_write_read &&(*i== *j);} BOOST_CHECK(correct_block_write_read);} } }) UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(BlockReadWriteTestSuite
#define N_SIZE
#define N_ITERATIONS
void dispatch()
Make the IPbus client issue a dispatch.
HwInterface getDevice(const std::string &aId)
Retrieves protocol, host, and port from the connection file to create an IPbus Client Retrieves the a...
WarningLevel Warning
Definition: LogLevels.cpp:79
ValWord< uint32_t > reg
BOOST_CHECK(!mem.valid())
void job_multiple(const std::string &connection, const std::string &id)
#define TIMEOUT_S
ValHeader write(const uint32_t &aValue) const
Write a single, unmasked word to a register.
Definition: Node.cpp:494
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:290
ValVector< uint32_t > readBlock(const uint32_t &aSize) const
Read a block of unsigned data from a block of registers or a block-read port.
Definition: Node.cpp:610
HwInterface hw
BOOST_CHECK_EQUAL(hw.getNode("SUBSYSTEM1.SUBMODULE.REG").getAddress(), hw.getNode("SUBSYSTEM1").getNode("SUBMODULE").getNode("REG").getAddress())
BOOST_CHECK_NO_THROW(hw.getNode("REG").writeBlock(xx))
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:140
std::vector< uint32_t > xx
Definition: test_block.cpp:238
void setTimeoutPeriod(const uint32_t &aTimeoutPeriod)
A method to modify the timeout period for any pending or future transactions.
std::size_t size() const
Return the size of the underlying memory.
Definition: ValMem.cpp:349
ValVector< uint32_t > mem
#define ThisLocation()
InfoLevel Info
Definition: LogLevels.cpp:114
ValWord< uint32_t > read() const
Read a single, unmasked, unsigned word.
Definition: Node.cpp:589
const Node & getNode() const
Ping the target for this client.
A class to open and manage XML connection files and wrap up the interfaces to the NodeTreeBuilder and...
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
ValHeader writeBlock(const std::vector< uint32_t > &aValues) const
Write a block of data to a block of registers or a block-write port.
Definition: Node.cpp:520