Statistics
| Branch: | Revision:

root / src / envir / indexedfileoutvectormgr.h @ 68da4f12

History | View | Annotate | Download (4.48 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  INDEXEDFILEOUTVECTORMGR.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Declaration of the following classes:
7
//     cIndexedFileOutputVectorManager
8
//
9
//  Author: Tamas Borbely
10
//
11
//==========================================================================
12
13
/*--------------------------------------------------------------*
14
  Copyright (C) 1992-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20
21
#ifndef __INDEXEDFILEOUTVECTORMGR_H
22
#define __INDEXEDFILEOUTVECTORMGR_H
23
24
#include <stdio.h>
25
#include <float.h>
26
#include <deque>
27
#include "platmisc.h"
28
#include "envirext.h"
29
#include "simutil.h"
30
#include "fileoutvectormgr.h"
31
32
NAMESPACE_BEGIN
33
34
#define MIN_BUFFER_MEMORY    (1024*1024)
35
#define DEFAULT_MEMORY_LIMIT "16MB"
36
37
/**
38
 * cFileOutputManager that writes vector data in chunks and
39
 * generate an index file for the output.
40
 *
41
 * @ingroup Envir
42
 */
43
class ENVIR_API cIndexedFileOutputVectorManager : public cFileOutputVectorManager
44
{
45
  protected:
46
    struct sBlock {
47
      file_offset_t offset; // file offset of the block
48
      file_offset_t size;   // size of the block
49
      eventnumber_t startEventNum; // event number of the first sample in the block
50
      eventnumber_t endEventNum;   // event number of the last sample in the block
51
      simtime_t startTime;  // simulation time of the first sample in the block
52
      simtime_t endTime;    // simulation time of the last sample in the block
53
      long count;           // count of samples in the block
54
      double min;           // minimum value of the samples
55
      double max;           // maximum value of the samples
56
      double sum;           // sum of values of the samples
57
      double sumSqr;        // sum of squares of values
58
59
      sBlock() { reset(); }
60
      void reset() { offset=-1; size=0; count=0; min=DBL_MAX; max=-DBL_MAX; sum=0.0; sumSqr=0.0; }
61
    };
62
63
    typedef std::vector<sBlock> Blocks;
64
65
    struct sSample {
66
        simtime_t simtime;
67
        eventnumber_t eventNumber;
68
        double value;
69
70
        sSample(simtime_t t, eventnumber_t eventNumber, double val) : simtime(t), eventNumber(eventNumber), value(val) {}
71
    };
72
73
    typedef std::vector<sSample> Samples;
74
75
    struct sVector : sVectorData {
76
      std::vector<sSample> buffer; // buffer holding recorded data not yet written to the file
77
      long maxBufferedSamples;     // maximum number of samples gathered in the buffer
78
      sBlock currentBlock;
79
80
      sVector() : buffer(), maxBufferedSamples(0) {}
81
      void allocateBuffer(long count) { buffer.reserve(count); }
82
    };
83
84
    typedef std::vector<sVector*> Vectors;
85
86
    opp_string ifname;  // index file name
87
    FILE *fi;           // file ptr of index file
88
    size_t maxMemoryUsed;
89
    size_t memoryUsed;
90
    Vectors vectors; // registered output vectors
91
92
  protected:
93
    virtual sVectorData *createVectorData();
94
    void openIndexFile();
95
    void closeIndexFile();
96
    virtual void initVector(sVectorData *vp);
97
    virtual void finalizeVector(sVector *vector);
98
    virtual void writeRunData();
99
    virtual void writeRecords();
100
    virtual void writeBlock(sVector *vector);
101
    virtual void writeBlockToIndexFile(sVector *vector);
102
  public:
103
    /** @name Constructors, destructor */
104
    //@{
105
106
    /**
107
     * Constructor.
108
     */
109
    explicit cIndexedFileOutputVectorManager();
110
    //@}
111
112
    /** @name Redefined cOutputVectorManager member functions. */
113
    //@{
114
115
    /**
116
     * Deletes output vector  and index files if exists (left over from previous runs).
117
     * The file is not yet opened, it is done inside registerVector() on demand.
118
     */
119
    virtual void startRun();
120
121
    /**
122
     * Writes out last chunk of registered output vectors and generates index entries for them.
123
     * Closes the output and index files.
124
     */
125
    virtual void endRun();
126
127
    /**
128
     * Registers a vector and returns a handle.
129
     */
130
    virtual void *registerVector(const char *modulename, const char *vectorname);
131
132
    /**
133
     * Deregisters the output vector.
134
     */
135
    virtual void deregisterVector(void *vechandle);
136
137
    /**
138
     * Writes the (time, value) pair into the buffer.
139
     * When the buffer is full, writes it out into the file.
140
     */
141
    virtual bool record(void *vectorhandle, simtime_t t, double value);
142
    //@}
143
};
144
145
NAMESPACE_END
146
147
148
#endif