DABC (Data Acquisition Backbone Core)  2.9.9
MemoryPool.h
Go to the documentation of this file.
1 // $Id: MemoryPool.h 4506 2020-05-26 07:55:57Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_MemoryPool
17 #define DABC_MemoryPool
18 
19 #ifndef DABC_ModuleAsync
20 #include "dabc/ModuleAsync.h"
21 #endif
22 
23 namespace dabc {
24 
25  class Mutex;
26  class MemoryBlock;
27  class MemoryPoolRef;
28 
37  class BufferContainer;
38 
39  class MemoryPool : public ModuleAsync {
40  friend class Manager;
41  friend class Buffer;
42  friend class MemoryPoolRef;
43  friend class BufferContainer;
44 
45  struct RequesterReq {
47  bool pending; // if true, request is pending
48  bool disconn; // if true, requester output is free and can be reused
49 
50  RequesterReq() : size(0), pending(false), disconn(false) {}
51  };
52 
53  protected:
54 
56 
57  unsigned fAlignment;
58 
59  std::vector<RequesterReq> fReqests;
60 
62 
63  bool fEvntFired;
65 
66  unsigned fChangeCounter;
67 
68  bool fUseThread;
69 
70  static unsigned fDfltAlignment;
71  static unsigned fDfltBufSize;
72 
73  virtual bool Find(ConfigIO &cfg);
74 
76  bool TakeRawBuffer(unsigned& indx);
77 
79  void ReleaseRawBuffer(unsigned indx);
80 
82  Buffer _TakeBuffer(BufferSize_t size, bool except, bool reserve_memory = true);
83 
85  bool _Allocate(BufferSize_t bufsize = 0, unsigned number = 0) throw();
86 
89 
92 
94  void IncreaseSegmRefs(MemSegment* segm, unsigned num);
95 
97  void DecreaseSegmRefs(MemSegment* segm, unsigned num);
98 
100  bool IsSingleSegmRefs(MemSegment* segm, unsigned num);
101 
102  virtual void OnThreadAssigned()
103  {
104  fUseThread = HasThread();
106  }
107 
108  bool ProcessSend(unsigned port);
109 
110  virtual void ProcessEvent(const EventId&);
111 
112  virtual int ExecuteCommand(Command cmd);
113 
115  virtual void ProcessConnectionActivated(const std::string &name, bool on);
116 
117  virtual void ProcessConnectEvent(const std::string &name, bool on);
118 
119 
120  bool RecheckRequests(bool from_recv = false);
121 
122  public:
123 
124  MemoryPool(const std::string &name, bool withmanager = false);
125  virtual ~MemoryPool();
126 
127  virtual const char* ClassName() const { return "MemoryPool"; }
128 
129  inline Mutex* GetPoolMutex() const { return ObjectMutex(); }
130 
132  bool IsEmpty() const;
133 
137  bool SetAlignment(unsigned align);
138 
143  bool Allocate(BufferSize_t bufsize = 0, unsigned number = 0) throw();
144 
149  bool Assign(bool isowner, const std::vector<void*>& bufs, const std::vector<unsigned>& sizes) throw();
150 
155  bool _GetPoolInfo(std::vector<void*>& bufs, std::vector<unsigned>& sizes, unsigned* changecnt = 0);
156 
158  bool GetPoolInfo(std::vector<void*>& bufs, std::vector<unsigned>& sizes);
159 
160 
162  bool Release() throw();
163 
167  unsigned GetAlignment() const;
168 
170  unsigned GetMaxNumSegments() const;
171 
173  unsigned GetNumBuffers() const;
174 
176  unsigned GetBufferSize(unsigned id) const;
177 
179  void* GetBufferLocation(unsigned id) const;
180 
182  double GetUsedRatio() const;
183 
185  unsigned GetMaxBufSize() const;
186 
188  unsigned GetMinBufSize() const;
189 
190 
197  Buffer TakeBuffer(BufferSize_t size = 0) throw();
198 
200  bool CheckChangeCounter(unsigned &cnt);
201 
206  bool Reconstruct(Command cmd);
207 
208  // these are static methods to change default configuration for all newly created pools
209 
210  static unsigned GetDfltAlignment() { return fDfltAlignment; }
211  static unsigned GetDfltBufSize() { return fDfltBufSize; }
212 
213  static void SetDfltAlignment(unsigned v) { fDfltAlignment = v; }
214  static void SetDfltBufSize(unsigned v) { fDfltBufSize = v; }
215  };
216 
217 
218  class CmdCreateMemoryPool : public Command {
219 
220  DABC_COMMAND(CmdCreateMemoryPool, "CreateMemoryPool")
221 
222  CmdCreateMemoryPool(const std::string &poolname) : Command(CmdName())
223  {
224  SetStr(xmlPoolName, poolname);
225  }
226 
227  void SetMem(unsigned buffersize, unsigned numbuffers, unsigned align = 0)
228  {
229  if (buffersize*numbuffers != 0) {
230  SetUInt(xmlNumBuffers, numbuffers);
231  SetUInt(xmlBufferSize, buffersize);
232  }
233 
234  if (align) SetUInt(xmlAlignment, align);
235  }
236  };
237 
238  // ________________________________________________________________________________
239 
245  class MemoryPoolRef : public ModuleAsyncRef {
246 
248 
249 
250  double GetUsedRatio() const
251  {
252  return GetObject() ? GetObject()->GetUsedRatio() : 0.;
253  }
254 
256  unsigned GetMaxBufSize() const
257  {
258  return GetObject() ? GetObject()->GetMaxBufSize() : 0;
259  }
260 
262  unsigned GetMinBufSize() const
263  {
264  return GetObject() ? GetObject()->GetMinBufSize() : 0;
265  }
266 
268  {
269  return GetObject() ? GetObject()->TakeBuffer(size) : Buffer();
270  }
271 
273  };
274 
275 }
276 
277 #endif
#define DABC_COMMAND(cmd_class, cmd_name)
Definition: Command.h:282
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Container for data, referenced by Buffer class.
Definition: Buffer.h:69
Reference on memory from memory pool.
Definition: Buffer.h:135
void SetMem(unsigned buffersize, unsigned numbuffers, unsigned align=0)
Definition: MemoryPool.h:227
Represents command with its arguments.
Definition: Command.h:99
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
bool SetUInt(const std::string &name, unsigned v)
Definition: Command.h:147
Interface class between xml configuration and dabc objects.
Definition: ConfigIO.h:38
Manager of everything in DABC
Definition: Manager.h:291
Reference on dabc::MemoryPool class
Definition: MemoryPool.h:245
unsigned GetMaxBufSize() const
Return maximum buffer size in the pool.
Definition: MemoryPool.h:256
double GetUsedRatio() const
Return usage of memory pool.
Definition: MemoryPool.h:250
Reference CreateNewRequester()
Definition: MemoryPool.cxx:696
unsigned GetMinBufSize() const
Return minimum buffer size in the pool.
Definition: MemoryPool.h:262
Buffer TakeBuffer(BufferSize_t size=0)
Definition: MemoryPool.h:267
static unsigned fDfltAlignment
default alignment for memory allocation
Definition: MemoryPool.h:70
std::vector< RequesterReq > fReqests
configuration for each output
Definition: MemoryPool.h:59
virtual void ProcessEvent(const EventId &)
Definition: MemoryPool.cxx:496
virtual ~MemoryPool()
Definition: MemoryPool.cxx:152
Buffer _TakeBuffer(BufferSize_t size, bool except, bool reserve_memory=true)
Central method, which reserves memory from pool and fill structures of buffer.
Definition: MemoryPool.cxx:333
bool _ProcessRequests()
Process submitted requests, if returns true if any requests was processed.
Queue< unsigned, true > fPending
queue with requester indexes which are waiting release of the memory
Definition: MemoryPool.h:61
bool Release()
Release memory and structures, allocated by memory pool.
Definition: MemoryPool.cxx:223
unsigned GetMaxNumSegments() const
Returns number of preallocated segments.
static void SetDfltAlignment(unsigned v)
Definition: MemoryPool.h:213
bool fEvntFired
indicates if event was fired to process memory requests
Definition: MemoryPool.h:63
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: MemoryPool.cxx:646
bool SetAlignment(unsigned align)
Following methods could be used for configuration of pool before memory pool is allocated.
Definition: MemoryPool.cxx:172
bool IsEmpty() const
Return true, if memory pool is not yet created.
Definition: MemoryPool.cxx:237
bool Assign(bool isowner, const std::vector< void * > &bufs, const std::vector< unsigned > &sizes)
This is alternative method to supply memory to the pool.
Definition: MemoryPool.cxx:209
virtual void OnThreadAssigned()
Definition: MemoryPool.h:102
void ReleaseRawBuffer(unsigned indx)
Release raw buffer, allocated before by TakeRawBuffer.
Definition: MemoryPool.cxx:326
bool Reconstruct(Command cmd)
Reconstruct memory pool base on command configuration.
Definition: MemoryPool.cxx:612
virtual void ProcessConnectionActivated(const std::string &name, bool on)
Method called when port started or stopped.
Definition: MemoryPool.cxx:678
unsigned fChangeCounter
memory pool change counter, incremented every time memory is allocated or freed
Definition: MemoryPool.h:66
void IncreaseSegmRefs(MemSegment *segm, unsigned num)
Method increases ref.counuters of all segments.
Definition: MemoryPool.cxx:423
MemoryBlock * fMem
list of preallocated memory
Definition: MemoryPool.h:55
bool _Allocate(BufferSize_t bufsize=0, unsigned number=0)
Method to allocate memory for the pool, mutex should be locked.
Definition: MemoryPool.cxx:187
Mutex * GetPoolMutex() const
Definition: MemoryPool.h:129
bool GetPoolInfo(std::vector< void * > &bufs, std::vector< unsigned > &sizes)
Return pointers and sizes of all memory buffers in the pool.
Definition: MemoryPool.cxx:311
unsigned GetMaxBufSize() const
Return maximum buffer size in the pool.
Definition: MemoryPool.cxx:258
bool CheckChangeCounter(unsigned &cnt)
Check if memory pool structure was changed since last call, do not involves memory pool mutex.
Definition: MemoryPool.cxx:603
bool IsSingleSegmRefs(MemSegment *segm, unsigned num)
Return true when all segments has refcnt==1.
Definition: MemoryPool.cxx:442
unsigned GetBufferSize(unsigned id) const
Returns size of preallocated buffer.
Definition: MemoryPool.cxx:251
virtual const char * ClassName() const
Returns class name of the object.
Definition: MemoryPool.h:127
bool TakeRawBuffer(unsigned &indx)
Reserve raw buffer without creating Buffer instance.
Definition: MemoryPool.cxx:318
unsigned fAlignment
alignment boundary for memory
Definition: MemoryPool.h:57
double GetUsedRatio() const
Return relative usage of memory pool buffers.
Definition: MemoryPool.cxx:629
bool Allocate(BufferSize_t bufsize=0, unsigned number=0)
Allocates memory for the memory pool and creates references.
Definition: MemoryPool.cxx:203
bool _GetPoolInfo(std::vector< void * > &bufs, std::vector< unsigned > &sizes, unsigned *changecnt=0)
Return pointers and sizes of all memory buffers in the pool Could be used by devices and transport to...
Definition: MemoryPool.cxx:294
Buffer TakeBuffer(BufferSize_t size=0)
Returns Buffer object with exclusive access rights.
Definition: MemoryPool.cxx:409
static unsigned GetDfltAlignment()
Definition: MemoryPool.h:210
bool fProcessingReq
flag indicate if memory pool processing requests, for debug purposes
Definition: MemoryPool.h:64
virtual bool Find(ConfigIO &cfg)
Method to locate object in xml file.
Definition: MemoryPool.cxx:157
static void SetDfltBufSize(unsigned v)
Definition: MemoryPool.h:214
unsigned GetMinBufSize() const
Return minimum buffer size in the pool.
Definition: MemoryPool.cxx:272
unsigned GetAlignment() const
Following methods should be used after memory pool is created.
Definition: MemoryPool.cxx:181
bool RecheckRequests(bool from_recv=false)
Definition: MemoryPool.cxx:513
unsigned GetNumBuffers() const
Returns number of preallocated buffers.
Definition: MemoryPool.cxx:244
MemoryPool(const std::string &name, bool withmanager=false)
Definition: MemoryPool.cxx:137
void DecreaseSegmRefs(MemSegment *segm, unsigned num)
Decrease references of specified segments.
Definition: MemoryPool.cxx:462
bool ProcessSend(unsigned port)
Method called by framework when at least one buffer can be send to output port.
Definition: MemoryPool.cxx:483
void ReplyReadyRequests()
Inform requesters, that buffer is provided.
virtual void ProcessConnectEvent(const std::string &name, bool on)
Method called by framework when connection state of the item is changed.
Definition: MemoryPool.cxx:686
static unsigned fDfltBufSize
default buffer size
Definition: MemoryPool.h:71
void * GetBufferLocation(unsigned id) const
Returns location of preallocated buffer.
Definition: MemoryPool.cxx:287
static unsigned GetDfltBufSize()
Definition: MemoryPool.h:211
bool fUseThread
indicate if thread functionality should be used to process supplied requests
Definition: MemoryPool.h:68
Reference on dabc::ModuleAsync class
Definition: ModuleAsync.h:201
Base class for user-derived code, implementing event-processing.
Definition: ModuleAsync.h:32
virtual void OnThreadAssigned()
Definition: Module.cxx:79
posix pthread mutex
Definition: threads.h:61
Mutex * ObjectMutex() const
Returns mutex, used for protection of Object data members.
Definition: Object.h:190
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
bool HasThread() const
Indicates if pointer on thread is not zero; thread-safe.
Definition: Worker.cxx:151
Event manipulation API.
Definition: api.h:23
const char * xmlBufferSize
Definition: Object.cxx:49
uint32_t BufferSize_t
Definition: Buffer.h:32
const char * xmlAlignment
Definition: Object.cxx:51
const char * xmlNumBuffers
Definition: Object.cxx:50
const char * xmlPoolName
Definition: Object.cxx:45
Event structure, exchanged between DABC threads.
Definition: Thread.h:70
Structure with descriptor of single memory segment.
Definition: Buffer.h:53