μHAL (v2.7.9)
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_MULTIPLIER 50
59 
60 
61 namespace uhal {
62 namespace tests {
63 
64 
65 void job_multiple ( const std::string& connection, const std::string& id, const size_t timeout )
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 );
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() );
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  std::vector<boost::thread*> jobs;
103 
104  for ( size_t i=0; i!=N_THREADS; ++i )
105  {
106  log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
107  jobs.push_back ( new boost::thread ( job_multiple, connectionFileURI, deviceId, timeout ) );
108  }
109 
110  for ( size_t i=0; i!=N_THREADS; ++i )
111  {
112  log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
113  //boost::posix_time::time_duration timeout = boost::posix_time::seconds ( TIMEOUT_S );
114  //CACTUS_CHECK ( jobs[i]->timed_join ( timeout ) );
115  jobs[i]->join();
116  delete jobs[i];
117  }
118 
119  log ( Warning() , ThisLocation() );
120 }
121 )
122 
123 
124 void job_single ( HwInterface& hw )
125 {
127  uint32_t x = static_cast<uint32_t> ( rand() );
128  hw.getNode ( "REG" ).write ( x );
129  ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
130  std::vector<uint32_t> xx;
131 
132  for ( size_t i=0; i!= N_SIZE; ++i )
133  {
134  xx.push_back ( static_cast<uint32_t> ( rand() ) );
135  }
136 
137  hw.getNode ( "MEM" ).writeBlock ( xx );
138  ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
139  hw.dispatch();
140  BOOST_CHECK ( reg.valid() );
141  BOOST_CHECK ( mem.valid() );
143  );
144  //can not check content in the mutlithreaded case
145 }
146 
147 
148 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_hwinterface, DummyHardwareFixture,
149 {
150  for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
151  {
152  HwInterface hw = getHwInterface();
153  std::vector<boost::thread*> jobs;
154 
155  for ( size_t i=0; i!=N_THREADS; ++i )
156  {
157  jobs.push_back ( new boost::thread ( job_single,hw ) );
158  }
159 
160  for ( size_t i=0; i!=N_THREADS; ++i )
161  {
162  jobs[i]->join();
163  delete jobs[i];
164  }
165  }
166 }
167 )
168 
169 
170 void job_single_copied ( HwInterface hw )
171 {
173  uint32_t x = static_cast<uint32_t> ( rand() );
174  hw.getNode ( "REG" ).write ( x );
175  ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
176  std::vector<uint32_t> xx;
177 
178  for ( size_t i=0; i!= N_SIZE; ++i )
179  {
180  xx.push_back ( static_cast<uint32_t> ( 0xDEADBEEF /*rand()*/ ) );
181  }
182 
183  hw.getNode ( "MEM" ).writeBlock ( xx );
184  ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
185  hw.dispatch();
186  BOOST_CHECK ( reg.valid() );
187  BOOST_CHECK ( mem.valid() );
189  );
190 }
191 
192 
193 UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_copied_hwinterface, DummyHardwareFixture,
194 {
195  for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
196  {
197  HwInterface hw = getHwInterface();
198  std::vector<boost::thread*> jobs;
199 
200  for ( size_t i=0; i!=N_THREADS; ++i )
201  {
202  jobs.push_back ( new boost::thread ( job_single_copied,hw ) );
203  }
204 
205  for ( size_t i=0; i!=N_THREADS; ++i )
206  {
207  jobs[i]->join();
208  delete jobs[i];
209  }
210  }
211 }
212 )
213 
214 
215 } // end ns tests
216 } // end ns uhal
tools.hpp
uhal::HwInterface
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:61
uhal::ValVector< uint32_t >
uhal::ConnectionManager::getDevice
HwInterface getDevice(const std::string &aId)
Retrieves protocol, host, and port from the connection file to create an IPbus Client Retrieves the a...
Definition: ConnectionManager.cpp:140
uhal::Node::readBlock
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:606
TIMEOUT_MULTIPLIER
#define TIMEOUT_MULTIPLIER
Definition: test_multithreaded.cpp:58
uhal::Node::writeBlock
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:516
fixtures.hpp
uhal::ValVector::valid
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:217
uhal::tests::reg
ValWord< uint32_t > reg
Definition: test_rawclient.cpp:194
uhal::tests::BOOST_CHECK
BOOST_CHECK(!mem.valid())
ThisLocation
#define ThisLocation()
Definition: log_inserters.location.hpp:38
uhal::HwInterface::setTimeoutPeriod
void setTimeoutPeriod(const uint32_t &aTimeoutPeriod)
A method to modify the timeout period for any pending or future transactions.
Definition: HwInterface.cpp:124
uhal::tests::writeBlock
c writeBlock(addr, xx)
uhal::HwInterface::dispatch
void dispatch()
Make the IPbus client issue a dispatch.
Definition: HwInterface.cpp:106
definitions.hpp
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::tests::hw
HwInterface hw
Definition: test_rawclient.cpp:93
uhal::tests::job_multiple
void job_multiple(const std::string &connection, const std::string &id, const size_t timeout)
Definition: test_multithreaded.cpp:65
uhal::tests::x
uint32_t x
Definition: test_single.cpp:93
uhal::tests::DummyHardwareFixture
Definition: fixtures.hpp:135
uhal::Info
InfoLevel Info
Definition: LogLevels.cpp:115
uhal::log
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:20
uhal::Integer
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
Definition: log_inserters.integer.hxx:43
uhal::tests::xx
std::vector< uint32_t > xx
Definition: test_block.cpp:247
uhal::tests::UHAL_TESTS_DEFINE_CLIENT_TEST_CASES
UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(BlockReadWriteTestSuite, block_write_read, DummyHardwareFixture, { std::vector< size_t > lDepths=getBlockUnitTestDepths(quickTest ? N_1MB :N_10MB);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
uhal::Warning
WarningLevel Warning
Definition: LogLevels.cpp:79
N_ITERATIONS
#define N_ITERATIONS
Definition: test_multithreaded.cpp:56
uhal.hpp
uhal::Node::read
ValWord< uint32_t > read() const
Read a single, unmasked, unsigned word.
Definition: Node.cpp:585
uhal::tests::DummyHardwareFixture
DummyHardwareFixture
Definition: test_block.cpp:131
uhal::ConnectionManager
A class to open and manage XML connection files and wrap up the interfaces to the NodeTreeBuilder and...
Definition: ConnectionManager.hpp:79
uhal::ValVector::size
std::size_t size() const
Return the size of the underlying memory.
Definition: ValMem.cpp:280
uhal::tests::BOOST_CHECK_NO_THROW
BOOST_CHECK_NO_THROW(hw.getNode("REG").writeBlock(xx))
log.hpp
uhal::ValWord< uint32_t >
uhal::tests::mem
ValVector< uint32_t > mem
Definition: test_rawclient.cpp:104
uhal::ValWord::valid
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:112
uhal::tests::BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(mem.size(), N)
uhal::Node::write
ValHeader write(const uint32_t &aValue) const
Write a single, unmasked word to a register.
Definition: Node.cpp:490
uhal::HwInterface::getNode
const Node & getNode() const
Retrieve the top-level node.
Definition: HwInterface.cpp:135
N_THREADS
#define N_THREADS
Definition: test_multithreaded.cpp:55
uhal::tests::write
c write(addr, xx[0])
N_SIZE
#define N_SIZE
Definition: test_multithreaded.cpp:57