CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RawCache.cc
Go to the documentation of this file.
1 //
3 // RawCache.cc
4 // -------
5 //
6 // Backup for RawMsgBuf messages containing raw FED data.
7 //
8 // Created on: Nov 16, 2011
9 // Andrei Spataru : aspataru@cern.ch
11 
14 #include <iostream>
15 #include <cstdlib>
16 #include <unistd.h>
17 
18 using namespace evf;
19 using std::cout;
20 using std::endl;
21 
23 //______________________________________________________________________________
24 
26  if (instance_ == 0) {
27  instance_ = new RawCache();
28  }
29  return instance_;
30 }
31 
32 void RawCache::initialise(unsigned int nMsgs, unsigned int cellSize) {
33  if (!initialised_) {
34  // set the desired size of the cache
35  nMsgs_ = nMsgs;
36  //set the cell size of all cells
37  cellSize_ = cellSize;
38 
39  msgs_ = new RawMsgBuf*[nMsgs_];
40  // initialise array with flags regarding cache slot usage
41  slotUsage_ = new bool[nMsgs_];
42 
43  // set all slots free and initialise
44  for (unsigned int i = 0; i < nMsgs_; i++) {
45  // not contiguous memory!!
48 
49  slotUsage_[i] = false;
50  }
51  initialised_ = true;
52  }
53 
54 }
55 
57  delete[] slotUsage_;
58  delete[] msgs_;
59 }
60 
62 
63  int freeSlot = getFreeSlot();
64 
65  while (freeSlot == -1) {
66  cout << "NO FREE SLOTS IN CACHE!" << endl;
67  //improve remove print usage
68  printUsage();
69  ::sleep(1);
70  }
71 
72  slotUsage_[freeSlot] = true;
73  return msgs_[freeSlot];
74 
75 }
76 
77 void RawCache::releaseMsg(unsigned int fuResourceId) {
78  RawMsgBuf* found = 0;
79 
80  for (unsigned int i = 0; i < nMsgs_; i++)
81  if (slotUsage_[i])
82  if (msgs_[i]->rawCell()->fuResourceId() == fuResourceId)
83  found = msgs_[i];
84 
85  if (found != 0) {
86  found->rawCell()->clear();
87  setFree(found);
88  //printUsage();
89  } else
90  cout << "RAW MSG BUF corresponding to fuResourceId = " << fuResourceId
91  << "not found in internal allocation list!" << endl;
92 }
93 
95  initialised_(false), nMsgs_(0) {
96 
97 }
98 
99 int RawCache::getFreeSlot() const {
100  for (unsigned int i = 0; i < nMsgs_; i++)
101  if (!slotUsage_[i])
102  return i;
103  return -1;
104 }
105 
107  for (unsigned int i = 0; i < nMsgs_; i++)
108  if (slotUsage_[i])
109  if (msgs_[i] == rmb) {
110  slotUsage_[i] = false;
111  return;
112  }
113  cout << "ERROR: Raw Message Buffer to free at address: " << rmb
114  << " not found in internal allocation list!" << endl;
115 }
116 
117 void RawCache::printUsage() const {
118  cout << "Raw Cache usage: ";
119  for (unsigned int i = 0; i < nMsgs_; i++)
120  cout << slotUsage_[i] << " ";
121  cout << endl;
122 }
bool * slotUsage_
Definition: RawCache.h:69
int i
Definition: DBlmapReader.cc:9
virtual ~RawCache()
Definition: RawCache.cc:56
FUShmRawCell * rawCell()
Definition: RawMsgBuf.h:35
#define MAX_MSG_SIZE
Definition: queue_defs.h:10
void sleep(Duration_t)
Definition: Utils.h:163
RawMsgBuf * getMsgToWrite()
Definition: RawCache.cc:61
static RawCache * getInstance()
Definition: RawCache.cc:25
bool initialised_
Definition: RawCache.h:66
RawMsgBuf ** msgs_
Definition: RawCache.h:67
void printUsage() const
Definition: RawCache.cc:117
void releaseMsg(unsigned int fuResourceId)
Definition: RawCache.cc:77
static const unsigned int RAW_MESSAGE_TYPE
Definition: msq_constants.h:14
void initialise(unsigned int nMsgs, unsigned int cellSize)
Definition: RawCache.cc:32
void setFree(RawMsgBuf *rawCell)
Definition: RawCache.cc:106
int getFreeSlot() const
Definition: RawCache.cc:99
void initialise(unsigned int rawCellSize)
Definition: RawMsgBuf.cc:18
unsigned int cellSize_
Definition: RawCache.h:68
tuple cout
Definition: gather_cfg.py:121
unsigned int nMsgs_
Definition: RawCache.h:68
static RawCache * instance_
Definition: RawCache.h:60