Skip to content

Commit 9eb9b08

Browse files
committed
using memory pool from base class
1 parent 5e096cb commit 9eb9b08

File tree

2 files changed

+33
-73
lines changed

2 files changed

+33
-73
lines changed

src/ReadoutEquipmentCruEmulator.cxx

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ class ReadoutEquipmentCruEmulator : public ReadoutEquipment {
1919
Thread::CallbackResult prepareBlocks();
2020

2121
private:
22-
std::shared_ptr<MemoryHandler> mh; // a memory pool from which to allocate data pages
2322
Thread::CallbackResult populateFifoOut(); // iterative callback
2423

2524
DataBlockId currentId; // current block id
2625

27-
int memPoolNumberOfElements; // number of pages in memory pool
28-
int memPoolElementSize; // size of each page
29-
3026
int cfgNumberOfLinks; // number of links to simulate. Will create data blocks round-robin.
3127
int cfgFeeId; // FEE id to be used
3228
int cfgLinkId; // Link id to be used (base number - will be incremented if multiple links selected)
@@ -36,32 +32,17 @@ class ReadoutEquipmentCruEmulator : public ReadoutEquipment {
3632
const unsigned int LHCBCRate=LHCOrbitRate*LHCBunches; // LHC bunch crossing rate, in Hz
3733

3834
int cruBlockSize; // size of 1 data block (RDH+payload)
39-
40-
// interval in BC clocks between two CRU block transfers, based on link input data rate
41-
int bcStep;
42-
35+
int bcStep; // interval in BC clocks between two CRU block transfers, based on link input data rate
4336

4437
int cfgTFperiod=256; // duration of a timeframe, in number of LHC orbits
4538
int cfgHBperiod=1; // interval between 2 HeartBeat triggers, in number of LHC orbits
4639
double cfgGbtLinkThroughput=3.2; // input link data rate in Gigabits/s per second, for one link (GBT=3.2 or 4.8 gbps)
4740

48-
4941
int cfgMaxBlocksPerPage; // max number of CRU blocks per page (0 => fill the page)
50-
/*
51-
int cfgNumberOfBlocksPerTrigger; // number of CRU blocks for 1 trigger
52-
int randomize;
53-
int pagesToGoForCurrentLink; // number of data pages left to send for current link
54-
int currentLink; // id of current Link sending data
55-
*/
5642

5743
uint32_t LHCorbit=0; // current LHC orbit
5844
uint32_t LHCbc=0; // current LHC bunch crossing
59-
// uint32_t HBid=0; // id of last HB frame received
60-
61-
// int TFperiod=256; // duration of a time frame, in number of LHC orbits
62-
// int HBperiod=1; // interval betweenHB triggers, in number of LHC orbits
6345

64-
6546
Timer elapsedTime; // elapsed time since equipment started
6647
double t0=0; // time of first block generated
6748

@@ -71,33 +52,16 @@ class ReadoutEquipmentCruEmulator : public ReadoutEquipment {
7152

7253
ReadoutEquipmentCruEmulator::ReadoutEquipmentCruEmulator(ConfigFile &cfg, std::string cfgEntryPoint) : ReadoutEquipment(cfg, cfgEntryPoint) {
7354

74-
7555
// get configuration values
76-
cfg.getOptionalValue<int>(cfgEntryPoint + ".memPoolNumberOfElements", memPoolNumberOfElements,10000);
77-
std::string cfgMemPoolElementSize;
78-
cfg.getOptionalValue<std::string>(cfgEntryPoint + ".memPoolElementSize", cfgMemPoolElementSize);
79-
memPoolElementSize=ReadoutUtils::getNumberOfBytesFromString(cfgMemPoolElementSize.c_str());
80-
if (memPoolElementSize<=0) {
81-
memPoolElementSize=1024*1024;
82-
}
8356
cfg.getOptionalValue<int>(cfgEntryPoint + ".maxBlocksPerPage", cfgMaxBlocksPerPage, (int)0);
8457
cfg.getOptionalValue<int>(cfgEntryPoint + ".cruBlockSize", cruBlockSize, (int)8192);
8558
cfg.getOptionalValue<int>(cfgEntryPoint + ".numberOfLinks", cfgNumberOfLinks, (int)1);
8659
cfg.getOptionalValue<int>(cfgEntryPoint + ".feeId", cfgFeeId, (int)0);
8760
cfg.getOptionalValue<int>(cfgEntryPoint + ".linkId", cfgLinkId, (int)0);
8861

8962
// log config summary
90-
theLog.log("Config summary: memPoolNumberOfElements=%d memPoolElementSize=%d maxBlocksPerPage=%d cruBlockSize=%d numberOfLinks=%d feeId=%d linkId=%d",
91-
memPoolNumberOfElements, memPoolElementSize, cfgMaxBlocksPerPage, cruBlockSize, cfgNumberOfLinks, cfgFeeId, cfgLinkId);
92-
93-
// create memory pool
94-
if (bigBlock==nullptr) {
95-
theLog.log("big block unavailable for output");
96-
throw __LINE__;
97-
} else {
98-
theLog.log("Using big block @ %p",bigBlock->ptr);
99-
mh=std::make_shared<MemoryHandler>(memPoolElementSize,memPoolNumberOfElements);
100-
}
63+
theLog.log("Equipment %s: maxBlocksPerPage=%d cruBlockSize=%d numberOfLinks=%d feeId=%d linkId=%d",
64+
name.c_str(), cfgMaxBlocksPerPage, cruBlockSize, cfgNumberOfLinks, cfgFeeId, cfgLinkId);
10165

10266
// init variables
10367
currentId=1; // TFid starts on 1
@@ -110,10 +74,13 @@ ReadoutEquipmentCruEmulator::ReadoutEquipmentCruEmulator(ConfigFile &cfg, std::s
11074

11175
// output queue: 1 block per link
11276
readyBlocks=std::make_unique<AliceO2::Common::Fifo<DataBlockContainerReference>>(cfgNumberOfLinks);
77+
if (readyBlocks==nullptr) {
78+
throw __LINE__;
79+
}
11380

11481
// init parameters
11582
bcStep=(int)(LHCBCRate*((cruBlockSize-sizeof(o2::Header::RAWDataHeader))*1.0/(cfgGbtLinkThroughput*1024*1024*1024/8)));
116-
theLog.log("Using block rate = %d BC",bcStep);
83+
theLog.log("Equipment %s: using block rate = %d BC", name.c_str(), bcStep);
11784
}
11885

11986
ReadoutEquipmentCruEmulator::~ReadoutEquipmentCruEmulator() {
@@ -122,9 +89,8 @@ ReadoutEquipmentCruEmulator::~ReadoutEquipmentCruEmulator() {
12289

12390

12491
Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
125-
/*
126-
cru emulator creates a set of data pages for each link and put them in the fifo to be retrieve by getNextBlock
127-
*/
92+
93+
// cru emulator creates a set of data pages for each link and put them in the fifo to be retrieve by getNextBlock
12894

12995
// check that we don't go faster than LHC...
13096
double t=elapsedTime.getTime();
@@ -148,13 +114,13 @@ Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
148114
// query memory pool for a free block
149115
DataBlockContainerReference nextBlock=nullptr;
150116
try {
151-
//nextBlock=std::make_shared<DataBlockContainerFromMemPool>(mp);
152-
nextBlock=std::make_shared<DataBlockContainerFromMemoryHandler>(mh);
117+
nextBlock=mp->getNewDataBlockContainer();
153118
}
154119
catch (...) {
155120
}
156121
if (nextBlock==nullptr) {
157122
// no pages left, retry later
123+
// todo: check how long we starve pages. monitor this counter.
158124
return Thread::CallbackResult::Idle;
159125
}
160126
pendingBlocks[i]=nextBlock;
@@ -172,8 +138,10 @@ Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
172138
for (int currentLink=0; currentLink<cfgNumberOfLinks; currentLink++) {
173139

174140
// fill the new data page for this link
175-
DataBlock *b=pendingBlocks[currentLink]->getData();
141+
DataBlock *b=pendingBlocks[currentLink]->getData();
176142

143+
//printf ("data block %p data=%p\n",b,b->data);
144+
177145
int offset; // number of bytes used in page
178146
int nBlocksInPage=0;
179147

@@ -182,8 +150,10 @@ Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
182150
unsigned int nowId=currentId;
183151

184152
int linkId=cfgLinkId+currentLink;
153+
int bytesAvailableInPage=b->header.dataSize; // a bit less than memoryPoolPageSize;
154+
//printf("bytes available: %d bytes\n",bytesAvailableInPage);
185155

186-
for (offset=0;offset+cruBlockSize<=memPoolElementSize;offset+=cruBlockSize) {
156+
for (offset=0;offset+cruBlockSize<=bytesAvailableInPage;offset+=cruBlockSize) {
187157

188158
unsigned int nextBc=nowBc+bcStep;
189159
unsigned int nextOrbit=nowOrbit;
@@ -212,6 +182,8 @@ Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
212182
// https://docs.google.com/document/d/1KUoLnEw5PndVcj4FKR5cjV-MBN3Bqfx_B0e6wQOIuVE/edit#heading=h.5q65he8hp62c
213183

214184
o2::Header::RAWDataHeader *rdh=(o2::Header::RAWDataHeader *)&b->data[offset];
185+
//printf("rdh=%p block=%p delta=%d\n",rdh,b->data,(int)((char *)rdh-(char *)b->data));
186+
215187
*rdh=defaultRDH; // reset fields to defaults
216188
rdh->blockLength=(uint16_t)cruBlockSize;
217189
rdh->triggerOrbit=nowOrbit;
@@ -227,7 +199,9 @@ Thread::CallbackResult ReadoutEquipmentCruEmulator::prepareBlocks() {
227199

228200
// size used (bytes) in page is last offset
229201
int dSize=offset;
230-
202+
203+
//printf("wrote %d bytes\n",dSize);
204+
231205
b->header.blockType=DataBlockType::H_BASE;
232206
b->header.headerSize=sizeof(DataBlockHeaderBase);
233207
b->header.dataSize=dSize;

src/ReadoutEquipmentDummy.cxx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ReadoutEquipmentDummy : public ReadoutEquipment {
1515
DataBlockContainerReference getNextBlock();
1616

1717
private:
18-
std::shared_ptr<MemoryPagesPool> mp; // a memory pool from which to allocate data pages
1918
Thread::CallbackResult populateFifoOut(); // iterative callback
2019

2120
DataBlockId currentId; // current block id
@@ -26,52 +25,38 @@ class ReadoutEquipmentDummy : public ReadoutEquipment {
2625

2726
ReadoutEquipmentDummy::ReadoutEquipmentDummy(ConfigFile &cfg, std::string cfgEntryPoint) : ReadoutEquipment(cfg, cfgEntryPoint) {
2827

29-
int memoryPoolPageSize=0.01*1024*1024;
30-
int memoryPoolNumberOfPages=10000;
31-
std::string memoryBankName=""; // by default, this uses the first memory bank available
32-
3328
// get configuration values
34-
cfg.getOptionalValue<std::string>(cfgEntryPoint + ".memoryBankName", memoryBankName);
35-
cfg.getOptionalValue<int>(cfgEntryPoint + ".memoryPoolPageSize", memoryPoolPageSize);
36-
cfg.getOptionalValue<int>(cfgEntryPoint + ".memoryPoolNumberOfPages", memoryPoolNumberOfPages);
37-
cfg.getOptionalValue<int>(cfgEntryPoint + ".eventMaxSize", eventMaxSize, (int)1024);
38-
cfg.getOptionalValue<int>(cfgEntryPoint + ".eventMinSize", eventMinSize, (int)1024);
29+
cfg.getOptionalValue<int>(cfgEntryPoint + ".eventMaxSize", eventMaxSize, (int)128*1024);
30+
cfg.getOptionalValue<int>(cfgEntryPoint + ".eventMinSize", eventMinSize, (int)128*1024);
3931
cfg.getOptionalValue<int>(cfgEntryPoint + ".fillData", fillData, (int)0);
4032

41-
theLog.log("%s : buffer %d pages x %d bytes, eventSize: %d -> %d",
42-
cfgEntryPoint.c_str(),(int) memoryPoolNumberOfPages, (int) memoryPoolPageSize, eventMinSize, eventMaxSize);
43-
33+
// log config summary
34+
theLog.log("Equipment %s: eventSize: %d -> %d, fillData=%d", name.c_str(), eventMinSize, eventMaxSize, fillData);
4435

4536
// ensure generated events will fit in blocks allocated from memory pool
4637
int maxElementSize=eventMaxSize+sizeof(DataBlockHeaderBase);
4738
if (maxElementSize>memoryPoolPageSize) {
48-
theLog.log("memoryPoolPageSize too small, need at least %d bytes",maxElementSize);
39+
theLog.log("memoryPoolPageSize too small, need at least %d bytes", maxElementSize);
4940
throw __LINE__;
5041
}
5142

52-
// create memory pool
53-
mp=theMemoryBankManager.getPagedPool(memoryPoolPageSize, memoryPoolNumberOfPages, memoryBankName);
54-
5543
// init variables
5644
currentId=0;
5745
}
5846

5947
ReadoutEquipmentDummy::~ReadoutEquipmentDummy() {
60-
// check if mempool still referenced
61-
if (!mp.unique()) {
62-
printf("Warning: mempool still has %d references\n",(int)mp.use_count());
63-
}
6448
}
6549

6650
DataBlockContainerReference ReadoutEquipmentDummy::getNextBlock() {
67-
6851
// query memory pool for a free block
6952
DataBlockContainerReference nextBlock=nullptr;
7053
try {
7154
nextBlock=mp->getNewDataBlockContainer();
7255
}
7356
catch (...) {
7457
}
58+
59+
// format data block
7560
if (nextBlock!=nullptr) {
7661
DataBlock *b=nextBlock->getData();
7762

@@ -87,16 +72,17 @@ DataBlockContainerReference ReadoutEquipmentDummy::getNextBlock() {
8772
b->header.dataSize=dSize;
8873
b->header.id=currentId;
8974
// say it's contiguous header+data
90-
// todo: align begin of data
75+
// todo: align begin of data ?
9176
b->data=&(((char *)b)[sizeof(DataBlock)]);
9277

93-
// fill (a bit of) data
78+
// optionaly fill data range
9479
if (fillData==1) {
9580
for (int k=0;k<dSize;k++) {
9681
b->data[k]=(char)k;
9782
}
9883
}
99-
}
84+
}
85+
10086
return nextBlock;
10187
}
10288

0 commit comments

Comments
 (0)