GemaCoreLib
The GeMA Core library
gmMemoryDump.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_MEMORY_DUMP_H_
25 #define _GEMA_MEMORY_DUMP_H_
26 
27 #include "gmCoreConfig.h"
28 #include "gmLog.h"
29 
30 #include <QString>
31 #include <QVector>
32 #include <QByteArray>
33 
34 class QFile;
35 
38 {
43 };
44 
46 #define GM_MEMORY_DUMP_ITEM_NAME_SIZE 80
47 
49 #define GM_MEMORY_DUMP_ITEM_DATA_SIZE 16
50 
52 #define GM_MEMORY_DUMP_MIN_COMPRESSION (4 * 1024)
53 
59 {
60 public:
62 
63  void addBuffer(char* buffer, qint64 size);
64 
66  qint64 size() const { return _totalSize; }
67 
68 private:
69  Q_DISABLE_COPY(GmMemoryBufferList)
70 
71  friend class GmMemoryDump; // Our private members are for the benefit of GmMemoryDump only :)
72 
74  bool isDone() const { return _bufIndex == _bufferList.size() && _bufOffset == 0; }
75 
76  bool writeDataToFile (QFile* file, qint64 size);
77  bool readDataFromFile(QFile* file, qint64 size);
78  void writeDataToMem (char* ptr, qint64 size);
79  void readDataFromMem (char* ptr, qint64 size);
80 
82  qint64 _totalSize;
83  int _bufIndex;
84  qint64 _bufOffset;
85 };
86 
100 {
101 public:
103  virtual ~GmMemoryDumpItem() {}
104 
108  virtual QString itemName() const = 0;
109 
111  virtual int itemType() const = 0;
112 
114  virtual bool canCompress() const = 0;
115 
117  virtual bool partialOp() const = 0;
118 
123  virtual bool fixedSize() const = 0;
124 
132  virtual bool opStart(int op) { Q_UNUSED(op); return true; }
133 
141  virtual bool opEnd(int op) { Q_UNUSED(op); return true; }
142 
144  virtual qint64 size() const = 0;
145 
158  virtual bool fillDataHeader(char* header) = 0;
159 
178  virtual void getDataBuffer(GmMemoryBufferList& bufferList, qint64 size = 0, qint64 offset = 0) = 0;
179 
202  virtual bool setDataBuffer(GmMemoryBufferList& bufferList, const char* header, qint64 size, qint64 offset = 0) = 0;
203 
217  virtual void bufferReleased(int op, bool ok) { Q_UNUSED(op); Q_UNUSED(ok); }
218 };
219 
222 {
223 public:
224  GmMemoryDump(GmMemoryDumpMode mode, const GmLogCategory& logger, int compression = 0,
225  int compressionMinSize = GM_MEMORY_DUMP_MIN_COMPRESSION,
226  double dumpSizeA = 1.0, unsigned dumpSizeB = 0);
227 
228  virtual ~GmMemoryDump();
229 
233  void setPrintStructure(bool mode) { _printStructure = mode; }
234 
235  int addDumpItem(GmMemoryDumpItem* item, int groupId = -1);
236 
238  GmMemoryDumpItem* item(int itemId) const
239  {
240  assert(itemId >= 0 && itemId < _dumpItems.size());
241  return _dumpItems[itemId]->_item;
242  }
243 
244  int findItem(QString name, int typeMask = 0, int typeValue = 0, int groupId = -1, int start = 0);
245 
247  int itemGroup(int itemId) const
248  {
249  assert(itemId >= 0 && itemId < _dumpItems.size());
250  assert(_dumpItems[itemId]->_data);
251  return _dumpItems[itemId]->_data->_groupId;
252  }
253 
255  int numItems() const { return _dumpItems.size(); }
256 
257  void setItemActive (int itemId, bool active);
258  void setGroupActive(int groupId, bool active);
259 
261  bool isItemActive(int itemId)
262  {
263  assert(itemId >= 0 && itemId < _dumpItems.size());
264  return _dumpItems[itemId]->_active;
265  }
266 
267  bool init(QString dumpFile, bool overwrite, bool keep);
268  bool init();
269 
270  bool save();
271  bool load();
272 
273  bool saveItem(int itemId, qint64 size = 0, qint64 offset = 0);
274  bool loadItem(int itemId, qint64 size = 0, qint64 offset = 0);
275 
276  bool saveActiveItems();
277  bool loadActiveItems();
278 
279  void flush();
280  void clear();
281 
283  int adjustGroupId(int id) const { assert(id >= -2); return id == -1 ? _nextGroupId : (id == -2 ? _currentGroupId : id); }
284 
285  static const char* modeToStr(GmMemoryDumpMode mode);
286  static int strToMode(const QString& str);
287 
288 private:
289  Q_DISABLE_COPY(GmMemoryDump)
290 
291 
293  {
294  char _magic[4];
295  qint32 _version;
296  qint64 _infoBlockPos;
297  };
298 
301  {
302  FileDataChunkHeader() { _dummy = 0; }
303 
304  qint32 _blockId;
305  qint32 _chunkId;
306  qint64 _chunkSize;
307  qint64 _nextChunkPos;
308  qint64 _dummy;
309  };
310 
313  {
314  FileInfoBlockHeader() { memset(_dummy, 0, sizeof(_dummy)); }
315 
316  qint32 _numItems;
318  char _dummy[8];
319  };
320 
323  {
326  qint32 _itemType;
327  qint16 _groupId;
328  qint16 _compressed;
329  qint64 _dataSize;
330  qint64 _dumpSize;
331  qint64 _dataBlockPos;
332  };
333 
334  // Ensure that structures have a size that is a multiple of 16 bytes.
335  static_assert(sizeof(FileControlHeader) % 16 == 0, "Unexpected structure size");
336  static_assert(sizeof(FileDataChunkHeader) % 16 == 0, "Unexpected structure size");
337  static_assert(sizeof(FileInfoBlockHeader) % 16 == 0, "Unexpected structure size");
338  static_assert(sizeof(FileItemHeader) % 16 == 0, "Unexpected structure size");
339 
341  struct ItemHeader
342  {
344  ItemHeader(qint32 id, qint16 groupId)
345  {
346  _blockId = id;
347  _groupId = groupId;
348  _compressed = 0;
349  _dataSize = 0;
350  _dumpSize = 0;
351  _dataHeaderPos = 0;
352  _dataBlockPos = 0;
353  memset(_itemData, 0, GM_MEMORY_DUMP_ITEM_DATA_SIZE);
354  }
355 
356  qint32 _blockId;
357  qint16 _groupId;
358  qint16 _compressed;
359  qint64 _dataSize;
360  qint64 _dumpSize;
361  qint64 _dataHeaderPos;
362  qint64 _dataBlockPos;
365  };
366 
368  struct DumpItem
369  {
371  DumpItem(GmMemoryDumpItem* item, qint32 id, qint16 groupId) { _item = item; _data = new ItemHeader(id, groupId); _active = true; }
372 
374  ~DumpItem() { delete _item; delete _data; }
375 
378  bool _active;
379  };
380 
381  bool fileInit(QString dumpFile, bool overwrite, bool keep);
382  bool fileInitNew();
383  bool fileInitExisting();
384  bool memoryInit();
385 
386  void fileClose();
387 
388  bool fileSaveItem (DumpItem* item, bool update, qint64 size = 0, qint64 offset = 0);
389  bool memorySaveItem(DumpItem* item, qint64 size = 0, qint64 offset = 0);
390 
391  bool fileLoadItem (DumpItem* item, qint64 size = 0, qint64 offset = 0);
392  bool memoryLoadItem(DumpItem* item, qint64 size = 0, qint64 offset = 0);
393 
394  bool writeOrCheckControlHeader(bool write, qint64* infoBlockPos);
395  bool saveDataBlock (qint32 blockId, qint64 blockSize, const char* buffer, qint64 size);
396  bool saveDataChunck(const FileDataChunkHeader* h, const char* buffer, qint64 size);
397 
398  bool saveChunkHeader (const FileDataChunkHeader* h);
399  bool saveChunkPadding(const FileDataChunkHeader* h, qint64 dataSize);
400 
401  bool loadChunkHeader(FileDataChunkHeader* h);
402  bool loadChunkData (char* buffer, qint64 offset, qint64 size);
403 
404  bool updateFileInfoHeader();
405  bool updateFileItemHeader(const ItemHeader* h, const GmMemoryDumpItem* item);
406 
407  bool readData (char* buffer, qint64 size);
408  bool readData (GmMemoryBufferList& blist, qint64 size);
409  bool writeData(const char* buffer, qint64 size);
410  bool writeData(GmMemoryBufferList& blist, qint64 size);
411 
412  qint64 blockSize(qint64 dataSize, bool grow);
413 
414 #if defined ENABLE_TESTS
415  // Allows test function to call checkAndPrintFileStructure()
416  friend Q_DECL_EXPORT void TEST_GmMemoryDumpCheck(GmMemoryDump*, bool);
417  void checkAndPrintFileStructure(bool print, bool saved);
418 #endif
419 
423  double _dumpSizeA;
424  unsigned _dumpSizeB;
426  bool _keepFile;
428 
430  bool _init;
431  qint64 _fileInfoPos;
432  qint64 _firstChunkPos;
437 
439 };
440 
441 #endif
int _currentGroupId
The id of the last added group id.
Definition: gmMemoryDump.h:435
bool isDone() const
Check that all data data on the buffer has been traversed by read or write functions.
Definition: gmMemoryDump.h:74
unsigned _dumpSizeB
Offset for extra grow size added to a data item size.
Definition: gmMemoryDump.h:424
#define GM_MEMORY_DUMP_ITEM_NAME_SIZE
The maximum size for a dump item name.
Definition: gmMemoryDump.h:46
virtual bool opEnd(int op)
Function called by GmMemoryDump::load(), GmMemoryDump::save(), GmMemoryDump::loadItem() and GmMemoryD...
Definition: gmMemoryDump.h:141
qint32 _chunkId
Sequential index of the chunk inside the block. Frst chunk has id 0.
Definition: gmMemoryDump.h:305
QVarLengthArray< QPair< char *, qint64 >, 5 > _bufferList
A list with buffers and their sizes.
Definition: gmMemoryDump.h:81
Dumped memory is saved to a file.
Definition: gmMemoryDump.h:41
bool _keepFile
Should we keep dump files after the object is destroyed?
Definition: gmMemoryDump.h:426
int adjustGroupId(int id) const
Returns the adjusted group id, translating the -1 (next group) or -2 (current group) options for addD...
Definition: gmMemoryDump.h:283
The file header for each data chunk.
Definition: gmMemoryDump.h:300
void setPrintStructure(bool mode)
Enables or disables printing the dump item structure, both when addDumpItem() is called and when a fi...
Definition: gmMemoryDump.h:233
double _dumpSizeA
Multiplier for extra grow size added to a data item size.
Definition: gmMemoryDump.h:423
The in memory control header for every data item.
Definition: gmMemoryDump.h:341
Declaration of usefull configuration definitions for the Core library.
GmMemoryDumpMode _mode
Dump mode.
Definition: gmMemoryDump.h:420
A helper class that can store a list of buffers and their sizes, allowing for GmMemoryDumpItem instan...
Definition: gmMemoryDump.h:58
#define GM_MEMORY_DUMP_ITEM_DATA_SIZE
The size of the per item fixed item data buffer.
Definition: gmMemoryDump.h:49
qint32 _blockId
The block id used for this item.
Definition: gmMemoryDump.h:356
qint32 _itemType
An integer with the data type id.
Definition: gmMemoryDump.h:326
The file header for every data item.
Definition: gmMemoryDump.h:322
ItemHeader * _data
The item control header.
Definition: gmMemoryDump.h:377
QFile * _file
The dump file (if the current mode so requires)
Definition: gmMemoryDump.h:429
virtual void bufferReleased(int op, bool ok)
Function called once after a call to getDataBuffer() / setDataBuffer() to inform the item data that t...
Definition: gmMemoryDump.h:217
qint32 _lastChunkBlockId
The block Id for the last chunk stored in the file.
Definition: gmMemoryDump.h:433
DumpItem(GmMemoryDumpItem *item, qint32 id, qint16 groupId)
Dump item constructor.
Definition: gmMemoryDump.h:371
GmMemoryDumpItem * item(int itemId) const
Returns the dump item, given the id returned by addDumpItem()
Definition: gmMemoryDump.h:238
#define GM_MEMORY_DUMP_MIN_COMPRESSION
The default minimum buffer size in bytes for compression to be worthy (must be > 0)
Definition: gmMemoryDump.h:52
qint16 _compressed
Was this item compressed?
Definition: gmMemoryDump.h:358
qint64 _nextChunkPos
The file offset for the next chunk of this data block (0 if this is the last chunk)
Definition: gmMemoryDump.h:307
qint64 _dumpSize
The (possibly compressed) size in bytes of the item data on the dump.
Definition: gmMemoryDump.h:330
qint16 _compressed
Was this item data compressed?
Definition: gmMemoryDump.h:328
int itemGroup(int itemId) const
Returns the group id of the given item.
Definition: gmMemoryDump.h:247
GmMemoryDumpMode
Supported memory dump modes.
Definition: gmMemoryDump.h:37
Dumped file is opend for restore only. No save operations are allowed.
Definition: gmMemoryDump.h:42
Dumped memory is saved on memory.
Definition: gmMemoryDump.h:40
int _nextGroupId
The id of the next item group id (can be different from _currentGroupId+1 if the user explicitly gave...
Definition: gmMemoryDump.h:436
virtual bool opStart(int op)
Function called by GmMemoryDump::load(), GmMemoryDump::save(), GmMemoryDump::loadItem() and GmMemoryD...
Definition: gmMemoryDump.h:132
int numItems() const
Returns the number of added dump items.
Definition: gmMemoryDump.h:255
qint64 _dataBlockPos
The position in the file for the first data chunk of this item (0 if not saved or memory dumps)
Definition: gmMemoryDump.h:362
bool _init
Check if init was called successfully.
Definition: gmMemoryDump.h:430
qint64 size() const
Returns the sum of the sizes of the stored buffers.
Definition: gmMemoryDump.h:66
qint16 _groupId
The item group id.
Definition: gmMemoryDump.h:327
qint64 _chunkSize
The size in bytes for this chunk data.
Definition: gmMemoryDump.h:306
void print(const GmMatrix &m, const GmLogCategory &logger, GmLogLevel level, int fieldWidth, char format, int precision)
Prints the matrix using the specified logger, level and precision fields.
Definition: gmMatrixUtils.cpp:34
const GmLogCategory & _logger
Basic logger object for dump messages.
Definition: gmMemoryDump.h:438
qint32 _lastChunkBlockId
The block Id for the last chunk stored in the file.
Definition: gmMemoryDump.h:317
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
bool _active
Is this item active or not? Used by saveActiveItems() / loadActiveItems()
Definition: gmMemoryDump.h:378
The file structure with file control info.
Definition: gmMemoryDump.h:292
int _bufIndex
The current buffer for the next operation.
Definition: gmMemoryDump.h:83
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
Auto mode. Dumped memory is saved either on file or on memory depending on memory usage.
Definition: gmMemoryDump.h:39
qint32 _numItems
The number of stored dump items in the file.
Definition: gmMemoryDump.h:316
qint64 _dataSize
The (uncompressed) size in bytes of the item data (0 if the data was not saved)
Definition: gmMemoryDump.h:329
The structure adding controll information to a GmMemoryDumpItem.
Definition: gmMemoryDump.h:368
bool _printStructure
Should we print the configured item structure?
Definition: gmMemoryDump.h:427
qint32 _blockId
Block id. Every chunk belonging to the same data item must share the same block id.
Definition: gmMemoryDump.h:304
QString _fileName
Dump file name.
Definition: gmMemoryDump.h:425
qint64 _totalSize
The sum of the buffers sizes on _bufferList.
Definition: gmMemoryDump.h:82
qint64 _bufOffset
The current buffer offset for the next operation.
Definition: gmMemoryDump.h:84
int _compression
Compression level for dumped data. From -1 to 9. 0 = no, 9 = max, -1 = zlib default.
Definition: gmMemoryDump.h:421
GmMemoryDumpItem * _item
The dump item.
Definition: gmMemoryDump.h:376
~DumpItem()
Dump item destructor. Deletes the stored item data object along with other control data.
Definition: gmMemoryDump.h:374
qint64 _dataBlockPos
The offset of the block data's first chunk on file (0 if the data was not saved)
Definition: gmMemoryDump.h:331
qint64 _dataSize
The (uncompressed) size in bytes of the item data.
Definition: gmMemoryDump.h:359
qint64 _dummy
Align the structure to a 16 bytes size. Should be 0.
Definition: gmMemoryDump.h:308
qint64 _dataHeaderPos
The position in the file for this item header (0 for memory dumps)
Definition: gmMemoryDump.h:361
qint16 _groupId
The item group id.
Definition: gmMemoryDump.h:357
QVector< DumpItem * > _dumpItems
A list with the set of dump items + their controll info.
Definition: gmMemoryDump.h:434
qint64 _infoBlockPos
The file offset for the Info block data.
Definition: gmMemoryDump.h:296
int _minCompressionSize
The minumum size a buffer must have to be compressed.
Definition: gmMemoryDump.h:422
The structure storing control info for an Info block.
Definition: gmMemoryDump.h:312
bool isItemActive(int itemId)
Returns true if the given item is active.
Definition: gmMemoryDump.h:261
qint64 _fileInfoPos
The position on the file for the Info block header.
Definition: gmMemoryDump.h:431
qint32 _version
The current file version.
Definition: gmMemoryDump.h:295
QByteArray _memData
The item data when dumping to memory. Not a direct pointer since qCompress() returns a byte array and...
Definition: gmMemoryDump.h:364
virtual ~GmMemoryDumpItem()
Virtual destructor.
Definition: gmMemoryDump.h:103
Interface for an object that can be dumped and undumped to either file or memory by GmMemoryDump.
Definition: gmMemoryDump.h:99
qint64 _firstChunkPos
The position in the dump file for the first dump item chunk.
Definition: gmMemoryDump.h:432
qint64 _dumpSize
The (possibly compressed) size in bytes of the item data.
Definition: gmMemoryDump.h:360
Class storing a memory dump containing several dump items.
Definition: gmMemoryDump.h:221
ItemHeader(qint32 id, qint16 groupId)
Constructor.
Definition: gmMemoryDump.h:344
Declaration of support functions and macros for information logging.