Statistics
| Branch: | Revision:

napa-baselibs / monl / mon_measure.h @ 5f3adef4

History | View | Annotate | Download (7.69 KB)

1
/***************************************************************************
2
 *   Copyright (C) 2009 by Robert Birke
3
 *   robert.birke@polito.it
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9

10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14

15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
18
 ***********************************************************************/
19

    
20
#ifndef _MON_MEASURE_HH_
21
#define _MON_MEASURE_HH_
22

    
23
#include <vector>
24
#include <bitset>
25
#include <map>
26
#include "mon_parameter.h"
27
#include "measure_plugin.h"
28
#include "ids.h"
29
#include "mon.h"
30
#include <math.h>
31
#include "result_buffer.h"
32
#include "ml.h"
33

    
34
#include "grapes_log.h"
35

    
36
#include <fstream>
37

    
38
/************************** monMeasure ********************************/
39

    
40
/* Measures can be:
41
 * - IN_BAND, OUT_OF_BAND, BOTH
42
 * - PACKET,DATA, BOTH
43
 * This is stored in a bitset called caps. If the bit is set the
44
 * corresponding type is true. flags contians how a particular instance
45
 * is used */
46

    
47
#pragma pack(push)  /* push current alignment to stack */
48
#pragma pack(1)     /* set alignment to 1 byte boundary */
49
struct res_mh_pair {
50
        result res;
51
        MonHandler mh;
52
};
53
#pragma pack(pop)   /* restore original alignment from stack */
54

    
55
typedef std::map<MeasurementId, class MonMeasure*> ExecutionList;
56

    
57
class MonMeasure {
58
protected:
59
        bool auto_loaded;
60

    
61
        /* Bitmask defining the capabilities of this instance of the measure */
62
        MeasurementCapabilities flags;
63

    
64
        /* Pointer to the MeasurePlugin class describing this measure */
65
        class MeasurePlugin *measure_plugin;
66

    
67
        /* Status of the measurement instance */
68
        MeasurementStatus status;
69

    
70
        MonHandler mh_remote;
71

    
72
        uint8_t dst_socketid[SOCKETID_SIZE];
73
        bool dst_socketid_publish;
74
        MsgType  msg_type;
75

    
76
        int used_counter;
77

    
78
        ResultBuffer *rb;
79

    
80
        /* Parameter values */
81
        MonParameterValue *param_values;
82

    
83

    
84
        void debugInit(const char *);
85
        void debugStop();
86
        std::fstream output_file;
87

    
88
        int paramChangeDefault(MonParameterType ph, MonParameterValue p) {
89
                if(ph == P_WINDOW_SIZE && rb != NULL)
90
                        return rb->resizeBuffer((int)p);
91
                if(ph == P_INIT_NAN_ZERO && rb != NULL)
92
                        return rb->init();
93
                if(ph == P_DEBUG_FILE) {
94
                        if(param_values[P_DEBUG_FILE] == p)
95
                                return EOK;
96
                        if(p == 1)
97
                                debugInit(measure_plugin->name.c_str());
98
                        if(p == 0)
99
                                debugStop();
100
                        return EOK;
101
                }
102
                if(ph < P_LAST_DEFAULT_PARAM)
103
                        return EOK;
104
                return paramChange(ph,p);
105
        }
106

    
107
        result *r_rx_list;
108
        result *r_tx_list;
109

    
110
        /* called when the measure is put on an execution list */
111
        /* should be used to reinitialise the measurement in subsequent uses */
112
        virtual void init() {};
113
        virtual void stop() {};
114

    
115

    
116
        friend class MeasureManager;
117
        friend class MeasureDispatcher;
118
        friend class ResultBuffer;
119
public:
120

    
121
        void debugOutput(char *out);
122

    
123
        class MeasureDispatcher *ptrDispatcher;
124
        MonHandler mh_local;
125

    
126
        /* Functions */
127
        /* Constructor: MUST BE CALLED BY DERIVED CLASSES*/
128
        MonMeasure(class MeasurePlugin *mp, MeasurementCapabilities mc, class MeasureDispatcher *ptrDisp);
129

    
130
        virtual ~MonMeasure() {
131
                delete[] param_values;
132
                if(rb != NULL)
133
                        delete rb;
134
        };
135

    
136
        /* Get vector of MonParameterValues (pointers to the MonParameterValue instances) */
137
        std::vector<class MonParameter* >& listParameters(void) {
138
                return measure_plugin->params;
139
        };
140

    
141
        /* Set MonParameterValue value */
142
        int setParameter(size_t pid, MonParameterValue p) {
143
                if(pid < measure_plugin->params.size() && measure_plugin->params[pid]->validator(p) == EOK) {
144
                        MonParameterValue pt;
145
                        pt = param_values[pid];
146
                        param_values[pid] = p;
147
                        if(paramChangeDefault(pid,p) == EOK) {
148
                                return EOK;
149
                        }
150
                        param_values[pid] = pt;
151
                }
152
                return -EINVAL;
153
        };
154

    
155
        /* Get MonParameterValue value */
156
        MonParameterValue getParameter(size_t pid) {
157
                if(pid >= measure_plugin->params.size())
158
                        return NAN;
159
                return param_values[pid];
160
        };
161

    
162
        MeasurementStatus getStatus() {
163
        return status;
164
        }
165

    
166
        /* Get flags */
167
        MeasurementCapabilities getFlags() {
168
                return flags;
169
        }
170

    
171
        /* Set flags */
172
        int setFlags(MeasurementCapabilities f) {
173
                if(f == measure_plugin->getCaps() && f == 0)
174
                        return EOK;
175
                /*check that f is a subset of Caps */
176
                if(f & ~(measure_plugin->getCaps() | TXRXBI | REMOTE))
177
                        return -ERANGE;
178
                /* Check packet vs chunk */
179
                if(!((f & PACKET)^(f & DATA)))
180
                        return -ERANGE;
181
                /* check inband vs out-of-band */
182
                if(f & TIMER_BASED)
183
                        flags |= TIMER_BASED | IN_BAND | OUT_OF_BAND;
184
                else 
185
                        if(!((f & IN_BAND)^(f & OUT_OF_BAND)))
186
                                return -ERANGE;
187
                return EOK;
188
        }
189

    
190
        int setPublishStatisticalType(const char * publishing_name, const char *channel, enum stat_types st[], int length, void *repo_client) {
191
                if(rb == NULL)
192
                {
193
                        info("rb == NULL");
194
                        return -EINVAL;
195
                }
196

    
197
                int result = rb->setPublishStatisticalType(publishing_name, channel, st, length, repo_client);
198

    
199
                return result;
200
        };
201

    
202
        void defaultInit() {
203
                init();
204
                if(param_values[P_DEBUG_FILE] == 1.0)
205
                        debugInit(measure_plugin->name.c_str());
206
        };
207

    
208
        void defaultStop() {
209
                stop();
210
                if(param_values[P_DEBUG_FILE] == 1.0)
211
                        debugStop();
212
        };
213

    
214
        /* called when  changes in parameter values happen */
215
        virtual int paramChange(MonParameterType ph, MonParameterValue p) {return EOK;};
216

    
217

    
218
        void RxPktLocal(ExecutionList *el) {
219
                if(r_rx_list == NULL)
220
                        return;
221
                newSample(RxPkt(r_rx_list, el));
222
        };
223
        void RxPktRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
224
                if(r_rx_list == NULL)
225
                        return;
226
                result r = RxPkt(r_rx_list, el);
227
                if(!isnan(r)) {
228
                        rmp[i].res = r;
229
                        rmp[i].mh = mh_remote;
230
                        i++;
231
                }
232
        };
233

    
234
        void RxDataLocal(ExecutionList *el) {
235
                if(r_rx_list == NULL)
236
                        return;
237
                newSample(RxData(r_rx_list,el));
238
        };
239
        void RxDataRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
240
                if(r_rx_list == NULL)
241
                        return;
242
                result r = RxData(r_rx_list, el);
243
                if(!isnan(r)) {
244
                        rmp[i].res = r;
245
                        rmp[i].mh = mh_remote;
246
                        i++;
247
                }
248
        };
249

    
250
        void TxPktLocal(ExecutionList *el) {
251
                if(r_tx_list == NULL)
252
                        return;
253
                newSample(TxPkt(r_tx_list, el));
254
        };
255
        void TxPktRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
256
                if(r_tx_list == NULL)
257
                        return;
258
                result r = TxPkt(r_tx_list, el);
259
                if(!isnan(r)) {
260
                        rmp[i].res = r;
261
                        rmp[i].mh = mh_remote;
262
                        i++;
263
                }
264
        };
265

    
266
        void TxDataLocal(ExecutionList *el)  {
267
                if(r_tx_list == NULL)
268
                        return;
269
                newSample(TxData(r_tx_list, el));
270
        };
271
        void TxDataRemote(struct res_mh_pair *rmp, int &i, ExecutionList *el) {
272
                if(r_tx_list == NULL)
273
                        return;
274
                result r = TxData(r_tx_list, el);
275
                if(!isnan(r)) {
276
                        rmp[i].res = r;
277
                        rmp[i].mh = mh_remote;
278
                        i++;
279
                }
280
        };
281

    
282
        int newSample(result r) {
283
                if(!isnan(r) && rb != NULL) {
284
                        rb->newSample(r);
285
                        return EOK;
286
                }
287
                else
288
                        return -EINVAL;
289
        };
290

    
291
        //If OOB this function is called upon scheduled event
292
        virtual void Run() {return;};
293
        virtual void receiveOobData(char *buf, int buf_len, result *r) {return;};
294
        int scheduleNextIn(struct timeval *tv);
295
        
296

    
297
        int sendOobData(char* buf, int buf_len);
298

    
299
        /* Functions processing a message (packet and data) and extracting the measure to be stored */
300
        virtual result RxPkt(result *r, ExecutionList *) {return NAN;};
301
        virtual result RxData(result *r, ExecutionList *) {return NAN;};
302
        virtual result TxPkt(result *r, ExecutionList *) {return NAN;};
303
        virtual result TxData(result *r, ExecutionList *) {return NAN;};
304
        
305
        //void OutOfBandcb(HANDLE *event_handle, void *arg) {return;};
306
};
307

    
308
#endif  /* _MON_MEASURE_HH_ */