μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <iostream>
36#include <cstdlib>
37#include <thread>
38#include <typeinfo>
39
40// Linux C++ headers
41#include <sys/time.h>
42
43#include <boost/date_time/posix_time/posix_time.hpp>
44#include <boost/test/unit_test.hpp>
45
46#include "uhal/uhal.hpp"
49#include "uhal/tests/tools.hpp"
50#include "uhal/log/log.hpp"
51
52
53#define N_THREADS 5
54#define N_ITERATIONS 5
55#define N_SIZE uint32_t(10*1024/4)
56#define TIMEOUT_MULTIPLIER 50
57
58
59namespace uhal {
60namespace tests {
61
62
63void job_multiple ( const std::string& connection, const std::string& id, const size_t timeout )
64{
66
67 for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
68 {
69 log ( Info() , "Iteration " , Integer ( iter ) );
70 ConnectionManager manager ( connection );
71 HwInterface hw = manager.getDevice ( id );
73
74 uint32_t x = static_cast<uint32_t> ( rand() );
75 hw.getNode ( "REG" ).write ( x );
77 std::vector<uint32_t> xx;
78
79 for ( size_t i=0; i!= N_SIZE; ++i )
80 {
81 xx.push_back ( static_cast<uint32_t> ( rand() ) );
82 }
83
84 hw.getNode ( "MEM" ).writeBlock ( xx );
86 log ( Warning() , ThisLocation() );
87 hw.dispatch();
88 log ( Warning() , ThisLocation() );
89 BOOST_CHECK ( reg.valid() );
90 BOOST_CHECK ( mem.valid() );
92 //can not check content in the mutlithreaded case
93 }
94 );
95}
96
97
98UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, multiple_hwinterfaces, DummyHardwareFixture,
99{
100 std::vector<std::shared_ptr<std::thread>> jobs;
101
102 for ( size_t i=0; i!=N_THREADS; ++i )
103 {
104 log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
105 jobs.emplace_back ( new std::thread(job_multiple, connectionFileURI, deviceId, timeout) );
106 }
107
108 for ( size_t i=0; i!=N_THREADS; ++i )
109 {
110 log ( Warning() , ThisLocation() , ":" , Integer ( i ) );
111 //boost::posix_time::time_duration timeout = boost::posix_time::seconds ( TIMEOUT_S );
112 //CACTUS_CHECK ( jobs[i]->timed_join ( timeout ) );
113 jobs[i]->join();
114 }
115
116 log ( Warning() , ThisLocation() );
117}
118)
119
120
121void job_single ( HwInterface& hw )
122{
124 uint32_t x = static_cast<uint32_t> ( rand() );
125 hw.getNode ( "REG" ).write ( x );
126 ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
127 std::vector<uint32_t> xx;
128
129 for ( size_t i=0; i!= N_SIZE; ++i )
130 {
131 xx.push_back ( static_cast<uint32_t> ( rand() ) );
132 }
133
134 hw.getNode ( "MEM" ).writeBlock ( xx );
135 ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
136 hw.dispatch();
137 BOOST_CHECK ( reg.valid() );
138 BOOST_CHECK ( mem.valid() );
140 );
141 //can not check content in the mutlithreaded case
142}
143
144
145UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_hwinterface, DummyHardwareFixture,
146{
147 for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
148 {
149 HwInterface hw = getHwInterface();
150 std::vector<std::shared_ptr<std::thread>> jobs;
151
152 for ( size_t i=0; i!=N_THREADS; ++i )
153 {
154 jobs.emplace_back ( new std::thread( [&hw] () {job_single(hw);} ) );
155 }
156
157 for ( size_t i=0; i!=N_THREADS; ++i )
158 {
159 jobs[i]->join();
160 }
161 }
162}
163)
164
165
166void job_single_copied ( HwInterface hw )
167{
169 uint32_t x = static_cast<uint32_t> ( rand() );
170 hw.getNode ( "REG" ).write ( x );
171 ValWord< uint32_t > reg = hw.getNode ( "REG" ).read();
172 std::vector<uint32_t> xx;
173
174 for ( size_t i=0; i!= N_SIZE; ++i )
175 {
176 xx.push_back ( static_cast<uint32_t> ( 0xDEADBEEF /*rand()*/ ) );
177 }
178
179 hw.getNode ( "MEM" ).writeBlock ( xx );
180 ValVector< uint32_t > mem = hw.getNode ( "MEM" ).readBlock ( N_SIZE );
181 hw.dispatch();
182 BOOST_CHECK ( reg.valid() );
183 BOOST_CHECK ( mem.valid() );
185 );
186}
187
188
189UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(MultithreadedTestSuite, single_copied_hwinterface, DummyHardwareFixture,
190{
191 for ( size_t iter=0; iter!= N_ITERATIONS ; ++iter )
192 {
193 HwInterface hw = getHwInterface();
194 std::vector<std::shared_ptr<std::thread>> jobs;
195
196 for ( size_t i=0; i!=N_THREADS; ++i )
197 {
198 jobs.emplace_back ( new std::thread ( [hw] () {job_single_copied(hw);} ) );
199 }
200
201 for ( size_t i=0; i!=N_THREADS; ++i )
202 {
203 jobs[i]->join();
204 }
205 jobs.clear();
206 }
207}
208)
209
210
211} // end ns tests
212} // end ns uhal
A class to open and manage XML connection files and wrap up the interfaces to the NodeTreeBuilder and...
HwInterface getDevice(const std::string &aId)
Retrieves protocol, host, and port from the connection file to create an IPbus Client Retrieves the a...
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:56
const Node & getNode() const
Retrieve the top-level node.
void dispatch()
Make the IPbus client issue a dispatch.
void setTimeoutPeriod(const uint32_t &aTimeoutPeriod)
A method to modify the timeout period for any pending or future transactions.
ValWord< uint32_t > read() const
Read a single, unmasked, unsigned word.
Definition: Node.cpp:611
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:542
ValHeader write(const uint32_t &aValue) const
Write a single, unmasked word to a register.
Definition: Node.cpp:516
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:632
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:217
std::size_t size() const
Return the size of the underlying memory.
Definition: ValMem.cpp:280
bool valid()
Return whether the Validated memory is marked as valid.
Definition: ValMem.cpp:112
iterator iter(handle obj)
Definition: pytypes.h:2042
#define ThisLocation()
BOOST_CHECK_EQUAL(mem.size(), N)
c write(addr, xx[0])
BOOST_CHECK_NO_THROW(hw.getNode("REG").writeBlock(xx))
ValVector< uint32_t > mem
BOOST_CHECK(!mem.valid())
HwInterface hw
c writeBlock(addr, xx)
uint32_t x
Definition: test_single.cpp:93
std::vector< uint32_t > xx
Definition: test_block.cpp:249
ValWord< uint32_t > reg
void job_multiple(const std::string &connection, const std::string &id, const size_t timeout)
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:18
WarningLevel Warning
Definition: LogLevels.cpp:79
InfoLevel Info
Definition: LogLevels.cpp:115
#define N_ITERATIONS
#define TIMEOUT_MULTIPLIER
#define N_THREADS
#define N_SIZE
#define UHAL_TESTS_DEFINE_CLIENT_TEST_CASES(test_suite_name, test_case_name, test_fixture, test_case_contents)
Definition: definitions.hpp:53