CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/EventFilter/ResourceBroker/src/RawCache.cc

Go to the documentation of this file.
00001 
00002 //
00003 // RawCache.cc
00004 // -------
00005 //
00006 // Backup for RawMsgBuf messages containing raw FED data.
00007 //
00008 //  Created on: Nov 16, 2011
00009 //                                                                              Andrei Spataru : aspataru@cern.ch
00011 
00012 #include "EventFilter/ResourceBroker/interface/RawCache.h"
00013 #include "EventFilter/ResourceBroker/interface/msq_constants.h"
00014 #include <iostream>
00015 #include <cstdlib>
00016 #include <unistd.h>
00017 
00018 using namespace evf;
00019 using std::cout;
00020 using std::endl;
00021 
00022 RawCache* RawCache::instance_ = 0;
00023 //______________________________________________________________________________
00024 
00025 RawCache* RawCache::getInstance() {
00026         if (instance_ == 0) {
00027                 instance_ = new RawCache();
00028         }
00029         return instance_;
00030 }
00031 
00032 void RawCache::initialise(unsigned int nMsgs, unsigned int cellSize) {
00033         if (!initialised_) {
00034                 // set the desired size of the cache
00035                 nMsgs_ = nMsgs;
00036                 //set the cell size of all cells
00037                 cellSize_ = cellSize;
00038 
00039                 msgs_ = new RawMsgBuf*[nMsgs_];
00040                 // initialise array with flags regarding cache slot usage
00041                 slotUsage_ = new bool[nMsgs_];
00042 
00043                 // set all slots free and initialise
00044                 for (unsigned int i = 0; i < nMsgs_; i++) {
00045                         // not contiguous memory!!
00046                         msgs_[i] = new RawMsgBuf(MAX_MSG_SIZE, RAW_MESSAGE_TYPE);
00047                         msgs_[i]->initialise(cellSize_);
00048 
00049                         slotUsage_[i] = false;
00050                 }
00051                 initialised_ = true;
00052         }
00053 
00054 }
00055 
00056 RawCache::~RawCache() {
00057         delete[] slotUsage_;
00058         delete[] msgs_;
00059 }
00060 
00061 RawMsgBuf* RawCache::getMsgToWrite() {
00062 
00063         int freeSlot = getFreeSlot();
00064 
00065         while (freeSlot == -1) {
00066                 cout << "NO FREE SLOTS IN CACHE!" << endl;
00067                 //improve remove print usage
00068                 printUsage();
00069 		::sleep(1);
00070         }
00071 
00072         slotUsage_[freeSlot] = true;
00073         return msgs_[freeSlot];
00074 
00075 }
00076 
00077 void RawCache::releaseMsg(unsigned int fuResourceId) {
00078         RawMsgBuf* found = 0;
00079 
00080         for (unsigned int i = 0; i < nMsgs_; i++)
00081                 if (slotUsage_[i])
00082                         if (msgs_[i]->rawCell()->fuResourceId() == fuResourceId)
00083                                 found = msgs_[i];
00084 
00085         if (found != 0) {
00086                 found->rawCell()->clear();
00087                 setFree(found);
00088                 //printUsage();
00089         } else
00090                 cout << "RAW MSG BUF corresponding to fuResourceId = " << fuResourceId
00091                                 << "not found in internal allocation list!" << endl;
00092 }
00093 
00094 RawCache::RawCache() :
00095         initialised_(false), nMsgs_(0) {
00096 
00097 }
00098 
00099 int RawCache::getFreeSlot() const {
00100         for (unsigned int i = 0; i < nMsgs_; i++)
00101                 if (!slotUsage_[i])
00102                         return i;
00103         return -1;
00104 }
00105 
00106 void RawCache::setFree(RawMsgBuf* rmb) {
00107         for (unsigned int i = 0; i < nMsgs_; i++)
00108                 if (slotUsage_[i])
00109                         if (msgs_[i] == rmb) {
00110                                 slotUsage_[i] = false;
00111                                 return;
00112                         }
00113         cout << "ERROR: Raw Message Buffer to free at address: " << rmb
00114                         << " not found in internal allocation list!" << endl;
00115 }
00116 
00117 void RawCache::printUsage() const {
00118         cout << "Raw Cache usage: ";
00119         for (unsigned int i = 0; i < nMsgs_; i++)
00120                 cout << slotUsage_[i] << " ";
00121         cout << endl;
00122 }