μHAL (v2.7.9)
Part of the IPbus software repository
tools.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/tests/tools.hpp"
36 
37 
38 #include <fcntl.h>
39 #include <vector>
40 
41 #include "uhal/ProtocolPCIe.hpp"
43 
44 
45 namespace uhal {
46 namespace tests {
47 
48 
50  mHw(aHw),
51  mHwThread(boost::bind(&DummyHardwareInterface::run, aHw))
52 {
53 }
54 
56 {
57  mHw->stop();
58  mHwThread.join();
59 }
60 
61 void DummyHardwareRunner::setReplyDelay(const boost::chrono::microseconds& aDelay)
62 {
63  mHw->setReplyDelay(aDelay);
64 }
65 
66 
67 double measureReadLatency(ClientInterface& aClient, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
68 {
69  return measureReadLatency(std::vector<ClientInterface*>(1, &aClient), aBaseAddr, aDepth, aNrIterations, aDispatchEachIteration, aVerbose);
70 }
71 
72 
73 double measureReadLatency(const std::vector<ClientInterface*>& aClients, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
74 {
75  typedef std::vector<ClientInterface*>::const_iterator ClientIterator_t;
76  Timer myTimer;
77 
78  for ( unsigned i = 0; i < aNrIterations ; ++i )
79  {
80  if ( aVerbose )
81  {
82  std::cout << "Iteration " << i << std::endl;
83  }
84 
85  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
86  {
87  (*lIt)->readBlock ( aBaseAddr, aDepth, defs::NON_INCREMENTAL );
88  }
89 
90  if ( aDispatchEachIteration )
91  {
92  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
93  {
94  (*lIt)->dispatch();
95  }
96  }
97  }
98 
99  if ( !aDispatchEachIteration )
100  {
101  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
102  {
103  (*lIt)->dispatch();
104  }
105  }
106 
107  return myTimer.elapsedSeconds();
108 }
109 
110 
111 double measureWriteLatency(ClientInterface& aClient, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
112 {
113  return measureWriteLatency(std::vector<ClientInterface*>(1, &aClient), aBaseAddr, aDepth, aNrIterations, aDispatchEachIteration, aVerbose);
114 }
115 
116 
117 double measureWriteLatency(const std::vector<ClientInterface*>& aClients, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
118 {
119  typedef std::vector<ClientInterface*>::const_iterator ClientIterator_t;
120 
121  // Send buffer - lots of "cafebabe" (in little-endian)
122  std::vector<uint32_t> sendBuffer ( aDepth, 0xbebafeca );
123  Timer myTimer;
124 
125  for ( unsigned i = 0; i < aNrIterations ; ++i )
126  {
127  if ( aVerbose )
128  {
129  std::cout << "Iteration " << i << std::endl;
130  }
131 
132  // Create the packet
133  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
134  {
135  (*lIt)->writeBlock ( aBaseAddr, sendBuffer, defs::NON_INCREMENTAL );
136  }
137 
138  if ( aDispatchEachIteration )
139  {
140  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
141  {
142  (*lIt)->dispatch();
143  }
144  }
145  }
146 
147  if ( !aDispatchEachIteration )
148  {
149  for (ClientIterator_t lIt = aClients.begin(); lIt != aClients.end(); lIt++)
150  {
151  (*lIt)->dispatch();
152  }
153  }
154 
155  return myTimer.elapsedSeconds();
156 }
157 
158 
159 double measureFileReadLatency(const std::string& aFilePath, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aVerbose)
160 {
161  PCIe::File lFile(aFilePath, O_RDWR | O_NONBLOCK);
162  lFile.open();
163 
164  std::vector<uint32_t> lRecvBuffer;
165  lFile.read(aBaseAddr, aDepth, lRecvBuffer);
166  lRecvBuffer.clear();
167 
168  Timer myTimer;
169  for ( unsigned i = 0; i < aNrIterations ; ++i )
170  {
171  if ( aVerbose )
172  {
173  std::cout << "Iteration " << i << std::endl;
174  }
175 
176  lFile.read(aBaseAddr, aDepth, lRecvBuffer);
177  lRecvBuffer.clear();
178  }
179 
180  return myTimer.elapsedSeconds();
181 }
182 
183 
184 double measureFileWriteLatency(const std::string& aFilePath, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aVerbose)
185 {
186  PCIe::File lFile(aFilePath, O_RDWR);
187  std::vector<uint32_t> lSendBuffer(aDepth, 0x0);
188  lFile.write(aBaseAddr, lSendBuffer);
189 
190  Timer myTimer;
191  for ( unsigned i = 0; i < aNrIterations ; ++i )
192  {
193  if ( aVerbose )
194  {
195  std::cout << "Iteration " << i << std::endl;
196  }
197 
198  lFile.write(aBaseAddr, lSendBuffer);
199  }
200 
201  return myTimer.elapsedSeconds();
202 }
203 
204 
205 } // end ns tests
206 } // end ns uhal
tools.hpp
uhal::tests::measureFileReadLatency
double measureFileReadLatency(const std::string &aFilePath, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aVerbose)
Definition: tools.cpp:159
ProtocolPCIe.hpp
uhal::PCIe::File::open
void open()
Definition: ProtocolPCIe.cpp:142
uhal::tests::DummyHardwareRunner::setReplyDelay
void setReplyDelay(const boost::chrono::microseconds &aDelay)
Definition: tools.cpp:61
uhal::ClientInterface
An abstract base class for defining the interface to the various IPbus clients as well as providing t...
Definition: ClientInterface.hpp:100
boost
Definition: log.hpp:13
uhal::PCIe::File::write
void write(const uint32_t aAddr, const std::vector< uint32_t > &aValues)
Definition: ProtocolPCIe.cpp:225
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::tests::measureWriteLatency
double measureWriteLatency(ClientInterface &aClient, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
Definition: tools.cpp:111
uhal::tests::DummyHardwareRunner::mHw
boost::scoped_ptr< DummyHardwareInterface > mHw
Definition: tools.hpp:67
uhal::tests::Timer
A very simple timer.
Definition: tools.hpp:74
DummyHardware.hpp
uhal::tests::DummyHardwareRunner::~DummyHardwareRunner
~DummyHardwareRunner()
Definition: tools.cpp:55
uhal::defs::NON_INCREMENTAL
@ NON_INCREMENTAL
Definition: definitions.hpp:53
uhal::tests::DummyHardwareRunner::DummyHardwareRunner
DummyHardwareRunner(DummyHardwareInterface *aHw)
Definition: tools.cpp:49
uhal::PCIe::File
Definition: ProtocolPCIe.hpp:100
uhal::tests::DummyHardwareInterface
Common abstract base class for IPbus 1.3 and 2.0 dummy hardware.
Definition: DummyHardware.hpp:66
uhal::PCIe::File::read
void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector< uint32_t > &aValues)
Definition: ProtocolPCIe.cpp:193
uhal::tests::measureFileWriteLatency
double measureFileWriteLatency(const std::string &aFilePath, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aVerbose)
Definition: tools.cpp:184
uhal::tests::DummyHardwareRunner::mHwThread
boost::thread mHwThread
Definition: tools.hpp:68
uhal::tests::Timer::elapsedSeconds
double elapsedSeconds()
Returns number of elapsed seconds since the timer was instantiated.
Definition: tools.hpp:83
uhal::tests::measureReadLatency
double measureReadLatency(ClientInterface &aClient, uint32_t aBaseAddr, uint32_t aDepth, size_t aNrIterations, bool aDispatchEachIteration, bool aVerbose)
Definition: tools.cpp:67